





'''simple docstring'''







import enum
import inspect
from typing import Iterable, List, Optional, Tuple, Union


class        __a   (enum.Enum  ):
							__a       : Tuple          =							"timm"
							__a       : Tuple          =							"transformers"





def 							lowerCamelCase_   (	SCREAMING_SNAKE_CASE__   :      Optional[Iterable[str]],      SCREAMING_SNAKE_CASE__   :      Optional[Iterable[int]],      SCREAMING_SNAKE_CASE__   :      Optional[Iterable[str]] )			->						int:
						if stage_names is None:
												raise ValueError('''Stage_names must be set for transformers backbones''' )

						if out_features is not None:
												if not isinstance(SCREAMING_SNAKE_CASE__,      (list,) ):
																		raise ValueError(F"""out_features must be a list {type(SCREAMING_SNAKE_CASE__ )}""" )
												if any(feat not in stage_names for feat in out_features ):
																		raise ValueError(F"""out_features must be a subset of stage_names: {stage_names} got {out_features}""" )

						if out_indices is not None:
												if not isinstance(SCREAMING_SNAKE_CASE__,      (list, tuple) ):
																		raise ValueError(F"""out_indices must be a list or tuple, got {type(SCREAMING_SNAKE_CASE__ )}""" )
												if any(idx >= len(SCREAMING_SNAKE_CASE__ ) for idx in out_indices ):
																		raise ValueError('''out_indices must be valid indices for stage_names {stage_names}, got {out_indices}''' )

						if out_features is not None and out_indices is not None:
												if len(SCREAMING_SNAKE_CASE__ ) != len(SCREAMING_SNAKE_CASE__ ):
																		raise ValueError('''out_features and out_indices should have the same length if both are set''' )
												if out_features != [stage_names[idx] for idx in out_indices]:
																		raise ValueError('''out_features and out_indices should correspond to the same stages if both are set''' )





def 							lowerCamelCase_   (	SCREAMING_SNAKE_CASE__   :      Optional[List[str]],      SCREAMING_SNAKE_CASE__   :      Optional[Union[List[int], Tuple[int]]],      SCREAMING_SNAKE_CASE__   :      List[str],      )			->						Optional[Any]:
						if out_indices is None and out_features is None:
												UpperCAmelCase_      :   Union[str, Any]      =	[len(SCREAMING_SNAKE_CASE__ ) - 1]
												UpperCAmelCase_      :   str      =	[stage_names[-1]]
						elif out_indices is None and out_features is not None:
												UpperCAmelCase_      :   List[str]      =	[stage_names.index(SCREAMING_SNAKE_CASE__ ) for layer in out_features]
						elif out_features is None and out_indices is not None:
												UpperCAmelCase_      :   List[str]      =	[stage_names[idx] for idx in out_indices]
						return out_features, out_indices





def 							lowerCamelCase_   (	SCREAMING_SNAKE_CASE__   :      Optional[List[str]],      SCREAMING_SNAKE_CASE__   :      Optional[Union[List[int], Tuple[int]]],      SCREAMING_SNAKE_CASE__   :      List[str],      )			->						Tuple[List[str], List[int]]:
						verify_out_features_out_indices(out_features=SCREAMING_SNAKE_CASE__,      out_indices=SCREAMING_SNAKE_CASE__,      stage_names=SCREAMING_SNAKE_CASE__ )
						UpperCAmelCase_    ,							UpperCAmelCase_      :   Dict      =	_align_output_features_output_indices(
						    out_features=SCREAMING_SNAKE_CASE__,      out_indices=SCREAMING_SNAKE_CASE__,      stage_names=SCREAMING_SNAKE_CASE__ )
						# Verify that the aligned out_features and out_indices are valid
						verify_out_features_out_indices(out_features=SCREAMING_SNAKE_CASE__,      out_indices=SCREAMING_SNAKE_CASE__,      stage_names=SCREAMING_SNAKE_CASE__ )
						return output_features, output_indices


