
    `in                     r   d dl mZ d dlmZ d dlmZmZmZ d dlZd dl	m
Z
 d dlmZ d dlmZ d dlmZ d d	lmZmZmZmZ d d
lmZ d dlmZ dgZd Zd Zd Zd Zd Zd Z d Z!d Z"d Z#d Z$d Z%d Z&d Z'd Z(d Z)d Z*d Z+d Z,d Z-d  Z.d!ej/        j        fd"Z0e G d# d$                      Z1d%ed&efd'Z2dS )(    )	dataclass)partial)AnyCallableOptionalN)$_disable_aten_to_metadata_assertions)	out_dtype)quantized_decomposed_lib)_WrapperModule)"_get_aten_graph_module_for_pattern,_replace_literals_with_existing_placeholders'_replace_literals_with_new_placeholders"remove_tensor_overload_for_qdq_ops)GraphModule)replace_pattern reference_representation_rewritec                    t           j        j                            | ||||t           j                  }t           j        j                            |||||	t           j                  }t           j        j        j                            |||
          }t           j        j                            |||||t           j                  }|S N)	torchopsquantized_decomposeddequantize_per_tensorint8atenlineardefaultquantize_per_tensor)x_i8x_scalex_zero_pointx_quant_minx_quant_max	weight_i8weight_scaleweight_zero_pointweight_quant_minweight_quant_max	bias_fp32	out_scaleout_zero_pointout_quant_minout_quant_maxx_fp32weight_fp32out_fp32out_i8s                      /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torch/ao/quantization/pt2e/representation/rewrite.py_qdq_quantized_linearr2      s    " Y+AAg|[+uz F )0FF
 K y~$,,V[)LLHY+??)^]M5: F M    c                    t           j        j                            | ||          } t           j        j                            |||	          }|                     t           j                  }|                    t           j                  }t          t           j        j        j        j        t           j	        ||z
  ||z
  d           }||z  }t          t           j        j        j
        j        t           j	        |
|          }||z   }t          t           j        j        j        j        t           j	        |||z  |z            |z   }t           j        j                            |||                              t           j                  }|S r   )r   r   r   clamptoint16r	   r   r   int32divTensormulr   )r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   x_i16
weight_i16acc_i32
bias_scalebias_i32r0   s                        r1   _reference_quantized_linearrA   =   s@   ( 9>k;??D	$$Y0@BRSSIGGEK  Eek**J 	%&& G <'J+2EKJWWH G 	IN%Kl"Y.		
 	
 	  Y^!!'=-HHKKEJWWFMr3   c
                    t           j        j                            | |||t           j                  \  }
}t           j        j                            | |
|||t           j                  }t           j        j                            ||
|||t           j                  } t           j        j                            |||||t           j                  }t           j        j        j        	                    | ||	          }|S r   )
r   r   r   choose_qparamsr   r   r   r   r   r   )r-   r!   r"   x_epsr#   r$   r%   r&   r'   r(   r   r    r   r.   r/   s                  r1   _qdq_dynamic_quantized_linearrE   r   s     "I:II[% G\ 9)=={K D Y+AAg|[+uz F )0FF
 K y~$,,V[)LLHOr3   c
                 &   t           j        j                            | |||t           j                  \  }
}| |
z  } t          j        |           } |                     t           j                  }||z   }t          j        |||          }|                    t           j                  }t           j        j	                            |||          }|                    t           j
                  }|                    t           j
                  }t          t           j        j	        j        j        t           j        ||z
  ||z
  d           }|
|z  }t          t           j        j	        j        j        t           j        |	|          }||z   }||
|z  z  }|S Ndtype)r   r   r   rC   r   roundr6   r8   r5   r   r7   r	   r   r   r9   r:   )r-   r!   r"   rD   r#   r$   r%   r&   r'   r(   r   r    x_i32r   r<   r=   r>   r?   r@   r/   s                       r1   #_reference_dynamic_quantized_linearrL      sW    "I:II[% G\
 gF [  FIIEKI((EL EK{K88E88%*8%%D	$$Y0@BRSSIGGEK  Eek**J 	%&& G <'J+2EKJWWH G'L01HOr3   c                    ddg}ddg}ddg}d}ddg}d}t           j        j                            | ||||t           j                  }t           j        j                            |||||	t           j                  }t           j        j        j                            |||
||||||	  	        }t           j        j                            |||||t           j                  }|S N   r   F)	r   r   r   r   r   r   convolutionr   r   )r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   stridepaddingdilation
transposedoutput_paddinggroupsr-   r.   r/   r0   s                            r1   _qdq_quantized_conv2drW      s    " VF!fG1vHJVNFY+AAg|[+uz F )0FF
 K y~)11
 
H Y+??)^]M5: F Mr3   c                    ddg}ddg}ddg}d}ddg}d}t           j        j                            | ||          } t           j        j                            |||	          }|                     t           j                  }|                    t           j                  }t          t           j        j        j        j        t           j	        ||z
  ||z
  d ||||||          }||z  }t          t           j        j        j
        j        t           j	        |
|          }|                    d          }|                    d          }||z   }t          t           j        j        j        j        t           j	        |||z  |z            |z   }t           j        j                            |||                              t           j                  }|S )NrO   r   F)r   r   r   r5   r6   r7   r	   rP   r   r8   r9   r:   	unsqueezer;   r   )r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   rQ   rR   rS   rT   rU   rV   r<   r=   r>   r?   r@   r0   s                              r1   _reference_quantized_conv2dr[      s   " VF!fG1vHJVNF 9>k;??D	$$Y0@BRSSIGGEK  Eek**J 	"*&& G <'J +2EKJWWH !!"%%H!!"%%H G 	IN%Kl"Y.		
 	
 	  Y^!!'=-HHKKEJWWFMr3   c
                    t           j        j                            | ||||	t           j                  }
t           j        j                            |||||	t           j                  }|
|z   }t           j        j                            |          }t           j        j                            |||||	t           j                  }|S r   )r   r   r   r   r   r   relur   r   r   r    y_i8y_scaley_zero_pointr)   r*   	quant_min	quant_maxr-   y_fp32r/   r0   s                 r1   _qdq_quantized_add_relure   G  s     Y+AAg|Y	5: F Y+AAg|Y	5: F Hy~""8,,HY+??)^Y	5: F Mr3   c
                    |                      t          j                  }
|                     t          j                  }t          t          j        j        j        j        t          j        |
|z
  ||z            }
t          t          j        j        j        j        t          j        ||z
  ||z            }|
|z   |z   }t          j        j                            |||	                               t          j	                  }|S )z
    See comments for `_reference_quantized_add` for more information on
    how to derive the formula for out_i8 based on x_i8 and y_i8
    )
r6   r   r8   r	   r   r   r;   r:   r5   r   r   r   r    r_   r`   ra   r)   r*   rb   rc   rK   y_i32out_i32r0   s                 r1   _reference_quantized_add_relurj   a  s      GGEK  EGGEK  E	!			9		 E 	!			9		 E emn,GY^!!'>9EEHHTTFMr3   c
                 B   t           j        j                            | ||||	t           j                  }
