


'''simple docstring'''




import inspect
import tempfile
from collections import OrderedDict, UserDict
from collections.abc import MutableMapping
from contextlib import ExitStack, contextmanager
from dataclasses import fields
from enum import Enum
from typing import Any, ContextManager, List, Tuple

import numpy as np

from .import_utils import is_flax_available, is_tf_available, is_torch_available, is_torch_fx_proxy


if is_flax_available():
       import jax.numpy as jnp

class 		UpperCAmelCase_      (  __lowercase		):





     def __get__( self       :      Dict    ,      UpperCAmelCase__       :      str    ,      UpperCAmelCase__       :      List[Any]=None    )				->  str:


        if obj is None:
           return self
        if self.fget is None:
           raise AttributeError('unreadable attribute'    )
        lowerCAmelCase     =			'__cached_' + self.fget.__name__
        lowerCAmelCase     =			getattr(__UpperCAmelCase    ,      __UpperCAmelCase    ,      __UpperCAmelCase    )
        if cached is None:
           lowerCAmelCase     =			self.fget(__UpperCAmelCase    )
           setattr(__UpperCAmelCase    ,      __UpperCAmelCase    ,      __UpperCAmelCase    )
        return cached





def 	a_					( lowerCamelCase	:     int				):
   lowerCAmelCase     =			val.lower()
   if val in {"y", "yes", "t", "true", "on", "1"}:
      return 1
   if val in {"n", "no", "f", "false", "off", "0"}:
      return 0
   raise ValueError(f'''invalid truth value {val!r}'''				)





def 	a_					( lowerCamelCase	:     str				):
   if is_torch_fx_proxy(lowerCamelCase				):
      return True
   if is_torch_available():
      import torch

      if isinstance(lowerCamelCase	, torch.Tensor				):
         return True
   if is_tf_available():
      import tensorflow as tf

      if isinstance(lowerCamelCase	, tf.Tensor				):
         return True

   if is_flax_available():
      import jax.numpy as jnp
      from jax.core import Tracer

      if isinstance(lowerCamelCase	, (jnp.ndarray, Tracer)				):
         return True

   return isinstance(lowerCamelCase	, np.ndarray				)





def 	a_					( lowerCamelCase	:     Optional[Any]				):
   return isinstance(lowerCamelCase	, np.ndarray				)





def 	a_					( lowerCamelCase	:     Tuple				):
   return _is_numpy(lowerCamelCase				)





def 	a_					( lowerCamelCase	:     Dict				):
   import torch

   return isinstance(lowerCamelCase	, torch.Tensor				)





def 	a_					( lowerCamelCase	:     Tuple				):
   return False if not is_torch_available() else _is_torch(lowerCamelCase				)





def 	a_					( lowerCamelCase	:     List[str]				):
   import torch

   return isinstance(lowerCamelCase	, torch.device				)





def 	a_					( lowerCamelCase	:     str				):
   return False if not is_torch_available() else _is_torch_device(lowerCamelCase				)





def 	a_					( lowerCamelCase	:     Any				):
   import torch

   if isinstance(lowerCamelCase	, lowerCamelCase				):
      if hasattr(lowerCamelCase	, lowerCamelCase				):
         lowerCAmelCase     =			getattr(lowerCamelCase	, lowerCamelCase				)
      else:
         return False
   return isinstance(lowerCamelCase	, torch.dtype				)





def 	a_					( lowerCamelCase	:     Union[str, Any]				):
   return False if not is_torch_available() else _is_torch_dtype(lowerCamelCase				)





def 	a_					( lowerCamelCase	:     str				):
   import tensorflow as tf

   return isinstance(lowerCamelCase	, tf.Tensor				)





def 	a_					( lowerCamelCase	:     Optional[Any]				):
   return False if not is_tf_available() else _is_tensorflow(lowerCamelCase				)





def 	a_					( lowerCamelCase	:     Optional[int]				):
   import tensorflow as tf

   # the `is_symbolic_tensor` predicate is only available starting with TF 2.14
   if hasattr(lowerCamelCase	, 'is_symbolic_tensor'				):
      return tf.is_symbolic_tensor(lowerCamelCase				)
   return type(lowerCamelCase				) == tf.Tensor





def 	a_					( lowerCamelCase	:     str				):
   return False if not is_tf_available() else _is_tf_symbolic_tensor(lowerCamelCase				)





def 	a_					( lowerCamelCase	:     Union[str, Any]				):
   import jax.numpy as jnp  # noqa: F811

   return isinstance(lowerCamelCase	, jnp.ndarray				)





