
    sg<N                     4   d dl mZmZmZmZmZmZmZ d dlZd dl	m
c mc mZ d dlm
c mc mc mZ d dlmZ d dlmZ d dlmZ ej,                  ej,                  ej.                  ej.                  hZdeeeef   ee   f   dededee   fd	Zd
eeef   deeef   deeeeej6                  f   f   fdZ	 d+dej:                  deeef   deddfdZd+dej:                  dedeeef   fdZ G d dej:                        Z  G d de       Z! G d de       Z"dedefdZ#dedefdZ$ G d dej:                        Z%dej:                  dej:                  dee&   d eddf
d!Z'd" Z(e!d#d$ej:                  d%ej:                  dee&   deeef   fd&Z)dej:                  dej:                  deeeeej6                  f   f   fd'Z*e"dfdej:                  dej:                  ddfd(Z+e"dd)d$ej:                  d%ej:                  deeeeej6                  f   f   fd*Z,y),    )AnyCallableDictListOptionalSetUnionN)prepare)&get_default_compare_output_module_liststr_listkey_strpostfixreturnc                 j   |j                  d      }|d   |k(  rdj                  |j                  d      dd       }| D ]Z  }dj                  |j                  d      dd       }dj                  |j                  d      dd       }||k(  r|c S ||k(  sX|c S  |dk(  rdj                  |j                  d      dd       }t        |      dk(  ry | D ]Z  }dj                  |j                  d      dd       }dj                  |j                  d      dd       }||k(  r|c S ||k(  sX|c S  y y )N. r   _packed_params)splitjoinlen)r   r   r   	split_strmatch_strings2pattern1pattern2s           M/var/www/html/venv/lib/python3.12/site-packages/torch/ao/ns/_numeric_suite.py_find_matchr      sC   
 c"I}www}}S1!B78 	Bwwrxx}Qr23Hwwrxx}Qr23Hx'	x'		 &&777==#5a#;<L< A% 77288C=2#6777288C=2#678+I8+I     
float_dictquantized_dictc                    t         j                  j                  d       i }|D ]8  }t        | |d      }|i ||<   | |   ||   d<   ||   ||   d<   /t        | |d      }|i ||<   | |   ||   d<   ||   d   ||   d<   |j	                  d      }|d	   d
k(  sv|d   dk(  s|d   }dj                  |dd       }|dz   |z   }|dz   |z   }	|| v s|	| v si ||<   | |   ||   d<   ||   j                         d   d   d   j                         d   d   ||   d<   | |	   ||   d<   ||   j                         d   d   d   j                         d   d   ||   d<   ; |S )aG  Compare the weights of the float module with its corresponding quantized
    module. Return a dict with key corresponding to module names and each entry being
    a dictionary with two keys 'float' and 'quantized', containing the float and
    quantized weights. This dict can be used to compare and compute the quantization
    error of the weights of float and quantized models.

    Example usage::

        wt_compare_dict = compare_weights(
            float_model.state_dict(), qmodel.state_dict())
        for key in wt_compare_dict:
            print(
                key,
                compute_error(
                    wt_compare_dict[key]['float'],
                    wt_compare_dict[key]['quantized'].dequantize()
                )
            )

    Args:
        float_dict: state dict of the float model
        quantized_dict: state dict of the quantized model

    Return:
        weight_dict: dict with key corresponding to module names and each entry being
        a dictionary with two keys 'float' and 'quantized', containing the float and
        quantized weights
    z/quantization_api._numeric_suite.compare_weightsweightNfloat	quantizedr   r   r   r   param_all_weight_valuesr   z.weight_ih_lz.weight_hh_l      )torch_C_log_api_usage_oncer   r   r   __getstate__)
r!   r"   weight_dictkey	match_keyr   layermodule_namefloat_weight_ih_keyfloat_weight_hh_keys
             r   compare_weightsr7   7   s   > 
HH  !RS#%K 
C:	 !K(29(=KW%,:3,?K[)  
C1AB	 !K(29(=KW%,:3,?,BK[) IIcN	R=G#	"9M(MbME((9Sb>2K"-">"F"-">"F"j05HJ5V#%C ,67J,KC )"3'446q9!<Q?LLNqQRST C - -77J,KC )"3'446q9!<Q?LLNqQRST C -;B r    modtarget_dictprefixc                     d }| j                         D ]/  \  }}t        |t              s|j                  | ||      dz   <    n | j                         D ]!  \  }}|r ||      |z   n|}t	        |||       # y)zThis is the helper function for get_logger_dict

    Args:
        mod: module we want to save all logger stats
        prefix: prefix for the current module
        target_dict: the dictionary used to save all logger stats
    c                     | dk(  r| S | dz   S )Nr   r    )r:   s    r   
get_prefixz+_get_logger_dict_helper.<locals>.get_prefix   s    2v76C<7r    statsN)named_children
isinstanceLoggerr?   _get_logger_dict_helper)r8   r9   r:   r>   namechildmodule_prefixs          r   rC   rC   |   s    8 ))+ eeV$8=K
6*W45
 ))+ Ce5;