t           j        j                            |||||	t           j                  }|
|z   }t           j        j                            |||||	t           j                  }|S r   )r   r   r   r   r   r   r^   s                 r1   _qdq_quantized_addrl     s     Y+AAg|Y	5: F Y+AAg|Y	5: F HY+??)^Y	5: F Mr3   c
                    |                      t          j                  }
|                     t          j                  }t          j        ||z  |
|z
  z                                 t          j                  }
t          j        ||z  ||z
  z                                 t          j                  }|
|z   |z   }d}d}	t          j        j                            |||	                               t          j                  }|S )a  
        # How to Derive the formula for out_i8 based on x_i8 and y_i8
        # (since quantized add takes x_i8, y_i8 and their quantization parameters, and produce an out_i8)

        # out_i8 is quantized output, we can write down the formula for it first:
    out_i8 = out_f32 / out_scale + out_zero_point           (1)

        # then out_fp32 is computed from x_f32 + y_f32, and the x_fp32 and y_fp32 are the dequantized x_i8 and y_i8
        out_f32 = x_f32 + y_f32           (2)
        x_fp32 = (x_i8 - x_zero_point) * x_scale         (3)
        y_fp32 = (y_i8 - y_zero_point) * y_scale         (4)

        # applying the above formula to the out_i8 equation we can get the following:
        out_i8 = out_fp32 / out_scale + out_zero_point             # (1)
           = (x_f32 + y_f32) / out_scale + out_zero_point      # applying (2) to substitute out_fp32 with x_fp32 + y_fp32
           = ((x_i8 - x_zero_point) * x_scale + (y_i8 - y_zero_point) * y_scale) / out_scale + out_zero_point  # apply (3) and (4)
       )r6   r   r8   rJ   r   r   r5   r   rg   s                 r1   _reference_quantized_addrp     s    : GGEK  EGGEK  EK9,1EFGGJJ5;WWEK9,1EFGGJJ5;WWEemn,GIIY^!!'9i@@CCEJOOFMr3   c	                 H   d}	d}
