





import random
import unittest

import numpy as np

import transformers
from transformers import is_flax_available, is_torch_available
from transformers.testing_utils import is_pt_flax_cross_test, require_flax


if is_flax_available():
     import os

     import jax.numpy as jnp
     from jax import jit

     from transformers import AutoTokenizer, FlaxAutoModelForCausalLM
     from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model

     UpperCAmelCase	:	Optional[int]		     =       """0.12"""  # assumed parallelism: 8


if is_torch_available():
     import torch
def      _A       (		SCREAMING_SNAKE_CASE     :   str       ,       SCREAMING_SNAKE_CASE     :   Any       ,       SCREAMING_SNAKE_CASE     :   Optional[int]=None						):





  """simple docstring"""



  if rng is None:
    a__	:					List[Any]										=random.Random()

  a__	:					int										=1
  for dim in shape:
    total_dims *= dim

  a__	:					Tuple										=[]
  for _ in range(SCREAMING_SNAKE_CASE__						):
    values.append(rng.randint(0       ,       vocab_size - 1						)						)

  a__	:					int										=np.array(SCREAMING_SNAKE_CASE__       ,       dtype=jnp.intaa						).reshape(SCREAMING_SNAKE_CASE__						)

  return output
def      _A       (		SCREAMING_SNAKE_CASE     :   Union[str, Any]       ,       SCREAMING_SNAKE_CASE     :   Optional[Any]=None						):





  """simple docstring"""



  a__	:					Any										=ids_tensor(SCREAMING_SNAKE_CASE__       ,       vocab_size=2       ,       rng=SCREAMING_SNAKE_CASE__						)
  # make sure that at least one token is attended to for each batch
  a__	:					Any										=1
  return attn_mask







