




'''simple docstring'''



import io
import json

import fsspec
import pytest

from datasets import Dataset, DatasetDict, Features, NamedSplit, Value
from datasets.io.json import JsonDatasetReader, JsonDatasetWriter

from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases







def       _UpperCamelCase			(_lowerCamelCase				:  Optional[Any] ,						_lowerCamelCase				:  Optional[Any]						)-> Dict:




     '''simple docstring'''





     assert isinstance(_lowerCamelCase ,						_lowerCamelCase						)
     assert dataset.num_rows == 4
     assert dataset.num_columns == 3
     assert dataset.column_names == ["col_1", "col_2", "col_3"]
     for feature, expected_dtype in expected_features.items():
          assert dataset.features[feature].dtype == expected_dtype







@pytest.mark.parametrize('''keep_in_memory''' ,						[False, True]						)
def       _UpperCamelCase			(_lowerCamelCase				:  int ,						_lowerCamelCase				:  int ,						_lowerCamelCase				:  int						)-> Optional[Any]:




     '''simple docstring'''





     __snake_case            =	tmp_path / '''cache'''
     __snake_case            =	{'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
     with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
          __snake_case            =	JsonDatasetReader(_lowerCamelCase ,						cache_dir=_lowerCamelCase ,						keep_in_memory=_lowerCamelCase						).read()
     _check_json_dataset(_lowerCamelCase ,						_lowerCamelCase						)







@pytest.mark.parametrize(
    '''features''' ,						[
        None,
        {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''},
        {'''col_1''': '''string''', '''col_2''': '''string''', '''col_3''': '''string'''},
        {'''col_1''': '''int32''', '''col_2''': '''int32''', '''col_3''': '''int32'''},
        {'''col_1''': '''float32''', '''col_2''': '''float32''', '''col_3''': '''float32'''},
    ] ,						)
def       _UpperCamelCase			(_lowerCamelCase				:  Optional[Any] ,						_lowerCamelCase				:  Tuple ,						_lowerCamelCase				:  Optional[Any]						)-> int:




     '''simple docstring'''





     __snake_case            =	tmp_path / '''cache'''
     __snake_case            =	{'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
     __snake_case            =	features.copy() if features else default_expected_features
     __snake_case            =	(
         Features({feature: Value(_lowerCamelCase						) for feature, dtype in features.items()}						) if features is not None else None
     )
     __snake_case            =	JsonDatasetReader(_lowerCamelCase ,						features=_lowerCamelCase ,						cache_dir=_lowerCamelCase						).read()
     _check_json_dataset(_lowerCamelCase ,						_lowerCamelCase						)







@pytest.mark.parametrize(
    '''features''' ,						[
        None,
        {'''col_3''': '''float64''', '''col_1''': '''string''', '''col_2''': '''int64'''},
    ] ,						)
def       _UpperCamelCase			(_lowerCamelCase				:  Union[str, Any] ,						_lowerCamelCase				:  Optional[int] ,						_lowerCamelCase				:  Optional[int]						)-> str:




     '''simple docstring'''





     __snake_case            =	tmp_path / '''cache'''
     __snake_case            =	{'''col_3''': '''float64''', '''col_1''': '''string''', '''col_2''': '''int64'''}
     __snake_case            =	features.copy() if features else default_expected_features
     __snake_case            =	(
         Features({feature: Value(_lowerCamelCase						) for feature, dtype in features.items()}						) if features is not None else None
     )
     __snake_case            =	JsonDatasetReader(_lowerCamelCase ,						features=_lowerCamelCase ,						cache_dir=_lowerCamelCase						).read()
     assert isinstance(_lowerCamelCase ,						_lowerCamelCase						)
     assert dataset.num_rows == 2
     assert dataset.num_columns == 3
     assert dataset.column_names == ["col_3", "col_1", "col_2"]
     for feature, expected_dtype in expected_features.items():
          assert dataset.features[feature].dtype == expected_dtype







def       _UpperCamelCase			(_lowerCamelCase				:  Dict ,						_lowerCamelCase				:  Union[str, Any]						)-> Any:




     '''simple docstring'''





     __snake_case            =	{'''col_2''': '''int64''', '''col_3''': '''float64''', '''col_1''': '''string'''}
     __snake_case            =	features.copy()
     __snake_case            =	(
         Features({feature: Value(_lowerCamelCase						) for feature, dtype in features.items()}						) if features is not None else None
     )
     __snake_case            =	tmp_path / '''cache'''
     __snake_case            =	JsonDatasetReader(_lowerCamelCase ,						features=_lowerCamelCase ,						cache_dir=_lowerCamelCase						).read()
     assert isinstance(_lowerCamelCase ,						_lowerCamelCase						)
     assert dataset.num_rows == 2
     assert dataset.num_columns == 3
     assert dataset.column_names == ["col_2", "col_3", "col_1"]
     for feature, expected_dtype in expected_features.items():
          assert dataset.features[feature].dtype == expected_dtype







@pytest.mark.parametrize('''split''' ,						[None, NamedSplit('''train'''						), '''train''', '''test''']						)
def       _UpperCamelCase			(_lowerCamelCase				:  Optional[int] ,						_lowerCamelCase				:  List[str] ,						_lowerCamelCase				:  str						)-> int:




     '''simple docstring'''





     __snake_case            =	tmp_path / '''cache'''
     __snake_case            =	{'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
     __snake_case            =	JsonDatasetReader(_lowerCamelCase ,						cache_dir=_lowerCamelCase ,						split=_lowerCamelCase						).read()
     _check_json_dataset(_lowerCamelCase ,						_lowerCamelCase						)
     assert dataset.split == split if split else "train"







@pytest.mark.parametrize('''path_type''' ,						[str, list]						)
def       _UpperCamelCase			(_lowerCamelCase				:  Any ,						_lowerCamelCase				:  Optional[Any] ,						_lowerCamelCase				:  int						)-> List[str]:




     '''simple docstring'''





     if issubclass(_lowerCamelCase ,						_lowerCamelCase						):
          __snake_case            =	jsonl_path
     elif issubclass(_lowerCamelCase ,						_lowerCamelCase						):
          __snake_case            =	[jsonl_path]
     __snake_case            =	tmp_path / '''cache'''
     __snake_case            =	{'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
     __snake_case            =	JsonDatasetReader(_lowerCamelCase ,						cache_dir=_lowerCamelCase						).read()
     _check_json_dataset(_lowerCamelCase ,						_lowerCamelCase						)







def       _UpperCamelCase			(_lowerCamelCase				:  Optional[Any] ,						_lowerCamelCase				:  Any ,						_lowerCamelCase				:  Optional[int]=("train",)						)-> Tuple:




     '''simple docstring'''





     assert isinstance(_lowerCamelCase ,						_lowerCamelCase						)
     for split in splits:
          __snake_case            =	dataset_dict[split]
          assert dataset.num_rows == 4
          assert dataset.num_columns == 3
          assert dataset.column_names == ["col_1", "col_2", "col_3"]
          for feature, expected_dtype in expected_features.items():
               assert dataset.features[feature].dtype == expected_dtype







@pytest.mark.parametrize('''keep_in_memory''' ,						[False, True]						)
def       _UpperCamelCase			(_lowerCamelCase				:  Optional[Any] ,						_lowerCamelCase				:  List[Any] ,						_lowerCamelCase				:  Union[str, Any]						)-> str:




     '''simple docstring'''





     __snake_case            =	tmp_path / '''cache'''
     __snake_case            =	{'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
     with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
          __snake_case            =	JsonDatasetReader({'''train''': jsonl_path} ,						cache_dir=_lowerCamelCase ,						keep_in_memory=_lowerCamelCase						).read()
     _check_json_datasetdict(_lowerCamelCase ,						_lowerCamelCase						)







@pytest.mark.parametrize(
    '''features''' ,						[
        None,
        {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''},
        {'''col_1''': '''string''', '''col_2''': '''string''', '''col_3''': '''string'''},
        {'''col_1''': '''int32''', '''col_2''': '''int32''', '''col_3''': '''int32'''},
        {'''col_1''': '''float32''', '''col_2''': '''float32''', '''col_3''': '''float32'''},
    ] ,						)
def       _UpperCamelCase			(_lowerCamelCase				:  Union[str, Any] ,						_lowerCamelCase				:  Union[str, Any] ,						_lowerCamelCase				:  Dict						)-> List[str]:




     '''simple docstring'''





     __snake_case            =	tmp_path / '''cache'''
     __snake_case            =	{'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
     __snake_case            =	features.copy() if features else default_expected_features
     __snake_case            =	(
         Features({feature: Value(_lowerCamelCase						) for feature, dtype in features.items()}						) if features is not None else None
     )
     __snake_case            =	JsonDatasetReader({'''train''': jsonl_path} ,						features=_lowerCamelCase ,						cache_dir=_lowerCamelCase						).read()
     _check_json_datasetdict(_lowerCamelCase ,						_lowerCamelCase						)







@pytest.mark.parametrize('''split''' ,						[None, NamedSplit('''train'''						), '''train''', '''test''']						)
def       _UpperCamelCase			(_lowerCamelCase				:  str ,						_lowerCamelCase				:  Union[str, Any] ,						_lowerCamelCase				:  str						)-> int:




     '''simple docstring'''





     if split:
          __snake_case            =	{split: jsonl_path}
     else:
          __snake_case            =	'''train'''
          __snake_case            =	{'''train''': jsonl_path, '''test''': jsonl_path}
     __snake_case            =	tmp_path / '''cache'''
     __snake_case            =	{'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
     __snake_case            =	JsonDatasetReader(_lowerCamelCase ,						cache_dir=_lowerCamelCase						).read()
     _check_json_datasetdict(_lowerCamelCase ,						_lowerCamelCase ,						splits=list(path.keys()						)						)
     assert all(dataset[split].split == split for split in path.keys()						)







def       _UpperCamelCase			(_lowerCamelCase				:  List[Any]						)-> str:




     '''simple docstring'''





     return json.load(_lowerCamelCase						)







def       _UpperCamelCase			(_lowerCamelCase				:  Union[str, Any]						)-> Optional[int]:




     '''simple docstring'''





     return [json.loads(_lowerCamelCase						) for line in buffer]







class      lowerCAmelCase :


       @pytest.mark.parametrize('''lines, load_json_function'''     ,	[(True, load_json_lines), (False, load_json)]	)
       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)   ->	Dict:



            '''simple docstring'''




            with io.BytesIO() as buffer:
                 JsonDatasetWriter(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	lines=__SCREAMING_SNAKE_CASE	).write()
                 buffer.seek(0	)
                 __snake_case            =	load_json_function(__SCREAMING_SNAKE_CASE	)
            assert isinstance(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)
            assert isinstance(exported_content[0]     ,	__SCREAMING_SNAKE_CASE	)
            assert len(__SCREAMING_SNAKE_CASE	) == 10


       @pytest.mark.parametrize(
           '''orient, container, keys, len_at'''     ,	[
               ('''records''', list, {'''tokens''', '''labels''', '''answers''', '''id'''}, None),
               ('''split''', dict, {'''columns''', '''data'''}, '''data'''),
               ('''index''', dict, set('''0123456789'''	), None),
               ('''columns''', dict, {'''tokens''', '''labels''', '''answers''', '''id'''}, '''tokens'''),
               ('''values''', list, None, None),
               ('''table''', dict, {'''schema''', '''data'''}, '''data'''),
           ]     ,	)
       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)   ->	str:



            '''simple docstring'''




            with io.BytesIO() as buffer:
                 JsonDatasetWriter(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	lines=__SCREAMING_SNAKE_CASE     ,	orient=__SCREAMING_SNAKE_CASE	).write()
                 buffer.seek(0	)
                 __snake_case            =	load_json(__SCREAMING_SNAKE_CASE	)
            assert isinstance(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)
            if keys:
                 if container is dict:
                      assert exported_content.keys() == keys
                 else:
                      assert exported_content[0].keys() == keys
            else:
                 assert not hasattr(__SCREAMING_SNAKE_CASE     ,	'''keys'''	) and not hasattr(exported_content[0]     ,	'''keys'''	)
            if len_at:
                 assert len(exported_content[len_at]	) == 10
            else:
                 assert len(__SCREAMING_SNAKE_CASE	) == 10


       @pytest.mark.parametrize('''lines, load_json_function'''     ,	[(True, load_json_lines), (False, load_json)]	)
       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)   ->	List[str]:



            '''simple docstring'''




            with io.BytesIO() as buffer:
                 JsonDatasetWriter(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	lines=__SCREAMING_SNAKE_CASE     ,	num_proc=2	).write()
                 buffer.seek(0	)
                 __snake_case            =	load_json_function(__SCREAMING_SNAKE_CASE	)
            assert isinstance(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)
            assert isinstance(exported_content[0]     ,	__SCREAMING_SNAKE_CASE	)
            assert len(__SCREAMING_SNAKE_CASE	) == 10


       @pytest.mark.parametrize(
           '''orient, container, keys, len_at'''     ,	[
               ('''records''', list, {'''tokens''', '''labels''', '''answers''', '''id'''}, None),
               ('''split''', dict, {'''columns''', '''data'''}, '''data'''),
               ('''index''', dict, set('''0123456789'''	), None),
               ('''columns''', dict, {'''tokens''', '''labels''', '''answers''', '''id'''}, '''tokens'''),
               ('''values''', list, None, None),
               ('''table''', dict, {'''schema''', '''data'''}, '''data'''),
           ]     ,	)
       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)   ->	int:



            '''simple docstring'''




            with io.BytesIO() as buffer:
                 JsonDatasetWriter(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	lines=__SCREAMING_SNAKE_CASE     ,	orient=__SCREAMING_SNAKE_CASE     ,	num_proc=2	).write()
                 buffer.seek(0	)
                 __snake_case            =	load_json(__SCREAMING_SNAKE_CASE	)
            assert isinstance(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)
            if keys:
                 if container is dict:
                      assert exported_content.keys() == keys
                 else:
                      assert exported_content[0].keys() == keys
            else:
                 assert not hasattr(__SCREAMING_SNAKE_CASE     ,	'''keys'''	) and not hasattr(exported_content[0]     ,	'''keys'''	)
            if len_at:
                 assert len(exported_content[len_at]	) == 10
            else:
                 assert len(__SCREAMING_SNAKE_CASE	) == 10


       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE	)   ->	Optional[Any]:



            '''simple docstring'''




            with pytest.raises(__SCREAMING_SNAKE_CASE	):
                 with io.BytesIO() as buffer:
                      JsonDatasetWriter(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	num_proc=0	)




       @pytest.mark.parametrize('''compression, extension'''     ,	[('''gzip''', '''gz'''), ('''bz2''', '''bz2'''), ('''xz''', '''xz''')]	)
       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)   ->	str:



            '''simple docstring'''




            __snake_case            =	tmp_path_factory.mktemp('''data'''	) / F'''test.json.{extension}'''
            __snake_case            =	str(shared_datadir / F'''test_file.json.{extension}'''	)
            JsonDatasetWriter(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	compression=__SCREAMING_SNAKE_CASE	).write()

            with fsspec.open(__SCREAMING_SNAKE_CASE     ,	'''rb'''     ,	compression='''infer'''	) as f:
                 __snake_case            =	f.read()
            with fsspec.open(__SCREAMING_SNAKE_CASE     ,	'''rb'''     ,	compression='''infer'''	) as f:
                 __snake_case            =	f.read()
            assert exported_content == original_content












'''simple docstring'''



from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


UpperCAmelCase_							:	int	     =		logging.get_logger(__name__)

UpperCAmelCase_							:	Dict	     =		{
    '''microsoft/swin-tiny-patch4-window7-224''': (
        '''https://huggingface.co/microsoft/swin-tiny-patch4-window7-224/resolve/main/config.json'''
    ),
    # See all Swin models at https://huggingface.co/models?filter=swin
}





class      lowerCAmelCase (			__lowerCAmelCase					, __lowerCAmelCase):
       __lowercase	:   List[Any]										=      '''swin'''

       __lowercase	:   str										=      {
           '''num_attention_heads''': '''num_heads''',
           '''num_hidden_layers''': '''num_layers''',
       }


       def __init__( self     ,	__SCREAMING_SNAKE_CASE=224     ,	__SCREAMING_SNAKE_CASE=4     ,	__SCREAMING_SNAKE_CASE=3     ,	__SCREAMING_SNAKE_CASE=96     ,	__SCREAMING_SNAKE_CASE=[2, 2, 6, 2]     ,	__SCREAMING_SNAKE_CASE=[3, 6, 12, 24]     ,	__SCREAMING_SNAKE_CASE=7     ,	__SCREAMING_SNAKE_CASE=4.0     ,	__SCREAMING_SNAKE_CASE=True     ,	__SCREAMING_SNAKE_CASE=0.0     ,	__SCREAMING_SNAKE_CASE=0.0     ,	__SCREAMING_SNAKE_CASE=0.1     ,	__SCREAMING_SNAKE_CASE="gelu"     ,	__SCREAMING_SNAKE_CASE=False     ,	__SCREAMING_SNAKE_CASE=0.02     ,	__SCREAMING_SNAKE_CASE=1E-5     ,	__SCREAMING_SNAKE_CASE=32     ,	__SCREAMING_SNAKE_CASE=None     ,	__SCREAMING_SNAKE_CASE=None     ,	**__SCREAMING_SNAKE_CASE     ,	)   ->	int:



            '''simple docstring'''




            super().__init__(**__SCREAMING_SNAKE_CASE	)

            __snake_case            =	image_size
            __snake_case            =	patch_size
            __snake_case            =	num_channels
            __snake_case            =	embed_dim
            __snake_case            =	depths
            __snake_case            =	len(__SCREAMING_SNAKE_CASE	)
            __snake_case            =	num_heads
            __snake_case            =	window_size
            __snake_case            =	mlp_ratio
            __snake_case            =	qkv_bias
            __snake_case            =	hidden_dropout_prob
            __snake_case            =	attention_probs_dropout_prob
            __snake_case            =	drop_path_rate
            __snake_case            =	hidden_act
            __snake_case            =	use_absolute_embeddings
            __snake_case            =	layer_norm_eps
            __snake_case            =	initializer_range
            __snake_case            =	encoder_stride
            # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel
            # this indicates the channel dimension after the last stage of the model
            __snake_case            =	int(embed_dim * 2 ** (len(__SCREAMING_SNAKE_CASE	) - 1)	)
            __snake_case            =	['''stem'''] + [F'''stage{idx}''' for idx in range(1     ,	len(__SCREAMING_SNAKE_CASE	) + 1	)]
            __snake_case       ,     __snake_case            =	get_aligned_output_features_output_indices(
                out_features=__SCREAMING_SNAKE_CASE     ,	out_indices=__SCREAMING_SNAKE_CASE     ,	stage_names=self.stage_names	)





class      lowerCAmelCase (			__lowerCAmelCase):
       __lowercase	:   Optional[int]										=      version.parse('''1.11''')


       @property
       def    lowerCAmelCase   ( self	)   ->	Mapping[str, Mapping[int, str]]:



            '''simple docstring'''




            return OrderedDict(
                [
                    ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}),
                ]	)




       @property
       def    lowerCAmelCase   ( self	)   ->	float:



            '''simple docstring'''




            return 1E-4






