
from manim import *



class    _snake_case (						UpperCAmelCase_				):


 def   lowercase__  (       self):


   '''simple docstring'''




   lowercase__	: str     		=   Rectangle(height=0.5       ,	width=0.5)
   lowercase__	: Tuple     		=   Rectangle(height=0.4_6       ,	width=0.4_6).set_stroke(width=0)

   lowercase__	: List[Any]     		=   [mem.copy() for i in range(6)]
   lowercase__	: Tuple     		=   [mem.copy() for i in range(6)]
   lowercase__	: List[str]     		=   VGroup(*SCREAMING_SNAKE_CASE_).arrange(SCREAMING_SNAKE_CASE_       ,	buff=0)
   lowercase__	: Tuple     		=   VGroup(*SCREAMING_SNAKE_CASE_).arrange(SCREAMING_SNAKE_CASE_       ,	buff=0)
   lowercase__	: List[Any]     		=   VGroup(SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_).arrange(SCREAMING_SNAKE_CASE_       ,	buff=0)
   lowercase__	: Dict     		=   Text("""CPU"""       ,	font_size=24)
   lowercase__	: List[str]     		=   Group(SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_).arrange(SCREAMING_SNAKE_CASE_       ,	buff=0.5       ,	aligned_edge=SCREAMING_SNAKE_CASE_)
   cpu.move_to([-2.5, -0.5, 0])
   self.add(SCREAMING_SNAKE_CASE_)

   lowercase__	: str     		=   [mem.copy() for i in range(1)]
   lowercase__	: Any     		=   VGroup(*SCREAMING_SNAKE_CASE_).arrange(SCREAMING_SNAKE_CASE_       ,	buff=0)
   lowercase__	: Optional[int]     		=   Text("""GPU"""       ,	font_size=24)
   lowercase__	: Union[str, Any]     		=   Group(SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_).arrange(SCREAMING_SNAKE_CASE_       ,	buff=0.5       ,	aligned_edge=SCREAMING_SNAKE_CASE_)
   gpu.align_to(SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_)
   gpu.set_x(gpu.get_x() - 1)

   self.add(SCREAMING_SNAKE_CASE_)

   lowercase__	: Optional[int]     		=   [mem.copy() for i in range(6)]
   lowercase__	: str     		=   VGroup(*SCREAMING_SNAKE_CASE_).arrange(SCREAMING_SNAKE_CASE_       ,	buff=0)

   lowercase__	: Optional[Any]     		=   Text("""Model"""       ,	font_size=24)
   lowercase__	: Tuple     		=   Group(SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_).arrange(SCREAMING_SNAKE_CASE_       ,	buff=0.5       ,	aligned_edge=SCREAMING_SNAKE_CASE_)
   model.move_to([3, -1.0, 0])

   self.play(
       Create(SCREAMING_SNAKE_CASE_       ,	run_time=1)       ,	Create(SCREAMING_SNAKE_CASE_       ,	run_time=1)       ,	Create(SCREAMING_SNAKE_CASE_       ,	run_time=1)       ,	)

   lowercase__	: Optional[Any]     		=   MarkupText(
       f'First, an empty model skeleton is loaded\ninto <span fgcolor=\'{YELLOW}\'>memory</span> without using much RAM.'       ,	font_size=24       ,	)

   lowercase__	: Any     		=   Square(side_length=2.2)
   key.move_to([-5, 2, 0])

   lowercase__	: Optional[int]     		=   MarkupText(
       f'<b>Key:</b>\n\n<span fgcolor=\'{YELLOW}\'>●</span> Empty Model'       ,	font_size=18       ,	)

   key_text.move_to([-5, 2.4, 0])

   step_a.move_to([2, 2, 0])
   self.play(Write(SCREAMING_SNAKE_CASE_       ,	run_time=2.5)       ,	Write(SCREAMING_SNAKE_CASE_)       ,	Write(SCREAMING_SNAKE_CASE_))

   self.add(SCREAMING_SNAKE_CASE_)

   lowercase__	: List[Any]     		=   []
   lowercase__	: Union[str, Any]     		=   []
   lowercase__	: str     		=   []
   for i, rect in enumerate(SCREAMING_SNAKE_CASE_):
     lowercase__	: Union[str, Any]     		=   Rectangle(height=0.4_6       ,	width=0.4_6).set_stroke(width=0.0).set_fill(SCREAMING_SNAKE_CASE_       ,	opacity=0.7)
     cpu_target.move_to(SCREAMING_SNAKE_CASE_)
     cpu_target.generate_target()
     lowercase__	: Union[str, Any]     		=   0.4_6 / 4
     lowercase__	: Tuple     		=   0.4_6 / 3

     if i == 0:
       cpu_target.target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT)       ,	buff=0.0_2       ,	direction=SCREAMING_SNAKE_CASE_)
       cpu_target.target.set_x(cpu_target.target.get_x() + 0.1)
     elif i == 3:
       cpu_target.target.next_to(cpu_targs[0].target       ,	direction=SCREAMING_SNAKE_CASE_       ,	buff=0.0)
     else:
       cpu_target.target.next_to(cpu_targs[i - 1].target       ,	direction=SCREAMING_SNAKE_CASE_       ,	buff=0.0)
     cpu_targs.append(SCREAMING_SNAKE_CASE_)

     first_animations.append(rect.animate(run_time=0.5).set_stroke(SCREAMING_SNAKE_CASE_))
     second_animations.append(MoveToTarget(SCREAMING_SNAKE_CASE_       ,	run_time=1.5))

   self.play(*SCREAMING_SNAKE_CASE_)
   self.play(*SCREAMING_SNAKE_CASE_)

   self.wait()













