


import os
import tempfile
from pathlib import Path
from unittest import TestCase

import pyarrow as pa
import pytest

from datasets.arrow_dataset import Dataset
from datasets.arrow_reader import ArrowReader, BaseReader, FileInstructions, ReadInstruction, make_file_instructions
from datasets.info import DatasetInfo
from datasets.splits import NamedSplit, Split, SplitDict, SplitInfo

from .utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases



class 							A					( UpperCAmelCase__							):
					'''simple docstring'''






					def 						lowerCamelCase__  (self					:		Union[str, Any]   ,      _UpperCAmelCase					:		Optional[Any]   ,      _UpperCAmelCase					:		List[Any]=False	)		->     Any:




							"""simple docstring"""


							lowercase__    ,			lowercase__    ,			lowercase__      =	(
							    filename_skip_take["""filename"""],
							    filename_skip_take["""skip"""] if """skip""" in filename_skip_take else None,
							    filename_skip_take["""take"""] if """take""" in filename_skip_take else None,
							)
							open(os.path.join(_UpperCAmelCase	)   ,      """wb"""	).close()
							lowercase__      =	pa.Table.from_pydict({"""filename""": [Path(_UpperCAmelCase	).name] * 100}	)
							if take == -1:
									lowercase__      =	len(_UpperCAmelCase	) - skip
							if skip is not None and take is not None:
									lowercase__      =	pa_table.slice(_UpperCAmelCase   ,      _UpperCAmelCase	)
							return pa_table






class 							A					( UpperCAmelCase__							):
					'''simple docstring'''






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




							"""simple docstring"""


							lowercase__      =	"""my_name"""
							lowercase__      =	SplitInfo(name="""train"""   ,      num_examples=100	)
							lowercase__      =	SplitInfo(name="""test"""   ,      num_examples=100	)
							lowercase__      =	[train_info, test_info]
							lowercase__      =	SplitDict()
							split_dict.add(_UpperCAmelCase	)
							split_dict.add(_UpperCAmelCase	)
							lowercase__      =	DatasetInfo(splits=_UpperCAmelCase	)

							with tempfile.TemporaryDirectory() as tmp_dir:
									lowercase__      =	ReaderTest(_UpperCAmelCase   ,      _UpperCAmelCase	)

									lowercase__      =	"""test[:33%]"""
									lowercase__      =	Dataset(**reader.read(_UpperCAmelCase   ,      _UpperCAmelCase   ,      _UpperCAmelCase	)	)
									self.assertEqual(dset["""filename"""][0]   ,      f'''{name}-test'''	)
									self.assertEqual(dset.num_rows   ,      33	)
									self.assertEqual(dset.num_columns   ,      1	)

									lowercase__      =	["""train""", """test[:33%]"""]
									lowercase__      =	[Split.TRAIN, ReadInstruction.from_spec("""test[:33%]"""	)]
									for instructions in [instructionsa, instructionsa]:
											lowercase__      =	[reader.read(_UpperCAmelCase   ,      _UpperCAmelCase   ,      _UpperCAmelCase	) for instr in instructions]
											lowercase__    ,			lowercase__      =	(Dataset(**_UpperCAmelCase	) for dataset_kwargs in datasets_kwargs)
											self.assertEqual(train_dset["""filename"""][0]   ,      f'''{name}-train'''	)
											self.assertEqual(train_dset.num_rows   ,      100	)
											self.assertEqual(train_dset.num_columns   ,      1	)
											self.assertIsInstance(train_dset.split   ,      _UpperCAmelCase	)
											self.assertEqual(str(train_dset.split	)   ,      """train"""	)
											self.assertEqual(test_dset["""filename"""][0]   ,      f'''{name}-test'''	)
											self.assertEqual(test_dset.num_rows   ,      33	)
											self.assertEqual(test_dset.num_columns   ,      1	)
											self.assertIsInstance(test_dset.split   ,      _UpperCAmelCase	)
											self.assertEqual(str(test_dset.split	)   ,      """test[:33%]"""	)
											del train_dset, test_dset

					def 						lowerCamelCase__  (self					:		Optional[int]	)		->     int:




							"""simple docstring"""


							lowercase__      =	"""my_name"""
							lowercase__      =	SplitInfo(name="""train"""   ,      num_examples=1000   ,      shard_lengths=[100] * 10	)
							lowercase__      =	[train_info]
							lowercase__      =	SplitDict()
							split_dict.add(_UpperCAmelCase	)
							lowercase__      =	DatasetInfo(splits=_UpperCAmelCase	)

							with tempfile.TemporaryDirectory() as tmp_dir:
									lowercase__      =	ReaderTest(_UpperCAmelCase   ,      _UpperCAmelCase	)

									lowercase__      =	"""train[:33%]"""
									lowercase__      =	Dataset(**reader.read(_UpperCAmelCase   ,      _UpperCAmelCase   ,      _UpperCAmelCase	)	)
									self.assertEqual(dset["""filename"""][0]   ,      f'''{name}-train-00000-of-00010'''	)
									self.assertEqual(dset["""filename"""][-1]   ,      f'''{name}-train-00003-of-00010'''	)
									self.assertEqual(dset.num_rows   ,      330	)
									self.assertEqual(dset.num_columns   ,      1	)


					def 						lowerCamelCase__  (self					:		List[Any]	)		->     List[Any]:




							"""simple docstring"""


							lowercase__      =	SplitInfo(name="""train"""   ,      num_examples=100	)
							lowercase__      =	SplitInfo(name="""test"""   ,      num_examples=100	)
							lowercase__      =	SplitDict()
							split_dict.add(_UpperCAmelCase	)
							split_dict.add(_UpperCAmelCase	)
							lowercase__      =	DatasetInfo(splits=_UpperCAmelCase	)

							with tempfile.TemporaryDirectory() as tmp_dir:
									lowercase__      =	ReaderTest(_UpperCAmelCase   ,      _UpperCAmelCase	)

									lowercase__      =	[
									    {"""filename""": os.path.join(_UpperCAmelCase   ,      """train"""	)},
									    {"""filename""": os.path.join(_UpperCAmelCase   ,      """test"""	), """skip""": 10, """take""": 10},
									]
									lowercase__      =	Dataset(**reader.read_files(_UpperCAmelCase   ,      original_instructions="""train+test[10:20]"""	)	)
									self.assertEqual(dset.num_rows   ,      110	)
									self.assertEqual(dset.num_columns   ,      1	)
									del dset






