
    Vhm                     l   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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jZ                  j*                  fd!Z.e G d" d#             Z/d$ed%efd&Z0y)'    )	dataclass)partial)AnyCallableOptionalN)	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                  j                  | ||||t         j                        }t         j                  j                  j                  |||||	t         j                        }t         j                  j
                  j                  j                  |||
      }t         j                  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                      a/home/dcms/DCMS/lib/python3.12/site-packages/torch/ao/quantization/pt2e/representation/rewrite.py_qdq_quantized_linearr1      s    " YY++AAg|[+uzzF ))00FF

K yy~~$$,,V[)LHYY++??)^]M5::F M    c                    t         j                  j                  j                  | ||      } t         j                  j                  j                  |||	      }| j	                  t         j
                        }|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                  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_i32r/   s                        r0   _reference_quantized_linearr@   <   sW   ( 99>>k;?D		$$Y0@BRSIGGEKK Eekk*J 		%%&&G <'J++22EKKJWH G 	IINN%%KKl"Y.		
 	  YY^^!!'=-HKKEJJWFMr2   c
                 T   t         j                  j                  j                  | |||t         j                        \  }
}t         j                  j                  j                  | |
|||t         j                        }t         j                  j                  j                  ||
|||t         j                        } t         j                  j                  j                  |||||t         j                        }t         j                  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                  r0   _qdq_dynamic_quantized_linearrD   q   s     "II::II[%G\ 99))=={KD YY++AAg|[+uzzF ))00FF

K yy~~$$,,V[)LHOr2   c
                    t         j                  j                  j                  | |||t         j                        \  }
}| |
z  } t        j
                  |       } | j                  t         j                        }||z   }t        j                  |||      }|j                  t         j                        }t         j                  j                  j                  |||      }|j                  t         j                        }|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   rB   r   roundr5   r7   r4   r   r6   r   r   r   r8   r9   )r,   r    r!   rC   r"   r#   r$   r%   r&   r'   r   r   x_i32r   r;   r<   r=   r>   r?   r.   s                       r0   #_reference_dynamic_quantized_linearrK      sd    "II::II[%G\
 gF [[ FIIEKKI(EL EKK{K8E88%**8%D		$$Y0@BRSIGGEKK Eekk*J 		%%&&G <'J++22EKKJWH G'L01HOr2   c                    ddg}ddg}ddg}d}ddg}d}t         j                  j                  j                  | ||||t         j                        }t         j                  j                  j                  |||||	t         j                        }t         j                  j
                  j                  j                  |||
||||||	      }t         j                  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.   r/   s                            r0   _qdq_quantized_conv2drV      s    " VF!fG1vHJVNFYY++AAg|[+uzzF ))00FF

K yy~~))11
H YY++??)^]M5::F Mr2   c                    ddg}ddg}ddg}d}ddg}d}t         j                  j                  j                  | ||      } t         j                  j                  j                  |||	      }| j	                  t         j
                        }|j	                  t         j
                        }t        t         j                  j                  j                  j                  t         j                  ||z
  ||z
  d ||||||      }||z  }t        t         j                  j                  j                  j                  t         j                  |
|      }|j                  d      }|j                  d      }||z   }t        t         j                  j                  j                  j                  t         j                  |||z  |z        |z   }t         j                  j                  j                  |||      j	                  t         j                        }|S )NrN   r   F)r   r   r   r4   r5   r6   r   rO   r   r7   r8   r9   	unsqueezer:   r   )r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   rP   rQ   rR   rS   rT   rU   r;   r<   r=   r>   r?   r/   s                              r0   _reference_quantized_conv2drZ      s   " VF!fG1vHJVNF 99>>k;?D		$$Y0@BRSIGGEKK Eekk*J 		""**&&G <'J ++22EKKJWH !!"%H!!"%H G 	IINN%%KKl"Y.		
 	  YY^^!!'=-HKKEJJWFMr2   c
                    t         j                  j                  j                  | ||||	t         j                        }
