



"""simple docstring"""




import unittest

from transformers import XLMConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch

 from transformers import (
     XLMForMultipleChoice,
     XLMForQuestionAnswering,
     XLMForQuestionAnsweringSimple,
     XLMForSequenceClassification,
     XLMForTokenClassification,
     XLMModel,
     XLMWithLMHeadModel,
 )
 from transformers.models.xlm.modeling_xlm import XLM_PRETRAINED_MODEL_ARCHIVE_LIST






class       UpperCAmelCase_    :



      def __init__(	self	:      Any	,      A	:      List[Any]	,      A	:      Optional[int]=1_3	,      A	:      List[Any]=7	,      A	:      Dict=True	,      A	:      List[Any]=True	,      A	:      Any=True	,      A	:      Tuple=True	,      A	:      Tuple=True	,      A	:      Dict=False	,      A	:      List[Any]=False	,      A	:      Optional[Any]=False	,      A	:      Union[str, Any]=2	,      A	:      Optional[Any]=9_9	,      A	:      Optional[int]=0	,      A	:      Tuple=3_2	,      A	:      str=5	,      A	:      List[Any]=4	,      A	:      Optional[int]=0.1	,      A	:      int=0.1	,      A	:      Tuple=5_1_2	,      A	:      Union[str, Any]=2	,      A	:      List[Any]=0.02	,      A	:      Optional[int]=2	,      A	:      Dict=4	,      A	:      Union[str, Any]="last"	,      A	:      Optional[int]=True	,      A	:      Dict=None	,      A	:      Tuple=0	,      ):
           _UpperCAmelCase     :      int														=       parent
           _UpperCAmelCase     :      Optional[Any]														=       batch_size
           _UpperCAmelCase     :      int														=       seq_length
           _UpperCAmelCase     :      Optional[Any]														=       is_training
           _UpperCAmelCase     :      Optional[int]														=       use_input_lengths
           _UpperCAmelCase     :      Any														=       use_token_type_ids
           _UpperCAmelCase     :      Any														=       use_labels
           _UpperCAmelCase     :      int														=       gelu_activation
           _UpperCAmelCase     :      List[str]														=       sinusoidal_embeddings
           _UpperCAmelCase     :      Dict														=       causal
           _UpperCAmelCase     :      Optional[Any]														=       asm
           _UpperCAmelCase     :      Optional[Any]														=       n_langs
           _UpperCAmelCase     :      Optional[Any]														=       vocab_size
           _UpperCAmelCase     :      Union[str, Any]														=       n_special
           _UpperCAmelCase     :      Tuple														=       hidden_size
           _UpperCAmelCase     :      List[str]														=       num_hidden_layers
           _UpperCAmelCase     :      List[Any]														=       num_attention_heads
           _UpperCAmelCase     :      str														=       hidden_dropout_prob
           _UpperCAmelCase     :      List[Any]														=       attention_probs_dropout_prob
           _UpperCAmelCase     :      List[Any]														=       max_position_embeddings
           _UpperCAmelCase     :      Optional[Any]														=       type_sequence_label_size
           _UpperCAmelCase     :      Optional[Any]														=       initializer_range
           _UpperCAmelCase     :      Optional[Any]														=       num_labels
           _UpperCAmelCase     :      Union[str, Any]														=       num_choices
           _UpperCAmelCase     :      Any														=       summary_type
           _UpperCAmelCase     :      List[Any]														=       use_proj
           _UpperCAmelCase     :      Dict														=       scope
           _UpperCAmelCase     :      Optional[int]														=       bos_token_id



      def 		snake_case_   (	self	:      Optional[int]		):
           _UpperCAmelCase     :      Union[str, Any]														=       ids_tensor([self.batch_size, self.seq_length]	,      self.vocab_size		)
           _UpperCAmelCase     :      List[str]														=       random_attention_mask([self.batch_size, self.seq_length]		)

           _UpperCAmelCase     :      Tuple														=       None
           if self.use_input_lengths:
                _UpperCAmelCase     :      str														=       (
                    ids_tensor([self.batch_size]	,      vocab_size=2		) + self.seq_length - 2
                )  # small variation of seq_length

           _UpperCAmelCase     :      Any														=       None
           if self.use_token_type_ids:
                _UpperCAmelCase     :      str														=       ids_tensor([self.batch_size, self.seq_length]	,      self.n_langs		)

           _UpperCAmelCase     :      str														=       None
           _UpperCAmelCase     :      Any														=       None
           _UpperCAmelCase     :      int														=       None
           if self.use_labels:
                _UpperCAmelCase     :      Optional[Any]														=       ids_tensor([self.batch_size]	,      self.type_sequence_label_size		)
                _UpperCAmelCase     :      Dict														=       ids_tensor([self.batch_size, self.seq_length]	,      self.num_labels		)
                _UpperCAmelCase     :      int														=       ids_tensor([self.batch_size]	,      2		).float()
                _UpperCAmelCase     :      List[Any]														=       ids_tensor([self.batch_size]	,      self.num_choices		)

           _UpperCAmelCase     :      Union[str, Any]														=       self.get_config()

           return (
               config,
               input_ids,
               token_type_ids,
               input_lengths,
               sequence_labels,
               token_labels,
               is_impossible_labels,
               choice_labels,
               input_mask,
           )



      def 		snake_case_   (	self	:      str		):
           return XLMConfig(
               vocab_size=self.vocab_size	,      n_special=self.n_special	,      emb_dim=self.hidden_size	,      n_layers=self.num_hidden_layers	,      n_heads=self.num_attention_heads	,      dropout=self.hidden_dropout_prob	,      attention_dropout=self.attention_probs_dropout_prob	,      gelu_activation=self.gelu_activation	,      sinusoidal_embeddings=self.sinusoidal_embeddings	,      asm=self.asm	,      causal=self.causal	,      n_langs=self.n_langs	,      max_position_embeddings=self.max_position_embeddings	,      initializer_range=self.initializer_range	,      summary_type=self.summary_type	,      use_proj=self.use_proj	,      num_labels=self.num_labels	,      bos_token_id=self.bos_token_id	,      )



      def 		snake_case_   (	self	:      int	,      A	:      Optional[Any]	,      A	:      str	,      A	:      Union[str, Any]	,      A	:      Tuple	,      A	:      List[str]	,      A	:      Optional[int]	,      A	:      Optional[Any]	,      A	:      Optional[int]	,      A	:      Tuple	,      ):
           _UpperCAmelCase     :      Tuple														=       XLMModel(config=A		)
           model.to(A		)
           model.eval()
           _UpperCAmelCase     :      int														=       model(A	,      lengths=A	,      langs=A		)
           _UpperCAmelCase     :      List[Any]														=       model(A	,      langs=A		)
           _UpperCAmelCase     :      Any														=       model(A		)
           self.parent.assertEqual(result.last_hidden_state.shape	,      (self.batch_size, self.seq_length, self.hidden_size)		)



      def 		snake_case_   (	self	:      Optional[int]	,      A	:      Dict	,      A	:      Optional[Any]	,      A	:      List[Any]	,      A	:      Tuple	,      A	:      int	,      A	:      List[Any]	,      A	:      str	,      A	:      Any	,      A	:      Optional[int]	,      ):
           _UpperCAmelCase     :      Optional[int]														=       XLMWithLMHeadModel(A		)
           model.to(A		)
           model.eval()

           _UpperCAmelCase     :      Optional[int]														=       model(A	,      token_type_ids=A	,      labels=A		)
           self.parent.assertEqual(result.loss.shape	,      ()		)
           self.parent.assertEqual(result.logits.shape	,      (self.batch_size, self.seq_length, self.vocab_size)		)



      def 		snake_case_   (	self	:      List[str]	,      A	:      Tuple	,      A	:      Optional[int]	,      A	:      str	,      A	:      Union[str, Any]	,      A	:      Tuple	,      A	:      str	,      A	:      Tuple	,      A	:      List[Any]	,      A	:      int	,      ):
           _UpperCAmelCase     :      Union[str, Any]														=       XLMForQuestionAnsweringSimple(A		)
           model.to(A		)
           model.eval()

           _UpperCAmelCase     :      Tuple														=       model(A		)

           _UpperCAmelCase     :      Any														=       model(A	,      start_positions=A	,      end_positions=A		)
           _UpperCAmelCase     :      Optional[int]														=       outputs
           self.parent.assertEqual(result.start_logits.shape	,      (self.batch_size, self.seq_length)		)
           self.parent.assertEqual(result.end_logits.shape	,      (self.batch_size, self.seq_length)		)



      def 		snake_case_   (	self	:      int	,      A	:      List[str]	,      A	:      Dict	,      A	:      Dict	,      A	:      Optional[Any]	,      A	:      Optional[Any]	,      A	:      int	,      A	:      int	,      A	:      int	,      A	:      List[Any]	,      ):
           _UpperCAmelCase     :      List[Any]														=       XLMForQuestionAnswering(A		)
           model.to(A		)
           model.eval()

           _UpperCAmelCase     :      Tuple														=       model(A		)

           _UpperCAmelCase     :      int														=       model(
               A	,      start_positions=A	,      end_positions=A	,      cls_index=A	,      is_impossible=A	,      p_mask=A	,      )

           _UpperCAmelCase     :      Dict														=       model(
               A	,      start_positions=A	,      end_positions=A	,      cls_index=A	,      is_impossible=A	,      )

           ((_UpperCAmelCase)							,			)     :      Optional[Any]														=       result_with_labels.to_tuple()

           _UpperCAmelCase     :      Tuple														=       model(A	,      start_positions=A	,      end_positions=A		)

           ((_UpperCAmelCase)							,			)     :      List[str]														=       result_with_labels.to_tuple()

           self.parent.assertEqual(result_with_labels.loss.shape	,      ()		)
           self.parent.assertEqual(result.start_top_log_probs.shape	,      (self.batch_size, model.config.start_n_top)		)
           self.parent.assertEqual(result.start_top_index.shape	,      (self.batch_size, model.config.start_n_top)		)
           self.parent.assertEqual(
               result.end_top_log_probs.shape	,      (self.batch_size, model.config.start_n_top * model.config.end_n_top)		)
           self.parent.assertEqual(
               result.end_top_index.shape	,      (self.batch_size, model.config.start_n_top * model.config.end_n_top)		)
           self.parent.assertEqual(result.cls_logits.shape	,      (self.batch_size,)		)



      def 		snake_case_   (	self	:      Union[str, Any]	,      A	:      Optional[int]	,      A	:      List[Any]	,      A	:      Dict	,      A	:      Dict	,      A	:      Any	,      A	:      int	,      A	:      int	,      A	:      Optional[Any]	,      A	:      int	,      ):
           _UpperCAmelCase     :      int														=       XLMForSequenceClassification(A		)
           model.to(A		)
           model.eval()

           _UpperCAmelCase     :      str														=       model(A		)
           _UpperCAmelCase     :      Tuple														=       model(A	,      labels=A		)
           self.parent.assertEqual(result.loss.shape	,      ()		)
           self.parent.assertEqual(result.logits.shape	,      (self.batch_size, self.type_sequence_label_size)		)



      def 		snake_case_   (	self	:      List[str]	,      A	:      Dict	,      A	:      Tuple	,      A	:      List[Any]	,      A	:      Dict	,      A	:      List[Any]	,      A	:      Tuple	,      A	:      Optional[int]	,      A	:      Dict	,      A	:      List[str]	,      ):
           _UpperCAmelCase     :      Any														=       self.num_labels
           _UpperCAmelCase     :      Any														=       XLMForTokenClassification(A		)
           model.to(A		)
           model.eval()

           _UpperCAmelCase     :      Any														=       model(A	,      attention_mask=A	,      labels=A		)
           self.parent.assertEqual(result.logits.shape	,      (self.batch_size, self.seq_length, self.num_labels)		)



      def 		snake_case_   (	self	:      int	,      A	:      Any	,      A	:      int	,      A	:      Optional[int]	,      A	:      str	,      A	:      Union[str, Any]	,      A	:      Tuple	,      A	:      Optional[int]	,      A	:      Union[str, Any]	,      A	:      int	,      ):
           _UpperCAmelCase     :      Optional[Any]														=       self.num_choices
           _UpperCAmelCase     :      Union[str, Any]														=       XLMForMultipleChoice(config=A		)
           model.to(A		)
           model.eval()
           _UpperCAmelCase     :      Dict														=       input_ids.unsqueeze(1		).expand(-1	,      self.num_choices	,      -1		).contiguous()
           _UpperCAmelCase     :      Union[str, Any]														=       token_type_ids.unsqueeze(1		).expand(-1	,      self.num_choices	,      -1		).contiguous()
           _UpperCAmelCase     :      int														=       input_mask.unsqueeze(1		).expand(-1	,      self.num_choices	,      -1		).contiguous()
           _UpperCAmelCase     :      Dict														=       model(
               A	,      attention_mask=A	,      token_type_ids=A	,      labels=A	,      )
           self.parent.assertEqual(result.logits.shape	,      (self.batch_size, self.num_choices)		)



      def 		snake_case_   (	self	:      List[Any]		):
           _UpperCAmelCase     :      str														=       self.prepare_config_and_inputs()
           (
               (
               _UpperCAmelCase
           )							,			(
               _UpperCAmelCase
           )							,			(
               _UpperCAmelCase
           )							,			(
               _UpperCAmelCase
           )							,			(
               _UpperCAmelCase
           )							,			(
               _UpperCAmelCase
           )							,			(
               _UpperCAmelCase
           )							,			(
               _UpperCAmelCase
           )							,			(
               _UpperCAmelCase
           )							,			
           )     :      Tuple														=       config_and_inputs
           _UpperCAmelCase     :      Optional[Any]														=       {"input_ids": input_ids, "token_type_ids": token_type_ids, "lengths": input_lengths}
           return config, inputs_dict