@pytest.mark.parametrize("""in_memory"""					,					[False, True]  )
def 				UpperCamelCase      (			__magic_name__  :							Union[str, Any]					,					__magic_name__  :							int					,					__magic_name__  :							Optional[Any]  )     ->						int:
		"""simple docstring"""




		lowercase__      =	arrow_file
		with assert_arrow_memory_increases() if in_memory else assert_arrow_memory_doesnt_increase():
				lowercase__      =	ArrowReader.read_table(__magic_name__					,					in_memory=__magic_name__  )
		assert table.shape == dataset.data.shape
		assert set(table.column_names  ) == set(dataset.data.column_names  )
		assert dict(table.to_pydict()  ) == dict(dataset.data.to_pydict()  )  # to_pydict returns OrderedDict






@pytest.mark.parametrize("""in_memory"""					,					[False, True]  )
def 				UpperCamelCase      (			__magic_name__  :							Any					,					__magic_name__  :							Optional[Any]					,					__magic_name__  :							List[Any]  )     ->						List[Any]:
		"""simple docstring"""




		lowercase__      =	arrow_file
		lowercase__      =	ArrowReader(""""""					,					__magic_name__  )
		with assert_arrow_memory_increases() if in_memory else assert_arrow_memory_doesnt_increase():
				lowercase__      =	reader.read_files([{"""filename""": filename}]					,					in_memory=__magic_name__  )
		assert dataset_kwargs.keys() == {"arrow_table", "info", "split"}
		lowercase__      =	dataset_kwargs["""arrow_table"""]
		assert table.shape == dataset.data.shape
		assert set(table.column_names  ) == set(dataset.data.column_names  )
		assert dict(table.to_pydict()  ) == dict(dataset.data.to_pydict()  )  # to_pydict returns OrderedDict






