



import math

import torch
from torch import nn
from torch.nn import init

from .binarizer import MagnitudeBinarizer, ThresholdBinarizer, TopKBinarizer
class      A__       (						nn.Linear    ):





							def __init__(			self  :       Dict					,			_UpperCAmelCase  :       int					,			_UpperCAmelCase  :       int					,			_UpperCAmelCase  :       bool = True					,			_UpperCAmelCase  :       str = "constant"					,			_UpperCAmelCase  :       float = 0.0					,			_UpperCAmelCase  :       str = "topK"					,			)						->		int:



											"""simple docstring"""


											super(SCREAMING_SNAKE_CASE__					,			self    ).__init__(in_features=SCREAMING_SNAKE_CASE__					,			out_features=SCREAMING_SNAKE_CASE__					,			bias=SCREAMING_SNAKE_CASE__    )
											assert pruning_method in ["topK", "threshold", "sigmoied_threshold", "magnitude", "l0"]
											__lowercase								= pruning_method

											if self.pruning_method in ["topK", "threshold", "sigmoied_threshold", "l0"]:
															__lowercase								= mask_scale
															__lowercase								= mask_init
															__lowercase								= nn.Parameter(torch.empty(self.weight.size()    )    )
															self.init_mask()





							def       a__			(			self  :       List[Any]    )						->		List[Any]:



											"""simple docstring"""


											if self.mask_init == "constant":
															init.constant_(self.mask_scores					,			val=self.mask_scale    )
											elif self.mask_init == "uniform":
															init.uniform_(self.mask_scores					,			a=-self.mask_scale					,			b=self.mask_scale    )
											elif self.mask_init == "kaiming":
															init.kaiming_uniform_(self.mask_scores					,			a=math.sqrt(5    )    )





							def       a__			(			self  :       List[Any]					,			_UpperCAmelCase  :       torch.tensor					,			_UpperCAmelCase  :       float    )						->		Any:



											"""simple docstring"""


											if self.pruning_method == "topK":
															__lowercase								= TopKBinarizer.apply(self.mask_scores					,			SCREAMING_SNAKE_CASE__    )
											elif self.pruning_method in ["threshold", "sigmoied_threshold"]:
															__lowercase								= "sigmoied" in self.pruning_method
															__lowercase								= ThresholdBinarizer.apply(self.mask_scores					,			SCREAMING_SNAKE_CASE__					,			SCREAMING_SNAKE_CASE__    )
											elif self.pruning_method == "magnitude":
															__lowercase								= MagnitudeBinarizer.apply(self.weight					,			SCREAMING_SNAKE_CASE__    )
											elif self.pruning_method == "l0":
															__lowercase								= -0.1, 1.1, 2 / 3
															if self.training:
																			__lowercase								= torch.zeros_like(self.mask_scores    ).uniform_().clamp(0.0_001					,			0.9_999    )
																			__lowercase								= torch.sigmoid((u.log() - (1 - u).log() + self.mask_scores) / b    )
															else:
																			__lowercase								= torch.sigmoid(self.mask_scores    )
															__lowercase								= s * (r - l) + l
															__lowercase								= s_bar.clamp(min=0.0					,			max=1.0    )
											# Mask weights with computed mask
											__lowercase								= mask * self.weight
											# Compute output (linear layer) with masked weights
											return nn.functional.linear(SCREAMING_SNAKE_CASE__					,			SCREAMING_SNAKE_CASE__					,			self.bias    )







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


SCREAMING_SNAKE_CASE__							=    {"""configuration_hubert""": ["""HUBERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """HubertConfig"""]}

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							SCREAMING_SNAKE_CASE__							=    [
							    """HUBERT_PRETRAINED_MODEL_ARCHIVE_LIST""",
							    """HubertForCTC""",
							    """HubertForSequenceClassification""",
							    """HubertModel""",
							    """HubertPreTrainedModel""",
							]


try:
							if not is_tf_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							SCREAMING_SNAKE_CASE__							=    [
							    """TF_HUBERT_PRETRAINED_MODEL_ARCHIVE_LIST""",
							    """TFHubertForCTC""",
							    """TFHubertModel""",
							    """TFHubertPreTrainedModel""",
							]

if TYPE_CHECKING:
							from .configuration_hubert import HUBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, HubertConfig

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_hubert import (
														    HUBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
														    HubertForCTC,
														    HubertForSequenceClassification,
														    HubertModel,
														    HubertPreTrainedModel,
														)

							try:
														if not is_tf_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_tf_hubert import (
														    TF_HUBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
														    TFHubertForCTC,
														    TFHubertModel,
														    TFHubertPreTrainedModel,
														)


else:
							import sys

							SCREAMING_SNAKE_CASE__							=    _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)