class        __a   :
							__a       : Optional[BackboneType]          =							None

							def      UpperCAmelCase__						(				self				:  Dict							,  __magic_name__				:  List[Any] )       ->							None:







													"""simple docstring"""
													if getattr(self							,  '''_backbone'''							,  __magic_name__ ) is None:
																			raise ValueError('''self._backbone must be set before calling _init_timm_backbone''' )

													# These will diagree with the defaults for the transformers models e.g. for resnet50
													# the transformer model has out_features = ['stem', 'stage1', 'stage2', 'stage3', 'stage4']
													# the timm model has out_features = ['act', 'layer1', 'layer2', 'layer3', 'layer4']
													UpperCAmelCase_      :   int      =	[stage['''module'''] for stage in self._backbone.feature_info.info]
													UpperCAmelCase_      :   Tuple      =	[stage['''num_chs'''] for stage in self._backbone.feature_info.info]
													UpperCAmelCase_      :   List[Any]      =	self._backbone.feature_info.out_indices
													UpperCAmelCase_      :   Any      =	self._backbone.feature_info.module_name()

													# We verify the out indices and out features are valid
													verify_out_features_out_indices(
													    out_features=__magic_name__							,  out_indices=__magic_name__							,  stage_names=self.stage_names )
													UpperCAmelCase_    ,							UpperCAmelCase_      :   Optional[Any]      =	out_features, out_indices

							def      UpperCAmelCase__						(				self				:  str							,  __magic_name__				:  Dict )       ->							None:







													"""simple docstring"""
													UpperCAmelCase_      :   str      =	getattr(__magic_name__							,  '''stage_names''' )
													UpperCAmelCase_      :   Tuple      =	getattr(__magic_name__							,  '''out_features'''							,  __magic_name__ )
													UpperCAmelCase_      :   Union[str, Any]      =	getattr(__magic_name__							,  '''out_indices'''							,  __magic_name__ )

													UpperCAmelCase_      :   List[str]      =	stage_names
													UpperCAmelCase_    ,							UpperCAmelCase_      :   str      =	get_aligned_output_features_output_indices(
													    out_features=__magic_name__							,  out_indices=__magic_name__							,  stage_names=__magic_name__ )
													# Number of channels for each stage. This is set in the transformer backbone model init
													UpperCAmelCase_      :   List[str]      =	None

							def      UpperCAmelCase__						(				self				:  Any							,  __magic_name__				:  Tuple )       ->							None:







													"""simple docstring"""
													UpperCAmelCase_      :   int      =	config

													UpperCAmelCase_      :   str      =	getattr(__magic_name__							,  '''use_timm_backbone'''							,  __magic_name__ )
													UpperCAmelCase_      :   str      =	BackboneType.TIMM if self.use_timm_backbone else BackboneType.TRANSFORMERS

													if self.backbone_type == BackboneType.TIMM:
																			self._init_timm_backbone(__magic_name__ )
													elif self.backbone_type == BackboneType.TRANSFORMERS:
																			self._init_transformers_backbone(__magic_name__ )
													else:
																			raise ValueError(F"""backbone_type {self.backbone_type} not supported.""" )

							@property
							def      UpperCAmelCase__						(				self				:  List[Any] )       ->							Optional[Any]:







													"""simple docstring"""
													return self._out_features

							@out_features.setter
							def      UpperCAmelCase__						(				self				:  int							,  __magic_name__				:  List[str] )       ->							List[str]:







													"""simple docstring"""
													UpperCAmelCase_    ,							UpperCAmelCase_      :   Union[str, Any]      =	get_aligned_output_features_output_indices(
													    out_features=__magic_name__							,  out_indices=__magic_name__							,  stage_names=self.stage_names )

							@property
							def      UpperCAmelCase__						(				self				:  int )       ->							List[Any]:







													"""simple docstring"""
													return self._out_indices

							@out_indices.setter
							def      UpperCAmelCase__						(				self				:  Union[str, Any]							,  __magic_name__				:  Union[Tuple[int], List[int]] )       ->							Union[str, Any]:







													"""simple docstring"""
													UpperCAmelCase_    ,							UpperCAmelCase_      :   int      =	get_aligned_output_features_output_indices(
													    out_features=__magic_name__							,  out_indices=__magic_name__							,  stage_names=self.stage_names )

							@property
							def      UpperCAmelCase__						(				self				:  int )       ->							Optional[int]:







													"""simple docstring"""
													# the current backbones will output the number of channels for each stage
													# even if that stage is not in the out_features list.
													return {stage: self.num_features[i] for i, stage in enumerate(self.stage_names )}

							@property
							def      UpperCAmelCase__						(				self				:  Tuple )       ->							Optional[Any]:







													"""simple docstring"""
													return [self.out_feature_channels[name] for name in self.out_features]

							def      UpperCAmelCase__						(				self				:  Any							,  *__magic_name__				:  Optional[int]							,  **__magic_name__				:  Dict )       ->							int:







													"""simple docstring"""
													UpperCAmelCase_      :   Tuple      =	dict(inspect.signature(self.forward ).parameters )
													UpperCAmelCase_      :   Optional[Any]      =	{k: v for k, v in kwargs.items() if k in signature}
													return self(*__magic_name__							,  **__magic_name__ )

							def      UpperCAmelCase__						(				self				:  Union[str, Any]							,  __magic_name__				:  List[str]							,  __magic_name__				:  Optional[bool] = None							,  __magic_name__				:  Optional[bool] = None							,  __magic_name__				:  Optional[bool] = None							,  )       ->							List[Any]:







													"""simple docstring"""
													raise NotImplementedError('''This method should be implemented by the derived class.''' )



							def      UpperCAmelCase__						(				self				:  Optional[Any] )       ->							Union[str, Any]:







													"""simple docstring"""
													UpperCAmelCase_      :   List[Any]      =	super().to_dict()
													UpperCAmelCase_      :   List[Any]      =	output.pop('''_out_features''' )
													UpperCAmelCase_      :   Tuple      =	output.pop('''_out_indices''' )
													return output





