


'''simple docstring'''

import logging

import numpy as np
import pytest
from scipy.linalg import eigh

logging.basicConfig(level=logging.INFO, format="""%(message)s""")





def __snake_case      (   UpperCAmelCase_		:		np.ndarray	):
 return input_array.reshape((input_array.size, 1)	)





def __snake_case      (   UpperCAmelCase_		:		np.ndarray      ,       UpperCAmelCase_		:		np.ndarray      ,       UpperCAmelCase_		:		int	):
 lowerCamelCase_  				=						np.nan
 for i in range(UpperCAmelCase_	):
  lowerCamelCase_  				=						features[:, labels == i]
  lowerCamelCase_  				=						data.mean(1	)
  # Centralize the data of class i
  lowerCamelCase_  				=						data - column_reshape(UpperCAmelCase_	)
  if i > 0:
   # If covariance_sum is not None
   covariance_sum += np.dot(UpperCAmelCase_      ,       centered_data.T	)
  else:
   # If covariance_sum is np.nan (i.e. first loop)
   lowerCamelCase_  				=						np.dot(UpperCAmelCase_      ,       centered_data.T	)

 return covariance_sum / features.shape[1]





def __snake_case      (   UpperCAmelCase_		:		np.ndarray      ,       UpperCAmelCase_		:		np.ndarray      ,       UpperCAmelCase_		:		int	):
 lowerCamelCase_  				=						features.mean(1	)
 lowerCamelCase_  				=						np.nan
 for i in range(UpperCAmelCase_	):
  lowerCamelCase_  				=						features[:, labels == i]
  lowerCamelCase_  				=						data.shape[1]
  lowerCamelCase_  				=						data.mean(1	)
  if i > 0:
   # If covariance_sum is not None
   covariance_sum += device_data * np.dot(
       column_reshape(UpperCAmelCase_	) - column_reshape(UpperCAmelCase_	)      ,       (column_reshape(UpperCAmelCase_	) - column_reshape(UpperCAmelCase_	)).T      ,       )
  else:
   # If covariance_sum is np.nan (i.e. first loop)
   lowerCamelCase_  				=						device_data * np.dot(
       column_reshape(UpperCAmelCase_	) - column_reshape(UpperCAmelCase_	)      ,       (column_reshape(UpperCAmelCase_	) - column_reshape(UpperCAmelCase_	)).T      ,       )

 return covariance_sum / features.shape[1]





def __snake_case      (   UpperCAmelCase_		:		np.ndarray      ,       UpperCAmelCase_		:		int	):

 # Check if the features have been loaded
 if features.any():
  lowerCamelCase_  				=						features.mean(1	)
  # Center the dataset
  lowerCamelCase_  				=						features - np.reshape(UpperCAmelCase_      ,       (data_mean.size, 1)	)
  lowerCamelCase_  				=						np.dot(UpperCAmelCase_      ,       centered_data.T	) / features.shape[1]
  lowerCamelCase_				,lowerCamelCase_  				=						np.linalg.eigh(UpperCAmelCase_	)
  # Take all the columns in the reverse order (-1), and then takes only the first
  lowerCamelCase_  				=						eigenvectors[:, ::-1][:, 0:dimensions]
  # Project the database on the new space
  lowerCamelCase_  				=						np.dot(filtered_eigenvectors.T      ,       UpperCAmelCase_	)
  logging.info("Principal Component Analysis computed"	)

  return projected_data
 else:
  logging.basicConfig(level=logging.ERROR      ,       format="%(message)s"      ,       force=UpperCAmelCase_	)
  logging.error("Dataset empty"	)
  raise AssertionError





