




'''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 		SCREAMING_SNAKE_CASE__  :





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



						"""simple docstring"""




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





			def 							A (      self						:    Tuple							):



						"""simple docstring"""




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

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

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

						__snake_case			=		self.get_config()

						return config, input_ids, tf.convert_to_tensor(a_							)





			def 							A (      self						:    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 							A (      self						:    Optional[int]      ,		a_						:    Union[str, Any]      ,		a_						:    List[str]      ,		a_						:    str							):



						"""simple docstring"""




						__snake_case			=		TFBlipTextModel(config=a_							)
						__snake_case			=		model(a_      ,		attention_mask=a_      ,		training=a_							)
						__snake_case			=		model(a_      ,		training=a_							)
						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 							A (      self						:    str							):



						"""simple docstring"""




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







@require_tf
class 		SCREAMING_SNAKE_CASE__  (			_UpperCamelCase				,     unittest.TestCase	):
			__SCREAMING_SNAKE_CASE    =			(TFBlipTextModel,) if is_tf_available() else ()
			__SCREAMING_SNAKE_CASE    =			False
			__SCREAMING_SNAKE_CASE    =			False
			__SCREAMING_SNAKE_CASE    =			False





			def 							A (      self						:    Optional[Any]							):



						"""simple docstring"""




						__snake_case			=		BlipTextModelTester(self							)
						__snake_case			=		ConfigTester(self      ,		config_class=a_      ,		hidden_size=37							)





			def 							A (      self						:    Dict							):



						"""simple docstring"""




						self.config_tester.run_common_tests()





			def 							A (      self						:    Tuple							):



						"""simple docstring"""




						__snake_case			=		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*a_							)





			def 							A (      self						:    str							):



						"""simple docstring"""




						pass





			def 							A (      self						:    List[Any]							):



						"""simple docstring"""




						pass





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



						"""simple docstring"""




						pass





			@unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING"							)
			def 							A (      self						:    Optional[Any]							):



						"""simple docstring"""




						pass





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



						"""simple docstring"""




						pass





			@slow
			def 							A (      self						:    Optional[int]							):



						"""simple docstring"""




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






			def 							A (      self						:    Optional[int]      ,		a_						:    int=True							):



						"""simple docstring"""




						super().test_pt_tf_model_equivalence(allow_missing_keys=a_							)












from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import convert_to_rgb, normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    OPENAI_CLIP_MEAN,
    OPENAI_CLIP_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
		import PIL


_lowercase		:  Tuple			       =logging.get_logger(__name__)






class 						UpperCamelCase_  (  snake_case__							):
					_a      :						List[str]						    =					['pixel_values']



					def __init__(		self     :     List[Any]       ,      lowerCamelCase     :     bool = True       ,      lowerCamelCase     :     Dict[str, int] = None       ,      lowerCamelCase     :     PILImageResampling = PILImageResampling.BICUBIC       ,      lowerCamelCase     :     bool = True       ,      lowerCamelCase     :     Union[int, float] = 1 / 2_55       ,      lowerCamelCase     :     bool = True       ,      lowerCamelCase     :     Optional[Union[float, List[float]]] = None       ,      lowerCamelCase     :     Optional[Union[float, List[float]]] = None       ,      lowerCamelCase     :     bool = True       ,      **lowerCamelCase     :     Optional[Any]       ,      ):
							super().__init__(**lowerCamelCase						)
							lowerCamelCase_	:     List[Any] 						=      size if size is not None else {'height': 3_84, 'width': 3_84}
							lowerCamelCase_	:     List[str] 						=      get_size_dict(lowerCamelCase       ,      default_to_square=lowerCamelCase						)

							lowerCamelCase_	:     Dict 						=      do_resize
							lowerCamelCase_	:     int 						=      size
							lowerCamelCase_	:     Union[str, Any] 						=      resample
							lowerCamelCase_	:     Tuple 						=      do_rescale
							lowerCamelCase_	:     Dict 						=      rescale_factor
							lowerCamelCase_	:     int 						=      do_normalize
							lowerCamelCase_	:     Any 						=      image_mean if image_mean is not None else OPENAI_CLIP_MEAN
							lowerCamelCase_	:     str 						=      image_std if image_std is not None else OPENAI_CLIP_STD
							lowerCamelCase_	:     int 						=      do_convert_rgb



					def   __a     (		self     :     Tuple       ,      lowerCamelCase     :     np.ndarray       ,      lowerCamelCase     :     Dict[str, int]       ,      lowerCamelCase     :     PILImageResampling = PILImageResampling.BICUBIC       ,      lowerCamelCase     :     Optional[Union[str, ChannelDimension]] = None       ,      **lowerCamelCase     :     List[Any]       ,      ):
							lowerCamelCase_	:     List[Any] 						=      get_size_dict(lowerCamelCase       ,      default_to_square=lowerCamelCase						)
							if "height" not in size or "width" not in size:
									raise ValueError(F"The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}"						)
							lowerCamelCase_	:     Optional[Any] 						=      (size['height'], size['width'])
							return resize(lowerCamelCase       ,      size=lowerCamelCase       ,      resample=lowerCamelCase       ,      data_format=lowerCamelCase       ,      **lowerCamelCase						)



					def   __a     (		self     :     Union[str, Any]       ,      lowerCamelCase     :     np.ndarray       ,      lowerCamelCase     :     Union[int, float]       ,      lowerCamelCase     :     Optional[Union[str, ChannelDimension]] = None       ,      **lowerCamelCase     :     Union[str, Any]       ,      ):
							return rescale(lowerCamelCase       ,      scale=lowerCamelCase       ,      data_format=lowerCamelCase       ,      **lowerCamelCase						)



					def   __a     (		self     :     str       ,      lowerCamelCase     :     np.ndarray       ,      lowerCamelCase     :     Union[float, List[float]]       ,      lowerCamelCase     :     Union[float, List[float]]       ,      lowerCamelCase     :     Optional[Union[str, ChannelDimension]] = None       ,      **lowerCamelCase     :     Optional[int]       ,      ):
							return normalize(lowerCamelCase       ,      mean=lowerCamelCase       ,      std=lowerCamelCase       ,      data_format=lowerCamelCase       ,      **lowerCamelCase						)





					def   __a     (		self     :     List[Any]       ,      lowerCamelCase     :     ImageInput       ,      lowerCamelCase     :     Optional[bool] = None       ,      lowerCamelCase     :     Optional[Dict[str, int]] = None       ,      lowerCamelCase     :     PILImageResampling = None       ,      lowerCamelCase     :     Optional[bool] = None       ,      lowerCamelCase     :     Optional[float] = None       ,      lowerCamelCase     :     Optional[bool] = None       ,      lowerCamelCase     :     Optional[Union[float, List[float]]] = None       ,      lowerCamelCase     :     Optional[Union[float, List[float]]] = None       ,      lowerCamelCase     :     Optional[Union[str, TensorType]] = None       ,      lowerCamelCase     :     bool = None       ,      lowerCamelCase     :     ChannelDimension = ChannelDimension.FIRST       ,      **lowerCamelCase     :     Any       ,      ):
							lowerCamelCase_	:     Optional[Any] 						=      do_resize if do_resize is not None else self.do_resize
							lowerCamelCase_	:     int 						=      resample if resample is not None else self.resample
							lowerCamelCase_	:     Dict 						=      do_rescale if do_rescale is not None else self.do_rescale
							lowerCamelCase_	:     Any 						=      rescale_factor if rescale_factor is not None else self.rescale_factor
							lowerCamelCase_	:     str 						=      do_normalize if do_normalize is not None else self.do_normalize
							lowerCamelCase_	:     List[str] 						=      image_mean if image_mean is not None else self.image_mean
							lowerCamelCase_	:     str 						=      image_std if image_std is not None else self.image_std
							lowerCamelCase_	:     int 						=      do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

							lowerCamelCase_	:     Union[str, Any] 						=      size if size is not None else self.size
							lowerCamelCase_	:     Optional[int] 						=      get_size_dict(lowerCamelCase       ,      default_to_square=lowerCamelCase						)

							lowerCamelCase_	:     Dict 						=      make_list_of_images(lowerCamelCase						)

							if not valid_images(lowerCamelCase						):
									raise ValueError(
									    'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '
									    'torch.Tensor, tf.Tensor or jax.ndarray.'						)

							if do_resize and size is None or resample is None:
									raise ValueError('Size and resample must be specified if do_resize is True.'						)

							if do_rescale and rescale_factor is None:
									raise ValueError('Rescale factor must be specified if do_rescale is True.'						)

							if do_normalize and (image_mean is None or image_std is None):
									raise ValueError('Image mean and std must be specified if do_normalize is True.'						)

							# PIL RGBA images are converted to RGB
							if do_convert_rgb:
									lowerCamelCase_	:     int 						=      [convert_to_rgb(lowerCamelCase						) for image in images]

							# All transformations expect numpy arrays.
							lowerCamelCase_	:     List[Any] 						=      [to_numpy_array(lowerCamelCase						) for image in images]

							if do_resize:
									lowerCamelCase_	:     Optional[int] 						=      [self.resize(image=lowerCamelCase       ,      size=lowerCamelCase       ,      resample=lowerCamelCase						) for image in images]

							if do_rescale:
									lowerCamelCase_	:     Dict 						=      [self.rescale(image=lowerCamelCase       ,      scale=lowerCamelCase						) for image in images]

							if do_normalize:
									lowerCamelCase_	:     Optional[int] 						=      [self.normalize(image=lowerCamelCase       ,      mean=lowerCamelCase       ,      std=lowerCamelCase						) for image in images]

							lowerCamelCase_	:     Optional[int] 						=      [to_channel_dimension_format(lowerCamelCase       ,      lowerCamelCase						) for image in images]

							lowerCamelCase_	:     Optional[int] 						=      BatchFeature(data={'pixel_values': images}       ,      tensor_type=lowerCamelCase						)

							return encoded_outputs
