



"""simple docstring"""





import argparse
import os

import gluonnlp as nlp
import mxnet as mx
import numpy as np
import torch
from gluonnlp.base import get_home_dir
from gluonnlp.model.bert import BERTEncoder
from gluonnlp.model.utils import _load_vocab
from gluonnlp.vocab import Vocab
from packaging import version
from torch import nn

from transformers import BertConfig, BertForMaskedLM, BertModel, RobertaTokenizer
from transformers.models.bert.modeling_bert import (
    BertIntermediate,
    BertLayer,
    BertOutput,
    BertSelfAttention,
    BertSelfOutput,
)
from transformers.utils import logging


if version.parse(nlp.__version__) != version.parse("""0.8.3"""):
  raise Exception("""requires gluonnlp == 0.8.3""")

if version.parse(mx.__version__) != version.parse("""1.5.0"""):
  raise Exception("""requires mxnet == 1.5.0""")

logging.set_verbosity_info()
lowerCamelCase       	=						logging.get_logger(__name__)

lowerCamelCase       	=						"""The Nymphenburg Palace is a beautiful palace in Munich!"""
def        a__     (				lowerCAmelCase__     ,      lowerCAmelCase__				):
      UpperCAmelCase_      			=							{
          "attention_cell": "multi_head",
          "num_layers": 4,
          "units": 1024,
          "hidden_size": 768,
          "max_length": 512,
          "num_heads": 8,
          "scaled": True,
          "dropout": 0.1,
          "use_residual": True,
          "embed_size": 1024,
          "embed_dropout": 0.1,
          "word_embed": None,
          "layer_norm_eps": 1e-5,
          "token_type_vocab_size": 2,
      }

      UpperCAmelCase_      			=							bort_4_8_768_1024_hparams

      # Let's construct the original Bort model here
      # Taken from official BERT implementation, see:
      # https://github.com/alexa/bort/blob/master/bort/bort.py
      UpperCAmelCase_      			=							BERTEncoder(
          attention_cell=predefined_args["attention_cell"]     ,      num_layers=predefined_args["num_layers"]     ,      units=predefined_args["units"]     ,      hidden_size=predefined_args["hidden_size"]     ,      max_length=predefined_args["max_length"]     ,      num_heads=predefined_args["num_heads"]     ,      scaled=predefined_args["scaled"]     ,      dropout=predefined_args["dropout"]     ,      output_attention=lowerCAmelCase__     ,      output_all_encodings=lowerCAmelCase__     ,      use_residual=predefined_args["use_residual"]     ,      activation=predefined_args.get("activation"     ,      "gelu"				)     ,      layer_norm_eps=predefined_args.get("layer_norm_eps"     ,      lowerCAmelCase__				)     ,      )

      # Vocab information needs to be fetched first
      # It's the same as RoBERTa, so RobertaTokenizer can be used later
      UpperCAmelCase_      			=							"openwebtext_ccnews_stories_books_cased"

      # Specify download folder to Gluonnlp's vocab
      UpperCAmelCase_      			=							os.path.join(get_home_dir()     ,      "models"				)
      UpperCAmelCase_      			=							_load_vocab(lowerCAmelCase__     ,      lowerCAmelCase__     ,      lowerCAmelCase__     ,      cls=lowerCAmelCase__				)

      UpperCAmelCase_      			=							nlp.model.BERTModel(
          lowerCAmelCase__     ,      len(lowerCAmelCase__				)     ,      units=predefined_args["units"]     ,      embed_size=predefined_args["embed_size"]     ,      embed_dropout=predefined_args["embed_dropout"]     ,      word_embed=predefined_args["word_embed"]     ,      use_pooler=lowerCAmelCase__     ,      use_token_type_embed=lowerCAmelCase__     ,      token_type_vocab_size=predefined_args["token_type_vocab_size"]     ,      use_classifier=lowerCAmelCase__     ,      use_decoder=lowerCAmelCase__     ,      )

      original_bort.load_parameters(lowerCAmelCase__     ,      cast_dtype=lowerCAmelCase__     ,      ignore_extra=lowerCAmelCase__				)
      UpperCAmelCase_      			=							original_bort._collect_params_with_prefix()

      # Build our config 🤗
      UpperCAmelCase_      			=							{
          "architectures": ["BertForMaskedLM"],
          "attention_probs_dropout_prob": predefined_args["dropout"],
          "hidden_act": "gelu",
          "hidden_dropout_prob": predefined_args["dropout"],
          "hidden_size": predefined_args["embed_size"],
          "initializer_range": 0.02,
          "intermediate_size": predefined_args["hidden_size"],
          "layer_norm_eps": predefined_args["layer_norm_eps"],
          "max_position_embeddings": predefined_args["max_length"],
          "model_type": "bort",
          "num_attention_heads": predefined_args["num_heads"],
          "num_hidden_layers": predefined_args["num_layers"],
          "pad_token_id": 1,  # 2 = BERT, 1 = RoBERTa
          "type_vocab_size": 1,  # 2 = BERT, 1 = RoBERTa
          "vocab_size": len(lowerCAmelCase__				),
      }

      UpperCAmelCase_      			=							BertConfig.from_dict(lowerCAmelCase__				)
      UpperCAmelCase_      			=							BertForMaskedLM(lowerCAmelCase__				)
      hf_bort_model.eval()

      # Parameter mapping table (Gluonnlp to Transformers)
      # * denotes layer index
      #
      # | Gluon Parameter                                                | Transformers Parameter
      # | -------------------------------------------------------------- | ----------------------
      # | `encoder.layer_norm.beta`                                      | `bert.embeddings.LayerNorm.bias`
      # | `encoder.layer_norm.gamma`                                     | `bert.embeddings.LayerNorm.weight`
      # | `encoder.position_weight`                                      | `bert.embeddings.position_embeddings.weight`
      # | `word_embed.0.weight`                                          | `bert.embeddings.word_embeddings.weight`
      # | `encoder.transformer_cells.*.attention_cell.proj_key.bias`     | `bert.encoder.layer.*.attention.self.key.bias`
      # | `encoder.transformer_cells.*.attention_cell.proj_key.weight`   | `bert.encoder.layer.*.attention.self.key.weight`
      # | `encoder.transformer_cells.*.attention_cell.proj_query.bias`   | `bert.encoder.layer.*.attention.self.query.bias`
      # | `encoder.transformer_cells.*.attention_cell.proj_query.weight` | `bert.encoder.layer.*.attention.self.query.weight`
      # | `encoder.transformer_cells.*.attention_cell.proj_value.bias`   | `bert.encoder.layer.*.attention.self.value.bias`
      # | `encoder.transformer_cells.*.attention_cell.proj_value.weight` | `bert.encoder.layer.*.attention.self.value.weight`
      # | `encoder.transformer_cells.*.ffn.ffn_2.bias`                   | `bert.encoder.layer.*.attention.output.dense.bias`
      # | `encoder.transformer_cells.*.ffn.ffn_2.weight`                 | `bert.encoder.layer.*.attention.output.dense.weight`
      # | `encoder.transformer_cells.*.layer_norm.beta`                  | `bert.encoder.layer.*.attention.output.LayerNorm.bias`
      # | `encoder.transformer_cells.*.layer_norm.gamma`                 | `bert.encoder.layer.*.attention.output.LayerNorm.weight`
      # | `encoder.transformer_cells.*.ffn.ffn_1.bias`                   | `bert.encoder.layer.*.intermediate.dense.bias`
      # | `encoder.transformer_cells.*.ffn.ffn_1.weight`                 | `bert.encoder.layer.*.intermediate.dense.weight`
      # | `encoder.transformer_cells.*.ffn.layer_norm.beta`              | `bert.encoder.layer.*.output.LayerNorm.bias`
      # | `encoder.transformer_cells.*.ffn.layer_norm.gamma`             | `bert.encoder.layer.*.output.LayerNorm.weight`
      # | `encoder.transformer_cells.*.proj.bias`                        | `bert.encoder.layer.*.output.dense.bias`
      # | `encoder.transformer_cells.*.proj.weight`                      | `bert.encoder.layer.*.output.dense.weight`

      # Helper function to convert MXNET Arrays to PyTorch
      def to_torch(lowerCAmelCase__				) -> nn.Parameter:
            return nn.Parameter(torch.FloatTensor(mx_array.data().asnumpy()				)				)

      # Check param shapes and map new HF param back
      def check_and_map_params(lowerCAmelCase__     ,      lowerCAmelCase__				):
            UpperCAmelCase_      			=							hf_param.shape

            UpperCAmelCase_      			=							to_torch(params[gluon_param]				)
            UpperCAmelCase_      			=							gluon_param.shape

            assert (
                shape_hf == shape_gluon
            ), f"""The gluon parameter {gluon_param} has shape {shape_gluon}, but expects shape {shape_hf} for Transformers"""

            return gluon_param

      UpperCAmelCase_      			=							check_and_map_params(
          hf_bort_model.bert.embeddings.word_embeddings.weight     ,      "word_embed.0.weight"				)
      UpperCAmelCase_      			=							check_and_map_params(
          hf_bort_model.bert.embeddings.position_embeddings.weight     ,      "encoder.position_weight"				)
      UpperCAmelCase_      			=							check_and_map_params(
          hf_bort_model.bert.embeddings.LayerNorm.bias     ,      "encoder.layer_norm.beta"				)
      UpperCAmelCase_      			=							check_and_map_params(
          hf_bort_model.bert.embeddings.LayerNorm.weight     ,      "encoder.layer_norm.gamma"				)

      # Inspired by RoBERTa conversion script, we just zero them out (Bort does not use them)
      UpperCAmelCase_      			=							torch.zeros_like(
          hf_bort_model.bert.embeddings.token_type_embeddings.weight.data				)

      for i in range(hf_bort_config.num_hidden_layers				):
            UpperCAmelCase_      			=							hf_bort_model.bert.encoder.layer[i]

            # self attention
            UpperCAmelCase_      			=							layer.attention.self

            UpperCAmelCase_      			=							check_and_map_params(
                self_attn.key.bias.data     ,      f"""encoder.transformer_cells.{i}.attention_cell.proj_key.bias"""				)

            UpperCAmelCase_      			=							check_and_map_params(
                self_attn.key.weight.data     ,      f"""encoder.transformer_cells.{i}.attention_cell.proj_key.weight"""				)
            UpperCAmelCase_      			=							check_and_map_params(
                self_attn.query.bias.data     ,      f"""encoder.transformer_cells.{i}.attention_cell.proj_query.bias"""				)
            UpperCAmelCase_      			=							check_and_map_params(
                self_attn.query.weight.data     ,      f"""encoder.transformer_cells.{i}.attention_cell.proj_query.weight"""				)
            UpperCAmelCase_      			=							check_and_map_params(
                self_attn.value.bias.data     ,      f"""encoder.transformer_cells.{i}.attention_cell.proj_value.bias"""				)
            UpperCAmelCase_      			=							check_and_map_params(
                self_attn.value.weight.data     ,      f"""encoder.transformer_cells.{i}.attention_cell.proj_value.weight"""				)

            # self attention output
            UpperCAmelCase_      			=							layer.attention.output

            UpperCAmelCase_      			=							check_and_map_params(
                self_output.dense.bias     ,      f"""encoder.transformer_cells.{i}.proj.bias"""				)
            UpperCAmelCase_      			=							check_and_map_params(
                self_output.dense.weight     ,      f"""encoder.transformer_cells.{i}.proj.weight"""				)
            UpperCAmelCase_      			=							check_and_map_params(
                self_output.LayerNorm.bias     ,      f"""encoder.transformer_cells.{i}.layer_norm.beta"""				)
            UpperCAmelCase_      			=							check_and_map_params(
                self_output.LayerNorm.weight     ,      f"""encoder.transformer_cells.{i}.layer_norm.gamma"""				)

            # intermediate
            UpperCAmelCase_      			=							layer.intermediate

            UpperCAmelCase_      			=							check_and_map_params(
                intermediate.dense.bias     ,      f"""encoder.transformer_cells.{i}.ffn.ffn_1.bias"""				)
            UpperCAmelCase_      			=							check_and_map_params(
                intermediate.dense.weight     ,      f"""encoder.transformer_cells.{i}.ffn.ffn_1.weight"""				)

            # output
            UpperCAmelCase_      			=							layer.output

            UpperCAmelCase_      			=							check_and_map_params(
                bert_output.dense.bias     ,      f"""encoder.transformer_cells.{i}.ffn.ffn_2.bias"""				)
            UpperCAmelCase_      			=							check_and_map_params(
                bert_output.dense.weight     ,      f"""encoder.transformer_cells.{i}.ffn.ffn_2.weight"""				)
            UpperCAmelCase_      			=							check_and_map_params(
                bert_output.LayerNorm.bias     ,      f"""encoder.transformer_cells.{i}.ffn.layer_norm.beta"""				)
            UpperCAmelCase_      			=							check_and_map_params(
                bert_output.LayerNorm.weight     ,      f"""encoder.transformer_cells.{i}.ffn.layer_norm.gamma"""				)

      # Save space and energy 🎄
      hf_bort_model.half()

      # Compare output of both models
      UpperCAmelCase_      			=							RobertaTokenizer.from_pretrained("roberta-base"				)

      UpperCAmelCase_      			=							tokenizer.encode_plus(lowerCAmelCase__				)["input_ids"]

      # Get gluon output
      UpperCAmelCase_      			=							mx.nd.array([input_ids]				)
      UpperCAmelCase_      			=							original_bort(inputs=lowerCAmelCase__     ,      token_types=[]				)

      # Get Transformer output (save and reload model again)
      hf_bort_model.save_pretrained(lowerCAmelCase__				)
      UpperCAmelCase_      			=							BertModel.from_pretrained(lowerCAmelCase__				)
      hf_bort_model.eval()

      UpperCAmelCase_      			=							tokenizer.encode_plus(lowerCAmelCase__     ,      return_tensors="pt"				)
      UpperCAmelCase_      			=							hf_bort_model(**lowerCAmelCase__				)[0]

      UpperCAmelCase_      			=							output_gluon[0].asnumpy()
      UpperCAmelCase_      			=							output_hf[0].detach().numpy()

      UpperCAmelCase_      			=							np.max(np.abs(hf_layer - gluon_layer				)				).item()
      UpperCAmelCase_      			=							np.allclose(lowerCAmelCase__     ,      lowerCAmelCase__     ,      atol=1e-3				)

      if success:
            print("✔️ Both model do output the same tensors"				)
      else:
            print("❌ Both model do **NOT** output the same tensors"				)
            print("Absolute difference is:"     ,      lowerCAmelCase__				)


