




'''simple docstring'''

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


				def __init__(		self    :					Dict       ,				lowercase_    :					List[str]=None       ,				**lowercase_    :					Union[str, Any]					):
									super().__init__(features=lowercase_					)
									snake_case_									=				torch_tensor_kwargs
									import torch  # noqa import torch at initialization


				def   A_ (		self    :					str       ,				lowercase_    :					str					):
									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    :					Any       ,				lowercase_    :					Tuple					):
									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()

									snake_case_									=				{}

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

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


				def   A_ (		self    :					List[Any]       ,				lowercase_    :					Optional[int]					):
									import torch

									# support for torch, tf, jax etc.
									if hasattr(lowercase_       ,				'''__array__'''					) and not isinstance(lowercase_       ,				torch.Tensor					):
														snake_case_									=				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    :					Optional[Any]       ,				lowercase_    :					dict					):
									return map_nested(self._recursive_tensorize       ,				lowercase_       ,				map_list=lowercase_					)


				def   A_ (		self    :					Dict       ,				lowercase_    :					pa.Table					):
									snake_case_									=				self.numpy_arrow_extractor().extract_row(lowercase_					)
									snake_case_									=				self.python_features_decoder.decode_row(lowercase_					)
									return self.recursive_tensorize(lowercase_					)


				def   A_ (		self    :					List[Any]       ,				lowercase_    :					pa.Table					):
									snake_case_									=				self.numpy_arrow_extractor().extract_column(lowercase_					)
									snake_case_									=				self.python_features_decoder.decode_column(lowercase_       ,				pa_table.column_names[0]					)
									snake_case_									=				self.recursive_tensorize(lowercase_					)
									snake_case_									=				self._consolidate(lowercase_					)
									return column


				def   A_ (		self    :					str       ,				lowercase_    :					pa.Table					):
									snake_case_									=				self.numpy_arrow_extractor().extract_batch(lowercase_					)
									snake_case_									=				self.python_features_decoder.decode_batch(lowercase_					)
									snake_case_									=				self.recursive_tensorize(lowercase_					)
									for column_name in batch:
														snake_case_									=				self._consolidate(batch[column_name]					)
									return batch





from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


UpperCAmelCase__   	=							{
    '''configuration_xlm''': ['''XLM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XLMConfig''', '''XLMOnnxConfig'''],
    '''tokenization_xlm''': ['''XLMTokenizer'''],
}

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase__   	=							[
      '''XLM_PRETRAINED_MODEL_ARCHIVE_LIST''',
      '''XLMForMultipleChoice''',
      '''XLMForQuestionAnswering''',
      '''XLMForQuestionAnsweringSimple''',
      '''XLMForSequenceClassification''',
      '''XLMForTokenClassification''',
      '''XLMModel''',
      '''XLMPreTrainedModel''',
      '''XLMWithLMHeadModel''',
  ]

try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase__   	=							[
      '''TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST''',
      '''TFXLMForMultipleChoice''',
      '''TFXLMForQuestionAnsweringSimple''',
      '''TFXLMForSequenceClassification''',
      '''TFXLMForTokenClassification''',
      '''TFXLMMainLayer''',
      '''TFXLMModel''',
      '''TFXLMPreTrainedModel''',
      '''TFXLMWithLMHeadModel''',
  ]


if TYPE_CHECKING:
  from .configuration_xlm import XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMConfig, XLMOnnxConfig
  from .tokenization_xlm import XLMTokenizer

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_xlm import (
        XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
        XLMForMultipleChoice,
        XLMForQuestionAnswering,
        XLMForQuestionAnsweringSimple,
        XLMForSequenceClassification,
        XLMForTokenClassification,
        XLMModel,
        XLMPreTrainedModel,
        XLMWithLMHeadModel,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_xlm import (
        TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFXLMForMultipleChoice,
        TFXLMForQuestionAnsweringSimple,
        TFXLMForSequenceClassification,
        TFXLMForTokenClassification,
        TFXLMMainLayer,
        TFXLMModel,
        TFXLMPreTrainedModel,
        TFXLMWithLMHeadModel,
    )

else:
  import sys

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

