


'''simple docstring'''



import copy
import os
from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Dict, Mapping, Optional, Union


if TYPE_CHECKING:
      from ...processing_utils import ProcessorMixin
      from ...utils import TensorType

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


A__						:   Tuple				=    logging.get_logger(__name__)

A__						:   Dict				=    {
    """google/owlvit-base-patch32""": """https://huggingface.co/google/owlvit-base-patch32/resolve/main/config.json""",
    """google/owlvit-base-patch16""": """https://huggingface.co/google/owlvit-base-patch16/resolve/main/config.json""",
    """google/owlvit-large-patch14""": """https://huggingface.co/google/owlvit-large-patch14/resolve/main/config.json""",
}





class   UpperCAmelCase_    (_UpperCAmelCase    ):





    """simple docstring"""






    lowerCamelCase    :   str					=     'owlvit_text_model'







    def __init__(				self     ,	SCREAMING_SNAKE_CASE_=4_94_08     ,	SCREAMING_SNAKE_CASE_=5_12     ,	SCREAMING_SNAKE_CASE_=20_48     ,	SCREAMING_SNAKE_CASE_=12     ,	SCREAMING_SNAKE_CASE_=8     ,	SCREAMING_SNAKE_CASE_=16     ,	SCREAMING_SNAKE_CASE_="quick_gelu"     ,	SCREAMING_SNAKE_CASE_=1E-5     ,	SCREAMING_SNAKE_CASE_=0.0     ,	SCREAMING_SNAKE_CASE_=0.0_2     ,	SCREAMING_SNAKE_CASE_=1.0     ,	SCREAMING_SNAKE_CASE_=0     ,	SCREAMING_SNAKE_CASE_=4_94_06     ,	SCREAMING_SNAKE_CASE_=4_94_07     ,	**SCREAMING_SNAKE_CASE_     ,	)							->	Tuple:
         super().__init__(pad_token_id=SCREAMING_SNAKE_CASE_     ,	bos_token_id=SCREAMING_SNAKE_CASE_     ,	eos_token_id=SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_      )

         __lowerCamelCase  :      List[Any]														=						vocab_size
         __lowerCamelCase  :      List[Any]														=						hidden_size
         __lowerCamelCase  :      int														=						intermediate_size
         __lowerCamelCase  :      Any														=						num_hidden_layers
         __lowerCamelCase  :      Dict														=						num_attention_heads
         __lowerCamelCase  :      List[Any]														=						max_position_embeddings
         __lowerCamelCase  :      str														=						hidden_act
         __lowerCamelCase  :      Optional[Any]														=						layer_norm_eps
         __lowerCamelCase  :      Tuple														=						attention_dropout
         __lowerCamelCase  :      Tuple														=						initializer_range
         __lowerCamelCase  :      Optional[Any]														=						initializer_factor







    @classmethod
    def  lowercase_							(				cls     ,	SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_      )							->	"PretrainedConfig":
         cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE_      )

         __lowerCamelCase							,			__lowerCamelCase  :      Any														=						cls.get_config_dict(SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_      )

         # get the text config dict if we are loading from OwlViTConfig
         if config_dict.get('model_type'      ) == "owlvit":
              __lowerCamelCase  :      str														=						config_dict['text_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(SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_      )







class   UpperCAmelCase_    (_UpperCAmelCase    ):





    """simple docstring"""






    lowerCamelCase    :   Tuple					=     'owlvit_vision_model'







    def __init__(				self     ,	SCREAMING_SNAKE_CASE_=7_68     ,	SCREAMING_SNAKE_CASE_=30_72     ,	SCREAMING_SNAKE_CASE_=12     ,	SCREAMING_SNAKE_CASE_=12     ,	SCREAMING_SNAKE_CASE_=3     ,	SCREAMING_SNAKE_CASE_=7_68     ,	SCREAMING_SNAKE_CASE_=32     ,	SCREAMING_SNAKE_CASE_="quick_gelu"     ,	SCREAMING_SNAKE_CASE_=1E-5     ,	SCREAMING_SNAKE_CASE_=0.0     ,	SCREAMING_SNAKE_CASE_=0.0_2     ,	SCREAMING_SNAKE_CASE_=1.0     ,	**SCREAMING_SNAKE_CASE_     ,	)							->	Tuple:
         super().__init__(**SCREAMING_SNAKE_CASE_      )

         __lowerCamelCase  :      str														=						hidden_size
         __lowerCamelCase  :      str														=						intermediate_size
         __lowerCamelCase  :      Union[str, Any]														=						num_hidden_layers
         __lowerCamelCase  :      Tuple														=						num_attention_heads
         __lowerCamelCase  :      str														=						num_channels
         __lowerCamelCase  :      Dict														=						image_size
         __lowerCamelCase  :      str														=						patch_size
         __lowerCamelCase  :      Tuple														=						hidden_act
         __lowerCamelCase  :      List[str]														=						layer_norm_eps
         __lowerCamelCase  :      Tuple														=						attention_dropout
         __lowerCamelCase  :      Optional[int]														=						initializer_range
         __lowerCamelCase  :      Any														=						initializer_factor







    @classmethod
    def  lowercase_							(				cls     ,	SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_      )							->	"PretrainedConfig":
         cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE_      )

         __lowerCamelCase							,			__lowerCamelCase  :      Optional[Any]														=						cls.get_config_dict(SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_      )

         # get the vision config dict if we are loading from OwlViTConfig
         if config_dict.get('model_type'      ) == "owlvit":
              __lowerCamelCase  :      Tuple														=						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(SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_      )







