



"""simple docstring"""





# Lint as: python3
import sys
from collections.abc import Mapping
from typing import TYPE_CHECKING, Dict, Optional

import numpy as np
import pyarrow as pa

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


if TYPE_CHECKING:
  import jax
  import jaxlib

lowerCamelCase       	=						get_logger()

lowerCamelCase       	=						None
class 				lowercase__       (   TensorFormatter[Mapping, '''jax.Array''', Mapping]				):







       '''simple docstring'''


       def __init__(							self      :  Optional[int]		,    _UpperCAmelCase      :  List[str]=None		,    _UpperCAmelCase      :  Optional[int]=None		,    **_UpperCAmelCase      :  Tuple   )			->						Union[str, Any]:


             '''simple docstring'''




             super().__init__(features=_UpperCAmelCase   )
             import jax
             from jaxlib.xla_client import Device

             if isinstance(_UpperCAmelCase		,    _UpperCAmelCase   ):
                   raise ValueError(
                       F"""Expected {device} to be a `str` not {type(_UpperCAmelCase   )}, as `jaxlib.xla_extension.Device` """
                       "is not serializable neither with `pickle` nor with `dill`. Instead you can surround "
                       "the device with `str()` to get its string identifier that will be internally mapped "
                       "to the actual `jaxlib.xla_extension.Device`."   )
             UpperCAmelCase_      			=							device if isinstance(_UpperCAmelCase		,    _UpperCAmelCase   ) else str(jax.devices()[0]   )
             # using global variable since `jaxlib.xla_extension.Device` is not serializable neither
             # with `pickle` nor with `dill`, so we need to use a global variable instead
             global DEVICE_MAPPING
             if DEVICE_MAPPING is None:
                   UpperCAmelCase_      			=							self._map_devices_to_str()
             if self.device not in list(DEVICE_MAPPING.keys()   ):
                   logger.warning(
                       F"""Device with string identifier {self.device} not listed among the available """
                       F"""devices: {list(DEVICE_MAPPING.keys()   )}, so falling back to the default """
                       F"""device: {str(jax.devices()[0]   )}."""   )
                   UpperCAmelCase_      			=							str(jax.devices()[0]   )
             UpperCAmelCase_      			=							jnp_array_kwargs



       @staticmethod
       def 					lowercase__    (							)			->						Dict[str, "jaxlib.xla_extension.Device"]:


             '''simple docstring'''




             import jax

             return {str(_UpperCAmelCase   ): device for device in jax.devices()}



       def 					lowercase__    (							self      :  int		,    _UpperCAmelCase      :  Optional[Any]   )			->						str:


             '''simple docstring'''




             import jax
             import jax.numpy as jnp

             if isinstance(_UpperCAmelCase		,    _UpperCAmelCase   ) and column:
                   if all(
                       isinstance(_UpperCAmelCase		,    jax.Array   ) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column   ):
                         return jnp.stack(_UpperCAmelCase		,    axis=0   )
             return column



       def 					lowercase__    (							self      :  Any		,    _UpperCAmelCase      :  Optional[int]   )			->						List[Any]:


             '''simple docstring'''




             import jax
             import jax.numpy as jnp

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

             UpperCAmelCase_      			=							{}

             if isinstance(_UpperCAmelCase		,    (np.number, np.ndarray)   ) and np.issubdtype(value.dtype		,    np.integer   ):
                   # the default int precision depends on the jax config
                   # see https://jax.readthedocs.io/en/latest/notebooks/Common_Gotchas_in_JAX.html#double-64bit-precision
                   if jax.config.jax_enable_xaa:
                         UpperCAmelCase_      			=							{"dtype": jnp.intaa}
                   else:
                         UpperCAmelCase_      			=							{"dtype": jnp.intaa}
             elif isinstance(_UpperCAmelCase		,    (np.number, np.ndarray)   ) and np.issubdtype(value.dtype		,    np.floating   ):
                   UpperCAmelCase_      			=							{"dtype": jnp.floataa}
             elif config.PIL_AVAILABLE and "PIL" in sys.modules:
                   import PIL.Image

                   if isinstance(_UpperCAmelCase		,    PIL.Image.Image   ):
                         UpperCAmelCase_      			=							np.asarray(_UpperCAmelCase   )

        # using global variable since `jaxlib.xla_extension.Device` is not serializable neither
        # with `pickle` nor with `dill`, so we need to use a global variable instead
             global DEVICE_MAPPING
             if DEVICE_MAPPING is None:
                   UpperCAmelCase_      			=							self._map_devices_to_str()

             with jax.default_device(DEVICE_MAPPING[self.device]   ):
                   # calling jnp.array on a np.ndarray does copy the data
                   # see https://github.com/google/jax/issues/4486
                   return jnp.array(_UpperCAmelCase		,    **{**default_dtype, **self.jnp_array_kwargs}   )



       def 					lowercase__    (							self      :  str		,    _UpperCAmelCase      :  Optional[Any]   )			->						Optional[int]:


             '''simple docstring'''




             import jax

             # support for torch, tf, jax etc.
             if config.TORCH_AVAILABLE and "torch" in sys.modules:
                   import torch

                   if isinstance(_UpperCAmelCase		,    torch.Tensor   ):
                         return self._tensorize(data_struct.detach().cpu().numpy()[()]   )
             if hasattr(_UpperCAmelCase		,    "__array__"   ) and not isinstance(_UpperCAmelCase		,    jax.Array   ):
                   UpperCAmelCase_      			=							data_struct.__array__()
             # support for nested types like struct of list of struct
             if isinstance(_UpperCAmelCase		,    np.ndarray   ):
                   if data_struct.dtype == object:  # jax arrays cannot be instantied from an array of objects
                         return self._consolidate([self.recursive_tensorize(_UpperCAmelCase   ) for substruct in data_struct]   )
             elif isinstance(_UpperCAmelCase		,    (list, tuple)   ):
                   return self._consolidate([self.recursive_tensorize(_UpperCAmelCase   ) for substruct in data_struct]   )
             return self._tensorize(_UpperCAmelCase   )



       def 					lowercase__    (							self      :  List[str]		,    _UpperCAmelCase      :  dict   )			->						Union[str, Any]:


             '''simple docstring'''




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



       def 					lowercase__    (							self      :  Union[str, Any]		,    _UpperCAmelCase      :  pa.Table   )			->						Mapping:


             '''simple docstring'''




             UpperCAmelCase_      			=							self.numpy_arrow_extractor().extract_row(_UpperCAmelCase   )
             UpperCAmelCase_      			=							self.python_features_decoder.decode_row(_UpperCAmelCase   )
             return self.recursive_tensorize(_UpperCAmelCase   )



       def 					lowercase__    (							self      :  Any		,    _UpperCAmelCase      :  pa.Table   )			->						"jax.Array":


             '''simple docstring'''




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







       def 					lowercase__    (							self      :  int		,    _UpperCAmelCase      :  pa.Table   )			->						Mapping:


             '''simple docstring'''




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









