






'''simple docstring'''
import os
import tempfile
import unittest
from pathlib import Path

from transformers import AutoConfig, is_torch_available
from transformers.testing_utils import require_torch, torch_device


if is_torch_available():
  from transformers import PyTorchBenchmark, PyTorchBenchmarkArguments






@require_torch
class        __SCREAMING_SNAKE_CASE (     unittest.TestCase			):




 def        __magic_name__		( self		:							Dict   , __lowercase		:							List[Any] )		->  int:
       for model_result in results.values():
             for batch_size, sequence_length in zip(model_result['''bs''']   , model_result['''ss'''] ):
                   SCREAMING_SNAKE_CASE__       :			List[str]													=model_result['''result'''][batch_size][sequence_length]
                   self.assertIsNotNone(__lowercase )




 def        __magic_name__		( self		:							int )		->  Optional[Any]:
       SCREAMING_SNAKE_CASE__       :			Optional[Any]													='''sshleifer/tiny-gpt2'''
       SCREAMING_SNAKE_CASE__       :			int													=PyTorchBenchmarkArguments(
           models=[MODEL_ID]   , training=__lowercase   , inference=__lowercase   , sequence_lengths=[8]   , batch_sizes=[1]   , multi_process=__lowercase   , )
       SCREAMING_SNAKE_CASE__       :			Any													=PyTorchBenchmark(__lowercase )
       SCREAMING_SNAKE_CASE__       :			List[Any]													=benchmark.run()
       self.check_results_dict_not_empty(results.time_inference_result )
       self.check_results_dict_not_empty(results.memory_inference_result )




 def        __magic_name__		( self		:							List[Any] )		->  Dict:
       SCREAMING_SNAKE_CASE__       :			Dict													='''sgugger/tiny-distilbert-classification'''
       SCREAMING_SNAKE_CASE__       :			Tuple													=PyTorchBenchmarkArguments(
           models=[MODEL_ID]   , training=__lowercase   , inference=__lowercase   , sequence_lengths=[8]   , batch_sizes=[1]   , multi_process=__lowercase   , only_pretrain_model=__lowercase   , )
       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=PyTorchBenchmark(__lowercase )
       SCREAMING_SNAKE_CASE__       :			Any													=benchmark.run()
       self.check_results_dict_not_empty(results.time_inference_result )
       self.check_results_dict_not_empty(results.memory_inference_result )




 def        __magic_name__		( self		:							Optional[Any] )		->  str:
       SCREAMING_SNAKE_CASE__       :			List[Any]													='''sshleifer/tiny-gpt2'''
       SCREAMING_SNAKE_CASE__       :			Optional[int]													=PyTorchBenchmarkArguments(
           models=[MODEL_ID]   , training=__lowercase   , inference=__lowercase   , torchscript=__lowercase   , sequence_lengths=[8]   , batch_sizes=[1]   , multi_process=__lowercase   , )
       SCREAMING_SNAKE_CASE__       :			Union[str, Any]													=PyTorchBenchmark(__lowercase )
       SCREAMING_SNAKE_CASE__       :			str													=benchmark.run()
       self.check_results_dict_not_empty(results.time_inference_result )
       self.check_results_dict_not_empty(results.memory_inference_result )




 @unittest.skipIf(torch_device == '''cpu'''   , '''Cant do half precision''' )
 def        __magic_name__		( self		:							str )		->  Union[str, Any]:
       SCREAMING_SNAKE_CASE__       :			Optional[int]													='''sshleifer/tiny-gpt2'''
       SCREAMING_SNAKE_CASE__       :			Dict													=PyTorchBenchmarkArguments(
           models=[MODEL_ID]   , training=__lowercase   , inference=__lowercase   , fpaa=__lowercase   , sequence_lengths=[8]   , batch_sizes=[1]   , multi_process=__lowercase   , )
       SCREAMING_SNAKE_CASE__       :			List[Any]													=PyTorchBenchmark(__lowercase )
       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=benchmark.run()
       self.check_results_dict_not_empty(results.time_inference_result )
       self.check_results_dict_not_empty(results.memory_inference_result )




 def        __magic_name__		( self		:							List[str] )		->  int:
       SCREAMING_SNAKE_CASE__       :			Union[str, Any]													='''sshleifer/tiny-gpt2'''
       SCREAMING_SNAKE_CASE__       :			Optional[int]													=AutoConfig.from_pretrained(__lowercase )
       # set architectures equal to `None`
       SCREAMING_SNAKE_CASE__       :			List[str]													=None
       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=PyTorchBenchmarkArguments(
           models=[MODEL_ID]   , training=__lowercase   , inference=__lowercase   , sequence_lengths=[8]   , batch_sizes=[1]   , multi_process=__lowercase   , )
       SCREAMING_SNAKE_CASE__       :			Dict													=PyTorchBenchmark(__lowercase   , configs=[config] )
       SCREAMING_SNAKE_CASE__       :			Union[str, Any]													=benchmark.run()
       self.check_results_dict_not_empty(results.time_inference_result )
       self.check_results_dict_not_empty(results.memory_inference_result )




 def        __magic_name__		( self		:							Optional[Any] )		->  int:
       SCREAMING_SNAKE_CASE__       :			List[str]													='''sshleifer/tiny-gpt2'''
       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=PyTorchBenchmarkArguments(
           models=[MODEL_ID]   , training=__lowercase   , inference=__lowercase   , sequence_lengths=[8]   , batch_sizes=[1]   , multi_process=__lowercase   , )
       SCREAMING_SNAKE_CASE__       :			Tuple													=PyTorchBenchmark(__lowercase )
       SCREAMING_SNAKE_CASE__       :			Optional[int]													=benchmark.run()
       self.check_results_dict_not_empty(results.time_train_result )
       self.check_results_dict_not_empty(results.memory_train_result )




 @unittest.skipIf(torch_device == '''cpu'''   , '''Can\'t do half precision''' )
 def        __magic_name__		( self		:							Dict )		->  Any:
       SCREAMING_SNAKE_CASE__       :			str													='''sshleifer/tiny-gpt2'''
       SCREAMING_SNAKE_CASE__       :			Any													=PyTorchBenchmarkArguments(
           models=[MODEL_ID]   , training=__lowercase   , inference=__lowercase   , sequence_lengths=[8]   , batch_sizes=[1]   , fpaa=__lowercase   , multi_process=__lowercase   , )
       SCREAMING_SNAKE_CASE__       :			str													=PyTorchBenchmark(__lowercase )
       SCREAMING_SNAKE_CASE__       :			List[Any]													=benchmark.run()
       self.check_results_dict_not_empty(results.time_train_result )
       self.check_results_dict_not_empty(results.memory_train_result )




 def        __magic_name__		( self		:							int )		->  Dict:
       SCREAMING_SNAKE_CASE__       :			Any													='''sshleifer/tiny-gpt2'''
       SCREAMING_SNAKE_CASE__       :			Union[str, Any]													=AutoConfig.from_pretrained(__lowercase )
       SCREAMING_SNAKE_CASE__       :			Any													=PyTorchBenchmarkArguments(
           models=[MODEL_ID]   , training=__lowercase   , inference=__lowercase   , sequence_lengths=[8]   , batch_sizes=[1]   , multi_process=__lowercase   , )
       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=PyTorchBenchmark(__lowercase   , configs=[config] )
       SCREAMING_SNAKE_CASE__       :			Any													=benchmark.run()
       self.check_results_dict_not_empty(results.time_inference_result )
       self.check_results_dict_not_empty(results.memory_inference_result )




 def        __magic_name__		( self		:							Dict )		->  Optional[Any]:
       SCREAMING_SNAKE_CASE__       :			List[str]													='''sshleifer/tinier_bart'''
       SCREAMING_SNAKE_CASE__       :			int													=AutoConfig.from_pretrained(__lowercase )
       SCREAMING_SNAKE_CASE__       :			int													=PyTorchBenchmarkArguments(
           models=[MODEL_ID]   , training=__lowercase   , inference=__lowercase   , sequence_lengths=[8]   , batch_sizes=[1]   , multi_process=__lowercase   , )
       SCREAMING_SNAKE_CASE__       :			str													=PyTorchBenchmark(__lowercase   , configs=[config] )
       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=benchmark.run()
       self.check_results_dict_not_empty(results.time_inference_result )
       self.check_results_dict_not_empty(results.memory_inference_result )




 def        __magic_name__		( self		:							str )		->  str:
       SCREAMING_SNAKE_CASE__       :			Union[str, Any]													='''sshleifer/tiny-gpt2'''
       SCREAMING_SNAKE_CASE__       :			Any													=AutoConfig.from_pretrained(__lowercase )
       SCREAMING_SNAKE_CASE__       :			str													=PyTorchBenchmarkArguments(
           models=[MODEL_ID]   , training=__lowercase   , inference=__lowercase   , sequence_lengths=[8]   , batch_sizes=[1]   , multi_process=__lowercase   , )
       SCREAMING_SNAKE_CASE__       :			Optional[int]													=PyTorchBenchmark(__lowercase   , configs=[config] )
       SCREAMING_SNAKE_CASE__       :			str													=benchmark.run()
       self.check_results_dict_not_empty(results.time_train_result )
       self.check_results_dict_not_empty(results.memory_train_result )




 def        __magic_name__		( self		:							Dict )		->  Union[str, Any]:
       SCREAMING_SNAKE_CASE__       :			Dict													='''sshleifer/tinier_bart'''
       SCREAMING_SNAKE_CASE__       :			Any													=AutoConfig.from_pretrained(__lowercase )
       SCREAMING_SNAKE_CASE__       :			Tuple													=PyTorchBenchmarkArguments(
           models=[MODEL_ID]   , training=__lowercase   , inference=__lowercase   , sequence_lengths=[8]   , batch_sizes=[1]   , multi_process=__lowercase   , )
       SCREAMING_SNAKE_CASE__       :			Optional[int]													=PyTorchBenchmark(__lowercase   , configs=[config] )
       SCREAMING_SNAKE_CASE__       :			str													=benchmark.run()
       self.check_results_dict_not_empty(results.time_train_result )
       self.check_results_dict_not_empty(results.memory_train_result )




 def        __magic_name__		( self		:							Any )		->  List[str]:
       SCREAMING_SNAKE_CASE__       :			int													='''sshleifer/tiny-gpt2'''
       with tempfile.TemporaryDirectory() as tmp_dir:
             SCREAMING_SNAKE_CASE__       :			Optional[int]													=PyTorchBenchmarkArguments(
                 models=[MODEL_ID]   , training=__lowercase   , inference=__lowercase   , save_to_csv=__lowercase   , sequence_lengths=[8]   , batch_sizes=[1]   , inference_time_csv_file=os.path.join(__lowercase   , '''inf_time.csv''' )   , train_memory_csv_file=os.path.join(__lowercase   , '''train_mem.csv''' )   , inference_memory_csv_file=os.path.join(__lowercase   , '''inf_mem.csv''' )   , train_time_csv_file=os.path.join(__lowercase   , '''train_time.csv''' )   , env_info_csv_file=os.path.join(__lowercase   , '''env.csv''' )   , multi_process=__lowercase   , )
             SCREAMING_SNAKE_CASE__       :			List[Any]													=PyTorchBenchmark(__lowercase )
             benchmark.run()
             self.assertTrue(Path(os.path.join(__lowercase   , '''inf_time.csv''' ) ).exists() )
             self.assertTrue(Path(os.path.join(__lowercase   , '''train_time.csv''' ) ).exists() )
             self.assertTrue(Path(os.path.join(__lowercase   , '''inf_mem.csv''' ) ).exists() )
             self.assertTrue(Path(os.path.join(__lowercase   , '''train_mem.csv''' ) ).exists() )
             self.assertTrue(Path(os.path.join(__lowercase   , '''env.csv''' ) ).exists() )




 def        __magic_name__		( self		:							int )		->  List[Any]:
       SCREAMING_SNAKE_CASE__       :			Optional[int]													='''sshleifer/tiny-gpt2'''

       def _check_summary_is_not_empty(__lowercase		:							Tuple ):
             self.assertTrue(hasattr(__lowercase   , '''sequential''' ) )
             self.assertTrue(hasattr(__lowercase   , '''cumulative''' ) )
             self.assertTrue(hasattr(__lowercase   , '''current''' ) )
             self.assertTrue(hasattr(__lowercase   , '''total''' ) )

       with tempfile.TemporaryDirectory() as tmp_dir:
             SCREAMING_SNAKE_CASE__       :			List[Any]													=PyTorchBenchmarkArguments(
                 models=[MODEL_ID]   , training=__lowercase   , inference=__lowercase   , sequence_lengths=[8]   , batch_sizes=[1]   , log_filename=os.path.join(__lowercase   , '''log.txt''' )   , log_print=__lowercase   , trace_memory_line_by_line=__lowercase   , multi_process=__lowercase   , )
             SCREAMING_SNAKE_CASE__       :			Union[str, Any]													=PyTorchBenchmark(__lowercase )
             SCREAMING_SNAKE_CASE__       :			Tuple													=benchmark.run()
             _check_summary_is_not_empty(result.inference_summary )
             _check_summary_is_not_empty(result.train_summary )
             self.assertTrue(Path(os.path.join(__lowercase   , '''log.txt''' ) ).exists() )