@require_flax
class 				__lowerCAmelCase       :
      _lowercase				:					str						=			None
      _lowercase				:					Dict						=			()







      def    _lowercase  (    self							)   ->						Tuple:



        '''simple docstring'''




        a__					,							a__	:					List[Any]										=self.model_tester.prepare_config_and_inputs_for_common()

        # cut to half length & take max batch_size 3
        a__	:					List[str]										=2
        a__	:					Dict										=inputs["input_ids"].shape[-1] // 2
        a__	:					Tuple										=inputs["input_ids"][:max_batch_size, :sequence_length]

        a__	:					Union[str, Any]										=jnp.ones_like(A_							)
        a__	:					List[Any]										=attention_mask[:max_batch_size, :sequence_length]

        # generate max 5 tokens
        a__	:					str										=input_ids.shape[-1] + 5
        if config.eos_token_id is not None and config.pad_token_id is None:
          # hack to allow generate for models such as GPT2 as is done in `generate()`
          a__	:					Any										=config.eos_token_id
        return config, input_ids, attention_mask, max_length







      @is_pt_flax_cross_test
      def    _lowercase  (    self							)   ->						Any:



        '''simple docstring'''




        a__					,							a__					,							a__					,							a__	:					Any										=self._get_input_ids_and_config()
        a__	:					Any										=False
        a__	:					List[Any]										=max_length
        a__	:					List[Any]										=0

        for model_class in self.all_generative_model_classes:
          a__	:					Union[str, Any]										=model_class(A_							)

          a__	:					Optional[int]										=model_class.__name__[4:]  # Skip the "Flax" at the beginning
          a__	:					List[Any]										=getattr(A_		,      A_							)
          a__	:					Dict										=pt_model_class(A_							).eval()
          a__	:					Union[str, Any]										=load_flax_weights_in_pytorch_model(A_		,      flax_model.params							)

          a__	:					int										=flax_model.generate(A_							).sequences
          a__	:					Optional[Any]										=pt_model.generate(torch.tensor(A_		,      dtype=torch.long							)							)

          if flax_generation_outputs.shape[-1] > pt_generation_outputs.shape[-1]:
            a__	:					Tuple										=flax_generation_outputs[:, : pt_generation_outputs.shape[-1]]

          self.assertListEqual(pt_generation_outputs.numpy().tolist()		,      flax_generation_outputs.tolist()							)







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



        '''simple docstring'''




        a__					,							a__					,							a__					,							a__	:					Optional[Any]										=self._get_input_ids_and_config()
        a__	:					int										=False
        a__	:					List[Any]										=max_length

        for model_class in self.all_generative_model_classes:
          a__	:					Dict										=model_class(A_							)

          a__	:					Optional[int]										=model.generate(A_							).sequences
          self.assertEqual(generation_outputs.shape[-1]		,      A_							)

          a__	:					List[Any]										=jit(model.generate							)
          a__	:					str										=jit_generate(A_							).sequences

          self.assertListEqual(generation_outputs.tolist()		,      jit_generation_outputs.tolist()							)







      def    _lowercase  (    self							)   ->						int:



        '''simple docstring'''




        a__					,							a__					,							a__					,							a__	:					Optional[Any]										=self._get_input_ids_and_config()
        a__	:					Any										=True
        a__	:					int										=max_length

        for model_class in self.all_generative_model_classes:
          a__	:					List[str]										=model_class(A_							)

          a__	:					Optional[Any]										=model.generate(A_							).sequences
          self.assertEqual(generation_outputs.shape[-1]		,      A_							)

          a__	:					List[Any]										=jit(model.generate							)
          a__	:					List[str]										=jit_generate(A_							).sequences

          self.assertListEqual(generation_outputs.tolist()		,      jit_generation_outputs.tolist()							)







      def    _lowercase  (    self							)   ->						List[str]:



        '''simple docstring'''




        a__					,							a__					,							a__					,							a__	:					Tuple										=self._get_input_ids_and_config()
        a__	:					Tuple										=False
        a__	:					Union[str, Any]										=max_length
        a__	:					Dict										=2

        for model_class in self.all_generative_model_classes:
          a__	:					List[Any]										=model_class(A_							)

          a__	:					Optional[Any]										=model.generate(A_							).sequences
          self.assertEqual(generation_outputs.shape[-1]		,      A_							)

          a__	:					Optional[Any]										=jit(model.generate							)
          a__	:					Optional[int]										=jit_generate(A_							).sequences

          self.assertListEqual(generation_outputs.tolist()		,      jit_generation_outputs.tolist()							)







      def    _lowercase  (    self							)   ->						Optional[Any]:



        '''simple docstring'''




        a__					,							a__					,							a__					,							a__	:					Tuple										=self._get_input_ids_and_config()
        a__	:					Optional[Any]										=False
        a__	:					str										=max_length
        a__	:					List[Any]										=2
        a__	:					Dict										=2

        for model_class in self.all_generative_model_classes:
          a__	:					Dict										=model_class(A_							)

          a__	:					int										=model.generate(A_							).sequences
          self.assertEqual(generation_outputs.shape[0]		,      input_ids.shape[0] * config.num_return_sequences							)







      def    _lowercase  (    self							)   ->						Optional[Any]:



        '''simple docstring'''




        a__					,							a__					,							a__					,							a__	:					List[str]										=self._get_input_ids_and_config()
        a__	:					str										=True
        a__	:					Optional[Any]										=max_length
        a__	:					Optional[int]										=0.8
        a__	:					List[str]										=1_0
        a__	:					List[Any]										=0.3
        a__	:					Optional[Any]										=1
        a__	:					List[Any]										=8
        a__	:					List[str]										=9

        for model_class in self.all_generative_model_classes:
          a__	:					str										=model_class(A_							)

          a__	:					Optional[Any]										=model.generate(A_							).sequences
          self.assertEqual(generation_outputs.shape[-1]		,      A_							)

          a__	:					Optional[int]										=jit(model.generate							)
          a__	:					int										=jit_generate(A_							).sequences

          self.assertListEqual(generation_outputs.tolist()		,      jit_generation_outputs.tolist()							)







      def    _lowercase  (    self							)   ->						List[str]:



        '''simple docstring'''




        a__					,							a__					,							a__					,							a__	:					Union[str, Any]										=self._get_input_ids_and_config()
        a__	:					List[str]										=max_length
        a__	:					List[str]										=1
        a__	:					Dict										=8
        a__	:					Any										=9

        for model_class in self.all_generative_model_classes:
          a__	:					str										=model_class(A_							)

          a__	:					Union[str, Any]										=model.generate(A_							).sequences
          self.assertEqual(generation_outputs.shape[-1]		,      A_							)

          a__	:					List[Any]										=jit(model.generate							)
          a__	:					Dict										=jit_generate(A_							).sequences

          self.assertListEqual(generation_outputs.tolist()		,      jit_generation_outputs.tolist()							)







      def    _lowercase  (    self							)   ->						Union[str, Any]:



        '''simple docstring'''




        a__					,							a__					,							a__					,							a__	:					str										=self._get_input_ids_and_config()
        a__	:					List[Any]										=max_length
        a__	:					int										=2
        a__	:					Dict										=1
        a__	:					Any										=8
        a__	:					Any										=9

        for model_class in self.all_generative_model_classes:
          a__	:					Union[str, Any]										=model_class(A_							)

          a__	:					Optional[int]										=model.generate(A_							).sequences
          self.assertEqual(generation_outputs.shape[-1]		,      A_							)

          a__	:					Optional[Any]										=jit(model.generate							)
          a__	:					Optional[Any]										=jit_generate(A_							).sequences

          self.assertListEqual(generation_outputs.tolist()		,      jit_generation_outputs.tolist()							)







      def    _lowercase  (    self							)   ->						Dict:



        '''simple docstring'''




        a__					,							a__					,							a__					,							a__	:					List[Any]										=self._get_input_ids_and_config()

        # pad attention mask on the left
        a__	:					Optional[int]										=attention_mask.at[(0, 0)].set(0							)

        a__	:					List[Any]										=False
        a__	:					int										=max_length

        for model_class in self.all_generative_model_classes:
          a__	:					Optional[Any]										=model_class(A_							)

          a__	:					List[Any]										=model.generate(A_		,      attention_mask=A_							).sequences
          self.assertEqual(generation_outputs.shape[-1]		,      A_							)

          a__	:					int										=jit(model.generate							)
          a__	:					int										=jit_generate(A_		,      attention_mask=A_							).sequences

          self.assertListEqual(generation_outputs.tolist()		,      jit_generation_outputs.tolist()							)







      def    _lowercase  (    self							)   ->						Union[str, Any]:



        '''simple docstring'''




        a__					,							a__					,							a__					,							a__	:					str										=self._get_input_ids_and_config()

        # pad attention mask on the left
        a__	:					int										=attention_mask.at[(0, 0)].set(0							)

        a__	:					Any										=True
        a__	:					int										=max_length

        for model_class in self.all_generative_model_classes:
          a__	:					List[Any]										=model_class(A_							)

          a__	:					Any										=model.generate(A_		,      attention_mask=A_							).sequences
          self.assertEqual(generation_outputs.shape[-1]		,      A_							)

          a__	:					Union[str, Any]										=jit(model.generate							)
          a__	:					Tuple										=jit_generate(A_		,      attention_mask=A_							).sequences

          self.assertListEqual(generation_outputs.tolist()		,      jit_generation_outputs.tolist()							)







      def    _lowercase  (    self							)   ->						int:



        '''simple docstring'''




        a__					,							a__					,							a__					,							a__	:					Any										=self._get_input_ids_and_config()

        # pad attention mask on the left
        a__	:					List[str]										=attention_mask.at[(0, 0)].set(0							)

        a__	:					int										=2
        a__	:					List[Any]										=max_length

        for model_class in self.all_generative_model_classes:
          a__	:					List[Any]										=model_class(A_							)

          a__	:					Tuple										=model.generate(A_		,      attention_mask=A_							).sequences
          self.assertEqual(generation_outputs.shape[-1]		,      A_							)

          a__	:					int										=jit(model.generate							)
          a__	:					Dict										=jit_generate(A_		,      attention_mask=A_							).sequences

          self.assertListEqual(generation_outputs.tolist()		,      jit_generation_outputs.tolist()							)







