
"""simple docstring"""




import collections
import gzip
import os
import urllib

import numpy
from tensorflow.python.framework import dtypes, random_seed
from tensorflow.python.platform import gfile
from tensorflow.python.util.deprecation import deprecated

UpperCAmelCase__   :					int												=				collections.namedtuple('_Datasets', ['train', 'validation', 'test'])

# CVDF mirror of http://yann.lecun.com/exdb/mnist/
UpperCAmelCase__   :					Optional[int]												=				'https://storage.googleapis.com/cvdf-datasets/mnist/'

def lowercase_						(     _snake_case		):
       SCREAMING_SNAKE_CASE__		: List[str]     =     numpy.dtype(numpy.uintaa		).newbyteorder(""">"""		)
       return numpy.frombuffer(bytestream.read(4		)     ,dtype=__UpperCAmelCase		)[0]

@deprecated(__UpperCAmelCase     ,"""Please use tf.data to implement this functionality."""		)
def lowercase_						(     _snake_case		):
       print("""Extracting"""     ,f.name		)
       with gzip.GzipFile(fileobj=__UpperCAmelCase		) as bytestream:
              SCREAMING_SNAKE_CASE__		: Any     =     _readaa(__UpperCAmelCase		)
              if magic != 2_051:
                     raise ValueError(
                         """Invalid magic number %d in MNIST image file: %s""" % (magic, f.name)		)
              SCREAMING_SNAKE_CASE__		: List[Any]     =     _readaa(__UpperCAmelCase		)
              SCREAMING_SNAKE_CASE__		: Optional[Any]     =     _readaa(__UpperCAmelCase		)
              SCREAMING_SNAKE_CASE__		: str     =     _readaa(__UpperCAmelCase		)
              SCREAMING_SNAKE_CASE__		: Dict     =     bytestream.read(rows * cols * num_images		)
              SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     numpy.frombuffer(__UpperCAmelCase     ,dtype=numpy.uinta		)
              SCREAMING_SNAKE_CASE__		: Optional[Any]     =     data.reshape(__UpperCAmelCase     ,__UpperCAmelCase     ,__UpperCAmelCase     ,1		)
              return data

@deprecated(__UpperCAmelCase     ,"""Please use tf.one_hot on tensors."""		)
def lowercase_						(     _snake_case     ,_snake_case		):
       SCREAMING_SNAKE_CASE__		: Tuple     =     labels_dense.shape[0]
       SCREAMING_SNAKE_CASE__		: int     =     numpy.arange(__UpperCAmelCase		) * num_classes
       SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     numpy.zeros((num_labels, num_classes)		)
       SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     1
       return labels_one_hot

@deprecated(__UpperCAmelCase     ,"""Please use tf.data to implement this functionality."""		)
def lowercase_						(     _snake_case     ,_snake_case=False     ,_snake_case=10		):
       print("""Extracting"""     ,f.name		)
       with gzip.GzipFile(fileobj=__UpperCAmelCase		) as bytestream:
              SCREAMING_SNAKE_CASE__		: Any     =     _readaa(__UpperCAmelCase		)
              if magic != 2_049:
                     raise ValueError(
                         """Invalid magic number %d in MNIST label file: %s""" % (magic, f.name)		)
              SCREAMING_SNAKE_CASE__		: Any     =     _readaa(__UpperCAmelCase		)
              SCREAMING_SNAKE_CASE__		: List[str]     =     bytestream.read(__UpperCAmelCase		)
              SCREAMING_SNAKE_CASE__		: Optional[int]     =     numpy.frombuffer(__UpperCAmelCase     ,dtype=numpy.uinta		)
              if one_hot:
                     return _dense_to_one_hot(__UpperCAmelCase     ,__UpperCAmelCase		)
              return labels



