
    h($                        d dl mZ d dlmZmZmZ d dlZd dlmZmZ d dl	m
Z
mZ ddlmZmZ ddlmZ d	d
lmZmZmZ d	dlmZ d	dlmZmZ d	dlmZmZmZmZmZ ddl m!Z!m"Z" g dZ# G d de      Z$ G d de      Z% G d de      Z&de'e   de(dee   de)de)dede&fdZ* G d d e      Z+ ed!"       ed#d$ f%      dd&d'd(deee+ef      de)de)dede&f
d)              Z,y)*    )partial)AnyOptionalUnionN)nnTensor)DeQuantStub	QuantStub   )Conv2dNormActivationSqueezeExcitation)ImageClassification   )register_modelWeightsWeightsEnum)_IMAGENET_CATEGORIES)_ovewrite_named_paramhandle_legacy_interface)_mobilenet_v3_confInvertedResidualInvertedResidualConfigMobileNet_V3_Large_WeightsMobileNetV3   )_fuse_modules_replace_relu)QuantizableMobileNetV3#MobileNet_V3_Large_QuantizedWeightsmobilenet_v3_largec                   b     e Zd ZdZdededdf fdZdedefdZdd	ee	   ddfd
Z
 fdZ xZS )QuantizableSqueezeExcitationr   argskwargsreturnNc                     t         j                  |d<   t        |   |i | t         j                  j                         | _        y )Nscale_activation)r   Hardsigmoidsuper__init__	quantizedFloatFunctionalskip_mulselfr#   r$   	__class__s      i/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/torchvision/models/quantization/mobilenetv3.pyr*   z%QuantizableSqueezeExcitation.__init__!   s8    %'^^!"$)&)446    inputc                 X    | j                   j                  | j                  |      |      S N)r-   mul_scale)r/   r3   s     r1   forwardz$QuantizableSqueezeExcitation.forward&   s"    }}  U!3U;;r2   is_qatc                 &    t        | ddg|d       y )Nfc1
activationTinplace)r   )r/   r9   s     r1   
fuse_modelz'QuantizableSqueezeExcitation.fuse_model)   s    dUL164Hr2   c           	      
   |j                  dd       }t        | d      r||dk  rt        j                  dg      t        j                  dg      t        j                  dgt        j                        t        j                  dgt        j                        t        j                  dg      t        j                  dg      d}	|	j                         D ]  \  }
}||
z   }||vs|||<    t        |   |||||||       y )	Nversionqconfigr   g      ?r   )dtyper   )z.scale_activation.activation_post_process.scalezFscale_activation.activation_post_process.activation_post_process.scalez3scale_activation.activation_post_process.zero_pointzKscale_activation.activation_post_process.activation_post_process.zero_pointz;scale_activation.activation_post_process.fake_quant_enabledz9scale_activation.activation_post_process.observer_enabled)gethasattrtorchtensorint32itemsr)   _load_from_state_dict)r/   
state_dictprefixlocal_metadatastrictmissing_keysunexpected_keys
error_msgsrA   default_state_dictkvfull_keyr0   s                r1   rJ   z2QuantizableSqueezeExcitation._load_from_state_dict,   s    !$$Y54#GaKBG,,PSuBUZ_ZfZfhkglZmGL||UVTW_d_j_jGk_d_k_kCu{{` PU||]^\_O`MR\\[\Z]M^	" +002 -1!A::-+,Jx(-
 	%	
r2   r5   )__name__
__module____qualname___versionr   r*   r   r8   r   boolr?   rJ   __classcell__r0   s   @r1   r"   r"      sY    H7c 7S 7T 7
<V < <I$ I4 I$
 $
r2   r"   c                   <     e Zd Zdededdf fdZdedefdZ xZS )QuantizableInvertedResidualr#   r$   r%   Nc                 v    t        |   |dt        i| t        j                  j                         | _        y )Nse_layer)r)   r*   r"   r   r+   r,   skip_addr.   s      r1   r*   z$QuantizableInvertedResidual.__init__U   s/    $P)EPP446r2   xc                     | j                   r+| j                  j                  || j                  |            S | j                  |      S r5   )use_res_connectra   addblockr/   rb   s     r1   r8   z#QuantizableInvertedResidual.forwardY   s8    ==$$Q

166::a= r2   )rV   rW   rX   r   r*   r   r8   r[   r\   s   @r1   r^   r^   S   s0    7c 7S 7T 7! !F !r2   r^   c                   T     e Zd Zdededdf fdZdedefdZd
dee   ddfd	Z	 xZ
S )r   r#   r$   r%   Nc                 `    t        |   |i | t               | _        t	               | _        y)zq
        MobileNet V3 main class

        Args:
           Inherits args from floating point MobileNetV3
        N)r)   r*   r
   quantr	   dequantr.   s      r1   r*   zQuantizableMobileNetV3.__init__a   s)     	$)&)[
"}r2   rb   c                 l    | j                  |      }| j                  |      }| j                  |      }|S r5   )rj   _forward_implrk   rg   s     r1   r8   zQuantizableMobileNetV3.forwardl   s1    JJqMq!LLOr2   r9   c                 8   | j                         D ]  }t        |      t        u rQddg}t        |      dk(  r/t        |d         t        j
                  u r|j                  d       t        |||d       et        |      t        u sw|j                  |        y )N01r   r   2Tr=   )
