





"""simple docstring"""





from collections import OrderedDict
from typing import Any, Mapping, Optional

from ... import PreTrainedTokenizer, TensorType, is_torch_available
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast
from ...utils import logging


lowerCAmelCase__     			=			logging.get_logger(__name__)

lowerCAmelCase__     			=			{
    '''EleutherAI/gpt-neo-1.3B''': '''https://huggingface.co/EleutherAI/gpt-neo-1.3B/resolve/main/config.json''',
    # See all GPTNeo models at https://huggingface.co/models?filter=gpt_neo
}





class        _lowerCamelCase      (       __lowerCamelCase ):
						UpperCAmelCase_ =			'''gpt_neo'''
						UpperCAmelCase_ =			['''past_key_values''']
						UpperCAmelCase_ =			{'''num_attention_heads''': '''num_heads''', '''num_hidden_layers''': '''num_layers'''}


						def __init__(self     :   Any   ,							__a     :   Tuple=5_02_57   ,							__a     :   Optional[Any]=20_48   ,							__a     :   str=20_48   ,							__a     :   Any=24   ,							__a     :   Union[str, Any]=[[["global", "local"], 12]]   ,							__a     :   str=16   ,							__a     :   Tuple=None   ,							__a     :   Optional[Any]=2_56   ,							__a     :   Optional[int]="gelu_new"   ,							__a     :   List[str]=0.0   ,							__a     :   Dict=0.0   ,							__a     :   int=0.0   ,							__a     :   Optional[int]=0.1   ,							__a     :   Tuple=1e-5   ,							__a     :   int=0.02   ,							__a     :   Union[str, Any]=True   ,							__a     :   Any=5_02_56   ,							__a     :   Tuple=5_02_56   ,							**__a     :   List[str]   ,							) ->   List[Any]:
								UpperCamelCase   =	vocab_size
								UpperCamelCase   =	max_position_embeddings
								UpperCamelCase   =	hidden_size
								UpperCamelCase   =	num_layers
								UpperCamelCase   =	num_heads
								UpperCamelCase   =	intermediate_size
								UpperCamelCase   =	window_size
								UpperCamelCase   =	activation_function
								UpperCamelCase   =	resid_dropout
								UpperCamelCase   =	embed_dropout
								UpperCamelCase   =	attention_dropout
								UpperCamelCase   =	classifier_dropout
								UpperCamelCase   =	layer_norm_epsilon
								UpperCamelCase   =	initializer_range
								UpperCamelCase   =	use_cache

								UpperCamelCase   =	bos_token_id
								UpperCamelCase   =	eos_token_id

								UpperCamelCase   =	attention_types
								UpperCamelCase   =	self.expand_attention_types_params(a_		)

								if len(self.attention_layers		) != self.num_layers:
										raise ValueError(
										    "Configuration for convolutional module is incorrect. "
										    "It is required that `len(config.attention_layers)` == `config.num_layers` "
										    F"but is `len(config.attention_layers) = {len(self.attention_layers		)}`, "
										    F"`config.num_layers = {self.num_layers}`. "
										    "`config.attention_layers` is prepared using `config.attention_types`. "
										    "Please verify the value of `config.attention_types` argument."		)

								super().__init__(bos_token_id=a_   ,							eos_token_id=a_   ,							**a_		)




						@staticmethod
						def     snake_case_   (__a     :   Tuple		) ->   Optional[int]:
								UpperCamelCase   =	[]
								for item in attention_types:
										for _ in range(item[1]		):
												attentions.extend(item[0]		)
								return attentions



def       a__					(					_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE    ):




		"""simple docstring"""


		import torch

		UpperCamelCase   =	input.size()
		UpperCamelCase   =	len(lowerCAmelCase_    )
		UpperCamelCase   =	shape[dimension]

		UpperCamelCase   =	torch.arange(0  ,	lowerCAmelCase_  ,	lowerCAmelCase_    )
		UpperCamelCase   =	torch.div(sizedim - size  ,	lowerCAmelCase_  ,	rounding_mode="floor"    ) + 1
		UpperCamelCase   =	torch.arange(lowerCAmelCase_    ) + low_indices[:min_length][:, None]

		UpperCamelCase   =	[slice(lowerCAmelCase_    )] * rank
		UpperCamelCase   =	indices
		UpperCamelCase   =	input[s]

		UpperCamelCase   =	list(range(0  ,	rank + 1    )    )
		perm.append(perm.pop(dimension + 1    )    )

		return sliced.permute(lowerCAmelCase_    )


def       a__					(					_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE    ):




		"""simple docstring"""


		import torch

		UpperCamelCase   =	torch.arange(1  ,	lowerCAmelCase_    )
		UpperCamelCase   =	torch.remainder(lowerCAmelCase_  ,	lowerCAmelCase_    )
		UpperCamelCase   =	remainders == 0
		UpperCamelCase   =	candidates[divisor_indices]
		UpperCamelCase   =	torch.max(lowerCAmelCase_    )
		return largest_divisor, torch.div(lowerCAmelCase_  ,	lowerCAmelCase_  ,	rounding_mode="floor"    )






