





from __future__ import annotations

import unittest

import numpy as np

from transformers import BlipTextConfig
from transformers.testing_utils import require_tf, slow
from transformers.utils import is_tf_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask


if is_tf_available():
     import tensorflow as tf

     from transformers import TFBlipTextModel
     from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST



class 							_UpperCamelCase					:

       def __init__(		self: List[Any]					,   _SCREAMING_SNAKE_CASE: Dict					,   _SCREAMING_SNAKE_CASE: Tuple=12					,   _SCREAMING_SNAKE_CASE: str=7					,   _SCREAMING_SNAKE_CASE: int=True					,   _SCREAMING_SNAKE_CASE: Optional[int]=True					,   _SCREAMING_SNAKE_CASE: Optional[int]=True					,   _SCREAMING_SNAKE_CASE: Tuple=99					,   _SCREAMING_SNAKE_CASE: Union[str, Any]=32					,   _SCREAMING_SNAKE_CASE: List[str]=32					,   _SCREAMING_SNAKE_CASE: Dict=2					,   _SCREAMING_SNAKE_CASE: List[str]=4					,   _SCREAMING_SNAKE_CASE: Optional[int]=37					,   _SCREAMING_SNAKE_CASE: Optional[int]=0.1					,   _SCREAMING_SNAKE_CASE: Optional[Any]=0.1					,   _SCREAMING_SNAKE_CASE: Union[str, Any]=512					,   _SCREAMING_SNAKE_CASE: Union[str, Any]=0.02					,   _SCREAMING_SNAKE_CASE: Union[str, Any]=0					,   _SCREAMING_SNAKE_CASE: Union[str, Any]=None					,   )       -> Union[str, Any]:




              """simple docstring"""


              UpperCamelCase_          =  parent
              UpperCamelCase_          =  batch_size
              UpperCamelCase_          =  seq_length
              UpperCamelCase_          =  is_training
              UpperCamelCase_          =  use_input_mask
              UpperCamelCase_          =  use_labels
              UpperCamelCase_          =  vocab_size
              UpperCamelCase_          =  hidden_size
              UpperCamelCase_          =  projection_dim
              UpperCamelCase_          =  num_hidden_layers
              UpperCamelCase_          =  num_attention_heads
              UpperCamelCase_          =  intermediate_size
              UpperCamelCase_          =  dropout
              UpperCamelCase_          =  attention_dropout
              UpperCamelCase_          =  max_position_embeddings
              UpperCamelCase_          =  initializer_range
              UpperCamelCase_          =  scope
              UpperCamelCase_          =  bos_token_id

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




              """simple docstring"""


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

              UpperCamelCase_          =  None
              if self.use_input_mask:
                     UpperCamelCase_          =  random_attention_mask([self.batch_size, self.seq_length]		)

              if input_mask is not None:
                     UpperCamelCase_          =  input_mask.numpy()
                     UpperCamelCase_       , UpperCamelCase_          =  input_mask.shape
                     UpperCamelCase_          =  np.random.randint(1					,   seq_length - 1					,   size=(batch_size,)		)
                     for batch_idx, start_index in enumerate(_SCREAMING_SNAKE_CASE		):
                            UpperCamelCase_          =  1
                            UpperCamelCase_          =  0

              UpperCamelCase_          =  self.get_config()

              return config, input_ids, tf.convert_to_tensor(_SCREAMING_SNAKE_CASE		)

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




              """simple docstring"""


              return BlipTextConfig(
                  vocab_size=self.vocab_size					,   hidden_size=self.hidden_size					,   projection_dim=self.projection_dim					,   num_hidden_layers=self.num_hidden_layers					,   num_attention_heads=self.num_attention_heads					,   intermediate_size=self.intermediate_size					,   dropout=self.dropout					,   attention_dropout=self.attention_dropout					,   max_position_embeddings=self.max_position_embeddings					,   initializer_range=self.initializer_range					,   bos_token_id=self.bos_token_id					,   )

       def lowercase (		self: str					,   _SCREAMING_SNAKE_CASE: int					,   _SCREAMING_SNAKE_CASE: Tuple					,   _SCREAMING_SNAKE_CASE: Union[str, Any]		)       -> List[Any]:




              """simple docstring"""


              UpperCamelCase_          =  TFBlipTextModel(config=_SCREAMING_SNAKE_CASE		)
              UpperCamelCase_          =  model(_SCREAMING_SNAKE_CASE					,   attention_mask=_SCREAMING_SNAKE_CASE					,   training=_SCREAMING_SNAKE_CASE		)
              UpperCamelCase_          =  model(_SCREAMING_SNAKE_CASE					,   training=_SCREAMING_SNAKE_CASE		)
              self.parent.assertEqual(result.last_hidden_state.shape					,   (self.batch_size, self.seq_length, self.hidden_size)		)
              self.parent.assertEqual(result.pooler_output.shape					,   (self.batch_size, self.hidden_size)		)



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




              """simple docstring"""


              UpperCamelCase_          =  self.prepare_config_and_inputs()
              UpperCamelCase_       , UpperCamelCase_       , UpperCamelCase_          =  config_and_inputs
              UpperCamelCase_          =  {"input_ids": input_ids, "attention_mask": input_mask}
              return config, inputs_dict



