




from manim import *






class 					__lowerCAmelCase				( UpperCAmelCase__				):







			def UpperCamelCase    ( self					:    Any   ):




									"""simple docstring"""


									_UpperCAmelCase									=  Rectangle(height=0.5					,					width=0.5   )
									_UpperCAmelCase									=  Rectangle(height=0.46					,					width=0.46   ).set_stroke(width=0   )
									_UpperCAmelCase									=  Rectangle(height=0.25					,					width=0.25   )

									_UpperCAmelCase									=  [mem.copy() for i in range(6   )]
									_UpperCAmelCase									=  [mem.copy() for i in range(6   )]
									_UpperCAmelCase									=  VGroup(*snake_case__   ).arrange(snake_case__					,					buff=0   )
									_UpperCAmelCase									=  VGroup(*snake_case__   ).arrange(snake_case__					,					buff=0   )
									_UpperCAmelCase									=  VGroup(snake_case__					,					snake_case__   ).arrange(snake_case__					,					buff=0   )
									_UpperCAmelCase									=  Text("CPU"					,					font_size=24   )
									_UpperCAmelCase									=  Group(snake_case__					,					snake_case__   ).arrange(snake_case__					,					buff=0.5					,					aligned_edge=snake_case__   )
									cpu.move_to([-2.5, -0.5, 0]   )
									self.add(snake_case__   )

									_UpperCAmelCase									=  [mem.copy() for i in range(4   )]
									_UpperCAmelCase									=  VGroup(*snake_case__   ).arrange(snake_case__					,					buff=0   )
									_UpperCAmelCase									=  Text("GPU"					,					font_size=24   )
									_UpperCAmelCase									=  Group(snake_case__					,					snake_case__   ).arrange(snake_case__					,					buff=0.5					,					aligned_edge=snake_case__   )
									gpu.move_to([-1, -1, 0]   )
									self.add(snake_case__   )

									_UpperCAmelCase									=  [mem.copy() for i in range(6   )]
									_UpperCAmelCase									=  VGroup(*snake_case__   ).arrange(snake_case__					,					buff=0   )

									_UpperCAmelCase									=  Text("Model"					,					font_size=24   )
									_UpperCAmelCase									=  Group(snake_case__					,					snake_case__   ).arrange(snake_case__					,					buff=0.5					,					aligned_edge=snake_case__   )
									model.move_to([3, -1.0, 0]   )
									self.add(snake_case__   )

									_UpperCAmelCase									=  []
									_UpperCAmelCase									=  []

									for i, rect in enumerate(snake_case__   ):
															rect.set_stroke(snake_case__   )

															_UpperCAmelCase									=  Rectangle(height=0.46 / 4					,					width=0.46 / 3   ).set_stroke(width=0.0   ).set_fill(snake_case__					,					opacity=0.7   )

															if i == 0:
																					cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT   )					,					buff=0.02					,					direction=snake_case__   )
																					cpu_target.set_x(cpu_target.get_x() + 0.1   )
															elif i == 3:
																					cpu_target.next_to(model_cpu_arr[0]					,					direction=snake_case__					,					buff=0.0   )
															else:
																					cpu_target.next_to(model_cpu_arr[i - 1]					,					direction=snake_case__					,					buff=0.0   )
															self.add(snake_case__   )
															model_cpu_arr.append(snake_case__   )

									self.add(*snake_case__					,					*snake_case__   )

									_UpperCAmelCase									=  [meta_mem.copy() for i in range(6   )]
									_UpperCAmelCase									=  [meta_mem.copy() for i in range(6   )]
									_UpperCAmelCase									=  VGroup(*snake_case__   ).arrange(snake_case__					,					buff=0   )
									_UpperCAmelCase									=  VGroup(*snake_case__   ).arrange(snake_case__					,					buff=0   )
									_UpperCAmelCase									=  VGroup(snake_case__					,					snake_case__   ).arrange(snake_case__					,					buff=0   )
									_UpperCAmelCase									=  Text("Disk"					,					font_size=24   )
									_UpperCAmelCase									=  Group(snake_case__					,					snake_case__   ).arrange(snake_case__					,					buff=0.5					,					aligned_edge=snake_case__   )
									disk.move_to([-4.0, -1.25, 0]   )
									self.add(snake_case__					,					snake_case__   )

									_UpperCAmelCase									=  []

									for i in range(6   ):
															_UpperCAmelCase									=  fill.copy().set_fill(snake_case__					,					opacity=0.8   )
															target.move_to(disk_left_col_base[i]   ).scale(0.5   )
															cpu_disk_arr.append(snake_case__   )

									self.add(*snake_case__   )

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

									_UpperCAmelCase									=  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(snake_case__					,					snake_case__   )

									_UpperCAmelCase									=  MarkupText(
									    F"""<span fgcolor='{BLUE}'>●</span> Checkpoint"""					,					font_size=18					,					)

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

									_UpperCAmelCase									=  MarkupText(F"""The offloaded weights are all sent to the CPU."""					,					font_size=24   )
									step_a.move_to([2, 2, 0]   )

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

									for i in range(6   ):
															_UpperCAmelCase									=  cpu_disk_arr[i]
															_UpperCAmelCase									=  rect.copy().set_fill(snake_case__					,					opacity=0.8   ).scale(2.0   )
															cpa.generate_target()
															cpa.target.move_to(model_base[i]   )

															if i == 0:
																					rect.set_fill(snake_case__					,					opacity=0.8   )
																					rect.generate_target()
																					rect.target.move_to(cpu_left_col_base[0]   ).scale(2.0   )

																					self.remove(
																					    *snake_case__					,					*snake_case__					,					)

															else:
																					rect.generate_target()
																					rect.target.move_to(cpu_left_col_base[i]   ).scale(2.0   )
															self.play(MoveToTarget(snake_case__   )					,					MoveToTarget(snake_case__   )					,					model_base[i].animate.set_stroke(snake_case__   )   )
									self.play(FadeOut(snake_case__   )   )

									_UpperCAmelCase									=  MarkupText(
									    F"""Finally, hooks are added to each weight in the model\nto transfer the weights from CPU to GPU\n\t\tand back when needed."""					,					font_size=24					,					)
									step_a.move_to([2, 2, 0]   )

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

									_UpperCAmelCase									=  []
									_UpperCAmelCase									=  []
									for i in range(6   ):
															_UpperCAmelCase									=  Arrow(start=snake_case__					,					end=snake_case__					,					color=snake_case__					,					buff=0.5   )
															a.next_to(model_base[i].get_left()					,					snake_case__					,					buff=0.2   )
															arrows.append(snake_case__   )
															animations.append(Write(snake_case__   )   )
									self.play(*snake_case__   )
									self.wait()