def 	a_					( lowerCamelCase	:     List[str]				):
   return False if not is_flax_available() else _is_jax(lowerCamelCase				)





def 	a_					( lowerCamelCase	:     Tuple				):
   if isinstance(lowerCamelCase	, (dict, UserDict)				):
      return {k: to_py_obj(lowerCamelCase				) for k, v in obj.items()}
   elif isinstance(lowerCamelCase	, (list, tuple)				):
      return [to_py_obj(lowerCamelCase				) for o in obj]
   elif is_tf_tensor(lowerCamelCase				):
      return obj.numpy().tolist()
   elif is_torch_tensor(lowerCamelCase				):
      return obj.detach().cpu().tolist()
   elif is_jax_tensor(lowerCamelCase				):
      return np.asarray(lowerCamelCase				).tolist()
   elif isinstance(lowerCamelCase	, (np.ndarray, np.number)				):  # tolist also works on 0d np arrays
      return obj.tolist()
   else:
      return obj





def 	a_					( lowerCamelCase	:     Dict				):
   if isinstance(lowerCamelCase	, (dict, UserDict)				):
      return {k: to_numpy(lowerCamelCase				) for k, v in obj.items()}
   elif isinstance(lowerCamelCase	, (list, tuple)				):
      return np.array(lowerCamelCase				)
   elif is_tf_tensor(lowerCamelCase				):
      return obj.numpy()
   elif is_torch_tensor(lowerCamelCase				):
      return obj.detach().cpu().numpy()
   elif is_jax_tensor(lowerCamelCase				):
      return np.asarray(lowerCamelCase				)
   else:
      return obj

class 		UpperCAmelCase_      (  __lowercase		):





     def 							__UpperCAmelCase      ( self       :      Dict    )				->  List[Any]:
        lowerCAmelCase     =			fields(self    )

        # Safety and consistency checks
        if not len(__UpperCAmelCase    ):
           raise ValueError(F'''{self.__class__.__name__} has no fields.'''    )
        if not all(field.default is None for field in class_fields[1:]    ):
           raise ValueError(F'''{self.__class__.__name__} should not have more than one required field.'''    )

        lowerCAmelCase     =			getattr(self    ,      class_fields[0].name    )
        lowerCAmelCase     =			all(getattr(self    ,      field.name    ) is None for field in class_fields[1:]    )

        if other_fields_are_none and not is_tensor(__UpperCAmelCase    ):
           if isinstance(__UpperCAmelCase    ,      __UpperCAmelCase    ):
              lowerCAmelCase     =			first_field.items()
              lowerCAmelCase     =			True
           else:
              try:
                 lowerCAmelCase     =			iter(__UpperCAmelCase    )
                 lowerCAmelCase     =			True
              except TypeError:
                 lowerCAmelCase     =			False

                   # if we provided an iterator as first field and the iterator is a (key, value) iterator
                   # set the associated fields
           if first_field_iterator:
              for idx, element in enumerate(__UpperCAmelCase    ):
                 if (
                     not isinstance(__UpperCAmelCase    ,      (list, tuple)    )
                     or not len(__UpperCAmelCase    ) == 2
                     or not isinstance(element[0]    ,      __UpperCAmelCase    )
                 ):
                    if idx == 0:
                       # If we do not have an iterator of key/values, set it as attribute
                       lowerCAmelCase     =			first_field
                    else:
                       # If we have a mixed iterator, raise an error
                       raise ValueError(
                           F'''Cannot set key/value for {element}. It needs to be a tuple (key, value).'''    )
                    break
                 setattr(self    ,      element[0]    ,      element[1]    )
                 if element[1] is not None:
                    lowerCAmelCase     =			element[1]
           elif first_field is not None:
              lowerCAmelCase     =			first_field
        else:
           for field in class_fields:
              lowerCAmelCase     =			getattr(self    ,      field.name    )
              if v is not None:
                 lowerCAmelCase     =			v





     def __delitem__( self       :      List[str]    ,      *UpperCAmelCase__       :      str    ,      **UpperCAmelCase__       :      Union[str, Any]    )				->  Optional[int]:
        raise Exception(F'''You cannot use ``__delitem__`` on a {self.__class__.__name__} instance.'''    )





     def 							__UpperCAmelCase      ( self       :      str    ,      *UpperCAmelCase__       :      Union[str, Any]    ,      **UpperCAmelCase__       :      int    )				->  Optional[Any]:
        raise Exception(F'''You cannot use ``setdefault`` on a {self.__class__.__name__} instance.'''    )





     def 							__UpperCAmelCase      ( self       :      List[str]    ,      *UpperCAmelCase__       :      Any    ,      **UpperCAmelCase__       :      Optional[int]    )				->  str:
        raise Exception(F'''You cannot use ``pop`` on a {self.__class__.__name__} instance.'''    )





     def 							__UpperCAmelCase      ( self       :      Optional[int]    ,      *UpperCAmelCase__       :      Dict    ,      **UpperCAmelCase__       :      List[str]    )				->  List[Any]:
        raise Exception(F'''You cannot use ``update`` on a {self.__class__.__name__} instance.'''    )





     def __getitem__( self       :      Optional[Any]    ,      UpperCAmelCase__       :      int    )				->  Union[str, Any]:


        if isinstance(__UpperCAmelCase    ,      __UpperCAmelCase    ):
           lowerCAmelCase     =			dict(self.items()    )
           return inner_dict[k]
        else:
           return self.to_tuple()[k]





     def __setattr__( self       :      List[str]    ,      UpperCAmelCase__       :      Any    ,      UpperCAmelCase__       :      List[str]    )				->  Any:


        if name in self.keys() and value is not None:
           # Don't call self.__setitem__ to avoid recursion errors
           super().__setitem__(__UpperCAmelCase    ,      __UpperCAmelCase    )
        super().__setattr__(__UpperCAmelCase    ,      __UpperCAmelCase    )





     def __setitem__( self       :      List[str]    ,      UpperCAmelCase__       :      List[Any]    ,      UpperCAmelCase__       :      Optional[Any]    )				->  Optional[Any]:
        super().__setitem__(__UpperCAmelCase    ,      __UpperCAmelCase    )
        # Don't call self.__setattr__ to avoid recursion errors
        super().__setattr__(__UpperCAmelCase    ,      __UpperCAmelCase    )





     def 							__UpperCAmelCase      ( self       :      Optional[Any]    )				->  Tuple[Any]:
        return tuple(self[k] for k in self.keys()    )