@require_flax
class 				__lowerCAmelCase       (    unittest.TestCase):







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



        '''simple docstring'''




        a__	:					str										=AutoTokenizer.from_pretrained("hf-internal-testing/tiny-bert"							)
        a__	:					int										=FlaxAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-bert-flax-only"							)

        a__	:					List[Any]										="Hello world"
        a__	:					List[str]										=tokenizer(A_		,      return_tensors="np"							).input_ids

        # typos are quickly detected (the correct argument is `do_sample`)
        with self.assertRaisesRegex(A_		,      "do_samples"							):
          model.generate(A_		,      do_samples=A_							)

        # arbitrary arguments that will not be used anywhere are also not accepted
        with self.assertRaisesRegex(A_		,      "foo"							):
          a__	:					int										={"foo": "bar"}
          model.generate(A_		,      **A_							)










import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
from ...utils import logging
from ..auto import CONFIG_MAPPING


_A		       =						logging.get_logger(__name__)

_A		       =						{
    'Salesforce/instruct-blip-flan-t5': 'https://huggingface.co/Salesforce/instruct-blip-flan-t5/resolve/main/config.json',
}






class     UpperCAmelCase__       (					A_		):



    """simple docstring"""






    UpperCAmelCase__						:		Optional[int]      =  "instructblip_vision_model"






    def __init__(			self					,   A_=1408					,   A_=6144					,   A_=39					,   A_=16					,   A_=224					,   A_=14					,   A_="gelu"					,   A_=1E-6					,   A_=0.0					,   A_=1E-10					,   A_=True					,   **A_					,   )  ->       Tuple:
         super().__init__(**A_    )

         __UpperCamelCase    							=hidden_size
         __UpperCamelCase    							=intermediate_size
         __UpperCamelCase    							=num_hidden_layers
         __UpperCamelCase    							=num_attention_heads
         __UpperCamelCase    							=patch_size
         __UpperCamelCase    							=image_size
         __UpperCamelCase    							=initializer_range
         __UpperCamelCase    							=attention_dropout
         __UpperCamelCase    							=layer_norm_eps
         __UpperCamelCase    							=hidden_act
         __UpperCamelCase    							=qkv_bias






    @classmethod
    def _a					(			cls					,   A_					,   **A_    )  ->       "PretrainedConfig":
         cls._set_token_in_kwargs(A_    )

         __UpperCamelCase							,      __UpperCamelCase    							=cls.get_config_dict(A_					,   **A_    )

         # get the vision config dict if we are loading from InstructBlipConfig
         if config_dict.get('model_type'    ) == "instructblip":
              __UpperCamelCase    							=config_dict['vision_config']

         if "model_type" in config_dict and hasattr(cls					,   'model_type'    ) and config_dict["model_type"] != cls.model_type:
              logger.warning(
                  f'You are using a model of type {config_dict["model_type"]} to instantiate a model of type '
                  f'{cls.model_type}. This is not supported for all configurations of models and can yield errors.'    )

         return cls.from_dict(A_					,   **A_    )






