



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






    """simple docstring"""





    def 					A			(    self :   int    ,		UpperCamelCase__ :   List[Any]					):


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






    def 					A			(    self :   List[str]					):


       """simple docstring"""







       UpperCamelCase      							=    'sshleifer/tiny-gpt2'
       UpperCamelCase      							=    TensorFlowBenchmarkArguments(
           models=[MODEL_ID]    ,		training=UpperCamelCase__    ,		inference=UpperCamelCase__    ,		sequence_lengths=[8]    ,		batch_sizes=[1]    ,		eager_mode=UpperCamelCase__    ,		multi_process=UpperCamelCase__    ,		)
       UpperCamelCase      							=    TensorFlowBenchmark(UpperCamelCase__					)
       UpperCamelCase      							=    benchmark.run()
       self.check_results_dict_not_empty(results.time_inference_result					)
       self.check_results_dict_not_empty(results.memory_inference_result					)






    def 					A			(    self :   Any					):


       """simple docstring"""







       UpperCamelCase      							=    'sgugger/tiny-distilbert-classification'
       UpperCamelCase      							=    TensorFlowBenchmarkArguments(
           models=[MODEL_ID]    ,		training=UpperCamelCase__    ,		inference=UpperCamelCase__    ,		sequence_lengths=[8]    ,		batch_sizes=[1]    ,		multi_process=UpperCamelCase__    ,		only_pretrain_model=UpperCamelCase__    ,		)
       UpperCamelCase      							=    TensorFlowBenchmark(UpperCamelCase__					)
       UpperCamelCase      							=    benchmark.run()
       self.check_results_dict_not_empty(results.time_inference_result					)
       self.check_results_dict_not_empty(results.memory_inference_result					)






    def 					A			(    self :   Optional[int]					):


       """simple docstring"""







       UpperCamelCase      							=    'sshleifer/tiny-gpt2'
       UpperCamelCase      							=    TensorFlowBenchmarkArguments(
           models=[MODEL_ID]    ,		training=UpperCamelCase__    ,		inference=UpperCamelCase__    ,		sequence_lengths=[8]    ,		batch_sizes=[1]    ,		multi_process=UpperCamelCase__    ,		)
       UpperCamelCase      							=    TensorFlowBenchmark(UpperCamelCase__					)
       UpperCamelCase      							=    benchmark.run()
       self.check_results_dict_not_empty(results.time_inference_result					)
       self.check_results_dict_not_empty(results.memory_inference_result					)






    def 					A			(    self :   Any					):


       """simple docstring"""







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


       """simple docstring"""







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


       """simple docstring"""







       UpperCamelCase      							=    'sshleifer/tiny-gpt2'
       UpperCamelCase      							=    TensorFlowBenchmarkArguments(
           models=[MODEL_ID]    ,		training=UpperCamelCase__    ,		inference=UpperCamelCase__    ,		sequence_lengths=[8]    ,		batch_sizes=[1]    ,		multi_process=UpperCamelCase__    ,		)
       UpperCamelCase      							=    TensorFlowBenchmark(UpperCamelCase__					)
       UpperCamelCase      							=    benchmark.run()
       self.check_results_dict_not_empty(results.time_train_result					)
       self.check_results_dict_not_empty(results.memory_train_result					)






    def 					A			(    self :   str					):


       """simple docstring"""







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


       """simple docstring"""







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


       """simple docstring"""







       UpperCamelCase      							=    'sshleifer/tiny-gpt2'
       UpperCamelCase      							=    TensorFlowBenchmarkArguments(
           models=[MODEL_ID]    ,		training=UpperCamelCase__    ,		inference=UpperCamelCase__    ,		sequence_lengths=[8]    ,		batch_sizes=[1]    ,		use_xla=UpperCamelCase__    ,		multi_process=UpperCamelCase__    ,		)
       UpperCamelCase      							=    TensorFlowBenchmark(UpperCamelCase__					)
       UpperCamelCase      							=    benchmark.run()
       self.check_results_dict_not_empty(results.time_inference_result					)
       self.check_results_dict_not_empty(results.memory_inference_result					)






    def 					A			(    self :   Tuple					):


       """simple docstring"""







       UpperCamelCase      							=    'sshleifer/tiny-gpt2'
       with tempfile.TemporaryDirectory() as tmp_dir:
          UpperCamelCase      							=    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__    ,		)
          UpperCamelCase      							=    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 					A			(    self :   Optional[Any]					):


       """simple docstring"""







       UpperCamelCase      							=    'sshleifer/tiny-gpt2'

       def _check_summary_is_not_empty(UpperCamelCase__ :   Optional[int]					):
          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:
          UpperCamelCase      							=    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__    ,		)
          UpperCamelCase      							=    TensorFlowBenchmark(UpperCamelCase__					)
          UpperCamelCase      							=    benchmark.run()
          _check_summary_is_not_empty(result.inference_summary					)
          self.assertTrue(Path(os.path.join(UpperCamelCase__    ,		'log.txt'					)					).exists()					)



