





'''simple docstring'''






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


  """simple docstring"""







  lowerCAmelCase_					=				MBartConfig
  lowerCAmelCase_					=				{}
  lowerCAmelCase_					=				"""gelu"""


  def __init__(				self    :					Tuple							,	UpperCamelCase__    :					List[Any]							,	UpperCamelCase__    :					Tuple=13							,	UpperCamelCase__    :					Tuple=7							,	UpperCamelCase__    :					Dict=True							,	UpperCamelCase__    :					Dict=False							,	UpperCamelCase__    :					Optional[int]=99							,	UpperCamelCase__    :					Dict=32							,	UpperCamelCase__    :					Optional[int]=2							,	UpperCamelCase__    :					List[Any]=4							,	UpperCamelCase__    :					Optional[Any]=37							,	UpperCamelCase__    :					Tuple=0.1							,	UpperCamelCase__    :					str=0.1							,	UpperCamelCase__    :					Tuple=20							,	UpperCamelCase__    :					Dict=2							,	UpperCamelCase__    :					str=1							,	UpperCamelCase__    :					Union[str, Any]=0							,	)     ->						Optional[int]:
    _UpperCamelCase       			=parent
    _UpperCamelCase       			=batch_size
    _UpperCamelCase       			=seq_length
    _UpperCamelCase       			=is_training
    _UpperCamelCase       			=use_labels
    _UpperCamelCase       			=vocab_size
    _UpperCamelCase       			=hidden_size
    _UpperCamelCase       			=num_hidden_layers
    _UpperCamelCase       			=num_attention_heads
    _UpperCamelCase       			=intermediate_size
    _UpperCamelCase       			=hidden_dropout_prob
    _UpperCamelCase       			=attention_probs_dropout_prob
    _UpperCamelCase       			=max_position_embeddings
    _UpperCamelCase       			=eos_token_id
    _UpperCamelCase       			=pad_token_id
    _UpperCamelCase       			=bos_token_id


  def   UpperCamelCase__      (				self    :					Union[str, Any]  )     ->						Union[str, Any]:
    _UpperCamelCase       			=ids_tensor([self.batch_size, self.seq_length - 1]							,	self.vocab_size  )
    _UpperCamelCase       			=tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size  )							,	1  )
    _UpperCamelCase       			=tf.concat([input_ids, eos_tensor]							,	axis=1  )

    _UpperCamelCase       			=ids_tensor([self.batch_size, self.seq_length]							,	self.vocab_size  )

    _UpperCamelCase       			=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							,	)
    _UpperCamelCase       			=prepare_mbart_inputs_dict(UpperCamelCase__							,	UpperCamelCase__							,	UpperCamelCase__  )
    return config, inputs_dict




  def   UpperCamelCase__      (				self    :					int							,	UpperCamelCase__    :					Dict							,	UpperCamelCase__    :					int  )     ->						Any:
    _UpperCamelCase       			=TFMBartModel(config=UpperCamelCase__  ).get_decoder()
    _UpperCamelCase       			=inputs_dict['''input_ids''']

    _UpperCamelCase       			=input_ids[:1, :]
    _UpperCamelCase       			=inputs_dict['''attention_mask'''][:1, :]
    _UpperCamelCase       			=inputs_dict['''head_mask''']
    _UpperCamelCase       			=1

    # first forward pass
    _UpperCamelCase       			=model(UpperCamelCase__							,	attention_mask=UpperCamelCase__							,	head_mask=UpperCamelCase__							,	use_cache=UpperCamelCase__  )

    _UpperCamelCase			,     _UpperCamelCase       			=outputs.to_tuple()
    _UpperCamelCase       			=past_key_values[1]




