



# Lint as: python3
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   snake_case_ (TensorFormatter[Mapping, """torch.Tensor""", Mapping]  ):




       """simple docstring"""






       def __init__(     self     ,lowercase=None     ,**lowercase):

             """simple docstring"""





             super().__init__(features=lowercase)
             UpperCAmelCase_  :	Union[str, Any]				      =		torch_tensor_kwargs
             import torch  # noqa import torch at initialization



       def  A_   (     self     ,lowercase):

             """simple docstring"""





             import torch

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



       def  A_   (     self     ,lowercase):

             """simple docstring"""





             import torch

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

             UpperCAmelCase_  :	List[str]				      =		{}

             if isinstance(lowercase     ,(np.number, np.ndarray)) and np.issubdtype(value.dtype     ,np.integer):
                   UpperCAmelCase_  :	Dict				      =		{"dtype": torch.intaa}
             elif isinstance(lowercase     ,(np.number, np.ndarray)) and np.issubdtype(value.dtype     ,np.floating):
                   UpperCAmelCase_  :	Optional[Any]				      =		{"dtype": torch.floataa}
             elif config.PIL_AVAILABLE and "PIL" in sys.modules:
                   import PIL.Image

                   if isinstance(lowercase     ,PIL.Image.Image):
                         UpperCAmelCase_  :	List[str]				      =		np.asarray(lowercase)
             return torch.tensor(lowercase     ,**{**default_dtype, **self.torch_tensor_kwargs})



       def  A_   (     self     ,lowercase):

             """simple docstring"""





             import torch

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



       def  A_   (     self     ,lowercase):

             """simple docstring"""





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



       def  A_   (     self     ,lowercase):

             """simple docstring"""





             UpperCAmelCase_  :	List[Any]				      =		self.numpy_arrow_extractor().extract_row(lowercase)
             UpperCAmelCase_  :	Tuple				      =		self.python_features_decoder.decode_row(lowercase)
             return self.recursive_tensorize(lowercase)



       def  A_   (     self     ,lowercase):

             """simple docstring"""





             UpperCAmelCase_  :	str				      =		self.numpy_arrow_extractor().extract_column(lowercase)
             UpperCAmelCase_  :	List[Any]				      =		self.python_features_decoder.decode_column(lowercase     ,pa_table.column_names[0])
             UpperCAmelCase_  :	List[Any]				      =		self.recursive_tensorize(lowercase)
             UpperCAmelCase_  :	List[str]				      =		self._consolidate(lowercase)
             return column



       def  A_   (     self     ,lowercase):

             """simple docstring"""





             UpperCAmelCase_  :	Any				      =		self.numpy_arrow_extractor().extract_batch(lowercase)
             UpperCAmelCase_  :	Optional[int]				      =		self.python_features_decoder.decode_batch(lowercase)
             UpperCAmelCase_  :	List[Any]				      =		self.recursive_tensorize(lowercase)
             for column_name in batch:
                   UpperCAmelCase_  :	Optional[int]				      =		self._consolidate(batch[column_name])
             return batch





from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


__lowerCamelCase         =			{
    '''configuration_roformer''': ['''ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''RoFormerConfig''', '''RoFormerOnnxConfig'''],
    '''tokenization_roformer''': ['''RoFormerTokenizer'''],
}

try:
   if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   __lowerCamelCase         =			['''RoFormerTokenizerFast''']

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   __lowerCamelCase         =			[
       '''ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''',
       '''RoFormerForCausalLM''',
       '''RoFormerForMaskedLM''',
       '''RoFormerForMultipleChoice''',
       '''RoFormerForQuestionAnswering''',
       '''RoFormerForSequenceClassification''',
       '''RoFormerForTokenClassification''',
       '''RoFormerLayer''',
       '''RoFormerModel''',
       '''RoFormerPreTrainedModel''',
       '''load_tf_weights_in_roformer''',
   ]


try:
   if not is_tf_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   __lowerCamelCase         =			[
       '''TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''',
       '''TFRoFormerForCausalLM''',
       '''TFRoFormerForMaskedLM''',
       '''TFRoFormerForMultipleChoice''',
       '''TFRoFormerForQuestionAnswering''',
       '''TFRoFormerForSequenceClassification''',
       '''TFRoFormerForTokenClassification''',
       '''TFRoFormerLayer''',
       '''TFRoFormerModel''',
       '''TFRoFormerPreTrainedModel''',
   ]


try:
   if not is_flax_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   __lowerCamelCase         =			[
       '''FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''',
       '''FlaxRoFormerForMaskedLM''',
       '''FlaxRoFormerForMultipleChoice''',
       '''FlaxRoFormerForQuestionAnswering''',
       '''FlaxRoFormerForSequenceClassification''',
       '''FlaxRoFormerForTokenClassification''',
       '''FlaxRoFormerModel''',
       '''FlaxRoFormerPreTrainedModel''',
   ]


if TYPE_CHECKING:
   from .configuration_roformer import ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, RoFormerConfig, RoFormerOnnxConfig
   from .tokenization_roformer import RoFormerTokenizer

   try:
      if not is_tokenizers_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .tokenization_roformer_fast import RoFormerTokenizerFast

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_roformer import (
          ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
          RoFormerForCausalLM,
          RoFormerForMaskedLM,
          RoFormerForMultipleChoice,
          RoFormerForQuestionAnswering,
          RoFormerForSequenceClassification,
          RoFormerForTokenClassification,
          RoFormerLayer,
          RoFormerModel,
          RoFormerPreTrainedModel,
          load_tf_weights_in_roformer,
      )

   try:
      if not is_tf_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_tf_roformer import (
          TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
          TFRoFormerForCausalLM,
          TFRoFormerForMaskedLM,
          TFRoFormerForMultipleChoice,
          TFRoFormerForQuestionAnswering,
          TFRoFormerForSequenceClassification,
          TFRoFormerForTokenClassification,
          TFRoFormerLayer,
          TFRoFormerModel,
          TFRoFormerPreTrainedModel,
      )

   try:
      if not is_flax_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_flax_roformer import (
          FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
          FlaxRoFormerForMaskedLM,
          FlaxRoFormerForMultipleChoice,
          FlaxRoFormerForQuestionAnswering,
          FlaxRoFormerForSequenceClassification,
          FlaxRoFormerForTokenClassification,
          FlaxRoFormerModel,
          FlaxRoFormerPreTrainedModel,
      )


else:
   import sys

   __lowerCamelCase         =			_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
