import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto import CONFIG_MAPPING


UpperCAmelCase_			    =							logging.get_logger(__name__)

UpperCAmelCase_			    =							{
    '''SenseTime/deformable-detr''': '''https://huggingface.co/sensetime/deformable-detr/resolve/main/config.json''',
    # See all Deformable DETR models at https://huggingface.co/models?filter=deformable-detr
}




class  __magic_name__  (		__a    ):





   """simple docstring"""




   lowerCAmelCase      :    Optional[Any]  			=   '''deformable_detr'''
   lowerCAmelCase      :    Dict  			=   {
       '''hidden_size''': '''d_model''',
       '''num_attention_heads''': '''encoder_attention_heads''',
   }



   def __init__(	self					:  Dict		,  _lowercase					:  int=True		,  _lowercase					:  Dict=None		,  _lowercase					:  Any=3		,  _lowercase					:  str=300		,  _lowercase					:  Dict=1_024		,  _lowercase					:  Optional[int]=6		,  _lowercase					:  Optional[int]=1_024		,  _lowercase					:  Union[str, Any]=8		,  _lowercase					:  List[str]=6		,  _lowercase					:  Dict=1_024		,  _lowercase					:  Tuple=8		,  _lowercase					:  List[str]=0.0		,  _lowercase					:  str=True		,  _lowercase					:  Optional[Any]="relu"		,  _lowercase					:  Any=256		,  _lowercase					:  Tuple=0.1		,  _lowercase					:  Optional[Any]=0.0		,  _lowercase					:  List[str]=0.0		,  _lowercase					:  Any=0.02		,  _lowercase					:  Optional[int]=1.0		,  _lowercase					:  Any=True		,  _lowercase					:  List[str]=False		,  _lowercase					:  Optional[Any]="sine"		,  _lowercase					:  Union[str, Any]="resnet50"		,  _lowercase					:  Optional[Any]=True		,  _lowercase					:  List[str]=False		,  _lowercase					:  Any=4		,  _lowercase					:  List[Any]=4		,  _lowercase					:  Any=4		,  _lowercase					:  Tuple=False		,  _lowercase					:  List[Any]=300		,  _lowercase					:  Dict=False		,  _lowercase					:  List[Any]=1		,  _lowercase					:  List[Any]=5		,  _lowercase					:  List[Any]=2		,  _lowercase					:  Dict=1		,  _lowercase					:  int=1		,  _lowercase					:  List[str]=5		,  _lowercase					:  int=2		,  _lowercase					:  Optional[Any]=0.1		,  _lowercase					:  Dict=0.25		,  _lowercase					:  Tuple=False		,  **_lowercase					:  str		,  ):



    """simple docstring"""



    if backbone_config is not None and use_timm_backbone:
     raise ValueError('''You can\'t specify both `backbone_config` and `use_timm_backbone`.'''							)

    if not use_timm_backbone:
     if backbone_config is None:
      logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.'''							)
      _UpperCamelCase:     Union[str, Any]							= CONFIG_MAPPING['''resnet'''](out_features=['''stage4''']							)
     elif isinstance(_lowercase		,  _lowercase							):
      _UpperCamelCase:     List[str]							= backbone_config.get('''model_type'''							)
      _UpperCamelCase:     str							= CONFIG_MAPPING[backbone_model_type]
      _UpperCamelCase:     Any							= config_class.from_dict(_lowercase							)
    _UpperCamelCase:     Dict							= use_timm_backbone
    _UpperCamelCase:     Union[str, Any]							= backbone_config
    _UpperCamelCase:     List[str]							= num_channels
    _UpperCamelCase:     str							= num_queries
    _UpperCamelCase:     int							= max_position_embeddings
    _UpperCamelCase:     Dict							= d_model
    _UpperCamelCase:     Tuple							= encoder_ffn_dim
    _UpperCamelCase:     List[str]							= encoder_layers
    _UpperCamelCase:     Optional[Any]							= encoder_attention_heads
    _UpperCamelCase:     Tuple							= decoder_ffn_dim
    _UpperCamelCase:     List[str]							= decoder_layers
    _UpperCamelCase:     Union[str, Any]							= decoder_attention_heads
    _UpperCamelCase:     Optional[Any]							= dropout
    _UpperCamelCase:     List[Any]							= attention_dropout
    _UpperCamelCase:     Optional[Any]							= activation_dropout
    _UpperCamelCase:     List[str]							= activation_function
    _UpperCamelCase:     List[str]							= init_std
    _UpperCamelCase:     List[Any]							= init_xavier_std
    _UpperCamelCase:     List[Any]							= encoder_layerdrop
    _UpperCamelCase:     int							= auxiliary_loss
    _UpperCamelCase:     int							= position_embedding_type
    _UpperCamelCase:     Optional[Any]							= backbone
    _UpperCamelCase:     List[str]							= use_pretrained_backbone
    _UpperCamelCase:     Optional[Any]							= dilation
    # deformable attributes
    _UpperCamelCase:     Union[str, Any]							= num_feature_levels
    _UpperCamelCase:     Any							= encoder_n_points
    _UpperCamelCase:     List[str]							= decoder_n_points
    _UpperCamelCase:     str							= two_stage
    _UpperCamelCase:     List[str]							= two_stage_num_proposals
    _UpperCamelCase:     List[str]							= with_box_refine
    if two_stage is True and with_box_refine is False:
     raise ValueError('''If two_stage is True, with_box_refine must be True.'''							)
    # Hungarian matcher
    _UpperCamelCase:     List[str]							= class_cost
    _UpperCamelCase:     Tuple							= bbox_cost
    _UpperCamelCase:     Dict							= giou_cost
    # Loss coefficients
    _UpperCamelCase:     Optional[int]							= mask_loss_coefficient
    _UpperCamelCase:     Dict							= dice_loss_coefficient
    _UpperCamelCase:     Optional[int]							= bbox_loss_coefficient
    _UpperCamelCase:     Optional[int]							= giou_loss_coefficient
    _UpperCamelCase:     str							= eos_coefficient
    _UpperCamelCase:     Tuple							= focal_alpha
    _UpperCamelCase:     int							= disable_custom_kernels
    super().__init__(is_encoder_decoder=_lowercase		,  **_lowercase							)



   @property
   def   lowerCAmelCase						(	self					:  Optional[Any]							):



    """simple docstring"""



    return self.encoder_attention_heads



   @property
   def   lowerCAmelCase						(	self					:  Tuple							):



    """simple docstring"""



    return self.d_model



   def   lowerCAmelCase						(	self					:  Dict							):



    """simple docstring"""



    _UpperCamelCase:     int							= copy.deepcopy(self.__dict__							)
    if self.backbone_config is not None:
     _UpperCamelCase:     Optional[Any]							= self.backbone_config.to_dict()
    _UpperCamelCase:     Optional[Any]							= self.__class__.model_type
    return output
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       lowerCAmelCase_     (		lowercase:							Optional[Any]  ,					lowercase:							Tuple		)    ->				Any:







 '''simple docstring'''
 _UpperCamelCase:     Union[str, Any]							= []
 for part_id in partition_order:
  _UpperCamelCase:     int							= df.where(F"""SPARK_PARTITION_ID() = {part_id}"""		).collect()
  for row_idx, row in enumerate(lowercase		):
   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       lowerCAmelCase_     (		)    ->				List[str]:







 '''simple docstring'''
 _UpperCamelCase:     Optional[int]							= pyspark.sql.SparkSession.builder.master('''local[*]'''		).appName('''pyspark'''		).getOrCreate()
 _UpperCamelCase:     int							= spark.range(100		).repartition(1		)
 _UpperCamelCase:     int							= Spark(lowercase		)
 # 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=16		)
 # Given that the dataframe has 100 rows and each partition has 2 rows, we expect 50 partitions.
 assert spark_builder.df.rdd.getNumPartitions() == 50




@require_not_windows
@require_dill_gt_0_3_2
def       lowerCAmelCase_     (		)    ->				List[str]:







 '''simple docstring'''
 _UpperCamelCase:     List[Any]							= pyspark.sql.SparkSession.builder.master('''local[*]'''		).appName('''pyspark'''		).getOrCreate()
 _UpperCamelCase:     Optional[Any]							= spark.range(10		).repartition(2		)
 _UpperCamelCase:     int							= [1, 0]
 _UpperCamelCase:     Any							= _generate_iterable_examples(lowercase  ,					lowercase		)  # Reverse the partitions.
 _UpperCamelCase:     List[str]							= _get_expected_row_ids_and_row_dicts_for_partition_order(lowercase  ,					lowercase		)

 for i, (row_id, row_dict) in enumerate(generate_fn()		):
  _UpperCamelCase	,							_UpperCamelCase:     List[Any]							= 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       lowerCAmelCase_     (		)    ->				Any:







 '''simple docstring'''
 _UpperCamelCase:     Optional[Any]							= pyspark.sql.SparkSession.builder.master('''local[*]'''		).appName('''pyspark'''		).getOrCreate()
 _UpperCamelCase:     Any							= spark.range(10		).repartition(1		)
 _UpperCamelCase:     int							= SparkExamplesIterable(lowercase		)
 assert it.n_shards == 1
 for i, (row_id, row_dict) in enumerate(lowercase		):
  assert row_id == F"""0_{i}"""
  assert row_dict == {"id": i}




@require_not_windows
@require_dill_gt_0_3_2
def       lowerCAmelCase_     (		)    ->				Union[str, Any]:







 '''simple docstring'''
 _UpperCamelCase:     Any							= pyspark.sql.SparkSession.builder.master('''local[*]'''		).appName('''pyspark'''		).getOrCreate()
 _UpperCamelCase:     str							= spark.range(30		).repartition(3		)
 # Mock the generator so that shuffle reverses the partition indices.
 with patch('''numpy.random.Generator'''		) as generator_mock:
  _UpperCamelCase:     Union[str, Any]							= lambda lowercase		: x.reverse()
  _UpperCamelCase:     List[str]							= _get_expected_row_ids_and_row_dicts_for_partition_order(lowercase  ,					[2, 1, 0]		)

  _UpperCamelCase:     Union[str, Any]							= SparkExamplesIterable(lowercase		).shuffle_data_sources(lowercase		)
  assert shuffled_it.n_shards == 3
  for i, (row_id, row_dict) in enumerate(lowercase		):
   _UpperCamelCase	,							_UpperCamelCase:     List[Any]							= 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       lowerCAmelCase_     (		)    ->				Union[str, Any]:







 '''simple docstring'''
 _UpperCamelCase:     str							= pyspark.sql.SparkSession.builder.master('''local[*]'''		).appName('''pyspark'''		).getOrCreate()
 _UpperCamelCase:     Tuple							= spark.range(20		).repartition(4		)

 # Partitions 0 and 2
 _UpperCamelCase:     List[Any]							= SparkExamplesIterable(lowercase		).shard_data_sources(worker_id=0  ,					num_workers=2		)
 assert shard_it_a.n_shards == 2
 _UpperCamelCase:     Dict							= _get_expected_row_ids_and_row_dicts_for_partition_order(lowercase  ,					[0, 2]		)
 for i, (row_id, row_dict) in enumerate(lowercase		):
  _UpperCamelCase	,							_UpperCamelCase:     Optional[int]							= 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:     str							= SparkExamplesIterable(lowercase		).shard_data_sources(worker_id=1  ,					num_workers=2		)
 assert shard_it_a.n_shards == 2
 _UpperCamelCase:     str							= _get_expected_row_ids_and_row_dicts_for_partition_order(lowercase  ,					[1, 3]		)
 for i, (row_id, row_dict) in enumerate(lowercase		):
  _UpperCamelCase	,							_UpperCamelCase:     str							= 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       lowerCAmelCase_     (		)    ->				Optional[int]:







 '''simple docstring'''
 _UpperCamelCase:     Any							= pyspark.sql.SparkSession.builder.master('''local[*]'''		).appName('''pyspark'''		).getOrCreate()
 _UpperCamelCase:     Union[str, Any]							= spark.range(100		).repartition(1		)
 _UpperCamelCase:     Optional[Any]							= Spark(lowercase		)
 # 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() == 100