




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







   def 		UpperCamelCase__			(	self      ,							UpperCamelCase      )				->    Optional[Any]:
     for model_result in results.values():
       for batch_size, sequence_length in zip(model_result['bs']      ,							model_result['ss']      ):
         __a			=					model_result['result'][batch_size][sequence_length]
         self.assertIsNotNone(_lowerCAmelCase      )







   def 		UpperCamelCase__			(	self      )				->    List[Any]:
     __a			=					'sshleifer/tiny-gpt2'
     __a			=					TensorFlowBenchmarkArguments(
         models=[MODEL_ID]      ,							training=_lowerCAmelCase      ,							inference=_lowerCAmelCase      ,							sequence_lengths=[8]      ,							batch_sizes=[1]      ,							eager_mode=_lowerCAmelCase      ,							multi_process=_lowerCAmelCase      ,							)
     __a			=					TensorFlowBenchmark(_lowerCAmelCase      )
     __a			=					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      )				->    List[str]:
     __a			=					'sgugger/tiny-distilbert-classification'
     __a			=					TensorFlowBenchmarkArguments(
         models=[MODEL_ID]      ,							training=_lowerCAmelCase      ,							inference=_lowerCAmelCase      ,							sequence_lengths=[8]      ,							batch_sizes=[1]      ,							multi_process=_lowerCAmelCase      ,							only_pretrain_model=_lowerCAmelCase      ,							)
     __a			=					TensorFlowBenchmark(_lowerCAmelCase      )
     __a			=					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      )				->    int:
     __a			=					'sshleifer/tiny-gpt2'
     __a			=					TensorFlowBenchmarkArguments(
         models=[MODEL_ID]      ,							training=_lowerCAmelCase      ,							inference=_lowerCAmelCase      ,							sequence_lengths=[8]      ,							batch_sizes=[1]      ,							multi_process=_lowerCAmelCase      ,							)
     __a			=					TensorFlowBenchmark(_lowerCAmelCase      )
     __a			=					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]:
     __a			=					'sshleifer/tiny-gpt2'
     __a			=					AutoConfig.from_pretrained(_lowerCAmelCase      )
     __a			=					TensorFlowBenchmarkArguments(
         models=[MODEL_ID]      ,							training=_lowerCAmelCase      ,							inference=_lowerCAmelCase      ,							sequence_lengths=[8]      ,							batch_sizes=[1]      ,							eager_mode=_lowerCAmelCase      ,							multi_process=_lowerCAmelCase      ,							)
     __a			=					TensorFlowBenchmark(_lowerCAmelCase      ,							[config]      )
     __a			=					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      )				->    List[str]:
     __a			=					'sshleifer/tiny-gpt2'
     __a			=					AutoConfig.from_pretrained(_lowerCAmelCase      )
     __a			=					TensorFlowBenchmarkArguments(
         models=[MODEL_ID]      ,							training=_lowerCAmelCase      ,							inference=_lowerCAmelCase      ,							sequence_lengths=[8]      ,							batch_sizes=[1]      ,							multi_process=_lowerCAmelCase      ,							)
     __a			=					TensorFlowBenchmark(_lowerCAmelCase      ,							[config]      )
     __a			=					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      )				->    List[Any]:
     __a			=					'sshleifer/tiny-gpt2'
     __a			=					TensorFlowBenchmarkArguments(
         models=[MODEL_ID]      ,							training=_lowerCAmelCase      ,							inference=_lowerCAmelCase      ,							sequence_lengths=[8]      ,							batch_sizes=[1]      ,							multi_process=_lowerCAmelCase      ,							)
     __a			=					TensorFlowBenchmark(_lowerCAmelCase      )
     __a			=					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]:
     __a			=					'sshleifer/tiny-gpt2'
     __a			=					AutoConfig.from_pretrained(_lowerCAmelCase      )
     __a			=					TensorFlowBenchmarkArguments(
         models=[MODEL_ID]      ,							training=_lowerCAmelCase      ,							inference=_lowerCAmelCase      ,							sequence_lengths=[8]      ,							batch_sizes=[1]      ,							multi_process=_lowerCAmelCase      ,							)
     __a			=					TensorFlowBenchmark(_lowerCAmelCase      ,							[config]      )
     __a			=					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      )				->    Optional[Any]:
     __a			=					'patrickvonplaten/t5-tiny-random'
     __a			=					AutoConfig.from_pretrained(_lowerCAmelCase      )
     __a			=					TensorFlowBenchmarkArguments(
         models=[MODEL_ID]      ,							training=_lowerCAmelCase      ,							inference=_lowerCAmelCase      ,							sequence_lengths=[8]      ,							batch_sizes=[1]      ,							multi_process=_lowerCAmelCase      ,							)
     __a			=					TensorFlowBenchmark(_lowerCAmelCase      ,							configs=[config]      )
     __a			=					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 		UpperCamelCase__			(	self      )				->    str:
     __a			=					'sshleifer/tiny-gpt2'
     __a			=					TensorFlowBenchmarkArguments(
         models=[MODEL_ID]      ,							training=_lowerCAmelCase      ,							inference=_lowerCAmelCase      ,							sequence_lengths=[8]      ,							batch_sizes=[1]      ,							use_xla=_lowerCAmelCase      ,							multi_process=_lowerCAmelCase      ,							)
     __a			=					TensorFlowBenchmark(_lowerCAmelCase      )
     __a			=					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      )				->    Any:
     __a			=					'sshleifer/tiny-gpt2'
     with tempfile.TemporaryDirectory() as tmp_dir:
       __a			=					TensorFlowBenchmarkArguments(
           models=[MODEL_ID]      ,							inference=_lowerCAmelCase      ,							save_to_csv=_lowerCAmelCase      ,							sequence_lengths=[8]      ,							batch_sizes=[1]      ,							inference_time_csv_file=os.path.join(_lowerCAmelCase      ,							'inf_time.csv'      )      ,							inference_memory_csv_file=os.path.join(_lowerCAmelCase      ,							'inf_mem.csv'      )      ,							env_info_csv_file=os.path.join(_lowerCAmelCase      ,							'env.csv'      )      ,							multi_process=_lowerCAmelCase      ,							)
       __a			=					TensorFlowBenchmark(_lowerCAmelCase      )
       benchmark.run()
       self.assertTrue(Path(os.path.join(_lowerCAmelCase      ,							'inf_time.csv'      )      ).exists()      )
       self.assertTrue(Path(os.path.join(_lowerCAmelCase      ,							'inf_mem.csv'      )      ).exists()      )
       self.assertTrue(Path(os.path.join(_lowerCAmelCase      ,							'env.csv'      )      ).exists()      )







   def 		UpperCamelCase__			(	self      )				->    Dict:
     __a			=					'sshleifer/tiny-gpt2'

     def _check_summary_is_not_empty(UpperCamelCase      ):
       self.assertTrue(hasattr(_lowerCAmelCase      ,							'sequential'      )      )
       self.assertTrue(hasattr(_lowerCAmelCase      ,							'cumulative'      )      )
       self.assertTrue(hasattr(_lowerCAmelCase      ,							'current'      )      )
       self.assertTrue(hasattr(_lowerCAmelCase      ,							'total'      )      )

     with tempfile.TemporaryDirectory() as tmp_dir:
       __a			=					TensorFlowBenchmarkArguments(
           models=[MODEL_ID]      ,							inference=_lowerCAmelCase      ,							sequence_lengths=[8]      ,							batch_sizes=[1]      ,							log_filename=os.path.join(_lowerCAmelCase      ,							'log.txt'      )      ,							log_print=_lowerCAmelCase      ,							trace_memory_line_by_line=_lowerCAmelCase      ,							eager_mode=_lowerCAmelCase      ,							multi_process=_lowerCAmelCase      ,							)
       __a			=					TensorFlowBenchmark(_lowerCAmelCase      )
       __a			=					benchmark.run()
       _check_summary_is_not_empty(result.inference_summary      )
       self.assertTrue(Path(os.path.join(_lowerCAmelCase      ,							'log.txt'      )      ).exists()      )









