


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




	@property
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





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




	@property
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       Dict					    =  ort.SessionOptions()
						__a			:       Dict					    =  False
						return options





	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       Tuple					    =  load_image(
						    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
						    '/in_paint/overture-creations-5sI6fQgYIuo.png'							)
						__a			:       int					    =  load_image(
						    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
						    '/in_paint/overture-creations-5sI6fQgYIuo_mask.png'							)
						__a			:       Dict					    =  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
						__a			:       str					    =  OnnxStableDiffusionInpaintPipelineLegacy.from_pretrained(
						    'CompVis/stable-diffusion-v1-4'						,				revision='onnx'						,				safety_checker=__a						,				feature_extractor=__a						,				provider=self.gpu_provider						,				sess_options=self.gpu_options						,				)
						pipe.set_progress_bar_config(disable=__a							)

						__a			:       Tuple					    =  'A red cat sitting on a park bench'

						__a			:       int					    =  np.random.RandomState(0							)
						__a			:       Tuple					    =  pipe(
						    prompt=__a						,				image=__a						,				mask_image=__a						,				strength=0.75						,				guidance_scale=7.5						,				num_inference_steps=15						,				generator=__a						,				output_type='np'						,				)

						__a			:       Tuple					    =  output.images[0]

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











from tempfile import TemporaryDirectory
from unittest import TestCase
from unittest.mock import MagicMock, patch

from transformers import AutoModel, TFAutoModel
from transformers.onnx import FeaturesManager
from transformers.testing_utils import SMALL_MODEL_IDENTIFIER, require_tf, require_torch






@require_torch
@require_tf
class 		lowercase_					(	lowercase   ):






      '''simple docstring'''




      def       __lowerCAmelCase					(      self      :						str  )    ->int:




       """simple docstring"""


       a		  =      SMALL_MODEL_IDENTIFIER
       a		  =      '''pt'''
       a		  =      '''tf'''







      def       __lowerCAmelCase					(      self      :						Optional[Any] ,					__UpperCAmelCase      :						str  )    ->Union[str, Any]:




       """simple docstring"""


       a		  =      AutoModel.from_pretrained(self.test_model  )
       model_pt.save_pretrained(__UpperCAmelCase  )







      def       __lowerCAmelCase					(      self      :						Any ,					__UpperCAmelCase      :						Union[str, Any]  )    ->List[str]:




       """simple docstring"""


       a		  =      TFAutoModel.from_pretrained(self.test_model ,					from_pt=__UpperCAmelCase  )
       model_tf.save_pretrained(__UpperCAmelCase  )







      def       __lowerCAmelCase					(      self      :						Any  )    ->int:




       """simple docstring"""


       a		  =      '''mock_framework'''

       # Framework provided - return whatever the user provides
       a		  =      FeaturesManager.determine_framework(self.test_model ,					__UpperCAmelCase  )
       self.assertEqual(__UpperCAmelCase ,					__UpperCAmelCase  )

       # Local checkpoint and framework provided - return provided framework
       # PyTorch checkpoint
       with TemporaryDirectory() as local_pt_ckpt:
        self._setup_pt_ckpt(__UpperCAmelCase  )
        a		  =      FeaturesManager.determine_framework(__UpperCAmelCase ,					__UpperCAmelCase  )
        self.assertEqual(__UpperCAmelCase ,					__UpperCAmelCase  )

       # TensorFlow checkpoint
       with TemporaryDirectory() as local_tf_ckpt:
        self._setup_tf_ckpt(__UpperCAmelCase  )
        a		  =      FeaturesManager.determine_framework(__UpperCAmelCase ,					__UpperCAmelCase  )
        self.assertEqual(__UpperCAmelCase ,					__UpperCAmelCase  )







      def       __lowerCAmelCase					(      self      :						str  )    ->int:




       """simple docstring"""


       with TemporaryDirectory() as local_pt_ckpt:
        self._setup_pt_ckpt(__UpperCAmelCase  )
        a		  =      FeaturesManager.determine_framework(__UpperCAmelCase  )
        self.assertEqual(__UpperCAmelCase ,					self.framework_pt  )

       # TensorFlow checkpoint
       with TemporaryDirectory() as local_tf_ckpt:
        self._setup_tf_ckpt(__UpperCAmelCase  )
        a		  =      FeaturesManager.determine_framework(__UpperCAmelCase  )
        self.assertEqual(__UpperCAmelCase ,					self.framework_tf  )

       # Invalid local checkpoint
       with TemporaryDirectory() as local_invalid_ckpt:
        with self.assertRaises(__UpperCAmelCase  ):
         a		  =      FeaturesManager.determine_framework(__UpperCAmelCase  )







      def       __lowerCAmelCase					(      self      :						Optional[int]  )    ->List[str]:




       """simple docstring"""


       a		  =      MagicMock(return_value=__UpperCAmelCase  )
       with patch('''transformers.onnx.features.is_tf_available''' ,					__UpperCAmelCase  ):
        a		  =      FeaturesManager.determine_framework(self.test_model  )
        self.assertEqual(__UpperCAmelCase ,					self.framework_pt  )

       # PyTorch not in environment -> use TensorFlow
       a		  =      MagicMock(return_value=__UpperCAmelCase  )
       with patch('''transformers.onnx.features.is_torch_available''' ,					__UpperCAmelCase  ):
        a		  =      FeaturesManager.determine_framework(self.test_model  )
        self.assertEqual(__UpperCAmelCase ,					self.framework_tf  )

       # Both in environment -> use PyTorch
       a		  =      MagicMock(return_value=__UpperCAmelCase  )
       a		  =      MagicMock(return_value=__UpperCAmelCase  )
       with patch('''transformers.onnx.features.is_tf_available''' ,					__UpperCAmelCase  ), patch(
           '''transformers.onnx.features.is_torch_available''' ,					__UpperCAmelCase  ):
        a		  =      FeaturesManager.determine_framework(self.test_model  )
        self.assertEqual(__UpperCAmelCase ,					self.framework_pt  )

       # Both not in environment -> raise error
       a		  =      MagicMock(return_value=__UpperCAmelCase  )
       a		  =      MagicMock(return_value=__UpperCAmelCase  )
       with patch('''transformers.onnx.features.is_tf_available''' ,					__UpperCAmelCase  ), patch(
           '''transformers.onnx.features.is_torch_available''' ,					__UpperCAmelCase  ):
        with self.assertRaises(__UpperCAmelCase  ):
         a		  =      FeaturesManager.determine_framework(self.test_model  )