t         j                  j                  j                  |||||	t         j                        }|
|z   }t         j                  j
                  j                  |      }t         j                  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.   r/   s                 r0   _qdq_quantized_add_relurd   F  s     YY++AAg|Y	5::F YY++AAg|Y	5::F Hyy~~""8,HYY++??)^Y	5::F Mr2   c
                 Z   | j                  t        j                        }
|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
                  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
    )
r5   r   r7   r   r   r   r:   r9   r4   r   r   r   r   r^   r_   r`   r(   r)   ra   rb   rJ   y_i32out_i32r/   s                 r0   _reference_quantized_add_reluri   `  s      GGEKK EGGEKK E		!!			9		E 		!!			9		E emn,GYY^^!!'>9EHHTFMr2   c
                 x   t         j                  j                  j                  | ||||	t         j                        }
t         j                  j                  j                  |||||	t         j                        }|
|z   }t         j                  j                  j                  |||||	t         j                        }|S r   )r   r   r   r   r   r   r]   s                 r0   _qdq_quantized_addrk     s     YY++AAg|Y	5::F YY++AAg|Y	5::F HYY++??)^Y	5::F Mr2   c
                    | j                  t        j                        }
|j                  t        j                        }t        j                  ||z  |
|z
  z        j                  t        j                        }
t        j                  ||z  ||z
  z        j                  t        j                        }|
|z   |z   }d}d}	t        j                  j
                  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 fomula 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)
       )r5   r   r7   rI   r   r   r4   r   rf   s                 r0   _reference_quantized_addro     s    : GGEKK EGGEKK EKK9,1EFGJJ5;;WEKK9,1EFGJJ5;;WEemn,GIIYY^^!!'9i@CCEJJOFMr2   c	                    d}	d}
d}d}d}t         j                  j                  j                  | ||||t         j                        }t         j                  j
                  j                  j                  ||	|
|||      \  }}t         j                  j                  j                  |||||t         j                        }|S rM   )	r   r   r   r   r   r   max_pool2d_with_indicesr   r   )r   r   r   r    r!   r(   r)   r*   r+   kernel_sizerP   rQ   rR   	ceil_moder,   r.   _r/   s                     r0   _qdq_quantized_max_pool2dru     s     KFGHIYY++AAg|[+uzzF ))..88@@VWh	KHa YY++??)^]M5::F Mr2   c	                    d}	d}
d}d}d}t        j                  | ||      } | j                  t         j                        }t         j                  j
                  j                  j                  ||z
  |	|
|||      \  }}|||z  z  |z   }t        j                  |||      }|j                  t         j                        }|S rM   )	r   r4   r5   r7   r   r   rq   r   r   )r   r   r   r    r!   r(   r)   r*   r+   rr   rP   rQ   rR   rs   rJ   rh   rt   r.   r/   s                      r0   _reference_quantized_max_pool2drw     s     KFGHI;;t[+6DGGEKK E77??k67HiJGQ 'I-.?H{{8]MBH[[$FMr2   c                 ~    t         j                  j                  j                  | ||||t         j                        }|S r   )r   r   r   r   r   r,   scale
zero_pointra   rb   xs         r0   _quantize_per_tensor_int8r}     s4    		&&::z9i	A Hr2   c                     | |z  }t        j                  |      }|j                  t         j                        }||z   }t        j                  |||      }|j                  t         j
                        }|S rF   )r   rI   r5   r7   r4   r   ry   s         r0   #_reference_quantize_per_tensor_int8r     sd     	A 	AA	5;;A	JAAy),A	5::AHr2   c                 ~    t         j                  j                  j                  | ||||t         j                        }|S r   )r   r   r   r   r   )r   rz   r{   ra   rb   r,   s         r0   _dequantize_per_tensor_int8r     s4    YY++AAeZIuzzF Mr2   c                     t         j                  j                  j                  | ||      } | j	                  t         j
                        |z
  |z  j	                  t         j
                        S rF   )r   r   r   r4   r5   float32)r   rz   r{   ra   rb   s        r0   %_reference_dequantize_per_tensor_int8r     sQ     99>>i;D WWU]]#j0E9==EMM=RRr2   c           	          t         j                  j                  j                  | |||||t         j                        }|S r   )r   r   r   quantize_per_channelr   )r,   scaleszero_pointsch_axisra   rb   r/   s          r0   _quantize_per_channel_int8r   *  s8     YY++@@WiEJJF Mr2   c                 ^   t        j                  | |d      } t         j                  j                  j	                  t        j
                  | |z        j                  t         j                        |z   ||      }t        j                  ||d      }|j                  t         j                        S NrX   )	r   	transposer   r   r4   rI   r5   r7   r   )r,   r   r   r   ra   rb   rh   s          r0   $_reference_quantize_per_channel_int8r   3  s}     __VWb1Fiinn""FVO$''4{BIyG oogw3G::ejj!!r2   c           	          t         j                  j                  j                  | |||||t         j                        }|S r   )r   r   r   dequantize_per_channelr   )r   r   r   r   ra   rb   r.   s          r0   _dequantize_per_channel_int8r   >  s8     yy--DDfk7Iy%**H Or2   c                 @   t         j                  j                  j                  | ||      } t        j                  | |d      } | j                  t         j                        }||z
  j                  t         j                        |z  }t        j                  ||d      }|S r   )r   r   r   r4   r   r5   r7   float)r   r   r   r   ra   rb   rJ   r.   s           r0   &_reference_dequantize_per_channel_int8r   H  sy     99>>i;D??4"-DGGEKK E#''4v=Hx"5HOr2   gmc                 (    t        | dgdddd      S )NrX            )rN   rm   rn   )exclude_literalsliteral_to_ph_idx)r   )r   s    r0   '_replace_ph_qdq_per_channel_replacementr   V  s    7
