
    Yh($                     
   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,dS )*    )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                   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      }/var/www/tools.fuzzalab.pt/emblema-extractor/venv/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)r/   r3   s     r1   forwardz$QuantizableSqueezeExcitation.forward&   s&    }  U!3!3U;;;r2   is_qatc                 0    t          | ddg|d           d S )Nfc1
activationTinplace)r   )r/   r9   s     r1   
fuse_modelz'QuantizableSqueezeExcitation.fuse_model)   s$    dUL164HHHHHHr2   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)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,   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(%%	
 	
 	
 	
 	
r2   r5   )__name__
__module____qualname___versionr   r*   r   r8   r   boolr?   rJ   __classcell__r0   s   @r1   r"   r"      s        H7c 7S 7T 7 7 7 7 7 7
<V < < < < <I I$ I4 I I I 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                      t                      j        |dt          i| t          j                                        | _        d S )Nse_layer)r)   r*   r"   r   r+   r,   skip_addr.   s      r1   r*   z$QuantizableInvertedResidual.__init__U   s>    $P)EPPPP4466r2   xc                     | j         r.| j                            ||                     |                    S |                     |          S r5   )use_res_connectra   addblockr/   rb   s     r1   r8   z#QuantizableInvertedResidual.forwardY   s?     	!=$$Q

1666::a== r2   )rV   rW   rX   r   r*   r   r8   r[   r\   s   @r1   r^   r^   S   st        7c 7S 7T 7 7 7 7 7 7! !F ! ! ! ! ! ! ! !r2   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      r1   r*   zQuantizableMobileNetV3.__init__a   s;     	$)&)))[[
"}}r2   rb   c                     |                      |          }|                     |          }|                     |          }|S r5   )rj   _forward_implrk   rg   s     r1   r8   zQuantizableMobileNetV3.forwardl   s8    JJqMMq!!LLOOr2   r9   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?   )r/   r9   mmodules_to_fuses       r1   r?   z!QuantizableMobileNetV3.fuse_modelr   s     	% 	%AAww...#&*q66Q;;4!::#8#8#**3///a&$GGGGGa888V$$$	% 	%r2   r5   )rV   rW   rX   r   r*   r   r8   r   rZ   r?   r[   r\   s   @r1   r   r   `   s        	%c 	%S 	%T 	% 	% 	% 	% 	% 	% F    % %$ %4 % % % % % % % %r2   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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   }   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%:::

Lr2   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)rV   rW   rX   r   r   r   r   r   IMAGENET1K_V1IMAGENET1K_QNNPACK_V1DEFAULT r2   r1   r   r      s        #Gc7.#>>>!. m5C##     
 
  0 $GGGr2   r   quantized_mobilenet_v3_large)name
pretrainedc                 ^    |                      dd          rt          j        nt          j        S )Nr}   F)rD   r   r   r   r   )r$   s    r1   <lambda>r      s,    zz*e,,:/EE+9 r2   )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$   ry   rz   s         r1   r    r       sa    ^ 7?^22D^ffgnooG.@.`.`Y_.`.`+|8,QY[cnngmn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      s-         ' ' ' ' ' ' ' ' ' '          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	
 	
	 	 	 ae	'o 'o 'oe?A[[\]'o 'o 	'o
 'o 'o 'o 'o	 	 54'o 'o 'or2   