


import unittest

import numpy as np

from transformers import is_flax_available
from transformers.testing_utils import require_flax

from ..test_modeling_flax_common import ids_tensor


if is_flax_available():
    import jax
    import jax.numpy as jnp

    from transformers.generation import (
        FlaxForcedBOSTokenLogitsProcessor,
        FlaxForcedEOSTokenLogitsProcessor,
        FlaxLogitsProcessorList,
        FlaxMinLengthLogitsProcessor,
        FlaxTemperatureLogitsWarper,
        FlaxTopKLogitsWarper,
        FlaxTopPLogitsWarper,
    )



@require_flax
class 							_UpperCamelCase (						unittest.TestCase						):

   '''simple docstring'''







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






         '''simple docstring'''



         __lowerCamelCase     :      Any       						=						jnp.ones((batch_size, length)							) / length
         return scores

   def   _snake_case    (  self  : int							):






         '''simple docstring'''



         __lowerCamelCase     :      Union[str, Any]       						=						None
         __lowerCamelCase     :      List[str]       						=						2_0

         __lowerCamelCase     :      List[str]       						=						self._get_uniform_logits(batch_size=2       ,  length=_lowerCamelCase							)

         # tweak scores to not be uniform anymore
         __lowerCamelCase     :      Optional[Any]       						=						scores.at[1, 5].set((1 / length) + 0.1							)  # peak, 1st batch
         __lowerCamelCase     :      Optional[Any]       						=						scores.at[1, 1_0].set((1 / length) - 0.4							)  # valley, 1st batch

         # compute softmax
         __lowerCamelCase     :      Dict       						=						jax.nn.softmax(_lowerCamelCase       ,  axis=-1							)

         __lowerCamelCase     :      Union[str, Any]       						=						FlaxTemperatureLogitsWarper(temperature=0.5							)
         __lowerCamelCase     :      Tuple       						=						FlaxTemperatureLogitsWarper(temperature=1.3							)

         __lowerCamelCase     :      Dict       						=						jax.nn.softmax(temp_dist_warper_sharper(_lowerCamelCase       ,  scores.copy()       ,  cur_len=_lowerCamelCase							)       ,  axis=-1							)
         __lowerCamelCase     :      Optional[Any]       						=						jax.nn.softmax(temp_dist_warper_smoother(_lowerCamelCase       ,  scores.copy()       ,  cur_len=_lowerCamelCase							)       ,  axis=-1							)

         # uniform distribution stays uniform
         self.assertTrue(jnp.allclose(probs[0, :]       ,  warped_prob_sharp[0, :]       ,  atol=1E-3							)							)
         self.assertTrue(jnp.allclose(probs[0, :]       ,  warped_prob_smooth[0, :]       ,  atol=1E-3							)							)

         # sharp peaks get higher, valleys get lower
         self.assertLess(probs[1, :].max()       ,  warped_prob_sharp[1, :].max()							)
         self.assertGreater(probs[1, :].min()       ,  warped_prob_sharp[1, :].min()							)

         # smooth peaks get lower, valleys get higher
         self.assertGreater(probs[1, :].max()       ,  warped_prob_smooth[1, :].max()							)
         self.assertLess(probs[1, :].min()       ,  warped_prob_smooth[1, :].min()							)

   def   _snake_case    (  self  : Optional[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      Union[str, Any]       						=						None
         __lowerCamelCase     :      Tuple       						=						1_0
         __lowerCamelCase     :      Optional[int]       						=						2

         # create ramp distribution
         __lowerCamelCase     :      int       						=						np.broadcast_to(np.arange(_lowerCamelCase							)[None, :]       ,  (batch_size, vocab_size)							).copy()
         __lowerCamelCase     :      List[str]       						=						ramp_logits[1:, : vocab_size // 2] + vocab_size

         __lowerCamelCase     :      str       						=						FlaxTopKLogitsWarper(3							)

         __lowerCamelCase     :      Union[str, Any]       						=						top_k_warp(_lowerCamelCase       ,  _lowerCamelCase       ,  cur_len=_lowerCamelCase							)

         # check that correct tokens are filtered
         self.assertListEqual(jnp.isinf(scores[0]							).tolist()       ,  7 * [True] + 3 * [False]							)
         self.assertListEqual(jnp.isinf(scores[1]							).tolist()       ,  2 * [True] + 3 * [False] + 5 * [True]							)

         # check special case
         __lowerCamelCase     :      List[Any]       						=						5
         __lowerCamelCase     :      str       						=						FlaxTopKLogitsWarper(top_k=1       ,  filter_value=0.0       ,  min_tokens_to_keep=3							)

         __lowerCamelCase     :      Optional[Any]       						=						np.broadcast_to(np.arange(_lowerCamelCase							)[None, :]       ,  (batch_size, length)							).copy()
         __lowerCamelCase     :      List[str]       						=						top_k_warp_safety_check(_lowerCamelCase       ,  _lowerCamelCase       ,  cur_len=_lowerCamelCase							)

         # min_tokens overwrites k: 3 tokens are kept => 2 tokens are nullified
         self.assertListEqual((scores == 0.0).sum(axis=-1							).tolist()       ,  [2, 2]							)

   def   _snake_case    (  self  : Optional[int]							):






         '''simple docstring'''



         __lowerCamelCase     :      Optional[int]       						=						None
         __lowerCamelCase     :      str       						=						1_0
         __lowerCamelCase     :      str       						=						2

         # create distribution and take log (inverse to Softmax as taken in TopPLogitsWarper)
         __lowerCamelCase     :      Dict       						=						np.log(np.array([[0.3, 0.1, 0.1, 0.5], [0.15, 0.3, 0.3, 0.25]]							)							)

         __lowerCamelCase     :      int       						=						FlaxTopPLogitsWarper(0.8							)
         __lowerCamelCase     :      Tuple       						=						np.exp(top_p_warp(_lowerCamelCase       ,  _lowerCamelCase       ,  cur_len=_lowerCamelCase							)							)

         # dist should be filtered to keep min num values so that sum is >= top_p
         # exp (-inf) => 0
         __lowerCamelCase     :      int       						=						np.array([[0.3, 0.0, 0.0, 0.5], [0.0, 0.3, 0.3, 0.25]]							)
         self.assertTrue(np.allclose(_lowerCamelCase       ,  _lowerCamelCase       ,  atol=1E-3							)							)

         # check edge cases with negative and extreme logits
         __lowerCamelCase     :      List[str]       						=						np.broadcast_to(np.arange(_lowerCamelCase							)[None, :]       ,  (batch_size, vocab_size)							).copy() - (
             vocab_size // 2
         )

         # make ramp_logits more extreme
         __lowerCamelCase     :      List[str]       						=						ramp_logits[1] * 100.0

         # make sure at least 2 tokens are kept
         __lowerCamelCase     :      int       						=						FlaxTopPLogitsWarper(0.9       ,  min_tokens_to_keep=2       ,  filter_value=0.0							)
         __lowerCamelCase     :      Any       						=						top_p_warp(_lowerCamelCase       ,  _lowerCamelCase       ,  cur_len=_lowerCamelCase							)

         # first batch should keep three tokens, second batch would keep only 1, but due to `min_tokens_to_keep=2` keeps 2.
         self.assertListEqual((filtered_dist != 0.0).sum(axis=-1							).tolist()       ,  [3, 2]							)

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






         '''simple docstring'''



         __lowerCamelCase     :      int       						=						2_0
         __lowerCamelCase     :      str       						=						4
         __lowerCamelCase     :      str       						=						0

         __lowerCamelCase     :      int       						=						FlaxMinLengthLogitsProcessor(min_length=1_0       ,  eos_token_id=_lowerCamelCase							)

         # check that min length is applied at length 5
         __lowerCamelCase     :      Union[str, Any]       						=						ids_tensor((batch_size, 2_0)       ,  vocab_size=2_0							)
         __lowerCamelCase     :      Dict       						=						5
         __lowerCamelCase     :      Any       						=						self._get_uniform_logits(_lowerCamelCase       ,  _lowerCamelCase							)
         __lowerCamelCase     :      Optional[Any]       						=						min_dist_processor(_lowerCamelCase       ,  _lowerCamelCase       ,  cur_len=_lowerCamelCase							)
         self.assertListEqual(scores_before_min_length[:, eos_token_id].tolist()       ,  4 * [-float("""inf"""							)]							)

         # check that min length is not applied anymore at length 15
         __lowerCamelCase     :      Optional[int]       						=						self._get_uniform_logits(_lowerCamelCase       ,  _lowerCamelCase							)
         __lowerCamelCase     :      Dict       						=						1_5
         __lowerCamelCase     :      int       						=						min_dist_processor(_lowerCamelCase       ,  _lowerCamelCase       ,  cur_len=_lowerCamelCase							)
         self.assertFalse(jnp.isinf(_lowerCamelCase							).any()							)

   def   _snake_case    (  self  : Any							):






         '''simple docstring'''



         __lowerCamelCase     :      Optional[int]       						=						2_0
         __lowerCamelCase     :      List[Any]       						=						4
         __lowerCamelCase     :      str       						=						0

         __lowerCamelCase     :      List[Any]       						=						FlaxForcedBOSTokenLogitsProcessor(bos_token_id=_lowerCamelCase							)

         # check that all scores are -inf except the bos_token_id score
         __lowerCamelCase     :      Union[str, Any]       						=						ids_tensor((batch_size, 1)       ,  vocab_size=2_0							)
         __lowerCamelCase     :      Dict       						=						1
         __lowerCamelCase     :      int       						=						self._get_uniform_logits(_lowerCamelCase       ,  _lowerCamelCase							)
         __lowerCamelCase     :      int       						=						logits_processor(_lowerCamelCase       ,  _lowerCamelCase       ,  cur_len=_lowerCamelCase							)
         self.assertTrue(jnp.isneginf(scores[:, bos_token_id + 1 :]							).all()							)
         self.assertListEqual(scores[:, bos_token_id].tolist()       ,  4 * [0]							)  # score for bos_token_id shold be zero

         # check that bos_token_id is not forced if current length is greater than 1
         __lowerCamelCase     :      List[str]       						=						3
         __lowerCamelCase     :      Optional[Any]       						=						self._get_uniform_logits(_lowerCamelCase       ,  _lowerCamelCase							)
         __lowerCamelCase     :      List[Any]       						=						logits_processor(_lowerCamelCase       ,  _lowerCamelCase       ,  cur_len=_lowerCamelCase							)
         self.assertFalse(jnp.isinf(_lowerCamelCase							).any()							)

   def   _snake_case    (  self  : int							):






         '''simple docstring'''



         __lowerCamelCase     :      Union[str, Any]       						=						2_0
         __lowerCamelCase     :      Union[str, Any]       						=						4
         __lowerCamelCase     :      int       						=						0
         __lowerCamelCase     :      Optional[int]       						=						5

         __lowerCamelCase     :      Optional[int]       						=						FlaxForcedEOSTokenLogitsProcessor(max_length=_lowerCamelCase       ,  eos_token_id=_lowerCamelCase							)

         # check that all scores are -inf except the eos_token_id when max_length is reached
         __lowerCamelCase     :      Union[str, Any]       						=						ids_tensor((batch_size, 4)       ,  vocab_size=2_0							)
         __lowerCamelCase     :      Optional[int]       						=						4
         __lowerCamelCase     :      Any       						=						self._get_uniform_logits(_lowerCamelCase       ,  _lowerCamelCase							)
         __lowerCamelCase     :      Dict       						=						logits_processor(_lowerCamelCase       ,  _lowerCamelCase       ,  cur_len=_lowerCamelCase							)
         self.assertTrue(jnp.isneginf(scores[:, eos_token_id + 1 :]							).all()							)
         self.assertListEqual(scores[:, eos_token_id].tolist()       ,  4 * [0]							)  # score for eos_token_id should be zero

         # check that eos_token_id is not forced if max_length is not reached
         __lowerCamelCase     :      List[Any]       						=						3
         __lowerCamelCase     :      Any       						=						self._get_uniform_logits(_lowerCamelCase       ,  _lowerCamelCase							)
         __lowerCamelCase     :      List[Any]       						=						logits_processor(_lowerCamelCase       ,  _lowerCamelCase       ,  cur_len=_lowerCamelCase							)
         self.assertFalse(jnp.isinf(_lowerCamelCase							).any()							)

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






         '''simple docstring'''



         __lowerCamelCase     :      List[Any]       						=						4
         __lowerCamelCase     :      List[str]       						=						1_0
         __lowerCamelCase     :      str       						=						1_5
         __lowerCamelCase     :      int       						=						2
         __lowerCamelCase     :      Dict       						=						1
         __lowerCamelCase     :      Dict       						=						1_5

         # dummy input_ids and scores
         __lowerCamelCase     :      Dict       						=						ids_tensor((batch_size, sequence_length)       ,  _lowerCamelCase							)
         __lowerCamelCase     :      Tuple       						=						input_ids.copy()

         __lowerCamelCase     :      List[Any]       						=						self._get_uniform_logits(_lowerCamelCase       ,  _lowerCamelCase							)
         __lowerCamelCase     :      Union[str, Any]       						=						scores.copy()

         # instantiate all dist processors
         __lowerCamelCase     :      Tuple       						=						FlaxTemperatureLogitsWarper(temperature=0.5							)
         __lowerCamelCase     :      str       						=						FlaxTopKLogitsWarper(3							)
         __lowerCamelCase     :      Dict       						=						FlaxTopPLogitsWarper(0.8							)

         # instantiate all logits processors
         __lowerCamelCase     :      Optional[Any]       						=						FlaxMinLengthLogitsProcessor(min_length=1_0       ,  eos_token_id=_lowerCamelCase							)
         __lowerCamelCase     :      Any       						=						FlaxForcedBOSTokenLogitsProcessor(bos_token_id=_lowerCamelCase							)
         __lowerCamelCase     :      List[Any]       						=						FlaxForcedEOSTokenLogitsProcessor(max_length=_lowerCamelCase       ,  eos_token_id=_lowerCamelCase							)

         __lowerCamelCase     :      Optional[int]       						=						1_0

         # no processor list
         __lowerCamelCase     :      Any       						=						temp_dist_warp(_lowerCamelCase       ,  _lowerCamelCase       ,  cur_len=_lowerCamelCase							)
         __lowerCamelCase     :      str       						=						top_k_warp(_lowerCamelCase       ,  _lowerCamelCase       ,  cur_len=_lowerCamelCase							)
         __lowerCamelCase     :      Optional[Any]       						=						top_p_warp(_lowerCamelCase       ,  _lowerCamelCase       ,  cur_len=_lowerCamelCase							)
         __lowerCamelCase     :      Any       						=						min_dist_proc(_lowerCamelCase       ,  _lowerCamelCase       ,  cur_len=_lowerCamelCase							)
         __lowerCamelCase     :      str       						=						bos_dist_proc(_lowerCamelCase       ,  _lowerCamelCase       ,  cur_len=_lowerCamelCase							)
         __lowerCamelCase     :      Dict       						=						eos_dist_proc(_lowerCamelCase       ,  _lowerCamelCase       ,  cur_len=_lowerCamelCase							)

         # with processor list
         __lowerCamelCase     :      Tuple       						=						FlaxLogitsProcessorList(
             [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc]							)
         __lowerCamelCase     :      str       						=						processor(_lowerCamelCase       ,  _lowerCamelCase       ,  cur_len=_lowerCamelCase							)

         # scores should be equal
         self.assertTrue(jnp.allclose(_lowerCamelCase       ,  _lowerCamelCase       ,  atol=1E-3							)							)

         # input_ids should never be changed
         self.assertListEqual(input_ids.tolist()       ,  input_ids_comp.tolist()							)


   def   _snake_case    (  self  : Any							):






         '''simple docstring'''



         __lowerCamelCase     :      Dict       						=						4
         __lowerCamelCase     :      List[Any]       						=						1_0
         __lowerCamelCase     :      Tuple       						=						1_5
         __lowerCamelCase     :      Optional[int]       						=						2
         __lowerCamelCase     :      Optional[Any]       						=						1
         __lowerCamelCase     :      Optional[int]       						=						1_5

         # dummy input_ids and scores
         __lowerCamelCase     :      int       						=						ids_tensor((batch_size, sequence_length)       ,  _lowerCamelCase							)
         __lowerCamelCase     :      Any       						=						input_ids.copy()

         __lowerCamelCase     :      Any       						=						self._get_uniform_logits(_lowerCamelCase       ,  _lowerCamelCase							)
         __lowerCamelCase     :      Union[str, Any]       						=						scores.copy()

         # instantiate all dist processors
         __lowerCamelCase     :      Tuple       						=						FlaxTemperatureLogitsWarper(temperature=0.5							)
         __lowerCamelCase     :      Union[str, Any]       						=						FlaxTopKLogitsWarper(3							)
         __lowerCamelCase     :      Any       						=						FlaxTopPLogitsWarper(0.8							)

         # instantiate all logits processors
         __lowerCamelCase     :      Optional[int]       						=						FlaxMinLengthLogitsProcessor(min_length=1_0       ,  eos_token_id=_lowerCamelCase							)
         __lowerCamelCase     :      int       						=						FlaxForcedBOSTokenLogitsProcessor(bos_token_id=_lowerCamelCase							)
         __lowerCamelCase     :      Tuple       						=						FlaxForcedEOSTokenLogitsProcessor(max_length=_lowerCamelCase       ,  eos_token_id=_lowerCamelCase							)

         __lowerCamelCase     :      List[str]       						=						1_0

         # no processor list
         def run_no_processor_list(_lowerCamelCase  : Optional[Any]       ,  _lowerCamelCase  : List[str]       ,  _lowerCamelCase  : int							):
               __lowerCamelCase     :      str       						=						temp_dist_warp(_lowerCamelCase       ,  _lowerCamelCase       ,  cur_len=_lowerCamelCase							)
               __lowerCamelCase     :      Dict       						=						top_k_warp(_lowerCamelCase       ,  _lowerCamelCase       ,  cur_len=_lowerCamelCase							)
               __lowerCamelCase     :      Any       						=						top_p_warp(_lowerCamelCase       ,  _lowerCamelCase       ,  cur_len=_lowerCamelCase							)
               __lowerCamelCase     :      List[Any]       						=						min_dist_proc(_lowerCamelCase       ,  _lowerCamelCase       ,  cur_len=_lowerCamelCase							)
               __lowerCamelCase     :      Optional[Any]       						=						bos_dist_proc(_lowerCamelCase       ,  _lowerCamelCase       ,  cur_len=_lowerCamelCase							)
               __lowerCamelCase     :      List[Any]       						=						eos_dist_proc(_lowerCamelCase       ,  _lowerCamelCase       ,  cur_len=_lowerCamelCase							)
               return scores

         # with processor list
         def run_processor_list(_lowerCamelCase  : List[Any]       ,  _lowerCamelCase  : List[str]       ,  _lowerCamelCase  : int							):
               __lowerCamelCase     :      Tuple       						=						FlaxLogitsProcessorList(
                   [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc]							)
               __lowerCamelCase     :      Optional[Any]       						=						processor(_lowerCamelCase       ,  _lowerCamelCase       ,  cur_len=_lowerCamelCase							)
               return scores

         __lowerCamelCase     :      int       						=						jax.jit(_lowerCamelCase							)
         __lowerCamelCase     :      Dict       						=						jax.jit(_lowerCamelCase							)

         __lowerCamelCase     :      str       						=						jitted_run_no_processor_list(_lowerCamelCase       ,  _lowerCamelCase       ,  _lowerCamelCase							)
         __lowerCamelCase     :      Optional[Any]       						=						jitted_run_processor_list(_lowerCamelCase       ,  _lowerCamelCase       ,  _lowerCamelCase							)

         # scores should be equal
         self.assertTrue(jnp.allclose(_lowerCamelCase       ,  _lowerCamelCase       ,  atol=1E-3							)							)

         # input_ids should never be changed
         self.assertListEqual(input_ids.tolist()       ,  input_ids_comp.tolist()							)







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							)