import unittest

import numpy as np
import torch

from diffusers import PNDMPipeline, PNDMScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()







class 							lowerCAmelCase__   ( unittest.TestCase						):

						@property
						def 			A_					(      self					)       ->   int:


								'''simple docstring'''

								torch.manual_seed(0					)
								_UpperCamelCase         =						UNetaDModel(
								    block_out_channels=(32, 64)							,				layers_per_block=2							,				sample_size=32							,				in_channels=3							,				out_channels=3							,				down_block_types=("""DownBlock2D""", """AttnDownBlock2D""")							,				up_block_types=("""AttnUpBlock2D""", """UpBlock2D""")							,				)
								return model







						def 			A_					(      self					)       ->   Any:


								'''simple docstring'''

								_UpperCamelCase         =						self.dummy_uncond_unet
								_UpperCamelCase         =						PNDMScheduler()

								_UpperCamelCase         =						PNDMPipeline(unet=a							,				scheduler=a					)
								pndm.to(a					)
								pndm.set_progress_bar_config(disable=a					)

								_UpperCamelCase         =						torch.manual_seed(0					)
								_UpperCamelCase         =						pndm(generator=a							,				num_inference_steps=20							,				output_type="""numpy"""					).images

								_UpperCamelCase         =						torch.manual_seed(0					)
								_UpperCamelCase         =						pndm(generator=a							,				num_inference_steps=20							,				output_type="""numpy"""							,				return_dict=a					)[0]

								_UpperCamelCase         =						image[0, -3:, -3:, -1]
								_UpperCamelCase         =						image_from_tuple[0, -3:, -3:, -1]

								assert image.shape == (1, 32, 32, 3)
								_UpperCamelCase         =						np.array([1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0]					)

								assert np.abs(image_slice.flatten() - expected_slice					).max() < 1e-2
								assert np.abs(image_from_tuple_slice.flatten() - expected_slice					).max() < 1e-2







@slow
@require_torch
class 							lowerCAmelCase__   ( unittest.TestCase						):

						def 			A_					(      self					)       ->   Optional[Any]:


								'''simple docstring'''

								_UpperCamelCase         =						"""google/ddpm-cifar10-32"""

								_UpperCamelCase         =						UNetaDModel.from_pretrained(a					)
								_UpperCamelCase         =						PNDMScheduler()

								_UpperCamelCase         =						PNDMPipeline(unet=a							,				scheduler=a					)
								pndm.to(a					)
								pndm.set_progress_bar_config(disable=a					)
								_UpperCamelCase         =						torch.manual_seed(0					)
								_UpperCamelCase         =						pndm(generator=a							,				output_type="""numpy"""					).images

								_UpperCamelCase         =						image[0, -3:, -3:, -1]

								assert image.shape == (1, 32, 32, 3)
								_UpperCamelCase         =						np.array([0.1564, 0.1_4645, 0.1406, 0.1_4715, 0.1_2425, 0.1_4045, 0.1_3115, 0.1_2175, 0.125]					)

								assert np.abs(image_slice.flatten() - expected_slice					).max() < 1e-2



