





'''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    ,   A__    ) ->	Optional[Any]:
      for model_result in results.values():
       for batch_size, sequence_length in zip(model_result['''bs''']    ,   model_result['''ss''']    ):
        snake_case				=			model_result['''result'''][batch_size][sequence_length]
        self.assertIsNotNone(A__    )







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








'''simple docstring'''







import random
import unittest

import torch

from diffusers import IFInpaintingSuperResolutionPipeline
from diffusers.utils import floats_tensor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import skip_mps, torch_device

from ..pipeline_params import (
    TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_INPAINTING_PARAMS,
)
from ..test_pipelines_common import PipelineTesterMixin
from . import IFPipelineTesterMixin
@skip_mps
class 			_lowercase						(       __a				,		__a				,		unittest.TestCase					):
     _UpperCAmelCase										=					IFInpaintingSuperResolutionPipeline
     _UpperCAmelCase										=					TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {'''width''', '''height'''}
     _UpperCAmelCase										=					TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({'''original_image'''}					)
     _UpperCAmelCase										=					PipelineTesterMixin.required_optional_params - {'''latents'''}







     def     UpperCamelCase			( self    ) ->	int:
      return self._get_superresolution_dummy_components()







     def     UpperCamelCase			( self    ,   A__    ,   A__=0    ) ->	Union[str, Any]:
      if str(A__    ).startswith('''mps'''    ):
       snake_case				=			torch.manual_seed(A__    )
      else:
       snake_case				=			torch.Generator(device=A__    ).manual_seed(A__    )

      snake_case				=			floats_tensor((1, 3, 16, 16)    ,   rng=random.Random(A__    )    ).to(A__    )
      snake_case				=			floats_tensor((1, 3, 32, 32)    ,   rng=random.Random(A__    )    ).to(A__    )
      snake_case				=			floats_tensor((1, 3, 32, 32)    ,   rng=random.Random(A__    )    ).to(A__    )

      snake_case				=			{
          '''prompt''': '''A painting of a squirrel eating a burger''',
          '''image''': image,
          '''original_image''': original_image,
          '''mask_image''': mask_image,
          '''generator''': generator,
          '''num_inference_steps''': 2,
          '''output_type''': '''numpy''',
      }

      return inputs







     @unittest.skipIf(
         torch_device != '''cuda''' or not is_xformers_available()    ,   reason='''XFormers attention is only available with CUDA and `xformers` installed'''    ,   )
     def     UpperCamelCase			( self    ) ->	List[Any]:
      self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3    )







     def     UpperCamelCase			( self    ) ->	Optional[Any]:
      self._test_save_load_optional_components()







     @unittest.skipIf(torch_device != '''cuda'''    ,   reason='''float16 requires CUDA'''    )
     def     UpperCamelCase			( self    ) ->	List[str]:
      # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder
      super().test_save_load_floataa(expected_max_diff=1e-1    )







     def     UpperCamelCase			( self    ) ->	int:
      self._test_attention_slicing_forward_pass(expected_max_diff=1e-2    )







     def     UpperCamelCase			( self    ) ->	Optional[Any]:
      self._test_save_load_local()







     def     UpperCamelCase			( self    ) ->	Dict:
      self._test_inference_batch_single_identical(
          expected_max_diff=1e-2    ,   )