'''simple docstring'''


import numpy as np

import datasets


UpperCAmelCase_ 		=      "\nCompute the Mahalanobis Distance\n\nMahalonobis distance is the distance between a point and a distribution.\nAnd not between two distinct points. It is effectively a multivariate equivalent of the Euclidean distance.\nIt was introduced by Prof. P. C. Mahalanobis in 1936\nand has been used in various statistical applications ever since\n[source: https://www.machinelearningplus.com/statistics/mahalanobis-distance/]\n"

UpperCAmelCase_ 		=      "\\n@article{de2000mahalanobis,\n  title={The mahalanobis distance},\n  author={De Maesschalck, Roy and Jouan-Rimbaud, Delphine and Massart, D{\'e}sir{\'e} L},\n  journal={Chemometrics and intelligent laboratory systems},\n  volume={50},\n  number={1},\n  pages={1--18},\n  year={2000},\n  publisher={Elsevier}\n}\n"

UpperCAmelCase_ 		=      "\nArgs:\n    X: List of datapoints to be compared with the `reference_distribution`.\n    reference_distribution: List of datapoints from the reference distribution we want to compare to.\nReturns:\n    mahalanobis: The Mahalonobis distance for each datapoint in `X`.\nExamples:\n\n    >>> mahalanobis_metric = datasets.load_metric(\"mahalanobis\")\n    >>> results = mahalanobis_metric.compute(reference_distribution=[[0, 1], [1, 0]], X=[[0, 1]])\n    >>> print(results)\n    {'mahalanobis': array([0.5])}\n"





@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION       ,							_KWARGS_DESCRIPTION )
class    __lowercase						(  datasets.Metric ):







   def 		UpperCamelCase__			(	self      )				->    Optional[Any]:
     return datasets.MetricInfo(
         description=_DESCRIPTION      ,							citation=_CITATION      ,							inputs_description=_KWARGS_DESCRIPTION      ,							features=datasets.Features(
             {
                 'X': datasets.Sequence(datasets.Value('float'      ,							id='sequence'      )      ,							id='X'      ),
             }      )      ,							)







   def 		UpperCamelCase__			(	self      ,							UpperCamelCase      ,							UpperCamelCase      )				->    Optional[int]:
     # convert to numpy arrays
     __a			=					np.array(UpperCamelCase      )
     __a			=					np.array(UpperCamelCase      )

     # Assert that arrays are 2D
     if len(X.shape      ) != 2:
       raise ValueError('Expected `X` to be a 2D vector'      )
     if len(reference_distribution.shape      ) != 2:
       raise ValueError('Expected `reference_distribution` to be a 2D vector'      )
     if reference_distribution.shape[0] < 2:
       raise ValueError(
           'Expected `reference_distribution` to be a 2D vector with more than one element in the first dimension'      )

     # Get mahalanobis distance for each prediction
     __a			=					X - np.mean(UpperCamelCase      )
     __a			=					np.cov(reference_distribution.T      )
     try:
       __a			=					np.linalg.inv(UpperCamelCase      )
     except np.linalg.LinAlgError:
       __a			=					np.linalg.pinv(UpperCamelCase      )
     __a			=					np.dot(UpperCamelCase      ,							UpperCamelCase      )
     __a			=					np.dot(UpperCamelCase      ,							X_minus_mu.T      ).diagonal()

     return {"mahalanobis": mahal_dist}



