

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


lowercase_             =     logging.get_logger(__name__)

lowercase_             =     {
    'salesforce/blip2-opt-2.7b': 'https://huggingface.co/salesforce/blip2-opt-2.7b/resolve/main/config.json',
}





class 				__lowerCAmelCase  (      SCREAMING_SNAKE_CASE							):
  _a		    =						"""blip_2_vision_model"""




  def __init__(       self							,     lowerCAmelCase=1_408							,     lowerCAmelCase=6_144							,     lowerCAmelCase=39							,     lowerCAmelCase=16							,     lowerCAmelCase=224							,     lowerCAmelCase=14							,     lowerCAmelCase="gelu"							,     lowerCAmelCase=0.00001							,     lowerCAmelCase=0.0							,     lowerCAmelCase=1e-10							,     lowerCAmelCase=True							,     **lowerCAmelCase							,     )						->				Dict:
      '''simple docstring'''






      super().__init__(**lowerCAmelCase      )

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




  @classmethod
  def 						A__  (       cls							,     lowerCAmelCase							,     **lowerCAmelCase      )						->				"PretrainedConfig":
      '''simple docstring'''






      cls._set_token_in_kwargs(lowerCAmelCase      )

      _lowercase , _lowercase			 =cls.get_config_dict(lowerCAmelCase							,     **lowerCAmelCase      )

      # get the vision config dict if we are loading from Blip2Config
      if config_dict.get('model_type'      ) == "blip-2":
          _lowercase			 =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(lowerCAmelCase							,     **lowerCAmelCase      )





class 				__lowerCAmelCase  (      SCREAMING_SNAKE_CASE							):
  _a		    =						"""blip_2_qformer"""




  def __init__(       self							,     lowerCAmelCase=30_522							,     lowerCAmelCase=768							,     lowerCAmelCase=12							,     lowerCAmelCase=12							,     lowerCAmelCase=3_072							,     lowerCAmelCase="gelu"							,     lowerCAmelCase=0.1							,     lowerCAmelCase=0.1							,     lowerCAmelCase=512							,     lowerCAmelCase=0.02							,     lowerCAmelCase=1e-12							,     lowerCAmelCase=0							,     lowerCAmelCase="absolute"							,     lowerCAmelCase=2							,     lowerCAmelCase=1_408							,     **lowerCAmelCase							,     )						->				Union[str, Any]:
      '''simple docstring'''






      super().__init__(pad_token_id=lowerCAmelCase							,     **lowerCAmelCase      )

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




  @classmethod
  def 						A__  (       cls							,     lowerCAmelCase							,     **lowerCAmelCase      )						->				"PretrainedConfig":
      '''simple docstring'''






      cls._set_token_in_kwargs(lowerCAmelCase      )

      _lowercase , _lowercase			 =cls.get_config_dict(lowerCAmelCase							,     **lowerCAmelCase      )

      # get the qformer config dict if we are loading from Blip2Config
      if config_dict.get('model_type'      ) == "blip-2":
          _lowercase			 =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(lowerCAmelCase							,     **lowerCAmelCase      )





class 				__lowerCAmelCase  (      SCREAMING_SNAKE_CASE							):
  _a		    =						"""blip-2"""
  _a		    =						True




  def __init__(       self							,     lowerCAmelCase=None							,     lowerCAmelCase=None							,     lowerCAmelCase=None							,     lowerCAmelCase=32							,     **lowerCAmelCase      )						->				int:
      '''simple docstring'''






      super().__init__(**lowerCAmelCase      )

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

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

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

      _lowercase			 =BlipaVisionConfig(**lowerCAmelCase      )
      _lowercase			 =BlipaQFormerConfig(**lowerCAmelCase      )
      _lowercase			 =text_config['model_type'] if 'model_type' in text_config else 'opt'
      _lowercase			 =CONFIG_MAPPING[text_model_type](**lowerCAmelCase      )

      _lowercase			 =self.text_config.tie_word_embeddings
      _lowercase			 =self.text_config.is_encoder_decoder

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




  @classmethod
  def 						A__  (       cls							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     **lowerCAmelCase							,     )						->				List[str]:
      '''simple docstring'''






      return cls(
          vision_config=vision_config.to_dict()							,     qformer_config=qformer_config.to_dict()							,     text_config=text_config.to_dict()							,     **lowerCAmelCase							,     )




  def 						A__  (       self      )						->				List[str]:
      '''simple docstring'''






      _lowercase			 =copy.deepcopy(self.__dict__      )
      _lowercase			 =self.vision_config.to_dict()
      _lowercase			 =self.qformer_config.to_dict()
      _lowercase			 =self.text_config.to_dict()
      _lowercase			 =self.__class__.model_type
      return output





import unittest

from knapsack import knapsack as k





class 				__lowerCAmelCase  (      unittest.TestCase							):




  def 						A__  (       self      )						->				List[str]:
        '''simple docstring'''






        _lowercase			 =0
        _lowercase			 =[0]
        _lowercase			 =[0]
        _lowercase			 =len(lowerCAmelCase      )
        self.assertEqual(k.knapsack(lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase      )							,     0      )

        _lowercase			 =[60]
        _lowercase			 =[10]
        _lowercase			 =len(lowerCAmelCase      )
        self.assertEqual(k.knapsack(lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase      )							,     0      )




  def 						A__  (       self      )						->				List[Any]:
        '''simple docstring'''






        _lowercase			 =3
        _lowercase			 =[1, 2, 3]
        _lowercase			 =[3, 2, 1]
        _lowercase			 =len(lowerCAmelCase      )
        self.assertEqual(k.knapsack(lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase      )							,     5      )




  def 						A__  (       self      )						->				str:
        '''simple docstring'''






        _lowercase			 =50
        _lowercase			 =[60, 100, 120]
        _lowercase			 =[10, 20, 30]
        _lowercase			 =len(lowerCAmelCase      )
        self.assertEqual(k.knapsack(lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase      )							,     220      )


if __name__ == "__main__":
  unittest.main()


