

import sys
from collections.abc import Mapping
from typing import TYPE_CHECKING

import numpy as np
import pyarrow as pa

from .. import config
from ..utils.py_utils import map_nested
from .formatting import TensorFormatter


if TYPE_CHECKING:
       import torch







class 						__magic_name__			(							TensorFormatter[Mapping, 'torch.Tensor', Mapping]							):

   '''simple docstring'''




   def __init__(				self,    lowercase_=None,    **lowercase_       )							->    List[str]:






     """simple docstring"""






     super().__init__(features=SCREAMING_SNAKE_CASE__       )
     a__  =torch_tensor_kwargs
     import torch  # noqa import torch at initialization



   def  _UpperCAmelCase (				self,    lowercase_       )							->    List[str]:






     """simple docstring"""






     import torch

     if isinstance(SCREAMING_SNAKE_CASE__,    SCREAMING_SNAKE_CASE__       ) and column:
       if all(
           isinstance(SCREAMING_SNAKE_CASE__,    torch.Tensor       ) and x.shape == column[0].shape and x.dtype == column[0].dtype
           for x in column       ):
         return torch.stack(SCREAMING_SNAKE_CASE__       )
     return column



   def  _UpperCAmelCase (				self,    lowercase_       )							->    Union[str, Any]:






     """simple docstring"""






     import torch

     if isinstance(SCREAMING_SNAKE_CASE__,    (str, bytes, type(SCREAMING_SNAKE_CASE__       ))       ):
       return value
     elif isinstance(SCREAMING_SNAKE_CASE__,    (np.character, np.ndarray)       ) and np.issubdtype(value.dtype,    np.character       ):
       return value.tolist()

     a__  ={}

     if isinstance(SCREAMING_SNAKE_CASE__,    (np.number, np.ndarray)       ) and np.issubdtype(value.dtype,    np.integer       ):
       a__  ={'''dtype''': torch.intaa}
     elif isinstance(SCREAMING_SNAKE_CASE__,    (np.number, np.ndarray)       ) and np.issubdtype(value.dtype,    np.floating       ):
       a__  ={'''dtype''': torch.floataa}
     elif config.PIL_AVAILABLE and "PIL" in sys.modules:
       import PIL.Image

       if isinstance(SCREAMING_SNAKE_CASE__,    PIL.Image.Image       ):
         a__  =np.asarray(SCREAMING_SNAKE_CASE__       )
     return torch.tensor(SCREAMING_SNAKE_CASE__,    **{**default_dtype, **self.torch_tensor_kwargs}       )



   def  _UpperCAmelCase (				self,    lowercase_       )							->    Optional[Any]:






     """simple docstring"""






     import torch

     # support for torch, tf, jax etc.
     if hasattr(SCREAMING_SNAKE_CASE__,    '''__array__'''       ) and not isinstance(SCREAMING_SNAKE_CASE__,    torch.Tensor       ):
       a__  =data_struct.__array__()
     # support for nested types like struct of list of struct
     if isinstance(SCREAMING_SNAKE_CASE__,    np.ndarray       ):
       if data_struct.dtype == object:  # torch tensors cannot be instantied from an array of objects
         return self._consolidate([self.recursive_tensorize(SCREAMING_SNAKE_CASE__       ) for substruct in data_struct]       )
     elif isinstance(SCREAMING_SNAKE_CASE__,    (list, tuple)       ):
       return self._consolidate([self.recursive_tensorize(SCREAMING_SNAKE_CASE__       ) for substruct in data_struct]       )
     return self._tensorize(SCREAMING_SNAKE_CASE__       )



   def  _UpperCAmelCase (				self,    lowercase_       )							->    int:






     """simple docstring"""






     return map_nested(self._recursive_tensorize,    SCREAMING_SNAKE_CASE__,    map_list=SCREAMING_SNAKE_CASE__       )



   def  _UpperCAmelCase (				self,    lowercase_       )							->    Mapping:






     """simple docstring"""






     a__  =self.numpy_arrow_extractor().extract_row(SCREAMING_SNAKE_CASE__       )
     a__  =self.python_features_decoder.decode_row(SCREAMING_SNAKE_CASE__       )
     return self.recursive_tensorize(SCREAMING_SNAKE_CASE__       )



   def  _UpperCAmelCase (				self,    lowercase_       )							->    "torch.Tensor":






     """simple docstring"""






     a__  =self.numpy_arrow_extractor().extract_column(SCREAMING_SNAKE_CASE__       )
     a__  =self.python_features_decoder.decode_column(SCREAMING_SNAKE_CASE__,    pa_table.column_names[0]       )
     a__  =self.recursive_tensorize(SCREAMING_SNAKE_CASE__       )
     a__  =self._consolidate(SCREAMING_SNAKE_CASE__       )
     return column



   def  _UpperCAmelCase (				self,    lowercase_       )							->    Mapping:






     """simple docstring"""






     a__  =self.numpy_arrow_extractor().extract_batch(SCREAMING_SNAKE_CASE__       )
     a__  =self.python_features_decoder.decode_batch(SCREAMING_SNAKE_CASE__       )
     a__  =self.recursive_tensorize(SCREAMING_SNAKE_CASE__       )
     for column_name in batch:
       a__  =self._consolidate(batch[column_name]       )
     return batch







