
'''simple docstring'''







import multiprocessing
import os
from typing import BinaryIO, Optional, Union

import fsspec

from .. import Dataset, Features, NamedSplit, config
from ..formatting import query_table
from ..packaged_modules.json.json import Json
from ..utils import logging
from ..utils.typing import NestedDataStructureLike, PathLike
from .abc import AbstractDatasetReader






class 			lowerCAmelCase__       (  lowerCamelCase_					):






					def __init__(       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE = None    ,							__SCREAMING_SNAKE_CASE = None    ,							__SCREAMING_SNAKE_CASE = None    ,							__SCREAMING_SNAKE_CASE = False    ,							__SCREAMING_SNAKE_CASE = False    ,							__SCREAMING_SNAKE_CASE = None    ,							__SCREAMING_SNAKE_CASE = None    ,							**__SCREAMING_SNAKE_CASE    ,							):


											"""simple docstring"""





											super().__init__(
											    __SCREAMING_SNAKE_CASE    ,							split=__SCREAMING_SNAKE_CASE    ,							features=__SCREAMING_SNAKE_CASE    ,							cache_dir=__SCREAMING_SNAKE_CASE    ,							keep_in_memory=__SCREAMING_SNAKE_CASE    ,							streaming=__SCREAMING_SNAKE_CASE    ,							num_proc=__SCREAMING_SNAKE_CASE    ,							**__SCREAMING_SNAKE_CASE    ,							)
											lowercase_     :					Tuple													=	field
											lowercase_     :					Any													=	path_or_paths if isinstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		) else {self.split: path_or_paths}
											lowercase_     :					Dict													=	Json(
											    cache_dir=__SCREAMING_SNAKE_CASE    ,							data_files=__SCREAMING_SNAKE_CASE    ,							features=__SCREAMING_SNAKE_CASE    ,							field=__SCREAMING_SNAKE_CASE    ,							**__SCREAMING_SNAKE_CASE    ,							)






					def      _snake_case   (       self		):


											"""simple docstring"""





											if self.streaming:
																	lowercase_     :					Dict													=	self.builder.as_streaming_dataset(split=self.split		)
											# Build regular (map-style) dataset
											else:
																	lowercase_     :					int													=	None
																	lowercase_     :					Dict													=	None
																	lowercase_     :					Any													=	None
																	lowercase_     :					List[str]													=	None

																	self.builder.download_and_prepare(
																	    download_config=__SCREAMING_SNAKE_CASE    ,							download_mode=__SCREAMING_SNAKE_CASE    ,							verification_mode=__SCREAMING_SNAKE_CASE    ,							base_path=__SCREAMING_SNAKE_CASE    ,							num_proc=self.num_proc    ,							)
																	lowercase_     :					Dict													=	self.builder.as_dataset(
																	    split=self.split    ,							verification_mode=__SCREAMING_SNAKE_CASE    ,							in_memory=self.keep_in_memory		)
											return dataset







class 			lowerCAmelCase__       :






					def __init__(       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE = None    ,							__SCREAMING_SNAKE_CASE = None    ,							**__SCREAMING_SNAKE_CASE    ,							):


											"""simple docstring"""





											if num_proc is not None and num_proc <= 0:
																	raise ValueError(F'''num_proc {num_proc} must be an integer > 0.'''		)

											lowercase_     :					int													=	dataset
											lowercase_     :					List[str]													=	path_or_buf
											lowercase_     :					int													=	batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE
											lowercase_     :					Dict													=	num_proc
											lowercase_     :					List[Any]													=	'''utf-8'''
											lowercase_     :					List[str]													=	to_json_kwargs






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					List[str]													=	self.to_json_kwargs.pop('''path_or_buf'''    ,							__SCREAMING_SNAKE_CASE		)
											lowercase_     :					str													=	self.to_json_kwargs.pop('''orient'''    ,							'''records'''		)
											lowercase_     :					Optional[int]													=	self.to_json_kwargs.pop('''lines'''    ,							True if orient == '''records''' else False		)
											lowercase_     :					Tuple													=	self.to_json_kwargs.pop('''index'''    ,							False if orient in ['''split''', '''table'''] else True		)
											lowercase_     :					Dict													=	self.to_json_kwargs.pop('''compression'''    ,							__SCREAMING_SNAKE_CASE		)

											if compression not in [None, "infer", "gzip", "bz2", "xz"]:
																	raise NotImplementedError(F'''`datasets` currently does not support {compression} compression'''		)

											if isinstance(self.path_or_buf    ,							(str, bytes, os.PathLike)		):
																	with fsspec.open(self.path_or_buf    ,							'''wb'''    ,							compression=__SCREAMING_SNAKE_CASE		) as buffer:
																							lowercase_     :					Optional[int]													=	self._write(file_obj=__SCREAMING_SNAKE_CASE    ,							orient=__SCREAMING_SNAKE_CASE    ,							lines=__SCREAMING_SNAKE_CASE    ,							index=__SCREAMING_SNAKE_CASE    ,							**self.to_json_kwargs		)
											else:
																	if compression:
																							raise NotImplementedError(
																							    F'''The compression parameter is not supported when writing to a buffer, but compression={compression}'''
																							    ''' was passed. Please provide a local path instead.'''		)
																	lowercase_     :					Any													=	self._write(
																	    file_obj=self.path_or_buf    ,							orient=__SCREAMING_SNAKE_CASE    ,							lines=__SCREAMING_SNAKE_CASE    ,							index=__SCREAMING_SNAKE_CASE    ,							**self.to_json_kwargs		)
											return written






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											lowercase_						,			lowercase_						,			lowercase_						,			lowercase_						,			lowercase_     :					Optional[Any]													=	args

											lowercase_     :					Union[str, Any]													=	query_table(
											    table=self.dataset.data    ,							key=slice(__SCREAMING_SNAKE_CASE    ,							offset + self.batch_size		)    ,							indices=self.dataset._indices    ,							)
											lowercase_     :					Dict													=	batch.to_pandas().to_json(
											    path_or_buf=__SCREAMING_SNAKE_CASE    ,							orient=__SCREAMING_SNAKE_CASE    ,							lines=__SCREAMING_SNAKE_CASE    ,							index=__SCREAMING_SNAKE_CASE    ,							**__SCREAMING_SNAKE_CASE		)
											if not json_str.endswith('''\n'''		):
																	json_str += "\n"
											return json_str.encode(self.encoding		)






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							**__SCREAMING_SNAKE_CASE    ,							):


											"""simple docstring"""





											lowercase_     :					List[str]													=	0

											if self.num_proc is None or self.num_proc == 1:
																	for offset in logging.tqdm(
																	    range(0    ,							len(self.dataset		)    ,							self.batch_size		)    ,							unit='''ba'''    ,							disable=not logging.is_progress_bar_enabled()    ,							desc='''Creating json from Arrow format'''    ,							):
																							lowercase_     :					List[str]													=	self._batch_json((offset, orient, lines, index, to_json_kwargs)		)
																							written += file_obj.write(__SCREAMING_SNAKE_CASE		)
											else:
																	lowercase_						,			lowercase_     :					Optional[Any]													=	len(self.dataset		), self.batch_size
																	with multiprocessing.Pool(self.num_proc		) as pool:
																							for json_str in logging.tqdm(
																							    pool.imap(
																							        self._batch_json    ,							[(offset, orient, lines, index, to_json_kwargs) for offset in range(0    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		)]    ,							)    ,							total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size    ,							unit='''ba'''    ,							disable=not logging.is_progress_bar_enabled()    ,							desc='''Creating json from Arrow format'''    ,							):
																													written += file_obj.write(__SCREAMING_SNAKE_CASE		)

											return written





