





import random
import unittest

import numpy as np
import torch

from diffusers import (
    DPMSolverMultistepScheduler,
    EulerAncestralDiscreteScheduler,
    EulerDiscreteScheduler,
    LMSDiscreteScheduler,
    OnnxStableDiffusionUpscalePipeline,
    PNDMScheduler,
)
from diffusers.utils import floats_tensor
from diffusers.utils.testing_utils import (
    is_onnx_available,
    load_image,
    nightly,
    require_onnxruntime,
    require_torch_gpu,
)

from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin


if is_onnx_available():
    import onnxruntime as ort





class  A__    (							__UpperCAmelCase ,  unittest.TestCase	):




   """simple docstring"""



   __A   :				Tuple         =  '''ssube/stable-diffusion-x4-upscaler-onnx'''

   def 							__lowercase		(					self					,  lowercase=0)			->						str:







          '''simple docstring'''

          a__						:	str =				floats_tensor((1, 3, 128, 128)					,  rng=random.Random(lowercase))
          a__						:	Optional[int] =				torch.manual_seed(lowercase)
          a__						:	List[str] =				{
              'prompt': 'A painting of a squirrel eating a burger',
              'image': image,
              'generator': generator,
              'num_inference_steps': 3,
              'guidance_scale': 7.5,
              'output_type': 'numpy',
          }
          return inputs

   def 							__lowercase		(					self)			->						Dict:







          '''simple docstring'''

          a__						:	Union[str, Any] =				OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint					,  provider='CPUExecutionProvider')
          pipe.set_progress_bar_config(disable=lowercase)

          a__						:	Any =				self.get_dummy_inputs()
          a__						:	Any =				pipe(**lowercase).images
          a__						:	Any =				image[0, -3:, -3:, -1].flatten()

          # started as 128, should now be 512
          assert image.shape == (1, 512, 512, 3)
          a__						:	Optional[int] =				np.array(
              [0.6_97_47_82, 0.68_90_20_93, 0.70_13_58_85, 0.7_58_36_18, 0.7_80_45_45, 0.7_85_49_12, 0.78_66_74_26, 0.78_74_38_63, 0.78_07_02_23])
          assert np.abs(image_slice - expected_slice).max() < 1e-1

   def 							__lowercase		(					self)			->						Tuple:







          '''simple docstring'''

          a__						:	Optional[int] =				OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint					,  provider='CPUExecutionProvider')
          a__						:	List[Any] =				PNDMScheduler.from_config(pipe.scheduler.config					,  skip_prk_steps=lowercase)
          pipe.set_progress_bar_config(disable=lowercase)

          a__						:	Union[str, Any] =				self.get_dummy_inputs()
          a__						:	Any =				pipe(**lowercase).images
          a__						:	Dict =				image[0, -3:, -3:, -1]

          assert image.shape == (1, 512, 512, 3)
          a__						:	Optional[Any] =				np.array(
              [0.6_89_88_92, 0.59_24_05_56, 0.52_49_95_27, 0.58_86_62_15, 0.52_25_82_35, 0.52_57_27_15, 0.62_41_44_73, 0.6_17_43_87, 0.6_21_49_64])
          assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-1

   def 							__lowercase		(					self)			->						Any:







          '''simple docstring'''

          a__						:	Any =				OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint					,  provider='CPUExecutionProvider')
          a__						:	Any =				DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
          pipe.set_progress_bar_config(disable=lowercase)

          a__						:	Union[str, Any] =				self.get_dummy_inputs()
          a__						:	List[str] =				pipe(**lowercase).images
          a__						:	int =				image[0, -3:, -3:, -1]

          assert image.shape == (1, 512, 512, 3)
          a__						:	Tuple =				np.array(
              [0.7_65_92_78, 0.76_43_76_64, 0.75_57_91_07, 0.7_69_11_16, 0.77_66_69_86, 0.7_72_76_72, 0.7_75_86_64, 0.7_81_22_26, 0.76_94_25_15])

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

   def 							__lowercase		(					self)			->						Union[str, Any]:







          '''simple docstring'''

          a__						:	List[str] =				OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint					,  provider='CPUExecutionProvider')
          a__						:	Optional[int] =				EulerDiscreteScheduler.from_config(pipe.scheduler.config)
          pipe.set_progress_bar_config(disable=lowercase)

          a__						:	Tuple =				self.get_dummy_inputs()
          a__						:	Any =				pipe(**lowercase).images
          a__						:	Dict =				image[0, -3:, -3:, -1]

          assert image.shape == (1, 512, 512, 3)
          a__						:	List[str] =				np.array(
              [0.6_97_47_82, 0.68_90_20_93, 0.70_13_58_85, 0.7_58_36_18, 0.7_80_45_45, 0.7_85_49_12, 0.78_66_74_26, 0.78_74_38_63, 0.78_07_02_23])
          assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-1

   def 							__lowercase		(					self)			->						Any:







          '''simple docstring'''

          a__						:	Tuple =				OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint					,  provider='CPUExecutionProvider')
          a__						:	str =				EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
          pipe.set_progress_bar_config(disable=lowercase)

          a__						:	List[Any] =				self.get_dummy_inputs()
          a__						:	Tuple =				pipe(**lowercase).images
          a__						:	Any =				image[0, -3:, -3:, -1]

          assert image.shape == (1, 512, 512, 3)
          a__						:	Tuple =				np.array(
              [0.77_42_44_96, 0.77_36_01, 0.7_64_52_88, 0.7_76_95_98, 0.7_77_27_39, 0.7_73_86_88, 0.78_18_72_33, 0.77_87_95_84, 0.76_70_43])

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





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




   """simple docstring"""



   @property
   def 							__lowercase		(					self)			->						str:







          '''simple docstring'''

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

   @property
   def 							__lowercase		(					self)			->						List[str]:







          '''simple docstring'''

          a__						:	Dict =				ort.SessionOptions()
          a__						:	Any =				False
          return options

   def 							__lowercase		(					self)			->						Dict:







          '''simple docstring'''

          a__						:	Optional[Any] =				load_image(
              'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
              '/img2img/sketch-mountains-input.jpg')
          a__						:	Tuple =				init_image.resize((128, 128))
          # using the PNDM scheduler by default
          a__						:	Union[str, Any] =				OnnxStableDiffusionUpscalePipeline.from_pretrained(
              'ssube/stable-diffusion-x4-upscaler-onnx'					,  provider=self.gpu_provider					,  sess_options=self.gpu_options					,  )
          pipe.set_progress_bar_config(disable=lowercase)

          a__						:	Tuple =				'A fantasy landscape, trending on artstation'

          a__						:	Any =				torch.manual_seed(0)
          a__						:	List[str] =				pipe(
              prompt=lowercase					,  image=lowercase					,  guidance_scale=7.5					,  num_inference_steps=10					,  generator=lowercase					,  output_type='np'					,  )
          a__						:	int =				output.images
          a__						:	List[Any] =				images[0, 255:258, 383:386, -1]

          assert images.shape == (1, 512, 512, 3)
          a__						:	List[str] =				np.array([0.48_83, 0.49_47, 0.49_80, 0.49_75, 0.49_82, 0.49_80, 0.50_00, 0.50_06, 0.49_72])
          # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues

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

   def 							__lowercase		(					self)			->						Union[str, Any]:







          '''simple docstring'''

          a__						:	List[Any] =				load_image(
              'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
              '/img2img/sketch-mountains-input.jpg')
          a__						:	Tuple =				init_image.resize((128, 128))
          a__						:	int =				LMSDiscreteScheduler.from_pretrained(
              'ssube/stable-diffusion-x4-upscaler-onnx'					,  subfolder='scheduler')
          a__						:	int =				OnnxStableDiffusionUpscalePipeline.from_pretrained(
              'ssube/stable-diffusion-x4-upscaler-onnx'					,  scheduler=lowercase					,  provider=self.gpu_provider					,  sess_options=self.gpu_options					,  )
          pipe.set_progress_bar_config(disable=lowercase)

          a__						:	str =				'A fantasy landscape, trending on artstation'

          a__						:	str =				torch.manual_seed(0)
          a__						:	int =				pipe(
              prompt=lowercase					,  image=lowercase					,  guidance_scale=7.5					,  num_inference_steps=20					,  generator=lowercase					,  output_type='np'					,  )
          a__						:	Union[str, Any] =				output.images
          a__						:	Union[str, Any] =				images[0, 255:258, 383:386, -1]

          assert images.shape == (1, 512, 512, 3)
          a__						:	str =				np.array(
              [0.50_17_37_53, 0.50_22_33_56, 0.50_20_39, 0.50_23_30_36, 0.5_02_37_25, 0.5_02_26_01, 0.5_01_87_58, 0.50_23_40_85, 0.50_24_15_66])
          # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues

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