d}d}d}t           j        j                            | ||||t           j                  }t           j        j        j                            ||	|
|||          \  }}t           j        j                            |||||t           j                  }|S rN   )	r   r   r   r   r   r   max_pool2d_with_indicesr   r   )r   r   r    r!   r"   r)   r*   r+   r,   kernel_sizerQ   rR   rS   	ceil_moder-   r/   _r0   s                     r1   _qdq_quantized_max_pool2drv     s     KFGHIY+AAg|[+uz F ).8@@VWh	 KHa Y+??)^]M5: F Mr3   c	                 l   d}	d}
d}d}d}t          j        | ||          } |                     t           j                  }t           j        j        j                            ||z
  |	|
|||          \  }}|||z  z  |z   }t          j        |||          }|                    t           j                  }|S rN   )	r   r5   r6   r8   r   r   rr   r   r   )r   r   r    r!   r"   r)   r*   r+   r,   rs   rQ   rR   rS   rt   rK   ri   ru   r/   r0   s                      r1   _reference_quantized_max_pool2drx     s     KFGHI;t[+66DGGEK  E7??k67Hi JGQ 'I-.?H{8]MBBH[[$$FMr3   c                 l    t           j        j                            | ||||t           j                  }|S r   )r   r   r   r   r   r-   scale
zero_pointrb   rc   xs         r1   _quantize_per_tensor_int8r~      s2    	&::z9i	 	A Hr3   c                     | |z  }t          j        |          }|                    t           j                  }||z   }t          j        |||          }|                    t           j                  }|S rG   )r   rJ   r6   r8   r5   r   rz   s         r1   #_reference_quantize_per_tensor_int8r     sg     	A 	AA	5;A	JAAy),,A	5:AHr3   c                 l    t           j        j                            | ||||t           j                  }|S r   )r   r   r   r   r   )r   r{   r|   rb   rc   r-   s         r1   _dequantize_per_tensor_int8r     s2    Y+AAeZIuz F Mr3   c                     t           j        j                            | ||          } |                     t           j                  |z
  |z                      t           j                  S rG   )r   r   r   r5   r6   float32)r   r{   r|   rb   rc   s        r1   %_reference_dequantize_per_tensor_int8r     sR     9>i;;D WWU]##j0E9==EM=RRRr3   c           	      n    t           j        j                            | |||||t           j                  }|S r   )r   r   r   quantize_per_channelr   )r-   scaleszero_pointsch_axisrb   rc   r0   s          r1   _quantize_per_channel_int8r   +  s6     Y+@@WiEJ F Mr3   c                 N   t          j        | |d          } t           j        j                            t          j        | |z                                t           j                  |z   ||          }t          j        ||d          }|                    t           j                  S NrY   )	r   	transposer   r   r5   rJ   r6   r8   r   )r-   r   r   r   rb   rc   ri   s          r1   $_reference_quantize_per_channel_int8r   4  s     _VWb11Fin""FVO$$''44{BIy G ogw33G::ej!!!r3   c           	      n    t           j        j                            | |||||t           j                  }|S r   )r   r   r   dequantize_per_channelr   )r   r   r   r   rb   rc   r/   s          r1   _dequantize_per_channel_int8r   ?  s6     y-DDfk7Iy%* H Or3   c                 2   t           j        j                            | ||          } t          j        | |d          } |                     t           j                  }||z
                      t           j                  |z  }t          j        ||d          }|S r   )r   r   r   r5   r   r6   r8   float)r   r   r   r   rb   rc   rK   r/   s           r1   &_reference_dequantize_per_channel_int8r   I  s|     9>i;;D?4"--DGGEK  E#''44v=Hx"55HOr3   gmc                 0    t          | dgdddd          S )NrY            )rO   rn   ro   )exclude_literalsliteral_to_ph_idx)r   )r   s    r1   '_replace_ph_qdq_per_channel_replacementr   W  s,    7
