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


  '''simple docstring'''







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







    """simple docstring"""






    __magic_name__    					=	parent
    __magic_name__    					=	batch_size
    __magic_name__    					=	seq_length
    __magic_name__    					=	is_training
    __magic_name__    					=	use_input_mask
    __magic_name__    					=	use_labels
    __magic_name__    					=	vocab_size
    __magic_name__    					=	hidden_size
    __magic_name__    					=	projection_dim
    __magic_name__    					=	num_hidden_layers
    __magic_name__    					=	num_attention_heads
    __magic_name__    					=	intermediate_size
    __magic_name__    					=	dropout
    __magic_name__    					=	attention_dropout
    __magic_name__    					=	max_position_embeddings
    __magic_name__    					=	initializer_range
    __magic_name__    					=	scope
    __magic_name__    					=	bos_token_id
  def 							_lowercase							(			self      :			Union[str, Any]   )      ->     Optional[int]:







    """simple docstring"""






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

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

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

    __magic_name__    					=	self.get_config()

    return config, input_ids, input_mask
  def 							_lowercase							(			self      :			Union[str, Any]   )      ->     Union[str, Any]:







    """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      :			Tuple      ,							UpperCamelCase__      :			Optional[int]      ,							UpperCamelCase__      :			Any      ,							UpperCamelCase__      :			str   )      ->     List[Any]:







    """simple docstring"""






    __magic_name__    					=	BlipTextModel(config=_UpperCAmelCase   )
    model.to(_UpperCAmelCase   )
    model.eval()
    with torch.no_grad():
      __magic_name__    					=	model(_UpperCAmelCase      ,							attention_mask=_UpperCAmelCase   )
      __magic_name__    					=	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 							_lowercase							(			self      :			Tuple   )      ->     int:







    """simple docstring"""






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







@require_torch
class 	UpperCAmelCase_      (			lowerCAmelCase__  ,	unittest.TestCase					):


  '''simple docstring'''







  a__   	=		(BlipTextModel,) if is_torch_available() else ()
  a__   	=		False
  a__   	=		False
  a__   	=		False
  def 							_lowercase							(			self      :			int   )      ->     str:







    """simple docstring"""






    __magic_name__    					=	BlipTextModelTester(self   )
    __magic_name__    					=	ConfigTester(self      ,							config_class=_UpperCAmelCase      ,							hidden_size=37   )
  def 							_lowercase							(			self      :			Union[str, Any]   )      ->     Union[str, Any]:







    """simple docstring"""






    self.config_tester.run_common_tests()
  def 							_lowercase							(			self      :			int   )      ->     Union[str, Any]:







    """simple docstring"""






    __magic_name__    					=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*_UpperCAmelCase   )
  def 							_lowercase							(			self      :			Optional[int]   )      ->     Any:







    """simple docstring"""






    pass
  def 							_lowercase							(			self      :			List[Any]   )      ->     Tuple:







    """simple docstring"""






    pass
  @unittest.skip(reason="""Blip does not use inputs_embeds"""   )
  def 							_lowercase							(			self      :			List[str]   )      ->     Optional[Any]:







    """simple docstring"""






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







    """simple docstring"""






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







    """simple docstring"""






    pass
  @slow
  def 							_lowercase							(			self      :			Optional[int]   )      ->     Any:







    """simple docstring"""






    for model_name in BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
      __magic_name__    					=	BlipTextModel.from_pretrained(_UpperCAmelCase   )
      self.assertIsNotNone(_UpperCAmelCase   )





  def 							_lowercase							(			self      :			List[Any]   )      ->     int:







    """simple docstring"""






    super().test_pt_tf_model_equivalence(allow_missing_keys=_UpperCAmelCase   )



import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
    import transformer_engine.pytorch as te

def a__   (	A_,					A_=True,					A_=True,					A_=True ):






  '''simple docstring'''





  if not is_fpa_available():
    raise ImportError("""Using `convert_model` requires transformer_engine to be installed.""" )
  for name, module in model.named_children():
    if isinstance(A_,					nn.Linear ) and to_transformer_engine and _convert_linear:
      # Return early if the linear layer weights are not multiples of 16
      if any(p % 16 != 0 for p in module.weight.shape ):
        return
      __magic_name__    					=	module.bias is not None
      __magic_name__    					=	te.Linear(
          module.in_features,					module.out_features,					bias=A_,					params_dtype=module.weight.dtype )
      __magic_name__    					=	module.weight.data.clone()
      if has_bias:
        __magic_name__    					=	module.bias.data.clone()

      setattr(A_,					A_,					A_ )
    elif isinstance(A_,					nn.LayerNorm ) and to_transformer_engine and _convert_ln:
      __magic_name__    					=	te.LayerNorm(module.normalized_shape[0],					eps=module.eps,					params_dtype=module.weight.dtype )
      __magic_name__    					=	module.weight.data.clone()
      __magic_name__    					=	module.bias.data.clone()

      setattr(A_,					A_,					A_ )
    elif isinstance(A_,					te.Linear ) and not to_transformer_engine and _convert_linear:
      __magic_name__    					=	module.bias is not None
      __magic_name__    					=	nn.Linear(
          module.in_features,					module.out_features,					bias=A_,					params_dtype=module.weight.dtype )
      __magic_name__    					=	module.weight.data.clone()
      if has_bias:
        __magic_name__    					=	module.bias.data.clone()

      setattr(A_,					A_,					A_ )
    elif isinstance(A_,					te.LayerNorm ) and not to_transformer_engine and _convert_ln:
      __magic_name__    					=	nn.LayerNorm(module.normalized_shape[0],					eps=module.eps,					params_dtype=module.weight.dtype )
      __magic_name__    					=	module.weight.data.clone()
      __magic_name__    					=	module.bias.data.clone()

      setattr(A_,					A_,					A_ )
    else:
      convert_model(
          A_,					to_transformer_engine=A_,					_convert_linear=_convert_linear,					_convert_ln=_convert_ln,					)





def a__   (	A_ ):






  '''simple docstring'''





  if not is_fpa_available():
    raise ImportError("""Using `has_transformer_engine_layers` requires transformer_engine to be installed.""" )
  for m in model.modules():
    if isinstance(A_,					(te.LayerNorm, te.Linear) ):
      return True
  return False