'''simple docstring'''
import gc
import unittest

import numpy as np
import torch

from diffusers import StableDiffusionKDiffusionPipeline
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()






@slow
@require_torch_gpu
class        __SCREAMING_SNAKE_CASE (     unittest.TestCase			):




 def        __magic_name__		( self		:							List[str] )		->  Optional[Any]:
       # clean up the VRAM after each test
       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()




 def        __magic_name__		( self		:							Union[str, Any] )		->  Dict:
       SCREAMING_SNAKE_CASE__       :			Optional[int]													=StableDiffusionKDiffusionPipeline.from_pretrained('''CompVis/stable-diffusion-v1-4''' )
       SCREAMING_SNAKE_CASE__       :			str													=sd_pipe.to(__lowercase )
       sd_pipe.set_progress_bar_config(disable=__lowercase )

       sd_pipe.set_scheduler('''sample_euler''' )

       SCREAMING_SNAKE_CASE__       :			List[Any]													='''A painting of a squirrel eating a burger'''
       SCREAMING_SNAKE_CASE__       :			List[str]													=torch.manual_seed(0 )
       SCREAMING_SNAKE_CASE__       :			Union[str, Any]													=sd_pipe([prompt]   , generator=__lowercase   , guidance_scale=9.0   , num_inference_steps=20   , output_type='''np''' )

       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=output.images

       SCREAMING_SNAKE_CASE__       :			int													=image[0, -3:, -3:, -1]

       assert image.shape == (1, 5_12, 5_12, 3)
       SCREAMING_SNAKE_CASE__       :			Any													=np.array([0.0447, 0.0492, 0.0468, 0.0408, 0.0383, 0.0408, 0.0354, 0.0380, 0.0339] )

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




 def        __magic_name__		( self		:							List[str] )		->  List[Any]:
       SCREAMING_SNAKE_CASE__       :			List[str]													=StableDiffusionKDiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base''' )
       SCREAMING_SNAKE_CASE__       :			int													=sd_pipe.to(__lowercase )
       sd_pipe.set_progress_bar_config(disable=__lowercase )

       sd_pipe.set_scheduler('''sample_euler''' )

       SCREAMING_SNAKE_CASE__       :			Any													='''A painting of a squirrel eating a burger'''
       SCREAMING_SNAKE_CASE__       :			Dict													=torch.manual_seed(0 )
       SCREAMING_SNAKE_CASE__       :			Tuple													=sd_pipe([prompt]   , generator=__lowercase   , guidance_scale=9.0   , num_inference_steps=20   , output_type='''np''' )

       SCREAMING_SNAKE_CASE__       :			Dict													=output.images

       SCREAMING_SNAKE_CASE__       :			Dict													=image[0, -3:, -3:, -1]

       assert image.shape == (1, 5_12, 5_12, 3)
       SCREAMING_SNAKE_CASE__       :			List[str]													=np.array([0.1237, 0.1320, 0.1438, 0.1359, 0.1390, 0.1132, 0.1277, 0.1175, 0.1112] )

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




 def        __magic_name__		( self		:							List[str] )		->  List[Any]:
       SCREAMING_SNAKE_CASE__       :			Any													=StableDiffusionKDiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base''' )
       SCREAMING_SNAKE_CASE__       :			Tuple													=sd_pipe.to(__lowercase )
       sd_pipe.set_progress_bar_config(disable=__lowercase )

       sd_pipe.set_scheduler('''sample_dpmpp_2m''' )

       SCREAMING_SNAKE_CASE__       :			Tuple													='''A painting of a squirrel eating a burger'''
       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=torch.manual_seed(0 )
       SCREAMING_SNAKE_CASE__       :			List[Any]													=sd_pipe(
           [prompt]   , generator=__lowercase   , guidance_scale=7.5   , num_inference_steps=15   , output_type='''np'''   , use_karras_sigmas=__lowercase   , )

       SCREAMING_SNAKE_CASE__       :			Optional[int]													=output.images

       SCREAMING_SNAKE_CASE__       :			Dict													=image[0, -3:, -3:, -1]

       assert image.shape == (1, 5_12, 5_12, 3)
       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=np.array(
           [0.11381689, 0.12112921, 0.1389457, 0.12549606, 0.1244964, 0.10831517, 0.11562866, 0.10867816, 0.10499048] )

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