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 			A__      (	snake_case_ :		Dict       ,				snake_case_ :		int      ):
 SCREAMING_SNAKE_CASE__:   Tuple=      []
 for part_id in partition_order:
  SCREAMING_SNAKE_CASE__:   Union[str, Any]=      df.where(F'SPARK_PARTITION_ID() = {part_id}'      ).collect()
  for row_idx, row in enumerate(snake_case_      ):
   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 			A__      (	):
 SCREAMING_SNAKE_CASE__:   Tuple=      pyspark.sql.SparkSession.builder.master('''local[*]'''      ).appName('''pyspark'''      ).getOrCreate()
 SCREAMING_SNAKE_CASE__:   Union[str, Any]=      spark.range(100      ).repartition(1      )
 SCREAMING_SNAKE_CASE__:   Optional[Any]=      Spark(snake_case_      )
 # 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 			A__      (	):
 SCREAMING_SNAKE_CASE__:   Optional[int]=      pyspark.sql.SparkSession.builder.master('''local[*]'''      ).appName('''pyspark'''      ).getOrCreate()
 SCREAMING_SNAKE_CASE__:   List[str]=      spark.range(10      ).repartition(2      )
 SCREAMING_SNAKE_CASE__:   str=      [1, 0]
 SCREAMING_SNAKE_CASE__:   Dict=      _generate_iterable_examples(snake_case_       ,				snake_case_      )  # Reverse the partitions.
 SCREAMING_SNAKE_CASE__:   int=      _get_expected_row_ids_and_row_dicts_for_partition_order(snake_case_       ,				snake_case_      )

 for i, (row_id, row_dict) in enumerate(generate_fn()      ):
  SCREAMING_SNAKE_CASE__,	SCREAMING_SNAKE_CASE__:   Tuple=      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 			A__      (	):
 SCREAMING_SNAKE_CASE__:   int=      pyspark.sql.SparkSession.builder.master('''local[*]'''      ).appName('''pyspark'''      ).getOrCreate()
 SCREAMING_SNAKE_CASE__:   Union[str, Any]=      spark.range(10      ).repartition(1      )
 SCREAMING_SNAKE_CASE__:   List[str]=      SparkExamplesIterable(snake_case_      )
 assert it.n_shards == 1
 for i, (row_id, row_dict) in enumerate(snake_case_      ):
  assert row_id == F'0_{i}'
  assert row_dict == {"id": i}