import inspect
import unittest

from transformers import ViTMSNConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch
    from torch import nn

    from transformers import ViTMSNForImageClassification, ViTMSNModel
    from transformers.models.vit_msn.modeling_vit_msn import VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
    from PIL import Image

    from transformers import ViTImageProcessor





class  A__    :




   """simple docstring"""



   def __init__(					self					,  lowercase					,  lowercase=13					,  lowercase=30					,  lowercase=2					,  lowercase=3					,  lowercase=True					,  lowercase=True					,  lowercase=32					,  lowercase=5					,  lowercase=4					,  lowercase=37					,  lowercase="gelu"					,  lowercase=0.1					,  lowercase=0.1					,  lowercase=10					,  lowercase=0.02					,  lowercase=None					,  )			->						Union[str, Any]:







          '''simple docstring'''

          a__						:	Optional[int] =				parent
          a__						:	List[str] =				batch_size
          a__						:	List[str] =				image_size
          a__						:	Dict =				patch_size
          a__						:	Optional[Any] =				num_channels
          a__						:	List[Any] =				is_training
          a__						:	str =				use_labels
          a__						:	Dict =				hidden_size
          a__						:	Tuple =				num_hidden_layers
          a__						:	Tuple =				num_attention_heads
          a__						:	Union[str, Any] =				intermediate_size
          a__						:	List[str] =				hidden_act
          a__						:	List[str] =				hidden_dropout_prob
          a__						:	Any =				attention_probs_dropout_prob
          a__						:	Dict =				type_sequence_label_size
          a__						:	Tuple =				initializer_range
          a__						:	Optional[int] =				scope

          # in ViT MSN, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
          a__						:	List[str] =				(image_size // patch_size) ** 2
          a__						:	Any =				num_patches + 1

   def 							__lowercase		(					self)			->						int:







          '''simple docstring'''

          a__						:	Tuple =				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

          a__						:	Tuple =				None
          if self.use_labels:
                 a__						:	List[Any] =				ids_tensor([self.batch_size]					,  self.type_sequence_label_size)

          a__						:	List[str] =				self.get_config()

          return config, pixel_values, labels

   def 							__lowercase		(					self)			->						Optional[int]:







          '''simple docstring'''

          return ViTMSNConfig(
              image_size=self.image_size					,  patch_size=self.patch_size					,  num_channels=self.num_channels					,  hidden_size=self.hidden_size					,  num_hidden_layers=self.num_hidden_layers					,  num_attention_heads=self.num_attention_heads					,  intermediate_size=self.intermediate_size					,  hidden_act=self.hidden_act					,  hidden_dropout_prob=self.hidden_dropout_prob					,  attention_probs_dropout_prob=self.attention_probs_dropout_prob					,  initializer_range=self.initializer_range					,  )

   def 							__lowercase		(					self					,  lowercase					,  lowercase					,  lowercase)			->						List[str]:







          '''simple docstring'''

          a__						:	int =				ViTMSNModel(config=lowercase)
          model.to(lowercase)
          model.eval()
          a__						:	int =				model(lowercase)
          self.parent.assertEqual(result.last_hidden_state.shape					,  (self.batch_size, self.seq_length, self.hidden_size))

   def 							__lowercase		(					self					,  lowercase					,  lowercase					,  lowercase)			->						Tuple:







          '''simple docstring'''

          a__						:	Optional[Any] =				self.type_sequence_label_size
          a__						:	List[str] =				ViTMSNForImageClassification(lowercase)
          model.to(lowercase)
          model.eval()
          a__						:	int =				model(lowercase					,  labels=lowercase)
          print('Pixel and labels shape: {pixel_values.shape}, {labels.shape}')
          print('Labels: {labels}')
          self.parent.assertEqual(result.logits.shape					,  (self.batch_size, self.type_sequence_label_size))

          # test greyscale images
          a__						:	List[str] =				1
          a__						:	Optional[int] =				ViTMSNForImageClassification(lowercase)
          model.to(lowercase)
          model.eval()

          a__						:	Dict =				floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
          a__						:	Dict =				model(lowercase)
          self.parent.assertEqual(result.logits.shape					,  (self.batch_size, self.type_sequence_label_size))

   def 							__lowercase		(					self)			->						Tuple:







          '''simple docstring'''

          a__						:	List[Any] =				self.prepare_config_and_inputs()
          a__ ,							a__ ,							a__						:	Optional[Any] =				config_and_inputs
          a__						:	Dict =				{'pixel_values': pixel_values}
          return config, inputs_dict





@require_torch
class  A__    (							__UpperCAmelCase ,  __UpperCAmelCase ,  unittest.TestCase	):




   """simple docstring"""



   __A   :				Any         =  (ViTMSNModel, ViTMSNForImageClassification) if is_torch_available() else ()
   __A   :				Tuple         =  (
       {'''feature-extraction''': ViTMSNModel, '''image-classification''': ViTMSNForImageClassification}
       if is_torch_available()
       else {}
   )

   __A   :				List[str]         =  False
   __A   :				Optional[Any]         =  False
   __A   :				Union[str, Any]         =  False
   __A   :				Any         =  False

   def 							__lowercase		(					self)			->						List[str]:







          '''simple docstring'''

          a__						:	Optional[int] =				ViTMSNModelTester(self)
          a__						:	Union[str, Any] =				ConfigTester(self					,  config_class=lowercase					,  has_text_modality=lowercase					,  hidden_size=37)

   def 							__lowercase		(					self)			->						List[Any]:







          '''simple docstring'''

          self.config_tester.run_common_tests()

   @unittest.skip(reason='ViTMSN does not use inputs_embeds')
   def 							__lowercase		(					self)			->						Any:







          '''simple docstring'''

          pass

   def 							__lowercase		(					self)			->						Tuple:







          '''simple docstring'''

          a__ ,							a__						:	Tuple =				self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
                 a__						:	Union[str, Any] =				model_class(lowercase)
                 self.assertIsInstance(model.get_input_embeddings()					,  (nn.Module))
                 a__						:	str =				model.get_output_embeddings()
                 self.assertTrue(x is None or isinstance(lowercase					,  nn.Linear))

   def 							__lowercase		(					self)			->						Tuple:







          '''simple docstring'''

          a__ ,							a__						:	Optional[int] =				self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
                 a__						:	Dict =				model_class(lowercase)
                 a__						:	List[Any] =				inspect.signature(model.forward)
                 # signature.parameters is an OrderedDict => so arg_names order is deterministic
                 a__						:	str =				[*signature.parameters.keys()]

                 a__						:	Optional[Any] =				['pixel_values']
                 self.assertListEqual(arg_names[:1]					,  lowercase)

   def 							__lowercase		(					self)			->						str:







          '''simple docstring'''

          a__						:	List[str] =				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*lowercase)

   def 							__lowercase		(					self)			->						str:







          '''simple docstring'''

          a__						:	Any =				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*lowercase)

   @slow
   def 							__lowercase		(					self)			->						Union[str, Any]:







          '''simple docstring'''

          for model_name in VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 a__						:	Tuple =				ViTMSNModel.from_pretrained(lowercase)
                 self.assertIsNotNone(lowercase)



