




import subprocess
import sys

from transformers import BertConfig, BertModel, BertTokenizer, pipeline
from transformers.testing_utils import TestCasePlus, require_torch
class 		UpperCAmelCase   (				UpperCAmelCase__			):







						'''simple docstring'''





						@require_torch
						def        snake_case__						(						self							:     Union[str, Any]						):






							"""simple docstring"""






							snake_case_		    =     "\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n        "

							snake_case_		    =     "\nmname = \"hf-internal-testing/tiny-random-bert\"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task=\"fill-mask\", model=mname)\nprint(\"success\")\n        "

							snake_case_		    =     "\nimport socket\ndef offline_socket(*args, **kwargs): raise RuntimeError(\"Offline mode is enabled, we shouldn't access internet\")\nsocket.socket = offline_socket\n        "

							# Force fetching the files so that we can use the cache
							snake_case_		    =     "hf-internal-testing/tiny-random-bert"
							BertConfig.from_pretrained(__lowercase						)
							BertModel.from_pretrained(__lowercase						)
							BertTokenizer.from_pretrained(__lowercase						)
							pipeline(task="fill-mask"       ,	model=__lowercase						)

							# baseline - just load from_pretrained with normal network
							snake_case_		    =     [sys.executable, "-c", "\n".join([load, run, mock]						)]

							# should succeed
							snake_case_		    =     self.get_env()
							# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
							snake_case_		    =     "1"
							snake_case_		    =     subprocess.run(__lowercase       ,	env=__lowercase       ,	check=__lowercase       ,	capture_output=__lowercase						)
							self.assertEqual(result.returncode       ,	0       ,	result.stderr						)
							self.assertIn("success"       ,	result.stdout.decode()						)






						@require_torch
						def        snake_case__						(						self							:     Tuple						):






							"""simple docstring"""






							snake_case_		    =     "\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n        "

							snake_case_		    =     "\nmname = \"hf-internal-testing/tiny-random-bert\"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task=\"fill-mask\", model=mname)\nprint(\"success\")\n        "

							snake_case_		    =     "\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error(\"Faking flaky internet\")\nsocket.socket = offline_socket\n        "

							# Force fetching the files so that we can use the cache
							snake_case_		    =     "hf-internal-testing/tiny-random-bert"
							BertConfig.from_pretrained(__lowercase						)
							BertModel.from_pretrained(__lowercase						)
							BertTokenizer.from_pretrained(__lowercase						)
							pipeline(task="fill-mask"       ,	model=__lowercase						)

							# baseline - just load from_pretrained with normal network
							snake_case_		    =     [sys.executable, "-c", "\n".join([load, run, mock]						)]

							# should succeed
							snake_case_		    =     self.get_env()
							snake_case_		    =     subprocess.run(__lowercase       ,	env=__lowercase       ,	check=__lowercase       ,	capture_output=__lowercase						)
							self.assertEqual(result.returncode       ,	0       ,	result.stderr						)
							self.assertIn("success"       ,	result.stdout.decode()						)






						@require_torch
						def        snake_case__						(						self							:     Optional[int]						):






							"""simple docstring"""






							snake_case_		    =     "\nfrom transformers import BertConfig, BertModel, BertTokenizer\n        "

							snake_case_		    =     "\nmname = \"hf-internal-testing/tiny-random-bert-sharded\"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nprint(\"success\")\n        "

							snake_case_		    =     "\nimport socket\ndef offline_socket(*args, **kwargs): raise ValueError(\"Offline mode is enabled\")\nsocket.socket = offline_socket\n        "

							# baseline - just load from_pretrained with normal network
							snake_case_		    =     [sys.executable, "-c", "\n".join([load, run]						)]

							# should succeed
							snake_case_		    =     self.get_env()
							snake_case_		    =     subprocess.run(__lowercase       ,	env=__lowercase       ,	check=__lowercase       ,	capture_output=__lowercase						)
							self.assertEqual(result.returncode       ,	0       ,	result.stderr						)
							self.assertIn("success"       ,	result.stdout.decode()						)

							# next emulate no network
							snake_case_		    =     [sys.executable, "-c", "\n".join([load, mock, run]						)]

							# Doesn't fail anymore since the model is in the cache due to other tests, so commenting this.
							# env["TRANSFORMERS_OFFLINE"] = "0"
							# result = subprocess.run(cmd, env=env, check=False, capture_output=True)
							# self.assertEqual(result.returncode, 1, result.stderr)

							# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
							snake_case_		    =     "1"
							snake_case_		    =     subprocess.run(__lowercase       ,	env=__lowercase       ,	check=__lowercase       ,	capture_output=__lowercase						)
							self.assertEqual(result.returncode       ,	0       ,	result.stderr						)
							self.assertIn("success"       ,	result.stdout.decode()						)






						@require_torch
						def        snake_case__						(						self							:     Tuple						):






							"""simple docstring"""






							snake_case_		    =     "\nfrom transformers import pipeline\n        "
							snake_case_		    =     "\nmname = \"hf-internal-testing/tiny-random-bert\"\npipe = pipeline(model=mname)\n        "

							snake_case_		    =     "\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error(\"Offline mode is enabled\")\nsocket.socket = offline_socket\n        "
							snake_case_		    =     self.get_env()
							snake_case_		    =     "1"
							snake_case_		    =     [sys.executable, "-c", "\n".join([load, mock, run]						)]
							snake_case_		    =     subprocess.run(__lowercase       ,	env=__lowercase       ,	check=__lowercase       ,	capture_output=__lowercase						)
							self.assertEqual(result.returncode       ,	1       ,	result.stderr						)
							self.assertIn(
							    "You cannot infer task automatically within `pipeline` when using offline mode"       ,	result.stderr.decode().replace("\n"       ,	""						)       ,	)






						@require_torch
						def        snake_case__						(						self							:     Tuple						):






							"""simple docstring"""






							snake_case_		    =     "\nfrom transformers import AutoModel\n        "
							snake_case_		    =     "\nmname = \"hf-internal-testing/test_dynamic_model\"\nAutoModel.from_pretrained(mname, trust_remote_code=True)\nprint(\"success\")\n        "

							# baseline - just load from_pretrained with normal network
							snake_case_		    =     [sys.executable, "-c", "\n".join([load, run]						)]

							# should succeed
							snake_case_		    =     self.get_env()
							snake_case_		    =     subprocess.run(__lowercase       ,	env=__lowercase       ,	check=__lowercase       ,	capture_output=__lowercase						)
							self.assertEqual(result.returncode       ,	0       ,	result.stderr						)
							self.assertIn("success"       ,	result.stdout.decode()						)

							# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
							snake_case_		    =     "1"
							snake_case_		    =     subprocess.run(__lowercase       ,	env=__lowercase       ,	check=__lowercase       ,	capture_output=__lowercase						)
							self.assertEqual(result.returncode       ,	0       ,	result.stderr						)
							self.assertIn("success"       ,	result.stdout.decode()						)






# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from argparse import ArgumentParser

from accelerate.commands.config import get_config_parser
from accelerate.commands.env import env_command_parser
from accelerate.commands.launch import launch_command_parser
from accelerate.commands.test import test_command_parser
from accelerate.commands.tpu import tpu_command_parser


def 						lowerCamelCase__    (		):







	'''simple docstring'''





	snake_case_		    =     ArgumentParser("Accelerate CLI tool"     ,	usage="accelerate <command> [<args>]"     ,	allow_abbrev=_A							)
	snake_case_		    =     parser.add_subparsers(help="accelerate command helpers"							)

	# Register commands
	get_config_parser(subparsers=_A							)
	env_command_parser(subparsers=_A							)
	launch_command_parser(subparsers=_A							)
	tpu_command_parser(subparsers=_A							)
	test_command_parser(subparsers=_A							)

	# Let's go
	snake_case_		    =     parser.parse_args()

	if not hasattr(_A     ,	"func"							):
		parser.print_help()
		exit(1							)

	# Run
	args.func(_A							)


if __name__ == "__main__":
		main()
