

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 SCREAMING_SNAKE_CASE     :
  """simple docstring"""






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






     """simple docstring"""







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







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






     """simple docstring"""







     __lowerCAmelCase  :      Optional[int]    =						floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]   ).to(
         lowerCAmelCase   )

     __lowerCAmelCase  :      List[str]    =						torch.ones([self.batch_size, self.min_size, self.max_size]	,							device=lowerCAmelCase   )

     __lowerCAmelCase  :      Optional[Any]    =						(
         torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size]	,							device=lowerCAmelCase   ) > 0.5
     ).float()
     __lowerCAmelCase  :      Optional[int]    =						(torch.rand((self.batch_size, self.num_labels)	,							device=lowerCAmelCase   ) > 0.5).long()

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







  def 	SCREAMING_SNAKE_CASE   (      self			:   Dict   )							->     Dict:






     """simple docstring"""







     __lowerCAmelCase  :      Optional[int]    =						MaskaFormerConfig(
         hidden_size=self.hidden_dim	,							)
     __lowerCAmelCase  :      Optional[int]    =						self.num_queries
     __lowerCAmelCase  :      List[str]    =						self.num_labels

     __lowerCAmelCase  :      Tuple    =						[1, 1, 1, 1]
     __lowerCAmelCase  :      Optional[int]    =						self.num_channels

     __lowerCAmelCase  :      int    =						64
     __lowerCAmelCase  :      Optional[Any]    =						1_28
     __lowerCAmelCase  :      List[Any]    =						self.hidden_dim
     __lowerCAmelCase  :      Any    =						self.hidden_dim
     __lowerCAmelCase  :      str    =						self.hidden_dim
     return config







  def 	SCREAMING_SNAKE_CASE   (      self			:   Optional[int]   )							->     int:






     """simple docstring"""







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







  def 	SCREAMING_SNAKE_CASE   (      self			:   Tuple	,							lowerCAmelCase			:   List[str]	,							lowerCAmelCase			:   Optional[Any]   )							->     Tuple:






     """simple docstring"""







     __lowerCAmelCase  :      List[str]    =						output.encoder_hidden_states
     __lowerCAmelCase  :      Any    =						output.pixel_decoder_hidden_states
     __lowerCAmelCase  :      List[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 	SCREAMING_SNAKE_CASE   (      self			:   Tuple	,							lowerCAmelCase			:   List[Any]	,							lowerCAmelCase			:   str	,							lowerCAmelCase			:   Any	,							lowerCAmelCase			:   Dict=False   )							->     Optional[int]:






     """simple docstring"""







     with torch.no_grad():
        __lowerCAmelCase  :      Optional[int]    =						MaskaFormerModel(config=lowerCAmelCase   )
        model.to(lowerCAmelCase   )
        model.eval()

        __lowerCAmelCase  :      Union[str, Any]    =						model(pixel_values=lowerCAmelCase	,							pixel_mask=lowerCAmelCase   )
        __lowerCAmelCase  :      Optional[int]    =						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 	SCREAMING_SNAKE_CASE   (      self			:   str	,							lowerCAmelCase			:   str	,							lowerCAmelCase			:   Tuple	,							lowerCAmelCase			:   Any	,							lowerCAmelCase			:   str	,							lowerCAmelCase			:   Optional[int]   )							->     Any:






     """simple docstring"""







     __lowerCAmelCase  :      Optional[int]    =						MaskaFormerForUniversalSegmentation(config=lowerCAmelCase   )
     model.to(lowerCAmelCase   )
     model.eval()

     def comm_check_on_output(lowerCAmelCase			:   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  :      Dict    =						model(pixel_values=lowerCAmelCase	,							pixel_mask=lowerCAmelCase   )
        __lowerCAmelCase  :      int    =						model(lowerCAmelCase   )

        comm_check_on_output(lowerCAmelCase   )

        __lowerCAmelCase  :      Optional[int]    =						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 SCREAMING_SNAKE_CASE     (  a_       ,							a_       ,							unittest.TestCase			):
  """simple docstring"""






  lowerCamelCase   :      List[Any]				=(MaskaFormerModel, MaskaFormerForUniversalSegmentation) if is_torch_available() else ()
  lowerCamelCase   :      Any				={"feature-extraction": MaskaFormerModel} if is_torch_available() else {}

  lowerCamelCase   :      Optional[Any]				=False
  lowerCamelCase   :      Optional[Any]				=False
  lowerCamelCase   :      Dict				=False
  lowerCamelCase   :      Optional[int]				=False







  def 	SCREAMING_SNAKE_CASE   (      self			:   List[Any]   )							->     Dict:






     """simple docstring"""







     __lowerCAmelCase  :      Optional[int]    =						MaskaFormerModelTester(self   )
     __lowerCAmelCase  :      Union[str, Any]    =						ConfigTester(self	,							config_class=lowerCAmelCase	,							has_text_modality=lowerCAmelCase   )







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






     """simple docstring"""







     self.config_tester.run_common_tests()







  def 	SCREAMING_SNAKE_CASE   (      self			:   Optional[Any]   )							->     str:






     """simple docstring"""







     __lowerCAmelCase  :      Tuple    =						self.model_tester.prepare_config_and_inputs_for_common()
     self.model_tester.create_and_check_maskaformer_model(lowerCAmelCase	,							**lowerCAmelCase	,							output_hidden_states=lowerCAmelCase   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   Dict   )							->     Dict:






     """simple docstring"""







     __lowerCAmelCase  :      Optional[int]    =						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 	SCREAMING_SNAKE_CASE   (      self			:   int   )							->     int:






     """simple docstring"""







     pass







  @unittest.skip(reason="""Mask2Former does not have a get_input_embeddings method"""   )
  def 	SCREAMING_SNAKE_CASE   (      self			:   Union[str, Any]   )							->     Union[str, Any]:






     """simple docstring"""







     pass







  @unittest.skip(reason="""Mask2Former is not a generative model"""   )
  def 	SCREAMING_SNAKE_CASE   (      self			:   List[str]   )							->     Optional[Any]:






     """simple docstring"""







     pass







  @unittest.skip(reason="""Mask2Former does not use token embeddings"""   )
  def 	SCREAMING_SNAKE_CASE   (      self			:   int   )							->     int:






     """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 	SCREAMING_SNAKE_CASE   (      self			:   Optional[Any]   )							->     int:






     """simple docstring"""







     pass







  @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""   )
  def 	SCREAMING_SNAKE_CASE   (      self			:   Dict   )							->     int:






     """simple docstring"""







     pass







  def 	SCREAMING_SNAKE_CASE   (      self			:   Tuple   )							->     str:






     """simple docstring"""







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

     for model_class in self.all_model_classes:
        __lowerCAmelCase  :      Tuple    =						model_class(lowerCAmelCase   )
        __lowerCAmelCase  :      Union[str, Any]    =						inspect.signature(model.forward   )
        # signature.parameters is an OrderedDict => so arg_names order is deterministic
        __lowerCAmelCase  :      Optional[int]    =						[*signature.parameters.keys()]

        __lowerCAmelCase  :      str    =						["""pixel_values"""]
        self.assertListEqual(arg_names[:1]	,							lowerCAmelCase   )







  @slow
  def 	SCREAMING_SNAKE_CASE   (      self			:   int   )							->     List[str]:






     """simple docstring"""







     for model_name in ["facebook/mask2former-swin-small-coco-instance"]:
        __lowerCAmelCase  :      List[str]    =						MaskaFormerModel.from_pretrained(lowerCAmelCase   )
        self.assertIsNotNone(lowerCAmelCase   )







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






     """simple docstring"""







     __lowerCAmelCase  :      Tuple    =						(self.model_tester.min_size,) * 2
     __lowerCAmelCase  :      List[Any]    =						{
         """pixel_values""": torch.randn((2, 3, *size)	,							device=lowerCAmelCase   ),
         """mask_labels""": torch.randn((2, 10, *size)	,							device=lowerCAmelCase   ),
         """class_labels""": torch.zeros(2	,							10	,							device=lowerCAmelCase   ).long(),
     }
     __lowerCAmelCase  :      Dict    =						self.model_tester.get_config()

     __lowerCAmelCase  :      Dict    =						MaskaFormerForUniversalSegmentation(lowerCAmelCase   ).to(lowerCAmelCase   )
     __lowerCAmelCase  :      List[Any]    =						model(**lowerCAmelCase   )
     self.assertTrue(outputs.loss is not None   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   str   )							->     Optional[Any]:






     """simple docstring"""







     __lowerCAmelCase  :      Dict    =						self.model_tester.prepare_config_and_inputs_for_common()
     self.model_tester.create_and_check_maskaformer_model(lowerCAmelCase	,							**lowerCAmelCase	,							output_hidden_states=lowerCAmelCase   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   int   )							->     List[Any]:






     """simple docstring"""







     __lowerCAmelCase  :      Dict    =						self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
        __lowerCAmelCase  :      Any    =						model_class(lowerCAmelCase   ).to(lowerCAmelCase   )
        __lowerCAmelCase  :      Optional[Any]    =						model(**lowerCAmelCase	,							output_attentions=lowerCAmelCase   )
        self.assertTrue(outputs.attentions is not None   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   Optional[Any]   )							->     Any:






     """simple docstring"""







     if not self.model_tester.is_training:
        return

     __lowerCAmelCase  :      Optional[int]    =						self.all_model_classes[1]
     __lowerCAmelCase  :      Dict    =						self.model_tester.prepare_config_and_inputs()

     __lowerCAmelCase  :      int    =						model_class(lowerCAmelCase   )
     model.to(lowerCAmelCase   )
     model.train()

     __lowerCAmelCase  :      List[str]    =						model(lowerCAmelCase	,							mask_labels=lowerCAmelCase	,							class_labels=lowerCAmelCase   ).loss
     loss.backward()







  def 	SCREAMING_SNAKE_CASE   (      self			:   Any   )							->     List[Any]:






     """simple docstring"""







     __lowerCAmelCase  :      List[str]    =						self.all_model_classes[1]
     __lowerCAmelCase  :      Optional[int]    =						self.model_tester.prepare_config_and_inputs()
     __lowerCAmelCase  :      Union[str, Any]    =						True
     __lowerCAmelCase  :      Dict    =						True

     __lowerCAmelCase  :      int    =						model_class(lowerCAmelCase   ).to(lowerCAmelCase   )
     model.train()

     __lowerCAmelCase  :      Tuple    =						model(lowerCAmelCase	,							mask_labels=lowerCAmelCase	,							class_labels=lowerCAmelCase   )

     __lowerCAmelCase  :      Optional[int]    =						outputs.encoder_hidden_states[0]
     encoder_hidden_states.retain_grad()

     __lowerCAmelCase  :      Optional[int]    =						outputs.pixel_decoder_hidden_states[0]
     pixel_decoder_hidden_states.retain_grad()

     __lowerCAmelCase  :      Tuple    =						outputs.transformer_decoder_hidden_states[0]
     transformer_decoder_hidden_states.retain_grad()

     __lowerCAmelCase  :      Dict    =						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								=			1e-4





def      snake_case_				()      ->     Optional[int]:
   __lowerCAmelCase  :      str    =						Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""					)
   return image



@require_vision
@slow
class SCREAMING_SNAKE_CASE     (  unittest.TestCase			):
  """simple docstring"""






  @cached_property
  def 	SCREAMING_SNAKE_CASE   (      self			:   Any   )							->     Any:






     """simple docstring"""







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







  @cached_property
  def 	SCREAMING_SNAKE_CASE   (      self			:   int   )							->     str:






     """simple docstring"""







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







  def 	SCREAMING_SNAKE_CASE   (      self			:   Tuple   )							->     Optional[int]:






     """simple docstring"""







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

     with torch.no_grad():
        __lowerCAmelCase  :      List[str]    =						model(**lowerCAmelCase   )

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

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

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







  def 	SCREAMING_SNAKE_CASE   (      self			:   Tuple   )							->     Optional[int]:






     """simple docstring"""







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

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







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






     """simple docstring"""







     __lowerCAmelCase  :      Any    =						MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints   ).to(lowerCAmelCase   ).eval()
     __lowerCAmelCase  :      str    =						self.default_image_processor

     __lowerCAmelCase  :      str    =						image_processor(
         [np.zeros((3, 8_00, 13_33)   ), np.zeros((3, 8_00, 13_33)   )]	,							segmentation_maps=[np.zeros((3_84, 3_84)   ).astype(np.floataa   ), np.zeros((3_84, 3_84)   ).astype(np.floataa   )]	,							return_tensors="""pt"""	,							)

     __lowerCAmelCase  :      List[str]    =						inputs["""pixel_values"""].to(lowerCAmelCase   )
     __lowerCAmelCase  :      Dict    =						[el.to(lowerCAmelCase   ) for el in inputs["""mask_labels"""]]
     __lowerCAmelCase  :      Optional[int]    =						[el.to(lowerCAmelCase   ) for el in inputs["""class_labels"""]]

     with torch.no_grad():
        __lowerCAmelCase  :      Any    =						model(**lowerCAmelCase   )

     self.assertTrue(outputs.loss is not None   )





from __future__ import annotations

import unittest

from transformers import AutoTokenizer, MBartConfig, is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow
from transformers.utils import cached_property

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


if is_tf_available():
   import tensorflow as tf

   from transformers import TFAutoModelForSeqaSeqLM, TFMBartForConditionalGeneration, TFMBartModel



@require_tf
class SCREAMING_SNAKE_CASE     :
  """simple docstring"""






  lowerCamelCase   :      Union[str, Any]										=MBartConfig
  lowerCamelCase   :      Optional[Any]										={}
  lowerCamelCase   :      Dict										="gelu"







  def __init__(      self			:   str	,							lowerCAmelCase			:   Any	,							lowerCAmelCase			:   List[Any]=13	,							lowerCAmelCase			:   List[str]=7	,							lowerCAmelCase			:   List[str]=True	,							lowerCAmelCase			:   Optional[Any]=False	,							lowerCAmelCase			:   Union[str, Any]=99	,							lowerCAmelCase			:   List[str]=32	,							lowerCAmelCase			:   List[Any]=2	,							lowerCAmelCase			:   Tuple=4	,							lowerCAmelCase			:   Any=37	,							lowerCAmelCase			:   Optional[int]=0.1	,							lowerCAmelCase			:   List[Any]=0.1	,							lowerCAmelCase			:   Dict=20	,							lowerCAmelCase			:   Any=2	,							lowerCAmelCase			:   Union[str, Any]=1	,							lowerCAmelCase			:   str=0	,							)							->     Any:






     """simple docstring"""







     __lowerCAmelCase  :      str    =						parent
     __lowerCAmelCase  :      int    =						batch_size
     __lowerCAmelCase  :      int    =						seq_length
     __lowerCAmelCase  :      Tuple    =						is_training
     __lowerCAmelCase  :      Optional[int]    =						use_labels
     __lowerCAmelCase  :      str    =						vocab_size
     __lowerCAmelCase  :      List[str]    =						hidden_size
     __lowerCAmelCase  :      Dict    =						num_hidden_layers
     __lowerCAmelCase  :      int    =						num_attention_heads
     __lowerCAmelCase  :      Any    =						intermediate_size
     __lowerCAmelCase  :      Dict    =						hidden_dropout_prob
     __lowerCAmelCase  :      List[Any]    =						attention_probs_dropout_prob
     __lowerCAmelCase  :      Tuple    =						max_position_embeddings
     __lowerCAmelCase  :      Union[str, Any]    =						eos_token_id
     __lowerCAmelCase  :      Optional[Any]    =						pad_token_id
     __lowerCAmelCase  :      int    =						bos_token_id







  def 	SCREAMING_SNAKE_CASE   (      self			:   List[Any]   )							->     List[Any]:






     """simple docstring"""







     __lowerCAmelCase  :      List[Any]    =						ids_tensor([self.batch_size, self.seq_length - 1]	,							self.vocab_size   )
     __lowerCAmelCase  :      Optional[Any]    =						tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size   )	,							1   )
     __lowerCAmelCase  :      int    =						tf.concat([input_ids, eos_tensor]	,							axis=1   )

     __lowerCAmelCase  :      Any    =						ids_tensor([self.batch_size, self.seq_length]	,							self.vocab_size   )

     __lowerCAmelCase  :      Dict    =						self.config_cls(
         vocab_size=self.vocab_size	,							d_model=self.hidden_size	,							encoder_layers=self.num_hidden_layers	,							decoder_layers=self.num_hidden_layers	,							encoder_attention_heads=self.num_attention_heads	,							decoder_attention_heads=self.num_attention_heads	,							encoder_ffn_dim=self.intermediate_size	,							decoder_ffn_dim=self.intermediate_size	,							dropout=self.hidden_dropout_prob	,							attention_dropout=self.attention_probs_dropout_prob	,							max_position_embeddings=self.max_position_embeddings	,							eos_token_ids=[2]	,							bos_token_id=self.bos_token_id	,							pad_token_id=self.pad_token_id	,							decoder_start_token_id=self.pad_token_id	,							**self.config_updates	,							)
     __lowerCAmelCase  :      Tuple    =						prepare_mbart_inputs_dict(lowerCAmelCase	,							lowerCAmelCase	,							lowerCAmelCase   )
     return config, inputs_dict







  def 	SCREAMING_SNAKE_CASE   (      self			:   List[str]	,							lowerCAmelCase			:   Any	,							lowerCAmelCase			:   str   )							->     Optional[Any]:






     """simple docstring"""







     __lowerCAmelCase  :      List[str]    =						TFMBartModel(config=lowerCAmelCase   ).get_decoder()
     __lowerCAmelCase  :      Tuple    =						inputs_dict["""input_ids"""]

     __lowerCAmelCase  :      Optional[Any]    =						input_ids[:1, :]
     __lowerCAmelCase  :      Union[str, Any]    =						inputs_dict["""attention_mask"""][:1, :]
     __lowerCAmelCase  :      Tuple    =						inputs_dict["""head_mask"""]
     __lowerCAmelCase  :      Any    =						1

     # first forward pass
     __lowerCAmelCase  :      List[Any]    =						model(lowerCAmelCase	,							attention_mask=lowerCAmelCase	,							head_mask=lowerCAmelCase	,							use_cache=lowerCAmelCase   )

     __lowerCAmelCase   ,__lowerCAmelCase  :      List[str]    =						outputs.to_tuple()
     __lowerCAmelCase  :      Union[str, Any]    =						past_key_values[1]






def      snake_case_				(__A					:						str ,       __A					:						Union[str, Any] ,       __A					:						Tuple ,       __A					:						Tuple=None ,       __A					:						Optional[Any]=None ,       __A					:						Optional[Any]=None ,       __A					:						Optional[int]=None ,       __A					:						Optional[Any]=None ,       )      ->     int:
   if attention_mask is None:
      __lowerCAmelCase  :      Dict    =						tf.cast(tf.math.not_equal(__A ,       config.pad_token_id					) ,       tf.inta					)
   if decoder_attention_mask is None:
      __lowerCAmelCase  :      str    =						tf.concat(
          [
              tf.ones(decoder_input_ids[:, :1].shape ,       dtype=tf.inta					),
              tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] ,       config.pad_token_id					) ,       tf.inta					),
          ] ,       axis=-1 ,       )
   if head_mask is None:
      __lowerCAmelCase  :      Tuple    =						tf.ones((config.encoder_layers, config.encoder_attention_heads)					)
   if decoder_head_mask is None:
      __lowerCAmelCase  :      List[Any]    =						tf.ones((config.decoder_layers, config.decoder_attention_heads)					)
   if cross_attn_head_mask is None:
      __lowerCAmelCase  :      Any    =						tf.ones((config.decoder_layers, config.decoder_attention_heads)					)
   return {
       "input_ids": input_ids,
       "decoder_input_ids": decoder_input_ids,
       "attention_mask": attention_mask,
       "decoder_attention_mask": decoder_attention_mask,
       "head_mask": head_mask,
       "decoder_head_mask": decoder_head_mask,
       "cross_attn_head_mask": cross_attn_head_mask,
   }



@require_tf
class SCREAMING_SNAKE_CASE     (  a_       ,							a_       ,							unittest.TestCase			):
  """simple docstring"""






  lowerCamelCase   :      Optional[int]										=(TFMBartForConditionalGeneration, TFMBartModel) if is_tf_available() else ()
  lowerCamelCase   :      List[str]										=(TFMBartForConditionalGeneration,) if is_tf_available() else ()
  lowerCamelCase   :      Union[str, Any]										=(
      {
          "conversational": TFMBartForConditionalGeneration,
          "feature-extraction": TFMBartModel,
          "summarization": TFMBartForConditionalGeneration,
          "text2text-generation": TFMBartForConditionalGeneration,
          "translation": TFMBartForConditionalGeneration,
      }
      if is_tf_available()
      else {}
  )
  lowerCamelCase   :      str										=True
  lowerCamelCase   :      Tuple										=False
  lowerCamelCase   :      Dict										=False







  def 	SCREAMING_SNAKE_CASE   (      self			:   str	,							lowerCAmelCase			:   Optional[Any]	,							lowerCAmelCase			:   Union[str, Any]	,							lowerCAmelCase			:   List[str]	,							lowerCAmelCase			:   Any	,							lowerCAmelCase			:   Optional[Any]   )							->     Optional[int]:






     """simple docstring"""







     if pipeline_test_casse_name != "FeatureExtractionPipelineTests":
        # Exception encountered when calling layer '...'
        return True

     return False







  def 	SCREAMING_SNAKE_CASE   (      self			:   Tuple   )							->     Optional[int]:






     """simple docstring"""







     __lowerCAmelCase  :      Union[str, Any]    =						TFMBartModelTester(self   )
     __lowerCAmelCase  :      Union[str, Any]    =						ConfigTester(self	,							config_class=lowerCAmelCase   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   List[str]   )							->     List[Any]:






     """simple docstring"""







     self.config_tester.run_common_tests()







  def 	SCREAMING_SNAKE_CASE   (      self			:   Tuple   )							->     List[str]:






     """simple docstring"""







     __lowerCAmelCase  :      List[Any]    =						self.model_tester.prepare_config_and_inputs_for_common()
     self.model_tester.check_decoder_model_past_large_inputs(*lowerCAmelCase   )






@require_sentencepiece
@require_tokenizers
@require_tf
class SCREAMING_SNAKE_CASE     (  unittest.TestCase			):
  """simple docstring"""






  lowerCamelCase   :      Union[str, Any]										=[
      " UN Chief Says There Is No Military Solution in Syria",
  ]
  lowerCamelCase   :      Tuple										=[
      "Şeful ONU declară că nu există o soluţie militară în Siria",
  ]
  lowerCamelCase   :      List[Any]										="facebook/mbart-large-en-ro"







  @cached_property
  def 	SCREAMING_SNAKE_CASE   (      self			:   List[str]   )							->     Any:






     """simple docstring"""







     return AutoTokenizer.from_pretrained(self.model_name   )







  @cached_property
  def 	SCREAMING_SNAKE_CASE   (      self			:   List[str]   )							->     Union[str, Any]:






     """simple docstring"""







     __lowerCAmelCase  :      int    =						TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name   )
     return model







  def 	SCREAMING_SNAKE_CASE   (      self			:   Optional[Any]	,							**lowerCAmelCase			:   Dict   )							->     Union[str, Any]:






     """simple docstring"""







     __lowerCAmelCase  :      Union[str, Any]    =						self.translate_src_text(**lowerCAmelCase   )
     self.assertListEqual(self.expected_text	,							lowerCAmelCase   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   Union[str, Any]	,							**lowerCAmelCase			:   int   )							->     str:






     """simple docstring"""







     __lowerCAmelCase  :      Dict    =						self.tokenizer(self.src_text	,							**lowerCAmelCase	,							return_tensors="""tf"""   )
     __lowerCAmelCase  :      Union[str, Any]    =						self.model.generate(
         model_inputs.input_ids	,							attention_mask=model_inputs.attention_mask	,							num_beams=2   )
     __lowerCAmelCase  :      List[str]    =						self.tokenizer.batch_decode(lowerCAmelCase	,							skip_special_tokens=lowerCAmelCase   )
     return generated_words







  @slow
  def 	SCREAMING_SNAKE_CASE   (      self			:   Optional[int]   )							->     Any:






     """simple docstring"""







     self._assert_generated_batch_equal_expected()


