






'''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

_A			:    Union[str, Any]					  =get_logger()

_A			:    Optional[dict]					  =None

class _lowercase			(       TensorFormatter[Mapping, """jax.Array""", Mapping]	):




							def __init__(							self:       List[Any]    ,							UpperCamelCase__:       List[Any]=None    ,							UpperCamelCase__:       Union[str, Any]=None    ,							**UpperCamelCase__:       Optional[int]  ):
														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`."""  )
														lowerCamelCase__      :			List[str] 						= 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:
																					lowerCamelCase__      :			str 						= 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]  )}.'''  )
																					lowerCamelCase__      :			str 						= str(jax.devices()[0]  )
														lowerCamelCase__      :			List[Any] 						= jnp_array_kwargs




							@staticmethod
							def 	lowerCamelCase_       (							):
														import jax

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




							def 	lowerCamelCase_       (							self:       Optional[Any]    ,							UpperCamelCase__:       List[Any]  ):
														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 	lowerCamelCase_       (							self:       List[Any]    ,							UpperCamelCase__:       Optional[Any]  ):
														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()

														lowerCamelCase__      :			int 						= {}

														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:
																												lowerCamelCase__      :			Union[str, Any] 						= {"""dtype""": jnp.intaa}
																					else:
																												lowerCamelCase__      :			Any 						= {"""dtype""": jnp.intaa}
														elif isinstance(UpperCamelCase__    ,							(np.number, np.ndarray)  ) and np.issubdtype(value.dtype    ,							np.floating  ):
																					lowerCamelCase__      :			Any 						= {"""dtype""": jnp.floataa}
														elif config.PIL_AVAILABLE and "PIL" in sys.modules:
																					import PIL.Image

																					if isinstance(UpperCamelCase__    ,							PIL.Image.Image  ):
																												lowerCamelCase__      :			List[str] 						= 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:
																					lowerCamelCase__      :			Optional[int] 						= 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 	lowerCamelCase_       (							self:       Tuple    ,							UpperCamelCase__:       Optional[Any]  ):
														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  ):
																					lowerCamelCase__      :			List[Any] 						= 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 	lowerCamelCase_       (							self:       Any    ,							UpperCamelCase__:       dict  ):
														return map_nested(self._recursive_tensorize    ,							UpperCamelCase__    ,							map_list=UpperCamelCase__  )




							def 	lowerCamelCase_       (							self:       Union[str, Any]    ,							UpperCamelCase__:       pa.Table  ):
														lowerCamelCase__      :			str 						= self.numpy_arrow_extractor().extract_row(UpperCamelCase__  )
														lowerCamelCase__      :			List[str] 						= self.python_features_decoder.decode_row(UpperCamelCase__  )
														return self.recursive_tensorize(UpperCamelCase__  )




							def 	lowerCamelCase_       (							self:       Optional[int]    ,							UpperCamelCase__:       pa.Table  ):
														lowerCamelCase__      :			List[Any] 						= self.numpy_arrow_extractor().extract_column(UpperCamelCase__  )
														lowerCamelCase__      :			Optional[Any] 						= self.python_features_decoder.decode_column(UpperCamelCase__    ,							pa_table.column_names[0]  )
														lowerCamelCase__      :			int 						= self.recursive_tensorize(UpperCamelCase__  )
														lowerCamelCase__      :			Any 						= self._consolidate(UpperCamelCase__  )
														return column




							def 	lowerCamelCase_       (							self:       Optional[Any]    ,							UpperCamelCase__:       pa.Table  ):
														lowerCamelCase__      :			Any 						= self.numpy_arrow_extractor().extract_batch(UpperCamelCase__  )
														lowerCamelCase__      :			int 						= self.python_features_decoder.decode_batch(UpperCamelCase__  )
														lowerCamelCase__      :			int 						= self.recursive_tensorize(UpperCamelCase__  )
														for column_name in batch:
																					lowerCamelCase__      :			Tuple 						= self._consolidate(batch[column_name]  )
														return batch









'''simple docstring'''







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


_A			:    str					  =logging.get_logger(__name__)

_A			:    int					  ={
    '''weiweishi/roc-bert-base-zh''': '''https://huggingface.co/weiweishi/roc-bert-base-zh/resolve/main/config.json''',
}

class _lowercase			(       _lowercase	):
							a          = """roc_bert"""




							def __init__(							self:       Optional[Any]    ,							UpperCamelCase__:       Any=30_522    ,							UpperCamelCase__:       Optional[Any]=768    ,							UpperCamelCase__:       Union[str, Any]=12    ,							UpperCamelCase__:       Tuple=12    ,							UpperCamelCase__:       Tuple=3_072    ,							UpperCamelCase__:       str="gelu"    ,							UpperCamelCase__:       List[Any]=0.1    ,							UpperCamelCase__:       List[str]=0.1    ,							UpperCamelCase__:       Dict=512    ,							UpperCamelCase__:       str=2    ,							UpperCamelCase__:       str=0.02    ,							UpperCamelCase__:       Tuple=1e-12    ,							UpperCamelCase__:       Any=True    ,							UpperCamelCase__:       Union[str, Any]=0    ,							UpperCamelCase__:       List[Any]="absolute"    ,							UpperCamelCase__:       Any=None    ,							UpperCamelCase__:       Any=True    ,							UpperCamelCase__:       Optional[int]=True    ,							UpperCamelCase__:       Union[str, Any]=768    ,							UpperCamelCase__:       int=910    ,							UpperCamelCase__:       Tuple=512    ,							UpperCamelCase__:       int=24_858    ,							UpperCamelCase__:       Optional[Any]=True    ,							**UpperCamelCase__:       Optional[Any]    ,							):
														lowerCamelCase__      :			Optional[Any] 						= vocab_size
														lowerCamelCase__      :			Tuple 						= max_position_embeddings
														lowerCamelCase__      :			List[Any] 						= hidden_size
														lowerCamelCase__      :			int 						= num_hidden_layers
														lowerCamelCase__      :			Tuple 						= num_attention_heads
														lowerCamelCase__      :			Any 						= intermediate_size
														lowerCamelCase__      :			List[str] 						= hidden_act
														lowerCamelCase__      :			str 						= hidden_dropout_prob
														lowerCamelCase__      :			Dict 						= attention_probs_dropout_prob
														lowerCamelCase__      :			Union[str, Any] 						= initializer_range
														lowerCamelCase__      :			Tuple 						= type_vocab_size
														lowerCamelCase__      :			Optional[Any] 						= layer_norm_eps
														lowerCamelCase__      :			List[Any] 						= use_cache
														lowerCamelCase__      :			Tuple 						= enable_pronunciation
														lowerCamelCase__      :			Union[str, Any] 						= enable_shape
														lowerCamelCase__      :			Union[str, Any] 						= pronunciation_embed_dim
														lowerCamelCase__      :			Any 						= pronunciation_vocab_size
														lowerCamelCase__      :			int 						= shape_embed_dim
														lowerCamelCase__      :			Tuple 						= shape_vocab_size
														lowerCamelCase__      :			Optional[Any] 						= concat_input
														lowerCamelCase__      :			str 						= position_embedding_type
														lowerCamelCase__      :			Dict 						= classifier_dropout
														super().__init__(pad_token_id=UpperCamelCase__    ,							**UpperCamelCase__  )

