


import importlib
import json
import os
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
import transformers.models.auto
from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig
from transformers.models.bert.configuration_bert import BertConfig
from transformers.models.roberta.configuration_roberta import RobertaConfig
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


sys.path.append(str(Path(__file__).parent.parent.parent.parent / '''utils'''))

from test_module.custom_configuration import CustomConfig  # noqa E402


snake_case							:	Tuple					      =				get_tests_dir('''fixtures/dummy-config.json''')







class 	snake_case_       (unittest.TestCase				):
					def 			lowerCamelCase__(				self       :int				) ->   List[Any]:
												a__					=					0
					def 			lowerCamelCase__(				self       :Optional[Any]				) ->   Tuple:
												self.assertIsNotNone(transformers.models.auto.__spec__				)
												self.assertIsNotNone(importlib.util.find_spec('transformers.models.auto'				)				)
					def 			lowerCamelCase__(				self       :Union[str, Any]				) ->   Any:
												a__					=					AutoConfig.from_pretrained('bert-base-uncased'				)
												self.assertIsInstance(__snake_case      ,__snake_case				)
					def 			lowerCamelCase__(				self       :str				) ->   Optional[Any]:
												a__					=					AutoConfig.from_pretrained(__snake_case				)
												self.assertIsInstance(__snake_case      ,__snake_case				)
					def 			lowerCamelCase__(				self       :Optional[int]				) ->   Any:
												a__					=					AutoConfig.from_pretrained(__snake_case				)
												self.assertIsInstance(__snake_case      ,__snake_case				)
					def 			lowerCamelCase__(				self       :int				) ->   List[Any]:
												a__					=					AutoConfig.for_model('roberta'				)
												self.assertIsInstance(__snake_case      ,__snake_case				)
					def 			lowerCamelCase__(				self       :Union[str, Any]				) ->   Union[str, Any]:
												with tempfile.TemporaryDirectory() as tmp_dir:
																			# This model name contains bert and roberta, but roberta ends up being picked.
																			a__					=					os.path.join(__snake_case      ,'fake-roberta'				)
																			os.makedirs(__snake_case      ,exist_ok=__snake_case				)
																			with open(os.path.join(__snake_case      ,'config.json'				)      ,'w'				) as f:
																										f.write(json.dumps({}				)				)
																			a__					=					AutoConfig.from_pretrained(__snake_case				)
																			self.assertEqual(type(__snake_case				)      ,__snake_case				)
					def 			lowerCamelCase__(				self       :List[Any]				) ->   Tuple:
												try:
																			AutoConfig.register('custom'      ,__snake_case				)
																			# Wrong model type will raise an error
																			with self.assertRaises(__snake_case				):
																										AutoConfig.register('model'      ,__snake_case				)
																			# Trying to register something existing in the Transformers library will raise an error
																			with self.assertRaises(__snake_case				):
																										AutoConfig.register('bert'      ,__snake_case				)

																			# Now that the config is registered, it can be used as any other config with the auto-API
																			a__					=					CustomConfig()
																			with tempfile.TemporaryDirectory() as tmp_dir:
																										config.save_pretrained(__snake_case				)
																										a__					=					AutoConfig.from_pretrained(__snake_case				)
																										self.assertIsInstance(__snake_case      ,__snake_case				)

												finally:
																			if "custom" in CONFIG_MAPPING._extra_content:
																										del CONFIG_MAPPING._extra_content["custom"]
					def 			lowerCamelCase__(				self       :Tuple				) ->   Tuple:
												with self.assertRaisesRegex(
												    __snake_case      ,'bert-base is not a local folder and is not a valid model identifier'				):
																			a__					=					AutoConfig.from_pretrained('bert-base'				)
					def 			lowerCamelCase__(				self       :Tuple				) ->   Any:
												with self.assertRaisesRegex(
												    __snake_case      ,R'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'				):
																			a__					=					AutoConfig.from_pretrained(__snake_case      ,revision='aaaaaa'				)
					def 			lowerCamelCase__(				self       :Optional[int]				) ->   int:
												with self.assertRaisesRegex(
												    __snake_case      ,'hf-internal-testing/no-config-test-repo does not appear to have a file named config.json.'      ,):
																			a__					=					AutoConfig.from_pretrained('hf-internal-testing/no-config-test-repo'				)
					def 			lowerCamelCase__(				self       :List[str]				) ->   List[str]:
												# If remote code is not set, we will time out when asking whether to load the model.
												with self.assertRaises(__snake_case				):
																			a__					=					AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'				)
												# If remote code is disabled, we can't load this config.
												with self.assertRaises(__snake_case				):
																			a__					=					AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'      ,trust_remote_code=__snake_case				)

												a__					=					AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'      ,trust_remote_code=__snake_case				)
												self.assertEqual(config.__class__.__name__      ,'NewModelConfig'				)

												# Test config can be reloaded.
												with tempfile.TemporaryDirectory() as tmp_dir:
																			config.save_pretrained(__snake_case				)
																			a__					=					AutoConfig.from_pretrained(__snake_case      ,trust_remote_code=__snake_case				)
												self.assertEqual(reloaded_config.__class__.__name__      ,'NewModelConfig'				)



					def 			lowerCamelCase__(				self       :Any				) ->   Optional[int]:







												class 	snake_case_       (lowerCamelCase_				):
																	UpperCAmelCase__ :   Tuple   				=      '''new-model'''

												try:
																			AutoConfig.register('new-model'      ,__snake_case				)
																			# If remote code is not set, the default is to use local
																			a__					=					AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'				)
																			self.assertEqual(config.__class__.__name__      ,'NewModelConfigLocal'				)

																			# If remote code is disabled, we load the local one.
																			a__					=					AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'      ,trust_remote_code=__snake_case				)
																			self.assertEqual(config.__class__.__name__      ,'NewModelConfigLocal'				)

																			# If remote is enabled, we load from the Hub
																			a__					=					AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'      ,trust_remote_code=__snake_case				)
																			self.assertEqual(config.__class__.__name__      ,'NewModelConfig'				)

												finally:
																			if "new-model" in CONFIG_MAPPING._extra_content:
																										del CONFIG_MAPPING._extra_content["new-model"]








