code: 
  
  
  
  """simple docstring"""
  import multiprocessing
  import os
  from typing import BinaryIO, Optional, Union
  
  from .. import Dataset, Features, NamedSplit, config
  from ..formatting import query_table
  from ..packaged_modules.csv.csv import Csv
  from ..utils import logging
  from ..utils.typing import NestedDataStructureLike, PathLike
  from .abc import AbstractDatasetReader
  class 							_UpperCAmelCase							(				a   ):
  
  
  
  
  
       '''simple docstring'''
  
  
  
       def __init__(      self ,			A ,			A = None ,			A = None ,			A = None ,			A = False ,			A = False ,			A = None ,			**A ,			)							->      List[str]:
           super().__init__(
               A ,			split=A ,			features=A ,			cache_dir=A ,			keep_in_memory=A ,			streaming=A ,			num_proc=A ,			**A ,			)
           _UpperCAmelCase							:						List[Any]       							=					path_or_paths if isinstance(A ,			A	) else {self.split: path_or_paths}
           _UpperCAmelCase							:						Union[str, Any]       							=					Csv(
               cache_dir=A ,			data_files=A ,			features=A ,			**A ,			)
  
  
  
  
  
       def 				__lowerCAmelCase			(      self	)							->      List[Any]:
           # Build iterable dataset
           if self.streaming:
               _UpperCAmelCase							:						Union[str, Any]       							=					self.builder.as_streaming_dataset(split=self.split	)
           # Build regular (map-style) dataset
           else:
               _UpperCAmelCase							:						str       							=					None
               _UpperCAmelCase							:						Dict       							=					None
               _UpperCAmelCase							:						List[Any]       							=					None
               _UpperCAmelCase							:						Union[str, Any]       							=					None
  
               self.builder.download_and_prepare(
                   download_config=A ,			download_mode=A ,			verification_mode=A ,			base_path=A ,			num_proc=self.num_proc ,			)
               _UpperCAmelCase							:						int       							=					self.builder.as_dataset(
                   split=self.split ,			verification_mode=A ,			in_memory=self.keep_in_memory	)
           return dataset
  
  
  
  class 							_UpperCAmelCase							:
  
  
  
  
  
       '''simple docstring'''
  
  
  
       def __init__(      self ,			A ,			A ,			A = None ,			A = None ,			**A ,			)							->      List[Any]:
           if num_proc is not None and num_proc <= 0:
               raise ValueError(f'num_proc {num_proc} must be an integer > 0.'	)
  
           _UpperCAmelCase							:						Tuple       							=					dataset
           _UpperCAmelCase							:						Optional[int]       							=					path_or_buf
           _UpperCAmelCase							:						str       							=					batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE
           _UpperCAmelCase							:						Union[str, Any]       							=					num_proc
           _UpperCAmelCase							:						Optional[Any]       							=					'''utf-8'''
           _UpperCAmelCase							:						Any       							=					to_csv_kwargs
  
  
  
  
  
       def 				__lowerCAmelCase			(      self	)							->      int:
           _UpperCAmelCase							:						Optional[int]       							=					self.to_csv_kwargs.pop('''path_or_buf''' ,			A	)
           _UpperCAmelCase							:						List[str]       							=					self.to_csv_kwargs.pop('''header''' ,			A	)
           _UpperCAmelCase							:						int       							=					self.to_csv_kwargs.pop('''index''' ,			A	)
  
           if isinstance(self.path_or_buf ,			(str, bytes, os.PathLike)	):
               with open(self.path_or_buf ,			'''wb+'''	) as buffer:
                   _UpperCAmelCase							:						Any       							=					self._write(file_obj=A ,			header=A ,			index=A ,			**self.to_csv_kwargs	)
           else:
               _UpperCAmelCase							:						List[str]       							=					self._write(file_obj=self.path_or_buf ,			header=A ,			index=A ,			**self.to_csv_kwargs	)
           return written
  
  
  
  
  
       def 				__lowerCAmelCase			(      self ,			A	)							->      Optional[Any]:
           _UpperCAmelCase							:						int       							=					args
  
           _UpperCAmelCase							:						str       							=					query_table(
               table=self.dataset.data ,			key=slice(A ,			offset + self.batch_size	) ,			indices=self.dataset._indices ,			)
           _UpperCAmelCase							:						int       							=					batch.to_pandas().to_csv(
               path_or_buf=A ,			header=header if (offset == 0) else False ,			index=A ,			**A	)
           return csv_str.encode(self.encoding	)
  
  
  
  
  
       def 				__lowerCAmelCase			(      self ,			A ,			A ,			A ,			**A	)							->      int:
           _UpperCAmelCase							:						Optional[int]       							=					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 CSV from Arrow format''' ,			):
                   _UpperCAmelCase							:						Dict       							=					self._batch_csv((offset, header, index, to_csv_kwargs)	)
                   written += file_obj.write(A	)
  
           else:
               _UpperCAmelCase							:						Dict       							=					len(self.dataset	), self.batch_size
               with multiprocessing.Pool(self.num_proc	) as pool:
                   for csv_str in logging.tqdm(
                       pool.imap(
                           self._batch_csv ,			[(offset, header, index, to_csv_kwargs) for offset in range(0 ,			A ,			A	)] ,			) ,			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 CSV from Arrow format''' ,			):
                       written += file_obj.write(A	)
  
           return written
  
  
  
  
  