bT!!5L5L   r3   c                       e Zd ZU dZeedf         ed<   eed<   eed<   dZe	ee
ge
f                  ed<   dZe	ee
ge
f                  ed<   dS )	_RewriteInfozData needed for rewrite, this includes example inputs, pattern and replacement functions
    and post transformation functions for the exported pattern and replacement GraphModule
    .example_inputspatternreplacementNpattern_post_transreplacement_post_trans)__name__
__module____qualname____doc__tupler   __annotations__r   r   r   r   r    r3   r1   r   r   ]  s          
 #s(O###IM;-*D!EFMMMMQHX{m[.H%IJQQQQQr3   r   modelreturnc                    t          j        dddt           j                  t          j        dt           j                  t          j        dt           j                  t          j        dgt           j                  t          j        dgt           j                  t          j        dddt           j                  t          j        dt           j                  t          j        dt           j                  t          j        dgt           j                  t          j        dgt           j                  t          j        dt           j                  t          j        dt           j                  t          j        dt           j                  t          j        dgt           j                  t          j        dgt           j                  f}t          j        dt           j                  ddt          j        t           j	                  j
        t          j        dddt           j                  t          j        dt           j                  t          j        dt           j                  t          j        dgt           j                  t          j        dgt           j                  t          j        dt           j                  f
}t          j        dddt           j                  t          j        dt           j                  t          j        dt           j                  t          j        dgt           j                  t          j        dgt           j                  t          j        dddt           j                  t          j        dt           j                  t          j        dt           j                  t          j        dgt           j                  t          j        dgt           j                  t          j        dt           j                  t          j        dt           j                  t          j        dt           j                  t          j        dgt           j                  t          j        dgt           j                  f}t          j        dddt           j                  t          j        dt           j                  t          j        dt           j                  t          j        dddt           j                  t          j        dt           j                  t          j        dt           j                  t          j        dt           j                  t          j        dt           j                  t          j        dgt           j                  t          j        dgt           j                  f
}t          j        dddt           j                  t          j        dt           j                  t          j        dt           j                  t          j        dgt           j                  t          j        dgt           j                  t          j        dt           j                  t          j        dt           j                  t          j        dgt           j                  t          j        dgt           j                  f	}t          j        dd	d	d	t           j                  t          j        dt           j                  t          j        dt           j                  t          j        dgt           j                  t          j        dgt           j                  f}t          j        dddt           j                  t          j        dt           j                  t          j        dt           j                  t          j        dgt           j                  t          j        dgt           j                  f}t          j        dd	d	d	t           j                  t          j        d	t           j                  t          j        d	t           j                  dddf}t          j        dddt           j                  t          j        d	t           j                  t          j        d	t           j                  dddf}	t          |t          t                    t          t                    t          t           dddd
t          j        t           j	                  j
        d	i          t          t           dddd
