
    sg              	          d dl mZ d dlmZmZmZ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DictListOptionalTypeN)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    \/var/www/html/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              ri }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"  dnnd+:<% T^^%8%8!9: .nn))'2c4(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 r2   s    r$   is_general_tensor_value_opz*QuantizeHandler.is_general_tensor_value_opO   s     r&   c                     | j                   S N)r,   r<   s    r$   r3   z QuantizeHandler.is_custom_module`   s    %%%r&   c                     | j                   S r?   )r-   r<   s    r$   r4   z$QuantizeHandler.is_standalone_modulec   s    )))r&   )NFF)__name__
__module____qualname____doc__r   r   strtorchnnModuler   r   r8   boolr=   r3   r4   r;   r&   r$   r   r   ,   s_    7 04".!. c588??*+. #8,	.@D "&*r&   r   observation_typedtype_configs#num_tensor_args_to_observation_typer9   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 )superr8   r.   rJ   rK   )r2   r#   r(   r)   	__class__rK   rL   rJ   s       r$   r8   zG_get_quantize_handler_cls.<locals>.ConfigurableQuantizeHandler.__init__q   s     G\74DE2++/RR MdNbNbMcB<.RR )L(()%
 "/D )9%!.Dr&   r9   c                 <    | j                   t        j                  k(  S r?   )rJ   r    OUTPUT_SHARE_OBSERVER_WITH_INPUTr<   s    r$   r=   zY_get_quantize_handler_cls.<locals>.ConfigurableQuantizeHandler.is_general_tensor_value_op   s    %%"CCDr&   r?   )rA   rB   rC   r   r   rE   rF   rG   rH   r   r   r8   rI   r=   __classcell__)rR   rK   rL   rJ   s   @r$   ConfigurableQuantizeHandlerrO   p   sQ    
 48		/%	/ #uxx./	/ 'x0		/ 	/&	 	r&   rV   )r   )rJ   rK   rL   rV   s   ``` r$   _get_quantize_handler_clsrW   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itemsrJ   rK   $_num_tensor_args_to_observation_typerW   )rX   pattern_to_quantize_handlerspatternconfigrJ   rK   rL   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rA   rB   rC   r;   r&   r$   r   r          r&   r   c                       e Zd Zy)r   Nrb   r;   r&   r$   r   r      rc   r&   r   c                       e Zd Zy)r   Nrb   r;   r&   r$   r   r      rc   r&   r   c                       e Zd Zy)r   Nrb   r;   r&   r$   r   r      rc   r&   r   c                       e Zd Zy)r   Nrb   r;   r&   r$   r   r      rc   r&   r   c                       e Zd Zy)r   Nrb   r;   r&   r$   r   r      rc   r&   r   c                       e Zd Zy)r   Nrb   r;   r&   r$   r   r      rc   r&   r   c                       e Zd ZdZy)r   zCCommon quantized op, first input and first output will be quantizedN)rA   rB   rC   rD   r;   r&   r$   r   r      s    Mr&   r   c                       e Zd Zy)r   Nrb   r;   r&   r$   r   r      rc   r&   r   c                       e Zd Zy)r   Nrb   r;   r&   r$   r   r      rc   r&   r   c                       e Zd Zy)r   Nrb   r;   r&   r$   r   r      rc   r&   r   c                       e Zd Zy)r   Nrb   r;   r&   r$   r   r      rc   r&   r   c                       e Zd Zy)r   Nrb   r;   r&   r$   r   r      rc   r&   r   )(abcr   typingr   r   r   r   r   rF   $torch.ao.quantization.backend_configr	   r
   r   torch.ao.quantization.utilsr   r   r   torch.fx.graphr   utilsr   __all__r%   r   intrW   r`   r   r   r   r   r   r   r   r   r   r   r   r   r   r;   r&   r$   <module>rx      s>    7 7  
 K J  0$8*c 8*v#'%#'$#' *.c?.B)C#' 
/	#'L(!(	'<
 (0	o 		 	
	o 	
	 	
	 	
	 	
	 	
N N
	O 	
	o 	
	/ 	
	/ 	
	o 	r&   