Ë
    g^(hp  ã            	       óò  — d dl mZ d dlmZmZ d dlZd dlmZmZm	Z	 d dl
mZmZmZ d dlmZ ddlmZ g d	¢Zd
„ Z G d„ de«      Zde	dee   deee	f   dee   fd„Zdedeeef   fd„Z G d„ de«      Z G d„ de«      Z G d„ de«      Z G d„ de«      Z G d„ de«      Z G d„ de«      Z  G d „ d!e«      Z! G d"„ d#e«      Z" G d$„ d%e«      Z# G d&„ d'e«      Z$ G d(„ d)e«      Z% G d*„ d+e«      Z& G d,„ d-e«      Z'y).é    )ÚABC)ÚCallableÚOptionalN)ÚBackendConfigÚDTypeConfigÚObservationType)ÚNodePatternÚPatternÚQuantizerCls)ÚNodeé   )Úall_node_args_have_no_tensors)ÚQuantizeHandlerÚBinaryOpQuantizeHandlerÚCatQuantizeHandlerÚConvReluQuantizeHandlerÚLinearReLUQuantizeHandlerÚBatchNormQuantizeHandlerÚEmbeddingQuantizeHandlerÚRNNDynamicQuantizeHandlerÚDefaultNodeQuantizeHandlerÚFixedQParamsOpQuantizeHandlerÚCopyNodeQuantizeHandlerÚ#GeneralTensorShapeOpQuantizeHandlerÚCustomModuleQuantizeHandlerÚStandaloneModuleQuantizeHandlerc                 óZ   — | €| S t        | t        «      s| d   } t        | t        «      sŒ| S )Néÿÿÿÿ)Ú
isinstancer   )Únode_patterns    úg/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/torch/ao/quantization/fx/quantize_handler.pyÚ_default_root_node_getterr"   #   s4   € ØÐØÐÜ˜¤tÔ,Ø# BÑ'ˆô ˜¤tÕ,àÐó    c                   ó|   — e Zd ZdZ	 	 	 ddedeeej                  j                  f   de
e   fd„Zdefd„Zd	„ Zd
„ Zy)r   z-Base handler class for the quantizer patternsNr    ÚmodulesÚroot_node_getterc                 ó¶  — || _         || _        |€t        } ||«      | _        || _        || _        d| _        t        | j                  t        «      r‡i }t        t        | j                  j                  «      «      D ]Y  }| j                  j                  |   }t        |t        «      sŒ-t        || j                  |«      rŒE| xj                  dz  c_        Œ[ yy)zWRecords pattern information in __init__, which will be used
        in convert
        Nr   r   )r    r%   r"   Ú	root_nodeÚis_custom_module_Úis_standalone_module_Únum_tensor_argsr   r   ÚrangeÚlenÚargsr   )	Úselfr    r%   r&   Úis_custom_moduleÚis_standalone_moduleÚcache_for_no_tensor_checkÚarg_idxÚargs	            r!   Ú__init__zQuantizeHandler.__init__/   sÆ   € ð )ˆÔØˆŒØÐ#Ü8ÐÙ)¨,Ó7ˆŒØ!1ˆÔØ%9ˆÔ"Ø ˆÔô �d—n‘n¤dÔ+Ø:<Ð%Ü ¤ T§^¡^×%8Ñ%8Ó!9Ó:ò .�Ø—n‘n×)Ñ)¨'Ñ2�Ü˜c¤4Õ(Ü5Ø˜TŸ\™\Ð+Dõð ×(Ò(¨AÑ-Ö(ñ.ð ,r#   Úreturnc                  ó   — y)aä  
        Returns True if the operator works for both floating point and
        quantized input, and does some computation based on the input Tensor,
        or the ops that only re-arranges the Tensor values or query some metadata
        about the Tensor
        so we need to insert observer/fake_quant for the output of the
        operator (same observer instance as input)
        since the distribution of values is different for input and output
        Tensors (for HistogramObserver) while they share the same quantization
        parameters
        Example operator: avgpool2d, reshape, transpose, maxpool2d
        Example observed operator:
        observer_0 - avgpool2d - observer_0 (same observer instance as input)
        F© ©r/   s    r!   Úis_general_tensor_value_opz*QuantizeHandler.is_general_tensor_value_opO   s   € ð r#   c                 ó   — | j                   S ©N)r)   r9   s    r!   r0   z QuantizeHandler.is_custom_module`   s   € Ø×%Ñ%Ð%r#   c                 ó   — | j                   S r<   )r*   r9   s    r!   r1   z$QuantizeHandler.is_standalone_modulec   s   € Ø×)Ñ)Ð)r#   )NFF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   ÚdictÚstrÚtorchÚnnÚModuler   r   r5   Úboolr:   r0   r1   r8   r#   r!   r   r   ,   s_   „ Ù7ð 04ØØ"ñ.à!ð.ð �c˜5Ÿ8™8Ÿ?™?Ð*Ñ+ð.ð # 8Ñ,ó	.ð@¨Dó ò"&ó*r#   r   Úobservation_typeÚdtype_configsÚ#num_tensor_args_to_observation_typer6   c                 ó2   ‡ ‡‡—  G ˆˆˆ fd„dt         «      }|S )zg
    Return a configurable QuantizeHandler that matches the given specifications from the backend.
    c                   ó|   •‡ — e Zd Z	 ddedeeej                  j                  f   de	e
   fˆ ˆˆˆfd„Zdefd„Zˆ xZS )ú>_get_quantize_handler_cls.<locals>.ConfigurableQuantizeHandlerr    r%   r&   c                 óÊ   •— t         ‰| �  |||«       ‰rA| j                  ‰v sJ d| j                  › d|› �«       ‚‰| j                     | _        ‰| _        y ‰| _        ‰| _        y )Nz7Must provide observation_type config for tensor number z, in num_tensor_args_to_observation_type for )Úsuperr5   r+   rH   rI   )r/   r    r%   r&   Ú	__class__rI   rJ   rH   s       €€€€r!   r5   zG_get_quantize_handler_cls.<locals>.ConfigurableQuantizeHandler.__init__q   s†   ø€ ô ‰GÑ˜\¨7Ð4DÔEÙ2Ø×+Ñ+Ð/RÑRð ØMÈd×NbÑNbÐMcØBÀ<À.ðRóÐRð )LØ×(Ñ(ñ)�Ô%ð
 "/ˆDÕð )9�Ô%Ø!.ˆDÕr#   r6   c                 ó<   — | j                   t        j                  k(  S r<   )rH   r   Ú OUTPUT_SHARE_OBSERVER_WITH_INPUTr9   s    r!   r:   zY_get_quantize_handler_cls.<locals>.ConfigurableQuantizeHandler.is_general_tensor_value_op„   s   € à×%Ñ%Ü"×CÑCñDðr#   r<   )r>   r?   r@   r	   rB   rC   rD   rE   rF   r   r   r5   rG   r:   Ú__classcell__)rP   rI   rJ   rH   s   @€€€r!   ÚConfigurableQuantizeHandlerrM   p   sQ   ù„ ð
 48ñ		/à%ð	/ð ˜#˜uŸx™xŸ™Ð.Ñ/ð	/ð ' xÑ0÷		/ð 	/ð&	°÷ 	r#   rT   )r   )rH   rI   rJ   rT   s   ``` r!   Ú_get_quantize_handler_clsrU   g   s   ú€ ÷ð ¤oô ð4 'Ð&r#   Úbackend_configc                 ó¶   — i }| j                   j                  «       D ]9  \  }}|j                  }|j                  }|j                  }t        |||«      ||<   Œ; |S )a#  
    Note: Quantize handler is just a holder for some check methods like
    (should_insert_observer_for_output), maybe this can be a enum as well,
    we can refactor this after we convert the path for fbgemm/qnnpack fully to the
    new path, this is not exposed to backend developers
    )Ú!_pattern_complex_format_to_configÚitemsrH   rI   Ú$_num_tensor_args_to_observation_typerU   )rV   Úpattern_to_quantize_handlersÚpatternÚconfigrH   rI   rJ   s          r!   Ú!_get_pattern_to_quantize_handlersr^   �   st   € ð $&Ð Ø)×KÑK×QÑQÓSò 
