


import os
import tempfile
from functools import partial
from unittest import TestCase
from unittest.mock import patch

import datasets
import datasets.config

from .utils import require_beam



class 							A   (	datasets.BeamBasedBuilder							):
					'''simple docstring'''






					def   lowerCamelCase__					(self   :						Tuple		)						->       Tuple:




							"""simple docstring"""


							return datasets.DatasetInfo(
							    features=datasets.Features({"""content""": datasets.Value("""string"""		)}		)				, supervised_keys=_UpperCAmelCase				, )

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




							"""simple docstring"""


							return [datasets.SplitGenerator(name=datasets.Split.TRAIN				, gen_kwargs={"""examples""": get_test_dummy_examples()}		)]


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




							"""simple docstring"""


							import apache_beam as beam

							return pipeline | "Load Examples" >> beam.Create(_UpperCAmelCase		)






class 							A   (	datasets.BeamBasedBuilder							):
					'''simple docstring'''






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




							"""simple docstring"""


							return datasets.DatasetInfo(
							    features=datasets.Features({"""a""": datasets.Sequence({"""b""": datasets.Value("""string"""		)}		)}		)				, supervised_keys=_UpperCAmelCase				, )

					def   lowerCamelCase__					(self   :						Optional[Any]				, _UpperCAmelCase   :						Tuple				, _UpperCAmelCase   :						Dict		)						->       str:




							"""simple docstring"""


							return [
							    datasets.SplitGenerator(name=datasets.Split.TRAIN				, gen_kwargs={"""examples""": get_test_nested_examples()}		)
							]


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




							"""simple docstring"""


							import apache_beam as beam

							return pipeline | "Load Examples" >> beam.Create(_UpperCAmelCase		)






def 			UpperCamelCase (     )       ->  str:
		"""simple docstring"""




		return [(i, {"content": content}) for i, content in enumerate(["""foo""", """bar""", """foobar"""]					)]






def 			UpperCamelCase (     )       ->  Dict:
		"""simple docstring"""




		return [(i, {"a": {"b": [content]}}) for i, content in enumerate(["""foo""", """bar""", """foobar"""]					)]







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






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




							"""simple docstring"""


							lowercase__					      =		len(get_test_dummy_examples()		)
							with tempfile.TemporaryDirectory() as tmp_cache_dir:
									lowercase__					      =		DummyBeamDataset(cache_dir=_UpperCAmelCase				, beam_runner="""DirectRunner"""		)
									builder.download_and_prepare()
									self.assertTrue(
									    os.path.exists(
									        os.path.join(_UpperCAmelCase				, builder.name				, """default"""				, """0.0.0"""				, f'''{builder.name}-train.arrow'''		)		)		)
									self.assertDictEqual(builder.info.features				, datasets.Features({"""content""": datasets.Value("""string"""		)}		)		)
									lowercase__					      =		builder.as_dataset()
									self.assertEqual(dset["""train"""].num_rows				, _UpperCAmelCase		)
									self.assertEqual(dset["""train"""].info.splits["""train"""].num_examples				, _UpperCAmelCase		)
									self.assertDictEqual(dset["""train"""][0]				, get_test_dummy_examples()[0][1]		)
									self.assertDictEqual(
									    dset["""train"""][expected_num_examples - 1]				, get_test_dummy_examples()[expected_num_examples - 1][1]		)
									self.assertTrue(
									    os.path.exists(os.path.join(_UpperCAmelCase				, builder.name				, """default"""				, """0.0.0"""				, """dataset_info.json"""		)		)		)
									del dset

					@require_beam
					def   lowerCamelCase__					(self   :						str		)						->       Dict:




							"""simple docstring"""


							import apache_beam as beam

							lowercase__					      =		beam.io.parquetio.WriteToParquet

							lowercase__					      =		len(get_test_dummy_examples()		)
							with tempfile.TemporaryDirectory() as tmp_cache_dir:
									lowercase__					      =		DummyBeamDataset(cache_dir=_UpperCAmelCase				, beam_runner="""DirectRunner"""		)
									with patch("""apache_beam.io.parquetio.WriteToParquet"""		) as write_parquet_mock:
											lowercase__					      =		partial(_UpperCAmelCase				, num_shards=2		)
											builder.download_and_prepare()
									self.assertTrue(
									    os.path.exists(
									        os.path.join(
									            _UpperCAmelCase				, builder.name				, """default"""				, """0.0.0"""				, f'''{builder.name}-train-00000-of-00002.arrow'''		)		)		)
									self.assertTrue(
									    os.path.exists(
									        os.path.join(
									            _UpperCAmelCase				, builder.name				, """default"""				, """0.0.0"""				, f'''{builder.name}-train-00000-of-00002.arrow'''		)		)		)
									self.assertDictEqual(builder.info.features				, datasets.Features({"""content""": datasets.Value("""string"""		)}		)		)
									lowercase__					      =		builder.as_dataset()
									self.assertEqual(dset["""train"""].num_rows				, _UpperCAmelCase		)
									self.assertEqual(dset["""train"""].info.splits["""train"""].num_examples				, _UpperCAmelCase		)
									# Order is not preserved when sharding, so we just check that all the elements are there
									self.assertListEqual(sorted(dset["""train"""]["""content"""]		)				, sorted(["""foo""", """bar""", """foobar"""]		)		)
									self.assertTrue(
									    os.path.exists(os.path.join(_UpperCAmelCase				, builder.name				, """default"""				, """0.0.0"""				, """dataset_info.json"""		)		)		)
									del dset

					@require_beam
					def   lowerCamelCase__					(self   :						Dict		)						->       int:




							"""simple docstring"""


							with tempfile.TemporaryDirectory() as tmp_cache_dir:
									lowercase__					      =		DummyBeamDataset(cache_dir=_UpperCAmelCase		)
									self.assertRaises(datasets.builder.MissingBeamOptions				, builder.download_and_prepare		)


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




							"""simple docstring"""


							lowercase__					      =		len(get_test_nested_examples()		)
							with tempfile.TemporaryDirectory() as tmp_cache_dir:
									lowercase__					      =		NestedBeamDataset(cache_dir=_UpperCAmelCase				, beam_runner="""DirectRunner"""		)
									builder.download_and_prepare()
									self.assertTrue(
									    os.path.exists(
									        os.path.join(_UpperCAmelCase				, builder.name				, """default"""				, """0.0.0"""				, f'''{builder.name}-train.arrow'''		)		)		)
									self.assertDictEqual(
									    builder.info.features				, datasets.Features({"""a""": datasets.Sequence({"""b""": datasets.Value("""string"""		)}		)}		)		)
									lowercase__					      =		builder.as_dataset()
									self.assertEqual(dset["""train"""].num_rows				, _UpperCAmelCase		)
									self.assertEqual(dset["""train"""].info.splits["""train"""].num_examples				, _UpperCAmelCase		)
									self.assertDictEqual(dset["""train"""][0]				, get_test_nested_examples()[0][1]		)
									self.assertDictEqual(
									    dset["""train"""][expected_num_examples - 1]				, get_test_nested_examples()[expected_num_examples - 1][1]		)
									self.assertTrue(
									    os.path.exists(os.path.join(_UpperCAmelCase				, builder.name				, """default"""				, """0.0.0"""				, """dataset_info.json"""		)		)		)
									del dset





