



'''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 tensorflow as tf


class 	lowerCamelCase__ (		TensorFormatter[Mapping, '''tf.Tensor''', Mapping]   ):


						def __init__( self						:  Optional[int]      ,       _a						:  Optional[Any]=None      ,       **_a						:  Optional[Any]			):
													super().__init__(features=_a			)
													a__:       Optional[int]											=tf_tensor_kwargs
													import tensorflow as tf  # noqa: import tf at initialization


						def   _lowerCamelCase			( self						:  List[str]      ,       _a						:  Union[str, Any]			):
													import tensorflow as tf

													if isinstance(_a      ,       _a			) and column:
																				if all(
																				    isinstance(_a      ,       tf.Tensor			) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column			):
																											return tf.stack(_a			)
																				elif all(
																				    isinstance(_a      ,       (tf.Tensor, tf.RaggedTensor)			) and x.ndim == 1 and x.dtype == column[0].dtype
																				    for x in column			):
																											# only rag 1-D tensors, otherwise some dimensions become ragged even though they were consolidated
																											return tf.ragged.stack(_a			)

													return column


						def   _lowerCamelCase			( self						:  Dict      ,       _a						:  Dict			):
													import tensorflow as tf

													if value is None:
																				return value

													a__:       Optional[Any]											={}

													if isinstance(_a      ,       (np.number, np.ndarray)			) and np.issubdtype(value.dtype      ,       np.integer			):
																				a__:       Any											={"dtype": tf.intaa}
													elif isinstance(_a      ,       (np.number, np.ndarray)			) and np.issubdtype(value.dtype      ,       np.floating			):
																				a__:       List[str]											={"dtype": tf.floataa}
													elif config.PIL_AVAILABLE and "PIL" in sys.modules:
																				import PIL.Image

																				if isinstance(_a      ,       PIL.Image.Image			):
																											a__:       Optional[int]											=np.asarray(_a			)

													return tf.convert_to_tensor(_a      ,       **{**default_dtype, **self.tf_tensor_kwargs}			)


						def   _lowerCamelCase			( self						:  Optional[int]      ,       _a						:  List[Any]			):
													import tensorflow as tf

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

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


						def   _lowerCamelCase			( self						:  Optional[Any]      ,       _a						:  dict			):
													return map_nested(self._recursive_tensorize      ,       _a      ,       map_list=_a			)


						def   _lowerCamelCase			( self						:  Any      ,       _a						:  pa.Table			):
													a__:       int											=self.numpy_arrow_extractor().extract_row(_a			)
													a__:       Optional[int]											=self.python_features_decoder.decode_row(_a			)
													return self.recursive_tensorize(_a			)


						def   _lowerCamelCase			( self						:  Union[str, Any]      ,       _a						:  pa.Table			):
													a__:       Tuple											=self.numpy_arrow_extractor().extract_column(_a			)
													a__:       str											=self.python_features_decoder.decode_column(_a      ,       pa_table.column_names[0]			)
													a__:       Tuple											=self.recursive_tensorize(_a			)
													a__:       Optional[int]											=self._consolidate(_a			)
													return column







						def   _lowerCamelCase			( self						:  Dict      ,       _a						:  pa.Table			):
													a__:       Dict											=self.numpy_arrow_extractor().extract_batch(_a			)
													a__:       List[str]											=self.python_features_decoder.decode_batch(_a			)
													a__:       Optional[Any]											=self.recursive_tensorize(_a			)
													for column_name in batch:
																				a__:       List[str]											=self._consolidate(batch[column_name]			)
													return batch










from typing import TYPE_CHECKING

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


__UpperCAmelCase						  =     {
    '''configuration_splinter''': ['''SPLINTER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''SplinterConfig'''],
    '''tokenization_splinter''': ['''SplinterTokenizer'''],
}

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

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__UpperCAmelCase						  =     [
		    '''SPLINTER_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''SplinterForQuestionAnswering''',
		    '''SplinterForPreTraining''',
		    '''SplinterLayer''',
		    '''SplinterModel''',
		    '''SplinterPreTrainedModel''',
		]


if TYPE_CHECKING:
		from .configuration_splinter import SPLINTER_PRETRAINED_CONFIG_ARCHIVE_MAP, SplinterConfig
		from .tokenization_splinter import SplinterTokenizer

		try:
				if not is_tokenizers_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .tokenization_splinter_fast import SplinterTokenizerFast

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_splinter import (
				    SPLINTER_PRETRAINED_MODEL_ARCHIVE_LIST,
				    SplinterForPreTraining,
				    SplinterForQuestionAnswering,
				    SplinterLayer,
				    SplinterModel,
				    SplinterPreTrainedModel,
				)


else:
		import sys

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





