





"""simple docstring"""
import unittest

import numpy as np

from transformers import BlipTextConfig
from transformers.testing_utils import require_torch, slow, torch_device
from transformers.utils import is_torch_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask


if is_torch_available():
		import torch

		from transformers import BlipTextModel
		from transformers.models.blip.modeling_blip import BLIP_PRETRAINED_MODEL_ARCHIVE_LIST






class    _UpperCAmelCase  :


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


					def       lowerCamelCase			(   self     :Optional[int]						):
												A						=    ids_tensor([self.batch_size, self.seq_length]	,			self.vocab_size						)

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

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

												A						=    self.get_config()

												return config, input_ids, input_mask


					def       lowerCamelCase			(   self     :Union[str, Any]						):
												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       lowerCamelCase			(   self     :str	,			__UpperCamelCase     :List[Any]	,			__UpperCamelCase     :Optional[Any]	,			__UpperCamelCase     :List[Any]						):
												A						=    BlipTextModel(config=__UpperCamelCase						)
												model.to(__UpperCamelCase						)
												model.eval()
												with torch.no_grad():
																			A						=    model(__UpperCamelCase	,			attention_mask=__UpperCamelCase						)
																			A						=    model(__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       lowerCamelCase			(   self     :str						):
												A						=    self.prepare_config_and_inputs()
												A,      A,      A						=    config_and_inputs
												A						=    {"input_ids": input_ids, "attention_mask": input_mask}
												return config, inputs_dict







@require_torch
class    _UpperCAmelCase  (      lowercase_	,				unittest.TestCase					):
					UpperCamelCase						=				(BlipTextModel,) if is_torch_available() else ()
					UpperCamelCase						=				False
					UpperCamelCase						=				False
					UpperCamelCase						=				False


					def       lowerCamelCase			(   self     :List[str]						):
												A						=    BlipTextModelTester(self						)
												A						=    ConfigTester(self	,			config_class=__UpperCamelCase	,			hidden_size=37						)


					def       lowerCamelCase			(   self     :List[str]						):
												self.config_tester.run_common_tests()


					def       lowerCamelCase			(   self     :List[str]						):
												A						=    self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*__UpperCamelCase						)


					def       lowerCamelCase			(   self     :List[Any]						):
												pass


					def       lowerCamelCase			(   self     :Optional[int]						):
												pass


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


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


					@unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING"						)
					def       lowerCamelCase			(   self     :Union[str, Any]						):
												pass


					@slow
					def       lowerCamelCase			(   self     :List[str]						):
												for model_name in BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			A						=    BlipTextModel.from_pretrained(__UpperCamelCase						)
																			self.assertIsNotNone(__UpperCamelCase						)


					def       lowerCamelCase			(   self     :str						):
												super().test_pt_tf_model_equivalence(allow_missing_keys=__UpperCamelCase						)