import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowercase_			:       Tuple						       =				logging.get_logger(__name__)

lowercase_			:       List[Any]						       =				{
    'facebook/sam-vit-huge': 'https://huggingface.co/facebook/sam-vit-huge/resolve/main/config.json',
    'facebook/sam-vit-large': 'https://huggingface.co/facebook/sam-vit-large/resolve/main/config.json',
    'facebook/sam-vit-base': 'https://huggingface.co/facebook/sam-vit-base/resolve/main/config.json',
}






class 					__lowerCAmelCase				( UpperCAmelCase__				):







			def __init__( self					:    Optional[int]					,					snake_case__					:    int=256					,					snake_case__					:    Dict=1_024					,					snake_case__					:    Dict=16					,					snake_case__					:    Union[str, Any]=16					,					snake_case__					:    Any=4					,					snake_case__					:    Tuple="gelu"					,					snake_case__					:    Tuple=1e-6					,					**snake_case__					:    str					,					):




									"""simple docstring"""


									super().__init__(**snake_case__   )
									_UpperCAmelCase									=  hidden_size
									_UpperCAmelCase									=  image_size
									_UpperCAmelCase									=  patch_size
									_UpperCAmelCase									=  image_size // patch_size
									_UpperCAmelCase									=  mask_input_channels
									_UpperCAmelCase									=  num_point_embeddings
									_UpperCAmelCase									=  hidden_act
									_UpperCAmelCase									=  layer_norm_eps






class 					__lowerCAmelCase				( UpperCAmelCase__				):







			def __init__( self					:    str					,					snake_case__					:    List[str]=256					,					snake_case__					:    Tuple="relu"					,					snake_case__					:    int=2_048					,					snake_case__					:    Optional[int]=2					,					snake_case__					:    Dict=8					,					snake_case__					:    List[Any]=2					,					snake_case__					:    List[Any]=3					,					snake_case__					:    List[Any]=3					,					snake_case__					:    Optional[int]=256					,					snake_case__					:    Tuple=1e-6					,					**snake_case__					:    Any					,					):




									"""simple docstring"""


									super().__init__(**snake_case__   )
									_UpperCAmelCase									=  hidden_size
									_UpperCAmelCase									=  hidden_act
									_UpperCAmelCase									=  mlp_dim
									_UpperCAmelCase									=  num_hidden_layers
									_UpperCAmelCase									=  num_attention_heads
									_UpperCAmelCase									=  attention_downsample_rate
									_UpperCAmelCase									=  num_multimask_outputs
									_UpperCAmelCase									=  iou_head_depth
									_UpperCAmelCase									=  iou_head_hidden_dim
									_UpperCAmelCase									=  layer_norm_eps