class 		UpperCAmelCase_      (  __lowercase       ,	__lowercase		):





     @classmethod
     def 							__UpperCAmelCase      ( cls       :      Optional[Any]    ,      UpperCAmelCase__       :      Dict    )				->  List[str]:
        raise ValueError(
            F'''{value} is not a valid {cls.__name__}, please select one of {list(cls._valueamember_map_.keys()    )}'''    )




class 		UpperCAmelCase_      (  __lowercase		):
     lowerCamelCase				:	Union[str, Any]		=     '''longest'''
     lowerCamelCase				:	Optional[int]		=     '''max_length'''
     lowerCamelCase				:	Optional[int]		=     '''do_not_pad'''




class 		UpperCAmelCase_      (  __lowercase		):
     lowerCamelCase				:	Union[str, Any]		=     '''pt'''
     lowerCamelCase				:	Tuple		=     '''tf'''
     lowerCamelCase				:	Tuple		=     '''np'''
     lowerCamelCase				:	Dict		=     '''jax'''




class 		UpperCAmelCase_      :





     def __init__( self       :      Optional[int]    ,      UpperCAmelCase__       :      List[ContextManager]    )				->  Optional[Any]:
        lowerCAmelCase     =			context_managers
        lowerCAmelCase     =			ExitStack()





     def __enter__( self       :      str    )				->  Dict:


        for context_manager in self.context_managers:
           self.stack.enter_context(__UpperCAmelCase    )





     def __exit__( self       :      int    ,      *UpperCAmelCase__       :      Optional[int]    ,      **UpperCAmelCase__       :      Optional[int]    )				->  Optional[int]:
        self.stack.__exit__(*__UpperCAmelCase    ,      **__UpperCAmelCase    )





def 	a_					( lowerCamelCase	:     List[Any]				):
   lowerCAmelCase     =			infer_framework(lowerCamelCase				)
   if framework == "tf":
      lowerCAmelCase     =			inspect.signature(model_class.call				)  # TensorFlow models
   elif framework == "pt":
      lowerCAmelCase     =			inspect.signature(model_class.forward				)  # PyTorch models
   else:
      lowerCAmelCase     =			inspect.signature(model_class.__call__				)  # Flax models

   for p in signature.parameters:
      if p == "return_loss" and signature.parameters[p].default is True:
         return True

   return False