"""simple docstring"""
from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCamelCase_								=		logging.get_logger(__name__)

lowerCamelCase_								=		{}






class 					_SCREAMING_SNAKE_CASE(   A  ):
       SCREAMING_SNAKE_CASE_   :      List[Any]							=  '''llama'''
       SCREAMING_SNAKE_CASE_   :      Optional[int]							=  ['''past_key_values''']




       def __init__(						self		,SCREAMING_SNAKE_CASE__=3_20_00		,SCREAMING_SNAKE_CASE__=40_96		,SCREAMING_SNAKE_CASE__=1_10_08		,SCREAMING_SNAKE_CASE__=32		,SCREAMING_SNAKE_CASE__=32		,SCREAMING_SNAKE_CASE__=None		,SCREAMING_SNAKE_CASE__="silu"		,SCREAMING_SNAKE_CASE__=20_48		,SCREAMING_SNAKE_CASE__=0.0_2		,SCREAMING_SNAKE_CASE__=1E-6		,SCREAMING_SNAKE_CASE__=True		,SCREAMING_SNAKE_CASE__=0		,SCREAMING_SNAKE_CASE__=1		,SCREAMING_SNAKE_CASE__=2		,SCREAMING_SNAKE_CASE__=1		,SCREAMING_SNAKE_CASE__=False		,SCREAMING_SNAKE_CASE__=None		,**SCREAMING_SNAKE_CASE__		,)			->      Union[str, Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  vocab_size
             __SCREAMING_SNAKE_CASE   :int						=  max_position_embeddings
             __SCREAMING_SNAKE_CASE   :List[str]						=  hidden_size
             __SCREAMING_SNAKE_CASE   :Tuple						=  intermediate_size
             __SCREAMING_SNAKE_CASE   :List[str]						=  num_hidden_layers
             __SCREAMING_SNAKE_CASE   :List[Any]						=  num_attention_heads

             # for backward compatibility
             if num_key_value_heads is None:
                   __SCREAMING_SNAKE_CASE   :Optional[int]						=  num_attention_heads

             __SCREAMING_SNAKE_CASE   :str						=  num_key_value_heads
             __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  hidden_act
             __SCREAMING_SNAKE_CASE   :List[str]						=  initializer_range
             __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  rms_norm_eps
             __SCREAMING_SNAKE_CASE   :Dict						=  pretraining_tp
             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  use_cache
             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  rope_scaling
             self._rope_scaling_validation()

             super().__init__(
                 pad_token_id=SCREAMING_SNAKE_CASE__		,bos_token_id=SCREAMING_SNAKE_CASE__		,eos_token_id=SCREAMING_SNAKE_CASE__		,tie_word_embeddings=SCREAMING_SNAKE_CASE__		,**SCREAMING_SNAKE_CASE__		,)




       def     _UpperCamelCase     (						self							)			->      List[Any]:





             """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}'''							)
             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  self.rope_scaling.get('''type'''		,SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE   :Optional[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}'''							)