@require_tf
class 							_UpperCamelCase					(	lowerCAmelCase_  ,						unittest.TestCase     ):
       _UpperCamelCase				:					Optional[Any]         =   (TFBlipTextModel,) if is_tf_available() else ()
       _UpperCamelCase				:					List[str]         =   False
       _UpperCamelCase				:					Optional[int]         =   False
       _UpperCamelCase				:					Dict         =   False

       def lowercase (		self: Any		)       -> str:




              """simple docstring"""


              UpperCamelCase_          =  BlipTextModelTester(self		)
              UpperCamelCase_          =  ConfigTester(self					,   config_class=_SCREAMING_SNAKE_CASE					,   hidden_size=37		)

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




              """simple docstring"""


              self.config_tester.run_common_tests()

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




              """simple docstring"""


              UpperCamelCase_          =  self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE		)

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




              """simple docstring"""


              pass

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




              """simple docstring"""


              pass

       @unittest.skip(reason="Blip does not use inputs_embeds"		)
       def lowercase (		self: str		)       -> Union[str, Any]:




              """simple docstring"""


              pass

       @unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING"		)
       def lowercase (		self: int		)       -> str:




              """simple docstring"""


              pass

       @unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING"		)
       def lowercase (		self: List[str]		)       -> int:




              """simple docstring"""


              pass

       @slow
       def lowercase (		self: Union[str, Any]		)       -> Dict:




              """simple docstring"""


              for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                     UpperCamelCase_          =  TFBlipTextModel.from_pretrained(_SCREAMING_SNAKE_CASE		)
                     self.assertIsNotNone(_SCREAMING_SNAKE_CASE		)



       def lowercase (		self: str					,   _SCREAMING_SNAKE_CASE: Any=True		)       -> int:




              """simple docstring"""


              super().test_pt_tf_model_equivalence(allow_missing_keys=_SCREAMING_SNAKE_CASE		)








import json
import os
import unittest

from transformers.models.xlm.tokenization_xlm import VOCAB_FILES_NAMES, XLMTokenizer
from transformers.testing_utils import slow

from ...test_tokenization_common import TokenizerTesterMixin



class 							_UpperCamelCase					(	lowerCAmelCase_  ,						unittest.TestCase     ):
       _UpperCamelCase				:					Union[str, Any]         =   XLMTokenizer
       _UpperCamelCase				:					Optional[int]         =   False

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




              """simple docstring"""


              super().setUp()

              # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
              UpperCamelCase_          =  [
                  "l",
                  "o",
                  "w",
                  "e",
                  "r",
                  "s",
                  "t",
                  "i",
                  "d",
                  "n",
                  "w</w>",
                  "r</w>",
                  "t</w>",
                  "lo",
                  "low",
                  "er</w>",
                  "low</w>",
                  "lowest</w>",
                  "newer</w>",
                  "wider</w>",
                  "<unk>",
              ]
              UpperCamelCase_          =  dict(zip(_SCREAMING_SNAKE_CASE					,   range(len(_SCREAMING_SNAKE_CASE		)		)		)		)
              UpperCamelCase_          =  ["l o 123", "lo w 1456", "e r</w> 1789", ""]

              UpperCamelCase_          =  os.path.join(self.tmpdirname					,   VOCAB_FILES_NAMES["vocab_file"]		)
              UpperCamelCase_          =  os.path.join(self.tmpdirname					,   VOCAB_FILES_NAMES["merges_file"]		)
              with open(self.vocab_file					,   "w"		) as fp:
                     fp.write(json.dumps(_SCREAMING_SNAKE_CASE		)		)
              with open(self.merges_file					,   "w"		) as fp:
                     fp.write("\n".join(_SCREAMING_SNAKE_CASE		)		)

       def lowercase (		self: Optional[Any]					,   _SCREAMING_SNAKE_CASE: List[str]		)       -> str:




              """simple docstring"""


              UpperCamelCase_          =  "lower newer"
              UpperCamelCase_          =  "lower newer"
              return input_text, output_text

       def lowercase (		self: int		)       -> Tuple:




              """simple docstring"""


              UpperCamelCase_          =  XLMTokenizer(self.vocab_file					,   self.merges_file		)

              UpperCamelCase_          =  "lower"
              UpperCamelCase_          =  ["low", "er</w>"]
              UpperCamelCase_          =  tokenizer.tokenize(_SCREAMING_SNAKE_CASE		)
              self.assertListEqual(_SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE		)

              UpperCamelCase_          =  tokens + ["<unk>"]
              UpperCamelCase_          =  [14, 15, 20]
              self.assertListEqual(tokenizer.convert_tokens_to_ids(_SCREAMING_SNAKE_CASE		)					,   _SCREAMING_SNAKE_CASE		)



       @slow
       def lowercase (		self: Optional[int]		)       -> Tuple:




              """simple docstring"""


              UpperCamelCase_          =  XLMTokenizer.from_pretrained("xlm-mlm-en-2048"		)

              UpperCamelCase_          =  tokenizer.encode("sequence builders"					,   add_special_tokens=_SCREAMING_SNAKE_CASE		)
              UpperCamelCase_          =  tokenizer.encode("multi-sequence build"					,   add_special_tokens=_SCREAMING_SNAKE_CASE		)

              UpperCamelCase_          =  tokenizer.build_inputs_with_special_tokens(_SCREAMING_SNAKE_CASE		)
              UpperCamelCase_          =  tokenizer.build_inputs_with_special_tokens(_SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE		)

              assert encoded_sentence == [0] + text + [1]
              assert encoded_pair == [0] + text + [1] + text_a + [1]

