


import json
import os
from functools import lru_cache
from typing import TYPE_CHECKING, List, Optional, Tuple

import regex as re

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


if TYPE_CHECKING:
    from transformers.pipelines.conversational import Conversation

__UpperCamelCase    :						int     =							logging.get_logger(__name__)


__UpperCamelCase    :						Any     =							{
    'vocab_file': 'vocab.json',
    'merges_file': 'merges.txt',
    'tokenizer_config_file': 'tokenizer_config.json',
}

__UpperCamelCase    :						Dict     =							{
    'vocab_file': {'facebook/blenderbot-3B': 'https://huggingface.co/facebook/blenderbot-3B/resolve/main/vocab.json'},
    'merges_file': {'facebook/blenderbot-3B': 'https://huggingface.co/facebook/blenderbot-3B/resolve/main/merges.txt'},
    'tokenizer_config_file': {
        'facebook/blenderbot-3B': 'https://huggingface.co/facebook/blenderbot-3B/resolve/main/tokenizer_config.json'
    },
}

__UpperCamelCase    :						Optional[int]     =							{'facebook/blenderbot-3B': 128}





@lru_cache()
# Copied from transformers.models.roberta.tokenization_roberta.bytes_to_unicode
def   _UpperCAmelCase       (     ):
      """simple docstring"""




      __lowerCamelCase     :      Tuple       						=						(
          list(range(ord("""!"""      )						,	ord("""~"""      ) + 1      )      ) + list(range(ord("""¡"""      )						,	ord("""¬"""      ) + 1      )      ) + list(range(ord("""®"""      )						,	ord("""ÿ"""      ) + 1      )      )
      )
      __lowerCamelCase     :      Any       						=						bs[:]
      __lowerCamelCase     :      Tuple       						=						0
      for b in range(2**8      ):
            if b not in bs:
                  bs.append(UpperCAmelCase      )
                  cs.append(2**8 + n      )
                  n += 1
      __lowerCamelCase     :      int       						=						[chr(UpperCAmelCase      ) for n in cs]
      return dict(zip(UpperCAmelCase						,	UpperCAmelCase      )      )





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




      __lowerCamelCase     :      str       						=						set()
      __lowerCamelCase     :      Union[str, Any]       						=						word[0]
      for char in word[1:]:
            pairs.add((prev_char, char)      )
            __lowerCamelCase     :      Optional[int]       						=						char
      return pairs