6*T1{MBCr    c                 b    t         j                  j                  d       i }t        | ||       |S )a  Traverse the modules and save all logger stats into target dict.
    This is mainly used for quantization accuracy debug.

    Type of loggers supported:
        ShadowLogger: used to log the outputs of the quantized module and its matching float shadow module,
        OutputLogger: used to log the outputs of the modules

    Args:
        mod: module we want to save all logger stats
        prefix: prefix for the current module

    Return:
        target_dict: the dictionary used to save all logger stats

    z/quantization_api._numeric_suite.get_logger_dict)r,   r-   r.   rC   )r8   r:   r9   s      r   get_logger_dictrH      s.      
HH  !RS#%KCf5r    c                   (     e Zd ZdZ fdZd Z xZS )rB   zBase class for stats loggingc                 Z    t         |           i | _        t        j                  | _        y N)super__init__r?   r,   quint8dtypeself	__class__s    r   rM   zLogger.__init__   s"    
 \\
r    c                      y)	
        Nr=   rQ   xs     r   forwardzLogger.forward   s    r    __name__
__module____qualname____doc__rM   rW   __classcell__rR   s   @r   rB   rB      s    '"r    rB   c                   (     e Zd ZdZ fdZd Z xZS )ShadowLoggerz^Class used in Shadow module to record the outputs of the original and
    shadow modules.
    c                 ^    t         |           g | j                  d<   g | j                  d<   y )Nr%   r&   rL   rM   r?   rP   s    r   rM   zShadowLogger.__init__   s*     

7"$

;r    c                     t        |      dkD  r|d   }t        |      dkD  r|d   }| j                  d   j                  |j                                | j                  d   j                  |j                                y)rT   r+   r   r&   r%   N)r   r?   appenddetach)rQ   rV   ys      r   rW   zShadowLogger.forward   sf    
 q6A:!Aq6A:!A

;&&qxxz2

7""188:.r    rX   r^   s   @r   r`   r`      s    %

