
    ڧg$                        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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,dS )*    )partial)AnyList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                   h     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                      j        |i | t           j                                        | _        d S )Nscale_activation)r   Hardsigmoidsuper__init__	quantizedFloatFunctionalskip_mulselfr$   r%   	__class__s      g/var/www/html/ai-engine/env/lib/python3.11/site-packages/torchvision/models/quantization/mobilenetv3.pyr+   z%QuantizableSqueezeExcitation.__init__!   sE    %'^!"$)&)))4466    inputc                 ^    | j                             |                     |          |          S N)r.   mul_scale)r0   r4   s     r2   forwardz$QuantizableSqueezeExcitation.forward&   s&    }  U!3!3U;;;r3   is_qatc                 0    t          | ddg|d           d S )Nfc1
activationTinplace)r   )r0   r:   s     r2   
fuse_modelz'QuantizableSqueezeExcitation.fuse_model)   s$    dUL164HHHHHHr3   c           	      (   |                     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}	|	                                D ]\  }
}||
z   }||vr|||<   t                                          |||||||           d S )	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)r0   
state_dictprefixlocal_metadatastrictmissing_keysunexpected_keys
error_msgsrB   default_state_dictkvfull_keyr1   s                r2   rK   z2QuantizableSqueezeExcitation._load_from_state_dict,   sC    !$$Y554## 	-GaKKBG,PSuBUBUZ_ZfhkglZmZmGL|UVTW_d_jGkGkGk_d_kCu{` ` ` PU|]^\_O`O`MR\[\Z]M^M^	" 	" +0022 - -1!A::--+,Jx(%%	
 	
 	
 	
 	
r3   r6   )__name__
__module____qualname___versionr   r+   r	   r9   r   boolr@   rK   __classcell__r1   s   @r2   r#   r#      s        H7c 7S 7T 7 7 7 7 7 7
<V < < < < <I I$ I4 I I I I$
 $
 $
 $
 $
 $
 $
 $
 $
r3   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                      t                      j        |dt          i| t          j                                        | _        d S )Nse_layer)r*   r+   r#   r   r,   r-   skip_addr/   s      r2   r+   z$QuantizableInvertedResidual.__init__U   s>    $P)EPPPP4466r3   xc                     | j         r.| j                            ||                     |                    S |                     |          S r6   )use_res_connectrb   addblockr0   rc   s     r2   r9   z#QuantizableInvertedResidual.forwardY   s?     	!=$$Q

1666::a== r3   )rW   rX   rY   r   r+   r	   r9   r\   r]   s   @r2   r_   r_   S   st        7c 7S 7T 7 7 7 7 7 7! !F ! ! ! ! ! ! ! !r3   r_   c                   Z     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                      j        |i | t                      | _        t	                      | _        dS )zq
        MobileNet V3 main class

        Args:
           Inherits args from floating point MobileNetV3
        N)r*   r+   r   quantr
   dequantr/   s      r2   r+   zQuantizableMobileNetV3.__init__a   s;     	$)&)))[[
"}}r3   rc   c                     |                      |          }|                     |          }|                     |          }|S r6   )rk   _forward_implrl   rh   s     r2   r9   zQuantizableMobileNetV3.forwardl   s8    JJqMMq!!LLOOr3   r:   c                 x   |                                  D ]}t          |          t          u raddg}t          |          dk    r6t          |d                   t          j        u r|                    d           t          |||d           yt          |          t          u r|	                    |           d S )N01r   r   2Tr>   )
modulestyper   lenr   ReLUappendr   r#   r@   )r0   r:   mmodules_to_fuses       r2   r@   z!QuantizableMobileNetV3.fuse_modelr   s     	% 	%AAww...#&*q66Q;;4!::#8#8#**3///a&$GGGGGa888V$$$	% 	%r3   r6   )rW   rX   rY   r   r+   r	   r9   r   r[   r@   r\   r]   s   @r2   r   r   `   s        	%c 	%S 	%T 	% 	% 	% 	% 	% 	% F    % %$ %4 % % % % % % % %r3   r   inverted_residual_settinglast_channelweightsprogressquantizer%   r&   c                    |Nt          |dt          |j        d                              d|j        v rt          |d|j        d                    |                    dd          }t	          | |fdt
          i|}t          |           |re|                    d           t          j	        j
                            |          |_        t          j	        j
                            |d           |*|                    |                    |d	                     |r:t          j	        j
                            |d           |                                 |S )
Nnum_classes
categoriesbackendqnnpackrg   T)r:   r>   )r}   
check_hash)r   ru   metapopr   r_   r   r@   rG   aoquantizationget_default_qat_qconfigrC   prepare_qatload_state_dictget_state_dictconverteval)rz   r{   r|   r}   r~   r%   r   models           r2   _mobilenet_v3_modelr   }   sP    fmSl9S5T5TUUU$$!&)W\)5LMMMjjI..G"#<lxxRmxqwxxE% ?
 	%%%-EEgNN))%)>>>g44hSW4XXYYY %%eT%:::

Lr3   c                   n    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
dS )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)rW   rX   rY   r   r   r   r   r   IMAGENET1K_V1IMAGENET1K_QNNPACK_V1DEFAULT r3   r2   r    r       s        #Gc7.#>>>!. m5C##     
 
  0 $GGGr3   r    quantized_mobilenet_v3_large)name
pretrainedc                 ^    |                      dd          rt          j        nt          j        S )Nr~   F)rE   r    r   r   r   )r%   s    r2   <lambda>r      s,    ::j%((6:PP'5 r3   )r|   TF)r|   r}   r~   c                     |rt           nt                              |           } 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    r   verifyr   r   )r|   r}   r~   r%   rz   r{   s         r2   r!   r!      sa    Z 7?^22D^ffgnooG.@.`.`Y_.`.`+|8,QY[cnngmnnnr3   )-	functoolsr   typingr   r   r   r   rG   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   intr[   r   r    r!   r   r3   r2   <module>r      s3         - - - - - - - - - - - -          8 8 8 8 8 8 8 8 ? ? ? ? ? ? ? ? 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 ( ( ( ( ( ( C C C C C C C C              0 / / / / / / /  2
 2
 2
 2
 2
#4 2
 2
 2
j
! 
! 
! 
! 
!"2 
! 
! 
!% % % % %[ % % %:!#$:;!! k"! 	!
 ! ! ! ! ! !H$ $ $ $ $+ $ $ $8 3444	6 	6   ae	'o 'o 'oe?A[[\]'o 'o 	'o
 'o 'o 'o 'o  54'o 'o 'or3   