if __name__ == "__main__":
  lowerCamelCase       	=						argparse.ArgumentParser()
  # Required parameters
  parser.add_argument(
      """--bort_checkpoint_path""", default=None, type=str, required=True, help="""Path the official Bort params file."""
  )
  parser.add_argument(
      """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model."""
  )
  lowerCamelCase       	=						parser.parse_args()
  convert_bort_checkpoint_to_pytorch(args.bort_checkpoint_path, args.pytorch_dump_folder_path)









"""simple docstring"""





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 				lowercase__       :







       '''simple docstring'''


       def __init__(							self      :  Any		,    _UpperCAmelCase      :  str		,    _UpperCAmelCase      :  Union[str, Any]=2		,    _UpperCAmelCase      :  Any=True		,    _UpperCAmelCase      :  int=False		,    _UpperCAmelCase      :  str=10		,    _UpperCAmelCase      :  int=3		,    _UpperCAmelCase      :  Optional[int]=32 * 8		,    _UpperCAmelCase      :  str=32 * 8		,    _UpperCAmelCase      :  Any=4		,    _UpperCAmelCase      :  List[Any]=64		,    )			->						str:


             '''simple docstring'''




             UpperCAmelCase_      			=							parent
             UpperCAmelCase_      			=							batch_size
             UpperCAmelCase_      			=							is_training
             UpperCAmelCase_      			=							use_auxiliary_loss
             UpperCAmelCase_      			=							num_queries
             UpperCAmelCase_      			=							num_channels
             UpperCAmelCase_      			=							min_size
             UpperCAmelCase_      			=							max_size
             UpperCAmelCase_      			=							num_labels
             UpperCAmelCase_      			=							hidden_dim
             UpperCAmelCase_      			=							hidden_dim



       def 					lowercase__    (							self      :  int   )			->						Optional[Any]:


             '''simple docstring'''




             UpperCAmelCase_      			=							floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]   ).to(
                 _UpperCAmelCase   )

             UpperCAmelCase_      			=							torch.ones([self.batch_size, self.min_size, self.max_size]		,    device=_UpperCAmelCase   )

             UpperCAmelCase_      			=							(
                 torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size]		,    device=_UpperCAmelCase   ) > 0.5
             ).float()
             UpperCAmelCase_      			=							(torch.rand((self.batch_size, self.num_labels)		,    device=_UpperCAmelCase   ) > 0.5).long()

             UpperCAmelCase_      			=							self.get_config()
             return config, pixel_values, pixel_mask, mask_labels, class_labels



       def 					lowercase__    (							self      :  Any   )			->						Tuple:


             '''simple docstring'''




             UpperCAmelCase_      			=							MaskaFormerConfig(
                 hidden_size=self.hidden_dim		,    )
             UpperCAmelCase_      			=							self.num_queries
             UpperCAmelCase_      			=							self.num_labels

             UpperCAmelCase_      			=							[1, 1, 1, 1]
             UpperCAmelCase_      			=							self.num_channels

             UpperCAmelCase_      			=							64
             UpperCAmelCase_      			=							128
             UpperCAmelCase_      			=							self.hidden_dim
             UpperCAmelCase_      			=							self.hidden_dim
             UpperCAmelCase_      			=							self.hidden_dim
             return config



       def 					lowercase__    (							self      :  Dict   )			->						Dict:


             '''simple docstring'''




             UpperCAmelCase_			,   UpperCAmelCase_			,   UpperCAmelCase_			,   UpperCAmelCase_			,   UpperCAmelCase_      			=							self.prepare_config_and_inputs()
             UpperCAmelCase_      			=							{"pixel_values": pixel_values, "pixel_mask": pixel_mask}
             return config, inputs_dict



       def 					lowercase__    (							self      :  Optional[Any]		,    _UpperCAmelCase      :  str		,    _UpperCAmelCase      :  Union[str, Any]   )			->						str:


             '''simple docstring'''




             UpperCAmelCase_      			=							output.encoder_hidden_states
             UpperCAmelCase_      			=							output.pixel_decoder_hidden_states
             UpperCAmelCase_      			=							output.transformer_decoder_hidden_states

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



       def 					lowercase__    (							self      :  Tuple		,    _UpperCAmelCase      :  List[Any]		,    _UpperCAmelCase      :  Any		,    _UpperCAmelCase      :  Union[str, Any]		,    _UpperCAmelCase      :  Optional[int]=False   )			->						str:


             '''simple docstring'''




             with torch.no_grad():
                   UpperCAmelCase_      			=							MaskaFormerModel(config=_UpperCAmelCase   )
                   model.to(_UpperCAmelCase   )
                   model.eval()

                   UpperCAmelCase_      			=							model(pixel_values=_UpperCAmelCase		,    pixel_mask=_UpperCAmelCase   )
                   UpperCAmelCase_      			=							model(_UpperCAmelCase		,    output_hidden_states=_UpperCAmelCase   )

             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(_UpperCAmelCase		,    _UpperCAmelCase   )







       def 					lowercase__    (							self      :  Optional[Any]		,    _UpperCAmelCase      :  Optional[int]		,    _UpperCAmelCase      :  Dict		,    _UpperCAmelCase      :  Optional[int]		,    _UpperCAmelCase      :  Union[str, Any]		,    _UpperCAmelCase      :  int   )			->						Optional[Any]:


             '''simple docstring'''




             UpperCAmelCase_      			=							MaskaFormerForUniversalSegmentation(config=_UpperCAmelCase   )
             model.to(_UpperCAmelCase   )
             model.eval()

             def comm_check_on_output(_UpperCAmelCase      :  List[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_      			=							model(pixel_values=_UpperCAmelCase		,    pixel_mask=_UpperCAmelCase   )
                   UpperCAmelCase_      			=							model(_UpperCAmelCase   )

                   comm_check_on_output(_UpperCAmelCase   )

                   UpperCAmelCase_      			=							model(
                       pixel_values=_UpperCAmelCase		,    pixel_mask=_UpperCAmelCase		,    mask_labels=_UpperCAmelCase		,    class_labels=_UpperCAmelCase   )

             comm_check_on_output(_UpperCAmelCase   )

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



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







       '''simple docstring'''


       UpperCamelCase						=					(MaskaFormerModel, MaskaFormerForUniversalSegmentation) if is_torch_available() else ()
       UpperCamelCase						=					{'''feature-extraction''': MaskaFormerModel} if is_torch_available() else {}

       UpperCamelCase						=					False
       UpperCamelCase						=					False
       UpperCamelCase						=					False
       UpperCamelCase						=					False



       def 					lowercase__    (							self      :  Tuple   )			->						Tuple:


             '''simple docstring'''




             UpperCAmelCase_      			=							MaskaFormerModelTester(self   )
             UpperCAmelCase_      			=							ConfigTester(self		,    config_class=_UpperCAmelCase		,    has_text_modality=_UpperCAmelCase   )



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


             '''simple docstring'''




             self.config_tester.run_common_tests()



       def 					lowercase__    (							self      :  Dict   )			->						str:


             '''simple docstring'''




             UpperCAmelCase_			,   UpperCAmelCase_      			=							self.model_tester.prepare_config_and_inputs_for_common()
             self.model_tester.create_and_check_maskaformer_model(_UpperCAmelCase		,    **_UpperCAmelCase		,    output_hidden_states=_UpperCAmelCase   )



       def 					lowercase__    (							self      :  Optional[int]   )			->						Dict:


             '''simple docstring'''




             UpperCAmelCase_      			=							self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_maskaformer_instance_segmentation_head_model(*_UpperCAmelCase   )



       @unittest.skip(reason="Mask2Former does not use inputs_embeds"   )
       def 					lowercase__    (							self      :  Tuple   )			->						Any:


             '''simple docstring'''




             pass



       @unittest.skip(reason="Mask2Former does not have a get_input_embeddings method"   )
       def 					lowercase__    (							self      :  Tuple   )			->						Optional[int]:


             '''simple docstring'''




             pass



       @unittest.skip(reason="Mask2Former is not a generative model"   )
       def 					lowercase__    (							self      :  str   )			->						Dict:


             '''simple docstring'''




             pass



       @unittest.skip(reason="Mask2Former does not use token embeddings"   )
       def 					lowercase__    (							self      :  Union[str, Any]   )			->						Union[str, Any]:


             '''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 					lowercase__    (							self      :  List[Any]   )			->						Dict:


             '''simple docstring'''




             pass



       @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."   )
       def 					lowercase__    (							self      :  List[str]   )			->						List[str]:


             '''simple docstring'''




             pass



       def 					lowercase__    (							self      :  Tuple   )			->						str:


             '''simple docstring'''




             UpperCAmelCase_			,   UpperCAmelCase_      			=							self.model_tester.prepare_config_and_inputs_for_common()

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

                   UpperCAmelCase_      			=							["pixel_values"]
                   self.assertListEqual(arg_names[:1]		,    _UpperCAmelCase   )



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


             '''simple docstring'''




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



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


             '''simple docstring'''




             UpperCAmelCase_      			=							(self.model_tester.min_size,) * 2
             UpperCAmelCase_      			=							{
                 "pixel_values": torch.randn((2, 3, *size)		,    device=_UpperCAmelCase   ),
                 "mask_labels": torch.randn((2, 10, *size)		,    device=_UpperCAmelCase   ),
                 "class_labels": torch.zeros(2		,    10		,    device=_UpperCAmelCase   ).long(),
             }
             UpperCAmelCase_      			=							self.model_tester.get_config()

             UpperCAmelCase_      			=							MaskaFormerForUniversalSegmentation(_UpperCAmelCase   ).to(_UpperCAmelCase   )
             UpperCAmelCase_      			=							model(**_UpperCAmelCase   )
             self.assertTrue(outputs.loss is not None   )



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


             '''simple docstring'''




             UpperCAmelCase_			,   UpperCAmelCase_      			=							self.model_tester.prepare_config_and_inputs_for_common()
             self.model_tester.create_and_check_maskaformer_model(_UpperCAmelCase		,    **_UpperCAmelCase		,    output_hidden_states=_UpperCAmelCase   )



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


             '''simple docstring'''




             UpperCAmelCase_			,   UpperCAmelCase_      			=							self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                   UpperCAmelCase_      			=							model_class(_UpperCAmelCase   ).to(_UpperCAmelCase   )
                   UpperCAmelCase_      			=							model(**_UpperCAmelCase		,    output_attentions=_UpperCAmelCase   )
                   self.assertTrue(outputs.attentions is not None   )



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


             '''simple docstring'''




             if not self.model_tester.is_training:
                   return

             UpperCAmelCase_      			=							self.all_model_classes[1]
             UpperCAmelCase_			,   UpperCAmelCase_			,   UpperCAmelCase_			,   UpperCAmelCase_			,   UpperCAmelCase_      			=							self.model_tester.prepare_config_and_inputs()

             UpperCAmelCase_      			=							model_class(_UpperCAmelCase   )
             model.to(_UpperCAmelCase   )
             model.train()

             UpperCAmelCase_      			=							model(_UpperCAmelCase		,    mask_labels=_UpperCAmelCase		,    class_labels=_UpperCAmelCase   ).loss
             loss.backward()







       def 					lowercase__    (							self      :  str   )			->						Optional[int]:


             '''simple docstring'''




             UpperCAmelCase_      			=							self.all_model_classes[1]
             UpperCAmelCase_			,   UpperCAmelCase_			,   UpperCAmelCase_			,   UpperCAmelCase_			,   UpperCAmelCase_      			=							self.model_tester.prepare_config_and_inputs()
             UpperCAmelCase_      			=							True
             UpperCAmelCase_      			=							True

             UpperCAmelCase_      			=							model_class(_UpperCAmelCase   ).to(_UpperCAmelCase   )
             model.train()

             UpperCAmelCase_      			=							model(_UpperCAmelCase		,    mask_labels=_UpperCAmelCase		,    class_labels=_UpperCAmelCase   )

             UpperCAmelCase_      			=							outputs.encoder_hidden_states[0]
             encoder_hidden_states.retain_grad()

             UpperCAmelCase_      			=							outputs.pixel_decoder_hidden_states[0]
             pixel_decoder_hidden_states.retain_grad()

             UpperCAmelCase_      			=							outputs.transformer_decoder_hidden_states[0]
             transformer_decoder_hidden_states.retain_grad()

             UpperCAmelCase_      			=							outputs.attentions[0]
             attentions.retain_grad()

             outputs.loss.backward(retain_graph=_UpperCAmelCase   )

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



lowerCamelCase       	=						1e-4
def        a__     (				):
      UpperCAmelCase_      			=							Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"				)
      return image


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







       '''simple docstring'''


       @cached_property
       def 					lowercase__    (							self      :  Optional[Any]   )			->						int:


             '''simple docstring'''




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



       @cached_property
       def 					lowercase__    (							self      :  Dict   )			->						Optional[int]:


             '''simple docstring'''




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



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


             '''simple docstring'''




             UpperCAmelCase_      			=							MaskaFormerModel.from_pretrained(self.model_checkpoints   ).to(_UpperCAmelCase   )
             UpperCAmelCase_      			=							self.default_image_processor
             UpperCAmelCase_      			=							prepare_img()
             UpperCAmelCase_      			=							image_processor(_UpperCAmelCase		,    return_tensors="pt"   ).to(_UpperCAmelCase   )
             UpperCAmelCase_      			=							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(_UpperCAmelCase		,    (1, 3, 384, 384)   )

             with torch.no_grad():
                   UpperCAmelCase_      			=							model(**_UpperCAmelCase   )

             UpperCAmelCase_      			=							torch.tensor(
                 [[-0.2790, -1.0717, -1.1668], [-0.5128, -0.3128, -0.4987], [-0.5832, 0.1971, -0.0197]]   ).to(_UpperCAmelCase   )
             self.assertTrue(
                 torch.allclose(
                     outputs.encoder_last_hidden_state[0, 0, :3, :3]		,    _UpperCAmelCase		,    atol=_UpperCAmelCase   )   )

             UpperCAmelCase_      			=							torch.tensor(
                 [[0.8973, 1.1847, 1.1776], [1.1934, 1.5040, 1.5128], [1.1153, 1.4486, 1.4951]]   ).to(_UpperCAmelCase   )
             self.assertTrue(
                 torch.allclose(
                     outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]		,    _UpperCAmelCase		,    atol=_UpperCAmelCase   )   )

             UpperCAmelCase_      			=							torch.tensor(
                 [[2.1152, 1.7000, -0.8603], [1.5808, 1.8004, -0.9353], [1.6043, 1.7495, -0.5999]]   ).to(_UpperCAmelCase   )
             self.assertTrue(
                 torch.allclose(
                     outputs.transformer_decoder_last_hidden_state[0, :3, :3]		,    _UpperCAmelCase		,    atol=_UpperCAmelCase   )   )



       def 					lowercase__    (							self      :  int   )			->						List[str]:


             '''simple docstring'''




             UpperCAmelCase_      			=							MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints   ).to(_UpperCAmelCase   ).eval()
             UpperCAmelCase_      			=							self.default_image_processor
             UpperCAmelCase_      			=							prepare_img()
             UpperCAmelCase_      			=							image_processor(_UpperCAmelCase		,    return_tensors="pt"   ).to(_UpperCAmelCase   )
             UpperCAmelCase_      			=							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(_UpperCAmelCase		,    (1, 3, 384, 384)   )

             with torch.no_grad():
                   UpperCAmelCase_      			=							model(**_UpperCAmelCase   )
             # masks_queries_logits
             UpperCAmelCase_      			=							outputs.masks_queries_logits
             self.assertEqual(
                 masks_queries_logits.shape		,    (1, model.config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)   )
             UpperCAmelCase_      			=							[
                 [-8.7839, -9.0056, -8.8121],
                 [-7.4104, -7.0313, -6.5401],
                 [-6.6105, -6.3427, -6.4675],
             ]
             UpperCAmelCase_      			=							torch.tensor(_UpperCAmelCase   ).to(_UpperCAmelCase   )
             self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]		,    _UpperCAmelCase		,    atol=_UpperCAmelCase   )   )
             # class_queries_logits
             UpperCAmelCase_      			=							outputs.class_queries_logits
             self.assertEqual(class_queries_logits.shape		,    (1, model.config.num_queries, model.config.num_labels + 1)   )
             UpperCAmelCase_      			=							torch.tensor(
                 [
                     [1.8324, -8.0835, -4.1922],
                     [0.8450, -9.0050, -3.6053],
                     [0.3045, -7.7293, -3.0275],
                 ]   ).to(_UpperCAmelCase   )
             self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]		,    _UpperCAmelCase		,    atol=_UpperCAmelCase   )   )







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


             '''simple docstring'''




             UpperCAmelCase_      			=							MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints   ).to(_UpperCAmelCase   ).eval()
             UpperCAmelCase_      			=							self.default_image_processor

             UpperCAmelCase_      			=							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_      			=							inputs["pixel_values"].to(_UpperCAmelCase   )
             UpperCAmelCase_      			=							[el.to(_UpperCAmelCase   ) for el in inputs["mask_labels"]]
             UpperCAmelCase_      			=							[el.to(_UpperCAmelCase   ) for el in inputs["class_labels"]]

             with torch.no_grad():
                   UpperCAmelCase_      			=							model(**_UpperCAmelCase   )

             self.assertTrue(outputs.loss is not None   )