modulestyper   lenr   ReLUappendr   r"   r?   )r/   r9   mmodules_to_fuses       r1   r?   z!QuantizableMobileNetV3.fuse_modelr   s     	%AAw..#&*q6Q;4!:#8#**3/a&$Ga88V$	%r2   r5   )rV   rW   rX   r   r*   r   r8   r   rZ   r?   r[   r\   s   @r1   r   r   `   sG    	%c 	%S 	%T 	% F %$ %4 %r2   r   inverted_residual_settinglast_channelweightsprogressquantizer$   r%   c                    |Kt        |dt        |j                  d                d|j                  v rt        |d|j                  d          |j                  dd      }t	        | |fdt
        i|}t        |       |rk|j                  d       t        j                  j                  j                  |      |_        t        j                  j                  j                  |d       |"|j                  |j                  |d	             |r;t        j                  j                  j!                  |d       |j#                          |S )
Nnum_classes
categoriesbackendqnnpackrf   T)r9   r=   )r|   
check_hash)r   rt   metapopr   r^   r   r?   rF   aoquantizationget_default_qat_qconfigrB   prepare_qatload_state_dictget_state_dictconverteval)ry   rz   r{   r|   r}   r$   r   models           r1   _mobilenet_v3_modelr   }   s    fmSl9S5TU$!&)W\\)5LMjjI.G"#<lxRmxqwxE%
 	%--EEgN))%)>g44hSW4XY%%eT%:

Lr2   c                   j    e Zd Z ed eed      ddeddej                  dd	d
didddd
      Z	e	Z
y)r   zUhttps://download.pytorch.org/models/quantized/mobilenet_v3_large_qnnpack-5bcacf28.pth   )	crop_sizeiS )r   r   r   zUhttps://github.com/pytorch/vision/tree/main/references/classification#qat-mobilenetv3zImageNet-1KgK7A@R@gxV@)zacc@1zacc@5g-?gҍ5@z
                These weights were produced by doing Quantization Aware Training (eager mode) on top of the unquantized
                weights listed below.
            )

num_paramsmin_sizer   r   recipeunquantized_metrics_ops
_file_size_docs)url
transformsr   N)rV   rW   rX   r   r   r   r   r   IMAGENET1K_V1IMAGENET1K_QNNPACK_V1DEFAULT r2   r1   r   r      s_    #c.#>!. m5CC##   
0 $Gr2   r   quantized_mobilenet_v3_large)name
pretrainedc                 f    | j                  dd      rt        j                  S t        j                  S )Nr}   F)rD   r   r   r   r   )r$   s    r1   <lambda>r      s0    zz*e, 0EE  ,99 r2   )r{   TF)r{   r|   r}   c                 x    |rt         nt        j                  |       } t        di |\  }}t	        ||| ||fi |S )a  
    MobileNetV3 (Large) model from
    `Searching for MobileNetV3 <https://arxiv.org/abs/1905.02244>`_.

    .. note::
        Note that ``quantize = True`` returns a quantized model with 8 bit
        weights. Quantized models only support inference and run on CPUs.
        GPU inference is not yet supported.

    Args:
        weights (:class:`~torchvision.models.quantization.MobileNet_V3_Large_QuantizedWeights` or :class:`~torchvision.models.MobileNet_V3_Large_Weights`, optional): The
            pretrained weights for the model. See
            :class:`~torchvision.models.quantization.MobileNet_V3_Large_QuantizedWeights` below for
            more details, and possible values. By default, no pre-trained
            weights are used.
        progress (bool): If True, displays a progress bar of the
            download to stderr. Default is True.
        quantize (bool): If True, return a quantized version of the model. Default is False.
        **kwargs: parameters passed to the ``torchvision.models.quantization.MobileNet_V3_Large_QuantizedWeights``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/quantization/mobilenetv3.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.quantization.MobileNet_V3_Large_QuantizedWeights
        :members:
    .. autoclass:: torchvision.models.MobileNet_V3_Large_Weights
        :members:
        :noindex:
    )r    )r   r   verifyr   r   )r{   r|   r}   r$   ry   rz   s         r1   r    r       sL    ^ 7?2D^ffgnoG.@.`Y_.`+|8,QY[cngmnnr2   )-	functoolsr   typingr   r   r   rF   r   r   torch.ao.quantizationr	   r
   ops.miscr   r   transforms._presetsr   _apir   r   r   _metar   _utilsr   r   mobilenetv3r   r   r   r   r   utilsr   r   __all__r"   r^   r   listintrZ   r   r   r    r   r2   r1   <module>r      sN    ' '   8 ? 6 7 7 ( C  02
#4 2
j
!"2 
!%[ %:!#$:;!! k"! 	!
 ! ! !H$+ $8 34	
	 ae	'oe?A[[\]'o 'o 	'o
 'o 'o	 5'or2   