






"""simple docstring"""



import inspect
import unittest

from transformers import SegformerConfig, is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

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 transformers import (
          MODEL_MAPPING,
          SegformerForImageClassification,
          SegformerForSemanticSegmentation,
          SegformerModel,
      )
      from transformers.models.segformer.modeling_segformer import SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
      from PIL import Image

      from transformers import SegformerImageProcessor






class 							__SCREAMING_SNAKE_CASE						(				snake_case_		):


       def 						_UpperCamelCase       (			self      :						Tuple							):
             '''simple docstring'''





             A__						:  int		  =  self.config_class(**self.inputs_dict							)
             self.parent.assertTrue(hasattr(snake_case			,    """hidden_sizes"""							)							)
             self.parent.assertTrue(hasattr(snake_case			,    """num_attention_heads"""							)							)
             self.parent.assertTrue(hasattr(snake_case			,    """num_encoder_blocks"""							)							)






class 							__SCREAMING_SNAKE_CASE						:


       def __init__(			self      :						Dict			,    snake_case      :						Optional[Any]			,    snake_case      :						Optional[Any]=13			,    snake_case      :						str=64			,    snake_case      :						List[str]=3			,    snake_case      :						int=4			,    snake_case      :						Tuple=[2, 2, 2, 2]			,    snake_case      :						List[Any]=[8, 4, 2, 1]			,    snake_case      :						Any=[16, 32, 64, 128]			,    snake_case      :						List[Any]=[1, 4, 8, 16]			,    snake_case      :						Union[str, Any]=[1, 2, 4, 8]			,    snake_case      :						Tuple=True			,    snake_case      :						List[Any]=True			,    snake_case      :						List[Any]="gelu"			,    snake_case      :						Optional[int]=0.1			,    snake_case      :						Any=0.1			,    snake_case      :						Optional[int]=0.02			,    snake_case      :						List[Any]=3			,    snake_case      :						Optional[int]=None			,    ):
             '''simple docstring'''





             A__						:  List[Any]		  =  parent
             A__						:  Any		  =  batch_size
             A__						:  Any		  =  image_size
             A__						:  List[str]		  =  num_channels
             A__						:  Tuple		  =  num_encoder_blocks
             A__						:  List[str]		  =  sr_ratios
             A__						:  str		  =  depths
             A__						:  Dict		  =  hidden_sizes
             A__						:  int		  =  downsampling_rates
             A__						:  List[Any]		  =  num_attention_heads
             A__						:  int		  =  is_training
             A__						:  List[Any]		  =  use_labels
             A__						:  Any		  =  hidden_act
             A__						:  Union[str, Any]		  =  hidden_dropout_prob
             A__						:  int		  =  attention_probs_dropout_prob
             A__						:  int		  =  initializer_range
             A__						:  List[Any]		  =  num_labels
             A__						:  Optional[int]		  =  scope


       def 						_UpperCamelCase       (			self      :						Union[str, Any]							):
             '''simple docstring'''





             A__						:  int		  =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

             A__						:  Tuple		  =  None
             if self.use_labels:
                   A__						:  Union[str, Any]		  =  ids_tensor([self.batch_size, self.image_size, self.image_size]			,    self.num_labels							)

             A__						:  Optional[int]		  =  self.get_config()
             return config, pixel_values, labels


       def 						_UpperCamelCase       (			self      :						Any							):
             '''simple docstring'''





             return SegformerConfig(
                 image_size=self.image_size			,    num_channels=self.num_channels			,    num_encoder_blocks=self.num_encoder_blocks			,    depths=self.depths			,    hidden_sizes=self.hidden_sizes			,    num_attention_heads=self.num_attention_heads			,    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 						_UpperCamelCase       (			self      :						Any			,    snake_case      :						List[str]			,    snake_case      :						Any			,    snake_case      :						Any							):
             '''simple docstring'''





             A__						:  int		  =  SegformerModel(config=snake_case							)
             model.to(snake_case							)
             model.eval()
             A__						:  Optional[Any]		  =  model(snake_case							)
             A__						:  Optional[int]		  =  self.image_size // (self.downsampling_rates[-1] * 2)
             self.parent.assertEqual(
                 result.last_hidden_state.shape			,    (self.batch_size, self.hidden_sizes[-1], expected_height, expected_width)							)


       def 						_UpperCamelCase       (			self      :						List[str]			,    snake_case      :						Optional[int]			,    snake_case      :						Union[str, Any]			,    snake_case      :						Optional[int]							):
             '''simple docstring'''





             A__						:  List[Any]		  =  self.num_labels
             A__						:  int		  =  SegformerForSemanticSegmentation(snake_case							)
             model.to(snake_case							)
             model.eval()
             A__						:  List[Any]		  =  model(snake_case							)
             self.parent.assertEqual(
                 result.logits.shape			,    (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)							)
             A__						:  Union[str, Any]		  =  model(snake_case			,    labels=snake_case							)
             self.parent.assertEqual(
                 result.logits.shape			,    (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)							)
             self.parent.assertGreater(result.loss			,    0.0							)


       def 						_UpperCamelCase       (			self      :						Dict			,    snake_case      :						Optional[int]			,    snake_case      :						str			,    snake_case      :						Tuple							):
             '''simple docstring'''





             A__						:  Union[str, Any]		  =  1
             A__						:  Tuple		  =  SegformerForSemanticSegmentation(config=snake_case							)
             model.to(snake_case							)
             model.eval()
             A__						:  Union[str, Any]		  =  torch.randint(0			,    1			,    (self.batch_size, self.image_size, self.image_size)							).to(snake_case							)
             A__						:  List[Any]		  =  model(snake_case			,    labels=snake_case							)
             self.parent.assertGreater(result.loss			,    0.0							)


       def 						_UpperCamelCase       (			self      :						List[str]							):
             '''simple docstring'''





             A__						:  List[str]		  =  self.prepare_config_and_inputs()
             A__  ,  A__  ,  A__						:  List[str]		  =  config_and_inputs
             A__						:  Optional[int]		  =  {"""pixel_values""": pixel_values}
             return config, inputs_dict






@require_torch
class 							__SCREAMING_SNAKE_CASE						(				snake_case_       ,		snake_case_       ,		unittest.TestCase		):
       snake_case_     =							(
           (
               SegformerModel,
               SegformerForSemanticSegmentation,
               SegformerForImageClassification,
           )
           if is_torch_available()
           else ()
       )
       snake_case_     =							(
           {
               """feature-extraction""": SegformerModel,
               """image-classification""": SegformerForImageClassification,
               """image-segmentation""": SegformerForSemanticSegmentation,
           }
           if is_torch_available()
           else {}
       )

       snake_case_     =							True
       snake_case_     =							False
       snake_case_     =							False
       snake_case_     =							False


       def 						_UpperCamelCase       (			self      :						Union[str, Any]							):
             '''simple docstring'''





             A__						:  Union[str, Any]		  =  SegformerModelTester(self							)
             A__						:  Union[str, Any]		  =  SegformerConfigTester(self			,    config_class=snake_case							)


       def 						_UpperCamelCase       (			self      :						Any							):
             '''simple docstring'''





             self.config_tester.run_common_tests()


       def 						_UpperCamelCase       (			self      :						List[Any]							):
             '''simple docstring'''





             A__						:  str		  =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*snake_case							)


       def 						_UpperCamelCase       (			self      :						Dict							):
             '''simple docstring'''





             A__						:  int		  =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_binary_image_segmentation(*snake_case							)


       def 						_UpperCamelCase       (			self      :						Optional[int]							):
             '''simple docstring'''





             A__						:  Dict		  =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_image_segmentation(*snake_case							)


       @unittest.skip("""SegFormer does not use inputs_embeds"""							)
       def 						_UpperCamelCase       (			self      :						Tuple							):
             '''simple docstring'''





             pass


       @unittest.skip("""SegFormer does not have get_input_embeddings method and get_output_embeddings methods"""							)
       def 						_UpperCamelCase       (			self      :						Tuple							):
             '''simple docstring'''





             pass


       def 						_UpperCamelCase       (			self      :						Union[str, Any]							):
             '''simple docstring'''





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

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

                   A__						:  Union[str, Any]		  =  ["""pixel_values"""]
                   self.assertListEqual(arg_names[:1]			,    snake_case							)


       def 						_UpperCamelCase       (			self      :						Dict							):
             '''simple docstring'''





             A__  ,  A__						:  int		  =  self.model_tester.prepare_config_and_inputs_for_common()
             A__						:  List[Any]		  =  True

             for model_class in self.all_model_classes:
                   A__						:  Optional[int]		  =  True
                   A__						:  Optional[Any]		  =  False
                   A__						:  Tuple		  =  True
                   A__						:  Dict		  =  model_class(snake_case							)
                   model.to(snake_case							)
                   model.eval()
                   with torch.no_grad():
                         A__						:  List[str]		  =  model(**self._prepare_for_class(snake_case			,    snake_case							)							)
                   A__						:  Optional[Any]		  =  outputs.attentions

                   A__						:  Optional[Any]		  =  sum(self.model_tester.depths							)
                   self.assertEqual(len(snake_case							)			,    snake_case							)

                   # check that output_attentions also work using config
                   del inputs_dict["output_attentions"]
                   A__						:  Dict		  =  True
                   A__						:  Optional[int]		  =  model_class(snake_case							)
                   model.to(snake_case							)
                   model.eval()
                   with torch.no_grad():
                         A__						:  List[Any]		  =  model(**self._prepare_for_class(snake_case			,    snake_case							)							)
                   A__						:  Any		  =  outputs.attentions

                   self.assertEqual(len(snake_case							)			,    snake_case							)

                   # verify the first attentions (first block, first layer)
                   A__						:  List[Any]		  =  (self.model_tester.image_size // 4) ** 2
                   A__						:  Optional[int]		  =  (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
                   self.assertListEqual(
                       list(attentions[0].shape[-3:]							)			,    [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]			,    )

                   # verify the last attentions (last block, last layer)
                   A__						:  Optional[int]		  =  (self.model_tester.image_size // 32) ** 2
                   A__						:  Union[str, Any]		  =  (self.model_tester.image_size // (32 * self.model_tester.sr_ratios[-1])) ** 2
                   self.assertListEqual(
                       list(attentions[-1].shape[-3:]							)			,    [self.model_tester.num_attention_heads[-1], expected_seq_len, expected_reduced_seq_len]			,    )
                   A__						:  List[Any]		  =  len(snake_case							)

                   # Check attention is always last and order is fine
                   A__						:  str		  =  True
                   A__						:  Dict		  =  True
                   A__						:  List[Any]		  =  model_class(snake_case							)
                   model.to(snake_case							)
                   model.eval()
                   with torch.no_grad():
                         A__						:  List[Any]		  =  model(**self._prepare_for_class(snake_case			,    snake_case							)							)

                   self.assertEqual(out_len + 1			,    len(snake_case							)							)

                   A__						:  str		  =  outputs.attentions

                   self.assertEqual(len(snake_case							)			,    snake_case							)
                   # verify the first attentions (first block, first layer)
                   A__						:  Any		  =  (self.model_tester.image_size // 4) ** 2
                   A__						:  int		  =  (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
                   self.assertListEqual(
                       list(self_attentions[0].shape[-3:]							)			,    [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]			,    )


       def 						_UpperCamelCase       (			self      :						Union[str, Any]							):
             '''simple docstring'''





             def check_hidden_states_output(snake_case      :						Optional[Any]			,    snake_case      :						Union[str, Any]			,    snake_case      :						Optional[int]							):
                   A__						:  Optional[int]		  =  model_class(snake_case							)
                   model.to(snake_case							)
                   model.eval()

                   with torch.no_grad():
                         A__						:  Any		  =  model(**self._prepare_for_class(snake_case			,    snake_case							)							)

                   A__						:  str		  =  outputs.hidden_states

                   A__						:  Optional[int]		  =  self.model_tester.num_encoder_blocks
                   self.assertEqual(len(snake_case							)			,    snake_case							)

                   # verify the first hidden states (first block)
                   self.assertListEqual(
                       list(hidden_states[0].shape[-3:]							)			,    [
                           self.model_tester.hidden_sizes[0],
                           self.model_tester.image_size // 4,
                           self.model_tester.image_size // 4,
                       ]			,    )

             A__  ,  A__						:  List[Any]		  =  self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                   A__						:  Any		  =  True
                   check_hidden_states_output(snake_case			,    snake_case			,    snake_case							)

                   # check that output_hidden_states also work using config
                   del inputs_dict["output_hidden_states"]
                   A__						:  str		  =  True

                   check_hidden_states_output(snake_case			,    snake_case			,    snake_case							)


       def 						_UpperCamelCase       (			self      :						Optional[Any]							):
             '''simple docstring'''





             if not self.model_tester.is_training:
                   return

             A__  ,  A__						:  List[str]		  =  self.model_tester.prepare_config_and_inputs_for_common()
             A__						:  Any		  =  True

             for model_class in self.all_model_classes:
                   if model_class in get_values(snake_case							):
                         continue

                   A__						:  Optional[int]		  =  model_class(snake_case							)
                   model.to(snake_case							)
                   model.train()
                   A__						:  int		  =  self._prepare_for_class(snake_case			,    snake_case			,    return_labels=snake_case							)
                   A__						:  int		  =  model(**snake_case							).loss
                   loss.backward()


       @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""							)
       def 						_UpperCamelCase       (			self      :						Any							):
             '''simple docstring'''





             pass


       @slow
       def 						_UpperCamelCase       (			self      :						Dict							):
             '''simple docstring'''





             for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   A__						:  int		  =  SegformerModel.from_pretrained(snake_case							)
                   self.assertIsNotNone(snake_case							)




def      _lowerCAmelCase		(							)		->Any:
      A__						:  List[str]		  =  Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""      )
      return image






@require_torch
class 							__SCREAMING_SNAKE_CASE						(				unittest.TestCase		):


       @slow
       def 						_UpperCamelCase       (			self      :						List[Any]							):
             '''simple docstring'''





             A__						:  Optional[Any]		  =  SegformerImageProcessor(
                 image_scale=(512, 512)			,    keep_ratio=snake_case			,    align=snake_case			,    do_random_crop=snake_case							)
             A__						:  Dict		  =  SegformerForSemanticSegmentation.from_pretrained("""nvidia/segformer-b0-finetuned-ade-512-512"""							).to(
                 snake_case							)

             A__						:  Dict		  =  prepare_img()
             A__						:  Optional[int]		  =  image_processor(images=snake_case			,    return_tensors="""pt"""							)
             A__						:  str		  =  encoded_inputs.pixel_values.to(snake_case							)

             with torch.no_grad():
                   A__						:  List[Any]		  =  model(snake_case							)

             A__						:  Union[str, Any]		  =  torch.Size((1, model.config.num_labels, 128, 128)							)
             self.assertEqual(outputs.logits.shape			,    snake_case							)

             A__						:  Union[str, Any]		  =  torch.tensor(
                 [
                     [[-4.6310, -5.5232, -6.2356], [-5.1921, -6.1444, -6.5996], [-5.4424, -6.2790, -6.7574]],
                     [[-12.1391, -13.3122, -13.9554], [-12.8732, -13.9352, -14.3563], [-12.9438, -13.8226, -14.2513]],
                     [[-12.5134, -13.4686, -14.4915], [-12.8669, -14.4343, -14.7758], [-13.2523, -14.5819, -15.0694]],
                 ]							).to(snake_case							)
             self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]			,    snake_case			,    atol=1e-4							)							)


       @slow
       def 						_UpperCamelCase       (			self      :						int							):
             '''simple docstring'''





             A__						:  Tuple		  =  SegformerImageProcessor(
                 image_scale=(512, 512)			,    keep_ratio=snake_case			,    align=snake_case			,    do_random_crop=snake_case							)
             A__						:  Dict		  =  SegformerForSemanticSegmentation.from_pretrained(
                 """nvidia/segformer-b1-finetuned-cityscapes-1024-1024"""							).to(snake_case							)

             A__						:  Tuple		  =  prepare_img()
             A__						:  List[str]		  =  image_processor(images=snake_case			,    return_tensors="""pt"""							)
             A__						:  Any		  =  encoded_inputs.pixel_values.to(snake_case							)

             with torch.no_grad():
                   A__						:  Optional[int]		  =  model(snake_case							)

             A__						:  Dict		  =  torch.Size((1, model.config.num_labels, 128, 128)							)
             self.assertEqual(outputs.logits.shape			,    snake_case							)

             A__						:  List[Any]		  =  torch.tensor(
                 [
                     [[-13.5748, -13.9111, -12.6500], [-14.3500, -15.3683, -14.2328], [-14.7532, -16.0424, -15.6087]],
                     [[-17.1651, -15.8725, -12.9653], [-17.2580, -17.3718, -14.8223], [-16.6058, -16.8783, -16.7452]],
                     [[-3.6456, -3.0209, -1.4203], [-3.0797, -3.1959, -2.0000], [-1.8757, -1.9217, -1.6997]],
                 ]							).to(snake_case							)
             self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]			,    snake_case			,    atol=1e-1							)							)


       @slow
       def 						_UpperCamelCase       (			self      :						Optional[Any]							):
             '''simple docstring'''





             A__						:  str		  =  SegformerImageProcessor(
                 image_scale=(512, 512)			,    keep_ratio=snake_case			,    align=snake_case			,    do_random_crop=snake_case							)
             A__						:  Union[str, Any]		  =  SegformerForSemanticSegmentation.from_pretrained("""nvidia/segformer-b0-finetuned-ade-512-512"""							).to(
                 snake_case							)

             A__						:  List[str]		  =  prepare_img()
             A__						:  Any		  =  image_processor(images=snake_case			,    return_tensors="""pt"""							)
             A__						:  Tuple		  =  encoded_inputs.pixel_values.to(snake_case							)

             with torch.no_grad():
                   A__						:  str		  =  model(snake_case							)

             A__						:  Tuple		  =  outputs.logits.detach().cpu()

             A__						:  List[str]		  =  image_processor.post_process_semantic_segmentation(outputs=snake_case			,    target_sizes=[(500, 300)]							)
             A__						:  Any		  =  torch.Size((500, 300)							)
             self.assertEqual(segmentation[0].shape			,    snake_case							)

             A__						:  List[str]		  =  image_processor.post_process_semantic_segmentation(outputs=snake_case							)
             A__						:  Optional[Any]		  =  torch.Size((128, 128)							)
             self.assertEqual(segmentation[0].shape			,    snake_case							)














