


import os
import unittest

from transformers import MobileBertTokenizer, MobileBertTokenizerFast
from transformers.models.bert.tokenization_bert import (
    VOCAB_FILES_NAMES,
    BasicTokenizer,
    WordpieceTokenizer,
    _is_control,
    _is_punctuation,
    _is_whitespace,
)
from transformers.testing_utils import require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin, filter_non_english



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

   '''simple docstring'''







   a_				:							Union[str, Any]    = MobileBertTokenizer
   a_				:							Any    = MobileBertTokenizerFast
   a_				:							Tuple    = True
   a_				:							Optional[int]    = True
   a_				:							List[str]    = filter_non_english
   a_				:							Dict    = "google/mobilebert-uncased"

   def   _snake_case    (  self  : Tuple							):






         '''simple docstring'''



         super().setUp()

         __lowerCamelCase     :      str       						=						[
             """[UNK]""",
             """[CLS]""",
             """[SEP]""",
             """[PAD]""",
             """[MASK]""",
             """want""",
             """##want""",
             """##ed""",
             """wa""",
             """un""",
             """runn""",
             """##ing""",
             """,""",
             """low""",
             """lowest""",
         ]
         __lowerCamelCase     :      Dict       						=						os.path.join(self.tmpdirname       ,  VOCAB_FILES_NAMES["""vocab_file"""]							)
         with open(self.vocab_file       ,  """w"""       ,  encoding="""utf-8"""							) as vocab_writer:
               vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens]							)							)

         __lowerCamelCase     :      Optional[Any]       						=						[
             (tokenizer_def[0], self.pre_trained_model_path, tokenizer_def[2])  # else the 'google/' prefix is stripped
             for tokenizer_def in self.tokenizers_list
         ]

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






         '''simple docstring'''



         __lowerCamelCase     :      Tuple       						=						"""UNwant\u00E9d,running"""
         __lowerCamelCase     :      Tuple       						=						"""unwanted, running"""
         return input_text, output_text

   def   _snake_case    (  self  : Optional[int]							):






         '''simple docstring'''



         __lowerCamelCase     :      Tuple       						=						self.tokenizer_class(self.vocab_file							)

         __lowerCamelCase     :      str       						=						tokenizer.tokenize("""UNwant\u00E9d,running"""							)
         self.assertListEqual(_lowerCamelCase       ,  ["""un""", """##want""", """##ed""", """,""", """runn""", """##ing"""]							)
         self.assertListEqual(tokenizer.convert_tokens_to_ids(_lowerCamelCase							)       ,  [9, 6, 7, 1_2, 1_0, 1_1]							)

   def   _snake_case    (  self  : List[str]							):






         '''simple docstring'''



         if not self.test_rust_tokenizer:
               return

         __lowerCamelCase     :      str       						=						self.get_tokenizer()
         __lowerCamelCase     :      Union[str, Any]       						=						self.get_rust_tokenizer()

         __lowerCamelCase     :      str       						=						"""UNwant\u00E9d,running"""

         __lowerCamelCase     :      Dict       						=						tokenizer.tokenize(_lowerCamelCase							)
         __lowerCamelCase     :      List[Any]       						=						rust_tokenizer.tokenize(_lowerCamelCase							)
         self.assertListEqual(_lowerCamelCase       ,  _lowerCamelCase							)

         __lowerCamelCase     :      Any       						=						tokenizer.encode(_lowerCamelCase       ,  add_special_tokens=_lowerCamelCase							)
         __lowerCamelCase     :      List[Any]       						=						rust_tokenizer.encode(_lowerCamelCase       ,  add_special_tokens=_lowerCamelCase							)
         self.assertListEqual(_lowerCamelCase       ,  _lowerCamelCase							)

         __lowerCamelCase     :      Union[str, Any]       						=						self.get_rust_tokenizer()
         __lowerCamelCase     :      Tuple       						=						tokenizer.encode(_lowerCamelCase							)
         __lowerCamelCase     :      Optional[int]       						=						rust_tokenizer.encode(_lowerCamelCase							)
         self.assertListEqual(_lowerCamelCase       ,  _lowerCamelCase							)

         # With lower casing
         __lowerCamelCase     :      Union[str, Any]       						=						self.get_tokenizer(do_lower_case=_lowerCamelCase							)
         __lowerCamelCase     :      Dict       						=						self.get_rust_tokenizer(do_lower_case=_lowerCamelCase							)

         __lowerCamelCase     :      Union[str, Any]       						=						"""UNwant\u00E9d,running"""

         __lowerCamelCase     :      Tuple       						=						tokenizer.tokenize(_lowerCamelCase							)
         __lowerCamelCase     :      Optional[int]       						=						rust_tokenizer.tokenize(_lowerCamelCase							)
         self.assertListEqual(_lowerCamelCase       ,  _lowerCamelCase							)

         __lowerCamelCase     :      List[Any]       						=						tokenizer.encode(_lowerCamelCase       ,  add_special_tokens=_lowerCamelCase							)
         __lowerCamelCase     :      Union[str, Any]       						=						rust_tokenizer.encode(_lowerCamelCase       ,  add_special_tokens=_lowerCamelCase							)
         self.assertListEqual(_lowerCamelCase       ,  _lowerCamelCase							)

         __lowerCamelCase     :      List[str]       						=						self.get_rust_tokenizer()
         __lowerCamelCase     :      str       						=						tokenizer.encode(_lowerCamelCase							)
         __lowerCamelCase     :      List[Any]       						=						rust_tokenizer.encode(_lowerCamelCase							)
         self.assertListEqual(_lowerCamelCase       ,  _lowerCamelCase							)

   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         __lowerCamelCase     :      List[str]       						=						BasicTokenizer()

         self.assertListEqual(tokenizer.tokenize("""ah\u535A\u63A8zz"""							)       ,  ["""ah""", """\u535A""", """\u63A8""", """zz"""]							)

   def   _snake_case    (  self  : Any							):






         '''simple docstring'''



         __lowerCamelCase     :      Optional[int]       						=						BasicTokenizer(do_lower_case=_lowerCamelCase							)

         self.assertListEqual(
             tokenizer.tokenize(""" \tHeLLo!how  \n Are yoU?  """							)       ,  ["""hello""", """!""", """how""", """are""", """you""", """?"""]							)
         self.assertListEqual(tokenizer.tokenize("""H\u00E9llo"""							)       ,  ["""hello"""]							)

   def   _snake_case    (  self  : Dict							):






         '''simple docstring'''



         __lowerCamelCase     :      int       						=						BasicTokenizer(do_lower_case=_lowerCamelCase       ,  strip_accents=_lowerCamelCase							)

         self.assertListEqual(
             tokenizer.tokenize(""" \tHäLLo!how  \n Are yoU?  """							)       ,  ["""hällo""", """!""", """how""", """are""", """you""", """?"""]							)
         self.assertListEqual(tokenizer.tokenize("""H\u00E9llo"""							)       ,  ["""h\u00E9llo"""]							)

   def   _snake_case    (  self  : Optional[int]							):






         '''simple docstring'''



         __lowerCamelCase     :      List[str]       						=						BasicTokenizer(do_lower_case=_lowerCamelCase       ,  strip_accents=_lowerCamelCase							)

         self.assertListEqual(
             tokenizer.tokenize(""" \tHäLLo!how  \n Are yoU?  """							)       ,  ["""hallo""", """!""", """how""", """are""", """you""", """?"""]							)
         self.assertListEqual(tokenizer.tokenize("""H\u00E9llo"""							)       ,  ["""hello"""]							)

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






         '''simple docstring'''



         __lowerCamelCase     :      Tuple       						=						BasicTokenizer(do_lower_case=_lowerCamelCase							)

         self.assertListEqual(
             tokenizer.tokenize(""" \tHäLLo!how  \n Are yoU?  """							)       ,  ["""hallo""", """!""", """how""", """are""", """you""", """?"""]							)
         self.assertListEqual(tokenizer.tokenize("""H\u00E9llo"""							)       ,  ["""hello"""]							)

   def   _snake_case    (  self  : Any							):






         '''simple docstring'''



         __lowerCamelCase     :      int       						=						BasicTokenizer(do_lower_case=_lowerCamelCase							)

         self.assertListEqual(
             tokenizer.tokenize(""" \tHeLLo!how  \n Are yoU?  """							)       ,  ["""HeLLo""", """!""", """how""", """Are""", """yoU""", """?"""]							)

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






         '''simple docstring'''



         __lowerCamelCase     :      int       						=						BasicTokenizer(do_lower_case=_lowerCamelCase       ,  strip_accents=_lowerCamelCase							)

         self.assertListEqual(
             tokenizer.tokenize(""" \tHäLLo!how  \n Are yoU?  """							)       ,  ["""HäLLo""", """!""", """how""", """Are""", """yoU""", """?"""]							)

   def   _snake_case    (  self  : int							):






         '''simple docstring'''



         __lowerCamelCase     :      Dict       						=						BasicTokenizer(do_lower_case=_lowerCamelCase       ,  strip_accents=_lowerCamelCase							)

         self.assertListEqual(
             tokenizer.tokenize(""" \tHäLLo!how  \n Are yoU?  """							)       ,  ["""HaLLo""", """!""", """how""", """Are""", """yoU""", """?"""]							)

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






         '''simple docstring'''



         __lowerCamelCase     :      Dict       						=						BasicTokenizer(do_lower_case=_lowerCamelCase       ,  never_split=["""[UNK]"""]							)

         self.assertListEqual(
             tokenizer.tokenize(""" \tHeLLo!how  \n Are yoU? [UNK]"""							)       ,  ["""HeLLo""", """!""", """how""", """Are""", """yoU""", """?""", """[UNK]"""]							)

   def   _snake_case    (  self  : Tuple							):






         '''simple docstring'''



         __lowerCamelCase     :      Any       						=						["""[UNK]""", """[CLS]""", """[SEP]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing"""]

         __lowerCamelCase     :      Union[str, Any]       						=						{}
         for i, token in enumerate(_lowerCamelCase							):
               __lowerCamelCase     :      Optional[int]       						=						i
         __lowerCamelCase     :      int       						=						WordpieceTokenizer(vocab=_lowerCamelCase       ,  unk_token="""[UNK]"""							)

         self.assertListEqual(tokenizer.tokenize(""""""							)       ,  []							)

         self.assertListEqual(tokenizer.tokenize("""unwanted running"""							)       ,  ["""un""", """##want""", """##ed""", """runn""", """##ing"""]							)

         self.assertListEqual(tokenizer.tokenize("""unwantedX running"""							)       ,  ["""[UNK]""", """runn""", """##ing"""]							)

   def   _snake_case    (  self  : Any							):






         '''simple docstring'''



         self.assertTrue(_is_whitespace(""" """							)							)
         self.assertTrue(_is_whitespace("""\t"""							)							)
         self.assertTrue(_is_whitespace("""\r"""							)							)
         self.assertTrue(_is_whitespace("""\n"""							)							)
         self.assertTrue(_is_whitespace("""\u00A0"""							)							)

         self.assertFalse(_is_whitespace("""A"""							)							)
         self.assertFalse(_is_whitespace("""-"""							)							)

   def   _snake_case    (  self  : List[Any]							):






         '''simple docstring'''



         self.assertTrue(_is_control("""\u0005"""							)							)

         self.assertFalse(_is_control("""A"""							)							)
         self.assertFalse(_is_control(""" """							)							)
         self.assertFalse(_is_control("""\t"""							)							)
         self.assertFalse(_is_control("""\r"""							)							)

   def   _snake_case    (  self  : Optional[int]							):






         '''simple docstring'''



         self.assertTrue(_is_punctuation("""-"""							)							)
         self.assertTrue(_is_punctuation("""$"""							)							)
         self.assertTrue(_is_punctuation("""`"""							)							)
         self.assertTrue(_is_punctuation("""."""							)							)

         self.assertFalse(_is_punctuation("""A"""							)							)
         self.assertFalse(_is_punctuation(""" """							)							)

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






         '''simple docstring'''



         __lowerCamelCase     :      Optional[Any]       						=						self.get_tokenizer()
         __lowerCamelCase     :      List[str]       						=						self.get_rust_tokenizer()

         # Example taken from the issue https://github.com/huggingface/tokenizers/issues/340
         self.assertListEqual([tokenizer.tokenize(_lowerCamelCase							) for t in ["""Test""", """\xad""", """test"""]]       ,  [["""[UNK]"""], [], ["""[UNK]"""]]							)

         self.assertListEqual(
             [rust_tokenizer.tokenize(_lowerCamelCase							) for t in ["""Test""", """\xad""", """test"""]]       ,  [["""[UNK]"""], [], ["""[UNK]"""]]							)

   @slow
   def   _snake_case    (  self  : Optional[int]							):






         '''simple docstring'''



         __lowerCamelCase     :      List[str]       						=						self.tokenizer_class.from_pretrained("""google/mobilebert-uncased"""							)

         __lowerCamelCase     :      str       						=						tokenizer.encode("""sequence builders"""       ,  add_special_tokens=_lowerCamelCase							)
         __lowerCamelCase     :      Optional[Any]       						=						tokenizer.encode("""multi-sequence build"""       ,  add_special_tokens=_lowerCamelCase							)

         __lowerCamelCase     :      List[Any]       						=						tokenizer.build_inputs_with_special_tokens(_lowerCamelCase							)
         __lowerCamelCase     :      List[Any]       						=						tokenizer.build_inputs_with_special_tokens(_lowerCamelCase       ,  _lowerCamelCase							)

         assert encoded_sentence == [1_0_1] + text + [1_0_2]
         assert encoded_pair == [1_0_1] + text + [1_0_2] + text_a + [1_0_2]

   def   _snake_case    (  self  : Tuple							):






         '''simple docstring'''



         for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
               with self.subTest(F"""{tokenizer.__class__.__name__} ({pretrained_name})"""							):
                     __lowerCamelCase     :      Any       						=						self.rust_tokenizer_class.from_pretrained(_lowerCamelCase       ,  **_lowerCamelCase							)

                     __lowerCamelCase     :      List[str]       						=						F"""A, naïve {tokenizer_r.mask_token} AllenNLP sentence."""
                     __lowerCamelCase     :      Any       						=						tokenizer_r.encode_plus(
                         _lowerCamelCase       ,  return_attention_mask=_lowerCamelCase       ,  return_token_type_ids=_lowerCamelCase       ,  return_offsets_mapping=_lowerCamelCase       ,  add_special_tokens=_lowerCamelCase       ,  )

                     __lowerCamelCase     :      List[Any]       						=						tokenizer_r.do_lower_case if hasattr(_lowerCamelCase       ,  """do_lower_case"""							) else False
                     __lowerCamelCase     :      Any       						=						(
                         [
                             ((0, 0), tokenizer_r.cls_token),
                             ((0, 1), """A"""),
                             ((1, 2), ""","""),
                             ((3, 5), """na"""),
                             ((5, 6), """##ï"""),
                             ((6, 8), """##ve"""),
                             ((9, 1_5), tokenizer_r.mask_token),
                             ((1_6, 2_1), """Allen"""),
                             ((2_1, 2_3), """##NL"""),
                             ((2_3, 2_4), """##P"""),
                             ((2_5, 3_3), """sentence"""),
                             ((3_3, 3_4), """."""),
                             ((0, 0), tokenizer_r.sep_token),
                         ]
                         if not do_lower_case
                         else [
                             ((0, 0), tokenizer_r.cls_token),
                             ((0, 1), """a"""),
                             ((1, 2), ""","""),
                             ((3, 8), """naive"""),
                             ((9, 1_5), tokenizer_r.mask_token),
                             ((1_6, 2_1), """allen"""),
                             ((2_1, 2_3), """##nl"""),
                             ((2_3, 2_4), """##p"""),
                             ((2_5, 3_3), """sentence"""),
                             ((3_3, 3_4), """."""),
                             ((0, 0), tokenizer_r.sep_token),
                         ]
                     )

                     self.assertEqual(
                         [e[1] for e in expected_results]       ,  tokenizer_r.convert_ids_to_tokens(tokens["""input_ids"""]							)							)
                     self.assertEqual([e[0] for e in expected_results]       ,  tokens["""offset_mapping"""]							)


   def   _snake_case    (  self  : Any							):






         '''simple docstring'''



         __lowerCamelCase     :      Dict       						=						["""的""", """人""", """有"""]
         __lowerCamelCase     :      str       						=						"""""".join(_lowerCamelCase							)
         for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
               with self.subTest(F"""{tokenizer.__class__.__name__} ({pretrained_name})"""							):
                     __lowerCamelCase     :      List[str]       						=						True
                     __lowerCamelCase     :      Tuple       						=						self.tokenizer_class.from_pretrained(_lowerCamelCase       ,  **_lowerCamelCase							)
                     __lowerCamelCase     :      Any       						=						self.rust_tokenizer_class.from_pretrained(_lowerCamelCase       ,  **_lowerCamelCase							)

                     __lowerCamelCase     :      Optional[Any]       						=						tokenizer_p.encode(_lowerCamelCase       ,  add_special_tokens=_lowerCamelCase							)
                     __lowerCamelCase     :      List[Any]       						=						tokenizer_r.encode(_lowerCamelCase       ,  add_special_tokens=_lowerCamelCase							)

                     __lowerCamelCase     :      Union[str, Any]       						=						tokenizer_r.convert_ids_to_tokens(_lowerCamelCase							)
                     __lowerCamelCase     :      int       						=						tokenizer_p.convert_ids_to_tokens(_lowerCamelCase							)

                     # it is expected that each Chinese character is not preceded by "##"
                     self.assertListEqual(_lowerCamelCase       ,  _lowerCamelCase							)
                     self.assertListEqual(_lowerCamelCase       ,  _lowerCamelCase							)

                     __lowerCamelCase     :      Union[str, Any]       						=						False
                     __lowerCamelCase     :      Any       						=						self.rust_tokenizer_class.from_pretrained(_lowerCamelCase       ,  **_lowerCamelCase							)
                     __lowerCamelCase     :      List[str]       						=						self.tokenizer_class.from_pretrained(_lowerCamelCase       ,  **_lowerCamelCase							)

                     __lowerCamelCase     :      Optional[int]       						=						tokenizer_r.encode(_lowerCamelCase       ,  add_special_tokens=_lowerCamelCase							)
                     __lowerCamelCase     :      str       						=						tokenizer_p.encode(_lowerCamelCase       ,  add_special_tokens=_lowerCamelCase							)

                     __lowerCamelCase     :      Optional[int]       						=						tokenizer_r.convert_ids_to_tokens(_lowerCamelCase							)
                     __lowerCamelCase     :      Optional[Any]       						=						tokenizer_p.convert_ids_to_tokens(_lowerCamelCase							)

                     # it is expected that only the first Chinese character is not preceded by "##".
                     __lowerCamelCase     :      Union[str, Any]       						=						[
                         F"""##{token}""" if idx != 0 else token for idx, token in enumerate(_lowerCamelCase							)
                     ]
                     self.assertListEqual(_lowerCamelCase       ,  _lowerCamelCase							)
                     self.assertListEqual(_lowerCamelCase       ,  _lowerCamelCase							)







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							)



