





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

       def __init__(		self:			Any       , _SCREAMING_SNAKE_CASE:			str=None       , **_SCREAMING_SNAKE_CASE:			List[Any]		)							->			int:




              """simple docstring"""


              super().__init__(features=_SCREAMING_SNAKE_CASE		)
              UpperCamelCase_ 							=	tf_tensor_kwargs
              import tensorflow as tf  # noqa: import tf at initialization

       def lowercase			(		self:			List[Any]       , _SCREAMING_SNAKE_CASE:			int		)							->			str:




              """simple docstring"""


              import tensorflow as tf

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

              return column

       def lowercase			(		self:			Any       , _SCREAMING_SNAKE_CASE:			Tuple		)							->			Any:




              """simple docstring"""


              import tensorflow as tf

              if value is None:
                     return value

              UpperCamelCase_ 							=	{}

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

                     if isinstance(_SCREAMING_SNAKE_CASE       , PIL.Image.Image		):
                            UpperCamelCase_ 							=	np.asarray(_SCREAMING_SNAKE_CASE		)

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

       def lowercase			(		self:			Union[str, Any]       , _SCREAMING_SNAKE_CASE:			Any		)							->			Optional[int]:




              """simple docstring"""


              import tensorflow as tf

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

                     if isinstance(_SCREAMING_SNAKE_CASE       , torch.Tensor		):
                            return self._tensorize(data_struct.detach().cpu().numpy()[()]		)
              if hasattr(_SCREAMING_SNAKE_CASE       , "__array__"		) and not isinstance(_SCREAMING_SNAKE_CASE       , tf.Tensor		):
                     UpperCamelCase_ 							=	data_struct.__array__()
              # support for nested types like struct of list of struct
              if isinstance(_SCREAMING_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(_SCREAMING_SNAKE_CASE		) for substruct in data_struct]		)
              elif isinstance(_SCREAMING_SNAKE_CASE       , (list, tuple)		):
                     return self._consolidate([self.recursive_tensorize(_SCREAMING_SNAKE_CASE		) for substruct in data_struct]		)
              return self._tensorize(_SCREAMING_SNAKE_CASE		)

       def lowercase			(		self:			Any       , _SCREAMING_SNAKE_CASE:			dict		)							->			Tuple:




              """simple docstring"""


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

       def lowercase			(		self:			Optional[Any]       , _SCREAMING_SNAKE_CASE:			pa.Table		)							->			Mapping:




              """simple docstring"""


              UpperCamelCase_ 							=	self.numpy_arrow_extractor().extract_row(_SCREAMING_SNAKE_CASE		)
              UpperCamelCase_ 							=	self.python_features_decoder.decode_row(_SCREAMING_SNAKE_CASE		)
              return self.recursive_tensorize(_SCREAMING_SNAKE_CASE		)

       def lowercase			(		self:			Optional[Any]       , _SCREAMING_SNAKE_CASE:			pa.Table		)							->			"tf.Tensor":




              """simple docstring"""


              UpperCamelCase_ 							=	self.numpy_arrow_extractor().extract_column(_SCREAMING_SNAKE_CASE		)
              UpperCamelCase_ 							=	self.python_features_decoder.decode_column(_SCREAMING_SNAKE_CASE       , pa_table.column_names[0]		)
              UpperCamelCase_ 							=	self.recursive_tensorize(_SCREAMING_SNAKE_CASE		)
              UpperCamelCase_ 							=	self._consolidate(_SCREAMING_SNAKE_CASE		)
              return column



       def lowercase			(		self:			List[Any]       , _SCREAMING_SNAKE_CASE:			pa.Table		)							->			Mapping:




              """simple docstring"""


              UpperCamelCase_ 							=	self.numpy_arrow_extractor().extract_batch(_SCREAMING_SNAKE_CASE		)
              UpperCamelCase_ 							=	self.python_features_decoder.decode_batch(_SCREAMING_SNAKE_CASE		)
              UpperCamelCase_ 							=	self.recursive_tensorize(_SCREAMING_SNAKE_CASE		)
              for column_name in batch:
                     UpperCamelCase_ 							=	self._consolidate(batch[column_name]		)
              return batch





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,
)


__SCREAMING_SNAKE_CASE	:							Any           =  {'''configuration_pegasus''': ['''PEGASUS_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''PegasusConfig''']}

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

try:
		if not is_tokenizers_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__SCREAMING_SNAKE_CASE	:							Tuple           =  ['''PegasusTokenizerFast''']

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

try:
		if not is_tf_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__SCREAMING_SNAKE_CASE	:							List[str]           =  [
		    '''TFPegasusForConditionalGeneration''',
		    '''TFPegasusModel''',
		    '''TFPegasusPreTrainedModel''',
		]

try:
		if not is_flax_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__SCREAMING_SNAKE_CASE	:							List[str]           =  [
		    '''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

		__SCREAMING_SNAKE_CASE	:							Dict           =  _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)