"""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


if is_torch_available():
		import torch

if is_vision_available():
		from PIL import Image

		from transformers import BlipImageProcessor






class    _UpperCAmelCase  (      unittest.TestCase					):


					def __init__(   self     :Optional[Any]	,			__UpperCamelCase     :Optional[int]	,			__UpperCamelCase     :Dict=7	,			__UpperCamelCase     :Optional[Any]=3	,			__UpperCamelCase     :Tuple=18	,			__UpperCamelCase     :List[Any]=30	,			__UpperCamelCase     :Optional[Any]=4_00	,			__UpperCamelCase     :Union[str, Any]=True	,			__UpperCamelCase     :Tuple=None	,			__UpperCamelCase     :List[str]=True	,			__UpperCamelCase     :Any=False	,			__UpperCamelCase     :str=[0.48_145_466, 0.4_578_275, 0.40_821_073]	,			__UpperCamelCase     :Optional[Any]=[0.26_862_954, 0.26_130_258, 0.27_577_711]	,			__UpperCamelCase     :Optional[Any]=True	,			):
												A						=    size if size is not None else {"height": 20, "width": 20}
												A						=    parent
												A						=    batch_size
												A						=    num_channels
												A						=    image_size
												A						=    min_resolution
												A						=    max_resolution
												A						=    do_resize
												A						=    size
												A						=    do_normalize
												A						=    image_mean
												A						=    image_std
												A						=    do_pad
												A						=    do_convert_rgb


					def       lowerCamelCase			(   self     :List[str]						):
												return {
												    "do_resize": self.do_resize,
												    "size": self.size,
												    "do_normalize": self.do_normalize,
												    "image_mean": self.image_mean,
												    "image_std": self.image_std,
												    "do_convert_rgb": self.do_convert_rgb,
												    "do_pad": self.do_pad,
												}


					def       lowerCamelCase			(   self     :Optional[Any]	,			__UpperCamelCase     :Optional[int]=False	,			__UpperCamelCase     :Dict=False	,			__UpperCamelCase     :Dict=False						):
												assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

												if equal_resolution:
																			A						=    []
																			for i in range(self.batch_size						):
																										image_inputs.append(
																										    np.random.randint(
																										        2_55	,			size=(self.num_channels, self.max_resolution, self.max_resolution)	,			dtype=np.uinta						)						)
												else:
																			A						=    []
																			for i in range(self.batch_size						):
																										A,      A						=    np.random.choice(np.arange(self.min_resolution	,			self.max_resolution						)	,			2						)
																										image_inputs.append(np.random.randint(2_55	,			size=(self.num_channels, width, height)	,			dtype=np.uinta						)						)

												if not numpify and not torchify:
																			# PIL expects the channel dimension as last dimension
																			A						=    [Image.fromarray(np.moveaxis(__UpperCamelCase	,			0	,			-1						)						) for x in image_inputs]

												if torchify:
																			A						=    [torch.from_numpy(__UpperCamelCase						) for x in image_inputs]

												return image_inputs







@require_torch
@require_vision
class    _UpperCAmelCase  (      lowercase_	,				unittest.TestCase					):
					UpperCamelCase						=				BlipImageProcessor if is_vision_available() else None


					def       lowerCamelCase			(   self     :Optional[Any]						):
												A						=    BlipImageProcessingTester(self						)


					@property
					def       lowerCamelCase			(   self     :List[str]						):
												return self.image_processor_tester.prepare_image_processor_dict()


					def       lowerCamelCase			(   self     :Any						):
												A						=    self.image_processing_class(**self.image_processor_dict						)
												self.assertTrue(hasattr(__UpperCamelCase	,			"do_resize"						)						)
												self.assertTrue(hasattr(__UpperCamelCase	,			"size"						)						)
												self.assertTrue(hasattr(__UpperCamelCase	,			"do_normalize"						)						)
												self.assertTrue(hasattr(__UpperCamelCase	,			"image_mean"						)						)
												self.assertTrue(hasattr(__UpperCamelCase	,			"image_std"						)						)
												self.assertTrue(hasattr(__UpperCamelCase	,			"do_convert_rgb"						)						)


					def       lowerCamelCase			(   self     :List[Any]						):
												pass


					def       lowerCamelCase			(   self     :Optional[Any]						):
												# Initialize image_processor
												A						=    self.image_processing_class(**self.image_processor_dict						)
												# create random PIL images
												A						=    self.image_processor_tester.prepare_inputs(equal_resolution=__UpperCamelCase						)
												for image in image_inputs:
																			self.assertIsInstance(__UpperCamelCase	,			Image.Image						)

												# Test not batched input
												A						=    image_processor(image_inputs[0]	,			return_tensors="pt"						).pixel_values
												self.assertEqual(
												    encoded_images.shape	,			(
												        1,
												        self.image_processor_tester.num_channels,
												        self.image_processor_tester.size["height"],
												        self.image_processor_tester.size["width"],
												    )	,			)

												# Test batched
												A						=    image_processor(__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.size["height"],
												        self.image_processor_tester.size["width"],
												    )	,			)


					def       lowerCamelCase			(   self     :Union[str, Any]						):
												# Initialize image_processor
												A						=    self.image_processing_class(**self.image_processor_dict						)
												# create random numpy tensors
												A						=    self.image_processor_tester.prepare_inputs(equal_resolution=__UpperCamelCase	,			numpify=__UpperCamelCase						)
												for image in image_inputs:
																			self.assertIsInstance(__UpperCamelCase	,			np.ndarray						)

												# Test not batched input
												A						=    image_processor(image_inputs[0]	,			return_tensors="pt"						).pixel_values
												self.assertEqual(
												    encoded_images.shape	,			(
												        1,
												        self.image_processor_tester.num_channels,
												        self.image_processor_tester.size["height"],
												        self.image_processor_tester.size["width"],
												    )	,			)

												# Test batched
												A						=    image_processor(__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.size["height"],
												        self.image_processor_tester.size["width"],
												    )	,			)


					def       lowerCamelCase			(   self     :Tuple						):
												# Initialize image_processor
												A						=    self.image_processing_class(**self.image_processor_dict						)
												# create random PyTorch tensors
												A						=    self.image_processor_tester.prepare_inputs(equal_resolution=__UpperCamelCase	,			torchify=__UpperCamelCase						)
												for image in image_inputs:
																			self.assertIsInstance(__UpperCamelCase	,			torch.Tensor						)

												# Test not batched input
												A						=    image_processor(image_inputs[0]	,			return_tensors="pt"						).pixel_values
												self.assertEqual(
												    encoded_images.shape	,			(
												        1,
												        self.image_processor_tester.num_channels,
												        self.image_processor_tester.size["height"],
												        self.image_processor_tester.size["width"],
												    )	,			)

												# Test batched
												A						=    image_processor(__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.size["height"],
												        self.image_processor_tester.size["width"],
												    )	,			)







@require_torch
@require_vision
class    _UpperCAmelCase  (      lowercase_	,				unittest.TestCase					):
					UpperCamelCase						=				BlipImageProcessor if is_vision_available() else None


					def       lowerCamelCase			(   self     :int						):
												A						=    BlipImageProcessingTester(self	,			num_channels=4						)
												A						=    3


					@property
					def       lowerCamelCase			(   self     :Optional[Any]						):
												return self.image_processor_tester.prepare_image_processor_dict()


					def       lowerCamelCase			(   self     :int						):
												A						=    self.image_processing_class(**self.image_processor_dict						)
												self.assertTrue(hasattr(__UpperCamelCase	,			"do_resize"						)						)
												self.assertTrue(hasattr(__UpperCamelCase	,			"size"						)						)
												self.assertTrue(hasattr(__UpperCamelCase	,			"do_normalize"						)						)
												self.assertTrue(hasattr(__UpperCamelCase	,			"image_mean"						)						)
												self.assertTrue(hasattr(__UpperCamelCase	,			"image_std"						)						)
												self.assertTrue(hasattr(__UpperCamelCase	,			"do_convert_rgb"						)						)


					def       lowerCamelCase			(   self     :Any						):
												pass


					def       lowerCamelCase			(   self     :Optional[Any]						):
												# Initialize image_processor
												A						=    self.image_processing_class(**self.image_processor_dict						)
												# create random PIL images
												A						=    self.image_processor_tester.prepare_inputs(equal_resolution=__UpperCamelCase						)
												for image in image_inputs:
																			self.assertIsInstance(__UpperCamelCase	,			Image.Image						)

												# Test not batched input
												A						=    image_processor(image_inputs[0]	,			return_tensors="pt"						).pixel_values
												self.assertEqual(
												    encoded_images.shape	,			(
												        1,
												        self.expected_encoded_image_num_channels,
												        self.image_processor_tester.size["height"],
												        self.image_processor_tester.size["width"],
												    )	,			)

												# Test batched
												A						=    image_processor(__UpperCamelCase	,			return_tensors="pt"						).pixel_values
												self.assertEqual(
												    encoded_images.shape	,			(
												        self.image_processor_tester.batch_size,
												        self.expected_encoded_image_num_channels,
												        self.image_processor_tester.size["height"],
												        self.image_processor_tester.size["width"],
												    )	,			)