def 	a_					( lowerCamelCase	:     Union[str, Any]				):
   lowerCAmelCase     =			model_class.__name__
   lowerCAmelCase     =			infer_framework(lowerCamelCase				)
   if framework == "tf":
      lowerCAmelCase     =			inspect.signature(model_class.call				)  # TensorFlow models
   elif framework == "pt":
      lowerCAmelCase     =			inspect.signature(model_class.forward				)  # PyTorch models
   else:
      lowerCAmelCase     =			inspect.signature(model_class.__call__				)  # Flax models

   if "QuestionAnswering" in model_name:
      return [p for p in signature.parameters if "label" in p or p in ("start_positions", "end_positions")]
   else:
      return [p for p in signature.parameters if "label" in p]





def 	a_					( lowerCamelCase	:     MutableMapping	, lowerCamelCase	:     str = ""	, lowerCamelCase	:     str = "."				):

   def _flatten_dict(lowerCamelCase	:     Optional[Any]	, lowerCamelCase	:     int=""	, lowerCamelCase	:     List[str]="."				):
      for k, v in d.items():
         lowerCAmelCase     =			str(lowerCamelCase				) + delimiter + str(lowerCamelCase				) if parent_key else k
         if v and isinstance(lowerCamelCase	, lowerCamelCase				):
            yield from flatten_dict(lowerCamelCase	, lowerCamelCase	, delimiter=lowerCamelCase				).items()
         else:
            yield key, v

   return dict(_flatten_dict(lowerCamelCase	, lowerCamelCase	, lowerCamelCase				)				)





@contextmanager
def 	a_					( lowerCamelCase	:     Any	, lowerCamelCase	:     bool = False				):
   if use_temp_dir:
      with tempfile.TemporaryDirectory() as tmp_dir:
         yield tmp_dir
   else:
      yield working_dir





def 	a_					( lowerCamelCase	:     List[Any]	, lowerCamelCase	:     List[str]=None				):
   if is_numpy_array(lowerCamelCase				):
      return np.transpose(lowerCamelCase	, axes=lowerCamelCase				)
   elif is_torch_tensor(lowerCamelCase				):
      return array.T if axes is None else array.permute(*lowerCamelCase				)
   elif is_tf_tensor(lowerCamelCase				):
      import tensorflow as tf

      return tf.transpose(lowerCamelCase	, perm=lowerCamelCase				)
   elif is_jax_tensor(lowerCamelCase				):
      return jnp.transpose(lowerCamelCase	, axes=lowerCamelCase				)
   else:
      raise ValueError(f'''Type not supported for transpose: {type(lowerCamelCase				)}.'''				)





def 	a_					( lowerCamelCase	:     int	, lowerCamelCase	:     Optional[int]				):
   if is_numpy_array(lowerCamelCase				):
      return np.reshape(lowerCamelCase	, lowerCamelCase				)
   elif is_torch_tensor(lowerCamelCase				):
      return array.reshape(*lowerCamelCase				)
   elif is_tf_tensor(lowerCamelCase				):
      import tensorflow as tf

      return tf.reshape(lowerCamelCase	, lowerCamelCase				)
   elif is_jax_tensor(lowerCamelCase				):
      return jnp.reshape(lowerCamelCase	, lowerCamelCase				)
   else:
      raise ValueError(f'''Type not supported for reshape: {type(lowerCamelCase				)}.'''				)





def 	a_					( lowerCamelCase	:     Dict	, lowerCamelCase	:     int=None				):
   if is_numpy_array(lowerCamelCase				):
      return np.squeeze(lowerCamelCase	, axis=lowerCamelCase				)
   elif is_torch_tensor(lowerCamelCase				):
      return array.squeeze() if axis is None else array.squeeze(dim=lowerCamelCase				)
   elif is_tf_tensor(lowerCamelCase				):
      import tensorflow as tf

      return tf.squeeze(lowerCamelCase	, axis=lowerCamelCase				)
   elif is_jax_tensor(lowerCamelCase				):
      return jnp.squeeze(lowerCamelCase	, axis=lowerCamelCase				)
   else:
      raise ValueError(f'''Type not supported for squeeze: {type(lowerCamelCase				)}.'''				)





def 	a_					( lowerCamelCase	:     Dict	, lowerCamelCase	:     Optional[int]				):
   if is_numpy_array(lowerCamelCase				):
      return np.expand_dims(lowerCamelCase	, lowerCamelCase				)
   elif is_torch_tensor(lowerCamelCase				):
      return array.unsqueeze(dim=lowerCamelCase				)
   elif is_tf_tensor(lowerCamelCase				):
      import tensorflow as tf

      return tf.expand_dims(lowerCamelCase	, axis=lowerCamelCase				)
   elif is_jax_tensor(lowerCamelCase				):
      return jnp.expand_dims(lowerCamelCase	, axis=lowerCamelCase				)
   else:
      raise ValueError(f'''Type not supported for expand_dims: {type(lowerCamelCase				)}.'''				)





