





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  (					lowercase__				):






							"""simple docstring"""

							def   A_	(   self					:  str				,						a					:  Any				,						a					:  List[str]=False     )						->Tuple:
													SCREAMING_SNAKE_CASE__,				SCREAMING_SNAKE_CASE__,				SCREAMING_SNAKE_CASE__  :					int =       (
													    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(a     )				,						"wb"     ).close()
													SCREAMING_SNAKE_CASE__  :					Optional[int] =       pa.Table.from_pydict({"filename": [Path(a     ).name] * 1_00}     )
													if take == -1:
																			SCREAMING_SNAKE_CASE__  :					Any =       len(a     ) - skip
													if skip is not None and take is not None:
																			SCREAMING_SNAKE_CASE__  :					Tuple =       pa_table.slice(a				,						a     )
													return pa_table







class  _a  (					lowercase__				):






							"""simple docstring"""

							def   A_	(   self					:  str     )						->List[str]:
													SCREAMING_SNAKE_CASE__  :					str =       "my_name"
													SCREAMING_SNAKE_CASE__  :					Dict =       SplitInfo(name="train"				,						num_examples=1_00     )
													SCREAMING_SNAKE_CASE__  :					List[str] =       SplitInfo(name="test"				,						num_examples=1_00     )
													SCREAMING_SNAKE_CASE__  :					List[Any] =       [train_info, test_info]
													SCREAMING_SNAKE_CASE__  :					List[str] =       SplitDict()
													split_dict.add(a     )
													split_dict.add(a     )
													SCREAMING_SNAKE_CASE__  :					Optional[Any] =       DatasetInfo(splits=a     )

													with tempfile.TemporaryDirectory() as tmp_dir:
																			SCREAMING_SNAKE_CASE__  :					Any =       ReaderTest(a				,						a     )

																			SCREAMING_SNAKE_CASE__  :					Optional[Any] =       "test[:33%]"
																			SCREAMING_SNAKE_CASE__  :					List[Any] =       Dataset(**reader.read(a				,						a				,						a     )     )
																			self.assertEqual(dset["filename"][0]				,						f"""{name}-test"""     )
																			self.assertEqual(dset.num_rows				,						33     )
																			self.assertEqual(dset.num_columns				,						1     )

																			SCREAMING_SNAKE_CASE__  :					Tuple =       ["train", "test[:33%]"]
																			SCREAMING_SNAKE_CASE__  :					Tuple =       [Split.TRAIN, ReadInstruction.from_spec("test[:33%]"     )]
																			for instructions in [instructionsa, instructionsa]:
																									SCREAMING_SNAKE_CASE__  :					Tuple =       [reader.read(a				,						a				,						a     ) for instr in instructions]
																									SCREAMING_SNAKE_CASE__,				SCREAMING_SNAKE_CASE__  :					Any =       (Dataset(**a     ) for dataset_kwargs in datasets_kwargs)
																									self.assertEqual(train_dset["filename"][0]				,						f"""{name}-train"""     )
																									self.assertEqual(train_dset.num_rows				,						1_00     )
																									self.assertEqual(train_dset.num_columns				,						1     )
																									self.assertIsInstance(train_dset.split				,						a     )
																									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				,						a     )
																									self.assertEqual(str(test_dset.split     )				,						"test[:33%]"     )
																									del train_dset, test_dset

							def   A_	(   self					:  Optional[int]     )						->Optional[Any]:
													SCREAMING_SNAKE_CASE__  :					List[Any] =       "my_name"
													SCREAMING_SNAKE_CASE__  :					List[Any] =       SplitInfo(name="train"				,						num_examples=10_00				,						shard_lengths=[1_00] * 10     )
													SCREAMING_SNAKE_CASE__  :					str =       [train_info]
													SCREAMING_SNAKE_CASE__  :					str =       SplitDict()
													split_dict.add(a     )
													SCREAMING_SNAKE_CASE__  :					Any =       DatasetInfo(splits=a     )

													with tempfile.TemporaryDirectory() as tmp_dir:
																			SCREAMING_SNAKE_CASE__  :					Optional[int] =       ReaderTest(a				,						a     )

																			SCREAMING_SNAKE_CASE__  :					List[Any] =       "train[:33%]"
																			SCREAMING_SNAKE_CASE__  :					str =       Dataset(**reader.read(a				,						a				,						a     )     )
																			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				,						3_30     )
																			self.assertEqual(dset.num_columns				,						1     )






							def   A_	(   self					:  Optional[int]     )						->int:
													SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       SplitInfo(name="train"				,						num_examples=1_00     )
													SCREAMING_SNAKE_CASE__  :					List[Any] =       SplitInfo(name="test"				,						num_examples=1_00     )
													SCREAMING_SNAKE_CASE__  :					int =       SplitDict()
													split_dict.add(a     )
													split_dict.add(a     )
													SCREAMING_SNAKE_CASE__  :					str =       DatasetInfo(splits=a     )

													with tempfile.TemporaryDirectory() as tmp_dir:
																			SCREAMING_SNAKE_CASE__  :					str =       ReaderTest(a				,						a     )

																			SCREAMING_SNAKE_CASE__  :					Dict =       [
																			    {"filename": os.path.join(a				,						"train"     )},
																			    {"filename": os.path.join(a				,						"test"     ), "skip": 10, "take": 10},
																			]
																			SCREAMING_SNAKE_CASE__  :					List[str] =       Dataset(**reader.read_files(a				,						original_instructions="train+test[10:20]"     )     )
																			self.assertEqual(dset.num_rows				,						1_10     )
																			self.assertEqual(dset.num_columns				,						1     )
																			del dset