"""simple docstring"""





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


lowerCamelCase       	=						logging.get_logger(__name__)

lowerCamelCase       	=						{
    """RWKV/rwkv-4-169m-pile""": """https://huggingface.co/RWKV/rwkv-4-169m-pile/resolve/main/config.json""",
    """RWKV/rwkv-4-430m-pile""": """https://huggingface.co/RWKV/rwkv-4-430m-pile/resolve/main/config.json""",
    """RWKV/rwkv-4-1b5-pile""": """https://huggingface.co/RWKV/rwkv-4-1b5-pile/resolve/main/config.json""",
    """RWKV/rwkv-4-3b-pile""": """https://huggingface.co/RWKV/rwkv-4-3b-pile/resolve/main/config.json""",
    """RWKV/rwkv-4-7b-pile""": """https://huggingface.co/RWKV/rwkv-4-7b-pile/resolve/main/config.json""",
    """RWKV/rwkv-4-14b-pile""": """https://huggingface.co/RWKV/rwkv-4-14b-pile/resolve/main/config.json""",
    """RWKV/rwkv-raven-1b5""": """https://huggingface.co/RWKV/rwkv-raven-1b5/resolve/main/config.json""",
    """RWKV/rwkv-raven-3b""": """https://huggingface.co/RWKV/rwkv-raven-3b/resolve/main/config.json""",
    """RWKV/rwkv-raven-7b""": """https://huggingface.co/RWKV/rwkv-raven-7b/resolve/main/config.json""",
    """RWKV/rwkv-raven-14b""": """https://huggingface.co/RWKV/rwkv-raven-14b/resolve/main/config.json""",
}
class 				lowercase__       (   SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       UpperCamelCase						=					'''rwkv'''
       UpperCamelCase						=					{'''max_position_embeddings''': '''context_length'''}



       def __init__(							self      :  str		,    _UpperCAmelCase      :  int=50277		,    _UpperCAmelCase      :  Optional[Any]=1024		,    _UpperCAmelCase      :  str=4096		,    _UpperCAmelCase      :  Any=32		,    _UpperCAmelCase      :  Any=None		,    _UpperCAmelCase      :  Optional[int]=None		,    _UpperCAmelCase      :  Tuple=1e-5		,    _UpperCAmelCase      :  str=0		,    _UpperCAmelCase      :  Any=0		,    _UpperCAmelCase      :  Union[str, Any]=6		,    _UpperCAmelCase      :  Dict=False		,    _UpperCAmelCase      :  Any=True		,    **_UpperCAmelCase      :  Optional[Any]		,    )			->						str:


             '''simple docstring'''




             UpperCAmelCase_      			=							vocab_size
             UpperCAmelCase_      			=							context_length
             UpperCAmelCase_      			=							hidden_size
             UpperCAmelCase_      			=							num_hidden_layers
             UpperCAmelCase_      			=							attention_hidden_size if attention_hidden_size is not None else hidden_size
             UpperCAmelCase_      			=							intermediate_size if intermediate_size is not None else 4 * hidden_size
             UpperCAmelCase_      			=							layer_norm_epsilon
             UpperCAmelCase_      			=							rescale_every
             UpperCAmelCase_      			=							use_cache

             UpperCAmelCase_      			=							bos_token_id
             UpperCAmelCase_      			=							eos_token_id

             super().__init__(
                 tie_word_embeddings=_UpperCAmelCase		,    bos_token_id=_UpperCAmelCase		,    eos_token_id=_UpperCAmelCase		,    **_UpperCAmelCase   )




