




'''simple docstring'''

import json
import os
import random
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import BertTokenizer, BertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
		from PIL import Image

		from transformers import FlavaImageProcessor, FlavaProcessor
		from transformers.models.flava.image_processing_flava import (
		    FLAVA_CODEBOOK_MEAN,
		    FLAVA_CODEBOOK_STD,
		    FLAVA_IMAGE_MEAN,
		    FLAVA_IMAGE_STD,
		)





@require_vision
class __magic_name__(						unittest.TestCase      ):






						def       __lowerCAmelCase(   self						:						List[str]       ):



									'''simple docstring'''






									snake_case__          =						tempfile.mkdtemp()

									# fmt: off
									snake_case__          =						["""[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing""", """,""", """low""", """lowest"""]
									# fmt: on
									snake_case__          =						os.path.join(self.tmpdirname					,   VOCAB_FILES_NAMES["""vocab_file"""]       )

									with open(self.vocab_file					,   """w"""					,   encoding="""utf-8"""       ) as fp:
												fp.write("""""".join([x + """\n""" for x in vocab_tokens]       )       )

									snake_case__          =						{
									    """image_mean""": FLAVA_IMAGE_MEAN,
									    """image_std""": FLAVA_IMAGE_STD,
									    """do_normalize""": True,
									    """do_resize""": True,
									    """size""": 2_2_4,
									    """do_center_crop""": True,
									    """crop_size""": 2_2_4,
									    """input_size_patches""": 1_4,
									    """total_mask_patches""": 7_5,
									    """mask_group_max_patches""": None,
									    """mask_group_min_patches""": 1_6,
									    """mask_group_min_aspect_ratio""": 0.3,
									    """mask_group_max_aspect_ratio""": None,
									    """codebook_do_resize""": True,
									    """codebook_size""": 1_1_2,
									    """codebook_do_center_crop""": True,
									    """codebook_crop_size""": 1_1_2,
									    """codebook_do_map_pixels""": True,
									    """codebook_do_normalize""": True,
									    """codebook_image_mean""": FLAVA_CODEBOOK_MEAN,
									    """codebook_image_std""": FLAVA_CODEBOOK_STD,
									}

									snake_case__          =						os.path.join(self.tmpdirname					,   a_       )
									with open(self.image_processor_file					,   """w"""					,   encoding="""utf-8"""       ) as fp:
												json.dump(a_					,   a_       )






						def       __lowerCAmelCase(   self						:						List[str]					,   **__UpperCamelCase						:						int       ):



									'''simple docstring'''






									return BertTokenizer.from_pretrained(self.tmpdirname					,   **a_       )






						def       __lowerCAmelCase(   self						:						str					,   **__UpperCamelCase						:						Any       ):



									'''simple docstring'''






									return BertTokenizerFast.from_pretrained(self.tmpdirname					,   **a_       )






						def       __lowerCAmelCase(   self						:						Dict					,   **__UpperCamelCase						:						Tuple       ):



									'''simple docstring'''






									return FlavaImageProcessor.from_pretrained(self.tmpdirname					,   **a_       )






						def       __lowerCAmelCase(   self						:						List[str]       ):



									'''simple docstring'''






									shutil.rmtree(self.tmpdirname       )






						def       __lowerCAmelCase(   self						:						Tuple       ):



									'''simple docstring'''






									snake_case__          =						[np.random.randint(2_5_5					,   size=(3, 3_0, 4_0_0)					,   dtype=np.uinta       )]

									snake_case__          =						[Image.fromarray(np.moveaxis(a_					,   0					,   -1       )       ) for x in image_inputs]

									return image_inputs






						def       __lowerCAmelCase(   self						:						Any       ):



									'''simple docstring'''






									snake_case__          =						self.get_tokenizer()
									snake_case__          =						self.get_rust_tokenizer()
									snake_case__          =						self.get_image_processor()

									snake_case__          =						FlavaProcessor(tokenizer=a_					,   image_processor=a_       )
									processor_slow.save_pretrained(self.tmpdirname       )
									snake_case__          =						FlavaProcessor.from_pretrained(self.tmpdirname					,   use_fast=a_       )

									snake_case__          =						FlavaProcessor(tokenizer=a_					,   image_processor=a_       )
									processor_fast.save_pretrained(self.tmpdirname       )
									snake_case__          =						FlavaProcessor.from_pretrained(self.tmpdirname       )

									self.assertEqual(processor_slow.tokenizer.get_vocab()					,   tokenizer_slow.get_vocab()       )
									self.assertEqual(processor_fast.tokenizer.get_vocab()					,   tokenizer_fast.get_vocab()       )
									self.assertEqual(tokenizer_slow.get_vocab()					,   tokenizer_fast.get_vocab()       )
									self.assertIsInstance(processor_slow.tokenizer					,   a_       )
									self.assertIsInstance(processor_fast.tokenizer					,   a_       )

									self.assertEqual(processor_slow.image_processor.to_json_string()					,   image_processor.to_json_string()       )
									self.assertEqual(processor_fast.image_processor.to_json_string()					,   image_processor.to_json_string()       )
									self.assertIsInstance(processor_slow.image_processor					,   a_       )
									self.assertIsInstance(processor_fast.image_processor					,   a_       )






						def       __lowerCAmelCase(   self						:						int       ):



									'''simple docstring'''






									snake_case__          =						FlavaProcessor(tokenizer=self.get_tokenizer()					,   image_processor=self.get_image_processor()       )
									processor.save_pretrained(self.tmpdirname       )

									snake_case__          =						self.get_tokenizer(bos_token="""(BOS)"""					,   eos_token="""(EOS)"""       )
									snake_case__          =						self.get_image_processor(do_normalize=a_					,   padding_value=1.0       )

									snake_case__          =						FlavaProcessor.from_pretrained(
									    self.tmpdirname					,   bos_token="""(BOS)"""					,   eos_token="""(EOS)"""					,   do_normalize=a_					,   padding_value=1.0       )

									self.assertEqual(processor.tokenizer.get_vocab()					,   tokenizer_add_kwargs.get_vocab()       )
									self.assertIsInstance(processor.tokenizer					,   a_       )

									self.assertEqual(processor.image_processor.to_json_string()					,   image_processor_add_kwargs.to_json_string()       )
									self.assertIsInstance(processor.image_processor					,   a_       )






						def       __lowerCAmelCase(   self						:						int       ):



									'''simple docstring'''






									snake_case__          =						self.get_image_processor()
									snake_case__          =						self.get_tokenizer()

									snake_case__          =						FlavaProcessor(tokenizer=a_					,   image_processor=a_       )

									snake_case__          =						self.prepare_image_inputs()

									snake_case__          =						image_processor(a_					,   return_tensors="""np"""       )
									snake_case__          =						processor(images=a_					,   return_tensors="""np"""       )

									for key in input_feat_extract.keys():
												self.assertAlmostEqual(input_feat_extract[key].sum()					,   input_processor[key].sum()					,   delta=1E-2       )

									# With rest of the args
									random.seed(1_2_3_4       )
									snake_case__          =						image_processor(
									    a_					,   return_image_mask=a_					,   return_codebook_pixels=a_					,   return_tensors="""np"""       )
									random.seed(1_2_3_4       )
									snake_case__          =						processor(
									    images=a_					,   return_image_mask=a_					,   return_codebook_pixels=a_					,   return_tensors="""np"""       )

									for key in input_feat_extract.keys():
												self.assertAlmostEqual(input_feat_extract[key].sum()					,   input_processor[key].sum()					,   delta=1E-2       )






						def       __lowerCAmelCase(   self						:						Any       ):



									'''simple docstring'''






									snake_case__          =						self.get_image_processor()
									snake_case__          =						self.get_tokenizer()

									snake_case__          =						FlavaProcessor(tokenizer=a_					,   image_processor=a_       )

									snake_case__          =						"""lower newer"""

									snake_case__          =						processor(text=a_       )

									snake_case__          =						tokenizer(a_       )

									for key in encoded_tok.keys():
												self.assertListEqual(encoded_tok[key]					,   encoded_processor[key]       )






						def       __lowerCAmelCase(   self						:						Optional[int]       ):



									'''simple docstring'''






									snake_case__          =						self.get_image_processor()
									snake_case__          =						self.get_tokenizer()

									snake_case__          =						FlavaProcessor(tokenizer=a_					,   image_processor=a_       )

									snake_case__          =						"""lower newer"""
									snake_case__          =						self.prepare_image_inputs()

									snake_case__          =						processor(text=a_					,   images=a_       )

									self.assertListEqual(list(inputs.keys()       )					,   ["""input_ids""", """token_type_ids""", """attention_mask""", """pixel_values"""]       )

									# add extra args
									snake_case__          =						processor(text=a_					,   images=a_					,   return_codebook_pixels=a_					,   return_image_mask=a_       )

									self.assertListEqual(
									    list(inputs.keys()       )					,   [
									        """input_ids""",
									        """token_type_ids""",
									        """attention_mask""",
									        """pixel_values""",
									        """codebook_pixel_values""",
									        """bool_masked_pos""",
									    ]					,   )

									# test if it raises when no input is passed
									with pytest.raises(a_       ):
												processor()






						def       __lowerCAmelCase(   self						:						Tuple       ):



									'''simple docstring'''






									snake_case__          =						self.get_image_processor()
									snake_case__          =						self.get_tokenizer()

									snake_case__          =						FlavaProcessor(tokenizer=a_					,   image_processor=a_       )

									snake_case__          =						[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

									snake_case__          =						processor.batch_decode(a_       )
									snake_case__          =						tokenizer.batch_decode(a_       )

									self.assertListEqual(a_					,   a_       )







						def       __lowerCAmelCase(   self						:						Optional[int]       ):



									'''simple docstring'''






									snake_case__          =						self.get_image_processor()
									snake_case__          =						self.get_tokenizer()

									snake_case__          =						FlavaProcessor(tokenizer=a_					,   image_processor=a_       )

									snake_case__          =						"""lower newer"""
									snake_case__          =						self.prepare_image_inputs()

									snake_case__          =						processor(text=a_					,   images=a_       )

									self.assertListEqual(list(inputs.keys()       )					,   processor.model_input_names       )



'''simple docstring'''

from collections import OrderedDict
from typing import Any, Mapping, Optional

from ... import PreTrainedTokenizer
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import TensorType, is_torch_available, logging


__lowercase:		int    						=  logging.get_logger(__name__)

__lowercase:		Tuple    						=  {
    "facebook/mbart-large-cc25": "https://huggingface.co/facebook/mbart-large-cc25/resolve/main/config.json",
    # See all MBART models at https://huggingface.co/models?filter=mbart
}







class 			UpperCAmelCase			(							SCREAMING_SNAKE_CASE__):
 _lowerCamelCase			:       List[str]            =       'mbart'
 _lowerCamelCase			:       str            =       ['past_key_values']
 _lowerCamelCase			:       List[str]            =       {'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}

 def __init__(			self			:						Tuple,       a_			:						Union[str, Any]=5_0265,       a_			:						Union[str, Any]=1024,       a_			:						Optional[int]=12,       a_			:						str=4096,       a_			:						Optional[Any]=16,       a_			:						str=12,       a_			:						Union[str, Any]=4096,       a_			:						List[Any]=16,       a_			:						Optional[Any]=0.0,       a_			:						int=0.0,       a_			:						Any=True,       a_			:						Optional[Any]=True,       a_			:						Any="gelu",       a_			:						int=1024,       a_			:						Any=0.1,       a_			:						Optional[int]=0.0,       a_			:						Tuple=0.0,       a_			:						Optional[Any]=0.02,       a_			:						Any=0.0,       a_			:						Optional[Any]=False,       a_			:						Any=1,       a_			:						Optional[Any]=0,       a_			:						List[str]=2,       a_			:						Any=2,       **a_			:						int,       ):





    """simple docstring"""



    UpperCamelCase__			    =    vocab_size
    UpperCamelCase__			    =    max_position_embeddings
    UpperCamelCase__			    =    d_model
    UpperCamelCase__			    =    encoder_ffn_dim
    UpperCamelCase__			    =    encoder_layers
    UpperCamelCase__			    =    encoder_attention_heads
    UpperCamelCase__			    =    decoder_ffn_dim
    UpperCamelCase__			    =    decoder_layers
    UpperCamelCase__			    =    decoder_attention_heads
    UpperCamelCase__			    =    dropout
    UpperCamelCase__			    =    attention_dropout
    UpperCamelCase__			    =    activation_dropout
    UpperCamelCase__			    =    activation_function
    UpperCamelCase__			    =    init_std
    UpperCamelCase__			    =    encoder_layerdrop
    UpperCamelCase__			    =    decoder_layerdrop
    UpperCamelCase__			    =    classifier_dropout
    UpperCamelCase__			    =    use_cache
    UpperCamelCase__			    =    encoder_layers
    UpperCamelCase__			    =    scale_embedding  # scale factor will be sqrt(d_model) if True
    super().__init__(
        pad_token_id=a_,       bos_token_id=a_,       eos_token_id=a_,       is_encoder_decoder=a_,       forced_eos_token_id=a_,       **a_,       )







class 			UpperCAmelCase			(							SCREAMING_SNAKE_CASE__):

 @property
 def 		lowercase_    (			self			:						Optional[Any]       ):





    """simple docstring"""



    if self.task in ["default", "seq2seq-lm"]:
       UpperCamelCase__			    =    OrderedDict(
           [
               ("input_ids", {0: "batch", 1: "encoder_sequence"}),
               ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
           ]       )

       if self.use_past:
          UpperCamelCase__			    =    {0: "batch"}
          UpperCamelCase__			    =    {0: "batch", 1: "past_decoder_sequence + sequence"}
       else:
          UpperCamelCase__			    =    {0: "batch", 1: "decoder_sequence"}
          UpperCamelCase__			    =    {0: "batch", 1: "decoder_sequence"}

       if self.use_past:
          self.fill_with_past_key_values_(a_,       direction="inputs"       )
    elif self.task == "causal-lm":
       # TODO: figure this case out.
       UpperCamelCase__			    =    OrderedDict(
           [
               ("input_ids", {0: "batch", 1: "encoder_sequence"}),
               ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
           ]       )
       if self.use_past:
          UpperCamelCase__    ,		UpperCamelCase__			    =    self.num_layers
          for i in range(a_       ):
             UpperCamelCase__			    =    {0: "batch", 2: "past_sequence + sequence"}
             UpperCamelCase__			    =    {0: "batch", 2: "past_sequence + sequence"}
    else:
       UpperCamelCase__			    =    OrderedDict(
           [
               ("input_ids", {0: "batch", 1: "encoder_sequence"}),
               ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
               ("decoder_input_ids", {0: "batch", 1: "decoder_sequence"}),
               ("decoder_attention_mask", {0: "batch", 1: "decoder_sequence"}),
           ]       )

    return common_inputs

 @property
 def 		lowercase_    (			self			:						Any       ):





    """simple docstring"""



    if self.task in ["default", "seq2seq-lm"]:
       UpperCamelCase__			    =    super().outputs
    else:
       UpperCamelCase__			    =    super(a_,       self       ).outputs
       if self.use_past:
          UpperCamelCase__    ,		UpperCamelCase__			    =    self.num_layers
          for i in range(a_       ):
             UpperCamelCase__			    =    {0: "batch", 2: "past_sequence + sequence"}
             UpperCamelCase__			    =    {0: "batch", 2: "past_sequence + sequence"}
    return common_outputs

 def 		lowercase_    (			self			:						str,       a_			:						PreTrainedTokenizer,       a_			:						int = -1,       a_			:						int = -1,       a_			:						bool = False,       a_			:						Optional[TensorType] = None,       ):





    """simple docstring"""



    UpperCamelCase__			    =    self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
        a_,       a_,       a_,       a_,       a_       )

    # Generate decoder inputs
    UpperCamelCase__			    =    seq_length if not self.use_past else 1
    UpperCamelCase__			    =    self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
        a_,       a_,       a_,       a_,       a_       )
    UpperCamelCase__			    =    {f'decoder_{name}': tensor for name, tensor in decoder_inputs.items()}
    UpperCamelCase__			    =    dict(**a_,       **a_       )

    if self.use_past:
       if not is_torch_available():
          raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed."       )
       else:
          import torch
       UpperCamelCase__    ,		UpperCamelCase__			    =    common_inputs["input_ids"].shape
       UpperCamelCase__			    =    common_inputs["decoder_input_ids"].shape[1]
       UpperCamelCase__    ,		UpperCamelCase__			    =    self.num_attention_heads
       UpperCamelCase__			    =    (
           batch,
           num_encoder_attention_heads,
           encoder_seq_length,
           self._config.hidden_size // num_encoder_attention_heads,
       )
       UpperCamelCase__			    =    decoder_seq_length + 3
       UpperCamelCase__			    =    (
           batch,
           num_decoder_attention_heads,
           decoder_past_length,
           self._config.hidden_size // num_decoder_attention_heads,
       )

       UpperCamelCase__			    =    torch.cat(
           [common_inputs["decoder_attention_mask"], torch.ones(a_,       a_       )],       dim=1       )

       UpperCamelCase__			    =    []
       # If the number of encoder and decoder layers are present in the model configuration, both are considered
       UpperCamelCase__    ,		UpperCamelCase__			    =    self.num_layers
       UpperCamelCase__			    =    min(a_,       a_       )
       UpperCamelCase__			    =    max(a_,       a_       ) - min_num_layers
       UpperCamelCase__			    =    "encoder" if num_encoder_layers > num_decoder_layers else "decoder"

       for _ in range(a_       ):
          common_inputs["past_key_values"].append(
              (
                  torch.zeros(a_       ),
                  torch.zeros(a_       ),
                  torch.zeros(a_       ),
                  torch.zeros(a_       ),
              )       )
       # TODO: test this.
       UpperCamelCase__			    =    encoder_shape if remaining_side_name == "encoder" else decoder_shape
       for _ in range(a_,       a_       ):
          common_inputs["past_key_values"].append((torch.zeros(a_       ), torch.zeros(a_       ))       )
    return common_inputs

 def 		lowercase_    (			self			:						Optional[int],       a_			:						PreTrainedTokenizer,       a_			:						int = -1,       a_			:						int = -1,       a_			:						bool = False,       a_			:						Optional[TensorType] = None,       ):





    """simple docstring"""



    UpperCamelCase__			    =    self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
        a_,       a_,       a_,       a_,       a_       )

    if self.use_past:
       if not is_torch_available():
          raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed."       )
       else:
          import torch
       UpperCamelCase__    ,		UpperCamelCase__			    =    common_inputs["input_ids"].shape
       # Not using the same length for past_key_values
       UpperCamelCase__			    =    seqlen + 2
       UpperCamelCase__    ,		UpperCamelCase__			    =    self.num_layers
       UpperCamelCase__    ,		UpperCamelCase__			    =    self.num_attention_heads
       UpperCamelCase__			    =    (
           batch,
           num_encoder_attention_heads,
           past_key_values_length,
           self._config.hidden_size // num_encoder_attention_heads,
       )

       UpperCamelCase__			    =    common_inputs["attention_mask"].dtype
       UpperCamelCase__			    =    torch.cat(
           [common_inputs["attention_mask"], torch.ones(a_,       a_,       dtype=a_       )],       dim=1       )
       UpperCamelCase__			    =    [
           (torch.zeros(a_       ), torch.zeros(a_       )) for _ in range(a_       )
       ]
    return common_inputs

 def 		lowercase_    (			self			:						List[Any],       a_			:						PreTrainedTokenizer,       a_			:						int = -1,       a_			:						int = -1,       a_			:						bool = False,       a_			:						Optional[TensorType] = None,       ):





    """simple docstring"""



    UpperCamelCase__			    =    compute_effective_axis_dimension(
        a_,       fixed_dimension=OnnxConfig.default_fixed_batch,       num_token_to_add=0       )

    # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
    UpperCamelCase__			    =    tokenizer.num_special_tokens_to_add(a_       )
    UpperCamelCase__			    =    compute_effective_axis_dimension(
        a_,       fixed_dimension=OnnxConfig.default_fixed_sequence,       num_token_to_add=a_       )

    # Generate dummy inputs according to compute batch and sequence
    UpperCamelCase__			    =    [" ".join([tokenizer.unk_token]       ) * seq_length] * batch_size
    UpperCamelCase__			    =    dict(tokenizer(a_,       return_tensors=a_       )       )
    return common_inputs

 def 		lowercase_    (			self			:						Union[str, Any],       a_			:						PreTrainedTokenizer,       a_			:						int = -1,       a_			:						int = -1,       a_			:						bool = False,       a_			:						Optional[TensorType] = None,       ):





    """simple docstring"""



    if self.task in ["default", "seq2seq-lm"]:
       UpperCamelCase__			    =    self._generate_dummy_inputs_for_default_and_seqaseq_lm(
           a_,       batch_size=a_,       seq_length=a_,       is_pair=a_,       framework=a_       )

    elif self.task == "causal-lm":
       UpperCamelCase__			    =    self._generate_dummy_inputs_for_causal_lm(
           a_,       batch_size=a_,       seq_length=a_,       is_pair=a_,       framework=a_       )
    else:
       UpperCamelCase__			    =    self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
           a_,       batch_size=a_,       seq_length=a_,       is_pair=a_,       framework=a_       )

    return common_inputs

 def 		lowercase_    (			self			:						Dict,       a_			:						int,       a_			:						str,       a_			:						List[Any],       a_			:						int       ):





    """simple docstring"""



    if self.task in ["default", "seq2seq-lm"]:
       UpperCamelCase__			    =    super()._flatten_past_key_values_(a_,       a_,       a_,       a_       )
    else:
       UpperCamelCase__			    =    super(a_,       self       )._flatten_past_key_values_(
           a_,       a_,       a_,       a_       )