import warnings
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


A	:		List[str]    =       logging.get_logger(__name__)

A	:		List[Any]    =       {
    'nvidia/segformer-b0-finetuned-ade-512-512': (
        'https://huggingface.co/nvidia/segformer-b0-finetuned-ade-512-512/resolve/main/config.json'
    ),
    # See all SegFormer models at https://huggingface.co/models?filter=segformer
}



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






					A__      =			'''segformer'''

					def __init__(self   :						Dict				, _UpperCAmelCase   :						Optional[int]=3				, _UpperCAmelCase   :						Dict=4				, _UpperCAmelCase   :						Union[str, Any]=[2, 2, 2, 2]				, _UpperCAmelCase   :						List[str]=[8, 4, 2, 1]				, _UpperCAmelCase   :						str=[32, 64, 160, 256]				, _UpperCAmelCase   :						Optional[int]=[7, 3, 3, 3]				, _UpperCAmelCase   :						int=[4, 2, 2, 2]				, _UpperCAmelCase   :						str=[1, 2, 5, 8]				, _UpperCAmelCase   :						Optional[int]=[4, 4, 4, 4]				, _UpperCAmelCase   :						List[Any]="gelu"				, _UpperCAmelCase   :						Optional[Any]=0.0				, _UpperCAmelCase   :						Dict=0.0				, _UpperCAmelCase   :						Dict=0.1				, _UpperCAmelCase   :						List[str]=0.02				, _UpperCAmelCase   :						str=0.1				, _UpperCAmelCase   :						Optional[Any]=1E-6				, _UpperCAmelCase   :						Optional[Any]=256				, _UpperCAmelCase   :						Any=255				, **_UpperCAmelCase   :						str				, )						->       Tuple:




							"""simple docstring"""


							super().__init__(**_UpperCAmelCase		)

							if "reshape_last_stage" in kwargs and kwargs["reshape_last_stage"] is False:
									warnings.warn(
									    """Reshape_last_stage is set to False in this config. This argument is deprecated and will soon be"""
									    """ removed, as the behaviour will default to that of reshape_last_stage = True."""				, _UpperCAmelCase				, )

							lowercase__					      =		num_channels
							lowercase__					      =		num_encoder_blocks
							lowercase__					      =		depths
							lowercase__					      =		sr_ratios
							lowercase__					      =		hidden_sizes
							lowercase__					      =		patch_sizes
							lowercase__					      =		strides
							lowercase__					      =		mlp_ratios
							lowercase__					      =		num_attention_heads
							lowercase__					      =		hidden_act
							lowercase__					      =		hidden_dropout_prob
							lowercase__					      =		attention_probs_dropout_prob
							lowercase__					      =		classifier_dropout_prob
							lowercase__					      =		initializer_range
							lowercase__					      =		drop_path_rate
							lowercase__					      =		layer_norm_eps
							lowercase__					      =		decoder_hidden_size
							lowercase__					      =		kwargs.get("""reshape_last_stage"""				, _UpperCAmelCase		)
							lowercase__					      =		semantic_loss_ignore_index






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






					A__      =			version.parse('''1.11'''							)

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




							"""simple docstring"""


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

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




							"""simple docstring"""


							return 1E-4


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




							"""simple docstring"""


							return 12