@require_torch
class       UpperCAmelCase_    (							_UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     unittest.TestCase		):
      __SCREAMING_SNAKE_CASE		:  int						=					(
          (
              XLMModel,
              XLMWithLMHeadModel,
              XLMForQuestionAnswering,
              XLMForSequenceClassification,
              XLMForQuestionAnsweringSimple,
              XLMForTokenClassification,
              XLMForMultipleChoice,
          )
          if is_torch_available()
          else ()
      )
      __SCREAMING_SNAKE_CASE		:  int						=					(
          (XLMWithLMHeadModel,) if is_torch_available() else ()
      )  # TODO (PVP): Check other models whether language generation is also applicable
      __SCREAMING_SNAKE_CASE		:  Any						=					(
          {
              'feature-extraction': XLMModel,
              'fill-mask': XLMWithLMHeadModel,
              'question-answering': XLMForQuestionAnsweringSimple,
              'text-classification': XLMForSequenceClassification,
              'text-generation': XLMWithLMHeadModel,
              'token-classification': XLMForTokenClassification,
              'zero-shot': XLMForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )



      def 		snake_case_   (	self	:      List[Any]	,      A	:      Any	,      A	:      Optional[Any]	,      A	:      Optional[int]	,      A	:      List[Any]	,      A	:      Any		):
           if (
               pipeline_test_casse_name == "QAPipelineTests"
               and tokenizer_name is not None
               and not tokenizer_name.endswith("Fast"		)
           ):
                # `QAPipelineTests` fails for a few models when the slower tokenizer are used.
                # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
                # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
                return True

           return False



      def 		snake_case_   (	self	:      Optional[int]	,      A	:      Optional[int]	,      A	:      str	,      A	:      Optional[Any]=False		):
           _UpperCAmelCase     :      str														=       super()._prepare_for_class(A	,      A	,      return_labels=A		)

           if return_labels:
                if model_class.__name__ == "XLMForQuestionAnswering":
                     _UpperCAmelCase     :      Union[str, Any]														=       torch.zeros(
                         self.model_tester.batch_size	,      dtype=torch.long	,      device=A		)
                     _UpperCAmelCase     :      Optional[Any]														=       torch.zeros(
                         self.model_tester.batch_size	,      dtype=torch.long	,      device=A		)

           return inputs_dict



      def 		snake_case_   (	self	:      Optional[Any]		):
           _UpperCAmelCase     :      Tuple														=       XLMModelTester(self		)
           _UpperCAmelCase     :      List[str]														=       ConfigTester(self	,      config_class=A	,      emb_dim=3_7		)



      def 		snake_case_   (	self	:      Any		):
           self.config_tester.run_common_tests()



      def 		snake_case_   (	self	:      Dict		):
           _UpperCAmelCase     :      List[Any]														=       self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_xlm_model(*A		)



      def 		snake_case_   (	self	:      Dict		):
           _UpperCAmelCase     :      List[Any]														=       self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_xlm_lm_head(*A		)



      def 		snake_case_   (	self	:      Optional[Any]		):
           _UpperCAmelCase     :      Tuple														=       self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_xlm_simple_qa(*A		)



      def 		snake_case_   (	self	:      int		):
           _UpperCAmelCase     :      Union[str, Any]														=       self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_xlm_qa(*A		)



      def 		snake_case_   (	self	:      int		):
           _UpperCAmelCase     :      Optional[Any]														=       self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_xlm_sequence_classif(*A		)



      def 		snake_case_   (	self	:      Union[str, Any]		):
           _UpperCAmelCase     :      str														=       self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_xlm_token_classif(*A		)



      def 		snake_case_   (	self	:      List[str]		):
           _UpperCAmelCase     :      Tuple														=       self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_xlm_for_multiple_choice(*A		)



      def 		snake_case_   (	self	:      str	,      A	:      str	,      A	:      int	,      A	:      Tuple	,      A	:      List[Any]	,      A	:      Optional[int]	,      A	:      Dict=False	,      A	:      List[str]=1		):
           self.assertIsInstance(A	,      A		)
           self.assertListEqual(
               [isinstance(A	,      A		) for iter_attentions in attentions]	,      [True] * len(A		)		)
           self.assertEqual(len(A		)	,      (max_length - min_length) * num_beam_groups		)

           for idx, iter_attentions in enumerate(A		):
                # adds PAD dummy token
                _UpperCAmelCase     :      Optional[int]														=       min_length + idx + 1
                _UpperCAmelCase     :      Dict														=       min_length + idx + 1

                _UpperCAmelCase     :      Tuple														=       (
                    batch_size * num_beam_groups,
                    config.num_attention_heads,
                    tgt_len,
                    src_len,
                )
                # check attn size
                self.assertListEqual(
                    [layer_attention.shape for layer_attention in iter_attentions]	,      [expected_shape] * len(A		)		)



      def 		snake_case_   (	self	:      Optional[Any]	,      A	:      List[str]	,      A	:      int	,      A	:      Dict	,      A	:      Union[str, Any]	,      A	:      int	,      A	:      Any=False	,      A	:      str=1		):
           self.assertIsInstance(A	,      A		)
           self.assertListEqual(
               [isinstance(A	,      A		) for iter_hidden_states in hidden_states]	,      [True] * len(A		)	,      )
           self.assertEqual(len(A		)	,      (max_length - min_length) * num_beam_groups		)

           for idx, iter_hidden_states in enumerate(A		):
                # adds PAD dummy token
                _UpperCAmelCase     :      Union[str, Any]														=       min_length + idx + 1
                _UpperCAmelCase     :      Dict														=       (batch_size * num_beam_groups, seq_len, config.hidden_size)
                # check hidden size
                self.assertListEqual(
                    [layer_hidden_states.shape for layer_hidden_states in iter_hidden_states]	,      [expected_shape] * len(A		)	,      )
           pass



      @slow
      def 		snake_case_   (	self	:      Any		):
           for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                _UpperCAmelCase     :      Any														=       XLMModel.from_pretrained(A		)
                self.assertIsNotNone(A		)







@require_torch
class       UpperCAmelCase_    (							unittest.TestCase		):



      @slow
      def 		snake_case_   (	self	:      Optional[int]		):
           _UpperCAmelCase     :      List[str]														=       XLMWithLMHeadModel.from_pretrained("xlm-mlm-en-2048"		)
           model.to(A		)
           _UpperCAmelCase     :      Optional[Any]														=       torch.tensor([[1_4, 4_4_7]]	,      dtype=torch.long	,      device=A		)  # the president
           _UpperCAmelCase     :      Any														=       [
               1_4,
               4_4_7,
               1_4,
               4_4_7,
               1_4,
               4_4_7,
               1_4,
               4_4_7,
               1_4,
               4_4_7,
               1_4,
               4_4_7,
               1_4,
               4_4_7,
               1_4,
               4_4_7,
               1_4,
               4_4_7,
               1_4,
               4_4_7,
           ]  # the president the president the president the president the president the president the president the president the president the president
           # TODO(PVP): this and other input_ids I tried for generation give pretty bad results. Not sure why. Model might just not be made for auto-regressive inference
           _UpperCAmelCase     :      Optional[int]														=       model.generate(A	,      do_sample=A		)
           self.assertListEqual(output_ids[0].cpu().numpy().tolist()	,      A		)





"""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       UpperCAmelCase_    (							unittest.TestCase		):



      def 		snake_case_   (	self	:      Union[str, Any]	,      A	:      Optional[Any]		):
           for model_result in results.values():
                for batch_size, sequence_length in zip(model_result["bs"]	,      model_result["ss"]		):
                     _UpperCAmelCase     :      List[str]														=       model_result["result"][batch_size][sequence_length]
                     self.assertIsNotNone(A		)



      def 		snake_case_   (	self	:      Optional[Any]		):
           _UpperCAmelCase     :      Any														=       "sshleifer/tiny-gpt2"
           _UpperCAmelCase     :      int														=       PyTorchBenchmarkArguments(
               models=[MODEL_ID]	,      training=A	,      inference=A	,      sequence_lengths=[8]	,      batch_sizes=[1]	,      multi_process=A	,      )
           _UpperCAmelCase     :      List[Any]														=       PyTorchBenchmark(A		)
           _UpperCAmelCase     :      List[Any]														=       benchmark.run()
           self.check_results_dict_not_empty(results.time_inference_result		)
           self.check_results_dict_not_empty(results.memory_inference_result		)



      def 		snake_case_   (	self	:      Dict		):
           _UpperCAmelCase     :      List[str]														=       "sgugger/tiny-distilbert-classification"
           _UpperCAmelCase     :      Optional[int]														=       PyTorchBenchmarkArguments(
               models=[MODEL_ID]	,      training=A	,      inference=A	,      sequence_lengths=[8]	,      batch_sizes=[1]	,      multi_process=A	,      only_pretrain_model=A	,      )
           _UpperCAmelCase     :      Dict														=       PyTorchBenchmark(A		)
           _UpperCAmelCase     :      Optional[Any]														=       benchmark.run()
           self.check_results_dict_not_empty(results.time_inference_result		)
           self.check_results_dict_not_empty(results.memory_inference_result		)



      def 		snake_case_   (	self	:      int		):
           _UpperCAmelCase     :      Any														=       "sshleifer/tiny-gpt2"
           _UpperCAmelCase     :      List[Any]														=       PyTorchBenchmarkArguments(
               models=[MODEL_ID]	,      training=A	,      inference=A	,      torchscript=A	,      sequence_lengths=[8]	,      batch_sizes=[1]	,      multi_process=A	,      )
           _UpperCAmelCase     :      Dict														=       PyTorchBenchmark(A		)
           _UpperCAmelCase     :      Optional[Any]														=       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 		snake_case_   (	self	:      List[str]		):
           _UpperCAmelCase     :      Union[str, Any]														=       "sshleifer/tiny-gpt2"
           _UpperCAmelCase     :      Optional[Any]														=       PyTorchBenchmarkArguments(
               models=[MODEL_ID]	,      training=A	,      inference=A	,      fpaa=A	,      sequence_lengths=[8]	,      batch_sizes=[1]	,      multi_process=A	,      )
           _UpperCAmelCase     :      List[str]														=       PyTorchBenchmark(A		)
           _UpperCAmelCase     :      List[Any]														=       benchmark.run()
           self.check_results_dict_not_empty(results.time_inference_result		)
           self.check_results_dict_not_empty(results.memory_inference_result		)



      def 		snake_case_   (	self	:      List[Any]		):
           _UpperCAmelCase     :      Optional[Any]														=       "sshleifer/tiny-gpt2"
           _UpperCAmelCase     :      int														=       AutoConfig.from_pretrained(A		)
           # set architectures equal to `None`
           _UpperCAmelCase     :      Optional[Any]														=       None
           _UpperCAmelCase     :      Optional[Any]														=       PyTorchBenchmarkArguments(
               models=[MODEL_ID]	,      training=A	,      inference=A	,      sequence_lengths=[8]	,      batch_sizes=[1]	,      multi_process=A	,      )
           _UpperCAmelCase     :      List[Any]														=       PyTorchBenchmark(A	,      configs=[config]		)
           _UpperCAmelCase     :      Optional[Any]														=       benchmark.run()
           self.check_results_dict_not_empty(results.time_inference_result		)
           self.check_results_dict_not_empty(results.memory_inference_result		)



      def 		snake_case_   (	self	:      int		):
           _UpperCAmelCase     :      Dict														=       "sshleifer/tiny-gpt2"
           _UpperCAmelCase     :      Union[str, Any]														=       PyTorchBenchmarkArguments(
               models=[MODEL_ID]	,      training=A	,      inference=A	,      sequence_lengths=[8]	,      batch_sizes=[1]	,      multi_process=A	,      )
           _UpperCAmelCase     :      Tuple														=       PyTorchBenchmark(A		)
           _UpperCAmelCase     :      Union[str, Any]														=       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 		snake_case_   (	self	:      List[Any]		):
           _UpperCAmelCase     :      Optional[int]														=       "sshleifer/tiny-gpt2"
           _UpperCAmelCase     :      str														=       PyTorchBenchmarkArguments(
               models=[MODEL_ID]	,      training=A	,      inference=A	,      sequence_lengths=[8]	,      batch_sizes=[1]	,      fpaa=A	,      multi_process=A	,      )
           _UpperCAmelCase     :      Tuple														=       PyTorchBenchmark(A		)
           _UpperCAmelCase     :      List[Any]														=       benchmark.run()
           self.check_results_dict_not_empty(results.time_train_result		)
           self.check_results_dict_not_empty(results.memory_train_result		)



      def 		snake_case_   (	self	:      List[Any]		):
           _UpperCAmelCase     :      Optional[Any]														=       "sshleifer/tiny-gpt2"
           _UpperCAmelCase     :      Union[str, Any]														=       AutoConfig.from_pretrained(A		)
           _UpperCAmelCase     :      Dict														=       PyTorchBenchmarkArguments(
               models=[MODEL_ID]	,      training=A	,      inference=A	,      sequence_lengths=[8]	,      batch_sizes=[1]	,      multi_process=A	,      )
           _UpperCAmelCase     :      List[Any]														=       PyTorchBenchmark(A	,      configs=[config]		)
           _UpperCAmelCase     :      List[str]														=       benchmark.run()
           self.check_results_dict_not_empty(results.time_inference_result		)
           self.check_results_dict_not_empty(results.memory_inference_result		)



      def 		snake_case_   (	self	:      str		):
           _UpperCAmelCase     :      List[str]														=       "sshleifer/tinier_bart"
           _UpperCAmelCase     :      Any														=       AutoConfig.from_pretrained(A		)
           _UpperCAmelCase     :      Optional[int]														=       PyTorchBenchmarkArguments(
               models=[MODEL_ID]	,      training=A	,      inference=A	,      sequence_lengths=[8]	,      batch_sizes=[1]	,      multi_process=A	,      )
           _UpperCAmelCase     :      int														=       PyTorchBenchmark(A	,      configs=[config]		)
           _UpperCAmelCase     :      Union[str, Any]														=       benchmark.run()
           self.check_results_dict_not_empty(results.time_inference_result		)
           self.check_results_dict_not_empty(results.memory_inference_result		)



      def 		snake_case_   (	self	:      Any		):
           _UpperCAmelCase     :      Tuple														=       "sshleifer/tiny-gpt2"
           _UpperCAmelCase     :      Tuple														=       AutoConfig.from_pretrained(A		)
           _UpperCAmelCase     :      List[Any]														=       PyTorchBenchmarkArguments(
               models=[MODEL_ID]	,      training=A	,      inference=A	,      sequence_lengths=[8]	,      batch_sizes=[1]	,      multi_process=A	,      )
           _UpperCAmelCase     :      int														=       PyTorchBenchmark(A	,      configs=[config]		)
           _UpperCAmelCase     :      Optional[int]														=       benchmark.run()
           self.check_results_dict_not_empty(results.time_train_result		)
           self.check_results_dict_not_empty(results.memory_train_result		)



      def 		snake_case_   (	self	:      str		):
           _UpperCAmelCase     :      Optional[int]														=       "sshleifer/tinier_bart"
           _UpperCAmelCase     :      int														=       AutoConfig.from_pretrained(A		)
           _UpperCAmelCase     :      List[Any]														=       PyTorchBenchmarkArguments(
               models=[MODEL_ID]	,      training=A	,      inference=A	,      sequence_lengths=[8]	,      batch_sizes=[1]	,      multi_process=A	,      )
           _UpperCAmelCase     :      str														=       PyTorchBenchmark(A	,      configs=[config]		)
           _UpperCAmelCase     :      int														=       benchmark.run()
           self.check_results_dict_not_empty(results.time_train_result		)
           self.check_results_dict_not_empty(results.memory_train_result		)



      def 		snake_case_   (	self	:      Dict		):
           _UpperCAmelCase     :      int														=       "sshleifer/tiny-gpt2"
           with tempfile.TemporaryDirectory() as tmp_dir:
                _UpperCAmelCase     :      Union[str, Any]														=       PyTorchBenchmarkArguments(
                    models=[MODEL_ID]	,      training=A	,      inference=A	,      save_to_csv=A	,      sequence_lengths=[8]	,      batch_sizes=[1]	,      inference_time_csv_file=os.path.join(A	,      "inf_time.csv"		)	,      train_memory_csv_file=os.path.join(A	,      "train_mem.csv"		)	,      inference_memory_csv_file=os.path.join(A	,      "inf_mem.csv"		)	,      train_time_csv_file=os.path.join(A	,      "train_time.csv"		)	,      env_info_csv_file=os.path.join(A	,      "env.csv"		)	,      multi_process=A	,      )
                _UpperCAmelCase     :      int														=       PyTorchBenchmark(A		)
                benchmark.run()
                self.assertTrue(Path(os.path.join(A	,      "inf_time.csv"		)		).exists()		)
                self.assertTrue(Path(os.path.join(A	,      "train_time.csv"		)		).exists()		)
                self.assertTrue(Path(os.path.join(A	,      "inf_mem.csv"		)		).exists()		)
                self.assertTrue(Path(os.path.join(A	,      "train_mem.csv"		)		).exists()		)
                self.assertTrue(Path(os.path.join(A	,      "env.csv"		)		).exists()		)



      def 		snake_case_   (	self	:      List[str]		):
           _UpperCAmelCase     :      List[Any]														=       "sshleifer/tiny-gpt2"

           def _check_summary_is_not_empty(A	:      int		):
                self.assertTrue(hasattr(A	,      "sequential"		)		)
                self.assertTrue(hasattr(A	,      "cumulative"		)		)
                self.assertTrue(hasattr(A	,      "current"		)		)
                self.assertTrue(hasattr(A	,      "total"		)		)

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