






import inspect
import unittest

import numpy as np

from tests.test_modeling_common import floats_tensor
from transformers import DetrConfig, MaskFormerConfig, SwinConfig, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
       import torch

       from transformers import MaskFormerForInstanceSegmentation, MaskFormerModel

       if is_vision_available():
              from transformers import MaskFormerImageProcessor

if is_vision_available():
       from PIL import Image
class     a    :



      """simple docstring"""


      def __init__(      self					:			str		,      __lowercase					:			Tuple		,      __lowercase					:			List[str]=2		,      __lowercase					:			Any=True		,      __lowercase					:			Optional[int]=False		,      __lowercase					:			int=10		,      __lowercase					:			List[Any]=3		,      __lowercase					:			List[str]=32 * 4		,      __lowercase					:			Union[str, Any]=32 * 6		,      __lowercase					:			Optional[Any]=4		,      __lowercase					:			Dict=32		,      )							->      str:
        __UpperCAmelCase							:	List[str]           =  parent
        __UpperCAmelCase							:	List[Any]           =  batch_size
        __UpperCAmelCase							:	str           =  is_training
        __UpperCAmelCase							:	List[Any]           =  use_auxiliary_loss
        __UpperCAmelCase							:	int           =  num_queries
        __UpperCAmelCase							:	Optional[Any]           =  num_channels
        __UpperCAmelCase							:	int           =  min_size
        __UpperCAmelCase							:	Union[str, Any]           =  max_size
        __UpperCAmelCase							:	Tuple           =  num_labels
        __UpperCAmelCase							:	Tuple           =  mask_feature_size
      def 	UpperCAmelCase      (      self					:			Any					)							->      int:
        __UpperCAmelCase							:	Tuple           =  floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]					).to(
            __lowercase					)

        __UpperCAmelCase							:	Optional[int]           =  torch.ones([self.batch_size, self.min_size, self.max_size]		,      device=__lowercase					)

        __UpperCAmelCase							:	List[str]           =  (
            torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size]		,      device=__lowercase					) > 0.5
        ).float()
        __UpperCAmelCase							:	Optional[int]           =  (torch.rand((self.batch_size, self.num_labels)		,      device=__lowercase					) > 0.5).long()

        __UpperCAmelCase							:	Dict           =  self.get_config()
        return config, pixel_values, pixel_mask, mask_labels, class_labels
      def 	UpperCAmelCase      (      self					:			int					)							->      Dict:
        return MaskFormerConfig.from_backbone_and_decoder_configs(
            backbone_config=SwinConfig(
                depths=[1, 1, 1, 1]		,      )		,      decoder_config=DetrConfig(
                decoder_ffn_dim=128		,      num_queries=self.num_queries		,      decoder_attention_heads=2		,      d_model=self.mask_feature_size		,      )		,      mask_feature_size=self.mask_feature_size		,      fpn_feature_size=self.mask_feature_size		,      num_channels=self.num_channels		,      num_labels=self.num_labels		,      )
      def 	UpperCAmelCase      (      self					:			List[str]					)							->      List[str]:
        __UpperCAmelCase      , __UpperCAmelCase      , __UpperCAmelCase      , __UpperCAmelCase      , __UpperCAmelCase							:	List[Any]           =  self.prepare_config_and_inputs()
        __UpperCAmelCase							:	Any           =  {"""pixel_values""": pixel_values, """pixel_mask""": pixel_mask}
        return config, inputs_dict
      def 	UpperCAmelCase      (      self					:			Optional[Any]		,      __lowercase					:			Any		,      __lowercase					:			List[Any]					)							->      List[str]:
        __UpperCAmelCase							:	List[Any]           =  output.encoder_hidden_states
        __UpperCAmelCase							:	List[Any]           =  output.pixel_decoder_hidden_states
        __UpperCAmelCase							:	Any           =  output.transformer_decoder_hidden_states

        self.parent.assertTrue(len(__lowercase					)		,      len(config.backbone_config.depths					)					)
        self.parent.assertTrue(len(__lowercase					)		,      len(config.backbone_config.depths					)					)
        self.parent.assertTrue(len(__lowercase					)		,      config.decoder_config.decoder_layers					)
      def 	UpperCAmelCase      (      self					:			Tuple		,      __lowercase					:			str		,      __lowercase					:			str		,      __lowercase					:			Optional[Any]		,      __lowercase					:			Union[str, Any]=False					)							->      Any:
        with torch.no_grad():
          __UpperCAmelCase							:	str           =  MaskFormerModel(config=__lowercase					)
          model.to(__lowercase					)
          model.eval()

          __UpperCAmelCase							:	Any           =  model(pixel_values=__lowercase		,      pixel_mask=__lowercase					)
          __UpperCAmelCase							:	int           =  model(__lowercase		,      output_hidden_states=__lowercase					)
        # the correct shape of output.transformer_decoder_hidden_states ensure the correcteness of the
        # encoder and pixel decoder
        self.parent.assertEqual(
            output.transformer_decoder_last_hidden_state.shape		,      (self.batch_size, self.num_queries, self.mask_feature_size)		,      )
        # let's ensure the other two hidden state exists
        self.parent.assertTrue(output.pixel_decoder_last_hidden_state is not None					)
        self.parent.assertTrue(output.encoder_last_hidden_state is not None					)

        if output_hidden_states:
          self.check_output_hidden_state(__lowercase		,      __lowercase					)




      def 	UpperCAmelCase      (      self					:			List[Any]		,      __lowercase					:			Optional[Any]		,      __lowercase					:			Optional[Any]		,      __lowercase					:			Dict		,      __lowercase					:			int		,      __lowercase					:			Optional[Any]					)							->      Dict:
        __UpperCAmelCase							:	List[str]           =  MaskFormerForInstanceSegmentation(config=__lowercase					)
        model.to(__lowercase					)
        model.eval()

        def comm_check_on_output(__lowercase					:			Any					):
          # let's still check that all the required stuff is there
          self.parent.assertTrue(result.transformer_decoder_last_hidden_state is not None					)
          self.parent.assertTrue(result.pixel_decoder_last_hidden_state is not None					)
          self.parent.assertTrue(result.encoder_last_hidden_state is not None					)
          # okay, now we need to check the logits shape
          # due to the encoder compression, masks have a //4 spatial size
          self.parent.assertEqual(
              result.masks_queries_logits.shape		,      (self.batch_size, self.num_queries, self.min_size // 4, self.max_size // 4)		,      )
          # + 1 for null class
          self.parent.assertEqual(
              result.class_queries_logits.shape		,      (self.batch_size, self.num_queries, self.num_labels + 1)					)

        with torch.no_grad():
          __UpperCAmelCase							:	Optional[int]           =  model(pixel_values=__lowercase		,      pixel_mask=__lowercase					)
          __UpperCAmelCase							:	Union[str, Any]           =  model(__lowercase					)

          comm_check_on_output(__lowercase					)

          __UpperCAmelCase							:	Any           =  model(
              pixel_values=__lowercase		,      pixel_mask=__lowercase		,      mask_labels=__lowercase		,      class_labels=__lowercase					)

        comm_check_on_output(__lowercase					)

        self.parent.assertTrue(result.loss is not None					)
        self.parent.assertEqual(result.loss.shape		,      torch.Size([1]					)					)


@require_torch
class     a    ( lowercase__							,       lowercase__							,       unittest.TestCase   ):



      """simple docstring"""


      a  : Optional[int]							       =	(MaskFormerModel, MaskFormerForInstanceSegmentation) if is_torch_available() else ()
      a  : Tuple							       =	(
          {'feature-extraction': MaskFormerModel, 'image-segmentation': MaskFormerForInstanceSegmentation}
          if is_torch_available()
          else {}
      )

      a  : List[Any]							       =	False
      a  : Dict							       =	False
      a  : Optional[Any]							       =	False
      a  : List[Any]							       =	False
      def 	UpperCAmelCase      (      self					:			int					)							->      Optional[Any]:
        __UpperCAmelCase							:	int           =  MaskFormerModelTester(self					)
        __UpperCAmelCase							:	str           =  ConfigTester(self		,      config_class=__lowercase		,      has_text_modality=__lowercase					)
      def 	UpperCAmelCase      (      self					:			Dict					)							->      List[str]:
        self.config_tester.run_common_tests()
      def 	UpperCAmelCase      (      self					:			Dict					)							->      Any:
        __UpperCAmelCase      , __UpperCAmelCase							:	Dict           =  self.model_tester.prepare_config_and_inputs_for_common()
        self.model_tester.create_and_check_maskformer_model(__lowercase		,      **__lowercase		,      output_hidden_states=__lowercase					)
      def 	UpperCAmelCase      (      self					:			Union[str, Any]					)							->      Optional[int]:
        __UpperCAmelCase							:	Union[str, Any]           =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_maskformer_instance_segmentation_head_model(*__lowercase					)
      @unittest.skip(reason="""MaskFormer does not use inputs_embeds"""					)
      def 	UpperCAmelCase      (      self					:			Optional[int]					)							->      int:
        pass
      @unittest.skip(reason="""MaskFormer does not have a get_input_embeddings method"""					)
      def 	UpperCAmelCase      (      self					:			List[Any]					)							->      Dict:
        pass
      @unittest.skip(reason="""MaskFormer is not a generative model"""					)
      def 	UpperCAmelCase      (      self					:			int					)							->      Tuple:
        pass
      @unittest.skip(reason="""MaskFormer does not use token embeddings"""					)
      def 	UpperCAmelCase      (      self					:			Union[str, Any]					)							->      str:
        pass
      @require_torch_multi_gpu
      @unittest.skip(
          reason="""MaskFormer has some layers using `add_module` which doesn't work well with `nn.DataParallel`"""					)
      def 	UpperCAmelCase      (      self					:			List[Any]					)							->      str:
        pass
      @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""					)
      def 	UpperCAmelCase      (      self					:			Any					)							->      int:
        pass
      def 	UpperCAmelCase      (      self					:			List[Any]					)							->      List[str]:
        __UpperCAmelCase      , __UpperCAmelCase							:	List[Any]           =  self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
          __UpperCAmelCase							:	Union[str, Any]           =  model_class(__lowercase					)
          __UpperCAmelCase							:	List[str]           =  inspect.signature(model.forward					)
          # signature.parameters is an OrderedDict => so arg_names order is deterministic
          __UpperCAmelCase							:	Tuple           =  [*signature.parameters.keys()]

          __UpperCAmelCase							:	str           =  ["""pixel_values"""]
          self.assertListEqual(arg_names[:1]		,      __lowercase					)
      @slow
      def 	UpperCAmelCase      (      self					:			int					)							->      Optional[Any]:
        for model_name in ["facebook/maskformer-swin-small-coco"]:
          __UpperCAmelCase							:	int           =  MaskFormerModel.from_pretrained(__lowercase					)
          self.assertIsNotNone(__lowercase					)
      def 	UpperCAmelCase      (      self					:			Optional[int]					)							->      Optional[Any]:
        __UpperCAmelCase							:	Tuple           =  (self.model_tester.min_size,) * 2
        __UpperCAmelCase							:	str           =  {
            """pixel_values""": torch.randn((2, 3, *size)		,      device=__lowercase					),
            """mask_labels""": torch.randn((2, 10, *size)		,      device=__lowercase					),
            """class_labels""": torch.zeros(2		,      10		,      device=__lowercase					).long(),
        }

        __UpperCAmelCase							:	Dict           =  MaskFormerForInstanceSegmentation(MaskFormerConfig()					).to(__lowercase					)
        __UpperCAmelCase							:	Any           =  model(**__lowercase					)
        self.assertTrue(outputs.loss is not None					)
      def 	UpperCAmelCase      (      self					:			Union[str, Any]					)							->      Optional[int]:
        __UpperCAmelCase      , __UpperCAmelCase							:	Tuple           =  self.model_tester.prepare_config_and_inputs_for_common()
        self.model_tester.create_and_check_maskformer_model(__lowercase		,      **__lowercase		,      output_hidden_states=__lowercase					)
      def 	UpperCAmelCase      (      self					:			Any					)							->      Optional[Any]:
        __UpperCAmelCase      , __UpperCAmelCase							:	Optional[int]           =  self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
          __UpperCAmelCase							:	Tuple           =  model_class(__lowercase					).to(__lowercase					)
          __UpperCAmelCase							:	Union[str, Any]           =  model(**__lowercase		,      output_attentions=__lowercase					)
          self.assertTrue(outputs.attentions is not None					)
      def 	UpperCAmelCase      (      self					:			Dict					)							->      int:
        if not self.model_tester.is_training:
          return
        # only MaskFormerForInstanceSegmentation has the loss
        __UpperCAmelCase							:	str           =  self.all_model_classes[1]
        __UpperCAmelCase      , __UpperCAmelCase      , __UpperCAmelCase      , __UpperCAmelCase      , __UpperCAmelCase							:	Optional[int]           =  self.model_tester.prepare_config_and_inputs()

        __UpperCAmelCase							:	str           =  model_class(__lowercase					)
        model.to(__lowercase					)
        model.train()

        __UpperCAmelCase							:	Union[str, Any]           =  model(__lowercase		,      mask_labels=__lowercase		,      class_labels=__lowercase					).loss
        loss.backward()




      def 	UpperCAmelCase      (      self					:			int					)							->      List[Any]:
        # only MaskFormerForInstanceSegmentation has the loss
        __UpperCAmelCase							:	Optional[Any]           =  self.all_model_classes[1]
        __UpperCAmelCase      , __UpperCAmelCase      , __UpperCAmelCase      , __UpperCAmelCase      , __UpperCAmelCase							:	List[Any]           =  self.model_tester.prepare_config_and_inputs()
        __UpperCAmelCase							:	int           =  True
        __UpperCAmelCase							:	Optional[Any]           =  True

        __UpperCAmelCase							:	Union[str, Any]           =  model_class(__lowercase					)
        model.to(__lowercase					)
        model.train()

        __UpperCAmelCase							:	Optional[Any]           =  model(__lowercase		,      mask_labels=__lowercase		,      class_labels=__lowercase					)

        __UpperCAmelCase							:	Optional[int]           =  outputs.encoder_hidden_states[0]
        encoder_hidden_states.retain_grad()

        __UpperCAmelCase							:	str           =  outputs.pixel_decoder_hidden_states[0]
        pixel_decoder_hidden_states.retain_grad()
        # we requires_grad=True in inputs_embeds (line 2152), the original implementation don't
        __UpperCAmelCase							:	List[Any]           =  outputs.transformer_decoder_hidden_states[0]
        transformer_decoder_hidden_states.retain_grad()

        __UpperCAmelCase							:	Tuple           =  outputs.attentions[0]
        attentions.retain_grad()

        outputs.loss.backward(retain_graph=__lowercase					)

        self.assertIsNotNone(encoder_hidden_states.grad					)
        self.assertIsNotNone(pixel_decoder_hidden_states.grad					)
        self.assertIsNotNone(transformer_decoder_hidden_states.grad					)
        self.assertIsNotNone(attentions.grad					)


a		:       str			     = 1e-4






def 				lowerCamelCase__      (    ):
  __UpperCAmelCase							:	Union[str, Any]           =  Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""		)
  return image