def __snake_case      (   UpperCAmelCase_		:		np.ndarray      ,       UpperCAmelCase_		:		np.ndarray      ,       UpperCAmelCase_		:		int      ,       UpperCAmelCase_		:		int	):
 assert classes > dimensions

 # Check if features have been already loaded
 if features.any:
  lowerCamelCase_				,lowerCamelCase_  				=						eigh(
      covariance_between_classes(UpperCAmelCase_      ,       UpperCAmelCase_      ,       UpperCAmelCase_	)      ,       covariance_within_classes(UpperCAmelCase_      ,       UpperCAmelCase_      ,       UpperCAmelCase_	)      ,       )
  lowerCamelCase_  				=						eigenvectors[:, ::-1][:, :dimensions]
  lowerCamelCase_				,lowerCamelCase_				,lowerCamelCase_  				=						np.linalg.svd(UpperCAmelCase_	)
  lowerCamelCase_  				=						svd_matrix[:, 0:dimensions]
  lowerCamelCase_  				=						np.dot(filtered_svd_matrix.T      ,       UpperCAmelCase_	)
  logging.info("Linear Discriminant Analysis computed"	)

  return projected_data
 else:
  logging.basicConfig(level=logging.ERROR      ,       format="%(message)s"      ,       force=UpperCAmelCase_	)
  logging.error("Dataset empty"	)
  raise AssertionError





def __snake_case      (   ):
 # Create dummy dataset with 2 classes and 3 features
 lowerCamelCase_  				=						np.array([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7]]	)
 lowerCamelCase_  				=						np.array([0, 0, 0, 1, 1]	)
 lowerCamelCase_  				=						2
 lowerCamelCase_  				=						2

 # Assert that the function raises an AssertionError if dimensions > classes
 with pytest.raises(UpperCAmelCase_	) as error_info:
  lowerCamelCase_  				=						linear_discriminant_analysis(
      UpperCAmelCase_      ,       UpperCAmelCase_      ,       UpperCAmelCase_      ,       UpperCAmelCase_	)
  if isinstance(UpperCAmelCase_      ,       np.ndarray	):
   raise AssertionError(
       "Did not raise AssertionError for dimensions > classes"	)
  assert error_info.type is AssertionError





def __snake_case      (   ):
 lowerCamelCase_  				=						np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]	)
 lowerCamelCase_  				=						2
 lowerCamelCase_  				=						np.array([[6.9282_0323, 8.6602_5404, 10.3923_0485], [3.0, 3.0, 3.0]]	)

 with pytest.raises(UpperCAmelCase_	) as error_info:
  lowerCamelCase_  				=						principal_component_analysis(UpperCAmelCase_      ,       UpperCAmelCase_	)
  if not np.allclose(UpperCAmelCase_      ,       UpperCAmelCase_	):
   raise AssertionError
  assert error_info.type is AssertionError