class     UpperCAmelCase__       (					A_		):



    """simple docstring"""






    UpperCAmelCase__						:		Optional[Any]      =  "instructblip_qformer"






    def __init__(			self					,   A_=30522					,   A_=768					,   A_=12					,   A_=12					,   A_=3072					,   A_="gelu"					,   A_=0.1					,   A_=0.1					,   A_=512					,   A_=0.02					,   A_=1E-12					,   A_=0					,   A_="absolute"					,   A_=2					,   A_=1408					,   **A_					,   )  ->       Optional[Any]:
         super().__init__(pad_token_id=A_					,   **A_    )

         __UpperCamelCase    							=vocab_size
         __UpperCamelCase    							=hidden_size
         __UpperCamelCase    							=num_hidden_layers
         __UpperCamelCase    							=num_attention_heads
         __UpperCamelCase    							=hidden_act
         __UpperCamelCase    							=intermediate_size
         __UpperCamelCase    							=hidden_dropout_prob
         __UpperCamelCase    							=attention_probs_dropout_prob
         __UpperCamelCase    							=max_position_embeddings
         __UpperCamelCase    							=initializer_range
         __UpperCamelCase    							=layer_norm_eps
         __UpperCamelCase    							=position_embedding_type
         __UpperCamelCase    							=cross_attention_frequency
         __UpperCamelCase    							=encoder_hidden_size






    @classmethod
    def _a					(			cls					,   A_					,   **A_    )  ->       "PretrainedConfig":
         cls._set_token_in_kwargs(A_    )

         __UpperCamelCase							,      __UpperCamelCase    							=cls.get_config_dict(A_					,   **A_    )

         # get the qformer config dict if we are loading from InstructBlipConfig
         if config_dict.get('model_type'    ) == "instructblip":
              __UpperCamelCase    							=config_dict['qformer_config']

         if "model_type" in config_dict and hasattr(cls					,   'model_type'    ) and config_dict["model_type"] != cls.model_type:
              logger.warning(
                  f'You are using a model of type {config_dict["model_type"]} to instantiate a model of type '
                  f'{cls.model_type}. This is not supported for all configurations of models and can yield errors.'    )

         return cls.from_dict(A_					,   **A_    )