class        __a   :

							@property
							def      UpperCAmelCase__						(				self				:  Optional[Any] )       ->							str:







													"""simple docstring"""
													return self._out_features

							@out_features.setter
							def      UpperCAmelCase__						(				self				:  int							,  __magic_name__				:  List[str] )       ->							Union[str, Any]:







													"""simple docstring"""
													UpperCAmelCase_    ,							UpperCAmelCase_      :   List[Any]      =	get_aligned_output_features_output_indices(
													    out_features=__magic_name__							,  out_indices=__magic_name__							,  stage_names=self.stage_names )

							@property
							def      UpperCAmelCase__						(				self				:  Optional[Any] )       ->							int:







													"""simple docstring"""
													return self._out_indices

							@out_indices.setter
							def      UpperCAmelCase__						(				self				:  List[Any]							,  __magic_name__				:  Union[Tuple[int], List[int]] )       ->							str:







													"""simple docstring"""
													UpperCAmelCase_    ,							UpperCAmelCase_      :   str      =	get_aligned_output_features_output_indices(
													    out_features=__magic_name__							,  out_indices=__magic_name__							,  stage_names=self.stage_names )



							def      UpperCAmelCase__						(				self				:  Union[str, Any] )       ->							int:







													"""simple docstring"""
													UpperCAmelCase_      :   str      =	super().to_dict()
													UpperCAmelCase_      :   Union[str, Any]      =	output.pop('''_out_features''' )
													UpperCAmelCase_      :   Any      =	output.pop('''_out_indices''' )
													return output













"""simple docstring"""


def 					a			(  UpperCamelCase_				:					int  ,						UpperCamelCase_				:					int  ,						UpperCamelCase_				:					int  ,						UpperCamelCase_				:					int		)	->		int:
 return len_board * len_board_column * row + column




def 					a			(  UpperCamelCase_				:					list[list[str]]  ,						UpperCamelCase_				:					str  ,						UpperCamelCase_				:					int  ,						UpperCamelCase_				:					int  ,						UpperCamelCase_				:					int  ,						UpperCamelCase_				:					set[int]  ,						)	->		bool:

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

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

 __A						:							List[str]      =						[(0, 1), (0, -1), (-1, 0), (1, 0)]
 __A						:							Any      =						len(UpperCamelCase_		)
 __A						:							Union[str, Any]      =						len(board[0]		)
 for direction in traverts_directions:
  __A						:							Any      =						row + direction[0]
  __A						:							List[Any]      =						column + direction[1]
  if not (0 <= next_i < len_board and 0 <= next_j < len_board_column):
   continue

  __A						:							Any      =						get_point_key(UpperCamelCase_  ,						UpperCamelCase_  ,						UpperCamelCase_  ,						UpperCamelCase_		)
  if key in visited_points_set:
   continue

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

  visited_points_set.remove(UpperCamelCase_		)

 return False




def 					a			(  UpperCamelCase_				:					list[list[str]]  ,						UpperCamelCase_				:					str		)	->		bool:
 __A						:							Optional[Any]      =						(
     'The board should be a non empty matrix of single chars strings.'
 )

 __A						:							Dict      =						len(UpperCamelCase_		)
 if not isinstance(UpperCamelCase_  ,						UpperCamelCase_		) or len(UpperCamelCase_		) == 0:
  raise ValueError(UpperCamelCase_		)

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

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

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

 __A						:							List[Any]      =						len(board[0]		)
 for i in range(UpperCamelCase_		):
  for j in range(UpperCamelCase_		):
   if exits_word(
       UpperCamelCase_  ,						UpperCamelCase_  ,						UpperCamelCase_  ,						UpperCamelCase_  ,						0  ,						{get_point_key(UpperCamelCase_  ,						UpperCamelCase_  ,						UpperCamelCase_  ,						UpperCamelCase_		)}		):
    return True

 return False


if __name__ == "__main__":
   import doctest

   doctest.testmod()