class 			lowerCAmelCase_		:


    """simple docstring"""



    @deprecated(
        SCREAMING_SNAKE_CASE__     , """Please use alternatives such as official/mnist/_DataSet.py"""
        """ from tensorflow/models."""     , )
    def __init__(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__=False     , SCREAMING_SNAKE_CASE__=False     , SCREAMING_SNAKE_CASE__=dtypes.floataa     , SCREAMING_SNAKE_CASE__=True     , SCREAMING_SNAKE_CASE__=None     , )      ->	List[str]:



           """simple docstring"""




           SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__		: Dict     =     random_seed.get_seed(SCREAMING_SNAKE_CASE__					)
           # If op level seed is not set, use whatever graph level seed is returned
           numpy.random.seed(seeda if seed is None else seeda					)
           SCREAMING_SNAKE_CASE__		: List[str]     =     dtypes.as_dtype(SCREAMING_SNAKE_CASE__					).base_dtype
           if dtype not in (dtypes.uinta, dtypes.floataa):
                  raise TypeError("""Invalid image dtype %r, expected uint8 or float32""" % dtype					)
           if fake_data:
                  SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     1_00_00
                  SCREAMING_SNAKE_CASE__		: Optional[Any]     =     one_hot
           else:
                  assert (
                      images.shape[0] == labels.shape[0]
                  ), F'''images.shape: {images.shape} labels.shape: {labels.shape}'''
                  SCREAMING_SNAKE_CASE__		: List[str]     =     images.shape[0]

                  # Convert shape from [num examples, rows, columns, depth]
                  # to [num examples, rows*columns] (assuming depth == 1)
                  if reshape:
                         assert images.shape[3] == 1
                         SCREAMING_SNAKE_CASE__		: Optional[Any]     =     images.reshape(
                             images.shape[0]     , images.shape[1] * images.shape[2]					)
                  if dtype == dtypes.floataa:
                         # Convert from [0, 255] -> [0.0, 1.0].
                         SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     images.astype(numpy.floataa					)
                         SCREAMING_SNAKE_CASE__		: Optional[Any]     =     numpy.multiply(SCREAMING_SNAKE_CASE__     , 1.0 / 255.0					)
           SCREAMING_SNAKE_CASE__		: List[Any]     =     images
           SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     labels
           SCREAMING_SNAKE_CASE__		: Tuple     =     0
           SCREAMING_SNAKE_CASE__		: Optional[Any]     =     0



    @property
    def 			__magic_name__					(self					)      ->	List[str]:



           """simple docstring"""




           return self._images



    @property
    def 			__magic_name__					(self					)      ->	Any:



           """simple docstring"""




           return self._labels



    @property
    def 			__magic_name__					(self					)      ->	Dict:



           """simple docstring"""




           return self._num_examples



    @property
    def 			__magic_name__					(self					)      ->	Dict:



           """simple docstring"""




           return self._epochs_completed



    def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__=False     , SCREAMING_SNAKE_CASE__=True					)      ->	Dict:



           """simple docstring"""




           if fake_data:
                  SCREAMING_SNAKE_CASE__		: int     =     [1] * 7_84
                  SCREAMING_SNAKE_CASE__		: Dict     =     [1] + [0] * 9 if self.one_hot else 0
                  return (
                      [fake_image for _ in range(SCREAMING_SNAKE_CASE__					)],
                      [fake_label for _ in range(SCREAMING_SNAKE_CASE__					)],
                  )
           SCREAMING_SNAKE_CASE__		: str     =     self._index_in_epoch
           # Shuffle for the first epoch
           if self._epochs_completed == 0 and start == 0 and shuffle:
                  SCREAMING_SNAKE_CASE__		: Tuple     =     numpy.arange(self._num_examples					)
                  numpy.random.shuffle(SCREAMING_SNAKE_CASE__					)
                  SCREAMING_SNAKE_CASE__		: Optional[Any]     =     self.images[perma]
                  SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     self.labels[perma]
           # Go to the next epoch
           if start + batch_size > self._num_examples:
                  # Finished epoch
                  self._epochs_completed += 1
                  # Get the rest examples in this epoch
                  SCREAMING_SNAKE_CASE__		: Optional[Any]     =     self._num_examples - start
                  SCREAMING_SNAKE_CASE__		: str     =     self._images[start : self._num_examples]
                  SCREAMING_SNAKE_CASE__		: Tuple     =     self._labels[start : self._num_examples]
                  # Shuffle the data
                  if shuffle:
                         SCREAMING_SNAKE_CASE__		: List[str]     =     numpy.arange(self._num_examples					)
                         numpy.random.shuffle(SCREAMING_SNAKE_CASE__					)
                         SCREAMING_SNAKE_CASE__		: str     =     self.images[perm]
                         SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     self.labels[perm]
                  # Start next epoch
                  SCREAMING_SNAKE_CASE__		: Tuple     =     0
                  SCREAMING_SNAKE_CASE__		: Optional[int]     =     batch_size - rest_num_examples
                  SCREAMING_SNAKE_CASE__		: str     =     self._index_in_epoch
                  SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     self._images[start:end]
                  SCREAMING_SNAKE_CASE__		: Dict     =     self._labels[start:end]
                  return (
                      numpy.concatenate((images_rest_part, images_new_part)     , axis=0					),
                      numpy.concatenate((labels_rest_part, labels_new_part)     , axis=0					),
                  )
           else:
                  self._index_in_epoch += batch_size
                  SCREAMING_SNAKE_CASE__		: List[str]     =     self._index_in_epoch
                  return self._images[start:end], self._labels[start:end]