'''simple docstring'''

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


__lowerCAmelCase				:      Optional[Any]						      =logging.get_logger(__name__)

__lowerCAmelCase				:      List[Any]						      ={
    "salesforce/blip2-opt-2.7b": "https://huggingface.co/salesforce/blip2-opt-2.7b/resolve/main/config.json",
}






class   UpperCAmelCase      (			UpperCamelCase__							):
 __lowercase  						=							"""blip_2_vision_model"""
 def __init__(							self :List[Any]		,			lowercase_ :List[str]=14_08		,			lowercase_ :int=61_44		,			lowercase_ :List[str]=39		,			lowercase_ :List[Any]=16		,			lowercase_ :Union[str, Any]=2_24		,			lowercase_ :List[str]=14		,			lowercase_ :Dict="gelu"		,			lowercase_ :Tuple=0.0_0_0_0_1		,			lowercase_ :Dict=0.0		,			lowercase_ :Optional[int]=1E-10		,			lowercase_ :Optional[Any]=True		,			**lowercase_ :List[str]		,			)->				int:
     super().__init__(**lowercase_				)

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



 @classmethod
 def 	UpperCAmelCase_				(							cls :Optional[Any]		,			lowercase_ :Union[str, os.PathLike]		,			**lowercase_ :Optional[Any]				)->				"PretrainedConfig":
     cls._set_token_in_kwargs(lowercase_				)

     A__,      A__							=     cls.get_config_dict(lowercase_		,			**lowercase_				)

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






class   UpperCAmelCase      (			UpperCamelCase__							):
 __lowercase  						=							"""blip_2_qformer"""
 def __init__(							self :Dict		,			lowercase_ :int=3_05_22		,			lowercase_ :Union[str, Any]=7_68		,			lowercase_ :Tuple=12		,			lowercase_ :Union[str, Any]=12		,			lowercase_ :str=30_72		,			lowercase_ :Any="gelu"		,			lowercase_ :Optional[int]=0.1		,			lowercase_ :List[str]=0.1		,			lowercase_ :Dict=5_12		,			lowercase_ :Optional[int]=0.0_2		,			lowercase_ :Dict=1E-12		,			lowercase_ :List[Any]=0		,			lowercase_ :str="absolute"		,			lowercase_ :List[str]=2		,			lowercase_ :Tuple=14_08		,			**lowercase_ :Tuple		,			)->				List[Any]:
     super().__init__(pad_token_id=lowercase_		,			**lowercase_				)

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



 @classmethod
 def 	UpperCAmelCase_				(							cls :int		,			lowercase_ :Union[str, os.PathLike]		,			**lowercase_ :str				)->				"PretrainedConfig":
     cls._set_token_in_kwargs(lowercase_				)

     A__,      A__							=     cls.get_config_dict(lowercase_		,			**lowercase_				)

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






class   UpperCAmelCase      (			UpperCamelCase__							):
 __lowercase  						=							"""blip-2"""
 __lowercase  						=							True
 def __init__(							self :int		,			lowercase_ :List[Any]=None		,			lowercase_ :Dict=None		,			lowercase_ :Tuple=None		,			lowercase_ :List[str]=32		,			**lowercase_ :Any				)->				Union[str, Any]:
     super().__init__(**lowercase_				)

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

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

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

     A__							=     BlipaVisionConfig(**lowercase_				)
     A__							=     BlipaQFormerConfig(**lowercase_				)
     A__							=     text_config["model_type"] if "model_type" in text_config else "opt"
     A__							=     CONFIG_MAPPING[text_model_type](**lowercase_				)

     A__							=     self.text_config.tie_word_embeddings
     A__							=     self.text_config.is_encoder_decoder

     A__							=     num_query_tokens
     A__							=     self.vision_config.hidden_size
     A__							=     self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
     A__							=     1.0
     A__							=     0.0_2
 @classmethod
 def 	UpperCAmelCase_				(							cls :Optional[Any]		,			lowercase_ :BlipaVisionConfig		,			lowercase_ :BlipaQFormerConfig		,			lowercase_ :PretrainedConfig		,			**lowercase_ :Any		,			)->				Optional[int]:
     return cls(
         vision_config=vision_config.to_dict()		,			qformer_config=qformer_config.to_dict()		,			text_config=text_config.to_dict()		,			**lowercase_		,			)



 def 	UpperCAmelCase_				(							self :Dict				)->				Any:
     A__							=     copy.deepcopy(self.__dict__				)
     A__							=     self.vision_config.to_dict()
     A__							=     self.qformer_config.to_dict()
     A__							=     self.text_config.to_dict()
     A__							=     self.__class__.model_type
     return output



