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



   def __init__(    self					,      lowerCAmelCase__					,      lowerCAmelCase__=12					,      lowerCAmelCase__=7					,      lowerCAmelCase__=True					,      lowerCAmelCase__=True					,      lowerCAmelCase__=True					,      lowerCAmelCase__=99					,      lowerCAmelCase__=32					,      lowerCAmelCase__=32					,      lowerCAmelCase__=5					,      lowerCAmelCase__=4					,      lowerCAmelCase__=37					,      lowerCAmelCase__=0.1					,      lowerCAmelCase__=0.1					,      lowerCAmelCase__=512					,      lowerCAmelCase__=0.02					,      lowerCAmelCase__=0					,      lowerCAmelCase__=None					,      )				->     Optional[int]:
        """simple docstring"""





        _UpperCamelCase  :Optional[int]       =parent
        _UpperCamelCase  :List[str]       =batch_size
        _UpperCamelCase  :Tuple       =seq_length
        _UpperCamelCase  :int       =is_training
        _UpperCamelCase  :Optional[Any]       =use_input_mask
        _UpperCamelCase  :Any       =use_labels
        _UpperCamelCase  :List[Any]       =vocab_size
        _UpperCamelCase  :Optional[Any]       =hidden_size
        _UpperCamelCase  :str       =projection_dim
        _UpperCamelCase  :Tuple       =num_hidden_layers
        _UpperCamelCase  :int       =num_attention_heads
        _UpperCamelCase  :Optional[int]       =intermediate_size
        _UpperCamelCase  :List[Any]       =dropout
        _UpperCamelCase  :Dict       =attention_dropout
        _UpperCamelCase  :Optional[int]       =max_position_embeddings
        _UpperCamelCase  :Optional[Any]       =initializer_range
        _UpperCamelCase  :Optional[Any]       =scope
        _UpperCamelCase  :Tuple       =bos_token_id



   def  _UpperCamelCase		(    self     )				->     Optional[Any]:
        """simple docstring"""





        _UpperCamelCase  :Any       =ids_tensor([self.batch_size, self.seq_length]					,      self.vocab_size     )

        _UpperCamelCase  :List[str]       =None
        if self.use_input_mask:
             _UpperCamelCase  :Union[str, Any]       =random_attention_mask([self.batch_size, self.seq_length]     )

        if input_mask is not None:
             _UpperCamelCase       ,      _UpperCamelCase  :Tuple       =input_mask.shape
             _UpperCamelCase  :str       =np.random.randint(1					,      seq_length - 1					,      size=(batch_size,)     )
             for batch_idx, start_index in enumerate(lowerCAmelCase__     ):
                  _UpperCamelCase  :Optional[Any]       =1
                  _UpperCamelCase  :Tuple       =0

        _UpperCamelCase  :Any       =self.get_config()

        return config, input_ids, input_mask



   def  _UpperCamelCase		(    self     )				->     Dict:
        """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  _UpperCamelCase		(    self					,      lowerCAmelCase__					,      lowerCAmelCase__					,      lowerCAmelCase__     )				->     Tuple:
        """simple docstring"""





        _UpperCamelCase  :Tuple       =BlipTextModel(config=lowerCAmelCase__     )
        model.to(lowerCAmelCase__     )
        model.eval()
        with torch.no_grad():
             _UpperCamelCase  :Union[str, Any]       =model(lowerCAmelCase__					,      attention_mask=lowerCAmelCase__     )
             _UpperCamelCase  :Optional[Any]       =model(lowerCAmelCase__     )
        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  _UpperCamelCase		(    self     )				->     Dict:
        """simple docstring"""





        _UpperCamelCase  :str       =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_torch
class    lowerCamelCase__  (   __snake_case       ,				unittest.TestCase     ):
   __UpperCAmelCase           =						(BlipTextModel,) if is_torch_available() else ()
   __UpperCAmelCase           =						False
   __UpperCAmelCase           =						False
   __UpperCAmelCase           =						False



   def  _UpperCamelCase		(    self     )				->     Optional[Any]:
        """simple docstring"""





        _UpperCamelCase  :int       =BlipTextModelTester(self     )
        _UpperCamelCase  :int       =ConfigTester(self					,      config_class=lowerCAmelCase__					,      hidden_size=37     )



   def  _UpperCamelCase		(    self     )				->     int:
        """simple docstring"""





        self.config_tester.run_common_tests()



   def  _UpperCamelCase		(    self     )				->     List[str]:
        """simple docstring"""





        _UpperCamelCase  :Optional[int]       =self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowerCAmelCase__     )



   def  _UpperCamelCase		(    self     )				->     Tuple:
        """simple docstring"""





        pass



   def  _UpperCamelCase		(    self     )				->     Any:
        """simple docstring"""





        pass



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





        pass



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





        pass



   @unittest.skip(reason="""BlipTextModel has no base class and is not available in MODEL_MAPPING"""     )
   def  _UpperCamelCase		(    self     )				->     Dict:
        """simple docstring"""





        pass



   @slow
   def  _UpperCamelCase		(    self     )				->     List[str]:
        """simple docstring"""





        for model_name in BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             _UpperCamelCase  :List[Any]       =BlipTextModel.from_pretrained(lowerCAmelCase__     )
             self.assertIsNotNone(lowerCAmelCase__     )




   def  _UpperCamelCase		(    self     )				->     Dict:
        """simple docstring"""





        super().test_pt_tf_model_equivalence(allow_missing_keys=lowerCAmelCase__     )






'''simple docstring'''

from typing import List

import PIL
import torch
from PIL import Image

from ...configuration_utils import ConfigMixin
from ...models.modeling_utils import ModelMixin
from ...utils import PIL_INTERPOLATION


class      SCREAMING_SNAKE_CASE__      (   snake_case__							,snake_case__		):







					"""simple docstring"""






					def __init__(   self       :      Any		):







							"""simple docstring"""
							super().__init__()

							self.register_buffer("watermark_image"		,	torch.zeros((62, 62, 4)		)		)
							__UpperCAmelCase	:							Optional[int]     =     None






					def 		lowerCamelCase_   (   self       :      List[Any]		,	UpperCAmelCase_       :      List[PIL.Image.Image]		,	UpperCAmelCase_       :      int=None		):







							"""simple docstring"""
							# copied from https://github.com/deep-floyd/IF/blob/b77482e36ca2031cb94dbca1001fc1e6400bf4ab/deepfloyd_if/modules/base.py#L287

							__UpperCAmelCase	:							int     =     images[0].height
							__UpperCAmelCase	:							Any     =     images[0].width

							__UpperCAmelCase	:							Union[str, Any]     =     sample_size or h

							__UpperCAmelCase	:							Dict     =     min(h / sample_size		,	w / sample_size		)
							__UpperCAmelCase  ,			__UpperCAmelCase	:							Optional[int]     =     (int(h / coef		), int(w / coef		)) if coef < 1 else (h, w)

							__UpperCAmelCase  ,			__UpperCAmelCase	:							Union[str, Any]     =     1_024**2, img_w * img_h
							__UpperCAmelCase	:							Optional[int]     =     (Sa / Sa) ** 0.5
							__UpperCAmelCase  ,			__UpperCAmelCase  ,			__UpperCAmelCase	:							str     =     int(K * 62		), img_w - int(14 * K		), img_h - int(14 * K		)

							if self.watermark_image_as_pil is None:
									__UpperCAmelCase	:							int     =     self.watermark_image.to(torch.uinta		).cpu().numpy()
									__UpperCAmelCase	:							Optional[int]     =     Image.fromarray(UpperCAmelCase_		,	mode="RGBA"		)
									__UpperCAmelCase	:							Optional[int]     =     watermark_image

							__UpperCAmelCase	:							List[str]     =     self.watermark_image_as_pil.resize(
							    (wm_size, wm_size)		,	PIL_INTERPOLATION["bicubic"]		,	reducing_gap=UpperCAmelCase_		)

							for pil_img in images:
									pil_img.paste(UpperCAmelCase_		,	box=(wm_x - wm_size, wm_y - wm_size, wm_x, wm_y)		,	mask=wm_img.split()[-1]		)

							return images