@require_vision
@slow
class     a    ( unittest.TestCase   ):



      """simple docstring"""


      @cached_property
      def 	UpperCAmelCase      (      self					:			List[str]					)							->      str:
        return (
            MaskFormerImageProcessor.from_pretrained("""facebook/maskformer-swin-small-coco"""					)
            if is_vision_available()
            else None
        )
      def 	UpperCAmelCase      (      self					:			Optional[int]					)							->      Any:
        __UpperCAmelCase							:	Optional[int]           =  MaskFormerModel.from_pretrained("""facebook/maskformer-swin-small-coco"""					).to(__lowercase					)
        __UpperCAmelCase							:	Optional[Any]           =  self.default_image_processor
        __UpperCAmelCase							:	Optional[int]           =  prepare_img()
        __UpperCAmelCase							:	Union[str, Any]           =  image_processor(__lowercase		,      return_tensors="""pt"""					).to(__lowercase					)
        __UpperCAmelCase							:	Dict           =  inputs["""pixel_values"""].shape
        # check size is divisible by 32
        self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0					)
        # check size
        self.assertEqual(__lowercase		,      (1, 3, 800, 1088)					)

        with torch.no_grad():
          __UpperCAmelCase							:	Union[str, Any]           =  model(**__lowercase					)

        __UpperCAmelCase							:	Union[str, Any]           =  torch.tensor(
            [[-0.0_482, 0.9_228, 0.4_951], [-0.2_547, 0.8_017, 0.8_527], [-0.0_069, 0.3_385, -0.0_089]]					).to(__lowercase					)
        self.assertTrue(
            torch.allclose(
                outputs.encoder_last_hidden_state[0, 0, :3, :3]		,      __lowercase		,      atol=__lowercase					)					)

        __UpperCAmelCase							:	int           =  torch.tensor(
            [[-0.8_422, -0.8_434, -0.9_718], [-1.0_144, -0.5_565, -0.4_195], [-1.0_038, -0.4_484, -0.1_961]]					).to(__lowercase					)
        self.assertTrue(
            torch.allclose(
                outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]		,      __lowercase		,      atol=__lowercase					)					)

        __UpperCAmelCase							:	List[str]           =  torch.tensor(
            [[0.2_852, -0.0_159, 0.9_735], [0.6_254, 0.1_858, 0.8_529], [-0.0_680, -0.4_116, 1.8_413]]					).to(__lowercase					)
        self.assertTrue(
            torch.allclose(
                outputs.transformer_decoder_last_hidden_state[0, :3, :3]		,      __lowercase		,      atol=__lowercase					)					)
      def 	UpperCAmelCase      (      self					:			List[str]					)							->      Tuple:
        __UpperCAmelCase							:	Tuple           =  (
            MaskFormerForInstanceSegmentation.from_pretrained("""facebook/maskformer-swin-small-coco"""					)
            .to(__lowercase					)
            .eval()
        )
        __UpperCAmelCase							:	List[Any]           =  self.default_image_processor
        __UpperCAmelCase							:	Union[str, Any]           =  prepare_img()
        __UpperCAmelCase							:	List[Any]           =  image_processor(__lowercase		,      return_tensors="""pt"""					).to(__lowercase					)
        __UpperCAmelCase							:	List[Any]           =  inputs["""pixel_values"""].shape
        # check size is divisible by 32
        self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0					)
        # check size
        self.assertEqual(__lowercase		,      (1, 3, 800, 1088)					)

        with torch.no_grad():
          __UpperCAmelCase							:	List[Any]           =  model(**__lowercase					)
        # masks_queries_logits
        __UpperCAmelCase							:	str           =  outputs.masks_queries_logits
        self.assertEqual(
            masks_queries_logits.shape		,      (1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)		,      )
        __UpperCAmelCase							:	Tuple           =  [
            [-1.3_737_124, -1.7_724_937, -1.9_364_233],
            [-1.5_977_281, -1.9_867_939, -2.1_523_695],
            [-1.5_795_398, -1.9_269_832, -2.093_942],
        ]
        __UpperCAmelCase							:	Union[str, Any]           =  torch.tensor(__lowercase					).to(__lowercase					)
        self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]		,      __lowercase		,      atol=__lowercase					)					)
        # class_queries_logits
        __UpperCAmelCase							:	Optional[int]           =  outputs.class_queries_logits
        self.assertEqual(
            class_queries_logits.shape		,      (1, model.config.decoder_config.num_queries, model.config.num_labels + 1)					)
        __UpperCAmelCase							:	List[str]           =  torch.tensor(
            [
                [1.6_5_1_2e0_0, -5.2_5_7_2e0_0, -3.3_5_1_9e0_0],
                [3.6_1_6_9e-0_2, -5.9_0_2_5e0_0, -2.9_3_1_3e0_0],
                [1.0_7_6_6e-0_4, -7.7_6_3_0e0_0, -5.1_2_6_3e0_0],
            ]					).to(__lowercase					)
        self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]		,      __lowercase		,      atol=__lowercase					)					)
      def 	UpperCAmelCase      (      self					:			List[str]					)							->      int:
        __UpperCAmelCase							:	Optional[Any]           =  (
            MaskFormerForInstanceSegmentation.from_pretrained("""facebook/maskformer-resnet101-coco-stuff"""					)
            .to(__lowercase					)
            .eval()
        )
        __UpperCAmelCase							:	List[str]           =  self.default_image_processor
        __UpperCAmelCase							:	Any           =  prepare_img()
        __UpperCAmelCase							:	List[str]           =  image_processor(__lowercase		,      return_tensors="""pt"""					).to(__lowercase					)
        __UpperCAmelCase							:	List[Any]           =  inputs["""pixel_values"""].shape
        # check size is divisible by 32
        self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0					)
        # check size
        self.assertEqual(__lowercase		,      (1, 3, 800, 1088)					)

        with torch.no_grad():
          __UpperCAmelCase							:	Optional[int]           =  model(**__lowercase					)
        # masks_queries_logits
        __UpperCAmelCase							:	Union[str, Any]           =  outputs.masks_queries_logits
        self.assertEqual(
            masks_queries_logits.shape		,      (1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)		,      )
        __UpperCAmelCase							:	Union[str, Any]           =  [[-0.9_046, -2.6_366, -4.6_062], [-3.4_179, -5.7_890, -8.8_057], [-4.9_179, -7.6_560, -10.7_711]]
        __UpperCAmelCase							:	Optional[Any]           =  torch.tensor(__lowercase					).to(__lowercase					)
        self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]		,      __lowercase		,      atol=__lowercase					)					)
        # class_queries_logits
        __UpperCAmelCase							:	Optional[Any]           =  outputs.class_queries_logits
        self.assertEqual(
            class_queries_logits.shape		,      (1, model.config.decoder_config.num_queries, model.config.num_labels + 1)					)
        __UpperCAmelCase							:	str           =  torch.tensor(
            [[4.7_188, -3.2_585, -2.8_857], [6.6_871, -2.9_181, -1.2_487], [7.2_449, -2.2_764, -2.1_874]]					).to(__lowercase					)
        self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]		,      __lowercase		,      atol=__lowercase					)					)




      def 	UpperCAmelCase      (      self					:			List[str]					)							->      Dict:
        __UpperCAmelCase							:	Any           =  (
            MaskFormerForInstanceSegmentation.from_pretrained("""facebook/maskformer-swin-small-coco"""					)
            .to(__lowercase					)
            .eval()
        )
        __UpperCAmelCase							:	Dict           =  self.default_image_processor

        __UpperCAmelCase							:	Optional[int]           =  image_processor(
            [np.zeros((3, 800, 1333)					), np.zeros((3, 800, 1333)					)]		,      segmentation_maps=[np.zeros((384, 384)					).astype(np.floataa					), np.zeros((384, 384)					).astype(np.floataa					)]		,      return_tensors="""pt"""		,      )

        __UpperCAmelCase							:	str           =  inputs["""pixel_values"""].to(__lowercase					)
        __UpperCAmelCase							:	Dict           =  [el.to(__lowercase					) for el in inputs["""mask_labels"""]]
        __UpperCAmelCase							:	str           =  [el.to(__lowercase					) for el in inputs["""class_labels"""]]

        with torch.no_grad():
          __UpperCAmelCase							:	str           =  model(**__lowercase					)

        self.assertTrue(outputs.loss is not None					)










