




from __future__ import annotations

from queue import Queue


def 							__SCREAMING_SNAKE_CASE						(    snake_case_			,					snake_case_		):

						'''simple docstring'''


						a ^= b
						b ^= a
						a ^= b
						return a, b


def 							__SCREAMING_SNAKE_CASE						(    snake_case_			,					snake_case_		):

						'''simple docstring'''


						_UpperCAmelCase									=  1
						while (1 << j) < max_node:
												for i in range(1			,					max_node + 1		):
																		_UpperCAmelCase									=  parent[j - 1][parent[j - 1][i]]
												j += 1
						return parent


def 							__SCREAMING_SNAKE_CASE						(    snake_case_			,					snake_case_			,					snake_case_			,					snake_case_		):

						'''simple docstring'''


						if level[u] < level[v]:
												_UpperCAmelCase							,				_UpperCAmelCase									=  swap(snake_case_			,					snake_case_		)
						# making depth of u same as depth of v
						for i in range(18			,					-1			,					-1		):
												if level[u] - (1 << i) >= level[v]:
																		_UpperCAmelCase									=  parent[i][u]
    # at the same depth if u==v that mean lca is found
						if u == v:
												return u
						# moving both nodes upwards till lca in found
						for i in range(18			,					-1			,					-1		):
												if parent[i][u] not in [0, parent[i][v]]:
																		_UpperCAmelCase							,				_UpperCAmelCase									=  parent[i][u], parent[i][v]
    # returning longest common ancestor of u,v
						return parent[0][u]


def 							__SCREAMING_SNAKE_CASE						(    snake_case_			,					snake_case_			,					snake_case_			,					snake_case_			,					snake_case_ = 1			,					):

						'''simple docstring'''


						_UpperCAmelCase									=  0
						_UpperCAmelCase									=  Queue(maxsize=snake_case_		)
						q.put(snake_case_		)
						while q.qsize() != 0:
												_UpperCAmelCase									=  q.get()
												for v in graph[u]:
																		if level[v] == -1:
																								_UpperCAmelCase									=  level[u] + 1
																								q.put(snake_case_		)
																								_UpperCAmelCase									=  u
						return level, parent


def 							__SCREAMING_SNAKE_CASE						(    ):

						'''simple docstring'''


						_UpperCAmelCase									=  13
						# initializing with 0
						_UpperCAmelCase									=  [[0 for _ in range(max_node + 10		)] for _ in range(20		)]
						# initializing with -1 which means every node is unvisited
						_UpperCAmelCase									=  [-1 for _ in range(max_node + 10		)]
						_UpperCAmelCase									=  {
						    1: [2, 3, 4],
						    2: [5],
						    3: [6, 7],
						    4: [8],
						    5: [9, 10],
						    6: [11],
						    7: [],
						    8: [12, 13],
						    9: [],
						    10: [],
						    11: [],
						    12: [],
						    13: [],
						}
						_UpperCAmelCase							,				_UpperCAmelCase									=  breadth_first_search(snake_case_			,					snake_case_			,					snake_case_			,					snake_case_			,					1		)
						_UpperCAmelCase									=  create_sparse(snake_case_			,					snake_case_		)
						print("LCA of node 1 and 3 is: "			,					lowest_common_ancestor(1			,					3			,					snake_case_			,					snake_case_		)		)
						print("LCA of node 5 and 6 is: "			,					lowest_common_ancestor(5			,					6			,					snake_case_			,					snake_case_		)		)
						print("LCA of node 7 and 11 is: "			,					lowest_common_ancestor(7			,					11			,					snake_case_			,					snake_case_		)		)
						print("LCA of node 6 and 7 is: "			,					lowest_common_ancestor(6			,					7			,					snake_case_			,					snake_case_		)		)
						print("LCA of node 4 and 12 is: "			,					lowest_common_ancestor(4			,					12			,					snake_case_			,					snake_case_		)		)
						print("LCA of node 8 and 8 is: "			,					lowest_common_ancestor(8			,					8			,					snake_case_			,					snake_case_		)		)


if __name__ == "__main__":
				main()










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