def 					_a   (__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE=None  ,      __SCREAMING_SNAKE_CASE=None  ,      __SCREAMING_SNAKE_CASE=None  ,      __SCREAMING_SNAKE_CASE=None  ,      __SCREAMING_SNAKE_CASE=None  ,      ):


  """simple docstring"""

  if attention_mask is None:
    _UpperCamelCase       			=tf.cast(tf.math.not_equal(__SCREAMING_SNAKE_CASE  ,      config.pad_token_id   )  ,      tf.inta   )
  if decoder_attention_mask is None:
    _UpperCamelCase       			=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:
    _UpperCamelCase       			=tf.ones((config.encoder_layers, config.encoder_attention_heads)   )
  if decoder_head_mask is None:
    _UpperCamelCase       			=tf.ones((config.decoder_layers, config.decoder_attention_heads)   )
  if cross_attn_head_mask is None:
    _UpperCamelCase       			=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 		UpperCAmelCase   (				lowercase_   ,     lowercase_   ,     unittest.TestCase):


  """simple docstring"""







  lowerCAmelCase_					=				(TFMBartForConditionalGeneration, TFMBartModel) if is_tf_available() else ()
  lowerCAmelCase_					=				(TFMBartForConditionalGeneration,) if is_tf_available() else ()
  lowerCAmelCase_					=				(
      {
          """conversational""": TFMBartForConditionalGeneration,
          """feature-extraction""": TFMBartModel,
          """summarization""": TFMBartForConditionalGeneration,
          """text2text-generation""": TFMBartForConditionalGeneration,
          """translation""": TFMBartForConditionalGeneration,
      }
      if is_tf_available()
      else {}
  )
  lowerCAmelCase_					=				True
  lowerCAmelCase_					=				False
  lowerCAmelCase_					=				False


  def   UpperCamelCase__      (				self    :					Optional[int]							,	UpperCamelCase__    :					Optional[Any]							,	UpperCamelCase__    :					Union[str, Any]							,	UpperCamelCase__    :					int							,	UpperCamelCase__    :					Optional[Any]							,	UpperCamelCase__    :					int  )     ->						int:
    if pipeline_test_casse_name != "FeatureExtractionPipelineTests":
      # Exception encountered when calling layer '...'
      return True

    return False


  def   UpperCamelCase__      (				self    :					int  )     ->						Optional[Any]:
    _UpperCamelCase       			=TFMBartModelTester(self  )
    _UpperCamelCase       			=ConfigTester(self							,	config_class=UpperCamelCase__  )


  def   UpperCamelCase__      (				self    :					Union[str, Any]  )     ->						Dict:
    self.config_tester.run_common_tests()




  def   UpperCamelCase__      (				self    :					List[Any]  )     ->						List[Any]:
    _UpperCamelCase       			=self.model_tester.prepare_config_and_inputs_for_common()
    self.model_tester.check_decoder_model_past_large_inputs(*UpperCamelCase__  )







@require_sentencepiece
@require_tokenizers
@require_tf
class 		UpperCAmelCase   (				unittest.TestCase):


  """simple docstring"""







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


  @cached_property
  def   UpperCamelCase__      (				self    :					Optional[Any]  )     ->						Dict:
    return AutoTokenizer.from_pretrained(self.model_name  )


  @cached_property
  def   UpperCamelCase__      (				self    :					List[str]  )     ->						Any:
    _UpperCamelCase       			=TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name  )
    return model


  def   UpperCamelCase__      (				self    :					int							,	**UpperCamelCase__    :					Union[str, Any]  )     ->						int:
    _UpperCamelCase       			=self.translate_src_text(**UpperCamelCase__  )
    self.assertListEqual(self.expected_text							,	UpperCamelCase__  )


  def   UpperCamelCase__      (				self    :					Union[str, Any]							,	**UpperCamelCase__    :					Union[str, Any]  )     ->						Tuple:
    _UpperCamelCase       			=self.tokenizer(self.src_text							,	**UpperCamelCase__							,	return_tensors='''tf'''  )
    _UpperCamelCase       			=self.model.generate(
        model_inputs.input_ids							,	attention_mask=model_inputs.attention_mask							,	num_beams=2  )
    _UpperCamelCase       			=self.tokenizer.batch_decode(UpperCamelCase__							,	skip_special_tokens=UpperCamelCase__  )
    return generated_words




  @slow
  def   UpperCamelCase__      (				self    :					List[str]  )     ->						List[str]:
    self._assert_generated_batch_equal_expected()