def A_	(		)  ->		Dict:
       a__						:	List[str] =				Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
       return image





@require_torch
@require_vision
class  A__    (							unittest.TestCase	):




   """simple docstring"""



   @cached_property
   def 							__lowercase		(					self)			->						List[Any]:







          '''simple docstring'''

          return ViTImageProcessor.from_pretrained('facebook/vit-msn-small') if is_vision_available() else None

   @slow
   def 							__lowercase		(					self)			->						List[Any]:







          '''simple docstring'''

          torch.manual_seed(2)
          a__						:	List[str] =				ViTMSNForImageClassification.from_pretrained('facebook/vit-msn-small').to(lowercase)

          a__						:	Any =				self.default_image_processor
          a__						:	List[Any] =				prepare_img()
          a__						:	Optional[Any] =				image_processor(images=lowercase					,  return_tensors='pt').to(lowercase)

          # forward pass
          with torch.no_grad():
                 a__						:	Tuple =				model(**lowercase)

          # verify the logits
          a__						:	Union[str, Any] =				torch.Size((1, 1000))
          self.assertEqual(outputs.logits.shape					,  lowercase)

          a__						:	Any =				torch.tensor([-0.08_03, -0.44_54, -0.23_75]).to(lowercase)

          self.assertTrue(torch.allclose(outputs.logits[0, :3]					,  lowercase					,  atol=1e-4))