‰ˆ�Ø!×2Ñ2ÐØ×,Ñ,ˆà×7Ñ7ð 	,ô 1JØ˜mÐ-Pó1
Ð$ WÒ-ð
ð (Ð'r#   c                   ó   — e Zd Zy)r   N©r>   r?   r@   r8   r#   r!   r   r   ¥   ó   „ Ør#   r   c                   ó   — e Zd Zy)r   Nr`   r8   r#   r!   r   r   ©   ra   r#   r   c                   ó   — e Zd Zy)r   Nr`   r8   r#   r!   r   r   ®   ra   r#   r   c                   ó   — e Zd Zy)r   Nr`   r8   r#   r!   r   r   ³   ra   r#   r   c                   ó   — e Zd Zy)r   Nr`   r8   r#   r!   r   r   ¸   ra   r#   r   c                   ó   — e Zd Zy)r   Nr`   r8   r#   r!   r   r   ½   ra   r#   r   c                   ó   — e Zd Zy)r   Nr`   r8   r#   r!   r   r   Â   ra   r#   r   c                   ó   — e Zd ZdZy)r   zCCommon quantized op, first input and first output will be quantizedN)r>   r?   r@   rA   r8   r#   r!   r   r   Ç   s   „ ÚMr#   r   c                   ó   — e Zd Zy)r   Nr`   r8   r#   r!   r   r   Ì   ra   r#   r   c                   ó   — e Zd Zy)r   Nr`   r8   r#   r!   r   r   Ñ   ra   r#   r   c                   ó   — e Zd Zy)r   Nr`   r8   r#   r!   r   r   Ö   ra   r#   r   c                   ó   — e Zd Zy)r   Nr`   r8   r#   r!   r   r   Û   ra   r#   r   c                   ó   — e Zd Zy)r   Nr`   r8   r#   r!   r   r   à   ra   r#   r   )(Úabcr   Útypingr   r   rD   Ú$torch.ao.quantization.backend_configr   r   r   Útorch.ao.quantization.utilsr	   r
   r   Útorch.fx.graphr   Úutilsr   Ú__all__r"   r   ÚlistrB   ÚintÚtyperU   r^   r   r   r   r   r   r   r   r   r   r   r   r   r   r8   r#   r!   ú<module>rx      s;  ðå ß %ã ÷ñ ÷
 KÑ JÝ å 0ò€ò$ô8*�cô 8*ðv#'Ø%ð#'à˜Ñ$ð#'ð *.¨c°?Ð.BÑ)Cð#'ð 
ˆ/Ñó	#'ðL(Ø!ð(à	ˆ'�<Ð
Ñ ó(ô0	˜oô 	ô	˜ô 	ô
	˜oô 	ô
	 ô 	ô
	˜ô 	ô
	˜ô 	ô
	 ô 	ô
N ô Nô
	 Oô 	ô
	˜oô 	ô
	¨/ô 	ô
	 /ô 	ô
	 oõ 	r#   