


"""simple docstring"""


from typing import List, Optional, Union

import numpy as np
import tensorflow as tf

from .utils import logging


A  :     Optional[Any]    					=    logging.get_logger(__name__)


def _lowerCamelCase			(	_UpperCamelCase   ):

			'''simple docstring'''




			if isinstance(_snake_case					,				np.ndarray   ):
						return list(tensor.shape   )

			__lowerCAmelCase       					=    tf.shape(_snake_case   )

			if tensor.shape == tf.TensorShape(_snake_case   ):
						return dynamic

			__lowerCAmelCase       					=    tensor.shape.as_list()

			return [dynamic[i] if s is None else s for i, s in enumerate(_snake_case   )]


def _lowerCamelCase			(	_UpperCamelCase					,				_UpperCamelCase = None					,				_UpperCamelCase = None   ):

			'''simple docstring'''




			return tf.nn.softmax(logits=logits + 1e-9					,				axis=_snake_case					,				name=_snake_case   )


def _lowerCamelCase			(	_UpperCamelCase					,				_UpperCamelCase					,				_UpperCamelCase					,				_UpperCamelCase=1e-5					,				_UpperCamelCase=-1   ):

			'''simple docstring'''




			if weight.shape.rank != 1 or bias.shape.rank != 1 or not isinstance(_snake_case					,				_snake_case   ):
						raise NotImplementedError("Only 1D weight and bias tensors are supported for now, with only a single axis."   )

			# Get mean and variance on the axis to be normalized
			__lowerCAmelCase					,			__lowerCAmelCase       					=    tf.nn.moments(_snake_case					,				axes=[axis]					,				keepdims=_snake_case   )

			if axis != -1:
						# Reshape scale and weight to have the same rank as inputs, but with 1 dimensions
						# on every dimension except axis
						__lowerCAmelCase       					=    [1] * inputs.shape.rank
						__lowerCAmelCase       					=    shape_list(_snake_case   )[axis]
						__lowerCAmelCase       					=    tf.reshape(_snake_case					,				_snake_case   )
						__lowerCAmelCase       					=    tf.reshape(_snake_case					,				_snake_case   )

			# Compute layer normalization using the batch_normalization
			# function.
			__lowerCAmelCase       					=    tf.nn.batch_normalization(
			    _snake_case					,				_snake_case					,				_snake_case					,				offset=_snake_case					,				scale=_snake_case					,				variance_epsilon=_snake_case					,				)
			return outputs


def _lowerCamelCase			(	_UpperCamelCase					,				_UpperCamelCase=0					,				_UpperCamelCase=-1   ):

			'''simple docstring'''




			if end_dim < 0:
						end_dim += input.shape.rank
			if start_dim < 0:
						start_dim += input.shape.rank

			if start_dim == end_dim:
						return input

			__lowerCAmelCase       					=    tf.shape(_snake_case   )
			__lowerCAmelCase       					=    tf.math.reduce_prod(in_shape[start_dim : end_dim + 1]   )
			__lowerCAmelCase       					=    tf.concat([in_shape[:start_dim], [flattened_dim], in_shape[end_dim + 1 :]]					,				axis=0   )
			return tf.reshape(_snake_case					,				_snake_case   )


def _lowerCamelCase			(	_UpperCamelCase   ):

			'''simple docstring'''




			if not isinstance(_snake_case					,				tf.Tensor   ):
						__lowerCAmelCase       					=    tf.convert_to_tensor(_snake_case   )  # Catches stray NumPy inputs
			if encoder_attention_mask.shape.rank == 3:
						__lowerCAmelCase       					=    encoder_attention_mask[:, None, :, :]
			if encoder_attention_mask.shape.rank == 2:
						__lowerCAmelCase       					=    encoder_attention_mask[:, None, None, :]
			# T5 has a mask that can compare sequence ids, we can simulate this here with this transposition
			# Cf. https://github.com/tensorflow/mesh/blob/8d2465e9bc93129b913b5ccc6a59aa97abd96ec6/mesh_tensorflow
			# /transformer/transformer_layers.py#L270
			# encoder_extended_attention_mask = (encoder_extended_attention_mask ==
			# encoder_extended_attention_mask.transpose(-1, -2))
			__lowerCAmelCase       					=    (
			    tf.cast(1					,				encoder_attention_mask.dtype   ) - encoder_extended_attention_mask
			) * encoder_extended_attention_mask.dtype.min

			return encoder_extended_attention_mask


def _lowerCamelCase			(	_UpperCamelCase					,				_UpperCamelCase					,				_UpperCamelCase = "input_ids"   ):

			'''simple docstring'''




			tf.debugging.assert_less(
			    _snake_case					,				tf.cast(_snake_case					,				dtype=tensor.dtype   )					,				message=(
			        f"The maximum value of {tensor_name} ({tf.math.reduce_max(_snake_case   )}) must be smaller than the embedding "
			        f"layer\'s input dimension ({embed_dim}). The likely cause is some problem at tokenization time."
			    )					,				)