'''simple docstring'''






import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
      import torch

if is_vision_available():
      from PIL import Image

      from transformers import PoolFormerImageProcessor







class 		UpperCAmelCase   (				unittest.TestCase):


  """simple docstring"""







  def __init__(				self    :					Optional[int]							,	UpperCamelCase__    :					Union[str, Any]							,	UpperCamelCase__    :					Any=7							,	UpperCamelCase__    :					List[Any]=3							,	UpperCamelCase__    :					Dict=30							,	UpperCamelCase__    :					Union[str, Any]=400							,	UpperCamelCase__    :					List[Any]=True							,	UpperCamelCase__    :					List[str]=None							,	UpperCamelCase__    :					List[str]=0.9							,	UpperCamelCase__    :					Any=None							,	UpperCamelCase__    :					Any=True							,	UpperCamelCase__    :					Optional[Any]=[0.5, 0.5, 0.5]							,	UpperCamelCase__    :					List[str]=[0.5, 0.5, 0.5]							,	)     ->						Dict:
    _UpperCamelCase       			=size if size is not None else {'''shortest_edge''': 30}
    _UpperCamelCase       			=crop_size if crop_size is not None else {'''height''': 30, '''width''': 30}
    _UpperCamelCase       			=parent
    _UpperCamelCase       			=batch_size
    _UpperCamelCase       			=num_channels
    _UpperCamelCase       			=min_resolution
    _UpperCamelCase       			=max_resolution
    _UpperCamelCase       			=do_resize_and_center_crop
    _UpperCamelCase       			=size
    _UpperCamelCase       			=crop_pct
    _UpperCamelCase       			=crop_size
    _UpperCamelCase       			=do_normalize
    _UpperCamelCase       			=image_mean
    _UpperCamelCase       			=image_std




  def   UpperCamelCase__      (				self    :					str  )     ->						Union[str, Any]:
    return {
        "size": self.size,
        "do_resize_and_center_crop": self.do_resize_and_center_crop,
        "crop_pct": self.crop_pct,
        "crop_size": self.crop_size,
        "do_normalize": self.do_normalize,
        "image_mean": self.image_mean,
        "image_std": self.image_std,
    }