@pytest.mark.parametrize("in_memory"      , [False, True]  )
def    UpperCAmelCase				(  _lowerCamelCase   :	str      , _lowerCamelCase   :	Optional[Any]      , _lowerCamelCase   :	Optional[Any]  ):





						'''simple docstring'''


						SCREAMING_SNAKE_CASE__  :					List[str] =       arrow_file
						with assert_arrow_memory_increases() if in_memory else assert_arrow_memory_doesnt_increase():
												SCREAMING_SNAKE_CASE__  :					str =       ArrowReader.read_table(_lowerCamelCase      , in_memory=_lowerCamelCase  )
						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				(  _lowerCamelCase   :	List[str]      , _lowerCamelCase   :	Tuple      , _lowerCamelCase   :	str  ):





						'''simple docstring'''


						SCREAMING_SNAKE_CASE__  :					Tuple =       arrow_file
						SCREAMING_SNAKE_CASE__  :					Any =       ArrowReader(""      , _lowerCamelCase  )
						with assert_arrow_memory_increases() if in_memory else assert_arrow_memory_doesnt_increase():
												SCREAMING_SNAKE_CASE__  :					Optional[int] =       reader.read_files([{"filename": filename}]      , in_memory=_lowerCamelCase  )
						assert dataset_kwargs.keys() == {"arrow_table", "info", "split"}
						SCREAMING_SNAKE_CASE__  :					Optional[Any] =       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				(  ):





						'''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%]"

						SCREAMING_SNAKE_CASE__  :					Dict =       "train+test"
						assert ReadInstruction.from_spec(_lowerCamelCase  ).to_spec() == spec_train_test

						SCREAMING_SNAKE_CASE__  :					List[str] =       "train[2:10]"
						assert ReadInstruction.from_spec(_lowerCamelCase  ).to_spec() == spec_train_abs

						SCREAMING_SNAKE_CASE__  :					Optional[int] =       "train[15%:-20%]"
						assert ReadInstruction.from_spec(_lowerCamelCase  ).to_spec() == spec_train_pct

						SCREAMING_SNAKE_CASE__  :					str =       "train[:10%](closest)"
						assert ReadInstruction.from_spec(_lowerCamelCase  ).to_spec() == "train[:10%]"

						SCREAMING_SNAKE_CASE__  :					Optional[int] =       "train[:10%](pct1_dropremainder)"
						assert ReadInstruction.from_spec(_lowerCamelCase  ).to_spec() == spec_train_pct_rounding

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







def    UpperCAmelCase				(  ):





						'''simple docstring'''


						SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       "dummy"
						SCREAMING_SNAKE_CASE__  :					Any =       [SplitInfo(name="train"      , num_examples=100  )]
						SCREAMING_SNAKE_CASE__  :					Dict =       "train[:33%]"
						SCREAMING_SNAKE_CASE__  :					int =       "arrow"
						SCREAMING_SNAKE_CASE__  :					Dict =       "prefix"

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

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







@pytest.mark.parametrize("name, expected_exception"      , [(None, TypeError), ("", ValueError)]  )
def    UpperCAmelCase				(  _lowerCamelCase   :	str      , _lowerCamelCase   :	List[str]  ):





						'''simple docstring'''


						SCREAMING_SNAKE_CASE__  :					List[str] =       [SplitInfo(name="train"      , num_examples=100  )]
						SCREAMING_SNAKE_CASE__  :					List[Any] =       "train"
						SCREAMING_SNAKE_CASE__  :					Tuple =       "arrow"
						SCREAMING_SNAKE_CASE__  :					Dict =       "prefix_path"
						with pytest.raises(_lowerCamelCase  ):
												SCREAMING_SNAKE_CASE__  :					Optional[Any] =       make_file_instructions(_lowerCamelCase      , _lowerCamelCase      , _lowerCamelCase      , _lowerCamelCase      , _lowerCamelCase  )






import copy

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


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

__lowercase							:Optional[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  (					lowercase__				):






							"""simple docstring"""

							def __init__(   self					:  List[Any]				,						a					:  int=2_56				,						a					:  Tuple=10_24				,						a					:  Optional[Any]=16				,						a					:  Dict=16				,						a					:  str=4				,						a					:  Union[str, Any]="gelu"				,						a					:  Optional[Any]=1E-6				,						**a					:  Dict				,						)						->Union[str, Any]:
													super().__init__(**a     )
													SCREAMING_SNAKE_CASE__  :					Any =       hidden_size
													SCREAMING_SNAKE_CASE__  :					List[Any] =       image_size
													SCREAMING_SNAKE_CASE__  :					List[str] =       patch_size
													SCREAMING_SNAKE_CASE__  :					str =       image_size // patch_size
													SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       mask_input_channels
													SCREAMING_SNAKE_CASE__  :					Any =       num_point_embeddings
													SCREAMING_SNAKE_CASE__  :					str =       hidden_act
													SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       layer_norm_eps







class  _a  (					lowercase__				):






							"""simple docstring"""

							def __init__(   self					:  Union[str, Any]				,						a					:  Optional[int]=2_56				,						a					:  Dict="relu"				,						a					:  Dict=20_48				,						a					:  Union[str, Any]=2				,						a					:  str=8				,						a					:  str=2				,						a					:  List[Any]=3				,						a					:  str=3				,						a					:  List[str]=2_56				,						a					:  str=1E-6				,						**a					:  Optional[int]				,						)						->Dict:
													super().__init__(**a     )
													SCREAMING_SNAKE_CASE__  :					List[Any] =       hidden_size
													SCREAMING_SNAKE_CASE__  :					List[Any] =       hidden_act
													SCREAMING_SNAKE_CASE__  :					Dict =       mlp_dim
													SCREAMING_SNAKE_CASE__  :					List[Any] =       num_hidden_layers
													SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       num_attention_heads
													SCREAMING_SNAKE_CASE__  :					List[Any] =       attention_downsample_rate
													SCREAMING_SNAKE_CASE__  :					List[str] =       num_multimask_outputs
													SCREAMING_SNAKE_CASE__  :					Optional[int] =       iou_head_depth
													SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       iou_head_hidden_dim
													SCREAMING_SNAKE_CASE__  :					Optional[int] =       layer_norm_eps







class  _a  (					lowercase__				):






							"""simple docstring"""

							def __init__(   self					:  Tuple				,						a					:  str=7_68				,						a					:  Tuple=2_56				,						a					:  Tuple=12				,						a					:  List[str]=12				,						a					:  Optional[Any]=3				,						a					:  str=10_24				,						a					:  str=16				,						a					:  Union[str, Any]="gelu"				,						a					:  Optional[int]=1E-06				,						a					:  Optional[Any]=0.0				,						a					:  str=1E-10				,						a					:  Optional[int]=True				,						a					:  Dict=4.0				,						a					:  Optional[Any]=True				,						a					:  List[str]=True				,						a					:  str=14				,						a					:  List[str]=[2, 5, 8, 11]				,						a					:  int=1_28				,						a					:  int=None				,						**a					:  List[str]				,						)						->Union[str, Any]:
													super().__init__(**a     )

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







class  _a  (					lowercase__				):






							"""simple docstring"""

							snake_case_							=    "sam"
							snake_case_							=    True

							def __init__(   self					:  str				,						a					:  Optional[int]=None				,						a					:  Any=None				,						a					:  Any=None				,						a					:  str=0.02				,						**a					:  int				,						)						->Optional[int]:
													super().__init__(**a     )
													SCREAMING_SNAKE_CASE__  :					Dict =       vision_config if vision_config is not None else {}
													SCREAMING_SNAKE_CASE__  :					str =       prompt_encoder_config if prompt_encoder_config is not None else {}
													SCREAMING_SNAKE_CASE__  :					Tuple =       mask_decoder_config if mask_decoder_config is not None else {}

													if isinstance(a				,						a     ):
																			SCREAMING_SNAKE_CASE__  :					Tuple =       vision_config.to_dict()
													if isinstance(a				,						a     ):
																			SCREAMING_SNAKE_CASE__  :					Dict =       prompt_encoder_config.to_dict()
													if isinstance(a				,						a     ):
																			SCREAMING_SNAKE_CASE__  :					List[Any] =       mask_decoder_config.to_dict()

													SCREAMING_SNAKE_CASE__  :					str =       SamVisionConfig(**a     )
													SCREAMING_SNAKE_CASE__  :					str =       SamPromptEncoderConfig(**a     )
													SCREAMING_SNAKE_CASE__  :					int =       SamMaskDecoderConfig(**a     )
													SCREAMING_SNAKE_CASE__  :					List[str] =       initializer_range






							def   A_	(   self					:  str     )						->Dict:
													SCREAMING_SNAKE_CASE__  :					int =       copy.deepcopy(self.__dict__     )
													SCREAMING_SNAKE_CASE__  :					str =       self.vision_config.to_dict()
													SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       self.prompt_encoder_config.to_dict()
													SCREAMING_SNAKE_CASE__  :					Optional[Any] =       self.mask_decoder_config.to_dict()
													SCREAMING_SNAKE_CASE__  :					Tuple =       self.__class__.model_type
													return output