class   UpperCAmelCase_    (_UpperCAmelCase    ):





    """simple docstring"""






    lowerCamelCase    :   Optional[int]					=     'owlvit'
    lowerCamelCase    :   Dict					=     True







    def __init__(				self     ,	SCREAMING_SNAKE_CASE_=None     ,	SCREAMING_SNAKE_CASE_=None     ,	SCREAMING_SNAKE_CASE_=5_12     ,	SCREAMING_SNAKE_CASE_=2.6_5_9_2     ,	SCREAMING_SNAKE_CASE_=True     ,	**SCREAMING_SNAKE_CASE_     ,	)							->	Optional[int]:
         super().__init__(**SCREAMING_SNAKE_CASE_      )

         if text_config is None:
              __lowerCamelCase  :      Dict														=						{}
              logger.info('text_config is None. Initializing the OwlViTTextConfig with default values.'      )

         if vision_config is None:
              __lowerCamelCase  :      Optional[Any]														=						{}
              logger.info('vision_config is None. initializing the OwlViTVisionConfig with default values.'      )

         __lowerCamelCase  :      List[str]														=						OwlViTTextConfig(**SCREAMING_SNAKE_CASE_      )
         __lowerCamelCase  :      List[Any]														=						OwlViTVisionConfig(**SCREAMING_SNAKE_CASE_      )

         __lowerCamelCase  :      Any														=						projection_dim
         __lowerCamelCase  :      List[str]														=						logit_scale_init_value
         __lowerCamelCase  :      List[Any]														=						return_dict
         __lowerCamelCase  :      str														=						1.0







    @classmethod
    def  lowercase_							(				cls     ,	SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_      )							->	"PretrainedConfig":
         cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE_      )

         __lowerCamelCase							,			__lowerCamelCase  :      str														=						cls.get_config_dict(SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_      )

         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(SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_      )







    @classmethod
    def  lowercase_							(				cls     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_      )							->	Any:
         __lowerCamelCase  :      Tuple														=						{}
         __lowerCamelCase  :      Optional[int]														=						text_config
         __lowerCamelCase  :      Dict														=						vision_config

         return cls.from_dict(SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_      )







    def  lowercase_							(				self      )							->	str:
         __lowerCamelCase  :      int														=						copy.deepcopy(self.__dict__      )
         __lowerCamelCase  :      Optional[Any]														=						self.text_config.to_dict()
         __lowerCamelCase  :      Any														=						self.vision_config.to_dict()
         __lowerCamelCase  :      str														=						self.__class__.model_type
         return output







