


from __future__ import annotations

import unittest

from transformers import XGLMConfig, XGLMTokenizer, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
    import tensorflow as tf

    from transformers.models.xglm.modeling_tf_xglm import (
        TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFXGLMForCausalLM,
        TFXGLMModel,
    )



@require_tf
class 							_UpperCamelCase :

   '''simple docstring'''







   a_				:							Optional[Any]    = XGLMConfig
   a_				:							str    = {}
   a_				:							List[Any]    = "gelu"

   def __init__(  self  : Tuple       ,  _lowerCamelCase  : Optional[Any]       ,  _lowerCamelCase  : str=1_4       ,  _lowerCamelCase  : Optional[Any]=7       ,  _lowerCamelCase  : Optional[Any]=True       ,  _lowerCamelCase  : Optional[int]=True       ,  _lowerCamelCase  : List[Any]=True       ,  _lowerCamelCase  : Any=9_9       ,  _lowerCamelCase  : Optional[Any]=3_2       ,  _lowerCamelCase  : str=2       ,  _lowerCamelCase  : Dict=4       ,  _lowerCamelCase  : Optional[Any]=3_7       ,  _lowerCamelCase  : Union[str, Any]="gelu"       ,  _lowerCamelCase  : Dict=0.1       ,  _lowerCamelCase  : List[Any]=0.1       ,  _lowerCamelCase  : Dict=5_1_2       ,  _lowerCamelCase  : Optional[int]=0.02       ,  ):






         '''simple docstring'''



         __lowerCamelCase     :      Union[str, Any]       						=						parent
         __lowerCamelCase     :      Tuple       						=						batch_size
         __lowerCamelCase     :      List[Any]       						=						seq_length
         __lowerCamelCase     :      int       						=						is_training
         __lowerCamelCase     :      List[str]       						=						use_input_mask
         __lowerCamelCase     :      Tuple       						=						use_labels
         __lowerCamelCase     :      Optional[Any]       						=						vocab_size
         __lowerCamelCase     :      Dict       						=						d_model
         __lowerCamelCase     :      Union[str, Any]       						=						num_hidden_layers
         __lowerCamelCase     :      Optional[int]       						=						num_attention_heads
         __lowerCamelCase     :      List[str]       						=						ffn_dim
         __lowerCamelCase     :      int       						=						activation_function
         __lowerCamelCase     :      int       						=						activation_dropout
         __lowerCamelCase     :      List[Any]       						=						attention_dropout
         __lowerCamelCase     :      Union[str, Any]       						=						max_position_embeddings
         __lowerCamelCase     :      Union[str, Any]       						=						initializer_range
         __lowerCamelCase     :      Any       						=						None
         __lowerCamelCase     :      List[Any]       						=						0
         __lowerCamelCase     :      List[str]       						=						2
         __lowerCamelCase     :      Dict       						=						1

   def   _snake_case    (  self  : List[Any]							):






         '''simple docstring'''



         return XGLMConfig.from_pretrained("""facebook/xglm-564M"""							)

   def   _snake_case    (  self  : Union[str, Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      List[str]       						=						tf.clip_by_value(
             ids_tensor([self.batch_size, self.seq_length]       ,  self.vocab_size							)       ,  clip_value_min=0       ,  clip_value_max=3							)

         __lowerCamelCase     :      Optional[int]       						=						None
         if self.use_input_mask:
               __lowerCamelCase     :      str       						=						random_attention_mask([self.batch_size, self.seq_length]							)

         __lowerCamelCase     :      Any       						=						self.get_config()

         __lowerCamelCase     :      Dict       						=						floats_tensor([self.num_hidden_layers, self.num_attention_heads]       ,  2							)

         return (
             config,
             input_ids,
             input_mask,
             head_mask,
         )

   def   _snake_case    (  self  : Optional[Any]							):






         '''simple docstring'''



         return XGLMConfig(
             vocab_size=self.vocab_size       ,  d_model=self.hidden_size       ,  num_layers=self.num_hidden_layers       ,  attention_heads=self.num_attention_heads       ,  ffn_dim=self.ffn_dim       ,  activation_function=self.activation_function       ,  activation_dropout=self.activation_dropout       ,  attention_dropout=self.attention_dropout       ,  max_position_embeddings=self.max_position_embeddings       ,  initializer_range=self.initializer_range       ,  use_cache=_lowerCamelCase       ,  bos_token_id=self.bos_token_id       ,  eos_token_id=self.eos_token_id       ,  pad_token_id=self.pad_token_id       ,  return_dict=_lowerCamelCase       ,  )


   def   _snake_case    (  self  : List[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      List[str]       						=						self.prepare_config_and_inputs()

         (
             (
             __lowerCamelCase
         )						, (
             __lowerCamelCase
         )						, (
             __lowerCamelCase
         )						, (
             __lowerCamelCase
         )						, 
         )     :      List[str]       						=						config_and_inputs

         __lowerCamelCase     :      Optional[int]       						=						{
             """input_ids""": input_ids,
             """head_mask""": head_mask,
         }

         return config, inputs_dict



@require_tf
class 							_UpperCamelCase (						A,A,unittest.TestCase						):

   '''simple docstring'''







   a_				:							str    = (TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else ()
   a_				:							str    = (TFXGLMForCausalLM,) if is_tf_available() else ()
   a_				:							int    = (
       {"feature-extraction": TFXGLMModel, "text-generation": TFXGLMForCausalLM} if is_tf_available() else {}
   )
   a_				:							Any    = False
   a_				:							Tuple    = False
   a_				:							List[str]    = False

   def   _snake_case    (  self  : Any							):






         '''simple docstring'''



         __lowerCamelCase     :      Union[str, Any]       						=						TFXGLMModelTester(self							)
         __lowerCamelCase     :      str       						=						ConfigTester(self       ,  config_class=_lowerCamelCase       ,  n_embd=3_7							)

   def   _snake_case    (  self  : List[Any]							):






         '''simple docstring'''



         self.config_tester.run_common_tests()

   @slow
   def   _snake_case    (  self  : int							):






         '''simple docstring'''



         for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               __lowerCamelCase     :      Dict       						=						TFXGLMModel.from_pretrained(_lowerCamelCase							)
               self.assertIsNotNone(_lowerCamelCase							)


   @unittest.skip(reason="""Currently, model embeddings are going to undergo a major refactor."""							)
   def   _snake_case    (  self  : Optional[Any]							):






         '''simple docstring'''



         super().test_resize_token_embeddings()



@require_tf
class 							_UpperCamelCase (						unittest.TestCase						):

   '''simple docstring'''







   @slow
   def   _snake_case    (  self  : Any       ,  _lowerCamelCase  : Dict=True							):






         '''simple docstring'''



         __lowerCamelCase     :      int       						=						TFXGLMForCausalLM.from_pretrained("""facebook/xglm-564M"""							)
         __lowerCamelCase     :      Optional[int]       						=						tf.convert_to_tensor([[2, 2_6_8, 9_8_6_5]]       ,  dtype=tf.intaa							)  # The dog
         # </s> The dog is a very friendly dog. He is very affectionate and loves to play with other
         # fmt: off
         __lowerCamelCase     :      int       						=						[2, 2_6_8, 9_8_6_5, 6_7, 1_1, 1_9_8_8, 5_7_2_5_2, 9_8_6_5, 5, 9_8_4, 6_7, 1_9_8_8, 2_1_3_8_3_8, 1_6_5_8, 5_3, 7_0_4_4_6, 3_3, 6_6_5_7, 2_7_8, 1_5_8_1]
         # fmt: on
         __lowerCamelCase     :      str       						=						model.generate(_lowerCamelCase       ,  do_sample=_lowerCamelCase       ,  num_beams=1							)
         if verify_outputs:
               self.assertListEqual(output_ids[0].numpy().tolist()       ,  _lowerCamelCase							)

   @slow
   def   _snake_case    (  self  : List[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      str       						=						XGLMTokenizer.from_pretrained("""facebook/xglm-564M"""							)
         __lowerCamelCase     :      Optional[int]       						=						TFXGLMForCausalLM.from_pretrained("""facebook/xglm-564M"""							)

         tf.random.set_seed(0							)
         __lowerCamelCase     :      List[str]       						=						tokenizer("""Today is a nice day and"""       ,  return_tensors="""tf"""							)
         __lowerCamelCase     :      str       						=						tokenized.input_ids
         # forces the generation to happen on CPU, to avoid GPU-related quirks (and assure same output regardless of the available devices)
         with tf.device(""":/CPU:0"""							):
               __lowerCamelCase     :      Optional[int]       						=						model.generate(_lowerCamelCase       ,  do_sample=_lowerCamelCase       ,  seed=[7, 0]							)
         __lowerCamelCase     :      Tuple       						=						tokenizer.decode(output_ids[0]       ,  skip_special_tokens=_lowerCamelCase							)

         __lowerCamelCase     :      Any       						=						(
             """Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due"""
         )
         self.assertEqual(_lowerCamelCase       ,  _lowerCamelCase							)


   @slow
   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         __lowerCamelCase     :      Dict       						=						TFXGLMForCausalLM.from_pretrained("""facebook/xglm-564M"""							)
         __lowerCamelCase     :      int       						=						XGLMTokenizer.from_pretrained("""facebook/xglm-564M"""							)

         __lowerCamelCase     :      List[str]       						=						"""left"""

         # use different length sentences to test batching
         __lowerCamelCase     :      int       						=						[
             """This is an extremelly long sentence that only exists to test the ability of the model to cope with """
             """left-padding, such as in batched generation. The output for the sequence below should be the same """
             """regardless of whether left padding is applied or not. When""",
             """Hello, my dog is a little""",
         ]

         __lowerCamelCase     :      List[str]       						=						tokenizer(_lowerCamelCase       ,  return_tensors="""tf"""       ,  padding=_lowerCamelCase							)
         __lowerCamelCase     :      List[Any]       						=						inputs["""input_ids"""]

         __lowerCamelCase     :      int       						=						model.generate(input_ids=_lowerCamelCase       ,  attention_mask=inputs["""attention_mask"""]       ,  max_new_tokens=1_2							)

         __lowerCamelCase     :      Optional[int]       						=						tokenizer(sentences[0]       ,  return_tensors="""tf"""							).input_ids
         __lowerCamelCase     :      Union[str, Any]       						=						model.generate(input_ids=_lowerCamelCase       ,  max_new_tokens=1_2							)

         __lowerCamelCase     :      Optional[Any]       						=						tokenizer(sentences[1]       ,  return_tensors="""tf"""							).input_ids
         __lowerCamelCase     :      Dict       						=						model.generate(input_ids=_lowerCamelCase       ,  max_new_tokens=1_2							)

         __lowerCamelCase     :      int       						=						tokenizer.batch_decode(_lowerCamelCase       ,  skip_special_tokens=_lowerCamelCase							)
         __lowerCamelCase     :      Any       						=						tokenizer.decode(output_non_padded[0]       ,  skip_special_tokens=_lowerCamelCase							)
         __lowerCamelCase     :      Union[str, Any]       						=						tokenizer.decode(output_padded[0]       ,  skip_special_tokens=_lowerCamelCase							)

         __lowerCamelCase     :      str       						=						[
             """This is an extremelly long sentence that only exists to test the ability of the model to cope with """
             """left-padding, such as in batched generation. The output for the sequence below should be the same """
             """regardless of whether left padding is applied or not. When left padding is applied, the sequence will be """
             """a single""",
             """Hello, my dog is a little bit of a shy one, but he is very friendly""",
         ]
         self.assertListEqual(_lowerCamelCase       ,  _lowerCamelCase							)
         self.assertListEqual(_lowerCamelCase       ,  [non_padded_sentence, padded_sentence]							)







import inspect
import unittest

import numpy as np

from tests.test_modeling_common import floats_tensor
from transformers import MaskaFormerConfig, 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 MaskaFormerForUniversalSegmentation, MaskaFormerModel

    if is_vision_available():
        from transformers import MaskaFormerImageProcessor

if is_vision_available():
    from PIL import Image



class 							_UpperCamelCase :

   '''simple docstring'''







   def __init__(  self  : int       ,  _lowerCamelCase  : List[str]       ,  _lowerCamelCase  : Tuple=2       ,  _lowerCamelCase  : Optional[int]=True       ,  _lowerCamelCase  : Union[str, Any]=False       ,  _lowerCamelCase  : Dict=1_0       ,  _lowerCamelCase  : Union[str, Any]=3       ,  _lowerCamelCase  : Tuple=3_2 * 8       ,  _lowerCamelCase  : int=3_2 * 8       ,  _lowerCamelCase  : str=4       ,  _lowerCamelCase  : List[Any]=6_4       ,  ):






         '''simple docstring'''



         __lowerCamelCase     :      Union[str, Any]       						=						parent
         __lowerCamelCase     :      Optional[int]       						=						batch_size
         __lowerCamelCase     :      List[str]       						=						is_training
         __lowerCamelCase     :      Dict       						=						use_auxiliary_loss
         __lowerCamelCase     :      Optional[int]       						=						num_queries
         __lowerCamelCase     :      Optional[Any]       						=						num_channels
         __lowerCamelCase     :      Dict       						=						min_size
         __lowerCamelCase     :      Optional[Any]       						=						max_size
         __lowerCamelCase     :      str       						=						num_labels
         __lowerCamelCase     :      Optional[Any]       						=						hidden_dim
         __lowerCamelCase     :      Optional[Any]       						=						hidden_dim

   def   _snake_case    (  self  : int							):






         '''simple docstring'''



         __lowerCamelCase     :      List[Any]       						=						floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]							).to(
             _lowerCamelCase							)

         __lowerCamelCase     :      Optional[Any]       						=						torch.ones([self.batch_size, self.min_size, self.max_size]       ,  device=_lowerCamelCase							)

         __lowerCamelCase     :      int       						=						(
             torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size]       ,  device=_lowerCamelCase							) > 0.5
         ).float()
         __lowerCamelCase     :      List[Any]       						=						(torch.rand((self.batch_size, self.num_labels)       ,  device=_lowerCamelCase							) > 0.5).long()

         __lowerCamelCase     :      Optional[Any]       						=						self.get_config()
         return config, pixel_values, pixel_mask, mask_labels, class_labels

   def   _snake_case    (  self  : Dict							):






         '''simple docstring'''



         __lowerCamelCase     :      Union[str, Any]       						=						MaskaFormerConfig(
             hidden_size=self.hidden_dim       ,  )
         __lowerCamelCase     :      List[Any]       						=						self.num_queries
         __lowerCamelCase     :      List[str]       						=						self.num_labels

         __lowerCamelCase     :      List[str]       						=						[1, 1, 1, 1]
         __lowerCamelCase     :      Optional[int]       						=						self.num_channels

         __lowerCamelCase     :      Optional[int]       						=						6_4
         __lowerCamelCase     :      int       						=						1_2_8
         __lowerCamelCase     :      Any       						=						self.hidden_dim
         __lowerCamelCase     :      List[str]       						=						self.hidden_dim
         __lowerCamelCase     :      List[Any]       						=						self.hidden_dim
         return config

   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         __lowerCamelCase						, __lowerCamelCase						, __lowerCamelCase						, __lowerCamelCase						, __lowerCamelCase     :      int       						=						self.prepare_config_and_inputs()
         __lowerCamelCase     :      Optional[int]       						=						{"""pixel_values""": pixel_values, """pixel_mask""": pixel_mask}
         return config, inputs_dict

   def   _snake_case    (  self  : str       ,  _lowerCamelCase  : int       ,  _lowerCamelCase  : Optional[int]							):






         '''simple docstring'''



         __lowerCamelCase     :      Tuple       						=						output.encoder_hidden_states
         __lowerCamelCase     :      Any       						=						output.pixel_decoder_hidden_states
         __lowerCamelCase     :      Optional[Any]       						=						output.transformer_decoder_hidden_states

         self.parent.assertTrue(len(_lowerCamelCase							)       ,  len(config.backbone_config.depths							)							)
         self.parent.assertTrue(len(_lowerCamelCase							)       ,  len(config.backbone_config.depths							)							)
         self.parent.assertTrue(len(_lowerCamelCase							)       ,  config.decoder_layers							)

   def   _snake_case    (  self  : Tuple       ,  _lowerCamelCase  : List[Any]       ,  _lowerCamelCase  : int       ,  _lowerCamelCase  : str       ,  _lowerCamelCase  : List[str]=False							):






         '''simple docstring'''



         with torch.no_grad():
               __lowerCamelCase     :      Any       						=						MaskaFormerModel(config=_lowerCamelCase							)
               model.to(_lowerCamelCase							)
               model.eval()

               __lowerCamelCase     :      Dict       						=						model(pixel_values=_lowerCamelCase       ,  pixel_mask=_lowerCamelCase							)
               __lowerCamelCase     :      Optional[Any]       						=						model(_lowerCamelCase       ,  output_hidden_states=_lowerCamelCase							)

         self.parent.assertEqual(
             output.transformer_decoder_last_hidden_state.shape       ,  (self.batch_size, self.num_queries, self.hidden_dim)       ,  )
         # 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(_lowerCamelCase       ,  _lowerCamelCase							)


   def   _snake_case    (  self  : Union[str, Any]       ,  _lowerCamelCase  : Optional[int]       ,  _lowerCamelCase  : Dict       ,  _lowerCamelCase  : List[str]       ,  _lowerCamelCase  : Union[str, Any]       ,  _lowerCamelCase  : Tuple							):






         '''simple docstring'''



         __lowerCamelCase     :      List[Any]       						=						MaskaFormerForUniversalSegmentation(config=_lowerCamelCase							)
         model.to(_lowerCamelCase							)
         model.eval()

         def comm_check_on_output(_lowerCamelCase  : Optional[int]							):
               # 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():
               __lowerCamelCase     :      Optional[int]       						=						model(pixel_values=_lowerCamelCase       ,  pixel_mask=_lowerCamelCase							)
               __lowerCamelCase     :      Tuple       						=						model(_lowerCamelCase							)

               comm_check_on_output(_lowerCamelCase							)

               __lowerCamelCase     :      str       						=						model(
                   pixel_values=_lowerCamelCase       ,  pixel_mask=_lowerCamelCase       ,  mask_labels=_lowerCamelCase       ,  class_labels=_lowerCamelCase							)

         comm_check_on_output(_lowerCamelCase							)

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



@require_torch
class 							_UpperCamelCase (						A,A,unittest.TestCase						):

   '''simple docstring'''







   a_				:							Union[str, Any]    = (MaskaFormerModel, MaskaFormerForUniversalSegmentation) if is_torch_available() else ()
   a_				:							Union[str, Any]    = {"feature-extraction": MaskaFormerModel} if is_torch_available() else {}

   a_				:							Optional[Any]    = False
   a_				:							int    = False
   a_				:							List[str]    = False
   a_				:							List[Any]    = False

   def   _snake_case    (  self  : Optional[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      Dict       						=						MaskaFormerModelTester(self							)
         __lowerCamelCase     :      int       						=						ConfigTester(self       ,  config_class=_lowerCamelCase       ,  has_text_modality=_lowerCamelCase							)

   def   _snake_case    (  self  : Optional[Any]							):






         '''simple docstring'''



         self.config_tester.run_common_tests()

   def   _snake_case    (  self  : Any							):






         '''simple docstring'''



         __lowerCamelCase						, __lowerCamelCase     :      Optional[Any]       						=						self.model_tester.prepare_config_and_inputs_for_common()
         self.model_tester.create_and_check_maskaformer_model(_lowerCamelCase       ,  **_lowerCamelCase       ,  output_hidden_states=_lowerCamelCase							)

   def   _snake_case    (  self  : int							):






         '''simple docstring'''



         __lowerCamelCase     :      Union[str, Any]       						=						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_maskaformer_instance_segmentation_head_model(*_lowerCamelCase							)

   @unittest.skip(reason="""Mask2Former does not use inputs_embeds"""							)
   def   _snake_case    (  self  : Dict							):






         '''simple docstring'''



         pass

   @unittest.skip(reason="""Mask2Former does not have a get_input_embeddings method"""							)
   def   _snake_case    (  self  : Tuple							):






         '''simple docstring'''



         pass

   @unittest.skip(reason="""Mask2Former is not a generative model"""							)
   def   _snake_case    (  self  : Optional[int]							):






         '''simple docstring'''



         pass

   @unittest.skip(reason="""Mask2Former does not use token embeddings"""							)
   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         pass

   @require_torch_multi_gpu
   @unittest.skip(
       reason="""Mask2Former has some layers using `add_module` which doesn't work well with `nn.DataParallel`"""							)
   def   _snake_case    (  self  : int							):






         '''simple docstring'''



         pass

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






         '''simple docstring'''



         pass

   def   _snake_case    (  self  : Any							):






         '''simple docstring'''



         __lowerCamelCase						, __lowerCamelCase     :      Dict       						=						self.model_tester.prepare_config_and_inputs_for_common()

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

               __lowerCamelCase     :      List[Any]       						=						["""pixel_values"""]
               self.assertListEqual(arg_names[:1]       ,  _lowerCamelCase							)

   @slow
   def   _snake_case    (  self  : Union[str, Any]							):






         '''simple docstring'''



         for model_name in ["facebook/mask2former-swin-small-coco-instance"]:
               __lowerCamelCase     :      Dict       						=						MaskaFormerModel.from_pretrained(_lowerCamelCase							)
               self.assertIsNotNone(_lowerCamelCase							)

   def   _snake_case    (  self  : Optional[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      List[Any]       						=						(self.model_tester.min_size,) * 2
         __lowerCamelCase     :      Optional[Any]       						=						{
             """pixel_values""": torch.randn((2, 3, *size)       ,  device=_lowerCamelCase							),
             """mask_labels""": torch.randn((2, 1_0, *size)       ,  device=_lowerCamelCase							),
             """class_labels""": torch.zeros(2       ,  1_0       ,  device=_lowerCamelCase							).long(),
         }
         __lowerCamelCase     :      Any       						=						self.model_tester.get_config()

         __lowerCamelCase     :      Tuple       						=						MaskaFormerForUniversalSegmentation(_lowerCamelCase							).to(_lowerCamelCase							)
         __lowerCamelCase     :      Union[str, Any]       						=						model(**_lowerCamelCase							)
         self.assertTrue(outputs.loss is not None							)

   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         __lowerCamelCase						, __lowerCamelCase     :      Any       						=						self.model_tester.prepare_config_and_inputs_for_common()
         self.model_tester.create_and_check_maskaformer_model(_lowerCamelCase       ,  **_lowerCamelCase       ,  output_hidden_states=_lowerCamelCase							)

   def   _snake_case    (  self  : Tuple							):






         '''simple docstring'''



         __lowerCamelCase						, __lowerCamelCase     :      List[str]       						=						self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
               __lowerCamelCase     :      List[str]       						=						model_class(_lowerCamelCase							).to(_lowerCamelCase							)
               __lowerCamelCase     :      int       						=						model(**_lowerCamelCase       ,  output_attentions=_lowerCamelCase							)
               self.assertTrue(outputs.attentions is not None							)

   def   _snake_case    (  self  : Any							):






         '''simple docstring'''



         if not self.model_tester.is_training:
               return

         __lowerCamelCase     :      str       						=						self.all_model_classes[1]
         __lowerCamelCase						, __lowerCamelCase						, __lowerCamelCase						, __lowerCamelCase						, __lowerCamelCase     :      Dict       						=						self.model_tester.prepare_config_and_inputs()

         __lowerCamelCase     :      List[Any]       						=						model_class(_lowerCamelCase							)
         model.to(_lowerCamelCase							)
         model.train()

         __lowerCamelCase     :      Tuple       						=						model(_lowerCamelCase       ,  mask_labels=_lowerCamelCase       ,  class_labels=_lowerCamelCase							).loss
         loss.backward()


   def   _snake_case    (  self  : Optional[int]							):






         '''simple docstring'''



         __lowerCamelCase     :      List[Any]       						=						self.all_model_classes[1]
         __lowerCamelCase						, __lowerCamelCase						, __lowerCamelCase						, __lowerCamelCase						, __lowerCamelCase     :      Optional[int]       						=						self.model_tester.prepare_config_and_inputs()
         __lowerCamelCase     :      Optional[int]       						=						True
         __lowerCamelCase     :      Any       						=						True

         __lowerCamelCase     :      Any       						=						model_class(_lowerCamelCase							).to(_lowerCamelCase							)
         model.train()

         __lowerCamelCase     :      Optional[Any]       						=						model(_lowerCamelCase       ,  mask_labels=_lowerCamelCase       ,  class_labels=_lowerCamelCase							)

         __lowerCamelCase     :      Union[str, Any]       						=						outputs.encoder_hidden_states[0]
         encoder_hidden_states.retain_grad()

         __lowerCamelCase     :      Any       						=						outputs.pixel_decoder_hidden_states[0]
         pixel_decoder_hidden_states.retain_grad()

         __lowerCamelCase     :      Optional[Any]       						=						outputs.transformer_decoder_hidden_states[0]
         transformer_decoder_hidden_states.retain_grad()

         __lowerCamelCase     :      List[str]       						=						outputs.attentions[0]
         attentions.retain_grad()

         outputs.loss.backward(retain_graph=_lowerCamelCase							)

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

__UpperCamelCase    :						str     =							1E-4





def   _UpperCAmelCase       (     ):
      """simple docstring"""




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




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

   '''simple docstring'''







   @cached_property
   def   _snake_case    (  self  : Union[str, Any]							):






         '''simple docstring'''



         return "facebook/mask2former-swin-small-coco-instance"

   @cached_property
   def   _snake_case    (  self  : Dict							):






         '''simple docstring'''



         return MaskaFormerImageProcessor.from_pretrained(self.model_checkpoints							) if is_vision_available() else None

   def   _snake_case    (  self  : List[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      int       						=						MaskaFormerModel.from_pretrained(self.model_checkpoints							).to(_lowerCamelCase							)
         __lowerCamelCase     :      str       						=						self.default_image_processor
         __lowerCamelCase     :      Dict       						=						prepare_img()
         __lowerCamelCase     :      List[Any]       						=						image_processor(_lowerCamelCase       ,  return_tensors="""pt"""							).to(_lowerCamelCase							)
         __lowerCamelCase     :      Optional[int]       						=						inputs["""pixel_values"""].shape
         # check size is divisible by 32
         self.assertTrue((inputs_shape[-1] % 3_2) == 0 and (inputs_shape[-2] % 3_2) == 0							)
         # check size
         self.assertEqual(_lowerCamelCase       ,  (1, 3, 3_8_4, 3_8_4)							)

         with torch.no_grad():
               __lowerCamelCase     :      str       						=						model(**_lowerCamelCase							)

         __lowerCamelCase     :      Dict       						=						torch.tensor(
             [[-0.2_790, -1.0_717, -1.1_668], [-0.5_128, -0.3_128, -0.4_987], [-0.5_832, 0.1_971, -0.0_197]]							).to(_lowerCamelCase							)
         self.assertTrue(
             torch.allclose(
                 outputs.encoder_last_hidden_state[0, 0, :3, :3]       ,  _lowerCamelCase       ,  atol=_lowerCamelCase							)							)

         __lowerCamelCase     :      Union[str, Any]       						=						torch.tensor(
             [[0.8_973, 1.1_847, 1.1_776], [1.1_934, 1.5_040, 1.5_128], [1.1_153, 1.4_486, 1.4_951]]							).to(_lowerCamelCase							)
         self.assertTrue(
             torch.allclose(
                 outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]       ,  _lowerCamelCase       ,  atol=_lowerCamelCase							)							)

         __lowerCamelCase     :      Tuple       						=						torch.tensor(
             [[2.1_152, 1.7_000, -0.8_603], [1.5_808, 1.8_004, -0.9_353], [1.6_043, 1.7_495, -0.5_999]]							).to(_lowerCamelCase							)
         self.assertTrue(
             torch.allclose(
                 outputs.transformer_decoder_last_hidden_state[0, :3, :3]       ,  _lowerCamelCase       ,  atol=_lowerCamelCase							)							)

   def   _snake_case    (  self  : int							):






         '''simple docstring'''



         __lowerCamelCase     :      int       						=						MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints							).to(_lowerCamelCase							).eval()
         __lowerCamelCase     :      Any       						=						self.default_image_processor
         __lowerCamelCase     :      Union[str, Any]       						=						prepare_img()
         __lowerCamelCase     :      int       						=						image_processor(_lowerCamelCase       ,  return_tensors="""pt"""							).to(_lowerCamelCase							)
         __lowerCamelCase     :      int       						=						inputs["""pixel_values"""].shape
         # check size is divisible by 32
         self.assertTrue((inputs_shape[-1] % 3_2) == 0 and (inputs_shape[-2] % 3_2) == 0							)
         # check size
         self.assertEqual(_lowerCamelCase       ,  (1, 3, 3_8_4, 3_8_4)							)

         with torch.no_grad():
               __lowerCamelCase     :      Dict       						=						model(**_lowerCamelCase							)
         # masks_queries_logits
         __lowerCamelCase     :      Dict       						=						outputs.masks_queries_logits
         self.assertEqual(
             masks_queries_logits.shape       ,  (1, model.config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)							)
         __lowerCamelCase     :      Any       						=						[
             [-8.7_839, -9.0_056, -8.8_121],
             [-7.4_104, -7.0_313, -6.5_401],
             [-6.6_105, -6.3_427, -6.4_675],
         ]
         __lowerCamelCase     :      List[str]       						=						torch.tensor(_lowerCamelCase							).to(_lowerCamelCase							)
         self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]       ,  _lowerCamelCase       ,  atol=_lowerCamelCase							)							)
         # class_queries_logits
         __lowerCamelCase     :      Union[str, Any]       						=						outputs.class_queries_logits
         self.assertEqual(class_queries_logits.shape       ,  (1, model.config.num_queries, model.config.num_labels + 1)							)
         __lowerCamelCase     :      List[Any]       						=						torch.tensor(
             [
                 [1.8_324, -8.0_835, -4.1_922],
                 [0.8_450, -9.0_050, -3.6_053],
                 [0.3_045, -7.7_293, -3.0_275],
             ]							).to(_lowerCamelCase							)
         self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]       ,  _lowerCamelCase       ,  atol=_lowerCamelCase							)							)


   def   _snake_case    (  self  : Optional[int]							):






         '''simple docstring'''



         __lowerCamelCase     :      Tuple       						=						MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints							).to(_lowerCamelCase							).eval()
         __lowerCamelCase     :      Dict       						=						self.default_image_processor

         __lowerCamelCase     :      List[Any]       						=						image_processor(
             [np.zeros((3, 8_0_0, 1_3_3_3)							), np.zeros((3, 8_0_0, 1_3_3_3)							)]       ,  segmentation_maps=[np.zeros((3_8_4, 3_8_4)							).astype(np.floataa							), np.zeros((3_8_4, 3_8_4)							).astype(np.floataa							)]       ,  return_tensors="""pt"""       ,  )

         __lowerCamelCase     :      int       						=						inputs["""pixel_values"""].to(_lowerCamelCase							)
         __lowerCamelCase     :      Optional[Any]       						=						[el.to(_lowerCamelCase							) for el in inputs["""mask_labels"""]]
         __lowerCamelCase     :      Union[str, Any]       						=						[el.to(_lowerCamelCase							) for el in inputs["""class_labels"""]]

         with torch.no_grad():
               __lowerCamelCase     :      Dict       						=						model(**_lowerCamelCase							)

         self.assertTrue(outputs.loss is not None							)



