


'''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 					lowercase__						(		TensorFormatter[Mapping, """tf.Tensor""", Mapping]      ):





							'''simple docstring'''


							def __init__(					self					,      __snake_case=None					,      **__snake_case						):
											super().__init__(features=__snake_case						)
											_SCREAMING_SNAKE_CASE				: List[str]  	=   tf_tensor_kwargs
											import tensorflow as tf  # noqa: import tf at initialization






							def UpperCAmelCase_			(					self					,      __snake_case						):
											import tensorflow as tf

											if isinstance(__snake_case					,      __snake_case						) and column:
															if all(
															    isinstance(__snake_case					,      tf.Tensor						) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column						):
																			return tf.stack(__snake_case						)
															elif all(
															    isinstance(__snake_case					,      (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(__snake_case						)

											return column






							def UpperCAmelCase_			(					self					,      __snake_case						):
											import tensorflow as tf

											if value is None:
															return value

											_SCREAMING_SNAKE_CASE				: Any  	=   {}

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

															if isinstance(__snake_case					,      PIL.Image.Image						):
																			_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   np.asarray(__snake_case						)

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






							def UpperCAmelCase_			(					self					,      __snake_case						):
											import tensorflow as tf

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

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






							def UpperCAmelCase_			(					self					,      __snake_case						):
											return map_nested(self._recursive_tensorize					,      __snake_case					,      map_list=__snake_case						)






							def UpperCAmelCase_			(					self					,      __snake_case						):
											_SCREAMING_SNAKE_CASE				: int  	=   self.numpy_arrow_extractor().extract_row(__snake_case						)
											_SCREAMING_SNAKE_CASE				: List[str]  	=   self.python_features_decoder.decode_row(__snake_case						)
											return self.recursive_tensorize(__snake_case						)






							def UpperCAmelCase_			(					self					,      __snake_case						):
											_SCREAMING_SNAKE_CASE				: int  	=   self.numpy_arrow_extractor().extract_column(__snake_case						)
											_SCREAMING_SNAKE_CASE				: List[Any]  	=   self.python_features_decoder.decode_column(__snake_case					,      pa_table.column_names[0]						)
											_SCREAMING_SNAKE_CASE				: Optional[Any]  	=   self.recursive_tensorize(__snake_case						)
											_SCREAMING_SNAKE_CASE				: Any  	=   self._consolidate(__snake_case						)
											return column






							def UpperCAmelCase_			(					self					,      __snake_case						):
											_SCREAMING_SNAKE_CASE				: List[str]  	=   self.numpy_arrow_extractor().extract_batch(__snake_case						)
											_SCREAMING_SNAKE_CASE				: Optional[int]  	=   self.python_features_decoder.decode_batch(__snake_case						)
											_SCREAMING_SNAKE_CASE				: str  	=   self.recursive_tensorize(__snake_case						)
											for column_name in batch:
															_SCREAMING_SNAKE_CASE				: List[str]  	=   self._consolidate(batch[column_name]						)
											return batch








'''simple docstring'''


from typing import TYPE_CHECKING

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


UpperCAmelCase_	:     Union[str, Any]							     =	{'configuration_pegasus': ['PEGASUS_PRETRAINED_CONFIG_ARCHIVE_MAP', 'PegasusConfig']}

try:
				if not is_sentencepiece_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				UpperCAmelCase_	:     Dict							     =	['PegasusTokenizer']

try:
				if not is_tokenizers_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				UpperCAmelCase_	:     Optional[int]							     =	['PegasusTokenizerFast']

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				UpperCAmelCase_	:     Optional[int]							     =	[
				    'PEGASUS_PRETRAINED_MODEL_ARCHIVE_LIST',
				    'PegasusForCausalLM',
				    'PegasusForConditionalGeneration',
				    'PegasusModel',
				    'PegasusPreTrainedModel',
				]

try:
				if not is_tf_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				UpperCAmelCase_	:     Optional[Any]							     =	[
				    'TFPegasusForConditionalGeneration',
				    'TFPegasusModel',
				    'TFPegasusPreTrainedModel',
				]

try:
				if not is_flax_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				UpperCAmelCase_	:     Any							     =	[
				    'FlaxPegasusForConditionalGeneration',
				    'FlaxPegasusModel',
				    'FlaxPegasusPreTrainedModel',
				]


if TYPE_CHECKING:
				from .configuration_pegasus import PEGASUS_PRETRAINED_CONFIG_ARCHIVE_MAP, PegasusConfig

				try:
								if not is_sentencepiece_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .tokenization_pegasus import PegasusTokenizer

				try:
								if not is_tokenizers_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .tokenization_pegasus_fast import PegasusTokenizerFast

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_pegasus import (
								    PEGASUS_PRETRAINED_MODEL_ARCHIVE_LIST,
								    PegasusForCausalLM,
								    PegasusForConditionalGeneration,
								    PegasusModel,
								    PegasusPreTrainedModel,
								)

				try:
								if not is_tf_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_tf_pegasus import TFPegasusForConditionalGeneration, TFPegasusModel, TFPegasusPreTrainedModel

				try:
								if not is_flax_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_flax_pegasus import (
								    FlaxPegasusForConditionalGeneration,
								    FlaxPegasusModel,
								    FlaxPegasusPreTrainedModel,
								)

else:
				import sys

				UpperCAmelCase_	:     List[str]							     =	_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)