class 					__lowerCAmelCase				( UpperCAmelCase__				):







			def __init__( self					:    List[str]					,					snake_case__					:    str=768					,					snake_case__					:    Dict=256					,					snake_case__					:    Any=12					,					snake_case__					:    Union[str, Any]=12					,					snake_case__					:    Any=3					,					snake_case__					:    List[str]=1_024					,					snake_case__					:    Union[str, Any]=16					,					snake_case__					:    Any="gelu"					,					snake_case__					:    List[str]=1e-06					,					snake_case__					:    Union[str, Any]=0.0					,					snake_case__					:    Optional[int]=1e-10					,					snake_case__					:    int=True					,					snake_case__					:    Dict=4.0					,					snake_case__					:    Optional[Any]=True					,					snake_case__					:    Tuple=True					,					snake_case__					:    List[Any]=14					,					snake_case__					:    List[str]=[2, 5, 8, 11]					,					snake_case__					:    List[Any]=128					,					snake_case__					:    List[Any]=None					,					**snake_case__					:    Any					,					):




									"""simple docstring"""


									super().__init__(**snake_case__   )

									_UpperCAmelCase									=  hidden_size
									_UpperCAmelCase									=  output_channels
									_UpperCAmelCase									=  num_hidden_layers
									_UpperCAmelCase									=  num_attention_heads
									_UpperCAmelCase									=  num_channels
									_UpperCAmelCase									=  image_size
									_UpperCAmelCase									=  patch_size
									_UpperCAmelCase									=  hidden_act
									_UpperCAmelCase									=  layer_norm_eps
									_UpperCAmelCase									=  attention_dropout
									_UpperCAmelCase									=  initializer_range
									_UpperCAmelCase									=  qkv_bias
									_UpperCAmelCase									=  mlp_ratio
									_UpperCAmelCase									=  use_abs_pos
									_UpperCAmelCase									=  use_rel_pos
									_UpperCAmelCase									=  window_size
									_UpperCAmelCase									=  global_attn_indexes
									_UpperCAmelCase									=  num_pos_feats
									_UpperCAmelCase									=  int(hidden_size * mlp_ratio   ) if mlp_dim is None else mlp_dim






class 					__lowerCAmelCase				( UpperCAmelCase__				):
			snake_case_   :				Tuple				  =  "sam"
			snake_case_   :				Tuple				  =  True







			def __init__( self					:    Optional[int]					,					snake_case__					:    Optional[Any]=None					,					snake_case__					:    Any=None					,					snake_case__					:    int=None					,					snake_case__					:    List[Any]=0.02					,					**snake_case__					:    str					,					):




									"""simple docstring"""


									super().__init__(**snake_case__   )
									_UpperCAmelCase									=  vision_config if vision_config is not None else {}
									_UpperCAmelCase									=  prompt_encoder_config if prompt_encoder_config is not None else {}
									_UpperCAmelCase									=  mask_decoder_config if mask_decoder_config is not None else {}

									if isinstance(snake_case__					,					snake_case__   ):
															_UpperCAmelCase									=  vision_config.to_dict()
									if isinstance(snake_case__					,					snake_case__   ):
															_UpperCAmelCase									=  prompt_encoder_config.to_dict()
									if isinstance(snake_case__					,					snake_case__   ):
															_UpperCAmelCase									=  mask_decoder_config.to_dict()

									_UpperCAmelCase									=  SamVisionConfig(**snake_case__   )
									_UpperCAmelCase									=  SamPromptEncoderConfig(**snake_case__   )
									_UpperCAmelCase									=  SamMaskDecoderConfig(**snake_case__   )
									_UpperCAmelCase									=  initializer_range







			def UpperCamelCase    ( self					:    Any   ):




									"""simple docstring"""


									_UpperCAmelCase									=  copy.deepcopy(self.__dict__   )
									_UpperCAmelCase									=  self.vision_config.to_dict()
									_UpperCAmelCase									=  self.prompt_encoder_config.to_dict()
									_UpperCAmelCase									=  self.mask_decoder_config.to_dict()
									_UpperCAmelCase									=  self.__class__.model_type
									return output




