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







   def 						lowerCamelCase_	(     self						:						Dict   ,   lowerCamelCase_						:						int				):





    """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(lowerCamelCase_				)







   def 						lowerCamelCase_	(     self						:						Union[str, Any]				):





    """simple docstring"""


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





    """simple docstring"""


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





    """simple docstring"""


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





    """simple docstring"""


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





    """simple docstring"""


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





    """simple docstring"""


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





    """simple docstring"""


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





    """simple docstring"""


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





    """simple docstring"""


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





    """simple docstring"""


    UpperCamelCase			  =      """sshleifer/tiny-gpt2"""
    with tempfile.TemporaryDirectory() as tmp_dir:
     UpperCamelCase			  =      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_   ,   )
     UpperCamelCase			  =      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]				):





    """simple docstring"""


    UpperCamelCase			  =      """sshleifer/tiny-gpt2"""

    def _check_summary_is_not_empty(lowerCamelCase_						:						Union[str, Any]				):
     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:
     UpperCamelCase			  =      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_   ,   )
     UpperCamelCase			  =      TensorFlowBenchmark(lowerCamelCase_				)
     UpperCamelCase			  =      benchmark.run()
     _check_summary_is_not_empty(result.inference_summary				)
     self.assertTrue(Path(os.path.join(lowerCamelCase_   ,   """log.txt"""				)				).exists()				)

from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


_SCREAMING_SNAKE_CASE 		=      logging.get_logger(__name__)

_SCREAMING_SNAKE_CASE 		=      {
    """sail/poolformer_s12""": """https://huggingface.co/sail/poolformer_s12/resolve/main/config.json""",
    # See all PoolFormer models at https://huggingface.co/models?filter=poolformer
}
class SCREAMING_SNAKE_CASE_		(      __lowerCAmelCase		):
   __lowerCAmelCase           =				"""poolformer"""







   def __init__(     self						:						str   ,   lowerCamelCase_						:						Any=3   ,   lowerCamelCase_						:						Any=16   ,   lowerCamelCase_						:						Any=16   ,   lowerCamelCase_						:						Optional[Any]=3   ,   lowerCamelCase_						:						List[str]=4.0   ,   lowerCamelCase_						:						Tuple=[2, 2, 6, 2]   ,   lowerCamelCase_						:						Optional[Any]=[64, 128, 320, 512]   ,   lowerCamelCase_						:						Tuple=[7, 3, 3, 3]   ,   lowerCamelCase_						:						Optional[Any]=[4, 2, 2, 2]   ,   lowerCamelCase_						:						Dict=[2, 1, 1, 1]   ,   lowerCamelCase_						:						Tuple=4   ,   lowerCamelCase_						:						Optional[int]=0.0   ,   lowerCamelCase_						:						Union[str, Any]="gelu"   ,   lowerCamelCase_						:						Union[str, Any]=True   ,   lowerCamelCase_						:						Union[str, Any]=1E-5   ,   lowerCamelCase_						:						int=0.0_2   ,   **lowerCamelCase_						:						str   ,   ):





    """simple docstring"""


    UpperCamelCase			  =      num_channels
    UpperCamelCase			  =      patch_size
    UpperCamelCase			  =      stride
    UpperCamelCase			  =      padding
    UpperCamelCase			  =      pool_size
    UpperCamelCase			  =      hidden_sizes
    UpperCamelCase			  =      mlp_ratio
    UpperCamelCase			  =      depths
    UpperCamelCase			  =      patch_sizes
    UpperCamelCase			  =      strides
    UpperCamelCase			  =      num_encoder_blocks
    UpperCamelCase			  =      drop_path_rate
    UpperCamelCase			  =      hidden_act
    UpperCamelCase			  =      use_layer_scale
    UpperCamelCase			  =      layer_scale_init_value
    UpperCamelCase			  =      initializer_range
    super().__init__(**lowerCamelCase_				)





class SCREAMING_SNAKE_CASE_		(      __lowerCAmelCase		):
   __lowerCAmelCase           =				version.parse("""1.11"""		)







   @property
   def 						lowerCamelCase_	(     self						:						Any				):





    """simple docstring"""


    return OrderedDict(
        [
            ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}),
        ]				)







   @property
   def 						lowerCamelCase_	(     self						:						Any				):





    """simple docstring"""


    return 2E-3