"""simple docstring"""



import unittest

import numpy as np
import torch

from diffusers import ScoreSdeVePipeline, ScoreSdeVeScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()






class 							__SCREAMING_SNAKE_CASE						(				unittest.TestCase		):


							@property
							def 						_UpperCamelCase       (			self      :						Dict							):
													'''simple docstring'''





													torch.manual_seed(0							)
													A__						:  Optional[Any]		  =  UNetaDModel(
													    block_out_channels=(32, 64)			,    layers_per_block=2			,    sample_size=32			,    in_channels=3			,    out_channels=3			,    down_block_types=("""DownBlock2D""", """AttnDownBlock2D""")			,    up_block_types=("""AttnUpBlock2D""", """UpBlock2D""")			,    )
													return model


							def 						_UpperCamelCase       (			self      :						Optional[Any]							):
													'''simple docstring'''





													A__						:  List[Any]		  =  self.dummy_uncond_unet
													A__						:  Dict		  =  ScoreSdeVeScheduler()

													A__						:  str		  =  ScoreSdeVePipeline(unet=snake_case			,    scheduler=snake_case							)
													sde_ve.to(snake_case							)
													sde_ve.set_progress_bar_config(disable=snake_case							)

													A__						:  Optional[int]		  =  torch.manual_seed(0							)
													A__						:  Any		  =  sde_ve(num_inference_steps=2			,    output_type="""numpy"""			,    generator=snake_case							).images

													A__						:  List[str]		  =  torch.manual_seed(0							)
													A__						:  int		  =  sde_ve(num_inference_steps=2			,    output_type="""numpy"""			,    generator=snake_case			,    return_dict=snake_case							)[
													    0
													]

													A__						:  int		  =  image[0, -3:, -3:, -1]
													A__						:  Optional[Any]		  =  image_from_tuple[0, -3:, -3:, -1]

													assert image.shape == (1, 32, 32, 3)
													A__						:  Optional[Any]		  =  np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0]							)

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






@slow
@require_torch
class 							__SCREAMING_SNAKE_CASE						(				unittest.TestCase		):


							def 						_UpperCamelCase       (			self      :						Tuple							):
													'''simple docstring'''





													A__						:  Union[str, Any]		  =  """google/ncsnpp-church-256"""
													A__						:  Tuple		  =  UNetaDModel.from_pretrained(snake_case							)

													A__						:  int		  =  ScoreSdeVeScheduler.from_pretrained(snake_case							)

													A__						:  List[Any]		  =  ScoreSdeVePipeline(unet=snake_case			,    scheduler=snake_case							)
													sde_ve.to(snake_case							)
													sde_ve.set_progress_bar_config(disable=snake_case							)

													A__						:  Dict		  =  torch.manual_seed(0							)
													A__						:  Optional[int]		  =  sde_ve(num_inference_steps=10			,    output_type="""numpy"""			,    generator=snake_case							).images

													A__						:  Optional[int]		  =  image[0, -3:, -3:, -1]

													assert image.shape == (1, 256, 256, 3)

													A__						:  Dict		  =  np.array([0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0]							)

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