bT!!5L r2   c                   z    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<   y)	_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    r2   r0   r   r   \  s[    
 #s(O#IM;-*D!EFMMQHX{m[.H%IJQr2   r   modelreturnc                 D   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        |        |
D ]  }|jL                  }|jN                  }|jP                  }|jR                  }|jT                  }tW        ||      }tK        |       tW        ||      }tK        |       |r ||      }|r ||      }|jY                          |jY                          t[        | ||        | S )Nrm   rn   )   r   rG   rN   )r   r   i)rN   r   r   r   r   r   )r   rX   )r   ).r   randintr   randnr   zerosinttensorfinfor   epsr   r
   rD   rK   r   r   r1   r@   r   rV   rZ   rd   ri   rk   ro   ru   rw   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                    r0   r   r   k  s   dCuzz:AU[[)AUYY'dV599-cU%)),dCuzz:AU[[)AUYY'dV599-cU%)),AU[[)AU[[)AUYY'dV599-cU%)),($& 	F%++.EMM"&&dCuzz:AU[[)AUYY'dV599-cU%)),AU[[)0, 	dCUZZ@AU[[)AUYY'dV599-cU%)),dCUZZ@AU[[)AUYY'dV599-cU%)),AU[[)AU[[)AUYY'dV599-cU%)),($& 	dCUZZ@AU[[)AUYY'dCUZZ@AU[[)AUYY'AU[[)AUYY'dV599-cU%)),1- 	dCUZZ@AU[[)AUYY'dV599-cU%)),AU[[)AUYY'dV599-cU%)),
,( 	Aq!Qekk2AU[[)AUYY'dV599-cU%)),0, 	dCUZZ@AU[[)AUYY'dV599-cU%)),2. 	Aq!Qekk2AU[[)AUYY'	1- 	dCUZZ@AU[[)AUYY'	3/ 	489>?<#'CEKK4N4R4RTU"V <#'CEKK4N4R4RTU"V	
 	,016733	
 	,0167;rdS;rdS	
 	52389	

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

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

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

 	556?@33	
 	778AB33	
}EN 'u-* 5%44&&"..)<<!-!D!D4WnM*738nU*;7(1G!0=Kw4!5$ Lr2   )1dataclassesr   	functoolsr   typingr   r   r   r   !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__r1   r@   rD   rK   rV   rZ   rd   ri   rk   ro   ru   rw   r}   r   r   r   r   r   r   r   fxr   r   r   r   r2   r0   <module>r      s    !  * *  7 I B  ! 6 '
 F2jB,^0fOd4"J2&R88 
S"0D0D  R R RHK HK Hr2   