


import warnings
from dataclasses import dataclass, field
from typing import Optional, Tuple

from .training_args import TrainingArguments
from .utils import cached_property, is_tf_available, logging, requires_backends


lowerCAmelCase__						:List[Any]         =							logging.get_logger(__name__)

if is_tf_available():
		import tensorflow as tf

@dataclass
class 	__a   (       UpperCAmelCase    ):
		_a			:						Any    			=			'tf'
		_a			:						Optional[str]    			=			field(
		    default=UpperCAmelCase					,   metadata={'help': 'Name of TPU'}					,   )

		_a			:						Optional[str]    			=			field(
		    default=UpperCAmelCase					,   metadata={'help': 'Zone of TPU'}					,   )

		_a			:						Optional[str]    			=			field(
		    default=UpperCAmelCase					,   metadata={'help': 'Name of Cloud TPU-enabled project'}					,   )

		_a			:						float    			=			field(
		    default=1.0					,   metadata={'help': 'Power for the Polynomial decay LR scheduler.'}					,   )

		_a			:						bool    			=			field(default=UpperCAmelCase					,   metadata={'help': 'Whether to activate the XLA compilation or not'}    )
		@cached_property
		def 			UpperCAmelCase__       (    self		)				-> Tuple["tf.distribute.Strategy", int]:




									"""simple docstring"""





									requires_backends(self    ,					['tf']		)
									logger.info('Tensorflow: setting up strategy'		)

									_UpperCAmelCase					=    tf.config.list_physical_devices('GPU'		)

									# Set to float16 at first
									if self.fpaa:
																tf.keras.mixed_precision.set_global_policy('mixed_float16'		)

									if self.no_cuda:
																_UpperCAmelCase					=    tf.distribute.OneDeviceStrategy(device='/cpu:0'		)
									else:
																try:
																							if self.tpu_name:
																														_UpperCAmelCase					=    tf.distribute.cluster_resolver.TPUClusterResolver(
																														    self.tpu_name    ,					zone=self.tpu_zone    ,					project=self.gcp_project		)
																							else:
																														_UpperCAmelCase					=    tf.distribute.cluster_resolver.TPUClusterResolver()
																except ValueError:
																							if self.tpu_name:
																														raise RuntimeError(f'''Couldn\'t connect to TPU {self.tpu_name}!'''		)
																							else:
																														_UpperCAmelCase					=    None

																if tpu:
																							# Set to bfloat16 in case of TPU
																							if self.fpaa:
																														tf.keras.mixed_precision.set_global_policy('mixed_bfloat16'		)

																							tf.config.experimental_connect_to_cluster(_SCREAMING_SNAKE_CASE		)
																							tf.tpu.experimental.initialize_tpu_system(_SCREAMING_SNAKE_CASE		)

																							_UpperCAmelCase					=    tf.distribute.TPUStrategy(_SCREAMING_SNAKE_CASE		)

																elif len(_SCREAMING_SNAKE_CASE		) == 0:
																							_UpperCAmelCase					=    tf.distribute.OneDeviceStrategy(device='/cpu:0'		)
																elif len(_SCREAMING_SNAKE_CASE		) == 1:
																							_UpperCAmelCase					=    tf.distribute.OneDeviceStrategy(device='/gpu:0'		)
																elif len(_SCREAMING_SNAKE_CASE		) > 1:
																							# If you only want to use a specific subset of GPUs use `CUDA_VISIBLE_DEVICES=0`
																							_UpperCAmelCase					=    tf.distribute.MirroredStrategy()
																else:
																							raise ValueError('Cannot find the proper strategy, please check your environment properties.'		)

									return strategy
		@property
		def 			UpperCAmelCase__       (    self		)				-> "tf.distribute.Strategy":




									"""simple docstring"""





									requires_backends(self    ,					['tf']		)
									return self._setup_strategy
		@property
		def 			UpperCAmelCase__       (    self		)				-> int:




									"""simple docstring"""





									requires_backends(self    ,					['tf']		)
									return self._setup_strategy.num_replicas_in_sync
		@property
		def 			UpperCAmelCase__       (    self		)				-> Optional[int]:




									"""simple docstring"""





									return False  # TF Logging is handled by Keras not the Trainer
		@property
		def 			UpperCAmelCase__       (    self		)				-> int:




									"""simple docstring"""





									if self.per_gpu_train_batch_size:
																logger.warning(
																    'Using deprecated `--per_gpu_train_batch_size` argument which will be removed in a future '
																    'version. Using `--per_device_train_batch_size` is preferred.'		)
									_UpperCAmelCase					=    self.per_gpu_train_batch_size or self.per_device_train_batch_size
									return per_device_batch_size * self.n_replicas
		@property
		def 			UpperCAmelCase__       (    self		)				-> int:




									"""simple docstring"""





									if self.per_gpu_eval_batch_size:
																logger.warning(
																    'Using deprecated `--per_gpu_eval_batch_size` argument which will be removed in a future '
																    'version. Using `--per_device_eval_batch_size` is preferred.'		)
									_UpperCAmelCase					=    self.per_gpu_eval_batch_size or self.per_device_eval_batch_size
									return per_device_batch_size * self.n_replicas






		@property
		def 			UpperCAmelCase__       (    self		)				-> int:




									"""simple docstring"""





									requires_backends(self    ,					['tf']		)
									warnings.warn(
									    'The n_gpu argument is deprecated and will be removed in a future version, use n_replicas instead.'    ,					_SCREAMING_SNAKE_CASE    ,					)
									return self._setup_strategy.num_replicas_in_sync




