


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 			_a				(     UpperCamelCase__     ):

							def  lowerCamelCase_					(				self:      Dict				)     ->  List[str]:





									"""simple docstring"""







									lowercase__				= SMALL_MODEL_IDENTIFIER
									lowercase__				= '''pt'''
									lowercase__				= '''tf'''

							def  lowerCamelCase_					(				self:      List[str]       ,		UpperCamelCase_:      Any				)     ->  int:





									"""simple docstring"""







									lowercase__				= AutoModel.from_pretrained(self.test_model				)
									model_pt.save_pretrained(UpperCamelCase_				)

							def  lowerCamelCase_					(				self:      Optional[Any]       ,		UpperCamelCase_:      Tuple				)     ->  List[Any]:





									"""simple docstring"""







									lowercase__				= TFAutoModel.from_pretrained(self.test_model       ,		from_pt=UpperCamelCase_				)
									model_tf.save_pretrained(UpperCamelCase_				)

							def  lowerCamelCase_					(				self:      str				)     ->  Any:





									"""simple docstring"""







									lowercase__				= '''mock_framework'''

									# Framework provided - return whatever the user provides
									lowercase__				= 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_				)
											lowercase__				= FeaturesManager.determine_framework(UpperCamelCase_       ,		UpperCamelCase_				)
											self.assertEqual(UpperCamelCase_       ,		UpperCamelCase_				)

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

							def  lowerCamelCase_					(				self:      Any				)     ->  Any:





									"""simple docstring"""







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

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

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


							def  lowerCamelCase_					(				self:      int				)     ->  Any:





									"""simple docstring"""







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

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

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

									# Both not in environment -> raise error
									lowercase__				= MagicMock(return_value=UpperCamelCase_				)
									lowercase__				= 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_				):
													lowercase__				= FeaturesManager.determine_framework(self.test_model				)









import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionImageVariationPipeline
from diffusers.utils.testing_utils import load_image, require_torch_gpu, slow, torch_device


a__      :     Optional[int]         =		False





class UpperCAmelCase_				( unittest.TestCase ):
  pass





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


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       VersatileDiffusionImageVariationPipeline.from_pretrained('''shi-labs/versatile-diffusion''' )
       pipe.to(__snake_case )
       pipe.set_progress_bar_config(disable=__snake_case )

       A_          =       load_image(
           '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg''' )
       A_          =       torch.manual_seed(0 )
       A_          =       pipe(
           image=__snake_case ,generator=__snake_case ,guidance_scale=7.5 ,num_inference_steps=5_0 ,output_type='''numpy''' ,).images

       A_          =       image[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

       assert image.shape == (1, 5_1_2, 5_1_2, 3)
       A_          =       np.array([0.0441, 0.0469, 0.0507, 0.0575, 0.0632, 0.0650, 0.0865, 0.0909, 0.0945] )

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



