





'''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


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

A_		:  Optional[Any]		 =						{
    "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 					__snake_case  (					__SCREAMING_SNAKE_CASE		,				__SCREAMING_SNAKE_CASE							):






   '''simple docstring'''





   lowerCamelCase__    							=      '''swin'''

   lowerCamelCase__    							=      {
       '''num_attention_heads''': '''num_heads''',
       '''num_hidden_layers''': '''num_layers''',
   }

   def __init__(							self	,		__SCREAMING_SNAKE_CASE=2_2_4	,		__SCREAMING_SNAKE_CASE=4	,		__SCREAMING_SNAKE_CASE=3	,		__SCREAMING_SNAKE_CASE=9_6	,		__SCREAMING_SNAKE_CASE=[2, 2, 6, 2]	,		__SCREAMING_SNAKE_CASE=[3, 6, 1_2, 2_4]	,		__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=3_2	,		__SCREAMING_SNAKE_CASE=None	,		__SCREAMING_SNAKE_CASE=None	,		**__SCREAMING_SNAKE_CASE	,		):
    super().__init__(**__SCREAMING_SNAKE_CASE       )

    snake_case__     :					List[str]    =  image_size
    snake_case__     :					Dict    =  patch_size
    snake_case__     :					Union[str, Any]    =  num_channels
    snake_case__     :					List[str]    =  embed_dim
    snake_case__     :					Optional[Any]    =  depths
    snake_case__     :					Dict    =  len(__SCREAMING_SNAKE_CASE       )
    snake_case__     :					Dict    =  num_heads
    snake_case__     :					str    =  window_size
    snake_case__     :					Union[str, Any]    =  mlp_ratio
    snake_case__     :					List[str]    =  qkv_bias
    snake_case__     :					List[str]    =  hidden_dropout_prob
    snake_case__     :					Optional[Any]    =  attention_probs_dropout_prob
    snake_case__     :					Tuple    =  drop_path_rate
    snake_case__     :					Tuple    =  hidden_act
    snake_case__     :					Union[str, Any]    =  use_absolute_embeddings
    snake_case__     :					int    =  layer_norm_eps
    snake_case__     :					List[str]    =  initializer_range
    snake_case__     :					Optional[Any]    =  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__     :					Optional[Any]    =  int(embed_dim * 2 ** (len(__SCREAMING_SNAKE_CASE       ) - 1)       )
    snake_case__     :					Tuple    =  ["""stem"""] + [f"stage{idx}" for idx in range(1	,		len(__SCREAMING_SNAKE_CASE       ) + 1       )]
    snake_case__  ,							snake_case__     :					List[Any]    =  get_aligned_output_features_output_indices(
        out_features=__SCREAMING_SNAKE_CASE	,		out_indices=__SCREAMING_SNAKE_CASE	,		stage_names=self.stage_names       )



class 					__snake_case  (					__SCREAMING_SNAKE_CASE							):






   '''simple docstring'''





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

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


   @property
   def 		__UpperCamelCase						(							self       ):
    return 1e-4












'''simple docstring'''





from unittest.mock import patch

import pyspark

from datasets.packaged_modules.spark.spark import (
    Spark,
    SparkExamplesIterable,
    _generate_iterable_examples,
)

from ..utils import (
    require_dill_gt_0_3_2,
    require_not_windows,
)
def __UpperCAmelCase   (					A					:			Optional[int]			,       A					:			Optional[int]		)    ->   str:
			UpperCAmelCase_      :       List[Any]          =       []
			for part_id in partition_order:
						UpperCAmelCase_      :       Any          =       df.where(F"SPARK_PARTITION_ID() = {part_id}"		).collect()
						for row_idx, row in enumerate(A		):
									expected_row_ids_and_row_dicts.append((F"{part_id}_{row_idx}", row.asDict())		)
			return expected_row_ids_and_row_dicts
@require_not_windows
@require_dill_gt_0_3_2
def __UpperCAmelCase   (					)    ->   Any:
			UpperCAmelCase_      :       Any          =       pyspark.sql.SparkSession.builder.master('''local[*]'''		).appName('''pyspark'''		).getOrCreate()
			UpperCAmelCase_      :       List[str]          =       spark.range(1_0_0		).repartition(1		)
			UpperCAmelCase_      :       List[Any]          =       Spark(A		)
			# The id ints will be converted to Pyarrow int64s, so each row will be 8 bytes. Setting a max_shard_size of 16 means
			# that each partition can hold 2 rows.
			spark_builder._repartition_df_if_needed(max_shard_size=1_6		)
			# Given that the dataframe has 100 rows and each partition has 2 rows, we expect 50 partitions.
			assert spark_builder.df.rdd.getNumPartitions() == 5_0
@require_not_windows
@require_dill_gt_0_3_2
def __UpperCAmelCase   (					)    ->   str:
			UpperCAmelCase_      :       List[str]          =       pyspark.sql.SparkSession.builder.master('''local[*]'''		).appName('''pyspark'''		).getOrCreate()
			UpperCAmelCase_      :       Optional[Any]          =       spark.range(1_0		).repartition(2		)
			UpperCAmelCase_      :       int          =       [1, 0]
			UpperCAmelCase_      :       str          =       _generate_iterable_examples(A			,       A		)  # Reverse the partitions.
			UpperCAmelCase_      :       Optional[Any]          =       _get_expected_row_ids_and_row_dicts_for_partition_order(A			,       A		)

			for i, (row_id, row_dict) in enumerate(generate_fn()		):
						UpperCAmelCase_    ,      UpperCAmelCase_      :       int          =       expected_row_ids_and_row_dicts[i]
						assert row_id == expected_row_id
						assert row_dict == expected_row_dict
@require_not_windows
@require_dill_gt_0_3_2
def __UpperCAmelCase   (					)    ->   Union[str, Any]:
			UpperCAmelCase_      :       str          =       pyspark.sql.SparkSession.builder.master('''local[*]'''		).appName('''pyspark'''		).getOrCreate()
			UpperCAmelCase_      :       List[Any]          =       spark.range(1_0		).repartition(1		)
			UpperCAmelCase_      :       Optional[int]          =       SparkExamplesIterable(A		)
			assert it.n_shards == 1
			for i, (row_id, row_dict) in enumerate(A		):
						assert row_id == F"0_{i}"
						assert row_dict == {"id": i}
@require_not_windows
@require_dill_gt_0_3_2
def __UpperCAmelCase   (					)    ->   Union[str, Any]:
			UpperCAmelCase_      :       Dict          =       pyspark.sql.SparkSession.builder.master('''local[*]'''		).appName('''pyspark'''		).getOrCreate()
			UpperCAmelCase_      :       Dict          =       spark.range(3_0		).repartition(3		)
			# Mock the generator so that shuffle reverses the partition indices.
			with patch('''numpy.random.Generator'''		) as generator_mock:
						UpperCAmelCase_      :       Any          =       lambda A		: x.reverse()
						UpperCAmelCase_      :       Dict          =       _get_expected_row_ids_and_row_dicts_for_partition_order(A			,       [2, 1, 0]		)

						UpperCAmelCase_      :       List[Any]          =       SparkExamplesIterable(A		).shuffle_data_sources(A		)
						assert shuffled_it.n_shards == 3
						for i, (row_id, row_dict) in enumerate(A		):
									UpperCAmelCase_    ,      UpperCAmelCase_      :       Dict          =       expected_row_ids_and_row_dicts[i]
									assert row_id == expected_row_id
									assert row_dict == expected_row_dict
@require_not_windows
@require_dill_gt_0_3_2
def __UpperCAmelCase   (					)    ->   int:
			UpperCAmelCase_      :       Tuple          =       pyspark.sql.SparkSession.builder.master('''local[*]'''		).appName('''pyspark'''		).getOrCreate()
			UpperCAmelCase_      :       int          =       spark.range(2_0		).repartition(4		)

			# Partitions 0 and 2
			UpperCAmelCase_      :       int          =       SparkExamplesIterable(A		).shard_data_sources(worker_id=0			,       num_workers=2		)
			assert shard_it_a.n_shards == 2
			UpperCAmelCase_      :       Union[str, Any]          =       _get_expected_row_ids_and_row_dicts_for_partition_order(A			,       [0, 2]		)
			for i, (row_id, row_dict) in enumerate(A		):
						UpperCAmelCase_    ,      UpperCAmelCase_      :       Dict          =       expected_row_ids_and_row_dicts_a[i]
						assert row_id == expected_row_id
						assert row_dict == expected_row_dict

			# Partitions 1 and 3
			UpperCAmelCase_      :       Tuple          =       SparkExamplesIterable(A		).shard_data_sources(worker_id=1			,       num_workers=2		)
			assert shard_it_a.n_shards == 2
			UpperCAmelCase_      :       List[str]          =       _get_expected_row_ids_and_row_dicts_for_partition_order(A			,       [1, 3]		)
			for i, (row_id, row_dict) in enumerate(A		):
						UpperCAmelCase_    ,      UpperCAmelCase_      :       Union[str, Any]          =       expected_row_ids_and_row_dicts_a[i]
						assert row_id == expected_row_id
						assert row_dict == expected_row_dict







@require_not_windows
@require_dill_gt_0_3_2
def __UpperCAmelCase   (					)    ->   Any:
			UpperCAmelCase_      :       List[Any]          =       pyspark.sql.SparkSession.builder.master('''local[*]'''		).appName('''pyspark'''		).getOrCreate()
			UpperCAmelCase_      :       List[str]          =       spark.range(1_0_0		).repartition(1		)
			UpperCAmelCase_      :       int          =       Spark(A		)
			# Choose a small max_shard_size for maximum partitioning.
			spark_builder._repartition_df_if_needed(max_shard_size=1		)
			# The new number of partitions should not be greater than the number of rows.
			assert spark_builder.df.rdd.getNumPartitions() == 1_0_0