@require_torch
@require_vision
class 		UpperCAmelCase   (				lowercase_   ,     unittest.TestCase):


  """simple docstring"""







  lowerCAmelCase_					=				PoolFormerImageProcessor if is_vision_available() else None


  def   UpperCamelCase__      (				self    :					Optional[int]  )     ->						int:
    _UpperCamelCase       			=PoolFormerImageProcessingTester(self  )


  @property
  def   UpperCamelCase__      (				self    :					Tuple  )     ->						Optional[int]:
    return self.image_processor_tester.prepare_image_processor_dict()


  def   UpperCamelCase__      (				self    :					Optional[Any]  )     ->						int:
    _UpperCamelCase       			=self.image_processing_class(**self.image_processor_dict  )
    self.assertTrue(hasattr(UpperCamelCase__							,	'''do_resize_and_center_crop'''  )  )
    self.assertTrue(hasattr(UpperCamelCase__							,	'''size'''  )  )
    self.assertTrue(hasattr(UpperCamelCase__							,	'''crop_pct'''  )  )
    self.assertTrue(hasattr(UpperCamelCase__							,	'''do_normalize'''  )  )
    self.assertTrue(hasattr(UpperCamelCase__							,	'''image_mean'''  )  )
    self.assertTrue(hasattr(UpperCamelCase__							,	'''image_std'''  )  )


  def   UpperCamelCase__      (				self    :					Any  )     ->						Dict:
    _UpperCamelCase       			=self.image_processing_class.from_dict(self.image_processor_dict  )
    self.assertEqual(image_processor.size							,	{'''shortest_edge''': 30}  )
    self.assertEqual(image_processor.crop_size							,	{'''height''': 30, '''width''': 30}  )

    _UpperCamelCase       			=self.image_processing_class.from_dict(self.image_processor_dict							,	size=42							,	crop_size=84  )
    self.assertEqual(image_processor.size							,	{'''shortest_edge''': 42}  )
    self.assertEqual(image_processor.crop_size							,	{'''height''': 84, '''width''': 84}  )


  def   UpperCamelCase__      (				self    :					Dict  )     ->						Optional[Any]:
    pass


  def   UpperCamelCase__      (				self    :					Union[str, Any]  )     ->						List[Any]:
    # Initialize image_processing
    _UpperCamelCase       			=self.image_processing_class(**self.image_processor_dict  )
    # create random PIL images
    _UpperCamelCase       			=prepare_image_inputs(self.image_processor_tester							,	equal_resolution=UpperCamelCase__  )
    for image in image_inputs:
      self.assertIsInstance(UpperCamelCase__							,	Image.Image  )

    # Test not batched input
    _UpperCamelCase       			=image_processing(image_inputs[0]							,	return_tensors='''pt'''  ).pixel_values

    self.assertEqual(
        encoded_images.shape							,	(
            1,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['''height'''],
            self.image_processor_tester.crop_size['''width'''],
        )							,	)

    # Test batched
    _UpperCamelCase       			=image_processing(UpperCamelCase__							,	return_tensors='''pt'''  ).pixel_values
    self.assertEqual(
        encoded_images.shape							,	(
            self.image_processor_tester.batch_size,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['''height'''],
            self.image_processor_tester.crop_size['''width'''],
        )							,	)


  def   UpperCamelCase__      (				self    :					Union[str, Any]  )     ->						Optional[int]:
    # Initialize image_processing
    _UpperCamelCase       			=self.image_processing_class(**self.image_processor_dict  )
    # create random numpy tensors
    _UpperCamelCase       			=prepare_image_inputs(self.image_processor_tester							,	equal_resolution=UpperCamelCase__							,	numpify=UpperCamelCase__  )
    for image in image_inputs:
      self.assertIsInstance(UpperCamelCase__							,	np.ndarray  )

    # Test not batched input
    _UpperCamelCase       			=image_processing(image_inputs[0]							,	return_tensors='''pt'''  ).pixel_values
    self.assertEqual(
        encoded_images.shape							,	(
            1,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['''height'''],
            self.image_processor_tester.crop_size['''width'''],
        )							,	)

    # Test batched
    _UpperCamelCase       			=image_processing(UpperCamelCase__							,	return_tensors='''pt'''  ).pixel_values
    self.assertEqual(
        encoded_images.shape							,	(
            self.image_processor_tester.batch_size,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['''height'''],
            self.image_processor_tester.crop_size['''width'''],
        )							,	)




  def   UpperCamelCase__      (				self    :					str  )     ->						List[Any]:
    # Initialize image_processing
    _UpperCamelCase       			=self.image_processing_class(**self.image_processor_dict  )
    # create random PyTorch tensors
    _UpperCamelCase       			=prepare_image_inputs(self.image_processor_tester							,	equal_resolution=UpperCamelCase__							,	torchify=UpperCamelCase__  )
    for image in image_inputs:
      self.assertIsInstance(UpperCamelCase__							,	torch.Tensor  )

    # Test not batched input
    _UpperCamelCase       			=image_processing(image_inputs[0]							,	return_tensors='''pt'''  ).pixel_values
    self.assertEqual(
        encoded_images.shape							,	(
            1,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['''height'''],
            self.image_processor_tester.crop_size['''width'''],
        )							,	)

    # Test batched
    _UpperCamelCase       			=image_processing(UpperCamelCase__							,	return_tensors='''pt'''  ).pixel_values
    self.assertEqual(
        encoded_images.shape							,	(
            self.image_processor_tester.batch_size,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['''height'''],
            self.image_processor_tester.crop_size['''width'''],
        )							,	)