import random
import unittest

import numpy as np

from diffusers import (
    DPMSolverMultistepScheduler,
    EulerAncestralDiscreteScheduler,
    EulerDiscreteScheduler,
    LMSDiscreteScheduler,
    OnnxStableDiffusionImgaImgPipeline,
    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    ( lowercase__							,       unittest.TestCase   ):



      """simple docstring"""


      a  : int							       =	'hf-internal-testing/tiny-random-OnnxStableDiffusionPipeline'
      def 	UpperCAmelCase      (      self					:			List[str]		,      __lowercase					:			Optional[Any]=0					)							->      Any:
        __UpperCAmelCase							:	Any           =  floats_tensor((1, 3, 128, 128)		,      rng=random.Random(__lowercase					)					)
        __UpperCAmelCase							:	int           =  np.random.RandomState(__lowercase					)
        __UpperCAmelCase							:	Optional[Any]           =  {
            """prompt""": """A painting of a squirrel eating a burger""",
            """image""": image,
            """generator""": generator,
            """num_inference_steps""": 3,
            """strength""": 0.75,
            """guidance_scale""": 7.5,
            """output_type""": """numpy""",
        }
        return inputs
      def 	UpperCAmelCase      (      self					:			Optional[int]					)							->      Optional[Any]:
        __UpperCAmelCase							:	List[Any]           =  OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint		,      provider="""CPUExecutionProvider"""					)
        pipe.set_progress_bar_config(disable=__lowercase					)

        __UpperCAmelCase							:	int           =  self.get_dummy_inputs()
        __UpperCAmelCase							:	Optional[Any]           =  pipe(**__lowercase					).images
        __UpperCAmelCase							:	Union[str, Any]           =  image[0, -3:, -3:, -1].flatten()

        assert image.shape == (1, 128, 128, 3)
        __UpperCAmelCase							:	List[str]           =  np.array([0.69_643, 0.58_484, 0.50_314, 0.58_760, 0.55_368, 0.59_643, 0.51_529, 0.41_217, 0.49_087]					)
        assert np.abs(image_slice - expected_slice					).max() < 1e-1
      def 	UpperCAmelCase      (      self					:			Optional[Any]					)							->      List[str]:
        __UpperCAmelCase							:	Dict           =  OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint		,      provider="""CPUExecutionProvider"""					)
        __UpperCAmelCase							:	int           =  PNDMScheduler.from_config(pipe.scheduler.config		,      skip_prk_steps=__lowercase					)
        pipe.set_progress_bar_config(disable=__lowercase					)

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

        assert image.shape == (1, 128, 128, 3)
        __UpperCAmelCase							:	str           =  np.array([0.61_737, 0.54_642, 0.53_183, 0.54_465, 0.52_742, 0.60_525, 0.49_969, 0.40_655, 0.48_154]					)

        assert np.abs(image_slice.flatten() - expected_slice					).max() < 1e-1
      def 	UpperCAmelCase      (      self					:			str					)							->      Tuple:
        __UpperCAmelCase							:	Tuple           =  OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint		,      provider="""CPUExecutionProvider"""					)
        __UpperCAmelCase							:	int           =  LMSDiscreteScheduler.from_config(pipe.scheduler.config					)
        pipe.set_progress_bar_config(disable=__lowercase					)

        # warmup pass to apply optimizations
        __UpperCAmelCase							:	Optional[int]           =  pipe(**self.get_dummy_inputs()					)

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

        assert image.shape == (1, 128, 128, 3)
        __UpperCAmelCase							:	Optional[int]           =  np.array([0.52_761, 0.59_977, 0.49_033, 0.49_619, 0.54_282, 0.50_311, 0.47_600, 0.40_918, 0.45_203]					)

        assert np.abs(image_slice.flatten() - expected_slice					).max() < 1e-1
      def 	UpperCAmelCase      (      self					:			Optional[Any]					)							->      str:
        __UpperCAmelCase							:	int           =  OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint		,      provider="""CPUExecutionProvider"""					)
        __UpperCAmelCase							:	Union[str, Any]           =  EulerDiscreteScheduler.from_config(pipe.scheduler.config					)
        pipe.set_progress_bar_config(disable=__lowercase					)

        __UpperCAmelCase							:	List[str]           =  self.get_dummy_inputs()
        __UpperCAmelCase							:	int           =  pipe(**__lowercase					).images
        __UpperCAmelCase							:	Dict           =  image[0, -3:, -3:, -1]

        assert image.shape == (1, 128, 128, 3)
        __UpperCAmelCase							:	Tuple           =  np.array([0.52_911, 0.60_004, 0.49_229, 0.49_805, 0.54_502, 0.50_680, 0.47_777, 0.41_028, 0.45_304]					)

        assert np.abs(image_slice.flatten() - expected_slice					).max() < 1e-1
      def 	UpperCAmelCase      (      self					:			int					)							->      Any:
        __UpperCAmelCase							:	Any           =  OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint		,      provider="""CPUExecutionProvider"""					)
        __UpperCAmelCase							:	List[Any]           =  EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config					)
        pipe.set_progress_bar_config(disable=__lowercase					)

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

        assert image.shape == (1, 128, 128, 3)
        __UpperCAmelCase							:	List[str]           =  np.array([0.52_911, 0.60_004, 0.49_229, 0.49_805, 0.54_502, 0.50_680, 0.47_777, 0.41_028, 0.45_304]					)

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




      def 	UpperCAmelCase      (      self					:			Tuple					)							->      str:
        __UpperCAmelCase							:	Dict           =  OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint		,      provider="""CPUExecutionProvider"""					)
        __UpperCAmelCase							:	str           =  DPMSolverMultistepScheduler.from_config(pipe.scheduler.config					)
        pipe.set_progress_bar_config(disable=__lowercase					)

        __UpperCAmelCase							:	Optional[Any]           =  self.get_dummy_inputs()
        __UpperCAmelCase							:	int           =  pipe(**__lowercase					).images
        __UpperCAmelCase							:	List[Any]           =  image[0, -3:, -3:, -1]

        assert image.shape == (1, 128, 128, 3)
        __UpperCAmelCase							:	Union[str, Any]           =  np.array([0.65_331, 0.58_277, 0.48_204, 0.56_059, 0.53_665, 0.56_235, 0.50_969, 0.40_009, 0.46_552]					)

        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 	UpperCAmelCase      (      self					:			Dict					)							->      List[Any]:
        return (
            "CUDAExecutionProvider",
            {
                "gpu_mem_limit": "15000000000",  # 15GB
                "arena_extend_strategy": "kSameAsRequested",
            },
        )
      @property
      def 	UpperCAmelCase      (      self					:			Tuple					)							->      Tuple:
        __UpperCAmelCase							:	Optional[int]           =  ort.SessionOptions()
        __UpperCAmelCase							:	List[Any]           =  False
        return options
      def 	UpperCAmelCase      (      self					:			List[str]					)							->      Tuple:
        __UpperCAmelCase							:	List[Any]           =  load_image(
            """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
            """/img2img/sketch-mountains-input.jpg"""					)
        __UpperCAmelCase							:	Dict           =  init_image.resize((768, 512)					)
        # using the PNDM scheduler by default
        __UpperCAmelCase							:	Any           =  OnnxStableDiffusionImgaImgPipeline.from_pretrained(
            """CompVis/stable-diffusion-v1-4"""		,      revision="""onnx"""		,      safety_checker=__lowercase		,      feature_extractor=__lowercase		,      provider=self.gpu_provider		,      sess_options=self.gpu_options		,      )
        pipe.set_progress_bar_config(disable=__lowercase					)

        __UpperCAmelCase							:	Dict           =  """A fantasy landscape, trending on artstation"""

        __UpperCAmelCase							:	str           =  np.random.RandomState(0					)
        __UpperCAmelCase							:	Optional[Any]           =  pipe(
            prompt=__lowercase		,      image=__lowercase		,      strength=0.75		,      guidance_scale=7.5		,      num_inference_steps=10		,      generator=__lowercase		,      output_type="""np"""		,      )
        __UpperCAmelCase							:	str           =  output.images
        __UpperCAmelCase							:	int           =  images[0, 255:258, 383:386, -1]

        assert images.shape == (1, 512, 768, 3)
        __UpperCAmelCase							:	Union[str, Any]           =  np.array([0.4_909, 0.5_059, 0.5_372, 0.4_623, 0.4_876, 0.5_049, 0.4_820, 0.4_956, 0.5_019]					)
        # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues

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




      def 	UpperCAmelCase      (      self					:			Optional[Any]					)							->      str:
        __UpperCAmelCase							:	Tuple           =  load_image(
            """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
            """/img2img/sketch-mountains-input.jpg"""					)
        __UpperCAmelCase							:	int           =  init_image.resize((768, 512)					)
        __UpperCAmelCase							:	Tuple           =  LMSDiscreteScheduler.from_pretrained(
            """runwayml/stable-diffusion-v1-5"""		,      subfolder="""scheduler"""		,      revision="""onnx"""					)
        __UpperCAmelCase							:	Tuple           =  OnnxStableDiffusionImgaImgPipeline.from_pretrained(
            """runwayml/stable-diffusion-v1-5"""		,      revision="""onnx"""		,      scheduler=__lowercase		,      safety_checker=__lowercase		,      feature_extractor=__lowercase		,      provider=self.gpu_provider		,      sess_options=self.gpu_options		,      )
        pipe.set_progress_bar_config(disable=__lowercase					)

        __UpperCAmelCase							:	Dict           =  """A fantasy landscape, trending on artstation"""

        __UpperCAmelCase							:	int           =  np.random.RandomState(0					)
        __UpperCAmelCase							:	Optional[int]           =  pipe(
            prompt=__lowercase		,      image=__lowercase		,      strength=0.75		,      guidance_scale=7.5		,      num_inference_steps=20		,      generator=__lowercase		,      output_type="""np"""		,      )
        __UpperCAmelCase							:	Union[str, Any]           =  output.images
        __UpperCAmelCase							:	Union[str, Any]           =  images[0, 255:258, 383:386, -1]

        assert images.shape == (1, 512, 768, 3)
        __UpperCAmelCase							:	str           =  np.array([0.8_043, 0.926, 0.9_581, 0.8_119, 0.8_954, 0.913, 0.7_209, 0.7_463, 0.7_431]					)
        # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues

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