import inspect
import os
import unittest
from pathlib import Path

import torch

import accelerate
from accelerate.test_utils import execute_subprocess_async
from accelerate.test_utils.testing import run_command







class 	snake_case_       (unittest.TestCase				):
					UpperCAmelCase__ :   Optional[Any]   				=      inspect.getfile(accelerate.test_utils				)
					UpperCAmelCase__ :   Tuple   				=      os.path.sep.join(mod_file.split(os.path.sep				)[:-1] + ['''scripts''', '''test_cli.py''']				)

					UpperCAmelCase__ :   Optional[int]   				=      ['''accelerate''', '''launch''']
					UpperCAmelCase__ :   List[str]   				=      Path.home() / '''.cache/huggingface/accelerate'''
					UpperCAmelCase__ :   Union[str, Any]   				=      '''default_config.yaml'''
					UpperCAmelCase__ :   List[Any]   				=      config_folder / config_file
					UpperCAmelCase__ :   Optional[int]   				=      config_folder / '''_default_config.yaml'''

					UpperCAmelCase__ :   int   				=      Path('''tests/test_configs'''				)
					@classmethod
					def 			lowerCamelCase__(				cls       :Any				) ->   int:
												if cls.config_path.is_file():
																			cls.config_path.rename(cls.changed_path				)
					@classmethod
					def 			lowerCamelCase__(				cls       :Dict				) ->   Union[str, Any]:
												if cls.changed_path.is_file():
																			cls.changed_path.rename(cls.config_path				)
					def 			lowerCamelCase__(				self       :Any				) ->   Optional[int]:
												a__					=					self.base_cmd
												if torch.cuda.is_available() and (torch.cuda.device_count() > 1):
																			cmd += ["--multi_gpu"]
												execute_subprocess_async(cmd + [self.test_file_path]      ,env=os.environ.copy()				)
					def 			lowerCamelCase__(				self       :str				) ->   List[Any]:
												for config in sorted(self.test_config_path.glob('**/*.yaml'				)				):
																			with self.subTest(config_file=__snake_case				):
																										execute_subprocess_async(
																										    self.base_cmd + ['--config_file', str(__snake_case				), self.test_file_path]      ,env=os.environ.copy()				)



					def 			lowerCamelCase__(				self       :int				) ->   List[str]:
												execute_subprocess_async(['accelerate', 'test']      ,env=os.environ.copy()				)