@deprecated(__UpperCAmelCase     ,"""Please write your own downloading logic."""		)
def lowercase_						(     _snake_case     ,_snake_case     ,_snake_case		):







       if not gfile.Exists(__UpperCAmelCase		):
              gfile.MakeDirs(__UpperCAmelCase		)
       SCREAMING_SNAKE_CASE__		: Optional[Any]     =     os.path.join(__UpperCAmelCase     ,__UpperCAmelCase		)
       if not gfile.Exists(__UpperCAmelCase		):
              urllib.request.urlretrieve(__UpperCAmelCase     ,__UpperCAmelCase		)  # noqa: S310
              with gfile.GFile(__UpperCAmelCase		) as f:
                     SCREAMING_SNAKE_CASE__		: List[str]     =     f.size()
              print("""Successfully downloaded"""     ,__UpperCAmelCase     ,__UpperCAmelCase     ,"""bytes."""		)
       return filepath

@deprecated(
    __UpperCAmelCase     ,"""Please use alternatives such as:""" """ tensorflow_datasets.load('mnist')"""		)
def lowercase_						(     _snake_case     ,_snake_case=False     ,_snake_case=False     ,_snake_case=dtypes.floataa     ,_snake_case=True     ,_snake_case=5_000     ,_snake_case=None     ,_snake_case=DEFAULT_SOURCE_URL     ,):







       if fake_data:

              def fake():
                     return _DataSet(
                         []     ,[]     ,fake_data=__UpperCAmelCase     ,one_hot=__UpperCAmelCase     ,dtype=__UpperCAmelCase     ,seed=__UpperCAmelCase		)

              SCREAMING_SNAKE_CASE__		: Tuple     =     fake()
              SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     fake()
              SCREAMING_SNAKE_CASE__		: Optional[int]     =     fake()
              return _Datasets(train=__UpperCAmelCase     ,validation=__UpperCAmelCase     ,test=__UpperCAmelCase		)

       if not source_url:  # empty string check
              SCREAMING_SNAKE_CASE__		: Optional[int]     =     DEFAULT_SOURCE_URL

       SCREAMING_SNAKE_CASE__		: List[Any]     =     """train-images-idx3-ubyte.gz"""
       SCREAMING_SNAKE_CASE__		: Any     =     """train-labels-idx1-ubyte.gz"""
       SCREAMING_SNAKE_CASE__		: str     =     """t10k-images-idx3-ubyte.gz"""
       SCREAMING_SNAKE_CASE__		: List[Any]     =     """t10k-labels-idx1-ubyte.gz"""

       SCREAMING_SNAKE_CASE__		: List[Any]     =     _maybe_download(
           __UpperCAmelCase     ,__UpperCAmelCase     ,source_url + train_images_file		)
       with gfile.Open(__UpperCAmelCase     ,"""rb"""		) as f:
              SCREAMING_SNAKE_CASE__		: int     =     _extract_images(__UpperCAmelCase		)

       SCREAMING_SNAKE_CASE__		: str     =     _maybe_download(
           __UpperCAmelCase     ,__UpperCAmelCase     ,source_url + train_labels_file		)
       with gfile.Open(__UpperCAmelCase     ,"""rb"""		) as f:
              SCREAMING_SNAKE_CASE__		: str     =     _extract_labels(__UpperCAmelCase     ,one_hot=__UpperCAmelCase		)

       SCREAMING_SNAKE_CASE__		: Dict     =     _maybe_download(
           __UpperCAmelCase     ,__UpperCAmelCase     ,source_url + test_images_file		)
       with gfile.Open(__UpperCAmelCase     ,"""rb"""		) as f:
              SCREAMING_SNAKE_CASE__		: List[Any]     =     _extract_images(__UpperCAmelCase		)

       SCREAMING_SNAKE_CASE__		: List[str]     =     _maybe_download(
           __UpperCAmelCase     ,__UpperCAmelCase     ,source_url + test_labels_file		)
       with gfile.Open(__UpperCAmelCase     ,"""rb"""		) as f:
              SCREAMING_SNAKE_CASE__		: Dict     =     _extract_labels(__UpperCAmelCase     ,one_hot=__UpperCAmelCase		)

       if not 0 <= validation_size <= len(__UpperCAmelCase		):
              SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     (
                  """Validation size should be between 0 and """
                  f'''{len(__UpperCAmelCase		)}. Received: {validation_size}.'''
              )
              raise ValueError(__UpperCAmelCase		)

       SCREAMING_SNAKE_CASE__		: Optional[int]     =     train_images[:validation_size]
       SCREAMING_SNAKE_CASE__		: Dict     =     train_labels[:validation_size]
       SCREAMING_SNAKE_CASE__		: List[Any]     =     train_images[validation_size:]
       SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     train_labels[validation_size:]

       SCREAMING_SNAKE_CASE__		: str     =     {"""dtype""": dtype, """reshape""": reshape, """seed""": seed}

       SCREAMING_SNAKE_CASE__		: List[Any]     =     _DataSet(__UpperCAmelCase     ,__UpperCAmelCase     ,**__UpperCAmelCase		)
       SCREAMING_SNAKE_CASE__		: str     =     _DataSet(__UpperCAmelCase     ,__UpperCAmelCase     ,**__UpperCAmelCase		)
       SCREAMING_SNAKE_CASE__		: Optional[Any]     =     _DataSet(__UpperCAmelCase     ,__UpperCAmelCase     ,**__UpperCAmelCase		)

       return _Datasets(train=__UpperCAmelCase     ,validation=__UpperCAmelCase     ,test=__UpperCAmelCase		)






