




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


 '''simple docstring'''







 def  lowerCAmelCase_    (				self    :						List[Any]							,							_lowerCAmelCase    :						Union[str, Any]		):
       for model_result in results.values():
             for batch_size, sequence_length in zip(model_result['bs']							,							model_result['ss']		):
                   SCREAMING_SNAKE_CASE_      		=     model_result['result'][batch_size][sequence_length]
                   self.assertIsNotNone(_lowerCAmelCase		)



 def  lowerCAmelCase_    (				self    :						Optional[int]		):
       SCREAMING_SNAKE_CASE_      		=     'sshleifer/tiny-gpt2'
       SCREAMING_SNAKE_CASE_      		=     TensorFlowBenchmarkArguments(
           models=[MODEL_ID]							,							training=_lowerCAmelCase							,							inference=_lowerCAmelCase							,							sequence_lengths=[8]							,							batch_sizes=[1]							,							eager_mode=_lowerCAmelCase							,							multi_process=_lowerCAmelCase							,							)
       SCREAMING_SNAKE_CASE_      		=     TensorFlowBenchmark(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     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[int]		):
       SCREAMING_SNAKE_CASE_      		=     'sgugger/tiny-distilbert-classification'
       SCREAMING_SNAKE_CASE_      		=     TensorFlowBenchmarkArguments(
           models=[MODEL_ID]							,							training=_lowerCAmelCase							,							inference=_lowerCAmelCase							,							sequence_lengths=[8]							,							batch_sizes=[1]							,							multi_process=_lowerCAmelCase							,							only_pretrain_model=_lowerCAmelCase							,							)
       SCREAMING_SNAKE_CASE_      		=     TensorFlowBenchmark(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     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    :						int		):
       SCREAMING_SNAKE_CASE_      		=     'sshleifer/tiny-gpt2'
       SCREAMING_SNAKE_CASE_      		=     TensorFlowBenchmarkArguments(
           models=[MODEL_ID]							,							training=_lowerCAmelCase							,							inference=_lowerCAmelCase							,							sequence_lengths=[8]							,							batch_sizes=[1]							,							multi_process=_lowerCAmelCase							,							)
       SCREAMING_SNAKE_CASE_      		=     TensorFlowBenchmark(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     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]		):
       SCREAMING_SNAKE_CASE_      		=     'sshleifer/tiny-gpt2'
       SCREAMING_SNAKE_CASE_      		=     AutoConfig.from_pretrained(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     TensorFlowBenchmarkArguments(
           models=[MODEL_ID]							,							training=_lowerCAmelCase							,							inference=_lowerCAmelCase							,							sequence_lengths=[8]							,							batch_sizes=[1]							,							eager_mode=_lowerCAmelCase							,							multi_process=_lowerCAmelCase							,							)
       SCREAMING_SNAKE_CASE_      		=     TensorFlowBenchmark(_lowerCAmelCase							,							[config]		)
       SCREAMING_SNAKE_CASE_      		=     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]		):
       SCREAMING_SNAKE_CASE_      		=     'sshleifer/tiny-gpt2'
       SCREAMING_SNAKE_CASE_      		=     AutoConfig.from_pretrained(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     TensorFlowBenchmarkArguments(
           models=[MODEL_ID]							,							training=_lowerCAmelCase							,							inference=_lowerCAmelCase							,							sequence_lengths=[8]							,							batch_sizes=[1]							,							multi_process=_lowerCAmelCase							,							)
       SCREAMING_SNAKE_CASE_      		=     TensorFlowBenchmark(_lowerCAmelCase							,							[config]		)
       SCREAMING_SNAKE_CASE_      		=     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]		):
       SCREAMING_SNAKE_CASE_      		=     'sshleifer/tiny-gpt2'
       SCREAMING_SNAKE_CASE_      		=     TensorFlowBenchmarkArguments(
           models=[MODEL_ID]							,							training=_lowerCAmelCase							,							inference=_lowerCAmelCase							,							sequence_lengths=[8]							,							batch_sizes=[1]							,							multi_process=_lowerCAmelCase							,							)
       SCREAMING_SNAKE_CASE_      		=     TensorFlowBenchmark(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     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    :						int		):
       SCREAMING_SNAKE_CASE_      		=     'sshleifer/tiny-gpt2'
       SCREAMING_SNAKE_CASE_      		=     AutoConfig.from_pretrained(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     TensorFlowBenchmarkArguments(
           models=[MODEL_ID]							,							training=_lowerCAmelCase							,							inference=_lowerCAmelCase							,							sequence_lengths=[8]							,							batch_sizes=[1]							,							multi_process=_lowerCAmelCase							,							)
       SCREAMING_SNAKE_CASE_      		=     TensorFlowBenchmark(_lowerCAmelCase							,							[config]		)
       SCREAMING_SNAKE_CASE_      		=     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    :						Dict		):
       SCREAMING_SNAKE_CASE_      		=     'patrickvonplaten/t5-tiny-random'
       SCREAMING_SNAKE_CASE_      		=     AutoConfig.from_pretrained(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     TensorFlowBenchmarkArguments(
           models=[MODEL_ID]							,							training=_lowerCAmelCase							,							inference=_lowerCAmelCase							,							sequence_lengths=[8]							,							batch_sizes=[1]							,							multi_process=_lowerCAmelCase							,							)
       SCREAMING_SNAKE_CASE_      		=     TensorFlowBenchmark(_lowerCAmelCase							,							configs=[config]		)
       SCREAMING_SNAKE_CASE_      		=     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  lowerCAmelCase_    (				self    :						List[Any]		):
       SCREAMING_SNAKE_CASE_      		=     'sshleifer/tiny-gpt2'
       SCREAMING_SNAKE_CASE_      		=     TensorFlowBenchmarkArguments(
           models=[MODEL_ID]							,							training=_lowerCAmelCase							,							inference=_lowerCAmelCase							,							sequence_lengths=[8]							,							batch_sizes=[1]							,							use_xla=_lowerCAmelCase							,							multi_process=_lowerCAmelCase							,							)
       SCREAMING_SNAKE_CASE_      		=     TensorFlowBenchmark(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     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[int]		):
       SCREAMING_SNAKE_CASE_      		=     'sshleifer/tiny-gpt2'
       with tempfile.TemporaryDirectory() as tmp_dir:
             SCREAMING_SNAKE_CASE_      		=     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							,							)
             SCREAMING_SNAKE_CASE_      		=     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  lowerCAmelCase_    (				self    :						Optional[Any]		):
       SCREAMING_SNAKE_CASE_      		=     'sshleifer/tiny-gpt2'

       def _check_summary_is_not_empty(_lowerCAmelCase    :						Dict		):
             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:
             SCREAMING_SNAKE_CASE_      		=     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							,							)
             SCREAMING_SNAKE_CASE_      		=     TensorFlowBenchmark(_lowerCAmelCase		)
             SCREAMING_SNAKE_CASE_      		=     benchmark.run()
             _check_summary_is_not_empty(result.inference_summary		)
             self.assertTrue(Path(os.path.join(_lowerCAmelCase							,							'log.txt'		)		).exists()		)





