from functools import partial
from typing import Any, Callable, Dict, List, Type, TypeVar, Union, overload

import torch
import torch.nn as nn
import torch.types


def        __snake_case       (						lowerCAmelCase_   ,       lowerCAmelCase_   ,       lowerCAmelCase_  )							->	torch.Tensor:







    if not inplace:
        SCREAMING_SNAKE_CASE__       							= ma + ma
    else:
        ma += ma

    return ma


def        __snake_case       (						lowerCAmelCase_   ,       lowerCAmelCase_  )							->	torch.Tensor:
    SCREAMING_SNAKE_CASE__       							= -1 * len(lowerCAmelCase_  )
    SCREAMING_SNAKE_CASE__       							= list(range(len(tensor.shape[:zero_index]  )  )  )
    return tensor.permute(first_inds + [zero_index + i for i in inds]  )


def        __snake_case       (						lowerCAmelCase_   ,       lowerCAmelCase_  )							->	torch.Tensor:
    return t.reshape(t.shape[:-no_dims] + (-1,)  )


def        __snake_case       (						lowerCAmelCase_   ,       lowerCAmelCase_   ,       lowerCAmelCase_   ,       lowerCAmelCase_ = 1e-4  )							->	torch.Tensor:
    SCREAMING_SNAKE_CASE__       							= mask.expand(*value.shape  )
    return torch.sum(mask * value   ,       dim=lowerCAmelCase_  ) / (eps + torch.sum(lowerCAmelCase_   ,       dim=lowerCAmelCase_  ))


def        __snake_case       (						lowerCAmelCase_   ,       lowerCAmelCase_ = 2.31_25   ,       lowerCAmelCase_ = 21.68_75   ,       lowerCAmelCase_ = 6_4  )							->	torch.Tensor:
    SCREAMING_SNAKE_CASE__       							= torch.linspace(lowerCAmelCase_   ,       lowerCAmelCase_   ,       no_bins - 1   ,       device=pts.device  )
    SCREAMING_SNAKE_CASE__       							= torch.sqrt(torch.sum((pts.unsqueeze(-2  ) - pts.unsqueeze(-3  )) ** 2   ,       dim=-1  )  )
    return torch.bucketize(lowerCAmelCase_   ,       lowerCAmelCase_  )


def        __snake_case       (						lowerCAmelCase_   ,       lowerCAmelCase_  )							->	dict:
    SCREAMING_SNAKE_CASE__       							= dicts[0]
    SCREAMING_SNAKE_CASE__       							= {}
    for k, v in first.items():
        SCREAMING_SNAKE_CASE__       							= [d[k] for d in dicts]
        if isinstance(lowerCAmelCase_   ,       lowerCAmelCase_  ):
            SCREAMING_SNAKE_CASE__       							= dict_multimap(lowerCAmelCase_   ,       lowerCAmelCase_  )
        else:
            SCREAMING_SNAKE_CASE__       							= fn(lowerCAmelCase_  )

    return new_dict


def        __snake_case       (						lowerCAmelCase_   ,       lowerCAmelCase_  )							->	torch.Tensor:
    SCREAMING_SNAKE_CASE__       							= v_bins.view(((1,) * len(x.shape  )) + (len(lowerCAmelCase_  ),)  )
    SCREAMING_SNAKE_CASE__       							= x[..., None] - reshaped_bins
    SCREAMING_SNAKE_CASE__       							= torch.argmin(torch.abs(lowerCAmelCase_  )   ,       dim=-1  )
    return nn.functional.one_hot(lowerCAmelCase_   ,       num_classes=len(lowerCAmelCase_  )  ).float()


def        __snake_case       (						lowerCAmelCase_   ,       lowerCAmelCase_   ,       lowerCAmelCase_ = 0   ,       lowerCAmelCase_ = 0  )							->	torch.Tensor:
    SCREAMING_SNAKE_CASE__       							= []
    for i, s in enumerate(data.shape[:no_batch_dims]  ):
        SCREAMING_SNAKE_CASE__       							= torch.arange(lowerCAmelCase_  )
        SCREAMING_SNAKE_CASE__       							= r.view(*(*((1,) * i), -1, *((1,) * (len(inds.shape  ) - i - 1)))  )
        ranges.append(lowerCAmelCase_  )

    SCREAMING_SNAKE_CASE__       							= [slice(lowerCAmelCase_  ) for _ in range(len(data.shape  ) - no_batch_dims  )]
    SCREAMING_SNAKE_CASE__       							= inds
    ranges.extend(lowerCAmelCase_  )
    # Matt note: Editing this to get around the behaviour of using a list as an array index changing
    # in recent Numpy versions
    return data[tuple(lowerCAmelCase_  )]


_A				:							Dict							= TypeVar("""T""")


def        __snake_case       (						lowerCAmelCase_   ,       lowerCAmelCase_   ,       lowerCAmelCase_  )							->	Dict[Any, Union[dict, list, tuple, Any]]:
    SCREAMING_SNAKE_CASE__       							= {}
    for k, v in dic.items():
        if isinstance(lowerCAmelCase_   ,       lowerCAmelCase_  ):
            SCREAMING_SNAKE_CASE__       							= dict_map(lowerCAmelCase_   ,       lowerCAmelCase_   ,       lowerCAmelCase_  )
        else:
            SCREAMING_SNAKE_CASE__       							= tree_map(lowerCAmelCase_   ,       lowerCAmelCase_   ,       lowerCAmelCase_  )

    return new_dict


