'''simple docstring'''




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






       def __init__(      self				,     UpperCamelCase_				,     UpperCamelCase_=12				,     UpperCamelCase_=7				,     UpperCamelCase_=True				,     UpperCamelCase_=True				,     UpperCamelCase_=True				,     UpperCamelCase_=99				,     UpperCamelCase_=32				,     UpperCamelCase_=32				,     UpperCamelCase_=2				,     UpperCamelCase_=4				,     UpperCamelCase_=37				,     UpperCamelCase_=0.1				,     UpperCamelCase_=0.1				,     UpperCamelCase_=5_12				,     UpperCamelCase_=0.0_2				,     UpperCamelCase_=0				,     UpperCamelCase_=None				,     ):
              __UpperCAmelCase  :				Union[str, Any]       				=		parent
              __UpperCAmelCase  :				Union[str, Any]       				=		batch_size
              __UpperCAmelCase  :				Optional[Any]       				=		seq_length
              __UpperCAmelCase  :				Dict       				=		is_training
              __UpperCAmelCase  :				List[Any]       				=		use_input_mask
              __UpperCAmelCase  :				Union[str, Any]       				=		use_labels
              __UpperCAmelCase  :				Optional[int]       				=		vocab_size
              __UpperCAmelCase  :				List[Any]       				=		hidden_size
              __UpperCAmelCase  :				Optional[Any]       				=		projection_dim
              __UpperCAmelCase  :				Dict       				=		num_hidden_layers
              __UpperCAmelCase  :				List[str]       				=		num_attention_heads
              __UpperCAmelCase  :				int       				=		intermediate_size
              __UpperCAmelCase  :				List[str]       				=		dropout
              __UpperCAmelCase  :				Union[str, Any]       				=		attention_dropout
              __UpperCAmelCase  :				Any       				=		max_position_embeddings
              __UpperCAmelCase  :				Union[str, Any]       				=		initializer_range
              __UpperCAmelCase  :				Optional[Any]       				=		scope
              __UpperCAmelCase  :				str       				=		bos_token_id






       def 			_snake_case						(      self      ):
              __UpperCAmelCase  :				Optional[Any]       				=		ids_tensor([self.batch_size, self.seq_length]				,     self.vocab_size      )

              __UpperCAmelCase  :				List[str]       				=		None
              if self.use_input_mask:
                     __UpperCAmelCase  :				Tuple       				=		random_attention_mask([self.batch_size, self.seq_length]      )

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

              __UpperCAmelCase  :				List[str]       				=		self.get_config()

              return config, input_ids, tf.convert_to_tensor(UpperCamelCase_      )






       def 			_snake_case						(      self      ):
              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 			_snake_case						(      self				,     UpperCamelCase_				,     UpperCamelCase_				,     UpperCamelCase_      ):
              __UpperCAmelCase  :				str       				=		TFBlipTextModel(config=UpperCamelCase_      )
              __UpperCAmelCase  :				List[str]       				=		model(UpperCamelCase_				,     attention_mask=UpperCamelCase_				,     training=UpperCamelCase_      )
              __UpperCAmelCase  :				Union[str, Any]       				=		model(UpperCamelCase_				,     training=UpperCamelCase_      )
              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 			_snake_case						(      self      ):
              __UpperCAmelCase  :				List[Any]       				=		self.prepare_config_and_inputs()
              __UpperCAmelCase				, __UpperCAmelCase				, __UpperCAmelCase  :				Union[str, Any]       				=		config_and_inputs
              __UpperCAmelCase  :				Tuple       				=		{"input_ids": input_ids, "attention_mask": input_mask}
              return config, inputs_dict






@require_tf
class 		__A       (__magic_name__ ,					unittest.TestCase   ):
       snake_case       :Optional[Any]				    =					(TFBlipTextModel,) if is_tf_available() else ()
       snake_case       :Any				    =					False
       snake_case       :Dict				    =					False
       snake_case       :Dict				    =					False






       def 			_snake_case						(      self      ):
              __UpperCAmelCase  :				Union[str, Any]       				=		BlipTextModelTester(self      )
              __UpperCAmelCase  :				Tuple       				=		ConfigTester(self				,     config_class=UpperCamelCase_				,     hidden_size=37      )






       def 			_snake_case						(      self      ):
              self.config_tester.run_common_tests()






       def 			_snake_case						(      self      ):
              __UpperCAmelCase  :				int       				=		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_model(*UpperCamelCase_      )






       def 			_snake_case						(      self      ):
              pass






       def 			_snake_case						(      self      ):
              pass






       @unittest.skip(reason="Blip does not use inputs_embeds"      )
       def 			_snake_case						(      self      ):
              pass






       @unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING"      )
       def 			_snake_case						(      self      ):
              pass






       @unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING"      )
       def 			_snake_case						(      self      ):
              pass






       @slow
       def 			_snake_case						(      self      ):
              for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                     __UpperCAmelCase  :				Tuple       				=		TFBlipTextModel.from_pretrained(UpperCamelCase_      )
                     self.assertIsNotNone(UpperCamelCase_      )







       def 			_snake_case						(      self				,     UpperCamelCase_=True      ):
              super().test_pt_tf_model_equivalence(allow_missing_keys=UpperCamelCase_      )