def    UpperCAmelCase_				( )   ->					list[list[int]]:
      return [list(range(10_00 - i     ,     -10_00 - i     ,     -1				)				) for i in range(10_00				)]


lowerCamelCase__					:  List[Any]      							=				generate_large_matrix()
lowerCamelCase__					:  List[Any]      							=				(
    [[4, 3, 2, -1], [3, 2, 1, -1], [1, 1, -1, -2], [-1, -1, -2, -3]],
    [[3, 2], [1, 0]],
    [[7, 7, 6]],
    [[7, 7, 6], [-1, -2, -3]],
    grid,
)

def    UpperCAmelCase_				( __UpperCAmelCase		:      list[list[int]]				)   ->					None:
      assert all(row == sorted(__UpperCAmelCase     ,     reverse=__UpperCAmelCase				) for row in grid				)
      assert all(list(__UpperCAmelCase				) == sorted(__UpperCAmelCase     ,     reverse=__UpperCAmelCase				) for col in zip(*__UpperCAmelCase				)				)

def    UpperCAmelCase_				( __UpperCAmelCase		:      list[int]				)   ->					int:
      SCREAMING_SNAKE_CASE_      		=     0
      SCREAMING_SNAKE_CASE_      		=     len(__UpperCAmelCase				) - 1

      # Edge cases such as no values or all numbers are negative.
      if not array or array[0] < 0:
            return 0

      while right + 1 > left:
            SCREAMING_SNAKE_CASE_      		=     (left + right) // 2
            SCREAMING_SNAKE_CASE_      		=     array[mid]

            # Num must be negative and the index must be greater than or equal to 0.
            if num < 0 and array[mid - 1] >= 0:
                  return mid

            if num >= 0:
                  SCREAMING_SNAKE_CASE_      		=     mid + 1
            else:
                  SCREAMING_SNAKE_CASE_      		=     mid - 1
    # No negative numbers so return the last index of the array + 1 which is the length.
      return len(__UpperCAmelCase				)

def    UpperCAmelCase_				( __UpperCAmelCase		:      list[list[int]]				)   ->					int:
      SCREAMING_SNAKE_CASE_      		=     0
      SCREAMING_SNAKE_CASE_      		=     len(grid[0]				)

      for i in range(len(__UpperCAmelCase				)				):
            SCREAMING_SNAKE_CASE_      		=     find_negative_index(grid[i][:bound]				)
            total += bound
      return (len(__UpperCAmelCase				) * len(grid[0]				)) - total

def    UpperCAmelCase_				( __UpperCAmelCase		:      list[list[int]]				)   ->					int:
      return len([number for row in grid for number in row if number < 0]				)

def    UpperCAmelCase_				( __UpperCAmelCase		:      list[list[int]]				)   ->					int:
      SCREAMING_SNAKE_CASE_      		=     0
      for row in grid:
            for i, number in enumerate(__UpperCAmelCase				):
                  if number < 0:
                        total += len(__UpperCAmelCase				) - i
                        break
      return total

def    UpperCAmelCase_				( )   ->					None:
      from timeit import timeit

      print('Running benchmarks'				)
      SCREAMING_SNAKE_CASE_      		=     (
          'from __main__ import count_negatives_binary_search, '
          'count_negatives_brute_force, count_negatives_brute_force_with_break, grid'
      )
      for func in (
          "count_negatives_binary_search",  # took 0.7727 seconds
          "count_negatives_brute_force_with_break",  # took 4.6505 seconds
          "count_negatives_brute_force",  # took 12.8160 seconds
      ):
            SCREAMING_SNAKE_CASE_      		=     timeit(f"{func}(grid=grid)"     ,     setup=__UpperCAmelCase     ,     number=5_00				)
            print(f"{func}() took {time:0.4f} seconds"				)


if __name__ == "__main__":
   import doctest

   doctest.testmod()
   benchmark()