@overload
def        __snake_case       (						lowerCAmelCase_   ,       lowerCAmelCase_   ,       lowerCAmelCase_  )							->	Any:
    ...


@overload
def        __snake_case       (						lowerCAmelCase_   ,       lowerCAmelCase_   ,       lowerCAmelCase_  )							->	dict:
    ...


@overload
def        __snake_case       (						lowerCAmelCase_   ,       lowerCAmelCase_   ,       lowerCAmelCase_  )							->	list:
    ...


@overload
def        __snake_case       (						lowerCAmelCase_   ,       lowerCAmelCase_   ,       lowerCAmelCase_  )							->	tuple:
    ...


def        __snake_case       (						lowerCAmelCase_   ,       lowerCAmelCase_   ,       lowerCAmelCase_  )							->	List[str]:







    if isinstance(lowerCAmelCase_   ,       lowerCAmelCase_  ):
        return dict_map(lowerCAmelCase_   ,       lowerCAmelCase_   ,       lowerCAmelCase_  )
    elif isinstance(lowerCAmelCase_   ,       lowerCAmelCase_  ):
        return [tree_map(lowerCAmelCase_   ,       lowerCAmelCase_   ,       lowerCAmelCase_  ) for x in tree]
    elif isinstance(lowerCAmelCase_   ,       lowerCAmelCase_  ):
        return tuple(tree_map(lowerCAmelCase_   ,       lowerCAmelCase_   ,       lowerCAmelCase_  ) for x in tree  )
    elif isinstance(lowerCAmelCase_   ,       lowerCAmelCase_  ):
        return fn(lowerCAmelCase_  )
    else:
        print(type(lowerCAmelCase_  )  )
        raise ValueError('''Not supported'''  )



_A				:							Any							= partial(tree_map, leaf_type=torch.Tensor)







import functools
import operator

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


_A				:							Any							= logging.get_logger(__name__)

_A				:							Any							= {
    """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  (     __SCREAMING_SNAKE_CASE						):


    '''simple docstring'''







    lowerCamelCase__						:		int							=  """hubert"""



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





        '''simple docstring'''

        super().__init__(**A_      ,							pad_token_id=A_      ,							bos_token_id=A_      ,							eos_token_id=A_    )
        SCREAMING_SNAKE_CASE__       							= hidden_size
        SCREAMING_SNAKE_CASE__       							= feat_extract_norm
        SCREAMING_SNAKE_CASE__       							= feat_extract_activation
        SCREAMING_SNAKE_CASE__       							= list(A_    )
        SCREAMING_SNAKE_CASE__       							= list(A_    )
        SCREAMING_SNAKE_CASE__       							= list(A_    )
        SCREAMING_SNAKE_CASE__       							= conv_bias
        SCREAMING_SNAKE_CASE__       							= num_conv_pos_embeddings
        SCREAMING_SNAKE_CASE__       							= num_conv_pos_embedding_groups
        SCREAMING_SNAKE_CASE__       							= len(self.conv_dim    )
        SCREAMING_SNAKE_CASE__       							= num_hidden_layers
        SCREAMING_SNAKE_CASE__       							= intermediate_size
        SCREAMING_SNAKE_CASE__       							= hidden_act
        SCREAMING_SNAKE_CASE__       							= num_attention_heads
        SCREAMING_SNAKE_CASE__       							= hidden_dropout
        SCREAMING_SNAKE_CASE__       							= attention_dropout
        SCREAMING_SNAKE_CASE__       							= activation_dropout
        SCREAMING_SNAKE_CASE__       							= feat_proj_layer_norm
        SCREAMING_SNAKE_CASE__       							= feat_proj_dropout
        SCREAMING_SNAKE_CASE__       							= final_dropout
        SCREAMING_SNAKE_CASE__       							= layerdrop
        SCREAMING_SNAKE_CASE__       							= layer_norm_eps
        SCREAMING_SNAKE_CASE__       							= initializer_range
        SCREAMING_SNAKE_CASE__       							= vocab_size
        SCREAMING_SNAKE_CASE__       							= do_stable_layer_norm
        SCREAMING_SNAKE_CASE__       							= use_weighted_layer_sum
        SCREAMING_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
        SCREAMING_SNAKE_CASE__       							= apply_spec_augment
        SCREAMING_SNAKE_CASE__       							= mask_time_prob
        SCREAMING_SNAKE_CASE__       							= mask_time_length
        SCREAMING_SNAKE_CASE__       							= mask_time_min_masks
        SCREAMING_SNAKE_CASE__       							= mask_feature_prob
        SCREAMING_SNAKE_CASE__       							= mask_feature_length
        SCREAMING_SNAKE_CASE__       							= mask_feature_min_masks

        # ctc loss
        SCREAMING_SNAKE_CASE__       							= ctc_loss_reduction
        SCREAMING_SNAKE_CASE__       							= ctc_zero_infinity




    @property
    def    lowercase_				(					self    ):





        '''simple docstring'''

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