def 				UpperCamelCase      (			)     ->						List[str]:
		"""simple docstring"""




		assert ReadInstruction("""train"""					,					to=10					,					unit="""abs"""  ).to_spec() == "train[:10]"
		assert ReadInstruction("""train"""					,					from_=-80					,					to=10					,					unit="""%"""  ).to_spec() == "train[-80%:10%]"

		lowercase__      =	"""train+test"""
		assert ReadInstruction.from_spec(__magic_name__  ).to_spec() == spec_train_test

		lowercase__      =	"""train[2:10]"""
		assert ReadInstruction.from_spec(__magic_name__  ).to_spec() == spec_train_abs

		lowercase__      =	"""train[15%:-20%]"""
		assert ReadInstruction.from_spec(__magic_name__  ).to_spec() == spec_train_pct

		lowercase__      =	"""train[:10%](closest)"""
		assert ReadInstruction.from_spec(__magic_name__  ).to_spec() == "train[:10%]"

		lowercase__      =	"""train[:10%](pct1_dropremainder)"""
		assert ReadInstruction.from_spec(__magic_name__  ).to_spec() == spec_train_pct_rounding

		lowercase__      =	"""train[:10%](pct1_dropremainder)+test[-10%:](pct1_dropremainder)"""
		assert ReadInstruction.from_spec(__magic_name__  ).to_spec() == spec_train_test_pct_rounding






def 				UpperCamelCase      (			)     ->						Optional[int]:
		"""simple docstring"""




		lowercase__      =	"""dummy"""
		lowercase__      =	[SplitInfo(name="""train"""					,					num_examples=100  )]
		lowercase__      =	"""train[:33%]"""
		lowercase__      =	"""arrow"""
		lowercase__      =	"""prefix"""

		lowercase__      =	make_file_instructions(__magic_name__					,					__magic_name__					,					__magic_name__					,					__magic_name__					,					__magic_name__  )
		assert isinstance(__magic_name__					,					__magic_name__  )
		assert file_instructions.num_examples == 33
		assert file_instructions.file_instructions == [
		    {"filename": os.path.join(__magic_name__					,					f'''{name}-train.arrow'''  ), "skip": 0, "take": 33}
		]

		lowercase__      =	[SplitInfo(name="""train"""					,					num_examples=100					,					shard_lengths=[10] * 10  )]
		lowercase__      =	make_file_instructions(__magic_name__					,					__magic_name__					,					__magic_name__					,					__magic_name__					,					__magic_name__  )
		assert isinstance(__magic_name__					,					__magic_name__  )
		assert file_instructions.num_examples == 33
		assert file_instructions.file_instructions == [
		    {"filename": os.path.join(__magic_name__					,					f'''{name}-train-00000-of-00010.arrow'''  ), "skip": 0, "take": -1},
		    {"filename": os.path.join(__magic_name__					,					f'''{name}-train-00001-of-00010.arrow'''  ), "skip": 0, "take": -1},
		    {"filename": os.path.join(__magic_name__					,					f'''{name}-train-00002-of-00010.arrow'''  ), "skip": 0, "take": -1},
		    {"filename": os.path.join(__magic_name__					,					f'''{name}-train-00003-of-00010.arrow'''  ), "skip": 0, "take": 3},
		]







@pytest.mark.parametrize("""name, expected_exception"""					,					[(None, TypeError), ("""""", ValueError)]  )
def 				UpperCamelCase      (			__magic_name__  :							Union[str, Any]					,					__magic_name__  :							Optional[Any]  )     ->						Optional[Any]:
		"""simple docstring"""




		lowercase__      =	[SplitInfo(name="""train"""					,					num_examples=100  )]
		lowercase__      =	"""train"""
		lowercase__      =	"""arrow"""
		lowercase__      =	"""prefix_path"""
		with pytest.raises(__magic_name__  ):
				lowercase__      =	make_file_instructions(__magic_name__					,					__magic_name__					,					__magic_name__					,					__magic_name__					,					__magic_name__  )





import copy

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


A :	Optional[Any]												=     logging.get_logger(__name__)