class   UpperCAmelCase_    (_UpperCAmelCase    ):





    """simple docstring"""






    @property
    def  lowercase_							(				self      )							->	Mapping[str, Mapping[int, str]]:
         return OrderedDict(
             [
                 ('input_ids', {0: 'batch', 1: 'sequence'}),
                 ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}),
                 ('attention_mask', {0: 'batch', 1: 'sequence'}),
             ]      )







    @property
    def  lowercase_							(				self      )							->	Mapping[str, Mapping[int, str]]:
         return OrderedDict(
             [
                 ('logits_per_image', {0: 'batch'}),
                 ('logits_per_text', {0: 'batch'}),
                 ('text_embeds', {0: 'batch'}),
                 ('image_embeds', {0: 'batch'}),
             ]      )







    @property
    def  lowercase_							(				self      )							->	float:
         return 1E-4







    def  lowercase_							(				self     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_ = -1     ,	SCREAMING_SNAKE_CASE_ = -1     ,	SCREAMING_SNAKE_CASE_ = None     ,	)							->	Mapping[str, Any]:
         __lowerCamelCase  :      Optional[int]														=						super().generate_dummy_inputs(
             processor.tokenizer     ,	batch_size=SCREAMING_SNAKE_CASE_     ,	seq_length=SCREAMING_SNAKE_CASE_     ,	framework=SCREAMING_SNAKE_CASE_      )
         __lowerCamelCase  :      str														=						super().generate_dummy_inputs(
             processor.image_processor     ,	batch_size=SCREAMING_SNAKE_CASE_     ,	framework=SCREAMING_SNAKE_CASE_      )
         return {**text_input_dict, **image_input_dict}







    @property
    def  lowercase_							(				self      )							->	int:
         return 14














from manim import *







