





import os
import tempfile
import unittest
from pathlib import Path

from transformers import AutoConfig, is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
				import tensorflow as tf

				from transformers import TensorFlowBenchmark, TensorFlowBenchmarkArguments

@require_tf
class 	A_       (unittest.TestCase			):
	def      _lowercase				(    self							,					_A		):






				'''simple docstring'''







				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(_A		)
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				'''sshleifer/tiny-gpt2'''
				UpperCAmelCase   		=				TensorFlowBenchmarkArguments(
				    models=[MODEL_ID]							,					training=_A							,					inference=_A							,					sequence_lengths=[8]							,					batch_sizes=[1]							,					eager_mode=_A							,					multi_process=_A							,					)
				UpperCAmelCase   		=				TensorFlowBenchmark(_A		)
				UpperCAmelCase   		=				benchmark.run()
				self.check_results_dict_not_empty(results.time_inference_result		)
				self.check_results_dict_not_empty(results.memory_inference_result		)
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				'''sgugger/tiny-distilbert-classification'''
				UpperCAmelCase   		=				TensorFlowBenchmarkArguments(
				    models=[MODEL_ID]							,					training=_A							,					inference=_A							,					sequence_lengths=[8]							,					batch_sizes=[1]							,					multi_process=_A							,					only_pretrain_model=_A							,					)
				UpperCAmelCase   		=				TensorFlowBenchmark(_A		)
				UpperCAmelCase   		=				benchmark.run()
				self.check_results_dict_not_empty(results.time_inference_result		)
				self.check_results_dict_not_empty(results.memory_inference_result		)
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				'''sshleifer/tiny-gpt2'''
				UpperCAmelCase   		=				TensorFlowBenchmarkArguments(
				    models=[MODEL_ID]							,					training=_A							,					inference=_A							,					sequence_lengths=[8]							,					batch_sizes=[1]							,					multi_process=_A							,					)
				UpperCAmelCase   		=				TensorFlowBenchmark(_A		)
				UpperCAmelCase   		=				benchmark.run()
				self.check_results_dict_not_empty(results.time_inference_result		)
				self.check_results_dict_not_empty(results.memory_inference_result		)
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				'''sshleifer/tiny-gpt2'''
				UpperCAmelCase   		=				AutoConfig.from_pretrained(_A		)
				UpperCAmelCase   		=				TensorFlowBenchmarkArguments(
				    models=[MODEL_ID]							,					training=_A							,					inference=_A							,					sequence_lengths=[8]							,					batch_sizes=[1]							,					eager_mode=_A							,					multi_process=_A							,					)
				UpperCAmelCase   		=				TensorFlowBenchmark(_A							,					[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      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				'''sshleifer/tiny-gpt2'''
				UpperCAmelCase   		=				AutoConfig.from_pretrained(_A		)
				UpperCAmelCase   		=				TensorFlowBenchmarkArguments(
				    models=[MODEL_ID]							,					training=_A							,					inference=_A							,					sequence_lengths=[8]							,					batch_sizes=[1]							,					multi_process=_A							,					)
				UpperCAmelCase   		=				TensorFlowBenchmark(_A							,					[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      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				'''sshleifer/tiny-gpt2'''
				UpperCAmelCase   		=				TensorFlowBenchmarkArguments(
				    models=[MODEL_ID]							,					training=_A							,					inference=_A							,					sequence_lengths=[8]							,					batch_sizes=[1]							,					multi_process=_A							,					)
				UpperCAmelCase   		=				TensorFlowBenchmark(_A		)
				UpperCAmelCase   		=				benchmark.run()
				self.check_results_dict_not_empty(results.time_train_result		)
				self.check_results_dict_not_empty(results.memory_train_result		)
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				'''sshleifer/tiny-gpt2'''
				UpperCAmelCase   		=				AutoConfig.from_pretrained(_A		)
				UpperCAmelCase   		=				TensorFlowBenchmarkArguments(
				    models=[MODEL_ID]							,					training=_A							,					inference=_A							,					sequence_lengths=[8]							,					batch_sizes=[1]							,					multi_process=_A							,					)
				UpperCAmelCase   		=				TensorFlowBenchmark(_A							,					[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      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				'''patrickvonplaten/t5-tiny-random'''
				UpperCAmelCase   		=				AutoConfig.from_pretrained(_A		)
				UpperCAmelCase   		=				TensorFlowBenchmarkArguments(
				    models=[MODEL_ID]							,					training=_A							,					inference=_A							,					sequence_lengths=[8]							,					batch_sizes=[1]							,					multi_process=_A							,					)
				UpperCAmelCase   		=				TensorFlowBenchmark(_A							,					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		)
	@unittest.skipIf(is_tf_available() and len(tf.config.list_physical_devices('''GPU'''		)		) == 0							,					'''Cannot do xla on CPU.'''		)
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				'''sshleifer/tiny-gpt2'''
				UpperCAmelCase   		=				TensorFlowBenchmarkArguments(
				    models=[MODEL_ID]							,					training=_A							,					inference=_A							,					sequence_lengths=[8]							,					batch_sizes=[1]							,					use_xla=_A							,					multi_process=_A							,					)
				UpperCAmelCase   		=				TensorFlowBenchmark(_A		)
				UpperCAmelCase   		=				benchmark.run()
				self.check_results_dict_not_empty(results.time_inference_result		)
				self.check_results_dict_not_empty(results.memory_inference_result		)
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				'''sshleifer/tiny-gpt2'''
				with tempfile.TemporaryDirectory() as tmp_dir:
							UpperCAmelCase   		=				TensorFlowBenchmarkArguments(
							    models=[MODEL_ID]							,					inference=_A							,					save_to_csv=_A							,					sequence_lengths=[8]							,					batch_sizes=[1]							,					inference_time_csv_file=os.path.join(_A							,					'''inf_time.csv'''		)							,					inference_memory_csv_file=os.path.join(_A							,					'''inf_mem.csv'''		)							,					env_info_csv_file=os.path.join(_A							,					'''env.csv'''		)							,					multi_process=_A							,					)
							UpperCAmelCase   		=				TensorFlowBenchmark(_A		)
							benchmark.run()
							self.assertTrue(Path(os.path.join(_A							,					'''inf_time.csv'''		)		).exists()		)
							self.assertTrue(Path(os.path.join(_A							,					'''inf_mem.csv'''		)		).exists()		)
							self.assertTrue(Path(os.path.join(_A							,					'''env.csv'''		)		).exists()		)







	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				'''sshleifer/tiny-gpt2'''

				def _check_summary_is_not_empty(_A		):
							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   		=				TensorFlowBenchmarkArguments(
							    models=[MODEL_ID]							,					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							,					eager_mode=_A							,					multi_process=_A							,					)
							UpperCAmelCase   		=				TensorFlowBenchmark(_A		)
							UpperCAmelCase   		=				benchmark.run()
							_check_summary_is_not_empty(result.inference_summary		)
							self.assertTrue(Path(os.path.join(_A							,					'''log.txt'''		)		).exists()		)










import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow


if is_torch_available():
				import torch

				from transformers import XLMRobertaModel

@require_sentencepiece
@require_tokenizers
@require_torch
class 	A_       (unittest.TestCase			):
	@slow
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				XLMRobertaModel.from_pretrained('''xlm-roberta-base'''		)
				UpperCAmelCase   		=				torch.tensor([[0, 5_8_1, 1_0_2_6_9, 8_3, 9_9_9_4_2, 1_3_6, 6_0_7_4_2, 2_3, 7_0, 8_0_5_8_3, 1_8_2_7_6, 2]]		)
				# The dog is cute and lives in the garden house

				UpperCAmelCase   		=				torch.Size((1, 1_2, 7_6_8)		)  # batch_size, sequence_length, embedding_vector_dim
				UpperCAmelCase   		=				torch.tensor(
				    [[-0.01_01, 0.12_18, -0.08_03, 0.08_01, 0.13_27, 0.07_76, -0.12_15, 0.23_83, 0.33_38, 0.31_06, 0.03_00, 0.02_52]]		)
				#  xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base')
				#  xlmr.eval()
				#  expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1]
				with torch.no_grad():
							UpperCAmelCase   		=				model(_A		)['''last_hidden_state'''].detach()
				self.assertEqual(output.shape							,					_A		)
				# compare the actual values for a slice of last dim
				self.assertTrue(torch.allclose(output[:, :, -1]							,					_A							,					atol=1E-3		)		)







	@slow
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				XLMRobertaModel.from_pretrained('''xlm-roberta-large'''		)
				UpperCAmelCase   		=				torch.tensor([[0, 5_8_1, 1_0_2_6_9, 8_3, 9_9_9_4_2, 1_3_6, 6_0_7_4_2, 2_3, 7_0, 8_0_5_8_3, 1_8_2_7_6, 2]]		)
				# The dog is cute and lives in the garden house

				UpperCAmelCase   		=				torch.Size((1, 1_2, 1_0_2_4)		)  # batch_size, sequence_length, embedding_vector_dim
				UpperCAmelCase   		=				torch.tensor(
				    [[-0.06_99, -0.03_18, 0.07_05, -0.12_41, 0.09_99, -0.05_20, 0.10_04, -0.18_38, -0.47_04, 0.14_37, 0.08_21, 0.01_26]]		)
				#  xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.large')
				#  xlmr.eval()
				#  expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1]
				with torch.no_grad():
							UpperCAmelCase   		=				model(_A		)['''last_hidden_state'''].detach()
				self.assertEqual(output.shape							,					_A		)
				# compare the actual values for a slice of last dim
				self.assertTrue(torch.allclose(output[:, :, -1]							,					_A							,					atol=1E-3		)		)