A :	Union[str, 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 							A					( UpperCAmelCase__							):
					'''simple docstring'''






					def __init__(self					:		Dict   ,      _UpperCAmelCase					:		List[str]=256   ,      _UpperCAmelCase					:		Tuple=1024   ,      _UpperCAmelCase					:		List[str]=16   ,      _UpperCAmelCase					:		Optional[Any]=16   ,      _UpperCAmelCase					:		Union[str, Any]=4   ,      _UpperCAmelCase					:		str="gelu"   ,      _UpperCAmelCase					:		List[str]=1E-6   ,      **_UpperCAmelCase					:		List[Any]   ,      )		->     Tuple:




							"""simple docstring"""


							super().__init__(**_UpperCAmelCase	)
							lowercase__      =	hidden_size
							lowercase__      =	image_size
							lowercase__      =	patch_size
							lowercase__      =	image_size // patch_size
							lowercase__      =	mask_input_channels
							lowercase__      =	num_point_embeddings
							lowercase__      =	hidden_act
							lowercase__      =	layer_norm_eps






class 							A					( UpperCAmelCase__							):
					'''simple docstring'''






					def __init__(self					:		List[str]   ,      _UpperCAmelCase					:		Optional[int]=256   ,      _UpperCAmelCase					:		int="relu"   ,      _UpperCAmelCase					:		Union[str, Any]=2048   ,      _UpperCAmelCase					:		int=2   ,      _UpperCAmelCase					:		Optional[Any]=8   ,      _UpperCAmelCase					:		Any=2   ,      _UpperCAmelCase					:		Tuple=3   ,      _UpperCAmelCase					:		List[str]=3   ,      _UpperCAmelCase					:		List[str]=256   ,      _UpperCAmelCase					:		Tuple=1E-6   ,      **_UpperCAmelCase					:		int   ,      )		->     Tuple:




							"""simple docstring"""


							super().__init__(**_UpperCAmelCase	)
							lowercase__      =	hidden_size
							lowercase__      =	hidden_act
							lowercase__      =	mlp_dim
							lowercase__      =	num_hidden_layers
							lowercase__      =	num_attention_heads
							lowercase__      =	attention_downsample_rate
							lowercase__      =	num_multimask_outputs
							lowercase__      =	iou_head_depth
							lowercase__      =	iou_head_hidden_dim
							lowercase__      =	layer_norm_eps






class 							A					( UpperCAmelCase__							):
					'''simple docstring'''






					def __init__(self					:		Optional[int]   ,      _UpperCAmelCase					:		List[Any]=768   ,      _UpperCAmelCase					:		Union[str, Any]=256   ,      _UpperCAmelCase					:		Union[str, Any]=12   ,      _UpperCAmelCase					:		Optional[Any]=12   ,      _UpperCAmelCase					:		Tuple=3   ,      _UpperCAmelCase					:		Optional[int]=1024   ,      _UpperCAmelCase					:		str=16   ,      _UpperCAmelCase					:		Tuple="gelu"   ,      _UpperCAmelCase					:		Any=1E-0_6   ,      _UpperCAmelCase					:		Any=0.0   ,      _UpperCAmelCase					:		str=1E-1_0   ,      _UpperCAmelCase					:		Tuple=True   ,      _UpperCAmelCase					:		Tuple=4.0   ,      _UpperCAmelCase					:		Optional[Any]=True   ,      _UpperCAmelCase					:		Optional[int]=True   ,      _UpperCAmelCase					:		Optional[Any]=14   ,      _UpperCAmelCase					:		Optional[int]=[2, 5, 8, 11]   ,      _UpperCAmelCase					:		List[Any]=128   ,      _UpperCAmelCase					:		int=None   ,      **_UpperCAmelCase					:		int   ,      )		->     Dict:




							"""simple docstring"""


							super().__init__(**_UpperCAmelCase	)

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






class 							A					( UpperCAmelCase__							):
					'''simple docstring'''






					A__    =				'''sam'''
					A__    =				True

					def __init__(self					:		List[str]   ,      _UpperCAmelCase					:		Any=None   ,      _UpperCAmelCase					:		List[Any]=None   ,      _UpperCAmelCase					:		List[Any]=None   ,      _UpperCAmelCase					:		List[Any]=0.02   ,      **_UpperCAmelCase					:		Optional[Any]   ,      )		->     Dict:




							"""simple docstring"""


							super().__init__(**_UpperCAmelCase	)
							lowercase__      =	vision_config if vision_config is not None else {}
							lowercase__      =	prompt_encoder_config if prompt_encoder_config is not None else {}
							lowercase__      =	mask_decoder_config if mask_decoder_config is not None else {}

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

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


					def 						lowerCamelCase__  (self					:		Union[str, Any]	)		->     Tuple:




							"""simple docstring"""


							lowercase__      =	copy.deepcopy(self.__dict__	)
							lowercase__      =	self.vision_config.to_dict()
							lowercase__      =	self.prompt_encoder_config.to_dict()
							lowercase__      =	self.mask_decoder_config.to_dict()
							lowercase__      =	self.__class__.model_type
							return output