'''simple docstring'''




# Lint as: python3
import sys
from collections.abc import Mapping
from typing import TYPE_CHECKING

import numpy as np
import pyarrow as pa

from .. import config
from ..utils.py_utils import map_nested
from .formatting import TensorFormatter


if TYPE_CHECKING:
   import torch






class 		__A       (TensorFormatter[Mapping, "torch.Tensor", Mapping]   ):






       def __init__(      self				,     UpperCamelCase_=None				,     **UpperCamelCase_      ):
              super().__init__(features=UpperCamelCase_      )
              __UpperCAmelCase  :				Union[str, Any]       				=		torch_tensor_kwargs
              import torch  # noqa import torch at initialization






       def 			_snake_case						(      self				,     UpperCamelCase_      ):
              import torch

              if isinstance(UpperCamelCase_				,     UpperCamelCase_      ) and column:
                     if all(
                         isinstance(UpperCamelCase_				,     torch.Tensor      ) and x.shape == column[0].shape and x.dtype == column[0].dtype
                         for x in column      ):
                            return torch.stack(UpperCamelCase_      )
              return column






       def 			_snake_case						(      self				,     UpperCamelCase_      ):
              import torch

              if isinstance(UpperCamelCase_				,     (str, bytes, type(UpperCamelCase_      ))      ):
                     return value
              elif isinstance(UpperCamelCase_				,     (np.character, np.ndarray)      ) and np.issubdtype(value.dtype				,     np.character      ):
                     return value.tolist()

              __UpperCAmelCase  :				int       				=		{}

              if isinstance(UpperCamelCase_				,     (np.number, np.ndarray)      ) and np.issubdtype(value.dtype				,     np.integer      ):
                     __UpperCAmelCase  :				Optional[int]       				=		{"dtype": torch.intaa}
              elif isinstance(UpperCamelCase_				,     (np.number, np.ndarray)      ) and np.issubdtype(value.dtype				,     np.floating      ):
                     __UpperCAmelCase  :				str       				=		{"dtype": torch.floataa}
              elif config.PIL_AVAILABLE and "PIL" in sys.modules:
                     import PIL.Image

                     if isinstance(UpperCamelCase_				,     PIL.Image.Image      ):
                            __UpperCAmelCase  :				str       				=		np.asarray(UpperCamelCase_      )
              return torch.tensor(UpperCamelCase_				,     **{**default_dtype, **self.torch_tensor_kwargs}      )






       def 			_snake_case						(      self				,     UpperCamelCase_      ):
              import torch

              # support for torch, tf, jax etc.
              if hasattr(UpperCamelCase_				,     "__array__"      ) and not isinstance(UpperCamelCase_				,     torch.Tensor      ):
                     __UpperCAmelCase  :				Dict       				=		data_struct.__array__()
              # support for nested types like struct of list of struct
              if isinstance(UpperCamelCase_				,     np.ndarray      ):
                     if data_struct.dtype == object:  # torch tensors cannot be instantied from an array of objects
                            return self._consolidate([self.recursive_tensorize(UpperCamelCase_      ) for substruct in data_struct]      )
              elif isinstance(UpperCamelCase_				,     (list, tuple)      ):
                     return self._consolidate([self.recursive_tensorize(UpperCamelCase_      ) for substruct in data_struct]      )
              return self._tensorize(UpperCamelCase_      )






       def 			_snake_case						(      self				,     UpperCamelCase_      ):
              return map_nested(self._recursive_tensorize				,     UpperCamelCase_				,     map_list=UpperCamelCase_      )






       def 			_snake_case						(      self				,     UpperCamelCase_      ):
              __UpperCAmelCase  :				List[str]       				=		self.numpy_arrow_extractor().extract_row(UpperCamelCase_      )
              __UpperCAmelCase  :				Union[str, Any]       				=		self.python_features_decoder.decode_row(UpperCamelCase_      )
              return self.recursive_tensorize(UpperCamelCase_      )






       def 			_snake_case						(      self				,     UpperCamelCase_      ):
              __UpperCAmelCase  :				Union[str, Any]       				=		self.numpy_arrow_extractor().extract_column(UpperCamelCase_      )
              __UpperCAmelCase  :				Optional[Any]       				=		self.python_features_decoder.decode_column(UpperCamelCase_				,     pa_table.column_names[0]      )
              __UpperCAmelCase  :				List[Any]       				=		self.recursive_tensorize(UpperCamelCase_      )
              __UpperCAmelCase  :				List[str]       				=		self._consolidate(UpperCamelCase_      )
              return column







       def 			_snake_case						(      self				,     UpperCamelCase_      ):
              __UpperCAmelCase  :				int       				=		self.numpy_arrow_extractor().extract_batch(UpperCamelCase_      )
              __UpperCAmelCase  :				Any       				=		self.python_features_decoder.decode_batch(UpperCamelCase_      )
              __UpperCAmelCase  :				Optional[int]       				=		self.recursive_tensorize(UpperCamelCase_      )
              for column_name in batch:
                     __UpperCAmelCase  :				Tuple       				=		self._consolidate(batch[column_name]      )
              return batch