class 							_UpperCamelCase (						A						):

   '''simple docstring'''







   a_				:							int    = VOCAB_FILES_NAMES
   a_				:							str    = PRETRAINED_VOCAB_FILES_MAP
   a_				:							int    = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
   a_				:							List[str]    = ["input_ids", "attention_mask"]

   def __init__(  self  : List[Any]       ,  _lowerCamelCase  : Dict       ,  _lowerCamelCase  : int       ,  _lowerCamelCase  : List[str]="replace"       ,  _lowerCamelCase  : List[str]="<s>"       ,  _lowerCamelCase  : Any="</s>"       ,  _lowerCamelCase  : str="</s>"       ,  _lowerCamelCase  : Any="<s>"       ,  _lowerCamelCase  : Union[str, Any]="<unk>"       ,  _lowerCamelCase  : Union[str, Any]="<pad>"       ,  _lowerCamelCase  : Optional[int]="<mask>"       ,  _lowerCamelCase  : Tuple=False       ,  **_lowerCamelCase  : Dict       ,  ):






         '''simple docstring'''



         __lowerCamelCase     :      Any       						=						AddedToken(_lowerCamelCase       ,  lstrip=_lowerCamelCase       ,  rstrip=_lowerCamelCase							) if isinstance(_lowerCamelCase       ,  _lowerCamelCase							) else bos_token
         __lowerCamelCase     :      str       						=						AddedToken(_lowerCamelCase       ,  lstrip=_lowerCamelCase       ,  rstrip=_lowerCamelCase							) if isinstance(_lowerCamelCase       ,  _lowerCamelCase							) else eos_token
         __lowerCamelCase     :      str       						=						AddedToken(_lowerCamelCase       ,  lstrip=_lowerCamelCase       ,  rstrip=_lowerCamelCase							) if isinstance(_lowerCamelCase       ,  _lowerCamelCase							) else sep_token
         __lowerCamelCase     :      Tuple       						=						AddedToken(_lowerCamelCase       ,  lstrip=_lowerCamelCase       ,  rstrip=_lowerCamelCase							) if isinstance(_lowerCamelCase       ,  _lowerCamelCase							) else cls_token
         __lowerCamelCase     :      Tuple       						=						AddedToken(_lowerCamelCase       ,  lstrip=_lowerCamelCase       ,  rstrip=_lowerCamelCase							) if isinstance(_lowerCamelCase       ,  _lowerCamelCase							) else unk_token
         __lowerCamelCase     :      Dict       						=						AddedToken(_lowerCamelCase       ,  lstrip=_lowerCamelCase       ,  rstrip=_lowerCamelCase							) if isinstance(_lowerCamelCase       ,  _lowerCamelCase							) else pad_token

         # Mask token behave like a normal word, i.e. include the space before it
         __lowerCamelCase     :      Optional[int]       						=						AddedToken(_lowerCamelCase       ,  lstrip=_lowerCamelCase       ,  rstrip=_lowerCamelCase							) if isinstance(_lowerCamelCase       ,  _lowerCamelCase							) else mask_token

         super().__init__(
             errors=_lowerCamelCase       ,  bos_token=_lowerCamelCase       ,  eos_token=_lowerCamelCase       ,  unk_token=_lowerCamelCase       ,  sep_token=_lowerCamelCase       ,  cls_token=_lowerCamelCase       ,  pad_token=_lowerCamelCase       ,  mask_token=_lowerCamelCase       ,  add_prefix_space=_lowerCamelCase       ,  **_lowerCamelCase       ,  )

         with open(_lowerCamelCase       ,  encoding="""utf-8"""							) as vocab_handle:
               __lowerCamelCase     :      Tuple       						=						json.load(_lowerCamelCase							)
         __lowerCamelCase     :      Any       						=						{v: k for k, v in self.encoder.items()}
         __lowerCamelCase     :      Optional[Any]       						=						errors  # how to handle errors in decoding
         __lowerCamelCase     :      Optional[int]       						=						bytes_to_unicode()
         __lowerCamelCase     :      Optional[int]       						=						{v: k for k, v in self.byte_encoder.items()}
         with open(_lowerCamelCase       ,  encoding="""utf-8"""							) as merges_handle:
               __lowerCamelCase     :      int       						=						merges_handle.read().split("""\n"""							)[1:-1]
         __lowerCamelCase     :      Union[str, Any]       						=						[tuple(merge.split()							) for merge in bpe_merges]
         __lowerCamelCase     :      List[Any]       						=						dict(zip(_lowerCamelCase       ,  range(len(_lowerCamelCase							)							)							)							)
         __lowerCamelCase     :      str       						=						{}
         __lowerCamelCase     :      Optional[Any]       						=						add_prefix_space

         # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions
         __lowerCamelCase     :      Tuple       						=						re.compile(R"""'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+"""							)

   @property
   # Copied from transformers.models.roberta.tokenization_roberta.RobertaTokenizer.vocab_size with Roberta->Blenderbot, RoBERTa->Blenderbot
   def   _snake_case    (  self  : Any							):






         '''simple docstring'''



         return len(self.encoder							)

   def   _snake_case    (  self  : Optional[int]							):






         '''simple docstring'''



         return dict(self.encoder       ,  **self.added_tokens_encoder							)

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






         '''simple docstring'''



         if token in self.cache:
               return self.cache[token]
         __lowerCamelCase     :      Dict       						=						tuple(_lowerCamelCase							)
         __lowerCamelCase     :      Dict       						=						get_pairs(_lowerCamelCase							)

         if not pairs:
               return token

         while True:
               __lowerCamelCase     :      List[Any]       						=						min(_lowerCamelCase       ,  key=lambda _lowerCamelCase							: self.bpe_ranks.get(_lowerCamelCase       ,  float("""inf"""							)							)							)
               if bigram not in self.bpe_ranks:
                     break
               __lowerCamelCase     :      str       						=						bigram
               __lowerCamelCase     :      Tuple       						=						[]
               __lowerCamelCase     :      List[str]       						=						0
               while i < len(_lowerCamelCase							):
                     try:
                           __lowerCamelCase     :      List[str]       						=						word.index(_lowerCamelCase       ,  _lowerCamelCase							)
                     except ValueError:
                           new_word.extend(word[i:]							)
                           break
                     else:
                           new_word.extend(word[i:j]							)
                           __lowerCamelCase     :      int       						=						j

                     if word[i] == first and i < len(_lowerCamelCase							) - 1 and word[i + 1] == second:
                           new_word.append(first + second							)
                           i += 2
                     else:
                           new_word.append(word[i]							)
                           i += 1
               __lowerCamelCase     :      List[str]       						=						tuple(_lowerCamelCase							)
               __lowerCamelCase     :      List[Any]       						=						new_word
               if len(_lowerCamelCase							) == 1:
                     break
               else:
                     __lowerCamelCase     :      List[Any]       						=						get_pairs(_lowerCamelCase							)
         __lowerCamelCase     :      List[Any]       						=						""" """.join(_lowerCamelCase							)
         __lowerCamelCase     :      List[Any]       						=						word
         return word

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






         '''simple docstring'''



         __lowerCamelCase     :      Optional[int]       						=						[]
         for token in re.findall(self.pat       ,  _lowerCamelCase							):
               __lowerCamelCase     :      Tuple       						=						"""""".join(
                   self.byte_encoder[b] for b in token.encode("""utf-8"""							)							)  # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case)
               bpe_tokens.extend(bpe_token for bpe_token in self.bpe(_lowerCamelCase							).split(""" """							)							)
         return bpe_tokens

   def   _snake_case    (  self  : str       ,  _lowerCamelCase  : Dict							):






         '''simple docstring'''



         return self.encoder.get(_lowerCamelCase       ,  self.encoder.get(self.unk_token							)							)

   def   _snake_case    (  self  : str       ,  _lowerCamelCase  : List[str]							):






         '''simple docstring'''



         return self.decoder.get(_lowerCamelCase							)

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






         '''simple docstring'''



         __lowerCamelCase     :      Union[str, Any]       						=						"""""".join(_lowerCamelCase							)
         __lowerCamelCase     :      Tuple       						=						bytearray([self.byte_decoder[c] for c in text]							).decode("""utf-8"""       ,  errors=self.errors							)
         return text

   def   _snake_case    (  self  : Optional[Any]       ,  _lowerCamelCase  : str       ,  _lowerCamelCase  : Optional[str] = None							):






         '''simple docstring'''



         if not os.path.isdir(_lowerCamelCase							):
               logger.error(F"""Vocabulary path ({save_directory}) should be a directory"""							)
               return
         __lowerCamelCase     :      Tuple       						=						os.path.join(
             _lowerCamelCase       ,  (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""]							)
         __lowerCamelCase     :      List[Any]       						=						os.path.join(
             _lowerCamelCase       ,  (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""merges_file"""]							)

         with open(_lowerCamelCase       ,  """w"""       ,  encoding="""utf-8"""							) as f:
               f.write(json.dumps(self.encoder       ,  indent=2       ,  sort_keys=_lowerCamelCase       ,  ensure_ascii=_lowerCamelCase							) + """\n"""							)

         __lowerCamelCase     :      str       						=						0
         with open(_lowerCamelCase       ,  """w"""       ,  encoding="""utf-8"""							) as writer:
               writer.write("""#version: 0.2\n"""							)
               for bpe_tokens, token_index in sorted(self.bpe_ranks.items()       ,  key=lambda _lowerCamelCase							: kv[1]							):
                     if index != token_index:
                           logger.warning(
                               F"""Saving vocabulary to {merge_file}: BPE merge indices are not consecutive."""
                               """ Please check that the tokenizer is not corrupted!"""							)
                           __lowerCamelCase     :      Any       						=						token_index
                     writer.write(""" """.join(_lowerCamelCase							) + """\n"""							)
                     index += 1

         return vocab_file, merge_file

   def   _snake_case    (  self  : Tuple       ,  _lowerCamelCase  : List[int]       ,  _lowerCamelCase  : Optional[List[int]] = None       ,  _lowerCamelCase  : bool = False							):






         '''simple docstring'''



         if already_has_special_tokens:
               return super().get_special_tokens_mask(
                   token_ids_a=_lowerCamelCase       ,  token_ids_a=_lowerCamelCase       ,  already_has_special_tokens=_lowerCamelCase							)

         if token_ids_a is None:
               return [1] + ([0] * len(_lowerCamelCase							)) + [1]
         return [1] + ([0] * len(_lowerCamelCase							)) + [1, 1] + ([0] * len(_lowerCamelCase							)) + [1]

   def   _snake_case    (  self  : int       ,  _lowerCamelCase  : List[int]       ,  _lowerCamelCase  : Optional[List[int]] = None							):






         '''simple docstring'''



         __lowerCamelCase     :      Dict       						=						[self.sep_token_id]
         __lowerCamelCase     :      Any       						=						[self.cls_token_id]

         if token_ids_a is None:
               return len(cls + token_ids_a + sep							) * [0]
         return len(cls + token_ids_a + sep + sep + token_ids_a + sep							) * [0]

   def   _snake_case    (  self  : Union[str, Any]       ,  _lowerCamelCase  : Dict       ,  _lowerCamelCase  : Union[str, Any]=False       ,  **_lowerCamelCase  : int							):






         '''simple docstring'''



         __lowerCamelCase     :      Any       						=						kwargs.pop("""add_prefix_space"""       ,  self.add_prefix_space							)
         if (is_split_into_words or add_prefix_space) and (len(_lowerCamelCase							) > 0 and not text[0].isspace()):
               __lowerCamelCase     :      Any       						=						""" """ + text
         return (text, kwargs)

   def   _snake_case    (  self  : List[Any]       ,  _lowerCamelCase  : List[int]       ,  _lowerCamelCase  : Optional[List[int]] = None							):






         '''simple docstring'''



         return token_ids_a + [self.eos_token_id]


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






         '''simple docstring'''



         __lowerCamelCase     :      Union[str, Any]       						=						[]
         for is_user, text in conversation.iter_texts():
               if is_user:
                     # We need to space prefix as it's being done within blenderbot
                     inputs.append(""" """ + text							)
               else:
                     # Generated responses should contain them already.
                     inputs.append(_lowerCamelCase							)

         __lowerCamelCase     :      Optional[int]       						=						"""  """.join(_lowerCamelCase							)
         __lowerCamelCase     :      Dict       						=						self.encode(_lowerCamelCase							)
         if len(_lowerCamelCase							) > self.model_max_length:
               __lowerCamelCase     :      Union[str, Any]       						=						input_ids[-self.model_max_length :]
               logger.warning(F"""Trimmed input from conversation as it was longer than {self.model_max_length} tokens."""							)
         return input_ids









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							)



