




'''simple docstring'''







import math

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

from .binarizer import MagnitudeBinarizer, ThresholdBinarizer, TopKBinarizer





class 					lowerCamelCase_							(	nn.Linear							):




					'''simple docstring'''






					def __init__(   self,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase = True,  __UpperCamelCase = "constant",  __UpperCamelCase = 0.0,  __UpperCamelCase = "topK",  ):
							super(__UpperCamelCase,  self			).__init__(in_features=__UpperCamelCase,  out_features=__UpperCamelCase,  bias=__UpperCamelCase			)
							assert pruning_method in ["topK", "threshold", "sigmoied_threshold", "magnitude", "l0"]
							_snake_case     	=	pruning_method

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






					def     __SCREAMING_SNAKE_CASE     (   self			):
							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     __SCREAMING_SNAKE_CASE     (   self,  __UpperCamelCase,  __UpperCamelCase			):
							# Get the mask
							if self.pruning_method == "topK":
									_snake_case     	=	TopKBinarizer.apply(self.mask_scores,  __UpperCamelCase			)
							elif self.pruning_method in ["threshold", "sigmoied_threshold"]:
									_snake_case     	=	"sigmoied" in self.pruning_method
									_snake_case     	=	ThresholdBinarizer.apply(self.mask_scores,  __UpperCamelCase,  __UpperCamelCase			)
							elif self.pruning_method == "magnitude":
									_snake_case     	=	MagnitudeBinarizer.apply(self.weight,  __UpperCamelCase			)
							elif self.pruning_method == "l0":
									_snake_case	,   _snake_case	,   _snake_case     	=	-0.1, 1.1, 2 / 3
									if self.training:
											_snake_case     	=	torch.zeros_like(self.mask_scores			).uniform_().clamp(0.00_01,  0.99_99			)
											_snake_case     	=	torch.sigmoid((u.log() - (1 - u).log() + self.mask_scores) / b			)
									else:
											_snake_case     	=	torch.sigmoid(self.mask_scores			)
									_snake_case     	=	s * (r - l) + l
									_snake_case     	=	s_bar.clamp(min=0.0,  max=1.0			)
							# Mask weights with computed mask
							_snake_case     	=	mask * self.weight
							# Compute output (linear layer) with masked weights
							return nn.functional.linear(__UpperCamelCase,  __UpperCamelCase,  self.bias			)











'''simple docstring'''







from typing import TYPE_CHECKING

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


_lowerCamelCase							:						List[Any]							     =  {
    'configuration_mobilenet_v1': [
        'MOBILENET_V1_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'MobileNetV1Config',
        'MobileNetV1OnnxConfig',
    ],
}

try:
						if not is_vision_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						_lowerCamelCase							:						str							     =  ['MobileNetV1FeatureExtractor']
						_lowerCamelCase							:						Tuple							     =  ['MobileNetV1ImageProcessor']

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						_lowerCamelCase							:						List[Any]							     =  [
						    'MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST',
						    'MobileNetV1ForImageClassification',
						    'MobileNetV1Model',
						    'MobileNetV1PreTrainedModel',
						    'load_tf_weights_in_mobilenet_v1',
						]


if TYPE_CHECKING:
						from .configuration_mobilenet_va import (
						    MOBILENET_V1_PRETRAINED_CONFIG_ARCHIVE_MAP,
						    MobileNetVaConfig,
						    MobileNetVaOnnxConfig,
						)

						try:
												if not is_vision_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .feature_extraction_mobilenet_va import MobileNetVaFeatureExtractor
												from .image_processing_mobilenet_va import MobileNetVaImageProcessor

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_mobilenet_va import (
												    MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST,
												    MobileNetVaForImageClassification,
												    MobileNetVaModel,
												    MobileNetVaPreTrainedModel,
												    load_tf_weights_in_mobilenet_va,
												)


else:
						import sys

						_lowerCamelCase							:						Union[str, Any]							     =  _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)