class        _lowerCamelCase      (       __lowerCamelCase ):


						@property
						def     snake_case_   (self     :   Union[str, Any]		) ->   Any:
								UpperCamelCase   =	OrderedDict({"input_ids": {0: "batch", 1: "sequence"}}		)
								if self.use_past:
										self.fill_with_past_key_values_(a_   ,							direction="inputs"		)
										UpperCamelCase   =	{0: "batch", 1: "past_sequence + sequence"}
								else:
										UpperCamelCase   =	{0: "batch", 1: "sequence"}

								return common_inputs


						@property
						def     snake_case_   (self     :   Union[str, Any]		) ->   List[Any]:
								return self._config.num_heads


						def     snake_case_   (self     :   Optional[Any]   ,							__a     :   List[Any]   ,							__a     :   Tuple = -1   ,							__a     :   Union[str, Any] = -1   ,							__a     :   str = False   ,							__a     :   Optional[Any] = None   ,							) ->   Union[str, Any]:
								UpperCamelCase   =	super(a_   ,							self		).generate_dummy_inputs(
								    a_   ,							batch_size=a_   ,							seq_length=a_   ,							is_pair=a_   ,							framework=a_		)

								# We need to order the input in the way they appears in the forward()
								UpperCamelCase   =	OrderedDict({"input_ids": common_inputs["input_ids"]}		)

								# Need to add the past_keys
								if self.use_past:
										if not is_torch_available():
												raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed."		)
										else:
												import torch

												UpperCamelCase   =	common_inputs["input_ids"].shape
												# Not using the same length for past_key_values
												UpperCamelCase   =	seqlen + 2
												UpperCamelCase   =	(
												    batch,
												    self.num_attention_heads,
												    past_key_values_length,
												    self._config.hidden_size // self.num_attention_heads,
												)
												UpperCamelCase   =	[
												    (torch.zeros(a_		), torch.zeros(a_		)) for _ in range(self.num_layers		)
												]

								UpperCamelCase   =	common_inputs["attention_mask"]
								if self.use_past:
										UpperCamelCase   =	ordered_inputs["attention_mask"].dtype
										UpperCamelCase   =	torch.cat(
										    [ordered_inputs["attention_mask"], torch.ones(a_   ,							a_   ,							dtype=a_		)]   ,							dim=1		)

								return ordered_inputs




						@property
						def     snake_case_   (self     :   int		) ->   Tuple:
								return 13













"""simple docstring"""





import pytest

from datasets.parallel import ParallelBackendConfig, parallel_backend
from datasets.utils.py_utils import map_nested

from .utils import require_dill_gt_0_3_2, require_joblibspark, require_not_windows


def       a__					(					_SCREAMING_SNAKE_CASE    ):  # picklable for multiprocessing




		"""simple docstring"""


		return i + 1


@require_dill_gt_0_3_2
@require_joblibspark
@require_not_windows
def       a__					(					):




		"""simple docstring"""


		with parallel_backend("spark"    ):
				assert ParallelBackendConfig.backend_name == "spark"

		UpperCamelCase   =	[1, 2, 3]
		with pytest.raises(_SCREAMING_SNAKE_CASE    ):
				with parallel_backend("unsupported backend"    ):
						map_nested(_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE  ,	num_proc=2    )

		with pytest.raises(_SCREAMING_SNAKE_CASE    ):
				with parallel_backend("unsupported backend"    ):
						map_nested(_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE  ,	num_proc=-1    )






@require_dill_gt_0_3_2
@require_joblibspark
@require_not_windows
@pytest.mark.parametrize("num_proc"  ,	[2, -1]    )
def       a__					(					_SCREAMING_SNAKE_CASE    ):




		"""simple docstring"""


		UpperCamelCase   =	[1, 2]
		UpperCamelCase   =	{"a": 1, "b": 2}
		UpperCamelCase   =	{"a": [1, 2], "b": [3, 4]}
		UpperCamelCase   =	{"a": {"1": 1}, "b": 2}
		UpperCamelCase   =	{"a": 1, "b": 2, "c": 3, "d": 4}
		UpperCamelCase   =	[2, 3]
		UpperCamelCase   =	{"a": 2, "b": 3}
		UpperCamelCase   =	{"a": [2, 3], "b": [4, 5]}
		UpperCamelCase   =	{"a": {"1": 2}, "b": 3}
		UpperCamelCase   =	{"a": 2, "b": 3, "c": 4, "d": 5}

		with parallel_backend("spark"    ):
				assert map_nested(_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE  ,	num_proc=_SCREAMING_SNAKE_CASE    ) == expected_map_nested_sa
				assert map_nested(_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE  ,	num_proc=_SCREAMING_SNAKE_CASE    ) == expected_map_nested_sa
				assert map_nested(_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE  ,	num_proc=_SCREAMING_SNAKE_CASE    ) == expected_map_nested_sa
				assert map_nested(_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE  ,	num_proc=_SCREAMING_SNAKE_CASE    ) == expected_map_nested_sa
				assert map_nested(_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE  ,	num_proc=_SCREAMING_SNAKE_CASE    ) == expected_map_nested_sa






