



import json
import os
import tempfile

import datasets
from utils import generate_example_dataset, get_duration


UpperCAmelCase_          =				5_0_0_0_0_0

UpperCAmelCase_      ,						UpperCAmelCase_          =				os.path.split(__file__)
UpperCAmelCase_          =				os.path.join(RESULTS_BASEPATH, "results", RESULTS_FILENAME.replace(".py", ".json"))
@get_duration
def 				UpperCAmelCase__		(					_SCREAMING_SNAKE_CASE :						datasets.Dataset					)->List[Any]:
      _lowerCAmelCase			      =	dataset.select(range(0  ,					len(_SCREAMING_SNAKE_CASE					)  ,					2					)					)
@get_duration
def 				UpperCAmelCase__		(					_SCREAMING_SNAKE_CASE :						datasets.Dataset					)->Optional[Any]:
      _lowerCAmelCase			      =	dataset.sort('''numbers'''					)
@get_duration
def 				UpperCAmelCase__		(					_SCREAMING_SNAKE_CASE :						datasets.Dataset					)->Dict:
      _lowerCAmelCase			      =	dataset.shuffle()
@get_duration
def 				UpperCAmelCase__		(					_SCREAMING_SNAKE_CASE :						datasets.Dataset					)->Union[str, Any]:
      _lowerCAmelCase			      =	dataset.train_test_split(0.1					)
@get_duration
def 				UpperCAmelCase__		(					_SCREAMING_SNAKE_CASE :						datasets.Dataset  ,					_SCREAMING_SNAKE_CASE :						Any=1_0					)->List[Any]:
      for shard_id in range(_SCREAMING_SNAKE_CASE					):
            _lowerCAmelCase			      =	dataset.shard(_SCREAMING_SNAKE_CASE  ,					_SCREAMING_SNAKE_CASE					)
def 				UpperCAmelCase__		(					)->Optional[Any]:
      _lowerCAmelCase			      =	{'''num examples''': SPEED_TEST_N_EXAMPLES}
      _lowerCAmelCase			      =	(select, sort, shuffle, train_test_split, shard)
      with tempfile.TemporaryDirectory() as tmp_dir:
            print('''generating dataset'''					)
            _lowerCAmelCase			      =	datasets.Features({'''text''': datasets.Value('''string'''					), '''numbers''': datasets.Value('''float32'''					)}					)
            _lowerCAmelCase			      =	generate_example_dataset(
                os.path.join(_SCREAMING_SNAKE_CASE  ,					'''dataset.arrow'''					)  ,					_SCREAMING_SNAKE_CASE  ,					num_examples=_SCREAMING_SNAKE_CASE					)
            print('''Functions'''					)
            for func in functions:
                  print(func.__name__					)
                  _lowerCAmelCase			      =	func(_SCREAMING_SNAKE_CASE					)

      with open(_SCREAMING_SNAKE_CASE  ,					'''wb'''					) as f:
            f.write(json.dumps(_SCREAMING_SNAKE_CASE					).encode('''utf-8'''					)					)


if __name__ == "__main__":  # useful to run the profiler
    benchmark_indices_mapping()




import copy

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


UpperCAmelCase_          =				logging.get_logger(__name__)

UpperCAmelCase_          =				{
    "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 		UpperCAmelCase    (    snake_case_					):





   def __init__(						self							,  _lowerCAmelCase=256							,  _lowerCAmelCase=1_024							,  _lowerCAmelCase=16							,  _lowerCAmelCase=16							,  _lowerCAmelCase=4							,  _lowerCAmelCase="gelu"							,  _lowerCAmelCase=1E-6							,  **_lowerCAmelCase							,  ):
         super().__init__(**_lowerCAmelCase      )
         _lowerCAmelCase			      =	hidden_size
         _lowerCAmelCase			      =	image_size
         _lowerCAmelCase			      =	patch_size
         _lowerCAmelCase			      =	image_size // patch_size
         _lowerCAmelCase			      =	mask_input_channels
         _lowerCAmelCase			      =	num_point_embeddings
         _lowerCAmelCase			      =	hidden_act
         _lowerCAmelCase			      =	layer_norm_eps






class 		UpperCAmelCase    (    snake_case_					):





   def __init__(						self							,  _lowerCAmelCase=256							,  _lowerCAmelCase="relu"							,  _lowerCAmelCase=2_048							,  _lowerCAmelCase=2							,  _lowerCAmelCase=8							,  _lowerCAmelCase=2							,  _lowerCAmelCase=3							,  _lowerCAmelCase=3							,  _lowerCAmelCase=256							,  _lowerCAmelCase=1E-6							,  **_lowerCAmelCase							,  ):
         super().__init__(**_lowerCAmelCase      )
         _lowerCAmelCase			      =	hidden_size
         _lowerCAmelCase			      =	hidden_act
         _lowerCAmelCase			      =	mlp_dim
         _lowerCAmelCase			      =	num_hidden_layers
         _lowerCAmelCase			      =	num_attention_heads
         _lowerCAmelCase			      =	attention_downsample_rate
         _lowerCAmelCase			      =	num_multimask_outputs
         _lowerCAmelCase			      =	iou_head_depth
         _lowerCAmelCase			      =	iou_head_hidden_dim
         _lowerCAmelCase			      =	layer_norm_eps






class 		UpperCAmelCase    (    snake_case_					):





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

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






class 		UpperCAmelCase    (    snake_case_					):
   SCREAMING_SNAKE_CASE__					      =  '''sam'''
   SCREAMING_SNAKE_CASE__					      =  True





   def __init__(						self							,  _lowerCAmelCase=None							,  _lowerCAmelCase=None							,  _lowerCAmelCase=None							,  _lowerCAmelCase=0.02							,  **_lowerCAmelCase							,  ):
         super().__init__(**_lowerCAmelCase      )
         _lowerCAmelCase			      =	vision_config if vision_config is not None else {}
         _lowerCAmelCase			      =	prompt_encoder_config if prompt_encoder_config is not None else {}
         _lowerCAmelCase			      =	mask_decoder_config if mask_decoder_config is not None else {}

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

         _lowerCAmelCase			      =	SamVisionConfig(**_lowerCAmelCase      )
         _lowerCAmelCase			      =	SamPromptEncoderConfig(**_lowerCAmelCase      )
         _lowerCAmelCase			      =	SamMaskDecoderConfig(**_lowerCAmelCase      )
         _lowerCAmelCase			      =	initializer_range





   def 						__lowerCAmelCase				(						self      ):
         _lowerCAmelCase			      =	copy.deepcopy(self.__dict__      )
         _lowerCAmelCase			      =	self.vision_config.to_dict()
         _lowerCAmelCase			      =	self.prompt_encoder_config.to_dict()
         _lowerCAmelCase			      =	self.mask_decoder_config.to_dict()
         _lowerCAmelCase			      =	self.__class__.model_type
         return output