def _lowerCamelCase			(	_UpperCamelCase					,				_UpperCamelCase					,				_UpperCamelCase   ):

			'''simple docstring'''




			__lowerCAmelCase       					=    6_4512
			# Check that no item in `data` is larger than `HDF5_OBJECT_HEADER_LIMIT`
			# because in that case even chunking the array would not make the saving
			# possible.
			__lowerCAmelCase       					=    [x for x in data if len(_snake_case   ) > HDF5_OBJECT_HEADER_LIMIT]

			# Expecting this to never be true.
			if bad_attributes:
						raise RuntimeError(
						    "The following attributes cannot be saved to HDF5 file because "
						    f"they are larger than {HDF5_OBJECT_HEADER_LIMIT} "
						    f"bytes: {bad_attributes}"   )

			__lowerCAmelCase       					=    np.asarray(_snake_case   )

			__lowerCAmelCase       					=    1
			__lowerCAmelCase       					=    np.array_split(_snake_case					,				_snake_case   )

			# This will never loop forever thanks to the test above.
			while any(x.nbytes > HDF5_OBJECT_HEADER_LIMIT for x in chunked_data   ):
						num_chunks += 1
						__lowerCAmelCase       					=    np.array_split(_snake_case					,				_snake_case   )

			if num_chunks > 1:
						for chunk_id, chunk_data in enumerate(_snake_case   ):
									__lowerCAmelCase       					=    chunk_data
			else:
						__lowerCAmelCase       					=    data


def _lowerCamelCase			(	_UpperCamelCase					,				_UpperCamelCase   ):

			'''simple docstring'''




			if name in group.attrs:
						__lowerCAmelCase       					=    [n.decode("utf8"   ) if hasattr(_snake_case					,				"decode"   ) else n for n in group.attrs[name]]
			else:
						__lowerCAmelCase       					=    []
						__lowerCAmelCase       					=    0
						while "%s%d" % (name, chunk_id) in group.attrs:
									data.extend(
									    [n.decode("utf8"   ) if hasattr(_snake_case					,				"decode"   ) else n for n in group.attrs["%s%d" % (name, chunk_id)]]   )
									chunk_id += 1
			return data


def _lowerCamelCase			(	_UpperCamelCase   ):

			'''simple docstring'''




			def _expand_single_ad_tensor(_UpperCamelCase   ):
						if isinstance(_snake_case					,				tf.Tensor   ) and t.shape.rank == 1:
									return tf.expand_dims(_snake_case					,				axis=-1   )
						return t

			return tf.nest.map_structure(_expand_single_ad_tensor					,				_snake_case   )








"""simple docstring"""


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







							def     snake_case       (  self					,     __a			):
										for model_result in results.values():
													for batch_size, sequence_length in zip(model_result["bs"]					,     model_result["ss"]			):
																__lowerCAmelCase       					=    model_result["result"][batch_size][sequence_length]
																self.assertIsNotNone(__a			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     eager_mode=__a					,     multi_process=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a			)
										__lowerCAmelCase       					=    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			):
										__lowerCAmelCase       					=    "sgugger/tiny-distilbert-classification"
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     multi_process=__a					,     only_pretrain_model=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a			)
										__lowerCAmelCase       					=    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			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     multi_process=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a			)
										__lowerCAmelCase       					=    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			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"
										__lowerCAmelCase       					=    AutoConfig.from_pretrained(__a			)
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     eager_mode=__a					,     multi_process=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a					,     [config]			)
										__lowerCAmelCase       					=    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			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"
										__lowerCAmelCase       					=    AutoConfig.from_pretrained(__a			)
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     multi_process=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a					,     [config]			)
										__lowerCAmelCase       					=    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			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     multi_process=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a			)
										__lowerCAmelCase       					=    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			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"
										__lowerCAmelCase       					=    AutoConfig.from_pretrained(__a			)
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     multi_process=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a					,     [config]			)
										__lowerCAmelCase       					=    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			):
										__lowerCAmelCase       					=    "patrickvonplaten/t5-tiny-random"
										__lowerCAmelCase       					=    AutoConfig.from_pretrained(__a			)
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     multi_process=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a					,     configs=[config]			)
										__lowerCAmelCase       					=    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     snake_case       (  self			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     use_xla=__a					,     multi_process=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a			)
										__lowerCAmelCase       					=    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			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"
										with tempfile.TemporaryDirectory() as tmp_dir:
													__lowerCAmelCase       					=    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					,     )
													__lowerCAmelCase       					=    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     snake_case       (  self			):
										__lowerCAmelCase       					=    "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:
													__lowerCAmelCase       					=    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					,     )
													__lowerCAmelCase       					=    TensorFlowBenchmark(__a			)
													__lowerCAmelCase       					=    benchmark.run()
													_check_summary_is_not_empty(result.inference_summary			)
													self.assertTrue(Path(os.path.join(__a					,     "log.txt"			)			).exists()			)




