






"""simple docstring"""


import json
import os
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from requests.exceptions import HTTPError

from transformers.utils import (
    CONFIG_NAME,
    FLAX_WEIGHTS_NAME,
    TF2_WEIGHTS_NAME,
    TRANSFORMERS_CACHE,
    WEIGHTS_NAME,
    cached_file,
    get_file_from_repo,
    has_file,
)


lowercase_				     =     'hf-internal-testing/tiny-random-bert'
lowercase_				     =     os.path.join(TRANSFORMERS_CACHE, 'models--hf-internal-testing--tiny-random-bert')
lowercase_				     =     '9b8c223d42b2188cb49d29af482996f9d0f3e5a6'


class 					snake_case		(							unittest.TestCase						):


   '''simple docstring'''

   def 				_SCREAMING_SNAKE_CASE				(	self :     int  ):

     '''simple docstring'''
     __A									= cached_file(_lowerCamelCase,       _lowerCamelCase  )
     # Should have downloaded the file in here
     self.assertTrue(os.path.isdir(_lowerCamelCase  )  )
     # Cache should contain at least those three subfolders:
     for subfolder in ["blobs", "refs", "snapshots"]:
       self.assertTrue(os.path.isdir(os.path.join(_lowerCamelCase,       _lowerCamelCase  )  )  )
     with open(os.path.join(_lowerCamelCase,       '''refs''',       '''main'''  )  ) as f:
       __A									= f.read()
     self.assertEqual(_lowerCamelCase,       os.path.join(_lowerCamelCase,       '''snapshots''',       _lowerCamelCase,       _lowerCamelCase  )  )
     self.assertTrue(os.path.isfile(_lowerCamelCase  )  )

     # File is cached at the same place the second time.
     __A									= cached_file(_lowerCamelCase,       _lowerCamelCase  )
     self.assertEqual(_lowerCamelCase,       _lowerCamelCase  )

     # Using a specific revision to test the full commit hash.
     __A									= cached_file(_lowerCamelCase,       _lowerCamelCase,       revision='''9b8c223'''  )
     self.assertEqual(_lowerCamelCase,       os.path.join(_lowerCamelCase,       '''snapshots''',       _lowerCamelCase,       _lowerCamelCase  )  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Any  ):

     '''simple docstring'''
     with self.assertRaisesRegex(_lowerCamelCase,       '''is not a valid model identifier'''  ):
       __A									= cached_file('''tiny-random-bert''',       _lowerCamelCase  )

     with self.assertRaisesRegex(_lowerCamelCase,       '''is not a valid git identifier'''  ):
       __A									= cached_file(_lowerCamelCase,       _lowerCamelCase,       revision='''aaaa'''  )

     with self.assertRaisesRegex(_lowerCamelCase,       '''does not appear to have a file named'''  ):
       __A									= cached_file(_lowerCamelCase,       '''conf'''  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     int  ):

     '''simple docstring'''
     with self.assertRaisesRegex(_lowerCamelCase,       '''does not appear to have a file named'''  ):
       __A									= cached_file(_lowerCamelCase,       '''conf'''  )

     with open(os.path.join(_lowerCamelCase,       '''refs''',       '''main'''  )  ) as f:
       __A									= f.read()
     self.assertTrue(os.path.isfile(os.path.join(_lowerCamelCase,       '''.no_exist''',       _lowerCamelCase,       '''conf'''  )  )  )

     __A									= cached_file(_lowerCamelCase,       '''conf''',       _raise_exceptions_for_missing_entries=_lowerCamelCase  )
     self.assertIsNone(_lowerCamelCase  )

     __A									= cached_file(_lowerCamelCase,       '''conf''',       local_files_only=_lowerCamelCase,       _raise_exceptions_for_missing_entries=_lowerCamelCase  )
     self.assertIsNone(_lowerCamelCase  )

     __A									= mock.Mock()
     __A									= 5_00
     __A									= {}
     __A									= HTTPError
     __A									= {}

     # Under the mock environment we get a 500 error when trying to reach the tokenizer.
     with mock.patch('''requests.Session.request''',       return_value=_lowerCamelCase  ) as mock_head:
       __A									= cached_file(_lowerCamelCase,       '''conf''',       _raise_exceptions_for_connection_errors=_lowerCamelCase  )
       self.assertIsNone(_lowerCamelCase  )
       # This check we did call the fake head request
       mock_head.assert_called()


   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[int]  ):

     '''simple docstring'''
     self.assertTrue(has_file('''hf-internal-testing/tiny-bert-pt-only''',       _lowerCamelCase  )  )
     self.assertFalse(has_file('''hf-internal-testing/tiny-bert-pt-only''',       _lowerCamelCase  )  )
     self.assertFalse(has_file('''hf-internal-testing/tiny-bert-pt-only''',       _lowerCamelCase  )  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     List[Any]  ):

     '''simple docstring'''
     # `get_file_from_repo` returns None if the file does not exist
     self.assertIsNone(get_file_from_repo('''bert-base-cased''',       '''ahah.txt'''  )  )

     # The function raises if the repository does not exist.
     with self.assertRaisesRegex(_lowerCamelCase,       '''is not a valid model identifier'''  ):
       get_file_from_repo('''bert-base-case''',       _lowerCamelCase  )

     # The function raises if the revision does not exist.
     with self.assertRaisesRegex(_lowerCamelCase,       '''is not a valid git identifier'''  ):
       get_file_from_repo('''bert-base-cased''',       _lowerCamelCase,       revision='''ahaha'''  )

     __A									= get_file_from_repo('''bert-base-cased''',       _lowerCamelCase  )
     # The name is the cached name which is not very easy to test, so instead we load the content.
     __A									= json.loads(open(_lowerCamelCase,       '''r'''  ).read()  )
     self.assertEqual(config['''hidden_size'''],       7_68  )






   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[int]  ):

     '''simple docstring'''
     with tempfile.TemporaryDirectory() as tmp_dir:
       __A									= Path(_lowerCamelCase  ) / '''a.txt'''
       filename.touch()
       self.assertEqual(get_file_from_repo(_lowerCamelCase,       '''a.txt'''  ),       str(_lowerCamelCase  )  )

       self.assertIsNone(get_file_from_repo(_lowerCamelCase,       '''b.txt'''  )  )