class 		_A						( UpperCAmelCase_ ):

  def    a					(    self						:	Dict			):
     """simple docstring"""

     __UpperCamelCase	:			Tuple					=					Rectangle(height=0.5   ,				width=0.5			)
     __UpperCamelCase	:			Tuple					=					Rectangle(height=0.46   ,				width=0.46			).set_stroke(width=0			)

     __UpperCamelCase	:			Optional[Any]					=					Rectangle(height=0.25   ,				width=0.25			)

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

     __UpperCamelCase	:			Tuple					=					[mem.copy() for i in range(4			)]
     __UpperCamelCase	:			Optional[Any]					=					VGroup(*lowerCamelCase__			).arrange(lowerCamelCase__   ,				buff=0			)
     __UpperCamelCase	:			Optional[Any]					=					Text("""GPU"""   ,				font_size=24			)
     __UpperCamelCase	:			Union[str, Any]					=					Group(lowerCamelCase__   ,				lowerCamelCase__			).arrange(lowerCamelCase__   ,				buff=0.5   ,				aligned_edge=lowerCamelCase__			)
     gpu.move_to([-1, -1, 0]			)
     self.add(lowerCamelCase__			)

     __UpperCamelCase	:			Dict					=					[mem.copy() for i in range(6			)]
     __UpperCamelCase	:			Tuple					=					VGroup(*lowerCamelCase__			).arrange(lowerCamelCase__   ,				buff=0			)

     __UpperCamelCase	:			Tuple					=					Text("""Model"""   ,				font_size=24			)
     __UpperCamelCase	:			List[str]					=					Group(lowerCamelCase__   ,				lowerCamelCase__			).arrange(lowerCamelCase__   ,				buff=0.5   ,				aligned_edge=lowerCamelCase__			)
     model.move_to([3, -1.0, 0]			)
     self.add(lowerCamelCase__			)

     __UpperCamelCase	:			Tuple					=					[]
     __UpperCamelCase	:			str					=					[]

     for i, rect in enumerate(lowerCamelCase__			):
        __UpperCamelCase	:			Optional[Any]					=					fill.copy().set_fill(lowerCamelCase__   ,				opacity=0.8			)
        target.move_to(lowerCamelCase__			)
        model_arr.append(lowerCamelCase__			)

        __UpperCamelCase	:			Tuple					=					Rectangle(height=0.46   ,				width=0.46			).set_stroke(width=0.0			).set_fill(lowerCamelCase__   ,				opacity=0.8			)
        cpu_target.move_to(cpu_left_col_base[i]			)
        model_cpu_arr.append(lowerCamelCase__			)

     self.add(*lowerCamelCase__   ,				*lowerCamelCase__			)

     __UpperCamelCase	:			Union[str, Any]					=					[meta_mem.copy() for i in range(6			)]
     __UpperCamelCase	:			Any					=					[meta_mem.copy() for i in range(6			)]
     __UpperCamelCase	:			int					=					VGroup(*lowerCamelCase__			).arrange(lowerCamelCase__   ,				buff=0			)
     __UpperCamelCase	:			Union[str, Any]					=					VGroup(*lowerCamelCase__			).arrange(lowerCamelCase__   ,				buff=0			)
     __UpperCamelCase	:			str					=					VGroup(lowerCamelCase__   ,				lowerCamelCase__			).arrange(lowerCamelCase__   ,				buff=0			)
     __UpperCamelCase	:			Optional[Any]					=					Text("""Disk"""   ,				font_size=24			)
     __UpperCamelCase	:			Optional[Any]					=					Group(lowerCamelCase__   ,				lowerCamelCase__			).arrange(lowerCamelCase__   ,				buff=0.5   ,				aligned_edge=lowerCamelCase__			)
     disk.move_to([-4, -1.25, 0]			)
     self.add(lowerCamelCase__   ,				lowerCamelCase__			)

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

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

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

     self.add(lowerCamelCase__   ,				lowerCamelCase__			)

     __UpperCamelCase	:			Optional[int]					=					MarkupText(
         f'<span fgcolor=\'{BLUE}\'>●</span> Checkpoint'   ,				font_size=18   ,				)

     blue_text.next_to(lowerCamelCase__   ,				DOWN * 2.4   ,				aligned_edge=key_text.get_left()			)
     self.add(lowerCamelCase__			)

     __UpperCamelCase	:			Any					=					MarkupText(
         f'Now watch as an input is passed through the model\nand how the memory is utilized and handled.'   ,				font_size=24   ,				)
     step_a.move_to([2, 2, 0]			)

     self.play(Write(lowerCamelCase__			)			)

     __UpperCamelCase	:			List[str]					=					Square(0.3			)
     input.set_fill(lowerCamelCase__   ,				opacity=1.0			)
     input.set_stroke(width=0.0			)
     input.next_to(model_base[0]   ,				lowerCamelCase__   ,				buff=0.5			)

     self.play(Write(lowerCamelCase__			)			)

     input.generate_target()
     input.target.next_to(model_arr[0]   ,				direction=lowerCamelCase__   ,				buff=0.02			)
     self.play(MoveToTarget(lowerCamelCase__			)			)

     self.play(FadeOut(lowerCamelCase__			)			)

     __UpperCamelCase	:			Dict					=					Arrow(start=lowerCamelCase__   ,				end=lowerCamelCase__   ,				color=lowerCamelCase__   ,				buff=0.5			)
     a.next_to(model_arr[0].get_left()   ,				lowerCamelCase__   ,				buff=0.2			)

     model_cpu_arr[0].generate_target()
     model_cpu_arr[0].target.move_to(gpu_rect[0]			)

     __UpperCamelCase	:			int					=					MarkupText(
         f'As the input reaches a layer, the hook triggers\nand weights are moved from the CPU\nto the GPU and back.'   ,				font_size=24   ,				)
     step_a.move_to([2, 2, 0]			)

     self.play(Write(lowerCamelCase__   ,				run_time=3			)			)

     __UpperCamelCase	:			Dict					=					{"""run_time""": 1, """fade_in""": True, """fade_out""": True, """buff""": 0.02}

     self.play(
         Write(lowerCamelCase__			)   ,				Circumscribe(model_arr[0]   ,				color=lowerCamelCase__   ,				**lowerCamelCase__			)   ,				Circumscribe(model_cpu_arr[0]   ,				color=lowerCamelCase__   ,				**lowerCamelCase__			)   ,				Circumscribe(gpu_rect[0]   ,				color=lowerCamelCase__   ,				**lowerCamelCase__			)   ,				)
     self.play(MoveToTarget(model_cpu_arr[0]			)			)

     __UpperCamelCase	:			int					=					a.copy()
     for i in range(6			):
        a_c.next_to(model_arr[i].get_right() + 0.02   ,				lowerCamelCase__   ,				buff=0.2			)

        input.generate_target()
        input.target.move_to(model_arr[i].get_right() + 0.02			)

        __UpperCamelCase	:			Tuple					=					AnimationGroup(
            FadeOut(lowerCamelCase__   ,				run_time=0.5			)   ,				MoveToTarget(lowerCamelCase__   ,				run_time=0.5			)   ,				FadeIn(lowerCamelCase__   ,				run_time=0.5			)   ,				lag_ratio=0.2			)

        self.play(lowerCamelCase__			)

        model_cpu_arr[i].generate_target()
        model_cpu_arr[i].target.move_to(cpu_left_col_base[i]			)

        if i < 5:
           model_cpu_arr[i + 1].generate_target()
           model_cpu_arr[i + 1].target.move_to(gpu_rect[0]			)
           if i >= 1:
              __UpperCamelCase	:			Any					=					0.7

           self.play(
               Circumscribe(model_arr[i]   ,				**lowerCamelCase__			)   ,				Circumscribe(cpu_left_col_base[i]   ,				**lowerCamelCase__			)   ,				Circumscribe(cpu_left_col_base[i + 1]   ,				color=lowerCamelCase__   ,				**lowerCamelCase__			)   ,				Circumscribe(gpu_rect[0]   ,				color=lowerCamelCase__   ,				**lowerCamelCase__			)   ,				Circumscribe(model_arr[i + 1]   ,				color=lowerCamelCase__   ,				**lowerCamelCase__			)   ,				)
           if i < 1:
              self.play(
                  MoveToTarget(model_cpu_arr[i]			)   ,				MoveToTarget(model_cpu_arr[i + 1]			)   ,				)
           else:
              self.play(
                  MoveToTarget(model_cpu_arr[i]   ,				run_time=0.7			)   ,				MoveToTarget(model_cpu_arr[i + 1]   ,				run_time=0.7			)   ,				)
        else:
           model_cpu_arr[i].generate_target()
           model_cpu_arr[i].target.move_to(cpu_left_col_base[-1]			)
           input.generate_target()
           input.target.next_to(model_arr[-1].get_right()   ,				RIGHT + 0.02   ,				buff=0.2			)

           self.play(
               Circumscribe(model_arr[-1]   ,				color=lowerCamelCase__   ,				**lowerCamelCase__			)   ,				Circumscribe(cpu_left_col_base[-1]   ,				color=lowerCamelCase__   ,				**lowerCamelCase__			)   ,				Circumscribe(gpu_rect[0]   ,				color=lowerCamelCase__   ,				**lowerCamelCase__			)   ,				)

           self.play(MoveToTarget(model_cpu_arr[i]			)			)

        __UpperCamelCase	:			str					=					a_c
        __UpperCamelCase	:			List[Any]					=					a_c.copy()

     input.generate_target()
     input.target.next_to(model_base[-1]   ,				RIGHT + 0.02   ,				buff=0.5			)
     self.play(
         FadeOut(lowerCamelCase__			)   ,				FadeOut(lowerCamelCase__   ,				run_time=0.5			)   ,				)

     __UpperCamelCase	:			List[str]					=					MarkupText(f'Inference on a model too large for GPU memory\nis successfully completed.'   ,				font_size=24			)
     step_a.move_to([2, 2, 0]			)

     self.play(Write(lowerCamelCase__   ,				run_time=3			)   ,				MoveToTarget(lowerCamelCase__			)			)

     self.wait()