@require_not_windows
@require_dill_gt_0_3_2
def 			A__      (	):
 SCREAMING_SNAKE_CASE__:   List[Any]=      pyspark.sql.SparkSession.builder.master('''local[*]'''      ).appName('''pyspark'''      ).getOrCreate()
 SCREAMING_SNAKE_CASE__:   Dict=      spark.range(30      ).repartition(3      )
 # Mock the generator so that shuffle reverses the partition indices.
 with patch('''numpy.random.Generator'''      ) as generator_mock:
  SCREAMING_SNAKE_CASE__:   str=      lambda snake_case_      : x.reverse()
  SCREAMING_SNAKE_CASE__:   List[str]=      _get_expected_row_ids_and_row_dicts_for_partition_order(snake_case_       ,				[2, 1, 0]      )

  SCREAMING_SNAKE_CASE__:   List[Any]=      SparkExamplesIterable(snake_case_      ).shuffle_data_sources(snake_case_      )
  assert shuffled_it.n_shards == 3
  for i, (row_id, row_dict) in enumerate(snake_case_      ):
   SCREAMING_SNAKE_CASE__,	SCREAMING_SNAKE_CASE__:   Optional[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 			A__      (	):
 SCREAMING_SNAKE_CASE__:   List[str]=      pyspark.sql.SparkSession.builder.master('''local[*]'''      ).appName('''pyspark'''      ).getOrCreate()
 SCREAMING_SNAKE_CASE__:   Any=      spark.range(20      ).repartition(4      )

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

 # Partitions 1 and 3
 SCREAMING_SNAKE_CASE__:   str=      SparkExamplesIterable(snake_case_      ).shard_data_sources(worker_id=1       ,				num_workers=2      )
 assert shard_it_a.n_shards == 2
 SCREAMING_SNAKE_CASE__:   Union[str, Any]=      _get_expected_row_ids_and_row_dicts_for_partition_order(snake_case_       ,				[1, 3]      )
 for i, (row_id, row_dict) in enumerate(snake_case_      ):
  SCREAMING_SNAKE_CASE__,	SCREAMING_SNAKE_CASE__:   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 			A__      (	):
 SCREAMING_SNAKE_CASE__:   str=      pyspark.sql.SparkSession.builder.master('''local[*]'''      ).appName('''pyspark'''      ).getOrCreate()
 SCREAMING_SNAKE_CASE__:   Optional[Any]=      spark.range(100      ).repartition(1      )
 SCREAMING_SNAKE_CASE__:   Dict=      Spark(snake_case_      )
 # 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


from . import (
    albert,
    align,
    altclip,
    audio_spectrogram_transformer,
    auto,
    autoformer,
    bark,
    bart,
    barthez,
    bartpho,
    beit,
    bert,
    bert_generation,
    bert_japanese,
    bertweet,
    big_bird,
    bigbird_pegasus,
    biogpt,
    bit,
    blenderbot,
    blenderbot_small,
    blip,
    blip_a,
    bloom,
    bridgetower,
    byta,
    camembert,
    canine,
    chinese_clip,
    clap,
    clip,
    clipseg,
    codegen,
    conditional_detr,
    convbert,
    convnext,
    convnextva,
    cpm,
    cpmant,
    ctrl,
    cvt,
    dataavec,
    deberta,
    deberta_va,
    decision_transformer,
    deformable_detr,
    deit,
    deprecated,
    deta,
    detr,
    dialogpt,
    dinat,
    distilbert,
    dit,
    donut,
    dpr,
    dpt,
    efficientformer,
    efficientnet,
    electra,
    encodec,
    encoder_decoder,
    ernie,
    ernie_m,
    esm,
    falcon,
    flaubert,
    flava,
    fnet,
    focalnet,
    fsmt,
    funnel,
    git,
    glpn,
    gpta,
    gpt_bigcode,
    gpt_neo,
    gpt_neox,
    gpt_neox_japanese,
    gpt_swa,
    gptj,
    gptsan_japanese,
    graphormer,
    groupvit,
    herbert,
    hubert,
    ibert,
    imagegpt,
    informer,
    instructblip,
    jukebox,
    layoutlm,
    layoutlmva,
    layoutlmva,
    layoutxlm,
    led,
    levit,
    lilt,
    llama,
    longformer,
    longta,
    luke,
    lxmert,
    mam_aaa,
    marian,
    markuplm,
    maskaformer,
    maskformer,
    mbart,
    mbartaa,
    mega,
    megatron_bert,
    megatron_gpta,
    mgp_str,
    mluke,
    mobilebert,
    mobilenet_va,
    mobilenet_va,
    mobilevit,
    mobilevitva,
    mpnet,
    mra,
    mta,
    musicgen,
    mvp,
    nat,
    nezha,
    nllb,
    nllb_moe,
    nystromformer,
    oneformer,
    open_llama,
    openai,
    opt,
    owlvit,
    pegasus,
    pegasus_x,
    perceiver,
    phobert,
    pixastruct,
    plbart,
    poolformer,
    prophetnet,
    qdqbert,
    rag,
    realm,
    reformer,
    regnet,
    rembert,
    resnet,
    roberta,
    roberta_prelayernorm,
    roc_bert,
    roformer,
    rwkv,
    sam,
    segformer,
    sew,
    sew_d,
    speech_encoder_decoder,
    speech_to_text,
    speech_to_text_a,
    speechta,
    splinter,
    squeezebert,
    swiftformer,
    swin,
    swinasr,
    swinva,
    switch_transformers,
    ta,
    table_transformer,
    tapas,
    time_series_transformer,
    timesformer,
    timm_backbone,
    transfo_xl,
    trocr,
    tvlt,
    umta,
    unispeech,
    unispeech_sat,
    upernet,
    videomae,
    vilt,
    vision_encoder_decoder,
    vision_text_dual_encoder,
    visual_bert,
    vit,
    vit_hybrid,
    vit_mae,
    vit_msn,
    vivit,
    wavaveca,
    wavaveca_conformer,
    wavaveca_phoneme,
    wavaveca_with_lm,
    wavlm,
    whisper,
    x_clip,
    xglm,
    xlm,
    xlm_prophetnet,
    xlm_roberta,
    xlm_roberta_xl,
    xlnet,
    xmod,
    yolos,
    yoso,
)

