
    kh$                        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,y)*    )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                   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      g/var/www/teggl/fontify/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)r0   r4   s     r2   forwardz$QuantizableSqueezeExcitation.forward&   s"    }}  U!3U;;r3   is_qatc                 &    t        | ddg|d       y )Nfc1
activationTinplace)r   )r0   r:   s     r2   
fuse_modelz'QuantizableSqueezeExcitation.fuse_model)   s    dUL164Hr3   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)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,   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(-
 	%	
r3   r6   )__name__
__module____qualname___versionr   r+   r	   r9   r   boolr@   rK   __classcell__r1   s   @r2   r#   r#      sY    H7c 7S 7T 7
<V < <I$ I4 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                 v    t        |   |dt        i| t        j                  j                         | _        y )Nse_layer)r*   r+   r#   r   r,   r-   skip_addr/   s      r2   r+   z$QuantizableInvertedResidual.__init__U   s/    $P)EPP446r3   xc                     | j                   r+| j                  j                  || j                  |            S | j                  |      S r6   )use_res_connectrb   addblockr0   rc   s     r2   r9   z#QuantizableInvertedResidual.forwardY   s8    ==$$Q

166::a= r3   )rW   rX   rY   r   r+   r	   r9   r\   r]   s   @r2   r_   r_   S   s0    7c 7S 7T 7! !F !r3   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      r2   r+   zQuantizableMobileNetV3.__init__a   s)     	$)&)[
"}r3   rc   c                 l    | j                  |      }| j                  |      }| j                  |      }|S r6   )rk   _forward_implrl   rh   s     r2   r9   zQuantizableMobileNetV3.forwardl   s1    JJqMq!LLOr3   r:   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@   )r0   r:   mmodules_to_fuses       r2   r@   z!QuantizableMobileNetV3.fuse_modelr   s     	%AAw..#&*q6Q;4!:#8#**3/a&$Ga88V$	%r3   r6   )rW   rX   rY   r   r+   r	   r9   r   r[   r@   r\   r]   s   @r2   r   r   `   sG    	%c 	%S 	%T 	% F %$ %4 %r3   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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   }   s    fmSl9S5TU$!&)W\\)5LMjjI.G"#<lxRmxqwxE%
 	%--EEgN))%)>g44hSW4XY%%eT%:

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