'''simple docstring'''







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


_lowercase			:  Any   							=	logging.get_logger(__name__)

_lowercase			:  Dict   							=	{
    "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 			lowerCAmelCase__       (  lowerCamelCase_					):
					lowerCAmelCase_					       =					'''gpt_neox'''






					def __init__(       self    ,							__SCREAMING_SNAKE_CASE=5_04_32    ,							__SCREAMING_SNAKE_CASE=61_44    ,							__SCREAMING_SNAKE_CASE=44    ,							__SCREAMING_SNAKE_CASE=64    ,							__SCREAMING_SNAKE_CASE=2_45_76    ,							__SCREAMING_SNAKE_CASE="gelu"    ,							__SCREAMING_SNAKE_CASE=0.25    ,							__SCREAMING_SNAKE_CASE=1_00_00    ,							__SCREAMING_SNAKE_CASE=0.0    ,							__SCREAMING_SNAKE_CASE=0.0    ,							__SCREAMING_SNAKE_CASE=0.1    ,							__SCREAMING_SNAKE_CASE=20_48    ,							__SCREAMING_SNAKE_CASE=0.02    ,							__SCREAMING_SNAKE_CASE=1E-5    ,							__SCREAMING_SNAKE_CASE=True    ,							__SCREAMING_SNAKE_CASE=0    ,							__SCREAMING_SNAKE_CASE=2    ,							__SCREAMING_SNAKE_CASE=False    ,							__SCREAMING_SNAKE_CASE=True    ,							__SCREAMING_SNAKE_CASE=None    ,							**__SCREAMING_SNAKE_CASE    ,							):


											"""simple docstring"""





											super().__init__(bos_token_id=__SCREAMING_SNAKE_CASE    ,							eos_token_id=__SCREAMING_SNAKE_CASE    ,							**__SCREAMING_SNAKE_CASE		)
											lowercase_     :					Optional[Any]													=	vocab_size
											lowercase_     :					Optional[Any]													=	max_position_embeddings
											lowercase_     :					Optional[int]													=	hidden_size
											lowercase_     :					Tuple													=	num_hidden_layers
											lowercase_     :					Any													=	num_attention_heads
											lowercase_     :					str													=	intermediate_size
											lowercase_     :					Any													=	hidden_act
											lowercase_     :					Tuple													=	rotary_pct
											lowercase_     :					Optional[Any]													=	rotary_emb_base
											lowercase_     :					Any													=	attention_dropout
											lowercase_     :					str													=	hidden_dropout
											lowercase_     :					Dict													=	classifier_dropout
											lowercase_     :					Tuple													=	initializer_range
											lowercase_     :					List[str]													=	layer_norm_eps
											lowercase_     :					Union[str, Any]													=	use_cache
											lowercase_     :					int													=	tie_word_embeddings
											lowercase_     :					Tuple													=	use_parallel_residual
											lowercase_     :					Optional[Any]													=	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      _snake_case   (       self		):


											"""simple docstring"""





											if self.rope_scaling is None:
																	return

											if not isinstance(self.rope_scaling    ,							__SCREAMING_SNAKE_CASE		) 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}'''		)
											lowercase_     :					List[Any]													=	self.rope_scaling.get('''type'''    ,							__SCREAMING_SNAKE_CASE		)
											lowercase_     :					int													=	self.rope_scaling.get('''factor'''    ,							__SCREAMING_SNAKE_CASE		)
											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(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		) or rope_scaling_factor <= 1.0:
																	raise ValueError(F'''`rope_scaling`\'s factor field must be an float > 1, got {rope_scaling_factor}'''		)