def 	a_					( lowerCamelCase	:     List[Any]				):
   if is_numpy_array(lowerCamelCase				):
      return np.size(lowerCamelCase				)
   elif is_torch_tensor(lowerCamelCase				):
      return array.numel()
   elif is_tf_tensor(lowerCamelCase				):
      import tensorflow as tf

      return tf.size(lowerCamelCase				)
   elif is_jax_tensor(lowerCamelCase				):
      return array.size
   else:
      raise ValueError(f'''Type not supported for expand_dims: {type(lowerCamelCase				)}.'''				)





def 	a_					( lowerCamelCase	:     List[Any]	, lowerCamelCase	:     int				):
   for key, value in auto_map.items():
      if isinstance(lowerCamelCase	, (tuple, list)				):
         lowerCAmelCase     =			[f'''{repo_id}--{v}''' if (v is not None and '--' not in v) else v for v in value]
      elif value is not None and "--" not in value:
         lowerCAmelCase     =			f'''{repo_id}--{value}'''

   return auto_map





def 	a_					( lowerCamelCase	:     str				):
   for base_class in inspect.getmro(lowerCamelCase				):
      lowerCAmelCase     =			base_class.__module__
      lowerCAmelCase     =			base_class.__name__
      if module.startswith('tensorflow'				) or module.startswith('keras'				) or name == "TFPreTrainedModel":
         return "tf"
      elif module.startswith('torch'				) or name == "PreTrainedModel":
         return "pt"
      elif module.startswith('flax'				) or module.startswith('jax'				) or name == "FlaxPreTrainedModel":
         return "flax"
   else:
      raise TypeError(f'''Could not infer framework from class {model_class}.'''				)













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


UpperCAmelCase__				   =		logging.get_logger(__name__)

UpperCAmelCase__				   =		{}






class 		lowercase_					(	lowercase   ):






      '''simple docstring'''




      __snake_case       =  '''llama'''
      __snake_case       =  ['''past_key_values''']







      def __init__(      self      :						Optional[Any] ,					__UpperCAmelCase      :						Union[str, Any]=32_000 ,					__UpperCAmelCase      :						str=4_096 ,					__UpperCAmelCase      :						int=11_008 ,					__UpperCAmelCase      :						Tuple=32 ,					__UpperCAmelCase      :						Optional[int]=32 ,					__UpperCAmelCase      :						List[str]=None ,					__UpperCAmelCase      :						Union[str, Any]="silu" ,					__UpperCAmelCase      :						Tuple=2_048 ,					__UpperCAmelCase      :						Optional[Any]=0.02 ,					__UpperCAmelCase      :						Any=1e-6 ,					__UpperCAmelCase      :						Union[str, Any]=True ,					__UpperCAmelCase      :						Optional[int]=0 ,					__UpperCAmelCase      :						Optional[int]=1 ,					__UpperCAmelCase      :						Optional[int]=2 ,					__UpperCAmelCase      :						Tuple=1 ,					__UpperCAmelCase      :						List[str]=False ,					__UpperCAmelCase      :						Tuple=None ,					**__UpperCAmelCase      :						Tuple ,					)    ->str:




       """simple docstring"""


       a		  =      vocab_size
       a		  =      max_position_embeddings
       a		  =      hidden_size
       a		  =      intermediate_size
       a		  =      num_hidden_layers
       a		  =      num_attention_heads

       # for backward compatibility
       if num_key_value_heads is None:
        a		  =      num_attention_heads

       a		  =      num_key_value_heads
       a		  =      hidden_act
       a		  =      initializer_range
       a		  =      rms_norm_eps
       a		  =      pretraining_tp
       a		  =      use_cache
       a		  =      rope_scaling
       self._rope_scaling_validation()

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







      def       __lowerCAmelCase					(      self      :						Tuple  )    ->Tuple:




       """simple docstring"""


       if self.rope_scaling is None:
        return

       if not isinstance(self.rope_scaling ,					__UpperCAmelCase  ) 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}"""  )
       a		  =      self.rope_scaling.get('''type''' ,					__UpperCAmelCase  )
       a		  =      self.rope_scaling.get('''factor''' ,					__UpperCAmelCase  )
       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(__UpperCAmelCase ,					__UpperCAmelCase  ) or rope_scaling_factor <= 1.0:
        raise ValueError(F"""`rope_scaling`'s factor field must be an float > 1, got {rope_scaling_factor}"""  )