/r    r`   c                   (     e Zd ZdZ fdZd Z xZS )OutputLoggerz+Class used to log the outputs of the modulec                 @    t         |           g | j                  d<   y )N
tensor_valrb   rP   s    r   rM   zOutputLogger.__init__   s    #%

< r    c                 B    | j                   d   j                  |       |S )rT   rj   )r?   rd   rU   s     r   rW   zOutputLogger.forward   s     
 	

< ''*r    rX   r^   s   @r   rh   rh      s    6&r    rh   tc                 d    t        |       t        u r| D cg c]  }t        |       c}S | S c c}w rK   )typetuple_convert_tuple_to_listrl   rV   s     r   rp   rp      s,    59!W5Eq1!"1%1L1L1s   -c                     t        |       t        u r| D cg c]  }t        |       c}S | j                  r| j	                         S | S c c}w rK   )rn   list_dequantize_tensor_listis_quantized
dequantizerq   s     r   rt   rt      sP     7d? .//	 	#/ >> \\^
 /s   A	c                       e Zd ZdZ fdZdej                  fdZdej                  dej                  dej                  fdZdej                  de	dej                  fdZ
dej                  dej                  dej                  fd	Zdej                  de	dej                  fd
Zddeej                     dedej                  fdZdej                  dej                  dej                  fdZ xZS )Shadowa  Shadow module attaches the float module to its matching quantized module
    as the shadow. Then it uses Logger module to process the outputs of both
    modules.

    Args:
        q_module: module quantized from float_module that we want to shadow
        float_module: float module used to shadow q_module
        logger_cls: type of logger used to process the outputs of q_module and
            float_module. ShadowLogger or custom loggers can be used.
    c                     t         |           || _        || _        t	        j
                         | _         |       | _        y rK   )rL   rM   orig_moduleshadow_modulennq
DeQuantizedequantlogger)rQ   q_modulefloat_module
logger_clsrR   s       r   rM   zShadow.__init__   s5    #)~~' lr    r   c                     t        |      } | j                  | }t        |      } | j                  | }| j	                  ||       |S rT   )rp   rz   rt   r{   r   )rQ   rV   xloutputxl_floatshadow_outputs         r   rW   zShadow.forward  sO    
 $A&!!!2&*2.***H5FM*r    rV   rf   c                     | j                   j                  ||      }|j                         }|j                         }| j                  j                  ||      }| j	                  ||       |S r   )rz   addrv   r{   r   rQ   rV   rf   r   r   s        r   r   z
Shadow.add  ]    
 !!%%a+LLNLLN**..q!4FM*r    c                     | j                   j                  ||      }|j                         }| j                  j                  ||      }| j	                  ||       |S r   )rz   
add_scalarrv   r{   r   r   s        r   r   zShadow.add_scalar  R    
 !!,,Q2LLN**55a;FM*r    c                     | j                   j                  ||      }|j                         }|j                         }| j                  j                  ||      }| j	                  ||       |S r   )rz   mulrv   r{   r   r   s        r   r   z
Shadow.mul*  r   r    c                     | j                   j                  ||      }|j                         }| j                  j                  ||      }| j	                  ||       |S r   )rz   
mul_scalarrv   r{   r   r   s        r   r   zShadow.mul_scalar6  r   r    dimc                     | j                   j                  ||      }|D cg c]  }|j                          }}| j                  j                  ||      }| j	                  ||       |S c c}w r   )rz   catrv   r{   r   )rQ   rV   r   r   rf   r   s         r   r   z
Shadow.catA  se    
 !!%%a-%&'Q\\^''**..q#6FM* (s   A*c                     | j                   j                  ||      }|j                         }|j                         }| j                  j                  ||      }| j	                  ||       |S r   )rz   add_relurv   r{   r   r   s        r   r   zShadow.add_reluL  s]    
 !!**1a0LLNLLN**33Aq9FM*r    )r   )rY   rZ   r[   r\   rM   r,   TensorrW   r   r%   r   r   r   r   intr   r   r]   r^   s   @r   rx   rx      s    	#
U\\ 

U\\ 
ell 
u|| 
	ELL 	U 	u|| 	
U\\ 
ell 
u|| 
	ELL 	U 	u|| 		T%,,' 	c 	%,, 	
%,, 
5<< 
ELL 
r    rx   r   r   module_swap_listr   c                    t         j                  j                  d       i }| j                         D ]
  \  }}|||<    i }|j                         D ]U  \  }}||vr||   }t	        |      |vrt        ||||       t	        |      |v s9t        ||      rFt        |||      ||<   W |j                         D ]  \  }	}
|
|j                  |	<    y)a  Prepare the model by attaching the float module to its matching quantized
    module as the shadow if the float module type is in module_swap_list.

    Example usage::

        prepare_model_with_stubs(float_model, q_model, module_swap_list, Logger)
        q_model(data)
        ob_dict = get_logger_dict(q_model)

    Args:
        float_module: float module used to generate the q_module
        q_module: module quantized from float_module
        module_swap_list: list of float module types to attach the shadow
        logger_cls: type of logger to be used in shadow module to process the outputs of
            quantized module and its float shadow module
    z8quantization_api._numeric_suite.prepare_model_with_stubsN)
r,   r-   r.   r@   rn   prepare_model_with_stubs_is_identical_module_typerx   items_modules)r   r   r   r   float_module_childrenrD   r8   reassign	float_modr1   values              r   r   r   Y  s    , 
HH  B !002 *	c&)d#* H,,. @	c,,)$/		?"22$Y5EzR 	?..7P8
 $CJ?HTN@  nn& '
U!&#'r    c                     | j                         D cg c]  }t        |       }}|j                         D cg c]  }t        |       }}||k(  S c c}w c c}w rK   )modulesrn   )mod1mod2r8   mod1_module_typesmod2_module_typess        r   r   r     sR    .2lln=sc==.2lln=sc== 111 >=s
   AA)r   float_modelq_modelc                    t         j                  j                  d       t        | |||        ||  t	        |      }|S )a  Compare quantized module in a model with its floating point counterpart,
    feeding both of them the same input. Return a dict with key corresponding to
    module names and each entry being a dictionary with two keys 'float' and
    'quantized', containing the output tensors of quantized and its matching
    float shadow module. This dict can be used to compare and compute the module
    level quantization error.

    This function first call prepare_model_with_stubs() to swap the quantized
    module that we want to compare with the Shadow module, which takes quantized
    module, corresponding float module and logger as input, and creates a forward
    path inside to make the float module to shadow quantized module sharing the
    same input. The logger can be customizable, default logger is ShadowLogger
    and it will save the outputs of the quantized module and float module that
    can be used to compute the module level quantization error.

    Example usage::

        module_swap_list = [torchvision.models.quantization.resnet.QuantizableBasicBlock]
        ob_dict = compare_model_stub(float_model,qmodel,module_swap_list, data)
        for key in ob_dict:
            print(key, compute_error(ob_dict[key]['float'], ob_dict[key]['quantized'].dequantize()))

    Args:
        float_model: float model used to generate the q_model
        q_model: model quantized from float_model
        module_swap_list: list of float module types at which shadow modules will
            be attached.
        data: input data used to run the prepared q_model
        logger_cls: type of logger to be used in shadow module to process the outputs of
            quantized module and its float shadow module
    z2quantization_api._numeric_suite.compare_model_stub)r,   r-   r.   r   rH   )r   r   r   r   dataob_dicts         r   compare_model_stubr     s=    L 
HH  !UV['3CZPTNg&GNr    c                 $   t         j                  j                  d       t        |       }t        |      }i }|D ]S  }t	        ||   d         dk(  rt        t        |d      |d      }|3i ||<   ||   d   ||   d<   ||   d   ||   d<   U |S )	a  Find the matching activation between float and quantized modules.

    Args:
        float_module: float module used to generate the q_module
        q_module: module quantized from float_module

    Return:
        act_dict: dict with key corresponding to quantized module names and each
        entry being a dictionary with two keys 'float' and 'quantized', containing
        the matching float and quantized activations
    z8quantization_api._numeric_suite.get_matching_activationsrj   r   T)reverser?   r%   r&   )r,   r-   r.   rH   r   r   sorted)r   r   r!   r"   act_dictr1   r2   s          r   get_matching_activationsr     s     
HH  B !.J$X.N "H K~c"<01Q6z4 @#wO	 HSM%/	%:<%HHSM'")7)<\)JHSM+&K Or    c                    t         j                  j                  d       |
t               }t         j                  j
                  j                  |d      }|| _        t        | d|i        ||_        t        |d|t        i        y)a  Prepare the model by attaching the logger to both float module
    and quantized module if they are in the allow_list.

    Args:
        float_module: float module used to generate the q_module
        q_module: module quantized from float_module
        logger_cls: type of logger to be attached to float_module and q_module
        allow_list: list of module types to attach logger
    z5quantization_api._numeric_suite.prepare_model_outputsN)
activationr$   T)inplace
allow_listprepare_custom_config_dict)r   r   observer_non_leaf_module_listr   )
r,   r-   r.   r   aoquantizationQConfigqconfigr
   *NON_LEAF_MODULE_TO_ADD_OBSERVER_ALLOW_LIST)r   r   r   r   qconfig_debugs        r   prepare_model_outputsr     s     
HH  ? ;=
HH))11ZPT1UM(LdzVX %H&P#%r    )r   r   c                    t         j                  j                  d       |
t               }t	        | |||        | |   ||  t        | |      }|S )aU  Compare output activations between float and quantized models at
    corresponding locations for the same input. Return a dict with key corresponding
    to quantized module names and each entry being a dictionary with two keys
    'float' and 'quantized', containing the activations of quantized model and
    float model at matching locations. This dict can be used to compare and
    compute the propagation quantization error.

    Example usage::

        act_compare_dict = compare_model_outputs(float_model, qmodel, data)
        for key in act_compare_dict:
            print(
                key,
                compute_error(
                    act_compare_dict[key]['float'],
                    act_compare_dict[key]['quantized'].dequantize()
                )
            )

    Args:
        float_model: float model used to generate the q_model
        q_model: model quantized from float_model
        data: input data used to run the prepared float_model and q_model
        logger_cls: type of logger to be attached to float_module and q_module
        allow_list: list of module types to attach logger

    Return:
        act_compare_dict: dict with key corresponding to quantized module names
        and each entry being a dictionary with two keys 'float' and 'quantized',
        containing the matching float and quantized activations
    z5quantization_api._numeric_suite.compare_model_outputs)r,   r-   r.   r   r   r   )r   r   r   r   r   act_compare_dicts         r   compare_model_outputsr     sZ    L 
HH  ? ;=
+w
JGTN/WEr    )r   )-typingr   r   r   r   r   r   r	   r,   torch.ao.nn.quantizedr   nnr&   r|   torch.ao.nn.quantized.dynamicdynamicnnqdtorch.nntorch.ao.quantizationr
   +torch.ao.quantization.quantization_mappingsr   LinearLSTMr   strr   r   r7   ModulerC   rH   rB   r`   rh   rp   rt   rx   rn   r   r   r   r   r   r   r=   r    r   <module>r      s   B B B  # # , ,  ) 	KKJJIIGG	. *DcNDI-.  c]	BBS#XB04S#XB	#tC%&
&'BP C	Cc3hC C 
	C4 C c4i .RYY $/6 /.6  Mc Mc Ms s bRYY bJ0'))0'ii0' $i0' 	0'
 
0'f2 **YY* $i* 
#t)_*Z))ii 
#tC%&
&'F 	!))!ii!
 
!P //YY/ 
#tC%&
&'/r    