t          j        t           j	                  j
        d	i                    t          |t          t"                    t          t$                    t&          t&                    t          |t          t(                    t          t*                    t          t&          dg          t          t&          dg                    t          |t          t,                    t          t.                              t          |t          t0                    t          t2                              t          |t          t4                    t          t6                    t&          t&                    t          |t          t8                    t          t:                              t          |t          t<                    t          t>                              t          |t          t@                    t          tB                    tD          tD                    t          |	t          tF                    t          tH                    tD          tD                    g
}
tK          |            tM                      5  |
D ]}|j'        }|j(        }|j)        }|j*        }|j+        }tY          ||          }tK          |           tY          ||          }tK          |           |r ||          }|r ||          }|-                                 |-                                 t]          | ||           	 d d d            n# 1 swxY w Y   | S )Nrn   ro   )   r   rH   rO   )r   r   i)rO   r   r   r   r   r   )r   rY   )r   )/r   randintr   randnr   zerosinttensorfinfor   epsr   r   rE   rL   r   r   r2   rA   r   rW   r[   re   rj   rl   rp   rv   rx   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   	recompiler   )r    _QUANTIZED_LINEAR_EXAMPLE_INPUTS(_DYNAMIC_QUANTIZED_LINEAR_EXAMPLE_INPUTS _QUANTIZED_CONV2d_EXAMPLE_INPUTS)_QUANTIZED_ADD_OR_ADD_RELU_EXAMPLE_INPUTS$_QUANTIZED_MAX_POOL2D_EXAMPLE_INPUTS(_QUANTIZE_PER_TENSOR_INT8_EXAMPLE_INPUTS*_DEQUANTIZE_PER_TENSOR_INT8_EXAMPLE_INPUTS)_QUANTIZE_PER_CHANNEL_INT8_EXAMPLE_INPUTS+_DEQUANTIZE_PER_CHANNEL_INT8_EXAMPLE_INPUTS_REWRITE_INFO_LISTrewrite_infor   r   r   r   r   s                    r1   r   r   l  sC
   dCuz:::AU[)))AUY'''dV59---cU%),,,dCuz:::AU[)))AUY'''dV59---cU%),,,AU[)))AU[)))AUY'''dV59---cU%),,,($& 	F%+...EM""&dCuz:::AU[)))AUY'''dV59---cU%),,,AU[)))0, 	dCUZ@@@AU[)))AUY'''dV59---cU%),,,dCUZ@@@AU[)))AUY'''dV59---cU%),,,AU[)))AU[)))AUY'''dV59---cU%),,,($& 	dCUZ@@@AU[)))AUY'''dCUZ@@@AU[)))AUY'''AU[)))AUY'''dV59---cU%),,,1- 	dCUZ@@@AU[)))AUY'''dV59---cU%),,,AU[)))AUY'''dV59---cU%),,,
,( 	Aq!Qek222AU[)))AUY'''dV59---cU%),,,0, 	dCUZ@@@AU[)))AUY'''dV59---cU%),,,2. 	Aq!Qek222AU[)))AUY'''	1- 	dCUZ@@@AU[)))AUY'''	3/ 	4899>??<#'CEK4N4N4RTU"V   <#'CEK4N4N4RTU"V  	
 	
 	,01167733	
 	
 	,011677;rdSSS;rdSSS	
 	
 	5233899	
 	

 	5-..344	
 	

 	0455:;;33	
 	
 	4455>??	
 	

 	6677@AA	
 	

 	5566?@@33	
 	
 	7788ABB33	
 	
}EN 'u---	-	/	/ 9 9. 	9 	9L)8N"*G&2K!-!@%1%H"8.QQG.w777< K /{;;;! 6,,W55% B44[AA!!###E7K8888'	99 9 9 9 9 9 9 9 9 9 9 9 9 9 9, Ls   "B:s))s-0s-)3dataclassesr   	functoolsr   typingr   r   r   r   torch._export.utilsr   !torch._higher_order_ops.out_dtyper	   $torch.ao.quantization.fx._decomposedr
   'torch.ao.quantization.pt2e.export_utilsr    torch.ao.quantization.pt2e.utilsr   r   r   r   torch.fxr   torch.fx.subgraph_rewriterr   __all__r2   rA   rE   rL   rW   r[   re   rj   rl   rp   rv   rx   r~   r   r   r   r   r   r   r   fxr   r   r   r   r3   r1   <module>r      s   ! ! ! ! ! !       * * * * * * * * * *  D D D D D D 7 7 7 7 7 7 I I I I I I B B B B B B            !           6 6 6 6 6 6 '
     F2 2 2j  B, , ,^0 0 0fO O Od  4" " "J  2& & &R  8  8       
S 
S 
S  " " "    0D     R R R R R R R RLK LK L L L L L Lr3   