



"""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 			lowercase__			(      unittest.TestCase    ):
       '''simple docstring'''







       def        lowerCamelCase_   (  self	,       snake_case						)			->							int:
         for model_result in results.values():
           for batch_size, sequence_length in zip(model_result['bs']	,       model_result['ss']						):
             _UpperCAmelCase							 =		model_result["result"][batch_size][sequence_length]
             self.assertIsNotNone(snake_case__						)






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






       def        lowerCamelCase_   (  self						)			->							str:
         _UpperCAmelCase							 =		"sgugger/tiny-distilbert-classification"
         _UpperCAmelCase							 =		PyTorchBenchmarkArguments(
             models=[MODEL_ID]	,       training=snake_case__	,       inference=snake_case__	,       sequence_lengths=[8]	,       batch_sizes=[1]	,       multi_process=snake_case__	,       only_pretrain_model=snake_case__	,       )
         _UpperCAmelCase							 =		PyTorchBenchmark(snake_case__						)
         _UpperCAmelCase							 =		benchmark.run()
         self.check_results_dict_not_empty(results.time_inference_result						)
         self.check_results_dict_not_empty(results.memory_inference_result						)






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






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






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






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






       def        lowerCamelCase_   (  self						)			->							List[Any]:
         _UpperCAmelCase							 =		"sshleifer/tinier_bart"
         _UpperCAmelCase							 =		AutoConfig.from_pretrained(snake_case__						)
         _UpperCAmelCase							 =		PyTorchBenchmarkArguments(
             models=[MODEL_ID]	,       training=snake_case__	,       inference=snake_case__	,       sequence_lengths=[8]	,       batch_sizes=[1]	,       multi_process=snake_case__	,       )
         _UpperCAmelCase							 =		PyTorchBenchmark(snake_case__	,       configs=[config]						)
         _UpperCAmelCase							 =		benchmark.run()
         self.check_results_dict_not_empty(results.time_inference_result						)
         self.check_results_dict_not_empty(results.memory_inference_result						)






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






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






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






       def        lowerCamelCase_   (  self						)			->							Dict:
         _UpperCAmelCase							 =		"sshleifer/tiny-gpt2"

         def _check_summary_is_not_empty(snake_case						):
           self.assertTrue(hasattr(snake_case__	,       'sequential'						)						)
           self.assertTrue(hasattr(snake_case__	,       'cumulative'						)						)
           self.assertTrue(hasattr(snake_case__	,       'current'						)						)
           self.assertTrue(hasattr(snake_case__	,       'total'						)						)

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







"""simple docstring"""





import unittest

import numpy as np

from transformers.testing_utils import require_flax, require_tf, require_torch
from transformers.utils import (
    expand_dims,
    flatten_dict,
    is_flax_available,
    is_tf_available,
    is_torch_available,
    reshape,
    squeeze,
    transpose,
)


if is_flax_available():
					import jax.numpy as jnp

if is_tf_available():
					import tensorflow as tf

if is_torch_available():
					import torch




class 			lowercase__			(      unittest.TestCase    ):
							'''simple docstring'''







							def        lowerCamelCase_   (  self						)			->							Dict:
									_UpperCAmelCase							 =		{
									    'task_specific_params': {
									        'summarization': {'length_penalty': 1.0, 'max_length': 128, 'min_length': 12, 'num_beams': 4},
									        'summarization_cnn': {'length_penalty': 2.0, 'max_length': 142, 'min_length': 56, 'num_beams': 4},
									        'summarization_xsum': {'length_penalty': 1.0, 'max_length': 62, 'min_length': 11, 'num_beams': 6},
									    }
									}
									_UpperCAmelCase							 =		{
									    'task_specific_params.summarization.length_penalty': 1.0,
									    'task_specific_params.summarization.max_length': 128,
									    'task_specific_params.summarization.min_length': 12,
									    'task_specific_params.summarization.num_beams': 4,
									    'task_specific_params.summarization_cnn.length_penalty': 2.0,
									    'task_specific_params.summarization_cnn.max_length': 142,
									    'task_specific_params.summarization_cnn.min_length': 56,
									    'task_specific_params.summarization_cnn.num_beams': 4,
									    'task_specific_params.summarization_xsum.length_penalty': 1.0,
									    'task_specific_params.summarization_xsum.max_length': 62,
									    'task_specific_params.summarization_xsum.min_length': 11,
									    'task_specific_params.summarization_xsum.num_beams': 6,
									}

									self.assertEqual(flatten_dict(snake_case						)	,       snake_case						)






							def        lowerCamelCase_   (  self						)			->							List[str]:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									self.assertTrue(np.allclose(transpose(snake_case						)	,       x.transpose()						)						)

									_UpperCAmelCase							 =		np.random.randn(3	,       4	,       5						)
									self.assertTrue(np.allclose(transpose(snake_case	,       axes=(1, 2, 0)						)	,       x.transpose((1, 2, 0)						)						)						)






							@require_torch
							def        lowerCamelCase_   (  self						)			->							List[str]:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									_UpperCAmelCase							 =		torch.tensor(snake_case						)
									self.assertTrue(np.allclose(transpose(snake_case						)	,       transpose(snake_case						).numpy()						)						)

									_UpperCAmelCase							 =		np.random.randn(3	,       4	,       5						)
									_UpperCAmelCase							 =		torch.tensor(snake_case						)
									self.assertTrue(np.allclose(transpose(snake_case	,       axes=(1, 2, 0)						)	,       transpose(snake_case	,       axes=(1, 2, 0)						).numpy()						)						)






							@require_tf
							def        lowerCamelCase_   (  self						)			->							Union[str, Any]:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									_UpperCAmelCase							 =		tf.constant(snake_case						)
									self.assertTrue(np.allclose(transpose(snake_case						)	,       transpose(snake_case						).numpy()						)						)

									_UpperCAmelCase							 =		np.random.randn(3	,       4	,       5						)
									_UpperCAmelCase							 =		tf.constant(snake_case						)
									self.assertTrue(np.allclose(transpose(snake_case	,       axes=(1, 2, 0)						)	,       transpose(snake_case	,       axes=(1, 2, 0)						).numpy()						)						)






							@require_flax
							def        lowerCamelCase_   (  self						)			->							Union[str, Any]:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									_UpperCAmelCase							 =		jnp.array(snake_case						)
									self.assertTrue(np.allclose(transpose(snake_case						)	,       np.asarray(transpose(snake_case						)						)						)						)

									_UpperCAmelCase							 =		np.random.randn(3	,       4	,       5						)
									_UpperCAmelCase							 =		jnp.array(snake_case						)
									self.assertTrue(np.allclose(transpose(snake_case	,       axes=(1, 2, 0)						)	,       np.asarray(transpose(snake_case	,       axes=(1, 2, 0)						)						)						)						)






							def        lowerCamelCase_   (  self						)			->							List[str]:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									self.assertTrue(np.allclose(reshape(snake_case	,       (4, 3)						)	,       np.reshape(snake_case	,       (4, 3)						)						)						)

									_UpperCAmelCase							 =		np.random.randn(3	,       4	,       5						)
									self.assertTrue(np.allclose(reshape(snake_case	,       (12, 5)						)	,       np.reshape(snake_case	,       (12, 5)						)						)						)






							@require_torch
							def        lowerCamelCase_   (  self						)			->							Optional[Any]:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									_UpperCAmelCase							 =		torch.tensor(snake_case						)
									self.assertTrue(np.allclose(reshape(snake_case	,       (4, 3)						)	,       reshape(snake_case	,       (4, 3)						).numpy()						)						)

									_UpperCAmelCase							 =		np.random.randn(3	,       4	,       5						)
									_UpperCAmelCase							 =		torch.tensor(snake_case						)
									self.assertTrue(np.allclose(reshape(snake_case	,       (12, 5)						)	,       reshape(snake_case	,       (12, 5)						).numpy()						)						)






							@require_tf
							def        lowerCamelCase_   (  self						)			->							Union[str, Any]:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									_UpperCAmelCase							 =		tf.constant(snake_case						)
									self.assertTrue(np.allclose(reshape(snake_case	,       (4, 3)						)	,       reshape(snake_case	,       (4, 3)						).numpy()						)						)

									_UpperCAmelCase							 =		np.random.randn(3	,       4	,       5						)
									_UpperCAmelCase							 =		tf.constant(snake_case						)
									self.assertTrue(np.allclose(reshape(snake_case	,       (12, 5)						)	,       reshape(snake_case	,       (12, 5)						).numpy()						)						)






							@require_flax
							def        lowerCamelCase_   (  self						)			->							Tuple:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									_UpperCAmelCase							 =		jnp.array(snake_case						)
									self.assertTrue(np.allclose(reshape(snake_case	,       (4, 3)						)	,       np.asarray(reshape(snake_case	,       (4, 3)						)						)						)						)

									_UpperCAmelCase							 =		np.random.randn(3	,       4	,       5						)
									_UpperCAmelCase							 =		jnp.array(snake_case						)
									self.assertTrue(np.allclose(reshape(snake_case	,       (12, 5)						)	,       np.asarray(reshape(snake_case	,       (12, 5)						)						)						)						)






							def        lowerCamelCase_   (  self						)			->							str:
									_UpperCAmelCase							 =		np.random.randn(1	,       3	,       4						)
									self.assertTrue(np.allclose(squeeze(snake_case						)	,       np.squeeze(snake_case						)						)						)

									_UpperCAmelCase							 =		np.random.randn(1	,       4	,       1	,       5						)
									self.assertTrue(np.allclose(squeeze(snake_case	,       axis=2						)	,       np.squeeze(snake_case	,       axis=2						)						)						)






							@require_torch
							def        lowerCamelCase_   (  self						)			->							Union[str, Any]:
									_UpperCAmelCase							 =		np.random.randn(1	,       3	,       4						)
									_UpperCAmelCase							 =		torch.tensor(snake_case						)
									self.assertTrue(np.allclose(squeeze(snake_case						)	,       squeeze(snake_case						).numpy()						)						)

									_UpperCAmelCase							 =		np.random.randn(1	,       4	,       1	,       5						)
									_UpperCAmelCase							 =		torch.tensor(snake_case						)
									self.assertTrue(np.allclose(squeeze(snake_case	,       axis=2						)	,       squeeze(snake_case	,       axis=2						).numpy()						)						)






							@require_tf
							def        lowerCamelCase_   (  self						)			->							Optional[int]:
									_UpperCAmelCase							 =		np.random.randn(1	,       3	,       4						)
									_UpperCAmelCase							 =		tf.constant(snake_case						)
									self.assertTrue(np.allclose(squeeze(snake_case						)	,       squeeze(snake_case						).numpy()						)						)

									_UpperCAmelCase							 =		np.random.randn(1	,       4	,       1	,       5						)
									_UpperCAmelCase							 =		tf.constant(snake_case						)
									self.assertTrue(np.allclose(squeeze(snake_case	,       axis=2						)	,       squeeze(snake_case	,       axis=2						).numpy()						)						)






							@require_flax
							def        lowerCamelCase_   (  self						)			->							str:
									_UpperCAmelCase							 =		np.random.randn(1	,       3	,       4						)
									_UpperCAmelCase							 =		jnp.array(snake_case						)
									self.assertTrue(np.allclose(squeeze(snake_case						)	,       np.asarray(squeeze(snake_case						)						)						)						)

									_UpperCAmelCase							 =		np.random.randn(1	,       4	,       1	,       5						)
									_UpperCAmelCase							 =		jnp.array(snake_case						)
									self.assertTrue(np.allclose(squeeze(snake_case	,       axis=2						)	,       np.asarray(squeeze(snake_case	,       axis=2						)						)						)						)






							def        lowerCamelCase_   (  self						)			->							Union[str, Any]:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									self.assertTrue(np.allclose(expand_dims(snake_case	,       axis=1						)	,       np.expand_dims(snake_case	,       axis=1						)						)						)






							@require_torch
							def        lowerCamelCase_   (  self						)			->							Dict:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									_UpperCAmelCase							 =		torch.tensor(snake_case						)
									self.assertTrue(np.allclose(expand_dims(snake_case	,       axis=1						)	,       expand_dims(snake_case	,       axis=1						).numpy()						)						)






							@require_tf
							def        lowerCamelCase_   (  self						)			->							int:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									_UpperCAmelCase							 =		tf.constant(snake_case						)
									self.assertTrue(np.allclose(expand_dims(snake_case	,       axis=1						)	,       expand_dims(snake_case	,       axis=1						).numpy()						)						)






							@require_flax
							def        lowerCamelCase_   (  self						)			->							str:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									_UpperCAmelCase							 =		jnp.array(snake_case						)
									self.assertTrue(np.allclose(expand_dims(snake_case	,       axis=1						)	,       np.asarray(expand_dims(snake_case	,       axis=1						)						)						)						)


