
'''simple docstring'''







import unittest

import numpy as np

from diffusers import OnnxStableDiffusionInpaintPipelineLegacy
from diffusers.utils.testing_utils import (
    is_onnx_available,
    load_image,
    load_numpy,
    nightly,
    require_onnxruntime,
    require_torch_gpu,
)


if is_onnx_available():
							import onnxruntime as ort






@nightly
@require_onnxruntime
@require_torch_gpu
class  lowerCAmelCase__   (  unittest.TestCase     ):






					@property
					def 	_snake_case    (   self		):


											"""simple docstring"""





											return (
											    "CUDAExecutionProvider",
											    {
											        "gpu_mem_limit": "15000000000",  # 15GB
											        "arena_extend_strategy": "kSameAsRequested",
											    },
											)






					@property
					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     Optional[int]													=       ort.SessionOptions()
											lowercase_	:     Any													=       False
											return options






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     List[Any]													=       load_image(
											    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
											    '''/in_paint/overture-creations-5sI6fQgYIuo.png'''		)
											lowercase_	:     Union[str, Any]													=       load_image(
											    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
											    '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png'''		)
											lowercase_	:     Any													=       load_numpy(
											    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
											    '''/in_paint/red_cat_sitting_on_a_park_bench_onnx.npy'''		)

											# using the PNDM scheduler by default
											lowercase_	:     Any													=       OnnxStableDiffusionInpaintPipelineLegacy.from_pretrained(
											    '''CompVis/stable-diffusion-v1-4'''				,							revision='''onnx'''				,							safety_checker=__SCREAMING_SNAKE_CASE				,							feature_extractor=__SCREAMING_SNAKE_CASE				,							provider=self.gpu_provider				,							sess_options=self.gpu_options				,							)
											pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE		)

											lowercase_	:     List[Any]													=       '''A red cat sitting on a park bench'''

											lowercase_	:     Any													=       np.random.RandomState(0		)
											lowercase_	:     Dict													=       pipe(
											    prompt=__SCREAMING_SNAKE_CASE				,							image=__SCREAMING_SNAKE_CASE				,							mask_image=__SCREAMING_SNAKE_CASE				,							strength=0.75				,							guidance_scale=7.5				,							num_inference_steps=15				,							generator=__SCREAMING_SNAKE_CASE				,							output_type='''np'''				,							)

											lowercase_	:     str													=       output.images[0]

											assert image.shape == (5_12, 5_12, 3)
											assert np.abs(expected_image - image		).max() < 1E-2







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




     '''simple docstring'''


     def  _lowerCamelCase (						self ,						__lowerCamelCase)			->				Dict:
            for model_result in results.values():
                   for batch_size, sequence_length in zip(model_result["bs"] ,						model_result["ss"]):
                          _A      :       Optional[int]				    = model_result["result"][batch_size][sequence_length]
                          self.assertIsNotNone(__lowerCamelCase)


     def  _lowerCamelCase (						self)			->				int:
            _A      :       Optional[int]				    = "sshleifer/tiny-gpt2"
            _A      :       int				    = PyTorchBenchmarkArguments(
                models=[MODEL_ID] ,						training=__lowerCamelCase ,						inference=__lowerCamelCase ,						sequence_lengths=[8] ,						batch_sizes=[1] ,						multi_process=__lowerCamelCase ,						)
            _A      :       List[str]				    = PyTorchBenchmark(__lowerCamelCase)
            _A      :       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  _lowerCamelCase (						self)			->				Dict:
            _A      :       int				    = "sgugger/tiny-distilbert-classification"
            _A      :       str				    = PyTorchBenchmarkArguments(
                models=[MODEL_ID] ,						training=__lowerCamelCase ,						inference=__lowerCamelCase ,						sequence_lengths=[8] ,						batch_sizes=[1] ,						multi_process=__lowerCamelCase ,						only_pretrain_model=__lowerCamelCase ,						)
            _A      :       Dict				    = PyTorchBenchmark(__lowerCamelCase)
            _A      :       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  _lowerCamelCase (						self)			->				Optional[Any]:
            _A      :       Tuple				    = "sshleifer/tiny-gpt2"
            _A      :       Optional[Any]				    = PyTorchBenchmarkArguments(
                models=[MODEL_ID] ,						training=__lowerCamelCase ,						inference=__lowerCamelCase ,						torchscript=__lowerCamelCase ,						sequence_lengths=[8] ,						batch_sizes=[1] ,						multi_process=__lowerCamelCase ,						)
            _A      :       Union[str, Any]				    = PyTorchBenchmark(__lowerCamelCase)
            _A      :       List[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  _lowerCamelCase (						self)			->				int:
            _A      :       Any				    = "sshleifer/tiny-gpt2"
            _A      :       Tuple				    = PyTorchBenchmarkArguments(
                models=[MODEL_ID] ,						training=__lowerCamelCase ,						inference=__lowerCamelCase ,						fpaa=__lowerCamelCase ,						sequence_lengths=[8] ,						batch_sizes=[1] ,						multi_process=__lowerCamelCase ,						)
            _A      :       Any				    = PyTorchBenchmark(__lowerCamelCase)
            _A      :       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  _lowerCamelCase (						self)			->				Any:
            _A      :       Union[str, Any]				    = "sshleifer/tiny-gpt2"
            _A      :       Any				    = AutoConfig.from_pretrained(__lowerCamelCase)
            # set architectures equal to `None`
            _A      :       Dict				    = None
            _A      :       Any				    = PyTorchBenchmarkArguments(
                models=[MODEL_ID] ,						training=__lowerCamelCase ,						inference=__lowerCamelCase ,						sequence_lengths=[8] ,						batch_sizes=[1] ,						multi_process=__lowerCamelCase ,						)
            _A      :       Union[str, Any]				    = PyTorchBenchmark(__lowerCamelCase ,						configs=[config])
            _A      :       int				    = 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:
            _A      :       List[Any]				    = "sshleifer/tiny-gpt2"
            _A      :       int				    = PyTorchBenchmarkArguments(
                models=[MODEL_ID] ,						training=__lowerCamelCase ,						inference=__lowerCamelCase ,						sequence_lengths=[8] ,						batch_sizes=[1] ,						multi_process=__lowerCamelCase ,						)
            _A      :       Optional[Any]				    = PyTorchBenchmark(__lowerCamelCase)
            _A      :       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  _lowerCamelCase (						self)			->				Optional[Any]:
            _A      :       Any				    = "sshleifer/tiny-gpt2"
            _A      :       Tuple				    = PyTorchBenchmarkArguments(
                models=[MODEL_ID] ,						training=__lowerCamelCase ,						inference=__lowerCamelCase ,						sequence_lengths=[8] ,						batch_sizes=[1] ,						fpaa=__lowerCamelCase ,						multi_process=__lowerCamelCase ,						)
            _A      :       List[Any]				    = PyTorchBenchmark(__lowerCamelCase)
            _A      :       Tuple				    = 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:
            _A      :       List[str]				    = "sshleifer/tiny-gpt2"
            _A      :       Union[str, Any]				    = AutoConfig.from_pretrained(__lowerCamelCase)
            _A      :       Any				    = PyTorchBenchmarkArguments(
                models=[MODEL_ID] ,						training=__lowerCamelCase ,						inference=__lowerCamelCase ,						sequence_lengths=[8] ,						batch_sizes=[1] ,						multi_process=__lowerCamelCase ,						)
            _A      :       Optional[Any]				    = PyTorchBenchmark(__lowerCamelCase ,						configs=[config])
            _A      :       Tuple				    = 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:
            _A      :       Tuple				    = "sshleifer/tinier_bart"
            _A      :       Optional[Any]				    = AutoConfig.from_pretrained(__lowerCamelCase)
            _A      :       Optional[int]				    = PyTorchBenchmarkArguments(
                models=[MODEL_ID] ,						training=__lowerCamelCase ,						inference=__lowerCamelCase ,						sequence_lengths=[8] ,						batch_sizes=[1] ,						multi_process=__lowerCamelCase ,						)
            _A      :       Dict				    = PyTorchBenchmark(__lowerCamelCase ,						configs=[config])
            _A      :       Optional[int]				    = 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)			->				str:
            _A      :       List[Any]				    = "sshleifer/tiny-gpt2"
            _A      :       Optional[Any]				    = AutoConfig.from_pretrained(__lowerCamelCase)
            _A      :       Optional[Any]				    = PyTorchBenchmarkArguments(
                models=[MODEL_ID] ,						training=__lowerCamelCase ,						inference=__lowerCamelCase ,						sequence_lengths=[8] ,						batch_sizes=[1] ,						multi_process=__lowerCamelCase ,						)
            _A      :       List[str]				    = PyTorchBenchmark(__lowerCamelCase ,						configs=[config])
            _A      :       Dict				    = 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:
            _A      :       int				    = "sshleifer/tinier_bart"
            _A      :       str				    = AutoConfig.from_pretrained(__lowerCamelCase)
            _A      :       Tuple				    = PyTorchBenchmarkArguments(
                models=[MODEL_ID] ,						training=__lowerCamelCase ,						inference=__lowerCamelCase ,						sequence_lengths=[8] ,						batch_sizes=[1] ,						multi_process=__lowerCamelCase ,						)
            _A      :       Tuple				    = PyTorchBenchmark(__lowerCamelCase ,						configs=[config])
            _A      :       Union[str, Any]				    = 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:
            _A      :       List[str]				    = "sshleifer/tiny-gpt2"
            with tempfile.TemporaryDirectory() as tmp_dir:
                   _A      :       Optional[Any]				    = PyTorchBenchmarkArguments(
                       models=[MODEL_ID] ,						training=__lowerCamelCase ,						inference=__lowerCamelCase ,						save_to_csv=__lowerCamelCase ,						sequence_lengths=[8] ,						batch_sizes=[1] ,						inference_time_csv_file=os.path.join(__lowerCamelCase ,						"inf_time.csv") ,						train_memory_csv_file=os.path.join(__lowerCamelCase ,						"train_mem.csv") ,						inference_memory_csv_file=os.path.join(__lowerCamelCase ,						"inf_mem.csv") ,						train_time_csv_file=os.path.join(__lowerCamelCase ,						"train_time.csv") ,						env_info_csv_file=os.path.join(__lowerCamelCase ,						"env.csv") ,						multi_process=__lowerCamelCase ,						)
                   _A      :       Tuple				    = PyTorchBenchmark(__lowerCamelCase)
                   benchmark.run()
                   self.assertTrue(Path(os.path.join(__lowerCamelCase ,						"inf_time.csv")).exists())
                   self.assertTrue(Path(os.path.join(__lowerCamelCase ,						"train_time.csv")).exists())
                   self.assertTrue(Path(os.path.join(__lowerCamelCase ,						"inf_mem.csv")).exists())
                   self.assertTrue(Path(os.path.join(__lowerCamelCase ,						"train_mem.csv")).exists())
                   self.assertTrue(Path(os.path.join(__lowerCamelCase ,						"env.csv")).exists())


     def  _lowerCamelCase (						self)			->				int:
            _A      :       Dict				    = "sshleifer/tiny-gpt2"

            def _check_summary_is_not_empty(__lowerCamelCase):
                   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:
                   _A      :       Union[str, Any]				    = PyTorchBenchmarkArguments(
                       models=[MODEL_ID] ,						training=__lowerCamelCase ,						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 ,						multi_process=__lowerCamelCase ,						)
                   _A      :       Optional[int]				    = PyTorchBenchmark(__lowerCamelCase)
                   _A      :       Dict				    = benchmark.run()
                   _check_summary_is_not_empty(result.inference_summary)
                   _check_summary_is_not_empty(result.train_summary)
                   self.assertTrue(Path(os.path.join(__lowerCamelCase ,						"log.txt")).exists())