import copy

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


lowerCAmelCase__						:int         =							logging.get_logger(__name__)

lowerCAmelCase__						:Optional[int]         =							{
    '''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 	__a   (       UpperCAmelCase    ):
		def __init__(    self    ,					_SCREAMING_SNAKE_CASE=256    ,					_SCREAMING_SNAKE_CASE=1024    ,					_SCREAMING_SNAKE_CASE=16    ,					_SCREAMING_SNAKE_CASE=16    ,					_SCREAMING_SNAKE_CASE=4    ,					_SCREAMING_SNAKE_CASE="gelu"    ,					_SCREAMING_SNAKE_CASE=1e-6    ,					**_SCREAMING_SNAKE_CASE    ,					)				-> Tuple:




									"""simple docstring"""





									super().__init__(**_SCREAMING_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 	__a   (       UpperCAmelCase    ):
		def __init__(    self    ,					_SCREAMING_SNAKE_CASE=256    ,					_SCREAMING_SNAKE_CASE="relu"    ,					_SCREAMING_SNAKE_CASE=2048    ,					_SCREAMING_SNAKE_CASE=2    ,					_SCREAMING_SNAKE_CASE=8    ,					_SCREAMING_SNAKE_CASE=2    ,					_SCREAMING_SNAKE_CASE=3    ,					_SCREAMING_SNAKE_CASE=3    ,					_SCREAMING_SNAKE_CASE=256    ,					_SCREAMING_SNAKE_CASE=1e-6    ,					**_SCREAMING_SNAKE_CASE    ,					)				-> List[Any]:




									"""simple docstring"""





									super().__init__(**_SCREAMING_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 	__a   (       UpperCAmelCase    ):
		def __init__(    self    ,					_SCREAMING_SNAKE_CASE=768    ,					_SCREAMING_SNAKE_CASE=256    ,					_SCREAMING_SNAKE_CASE=12    ,					_SCREAMING_SNAKE_CASE=12    ,					_SCREAMING_SNAKE_CASE=3    ,					_SCREAMING_SNAKE_CASE=1024    ,					_SCREAMING_SNAKE_CASE=16    ,					_SCREAMING_SNAKE_CASE="gelu"    ,					_SCREAMING_SNAKE_CASE=1e-0_6    ,					_SCREAMING_SNAKE_CASE=0.0    ,					_SCREAMING_SNAKE_CASE=1e-1_0    ,					_SCREAMING_SNAKE_CASE=True    ,					_SCREAMING_SNAKE_CASE=4.0    ,					_SCREAMING_SNAKE_CASE=True    ,					_SCREAMING_SNAKE_CASE=True    ,					_SCREAMING_SNAKE_CASE=14    ,					_SCREAMING_SNAKE_CASE=[2, 5, 8, 11]    ,					_SCREAMING_SNAKE_CASE=128    ,					_SCREAMING_SNAKE_CASE=None    ,					**_SCREAMING_SNAKE_CASE    ,					)				-> List[str]:




									"""simple docstring"""





									super().__init__(**_SCREAMING_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 	__a   (       UpperCAmelCase    ):
		_a			:						Any    			=			'sam'
		_a			:						Any    			=			True
		def __init__(    self    ,					_SCREAMING_SNAKE_CASE=None    ,					_SCREAMING_SNAKE_CASE=None    ,					_SCREAMING_SNAKE_CASE=None    ,					_SCREAMING_SNAKE_CASE=0.02    ,					**_SCREAMING_SNAKE_CASE    ,					)				-> Tuple:




									"""simple docstring"""





									super().__init__(**_SCREAMING_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(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE		):
																_UpperCAmelCase					=    vision_config.to_dict()
									if isinstance(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE		):
																_UpperCAmelCase					=    prompt_encoder_config.to_dict()
									if isinstance(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE		):
																_UpperCAmelCase					=    mask_decoder_config.to_dict()

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






		def 			UpperCAmelCase__       (    self		)				-> str:




									"""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