if __name__ == "__main__":
     import doctest

     doctest.testmod()







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





  """simple docstring"""




  def 						snake_case	(					self    ,						UpperCamelCase  ):







   """simple docstring"""
   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(UpperCamelCase  )






  def 						snake_case	(					self  ):







   """simple docstring"""
   lowerCamelCase_  				=						"sshleifer/tiny-gpt2"
   lowerCamelCase_  				=						TensorFlowBenchmarkArguments(
       models=[MODEL_ID]    ,						training=UpperCamelCase    ,						inference=UpperCamelCase    ,						sequence_lengths=[8]    ,						batch_sizes=[1]    ,						eager_mode=UpperCamelCase    ,						multi_process=UpperCamelCase    ,						)
   lowerCamelCase_  				=						TensorFlowBenchmark(UpperCamelCase  )
   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  ):







   """simple docstring"""
   lowerCamelCase_  				=						"sgugger/tiny-distilbert-classification"
   lowerCamelCase_  				=						TensorFlowBenchmarkArguments(
       models=[MODEL_ID]    ,						training=UpperCamelCase    ,						inference=UpperCamelCase    ,						sequence_lengths=[8]    ,						batch_sizes=[1]    ,						multi_process=UpperCamelCase    ,						only_pretrain_model=UpperCamelCase    ,						)
   lowerCamelCase_  				=						TensorFlowBenchmark(UpperCamelCase  )
   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  ):







   """simple docstring"""
   lowerCamelCase_  				=						"sshleifer/tiny-gpt2"
   lowerCamelCase_  				=						TensorFlowBenchmarkArguments(
       models=[MODEL_ID]    ,						training=UpperCamelCase    ,						inference=UpperCamelCase    ,						sequence_lengths=[8]    ,						batch_sizes=[1]    ,						multi_process=UpperCamelCase    ,						)
   lowerCamelCase_  				=						TensorFlowBenchmark(UpperCamelCase  )
   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  ):







   """simple docstring"""
   lowerCamelCase_  				=						"sshleifer/tiny-gpt2"
   lowerCamelCase_  				=						AutoConfig.from_pretrained(UpperCamelCase  )
   lowerCamelCase_  				=						TensorFlowBenchmarkArguments(
       models=[MODEL_ID]    ,						training=UpperCamelCase    ,						inference=UpperCamelCase    ,						sequence_lengths=[8]    ,						batch_sizes=[1]    ,						eager_mode=UpperCamelCase    ,						multi_process=UpperCamelCase    ,						)
   lowerCamelCase_  				=						TensorFlowBenchmark(UpperCamelCase    ,						[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  ):







   """simple docstring"""
   lowerCamelCase_  				=						"sshleifer/tiny-gpt2"
   lowerCamelCase_  				=						AutoConfig.from_pretrained(UpperCamelCase  )
   lowerCamelCase_  				=						TensorFlowBenchmarkArguments(
       models=[MODEL_ID]    ,						training=UpperCamelCase    ,						inference=UpperCamelCase    ,						sequence_lengths=[8]    ,						batch_sizes=[1]    ,						multi_process=UpperCamelCase    ,						)
   lowerCamelCase_  				=						TensorFlowBenchmark(UpperCamelCase    ,						[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  ):







   """simple docstring"""
   lowerCamelCase_  				=						"sshleifer/tiny-gpt2"
   lowerCamelCase_  				=						TensorFlowBenchmarkArguments(
       models=[MODEL_ID]    ,						training=UpperCamelCase    ,						inference=UpperCamelCase    ,						sequence_lengths=[8]    ,						batch_sizes=[1]    ,						multi_process=UpperCamelCase    ,						)
   lowerCamelCase_  				=						TensorFlowBenchmark(UpperCamelCase  )
   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  ):







   """simple docstring"""
   lowerCamelCase_  				=						"sshleifer/tiny-gpt2"
   lowerCamelCase_  				=						AutoConfig.from_pretrained(UpperCamelCase  )
   lowerCamelCase_  				=						TensorFlowBenchmarkArguments(
       models=[MODEL_ID]    ,						training=UpperCamelCase    ,						inference=UpperCamelCase    ,						sequence_lengths=[8]    ,						batch_sizes=[1]    ,						multi_process=UpperCamelCase    ,						)
   lowerCamelCase_  				=						TensorFlowBenchmark(UpperCamelCase    ,						[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  ):







   """simple docstring"""
   lowerCamelCase_  				=						"patrickvonplaten/t5-tiny-random"
   lowerCamelCase_  				=						AutoConfig.from_pretrained(UpperCamelCase  )
   lowerCamelCase_  				=						TensorFlowBenchmarkArguments(
       models=[MODEL_ID]    ,						training=UpperCamelCase    ,						inference=UpperCamelCase    ,						sequence_lengths=[8]    ,						batch_sizes=[1]    ,						multi_process=UpperCamelCase    ,						)
   lowerCamelCase_  				=						TensorFlowBenchmark(UpperCamelCase    ,						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  ):







   """simple docstring"""
   lowerCamelCase_  				=						"sshleifer/tiny-gpt2"
   lowerCamelCase_  				=						TensorFlowBenchmarkArguments(
       models=[MODEL_ID]    ,						training=UpperCamelCase    ,						inference=UpperCamelCase    ,						sequence_lengths=[8]    ,						batch_sizes=[1]    ,						use_xla=UpperCamelCase    ,						multi_process=UpperCamelCase    ,						)
   lowerCamelCase_  				=						TensorFlowBenchmark(UpperCamelCase  )
   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  ):







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







  def 						snake_case	(					self  ):







   """simple docstring"""
   lowerCamelCase_  				=						"sshleifer/tiny-gpt2"

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

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