"""simple docstring"""




import functools
import operator

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


UpperCAmelCase__   :					str												=				logging.get_logger(__name__)

UpperCAmelCase__   :					Dict												=				{
    'microsoft/unispeech-large-1500h-cv': (
        'https://huggingface.co/microsoft/unispeech-large-1500h-cv/resolve/main/config.json'
    ),
    # See all UniSpeech models at https://huggingface.co/models?filter=unispeech
}



class 			lowerCAmelCase_		(a__						):


				"""simple docstring"""



				__UpperCamelCase   :		List[str]       =     '''unispeech'''



				def __init__(self     , SCREAMING_SNAKE_CASE__=32     , SCREAMING_SNAKE_CASE__=7_68     , SCREAMING_SNAKE_CASE__=12     , SCREAMING_SNAKE_CASE__=12     , SCREAMING_SNAKE_CASE__=30_72     , SCREAMING_SNAKE_CASE__="gelu"     , SCREAMING_SNAKE_CASE__=0.1     , SCREAMING_SNAKE_CASE__=0.1     , SCREAMING_SNAKE_CASE__=0.1     , SCREAMING_SNAKE_CASE__=0.0     , SCREAMING_SNAKE_CASE__=0.0     , SCREAMING_SNAKE_CASE__=0.1     , SCREAMING_SNAKE_CASE__=0.1     , SCREAMING_SNAKE_CASE__=0.02     , SCREAMING_SNAKE_CASE__=1E-5     , SCREAMING_SNAKE_CASE__="group"     , SCREAMING_SNAKE_CASE__="gelu"     , SCREAMING_SNAKE_CASE__=(5_12, 5_12, 5_12, 5_12, 5_12, 5_12, 5_12)     , SCREAMING_SNAKE_CASE__=(5, 2, 2, 2, 2, 2, 2)     , SCREAMING_SNAKE_CASE__=(10, 3, 3, 3, 3, 2, 2)     , SCREAMING_SNAKE_CASE__=False     , SCREAMING_SNAKE_CASE__=1_28     , SCREAMING_SNAKE_CASE__=16     , SCREAMING_SNAKE_CASE__=False     , SCREAMING_SNAKE_CASE__=True     , SCREAMING_SNAKE_CASE__=0.05     , SCREAMING_SNAKE_CASE__=10     , SCREAMING_SNAKE_CASE__=2     , SCREAMING_SNAKE_CASE__=0.0     , SCREAMING_SNAKE_CASE__=10     , SCREAMING_SNAKE_CASE__=0     , SCREAMING_SNAKE_CASE__=3_20     , SCREAMING_SNAKE_CASE__=2     , SCREAMING_SNAKE_CASE__=0.1     , SCREAMING_SNAKE_CASE__=1_00     , SCREAMING_SNAKE_CASE__=2_56     , SCREAMING_SNAKE_CASE__=2_56     , SCREAMING_SNAKE_CASE__=0.1     , SCREAMING_SNAKE_CASE__="mean"     , SCREAMING_SNAKE_CASE__=False     , SCREAMING_SNAKE_CASE__=False     , SCREAMING_SNAKE_CASE__=2_56     , SCREAMING_SNAKE_CASE__=80     , SCREAMING_SNAKE_CASE__=0     , SCREAMING_SNAKE_CASE__=1     , SCREAMING_SNAKE_CASE__=2     , SCREAMING_SNAKE_CASE__=0.5     , **SCREAMING_SNAKE_CASE__     , )      ->	List[Any]:



											"""simple docstring"""




											super().__init__(**SCREAMING_SNAKE_CASE__     , pad_token_id=SCREAMING_SNAKE_CASE__     , bos_token_id=SCREAMING_SNAKE_CASE__     , eos_token_id=SCREAMING_SNAKE_CASE__					)
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     hidden_size
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     feat_extract_norm
											SCREAMING_SNAKE_CASE__		: Any     =     feat_extract_activation
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     list(SCREAMING_SNAKE_CASE__					)
											SCREAMING_SNAKE_CASE__		: str     =     list(SCREAMING_SNAKE_CASE__					)
											SCREAMING_SNAKE_CASE__		: Tuple     =     list(SCREAMING_SNAKE_CASE__					)
											SCREAMING_SNAKE_CASE__		: List[str]     =     conv_bias
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     num_conv_pos_embeddings
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     num_conv_pos_embedding_groups
											SCREAMING_SNAKE_CASE__		: Dict     =     len(self.conv_dim					)
											SCREAMING_SNAKE_CASE__		: List[str]     =     num_hidden_layers
											SCREAMING_SNAKE_CASE__		: str     =     intermediate_size
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     hidden_act
											SCREAMING_SNAKE_CASE__		: Any     =     num_attention_heads
											SCREAMING_SNAKE_CASE__		: Dict     =     hidden_dropout
											SCREAMING_SNAKE_CASE__		: List[str]     =     attention_dropout
											SCREAMING_SNAKE_CASE__		: Any     =     activation_dropout
											SCREAMING_SNAKE_CASE__		: int     =     feat_proj_dropout
											SCREAMING_SNAKE_CASE__		: List[Any]     =     final_dropout
											SCREAMING_SNAKE_CASE__		: str     =     layerdrop
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     layer_norm_eps
											SCREAMING_SNAKE_CASE__		: Dict     =     initializer_range
											SCREAMING_SNAKE_CASE__		: Dict     =     num_ctc_classes
											SCREAMING_SNAKE_CASE__		: List[Any]     =     vocab_size
											SCREAMING_SNAKE_CASE__		: str     =     do_stable_layer_norm
											SCREAMING_SNAKE_CASE__		: Dict     =     use_weighted_layer_sum
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     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
											SCREAMING_SNAKE_CASE__		: Tuple     =     apply_spec_augment
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     mask_time_prob
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     mask_time_length
											SCREAMING_SNAKE_CASE__		: List[str]     =     mask_time_min_masks
											SCREAMING_SNAKE_CASE__		: Tuple     =     mask_feature_prob
											SCREAMING_SNAKE_CASE__		: Any     =     mask_feature_length
											SCREAMING_SNAKE_CASE__		: int     =     mask_feature_min_masks

											# parameters for pretraining with codevector quantized representations
											SCREAMING_SNAKE_CASE__		: Dict     =     num_codevectors_per_group
											SCREAMING_SNAKE_CASE__		: Any     =     num_codevector_groups
											SCREAMING_SNAKE_CASE__		: str     =     contrastive_logits_temperature
											SCREAMING_SNAKE_CASE__		: str     =     feat_quantizer_dropout
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     num_negatives
											SCREAMING_SNAKE_CASE__		: Tuple     =     codevector_dim
											SCREAMING_SNAKE_CASE__		: List[Any]     =     proj_codevector_dim
											SCREAMING_SNAKE_CASE__		: Tuple     =     diversity_loss_weight

											# ctc loss
											SCREAMING_SNAKE_CASE__		: int     =     ctc_loss_reduction
											SCREAMING_SNAKE_CASE__		: str     =     ctc_zero_infinity

											# pretraining loss
											SCREAMING_SNAKE_CASE__		: List[str]     =     replace_prob



				@property
				def 			__magic_name__					(self					)      ->	Tuple:



											"""simple docstring"""




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




