

# 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 						__snake_case						(							TensorFormatter[Mapping, "tf.Tensor", Mapping]     ):
       """simple docstring"""







       def __init__(		self			,   _UpperCamelCase=None			,   **_UpperCamelCase      )			->       Union[str, Any]:






              """simple docstring"""


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


       def  a		(		self			,   _UpperCamelCase      )			->       Union[str, Any]:






              """simple docstring"""


              import tensorflow as tf

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

              return column


       def  a		(		self			,   _UpperCamelCase      )			->       List[str]:






              """simple docstring"""


              import tensorflow as tf

              if value is None:
                     return value

              __snake_case					  =       {}

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

                     if isinstance(_UpperCamelCase			,   PIL.Image.Image      ):
                            __snake_case					  =       np.asarray(_UpperCamelCase      )

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


       def  a		(		self			,   _UpperCamelCase      )			->       Optional[Any]:






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


       def  a		(		self			,   _UpperCamelCase      )			->       Dict:






              """simple docstring"""


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


       def  a		(		self			,   _UpperCamelCase      )			->       Mapping:






              """simple docstring"""


              __snake_case					  =       self.numpy_arrow_extractor().extract_row(_UpperCamelCase      )
              __snake_case					  =       self.python_features_decoder.decode_row(_UpperCamelCase      )
              return self.recursive_tensorize(_UpperCamelCase      )


       def  a		(		self			,   _UpperCamelCase      )			->       "tf.Tensor":






              """simple docstring"""


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






       def  a		(		self			,   _UpperCamelCase      )			->       Mapping:






              """simple docstring"""


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




import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCamelCase__		   =    logging.get_logger(__name__)

UpperCamelCase__		   =    {
    '''facebook/hubert-base-ls960''': '''https://huggingface.co/facebook/hubert-base-ls960/resolve/main/config.json''',
    # See all Hubert models at https://huggingface.co/models?filter=hubert
}

class 						__snake_case						(							snake_case__     ):
       """simple docstring"""







       __SCREAMING_SNAKE_CASE													=       "hubert"


       def __init__(		self			,   _UpperCamelCase=32			,   _UpperCamelCase=7_68			,   _UpperCamelCase=12			,   _UpperCamelCase=12			,   _UpperCamelCase=30_72			,   _UpperCamelCase="gelu"			,   _UpperCamelCase=0.1			,   _UpperCamelCase=0.1			,   _UpperCamelCase=0.1			,   _UpperCamelCase=True			,   _UpperCamelCase=0.0			,   _UpperCamelCase=0.1			,   _UpperCamelCase=0.1			,   _UpperCamelCase=0.02			,   _UpperCamelCase=1E-5			,   _UpperCamelCase="group"			,   _UpperCamelCase="gelu"			,   _UpperCamelCase=(5_12, 5_12, 5_12, 5_12, 5_12, 5_12, 5_12)			,   _UpperCamelCase=(5, 2, 2, 2, 2, 2, 2)			,   _UpperCamelCase=(10, 3, 3, 3, 3, 2, 2)			,   _UpperCamelCase=False			,   _UpperCamelCase=1_28			,   _UpperCamelCase=16			,   _UpperCamelCase=False			,   _UpperCamelCase=True			,   _UpperCamelCase=0.05			,   _UpperCamelCase=10			,   _UpperCamelCase=2			,   _UpperCamelCase=0.0			,   _UpperCamelCase=10			,   _UpperCamelCase=0			,   _UpperCamelCase="sum"			,   _UpperCamelCase=False			,   _UpperCamelCase=False			,   _UpperCamelCase=2_56			,   _UpperCamelCase=0			,   _UpperCamelCase=1			,   _UpperCamelCase=2			,   **_UpperCamelCase			,   )			->       Any:






              """simple docstring"""


              super().__init__(**_UpperCamelCase			,   pad_token_id=_UpperCamelCase			,   bos_token_id=_UpperCamelCase			,   eos_token_id=_UpperCamelCase      )
              __snake_case					  =       hidden_size
              __snake_case					  =       feat_extract_norm
              __snake_case					  =       feat_extract_activation
              __snake_case					  =       list(_UpperCamelCase      )
              __snake_case					  =       list(_UpperCamelCase      )
              __snake_case					  =       list(_UpperCamelCase      )
              __snake_case					  =       conv_bias
              __snake_case					  =       num_conv_pos_embeddings
              __snake_case					  =       num_conv_pos_embedding_groups
              __snake_case					  =       len(self.conv_dim      )
              __snake_case					  =       num_hidden_layers
              __snake_case					  =       intermediate_size
              __snake_case					  =       hidden_act
              __snake_case					  =       num_attention_heads
              __snake_case					  =       hidden_dropout
              __snake_case					  =       attention_dropout
              __snake_case					  =       activation_dropout
              __snake_case					  =       feat_proj_layer_norm
              __snake_case					  =       feat_proj_dropout
              __snake_case					  =       final_dropout
              __snake_case					  =       layerdrop
              __snake_case					  =       layer_norm_eps
              __snake_case					  =       initializer_range
              __snake_case					  =       vocab_size
              __snake_case					  =       do_stable_layer_norm
              __snake_case					  =       use_weighted_layer_sum
              __snake_case					  =       classifier_proj_size

              if (
                  (len(self.conv_stride      ) != self.num_feat_extract_layers)
                  or (len(self.conv_kernel      ) != self.num_feat_extract_layers)
                  or (len(self.conv_dim      ) != self.num_feat_extract_layers)
              ):
                     raise ValueError(
                         """Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =="""
                         """ `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ="""
                         F' {len(self.conv_dim      )}`, `len(config.conv_stride) = {len(self.conv_stride      )}`,'
                         F' `len(config.conv_kernel) = {len(self.conv_kernel      )}`.'      )

              # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
              __snake_case					  =       apply_spec_augment
              __snake_case					  =       mask_time_prob
              __snake_case					  =       mask_time_length
              __snake_case					  =       mask_time_min_masks
              __snake_case					  =       mask_feature_prob
              __snake_case					  =       mask_feature_length
              __snake_case					  =       mask_feature_min_masks

              # ctc loss
              __snake_case					  =       ctc_loss_reduction
              __snake_case					  =       ctc_zero_infinity






       @property
       def  a		(		self      )			->       Union[str, Any]:






              """simple docstring"""


              return functools.reduce(operator.mul			,   self.conv_stride			,   1      )

