

import copy

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


_SCREAMING_SNAKE_CASE          = logging.get_logger(__name__)

_SCREAMING_SNAKE_CASE          = {
    '''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_   ( _lowerCAmelCase):




 def __init__( self     ,     _UpperCAmelCase=256     ,     _UpperCAmelCase=1_024     ,     _UpperCAmelCase=16     ,     _UpperCAmelCase=16     ,     _UpperCAmelCase=4     ,     _UpperCAmelCase="gelu"     ,     _UpperCAmelCase=1E-6     ,     **_UpperCAmelCase     ,     ):
   super().__init__(**_UpperCAmelCase       )
   __UpperCAmelCase  :     int		=    hidden_size
   __UpperCAmelCase  :     Any		=    image_size
   __UpperCAmelCase  :     Dict		=    patch_size
   __UpperCAmelCase  :     Dict		=    image_size // patch_size
   __UpperCAmelCase  :     Any		=    mask_input_channels
   __UpperCAmelCase  :     Dict		=    num_point_embeddings
   __UpperCAmelCase  :     Union[str, Any]		=    hidden_act
   __UpperCAmelCase  :     List[Any]		=    layer_norm_eps



class     lowerCamelCase_   ( _lowerCAmelCase):




 def __init__( self     ,     _UpperCAmelCase=256     ,     _UpperCAmelCase="relu"     ,     _UpperCAmelCase=2_048     ,     _UpperCAmelCase=2     ,     _UpperCAmelCase=8     ,     _UpperCAmelCase=2     ,     _UpperCAmelCase=3     ,     _UpperCAmelCase=3     ,     _UpperCAmelCase=256     ,     _UpperCAmelCase=1E-6     ,     **_UpperCAmelCase     ,     ):
   super().__init__(**_UpperCAmelCase       )
   __UpperCAmelCase  :     List[Any]		=    hidden_size
   __UpperCAmelCase  :     Dict		=    hidden_act
   __UpperCAmelCase  :     Optional[Any]		=    mlp_dim
   __UpperCAmelCase  :     int		=    num_hidden_layers
   __UpperCAmelCase  :     List[str]		=    num_attention_heads
   __UpperCAmelCase  :     Dict		=    attention_downsample_rate
   __UpperCAmelCase  :     Dict		=    num_multimask_outputs
   __UpperCAmelCase  :     Dict		=    iou_head_depth
   __UpperCAmelCase  :     Optional[int]		=    iou_head_hidden_dim
   __UpperCAmelCase  :     List[Any]		=    layer_norm_eps



class     lowerCamelCase_   ( _lowerCAmelCase):




 def __init__( self     ,     _UpperCAmelCase=768     ,     _UpperCAmelCase=256     ,     _UpperCAmelCase=12     ,     _UpperCAmelCase=12     ,     _UpperCAmelCase=3     ,     _UpperCAmelCase=1_024     ,     _UpperCAmelCase=16     ,     _UpperCAmelCase="gelu"     ,     _UpperCAmelCase=1E-06     ,     _UpperCAmelCase=0.0     ,     _UpperCAmelCase=1E-10     ,     _UpperCAmelCase=True     ,     _UpperCAmelCase=4.0     ,     _UpperCAmelCase=True     ,     _UpperCAmelCase=True     ,     _UpperCAmelCase=14     ,     _UpperCAmelCase=[2, 5, 8, 11]     ,     _UpperCAmelCase=128     ,     _UpperCAmelCase=None     ,     **_UpperCAmelCase     ,     ):
   super().__init__(**_UpperCAmelCase       )

   __UpperCAmelCase  :     Any		=    hidden_size
   __UpperCAmelCase  :     str		=    output_channels
   __UpperCAmelCase  :     List[str]		=    num_hidden_layers
   __UpperCAmelCase  :     Tuple		=    num_attention_heads
   __UpperCAmelCase  :     Union[str, Any]		=    num_channels
   __UpperCAmelCase  :     List[Any]		=    image_size
   __UpperCAmelCase  :     Optional[int]		=    patch_size
   __UpperCAmelCase  :     Tuple		=    hidden_act
   __UpperCAmelCase  :     Optional[int]		=    layer_norm_eps
   __UpperCAmelCase  :     Union[str, Any]		=    attention_dropout
   __UpperCAmelCase  :     Optional[int]		=    initializer_range
   __UpperCAmelCase  :     Union[str, Any]		=    qkv_bias
   __UpperCAmelCase  :     Optional[Any]		=    mlp_ratio
   __UpperCAmelCase  :     Union[str, Any]		=    use_abs_pos
   __UpperCAmelCase  :     str		=    use_rel_pos
   __UpperCAmelCase  :     Tuple		=    window_size
   __UpperCAmelCase  :     Tuple		=    global_attn_indexes
   __UpperCAmelCase  :     str		=    num_pos_feats
   __UpperCAmelCase  :     Union[str, Any]		=    int(hidden_size * mlp_ratio       ) if mlp_dim is None else mlp_dim



class     lowerCamelCase_   ( _lowerCAmelCase):
 _lowerCAmelCase       		=  '''sam'''
 _lowerCAmelCase       		=  True




 def __init__( self     ,     _UpperCAmelCase=None     ,     _UpperCAmelCase=None     ,     _UpperCAmelCase=None     ,     _UpperCAmelCase=0.0_2     ,     **_UpperCAmelCase     ,     ):
   super().__init__(**_UpperCAmelCase       )
   __UpperCAmelCase  :     Tuple		=    vision_config if vision_config is not None else {}
   __UpperCAmelCase  :     List[Any]		=    prompt_encoder_config if prompt_encoder_config is not None else {}
   __UpperCAmelCase  :     Any		=    mask_decoder_config if mask_decoder_config is not None else {}

   if isinstance(_UpperCAmelCase     ,     _UpperCAmelCase       ):
     __UpperCAmelCase  :     Union[str, Any]		=    vision_config.to_dict()
   if isinstance(_UpperCAmelCase     ,     _UpperCAmelCase       ):
     __UpperCAmelCase  :     int		=    prompt_encoder_config.to_dict()
   if isinstance(_UpperCAmelCase     ,     _UpperCAmelCase       ):
     __UpperCAmelCase  :     Any		=    mask_decoder_config.to_dict()

   __UpperCAmelCase  :     Union[str, Any]		=    SamVisionConfig(**_UpperCAmelCase       )
   __UpperCAmelCase  :     str		=    SamPromptEncoderConfig(**_UpperCAmelCase       )
   __UpperCAmelCase  :     Union[str, Any]		=    SamMaskDecoderConfig(**_UpperCAmelCase       )
   __UpperCAmelCase  :     Tuple		=    initializer_range




 def 						a					( self       ):
   __UpperCAmelCase  :     Any		=    copy.deepcopy(self.__dict__       )
   __UpperCAmelCase  :     Any		=    self.vision_config.to_dict()
   __UpperCAmelCase  :     str		=    self.prompt_encoder_config.to_dict()
   __UpperCAmelCase  :     List[str]		=    self.mask_decoder_config.to_dict()
   __UpperCAmelCase  :     int		=    self.__class__.model_type
   return output








def    _snake_case		(snake_case_				:    int						,					snake_case_				:    int						,					snake_case_				:    int						,					snake_case_				:    int     )       ->		int:
  return len_board * len_board_column * row + column





def    _snake_case		(snake_case_				:    list[list[str]]						,					snake_case_				:    str						,					snake_case_				:    int						,					snake_case_				:    int						,					snake_case_				:    int						,					snake_case_				:    set[int]						,					)       ->		bool:

  if board[row][column] != word[word_index]:
    return False

  if word_index == len(snake_case_     ) - 1:
    return True

  __UpperCAmelCase  :     List[Any]		=    [(0, 1), (0, -1), (-1, 0), (1, 0)]
  __UpperCAmelCase  :     Dict		=    len(snake_case_     )
  __UpperCAmelCase  :     str		=    len(board[0]     )
  for direction in traverts_directions:
    __UpperCAmelCase  :     Dict		=    row + direction[0]
    __UpperCAmelCase  :     Optional[int]		=    column + direction[1]
    if not (0 <= next_i < len_board and 0 <= next_j < len_board_column):
      continue

    __UpperCAmelCase  :     str		=    get_point_key(snake_case_						,					snake_case_						,					snake_case_						,					snake_case_     )
    if key in visited_points_set:
      continue

    visited_points_set.add(snake_case_     )
    if exits_word(snake_case_						,					snake_case_						,					snake_case_						,					snake_case_						,					word_index + 1						,					snake_case_     ):
      return True

    visited_points_set.remove(snake_case_     )

  return False





def    _snake_case		(snake_case_				:    list[list[str]]						,					snake_case_				:    str     )       ->		bool:
  __UpperCAmelCase  :     str		=    (
      'The board should be a non empty matrix of single chars strings.'
  )

  __UpperCAmelCase  :     Any		=    len(snake_case_     )
  if not isinstance(snake_case_						,					snake_case_     ) or len(snake_case_     ) == 0:
    raise ValueError(snake_case_     )

  for row in board:
    if not isinstance(snake_case_						,					snake_case_     ) or len(snake_case_     ) == 0:
      raise ValueError(snake_case_     )

    for item in row:
      if not isinstance(snake_case_						,					snake_case_     ) or len(snake_case_     ) != 1:
        raise ValueError(snake_case_     )

    # Validate word
  if not isinstance(snake_case_						,					snake_case_     ) or len(snake_case_     ) == 0:
    raise ValueError(
        'The word parameter should be a string of length greater than 0.'     )

  __UpperCAmelCase  :     str		=    len(board[0]     )
  for i in range(snake_case_     ):
    for j in range(snake_case_     ):
      if exits_word(
          snake_case_						,					snake_case_						,					snake_case_						,					snake_case_						,					0						,					{get_point_key(snake_case_						,					snake_case_						,					snake_case_						,					snake_case_     )}     ):
        return True

  return False


if __name__ == "__main__":
      import doctest

      doctest.testmod()