class 	snake_case_       (unittest.TestCase				):
					UpperCAmelCase__ :   List[Any]   				=      '''test-tpu'''
					UpperCAmelCase__ :   str   				=      '''us-central1-a'''
					UpperCAmelCase__ :   Optional[Any]   				=      '''ls'''
					UpperCAmelCase__ :   Optional[int]   				=      ['''accelerate''', '''tpu-config''']
					UpperCAmelCase__ :   Any   				=      '''cd /usr/share'''
					UpperCAmelCase__ :   Tuple   				=      '''tests/test_samples/test_command_file.sh'''
					UpperCAmelCase__ :   List[Any]   				=      '''Running gcloud compute tpus tpu-vm ssh'''
					def 			lowerCamelCase__(				self       :Optional[Any]				) ->   Tuple:
												a__					=					run_command(
												    self.cmd
												    + ['--command', self.command, '--tpu_zone', self.tpu_zone, '--tpu_name', self.tpu_name, '--debug']      ,return_stdout=__snake_case      ,)
												self.assertIn(
												    F'{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all'      ,__snake_case      ,)
					def 			lowerCamelCase__(				self       :int				) ->   str:
												a__					=					run_command(
												    self.cmd
												    + [
												        '--config_file',
												        'tests/test_configs/0_12_0.yaml',
												        '--command',
												        self.command,
												        '--tpu_zone',
												        self.tpu_zone,
												        '--tpu_name',
												        self.tpu_name,
												        '--debug',
												    ]      ,return_stdout=__snake_case      ,)
												self.assertIn(
												    F'{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all'      ,__snake_case      ,)
					def 			lowerCamelCase__(				self       :Any				) ->   Dict:
												a__					=					run_command(
												    self.cmd + ['--config_file', 'tests/test_configs/latest.yaml', '--debug']      ,return_stdout=__snake_case				)
												self.assertIn(
												    F'{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo "hello world"; echo "this is a second command" --worker all'      ,__snake_case      ,)
					def 			lowerCamelCase__(				self       :Tuple				) ->   str:
												a__					=					run_command(
												    self.cmd + ['--config_file', 'tests/test_configs/latest.yaml', '--command', self.command, '--debug']      ,return_stdout=__snake_case      ,)
												self.assertIn(
												    F'{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all'      ,__snake_case      ,)
					def 			lowerCamelCase__(				self       :Dict				) ->   int:
												a__					=					run_command(
												    self.cmd
												    + [
												        '--config_file',
												        'tests/test_configs/latest.yaml',
												        '--command',
												        self.command,
												        '--command',
												        'echo "Hello World"',
												        '--debug',
												    ]      ,return_stdout=__snake_case      ,)
												self.assertIn(
												    F'{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls; echo "Hello World" --worker all'      ,__snake_case      ,)
					def 			lowerCamelCase__(				self       :Union[str, Any]				) ->   List[Any]:
												a__					=					run_command(
												    self.cmd
												    + ['--config_file', 'tests/test_configs/latest.yaml', '--command_file', self.command_file, '--debug']      ,return_stdout=__snake_case      ,)
												self.assertIn(
												    F'{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo "hello world"; echo "this is a second command" --worker all'      ,__snake_case      ,)
					def 			lowerCamelCase__(				self       :int				) ->   int:
												a__					=					run_command(
												    self.cmd
												    + [
												        '--config_file',
												        'tests/test_configs/0_12_0.yaml',
												        '--command_file',
												        self.command_file,
												        '--tpu_zone',
												        self.tpu_zone,
												        '--tpu_name',
												        self.tpu_name,
												        '--debug',
												    ]      ,return_stdout=__snake_case      ,)
												self.assertIn(
												    F'{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo "hello world"; echo "this is a second command" --worker all'      ,__snake_case      ,)
					def 			lowerCamelCase__(				self       :Union[str, Any]				) ->   List[Any]:
												a__					=					run_command(
												    self.cmd + ['--config_file', 'tests/test_configs/latest.yaml', '--install_accelerate', '--debug']      ,return_stdout=__snake_case      ,)
												self.assertIn(
												    F'{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; pip install accelerate -U; echo "hello world"; echo "this is a second command" --worker all'      ,__snake_case      ,)



					def 			lowerCamelCase__(				self       :Any				) ->   Optional[int]:
												a__					=					run_command(
												    self.cmd
												    + [
												        '--config_file',
												        'tests/test_configs/latest.yaml',
												        '--install_accelerate',
												        '--accelerate_version',
												        '12.0.0',
												        '--debug',
												    ]      ,return_stdout=__snake_case      ,)
												self.assertIn(
												    F'{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; pip install accelerate==12.0.0; echo "hello world"; echo "this is a second command" --worker all'      ,__snake_case      ,)