class     UpperCAmelCase__       (					A_		):



    """simple docstring"""






    UpperCAmelCase__						:		Optional[int]      =  "instructblip"
    UpperCAmelCase__						:		Optional[Any]      =  True






    def __init__(			self					,   A_=None					,   A_=None					,   A_=None					,   A_=32					,   **A_    )  ->       List[str]:
         super().__init__(**A_    )

         if vision_config is None:
              __UpperCamelCase    							={}
              logger.info('vision_config is None. initializing the InstructBlipVisionConfig with default values.'    )

         if qformer_config is None:
              __UpperCamelCase    							={}
              logger.info('qformer_config is None. Initializing the InstructBlipQFormerConfig with default values.'    )

         if text_config is None:
              __UpperCamelCase    							={}
              logger.info('text_config is None. Initializing the text config with default values (`OPTConfig`).'    )

         __UpperCamelCase    							=InstructBlipVisionConfig(**A_    )
         __UpperCamelCase    							=InstructBlipQFormerConfig(**A_    )
         __UpperCamelCase    							=text_config['model_type'] if 'model_type' in text_config else 'opt'
         __UpperCamelCase    							=CONFIG_MAPPING[text_model_type](**A_    )

         __UpperCamelCase    							=self.text_config.tie_word_embeddings
         __UpperCamelCase    							=self.text_config.is_encoder_decoder

         __UpperCamelCase    							=num_query_tokens
         __UpperCamelCase    							=self.vision_config.hidden_size
         __UpperCamelCase    							=self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
         __UpperCamelCase    							=1.0
         __UpperCamelCase    							=0.02






    @classmethod
    def _a					(			cls					,   A_					,   A_					,   A_					,   **A_					,   )  ->       Optional[Any]:
         return cls(
             vision_config=vision_config.to_dict()					,   qformer_config=qformer_config.to_dict()					,   text_config=text_config.to_dict()					,   **A_					,   )






    def _a					(			self    )  ->       Optional[Any]:
         __UpperCamelCase    							=copy.deepcopy(self.__dict__    )
         __UpperCamelCase    							=self.vision_config.to_dict()
         __UpperCamelCase    							=self.qformer_config.to_dict()
         __UpperCamelCase    							=self.text_config.to_dict()
         __UpperCamelCase    							=self.__class__.model_type
         return output