style_context: 
  
  
  
  """simple docstring"""
  from collections import OrderedDict
  from typing import Any, List, Mapping, Optional
  
  from ... import PreTrainedTokenizer, TensorType, is_torch_available
  from ...configuration_utils import PretrainedConfig
  from ...onnx import OnnxConfigWithPast, PatchingSpec
  from ...utils import logging
  
  
  _lowerCAmelCase					:Union[str, Any]  =      logging.get_logger(__name__)
  
  _lowerCAmelCase					:List[str]  =      {
      'gpt2': 'https://huggingface.co/gpt2/resolve/main/config.json',
      'gpt2-medium': 'https://huggingface.co/gpt2-medium/resolve/main/config.json',
      'gpt2-large': 'https://huggingface.co/gpt2-large/resolve/main/config.json',
      'gpt2-xl': 'https://huggingface.co/gpt2-xl/resolve/main/config.json',
      'distilgpt2': 'https://huggingface.co/distilgpt2/resolve/main/config.json',
  }
  class 							_UpperCAmelCase							(				a   ):
  
  
  
  
  
       '''simple docstring'''
  
  
  
       a__             ='''gpt2'''
       a__             =['''past_key_values''']
       a__             ={
           '''hidden_size''': '''n_embd''',
           '''max_position_embeddings''': '''n_positions''',
           '''num_attention_heads''': '''n_head''',
           '''num_hidden_layers''': '''n_layer''',
       }
  
  
  
  
  
       def __init__(      self ,			A=5_0_2_5_7 ,			A=1_0_2_4 ,			A=7_6_8 ,			A=1_2 ,			A=1_2 ,			A=None ,			A="gelu_new" ,			A=0.1 ,			A=0.1 ,			A=0.1 ,			A=1E-5 ,			A=0.02 ,			A="cls_index" ,			A=True ,			A=None ,			A=True ,			A=0.1 ,			A=True ,			A=True ,			A=5_0_2_5_6 ,			A=5_0_2_5_6 ,			A=False ,			A=False ,			**A ,			)							->      Union[str, Any]:
           _UpperCAmelCase							:						Optional[int]       							=					vocab_size
           _UpperCAmelCase							:						Union[str, Any]       							=					n_positions
           _UpperCAmelCase							:						Optional[Any]       							=					n_embd
           _UpperCAmelCase							:						Tuple       							=					n_layer
           _UpperCAmelCase							:						Any       							=					n_head
           _UpperCAmelCase							:						Union[str, Any]       							=					n_inner
           _UpperCAmelCase							:						Optional[int]       							=					activation_function
           _UpperCAmelCase							:						Optional[int]       							=					resid_pdrop
           _UpperCAmelCase							:						Dict       							=					embd_pdrop
           _UpperCAmelCase							:						List[Any]       							=					attn_pdrop
           _UpperCAmelCase							:						Tuple       							=					layer_norm_epsilon
           _UpperCAmelCase							:						Optional[int]       							=					initializer_range
           _UpperCAmelCase							:						List[str]       							=					summary_type
           _UpperCAmelCase							:						Tuple       							=					summary_use_proj
           _UpperCAmelCase							:						List[Any]       							=					summary_activation
           _UpperCAmelCase							:						Optional[int]       							=					summary_first_dropout
           _UpperCAmelCase							:						int       							=					summary_proj_to_labels
           _UpperCAmelCase							:						Union[str, Any]       							=					scale_attn_weights
           _UpperCAmelCase							:						Any       							=					use_cache
           _UpperCAmelCase							:						Tuple       							=					scale_attn_by_inverse_layer_idx
           _UpperCAmelCase							:						Optional[Any]       							=					reorder_and_upcast_attn
  
           _UpperCAmelCase							:						Union[str, Any]       							=					bos_token_id
           _UpperCAmelCase							:						Optional[int]       							=					eos_token_id
  
           super().__init__(bos_token_id=A ,			eos_token_id=A ,			**A	)
  
  
  
  class 							_UpperCAmelCase							(				a   ):
  
  
  
  
  
       '''simple docstring'''
  
  
  
       def __init__(      self ,			A ,			A = "default" ,			A = None ,			A = False ,			)							->      str:
           super().__init__(A ,			task=A ,			patching_specs=A ,			use_past=A	)
           if not getattr(self._config ,			'''pad_token_id''' ,			A	):
               # TODO: how to do that better?
               _UpperCAmelCase							:						Optional[int]       							=					0
  
  
  
  
  
       @property
       def 				__lowerCAmelCase			(      self	)							->      Mapping[str, Mapping[int, str]]:
           _UpperCAmelCase							:						List[str]       							=					OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}}	)
           if self.use_past:
               self.fill_with_past_key_values_(A ,			direction='''inputs'''	)
               _UpperCAmelCase							:						Union[str, Any]       							=					{0: '''batch''', 1: '''past_sequence + sequence'''}
           else:
               _UpperCAmelCase							:						Tuple       							=					{0: '''batch''', 1: '''sequence'''}
  
           return common_inputs
  
  
  
  
  
       @property
       def 				__lowerCAmelCase			(      self	)							->      int:
           return self._config.n_layer
  
  
  
  
  
       @property
       def 				__lowerCAmelCase			(      self	)							->      int:
           return self._config.n_head
  
  
  
  
  
       def 				__lowerCAmelCase			(      self ,			A ,			A = -1 ,			A = -1 ,			A = False ,			A = None ,			)							->      Mapping[str, Any]:
           _UpperCAmelCase							:						int       							=					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							:						List[Any]       							=					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							,       _UpperCAmelCase							:						Optional[Any]       							=					common_inputs['''input_ids'''].shape
                   # Not using the same length for past_key_values
                   _UpperCAmelCase							:						Optional[Any]       							=					seqlen + 2
                   _UpperCAmelCase							:						Union[str, Any]       							=					(
                       batch,
                       self.num_attention_heads,
                       past_key_values_length,
                       self._config.hidden_size // self.num_attention_heads,
                   )
                   _UpperCAmelCase							:						List[Any]       							=					[
                       (torch.zeros(A	), torch.zeros(A	)) for _ in range(self.num_layers	)
                   ]
  
           _UpperCAmelCase							:						int       							=					common_inputs['''attention_mask''']
           if self.use_past:
               _UpperCAmelCase							:						int       							=					ordered_inputs['''attention_mask'''].dtype
               _UpperCAmelCase							:						Dict       							=					torch.cat(
                   [ordered_inputs['''attention_mask'''], torch.ones(A ,			A ,			dtype=A	)] ,			dim=1	)
  
           return ordered_inputs
  
  
  
  
  
       @property
       def 				__lowerCAmelCase			(      self	)							->      int:
           return 1_3
  
  
  