from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase_			    =							logging.get_logger(__name__)

UpperCAmelCase_			    =							{
    '''EleutherAI/gpt-neox-20b''': '''https://huggingface.co/EleutherAI/gpt-neox-20b/resolve/main/config.json''',
    # See all GPTNeoX models at https://huggingface.co/models?filter=gpt_neox
}




class  __magic_name__  (		__a    ):





  """simple docstring"""




  lowerCAmelCase      :    str  			=   '''gpt_neox'''



  def __init__(	self					:  str		,  _lowercase					:  int=50_432		,  _lowercase					:  int=6_144		,  _lowercase					:  Any=44		,  _lowercase					:  Optional[Any]=64		,  _lowercase					:  Optional[Any]=24_576		,  _lowercase					:  List[str]="gelu"		,  _lowercase					:  Dict=0.25		,  _lowercase					:  Union[str, Any]=10_000		,  _lowercase					:  str=0.0		,  _lowercase					:  int=0.0		,  _lowercase					:  Any=0.1		,  _lowercase					:  Dict=2_048		,  _lowercase					:  Dict=0.02		,  _lowercase					:  List[str]=1E-5		,  _lowercase					:  Dict=True		,  _lowercase					:  Any=0		,  _lowercase					:  List[Any]=2		,  _lowercase					:  int=False		,  _lowercase					:  Any=True		,  _lowercase					:  str=None		,  **_lowercase					:  List[Any]		,  ):



   """simple docstring"""



   super().__init__(bos_token_id=_lowercase		,  eos_token_id=_lowercase		,  **_lowercase							)
   _UpperCamelCase:     Any							= vocab_size
   _UpperCamelCase:     Optional[Any]							= max_position_embeddings
   _UpperCamelCase:     Union[str, Any]							= hidden_size
   _UpperCamelCase:     str							= num_hidden_layers
   _UpperCamelCase:     Dict							= num_attention_heads
   _UpperCamelCase:     Tuple							= intermediate_size
   _UpperCamelCase:     Any							= hidden_act
   _UpperCamelCase:     int							= rotary_pct
   _UpperCamelCase:     Tuple							= rotary_emb_base
   _UpperCamelCase:     int							= attention_dropout
   _UpperCamelCase:     Dict							= hidden_dropout
   _UpperCamelCase:     List[Any]							= classifier_dropout
   _UpperCamelCase:     int							= initializer_range
   _UpperCamelCase:     Dict							= layer_norm_eps
   _UpperCamelCase:     List[Any]							= use_cache
   _UpperCamelCase:     Union[str, Any]							= tie_word_embeddings
   _UpperCamelCase:     Dict							= use_parallel_residual
   _UpperCamelCase:     List[str]							= rope_scaling
   self._rope_scaling_validation()

   if self.hidden_size % self.num_attention_heads != 0:
    raise ValueError(
        '''The hidden size is not divisble by the number of attention heads! Make sure to update them!'''							)



  def   lowerCAmelCase						(	self					:  Optional[Any]							):



   """simple docstring"""



   if self.rope_scaling is None:
    return

   if not isinstance(self.rope_scaling		,  _lowercase							) or len(self.rope_scaling							) != 2:
    raise ValueError(
        '''`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, '''
        f"""got {self.rope_scaling}"""							)
   _UpperCamelCase:     Optional[Any]							= self.rope_scaling.get('''type'''		,  _lowercase							)
   _UpperCamelCase:     List[Any]							= self.rope_scaling.get('''factor'''		,  _lowercase							)
   if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]:
    raise ValueError(
        f"""`rope_scaling`'s name field must be one of ['linear', 'dynamic'], got {rope_scaling_type}"""							)
   if rope_scaling_factor is None or not isinstance(_lowercase		,  _lowercase							) or rope_scaling_factor <= 1.0:
    raise ValueError(f"""`rope_scaling`'s factor field must be an float > 1, got {rope_scaling_factor}"""							)
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