U
    (d$                     @   s  d dl mZ d dlmZ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 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" dddgZ#G dd deZ$G dd deZ%G dd deZ&ee e'ee e(e(ee&dddZ)G dd deZ*eddd fddd d!d"eee*ef  e(e(ee&d#d$dZ+d	d%lm,Z, d	d&lm-Z- e,d'e*j.j/iZ0dS )(    )partial)AnyListOptionalUnionN)nnTensor)	QuantStubDeQuantStub   )Conv2dNormActivationSqueezeExcitation)ImageClassification   )WeightsEnumWeights)_IMAGENET_CATEGORIES)handle_legacy_interface_ovewrite_named_param)InvertedResidualInvertedResidualConfigMobileNetV3_mobilenet_v3_confMobileNet_V3_Large_Weights   )_fuse_modules_replace_reluQuantizableMobileNetV3#MobileNet_V3_Large_QuantizedWeightsmobilenet_v3_largec                       s\   e Zd ZdZeedd fddZeedddZdee	 dd	d
dZ
 fddZ  ZS )QuantizableSqueezeExcitationr   Nargskwargsreturnc                    s(   t j|d< t j|| t j | _d S )NZscale_activation)r   ZHardsigmoidsuper__init__	quantizedFloatFunctionalskip_mulselfr"   r#   	__class__ O/tmp/pip-unpacked-wheel-vx7f76es/torchvision/models/quantization/mobilenetv3.pyr&   !   s    
z%QuantizableSqueezeExcitation.__init__)inputr$   c                 C   s   | j | ||S N)r)   mulZ_scale)r+   r0   r.   r.   r/   forward&   s    z$QuantizableSqueezeExcitation.forwardis_qatr$   c                 C   s   t | ddg|dd d S )NZfc1Z
activationTZinplace)r   )r+   r5   r.   r.   r/   
fuse_model)   s    z'QuantizableSqueezeExcitation.fuse_modelc              	      s   | dd }t| dr|d ks&|dk rtdgtdgtjdgtjdtjdgtjdtdgtdgd}	|	 D ] \}
}||
 }||kr||||< q|t ||||||| d S )	Nversionqconfigr   g      ?r   )Z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torchZtensorZint32itemsr%   _load_from_state_dict)r+   Z
state_dictprefixZlocal_metadatastrictZmissing_keysZunexpected_keysZ
error_msgsr8   Zdefault_state_dictkvZfull_keyr,   r.   r/   r>   ,   s2    


 



z2QuantizableSqueezeExcitation._load_from_state_dict)N)__name__
__module____qualname___versionr   r&   r   r3   r   boolr7   r>   __classcell__r.   r.   r,   r/   r       s
   r    c                       s6   e Zd Zeedd fddZeedddZ  ZS )QuantizableInvertedResidualNr!   c                    s&   t  j|dti| tj | _d S )NZse_layer)r%   r&   r    r   r'   r(   skip_addr*   r,   r.   r/   r&   U   s    z$QuantizableInvertedResidual.__init__xr$   c                 C   s(   | j r| j|| |S | |S d S r1   )Zuse_res_connectrJ   addblockr+   rL   r.   r.   r/   r3   Y   s    z#QuantizableInvertedResidual.forward)rC   rD   rE   r   r&   r   r3   rH   r.   r.   r,   r/   rI   S   s   rI   c                       sL   e Zd Zeedd fddZeedddZdee ddd	d
Z	  Z
S )r   Nr!   c                    s"   t  j|| t | _t | _dS )zq
        MobileNet V3 main class

        Args:
           Inherits args from floating point MobileNetV3
        N)r%   r&   r	   quantr
   dequantr*   r,   r.   r/   r&   a   s    zQuantizableMobileNetV3.__init__rK   c                 C   s"   |  |}| |}| |}|S r1   )rP   Z_forward_implrQ   rO   r.   r.   r/   r3   l   s    


zQuantizableMobileNetV3.forwardr4   c                 C   sv   |   D ]h}t|tkrZddg}t|dkrHt|d tjkrH|d t|||dd qt|tkr|	| qd S )N01r   r   2Tr6   )
modulestyper   lenr   ZReLUappendr   r    r7   )r+   r5   mZmodules_to_fuser.   r.   r/   r7   r   s    
z!QuantizableMobileNetV3.fuse_model)N)rC   rD   rE   r   r&   r   r3   r   rG   r7   rH   r.   r.   r,   r/   r   `   s   )inverted_residual_settinglast_channelweightsprogressquantizer#   r$   c                 K   s   |d k	r:t |dt|jd  d|jkr:t |d|jd  |dd}t| |fdti|}t| |r|jdd tj	j
||_tj	j
j|dd |d k	r||j|d	 |rtj	j
j|dd |  |S )
NZnum_classes
categoriesbackendqnnpackrN   T)r5   r6   )r]   )r   rW   metapopr   rI   r   r7   r<   ZaoZquantizationZget_default_qat_qconfigr9   Zprepare_qatZload_state_dictZget_state_dictconverteval)rZ   r[   r\   r]   r^   r#   r`   modelr.   r.   r/   _mobilenet_v3_model}   s"    
rg   c                   @   sD   e Zd Zedeeddddeddejdd	d
didddZ	e	Z
dS )r   zUhttps://download.pytorch.org/models/quantized/mobilenet_v3_large_qnnpack-5bcacf28.pth   )Z	crop_sizeiS )r   r   ra   zUhttps://github.com/pytorch/vision/tree/main/references/classification#qat-mobilenetv3zImageNet-1KgK7A@R@gxV@)zacc@1zacc@5z
                These weights were produced by doing Quantization Aware Training (eager mode) on top of the unquantized
                weights listed below.
            )Z
num_paramsZmin_sizer_   r`   ZrecipeZunquantizedZ_metricsZ_docs)urlZ
transformsrb   N)rC   rD   rE   r   r   r   r   r   IMAGENET1K_V1IMAGENET1K_QNNPACK_V1DEFAULTr.   r.   r.   r/   r      s$   
Z
pretrainedc                 C   s   |  ddrtjS tjS )Nr^   F)r:   r   rk   r   rj   )r#   r.   r.   r/   <lambda>   s    
rm   )r\   TF)r\   r]   r^   )r\   r]   r^   r#   r$   c                 K   s4   |rt nt| } td|\}}t||| ||f|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   r   verifyr   rg   )r\   r]   r^   r#   rZ   r[   r.   r.   r/   r      s    ,)
_ModelURLs)
model_urlsZmobilenet_v3_large_qnnpack)1	functoolsr   typingr   r   r   r   r<   r   r   Ztorch.ao.quantizationr	   r
   Zops.miscr   r   Ztransforms._presetsr   Z_apir   r   Z_metar   _utilsr   r   Zmobilenetv3r   r   r   r   r   utilsr   r   __all__r    rI   r   intrG   rg   r   r   ro   rp   rk   ri   Zquant_model_urlsr.   r.   r.   r/   <module>   sb   5$
+ 