"""simple docstring"""


import os
import tempfile
import unittest
from pathlib import Path

from transformers import AutoConfig, is_torch_available
from transformers.testing_utils import require_torch, torch_device


if is_torch_available():
       from transformers import PyTorchBenchmark, PyTorchBenchmarkArguments


@require_torch
class 					snake_case		(							unittest.TestCase						):


   '''simple docstring'''

   def 				_SCREAMING_SNAKE_CASE				(	self :     Any,       _lowerCamelCase :     Optional[int]  ):

     '''simple docstring'''
     for model_result in results.values():
       for batch_size, sequence_length in zip(model_result['''bs'''],       model_result['''ss''']  ):
         __A									= model_result['''result'''][batch_size][sequence_length]
         self.assertIsNotNone(_lowerCamelCase  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[Any]  ):

     '''simple docstring'''
     __A									= '''sshleifer/tiny-gpt2'''
     __A									= PyTorchBenchmarkArguments(
         models=[MODEL_ID],       training=_lowerCamelCase,       inference=_lowerCamelCase,       sequence_lengths=[8],       batch_sizes=[1],       multi_process=_lowerCamelCase,       )
     __A									= PyTorchBenchmark(_lowerCamelCase  )
     __A									= benchmark.run()
     self.check_results_dict_not_empty(results.time_inference_result  )
     self.check_results_dict_not_empty(results.memory_inference_result  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[Any]  ):

     '''simple docstring'''
     __A									= '''sgugger/tiny-distilbert-classification'''
     __A									= PyTorchBenchmarkArguments(
         models=[MODEL_ID],       training=_lowerCamelCase,       inference=_lowerCamelCase,       sequence_lengths=[8],       batch_sizes=[1],       multi_process=_lowerCamelCase,       only_pretrain_model=_lowerCamelCase,       )
     __A									= PyTorchBenchmark(_lowerCamelCase  )
     __A									= benchmark.run()
     self.check_results_dict_not_empty(results.time_inference_result  )
     self.check_results_dict_not_empty(results.memory_inference_result  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Any  ):

     '''simple docstring'''
     __A									= '''sshleifer/tiny-gpt2'''
     __A									= PyTorchBenchmarkArguments(
         models=[MODEL_ID],       training=_lowerCamelCase,       inference=_lowerCamelCase,       torchscript=_lowerCamelCase,       sequence_lengths=[8],       batch_sizes=[1],       multi_process=_lowerCamelCase,       )
     __A									= PyTorchBenchmark(_lowerCamelCase  )
     __A									= benchmark.run()
     self.check_results_dict_not_empty(results.time_inference_result  )
     self.check_results_dict_not_empty(results.memory_inference_result  )


   @unittest.skipIf(torch_device == '''cpu''',       '''Cant do half precision'''  )
   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[Any]  ):

     '''simple docstring'''
     __A									= '''sshleifer/tiny-gpt2'''
     __A									= PyTorchBenchmarkArguments(
         models=[MODEL_ID],       training=_lowerCamelCase,       inference=_lowerCamelCase,       fpaa=_lowerCamelCase,       sequence_lengths=[8],       batch_sizes=[1],       multi_process=_lowerCamelCase,       )
     __A									= PyTorchBenchmark(_lowerCamelCase  )
     __A									= benchmark.run()
     self.check_results_dict_not_empty(results.time_inference_result  )
     self.check_results_dict_not_empty(results.memory_inference_result  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Dict  ):

     '''simple docstring'''
     __A									= '''sshleifer/tiny-gpt2'''
     __A									= AutoConfig.from_pretrained(_lowerCamelCase  )
     # set architectures equal to `None`
     __A									= None
     __A									= PyTorchBenchmarkArguments(
         models=[MODEL_ID],       training=_lowerCamelCase,       inference=_lowerCamelCase,       sequence_lengths=[8],       batch_sizes=[1],       multi_process=_lowerCamelCase,       )
     __A									= PyTorchBenchmark(_lowerCamelCase,       configs=[config]  )
     __A									= benchmark.run()
     self.check_results_dict_not_empty(results.time_inference_result  )
     self.check_results_dict_not_empty(results.memory_inference_result  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     int  ):

     '''simple docstring'''
     __A									= '''sshleifer/tiny-gpt2'''
     __A									= PyTorchBenchmarkArguments(
         models=[MODEL_ID],       training=_lowerCamelCase,       inference=_lowerCamelCase,       sequence_lengths=[8],       batch_sizes=[1],       multi_process=_lowerCamelCase,       )
     __A									= PyTorchBenchmark(_lowerCamelCase  )
     __A									= benchmark.run()
     self.check_results_dict_not_empty(results.time_train_result  )
     self.check_results_dict_not_empty(results.memory_train_result  )


   @unittest.skipIf(torch_device == '''cpu''',       '''Can\'t do half precision'''  )
   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[int]  ):

     '''simple docstring'''
     __A									= '''sshleifer/tiny-gpt2'''
     __A									= PyTorchBenchmarkArguments(
         models=[MODEL_ID],       training=_lowerCamelCase,       inference=_lowerCamelCase,       sequence_lengths=[8],       batch_sizes=[1],       fpaa=_lowerCamelCase,       multi_process=_lowerCamelCase,       )
     __A									= PyTorchBenchmark(_lowerCamelCase  )
     __A									= benchmark.run()
     self.check_results_dict_not_empty(results.time_train_result  )
     self.check_results_dict_not_empty(results.memory_train_result  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     str  ):

     '''simple docstring'''
     __A									= '''sshleifer/tiny-gpt2'''
     __A									= AutoConfig.from_pretrained(_lowerCamelCase  )
     __A									= PyTorchBenchmarkArguments(
         models=[MODEL_ID],       training=_lowerCamelCase,       inference=_lowerCamelCase,       sequence_lengths=[8],       batch_sizes=[1],       multi_process=_lowerCamelCase,       )
     __A									= PyTorchBenchmark(_lowerCamelCase,       configs=[config]  )
     __A									= benchmark.run()
     self.check_results_dict_not_empty(results.time_inference_result  )
     self.check_results_dict_not_empty(results.memory_inference_result  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     List[Any]  ):

     '''simple docstring'''
     __A									= '''sshleifer/tinier_bart'''
     __A									= AutoConfig.from_pretrained(_lowerCamelCase  )
     __A									= PyTorchBenchmarkArguments(
         models=[MODEL_ID],       training=_lowerCamelCase,       inference=_lowerCamelCase,       sequence_lengths=[8],       batch_sizes=[1],       multi_process=_lowerCamelCase,       )
     __A									= PyTorchBenchmark(_lowerCamelCase,       configs=[config]  )
     __A									= benchmark.run()
     self.check_results_dict_not_empty(results.time_inference_result  )
     self.check_results_dict_not_empty(results.memory_inference_result  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[Any]  ):

     '''simple docstring'''
     __A									= '''sshleifer/tiny-gpt2'''
     __A									= AutoConfig.from_pretrained(_lowerCamelCase  )
     __A									= PyTorchBenchmarkArguments(
         models=[MODEL_ID],       training=_lowerCamelCase,       inference=_lowerCamelCase,       sequence_lengths=[8],       batch_sizes=[1],       multi_process=_lowerCamelCase,       )
     __A									= PyTorchBenchmark(_lowerCamelCase,       configs=[config]  )
     __A									= benchmark.run()
     self.check_results_dict_not_empty(results.time_train_result  )
     self.check_results_dict_not_empty(results.memory_train_result  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Dict  ):

     '''simple docstring'''
     __A									= '''sshleifer/tinier_bart'''
     __A									= AutoConfig.from_pretrained(_lowerCamelCase  )
     __A									= PyTorchBenchmarkArguments(
         models=[MODEL_ID],       training=_lowerCamelCase,       inference=_lowerCamelCase,       sequence_lengths=[8],       batch_sizes=[1],       multi_process=_lowerCamelCase,       )
     __A									= PyTorchBenchmark(_lowerCamelCase,       configs=[config]  )
     __A									= benchmark.run()
     self.check_results_dict_not_empty(results.time_train_result  )
     self.check_results_dict_not_empty(results.memory_train_result  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[Any]  ):

     '''simple docstring'''
     __A									= '''sshleifer/tiny-gpt2'''
     with tempfile.TemporaryDirectory() as tmp_dir:
       __A									= PyTorchBenchmarkArguments(
           models=[MODEL_ID],       training=_lowerCamelCase,       inference=_lowerCamelCase,       save_to_csv=_lowerCamelCase,       sequence_lengths=[8],       batch_sizes=[1],       inference_time_csv_file=os.path.join(_lowerCamelCase,       '''inf_time.csv'''  ),       train_memory_csv_file=os.path.join(_lowerCamelCase,       '''train_mem.csv'''  ),       inference_memory_csv_file=os.path.join(_lowerCamelCase,       '''inf_mem.csv'''  ),       train_time_csv_file=os.path.join(_lowerCamelCase,       '''train_time.csv'''  ),       env_info_csv_file=os.path.join(_lowerCamelCase,       '''env.csv'''  ),       multi_process=_lowerCamelCase,       )
       __A									= PyTorchBenchmark(_lowerCamelCase  )
       benchmark.run()
       self.assertTrue(Path(os.path.join(_lowerCamelCase,       '''inf_time.csv'''  )  ).exists()  )
       self.assertTrue(Path(os.path.join(_lowerCamelCase,       '''train_time.csv'''  )  ).exists()  )
       self.assertTrue(Path(os.path.join(_lowerCamelCase,       '''inf_mem.csv'''  )  ).exists()  )
       self.assertTrue(Path(os.path.join(_lowerCamelCase,       '''train_mem.csv'''  )  ).exists()  )
       self.assertTrue(Path(os.path.join(_lowerCamelCase,       '''env.csv'''  )  ).exists()  )






   def 				_SCREAMING_SNAKE_CASE				(	self :     Union[str, Any]  ):

     '''simple docstring'''
     __A									= '''sshleifer/tiny-gpt2'''

     def _check_summary_is_not_empty(_lowerCamelCase :     List[Any]  ):
       self.assertTrue(hasattr(_lowerCamelCase,       '''sequential'''  )  )
       self.assertTrue(hasattr(_lowerCamelCase,       '''cumulative'''  )  )
       self.assertTrue(hasattr(_lowerCamelCase,       '''current'''  )  )
       self.assertTrue(hasattr(_lowerCamelCase,       '''total'''  )  )

     with tempfile.TemporaryDirectory() as tmp_dir:
       __A									= PyTorchBenchmarkArguments(
           models=[MODEL_ID],       training=_lowerCamelCase,       inference=_lowerCamelCase,       sequence_lengths=[8],       batch_sizes=[1],       log_filename=os.path.join(_lowerCamelCase,       '''log.txt'''  ),       log_print=_lowerCamelCase,       trace_memory_line_by_line=_lowerCamelCase,       multi_process=_lowerCamelCase,       )
       __A									= PyTorchBenchmark(_lowerCamelCase  )
       __A									= benchmark.run()
       _check_summary_is_not_empty(result.inference_summary  )
       _check_summary_is_not_empty(result.train_summary  )
       self.assertTrue(Path(os.path.join(_lowerCamelCase,       '''log.txt'''  )  ).exists()  )




