


'''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     __lowerCAmelCase		(unittest.TestCase	):





      '''simple docstring'''






      def  UpperCamelCase__						(self       :       Optional[Any]  ,		UpperCamelCase       :       Dict					):

             '''simple docstring'''







             for model_result in results.values():
                    for batch_size, sequence_length in zip(model_result['''bs''']  ,		model_result['''ss''']					):
                           lowercase__      =					model_result['''result'''][batch_size][sequence_length]
                           self.assertIsNotNone(UpperCamelCase					)



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

             '''simple docstring'''







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



      def  UpperCamelCase__						(self       :       Optional[int]					):

             '''simple docstring'''







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



      def  UpperCamelCase__						(self       :       Dict					):

             '''simple docstring'''







             lowercase__      =					'''sshleifer/tiny-gpt2'''
             lowercase__      =					PyTorchBenchmarkArguments(
                 models=[MODEL_ID]  ,		training=UpperCamelCase  ,		inference=UpperCamelCase  ,		torchscript=UpperCamelCase  ,		sequence_lengths=[8]  ,		batch_sizes=[1]  ,		multi_process=UpperCamelCase  ,		)
             lowercase__      =					PyTorchBenchmark(UpperCamelCase					)
             lowercase__      =					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  UpperCamelCase__						(self       :       Any					):

             '''simple docstring'''







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



      def  UpperCamelCase__						(self       :       str					):

             '''simple docstring'''







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



      def  UpperCamelCase__						(self       :       Optional[Any]					):

             '''simple docstring'''







             lowercase__      =					'''sshleifer/tiny-gpt2'''
             lowercase__      =					PyTorchBenchmarkArguments(
                 models=[MODEL_ID]  ,		training=UpperCamelCase  ,		inference=UpperCamelCase  ,		sequence_lengths=[8]  ,		batch_sizes=[1]  ,		multi_process=UpperCamelCase  ,		)
             lowercase__      =					PyTorchBenchmark(UpperCamelCase					)
             lowercase__      =					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  UpperCamelCase__						(self       :       Optional[Any]					):

             '''simple docstring'''







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



      def  UpperCamelCase__						(self       :       int					):

             '''simple docstring'''







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



      def  UpperCamelCase__						(self       :       Optional[Any]					):

             '''simple docstring'''







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



      def  UpperCamelCase__						(self       :       Tuple					):

             '''simple docstring'''







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



      def  UpperCamelCase__						(self       :       List[str]					):

             '''simple docstring'''







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



      def  UpperCamelCase__						(self       :       List[str]					):

             '''simple docstring'''







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






      def  UpperCamelCase__						(self       :       List[Any]					):

             '''simple docstring'''







             lowercase__      =					'''sshleifer/tiny-gpt2'''

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

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








'''simple docstring'''

from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


lowerCamelCase				:  Tuple    		=      {
    'configuration_distilbert': [
        'DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'DistilBertConfig',
        'DistilBertOnnxConfig',
    ],
    'tokenization_distilbert': ['DistilBertTokenizer'],
}

try:
 if not is_tokenizers_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCamelCase				:  str    		=      ['DistilBertTokenizerFast']

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCamelCase				:  List[str]    		=      [
     'DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST',
     'DistilBertForMaskedLM',
     'DistilBertForMultipleChoice',
     'DistilBertForQuestionAnswering',
     'DistilBertForSequenceClassification',
     'DistilBertForTokenClassification',
     'DistilBertModel',
     'DistilBertPreTrainedModel',
 ]

try:
 if not is_tf_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCamelCase				:  Union[str, Any]    		=      [
     'TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST',
     'TFDistilBertForMaskedLM',
     'TFDistilBertForMultipleChoice',
     'TFDistilBertForQuestionAnswering',
     'TFDistilBertForSequenceClassification',
     'TFDistilBertForTokenClassification',
     'TFDistilBertMainLayer',
     'TFDistilBertModel',
     'TFDistilBertPreTrainedModel',
 ]

try:
 if not is_flax_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCamelCase				:  str    		=      [
     'FlaxDistilBertForMaskedLM',
     'FlaxDistilBertForMultipleChoice',
     'FlaxDistilBertForQuestionAnswering',
     'FlaxDistilBertForSequenceClassification',
     'FlaxDistilBertForTokenClassification',
     'FlaxDistilBertModel',
     'FlaxDistilBertPreTrainedModel',
 ]


if TYPE_CHECKING:
 from .configuration_distilbert import (
     DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP,
     DistilBertConfig,
     DistilBertOnnxConfig,
 )
 from .tokenization_distilbert import DistilBertTokenizer

 try:
  if not is_tokenizers_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_distilbert_fast import DistilBertTokenizerFast

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_distilbert import (
      DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
      DistilBertForMaskedLM,
      DistilBertForMultipleChoice,
      DistilBertForQuestionAnswering,
      DistilBertForSequenceClassification,
      DistilBertForTokenClassification,
      DistilBertModel,
      DistilBertPreTrainedModel,
  )

 try:
  if not is_tf_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_tf_distilbert import (
      TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
      TFDistilBertForMaskedLM,
      TFDistilBertForMultipleChoice,
      TFDistilBertForQuestionAnswering,
      TFDistilBertForSequenceClassification,
      TFDistilBertForTokenClassification,
      TFDistilBertMainLayer,
      TFDistilBertModel,
      TFDistilBertPreTrainedModel,
  )

 try:
  if not is_flax_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_flax_distilbert import (
      FlaxDistilBertForMaskedLM,
      FlaxDistilBertForMultipleChoice,
      FlaxDistilBertForQuestionAnswering,
      FlaxDistilBertForSequenceClassification,
      FlaxDistilBertForTokenClassification,
      FlaxDistilBertModel,
      FlaxDistilBertPreTrainedModel,
  )

else:
 import sys

 lowerCamelCase				:  int    		=      _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)




