
    sgSk                     N   d dl mZ d dlmZ d dlmZ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 e	j4                  ddde	j6                         e	j8                  de	j:                         e	j<                  de	j>                         e	j@                  dge	j>                         e	j@                  dge	j>                         e	j4                  ddde	j6                         e	j8                  de	j:                         e	j<                  de	j>                         e	j@                  dge	j>                         e	j@                  dge	j>                         e	j8                  de	j:                         e	j8                  de	j:                         e	j<                  de	j>                         e	j@                  dge	j>                         e	j@                  dge	j>                        fZ!d Z"d Z# e	j8                  de	j:                        dd e	jH                  e	jJ                        jL                   e	j4                  ddde	j6                         e	j8                  de	j:                         e	j<                  de	j>                         e	j@                  dge	j>                         e	j@                  dge	j>                         e	j8                  de	j:                        f
Z'd Z(d Z) e	j4                  ddde	j6                         e	j8                  de	j:                         e	j<                  de	j>                         e	j@                  dge	j>                         e	j@                  dge	j>                         e	j4                  ddde	j6                         e	j8                  de	j:                         e	j<                  de	j>                         e	j@                  dge	j>                         e	j@                  dge	j>                         e	j8                  de	j:                         e	j8                  de	j:                         e	j<                  de	j>                         e	j@                  dge	j>                         e	j@                  dge	j>                        fZ*d Z+d Z, e	j4                  ddde	j6                         e	j8                  de	j:                         e	j<                  de	j>                         e	j4                  ddde	j6                         e	j8                  de	j:                         e	j<                  de	j>                         e	j8                  de	j:                         e	j<                  de	j>                         e	j@                  dge	j>                         e	j@                  dge	j>                        f
Z-d Z.d Z/d Z0d Z1 e	j4                  ddde	j6                         e	j8                  de	j:                         e	j<                  de	j>                         e	j@                  dge	j>                         e	j@                  dge	j>                         e	j8                  de	j:                         e	j<                  de	j>                         e	j@                  dge	j>                         e	j@                  dge	j>                        f	Z2d Z3d Z4 e	j8                  dd d d e	j:                         e	j8                  de	j:                         e	j<                  de	j>                         e	j@                  dge	j>                         e	j@                  dge	j>                        fZ5d! Z6d" Z7 e	j4                  ddde	j6                         e	j8                  de	j:                         e	j<                  de	j>                         e	j@                  dge	j>                         e	j@                  dge	j>                        fZ8d# Z9d$ Z: e	j8                  dd d d e	j:                         e	j8                  d e	j:                         e	j<                  d e	j>                        dddfZ;d% Z<d& Z= e	j4                  ddde	j6                         e	j8                  d e	j:                         e	j<                  d e	j>                        dddfZ>d' Z?d( Z@d)e	j                  j,                  fd*ZBe G d+ d,             ZC eCe' ee(       ee)       eedddd- e	jH                  e	jJ                        jL                  d i.       eedddd- e	jH                  e	jJ                        jL                  d i.             eCe! ee"       ee#      ee       eCe* ee+       ee,       eed/g0       eed/g0             eCe- ee.       ee/             eCe- ee0       ee1             eCe2 ee3       ee4      ee       eCe5 ee6       ee7             eCe8 ee9       ee:             eCe; ee<       ee=      eBeB       eCe> ee?       ee@      eBeB      g
ZDd1ed2efd3ZEy)4    )	dataclass)partial)AnyCallableOptionalTupleN)	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_rewrite   )      dtype   )r   r   ic                    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                      d/var/www/html/venv/lib/python3.12/site-packages/torch/ao/quantization/pt2e/representation/rewrite.py_qdq_quantized_linearr9   ,   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/   r0   r1   r2   r3   x_i16
weight_i16acc_i32
bias_scalebias_i32r7   s                        r8   _reference_quantized_linearrH   O   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Mr:   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#   )r4   r(   r)   x_epsr*   r+   r,   r-   r.   r/   r&   r'   r%   r5   r6   s                  r8   _qdq_dynamic_quantized_linearrL      s     "II::II[%G\ 99))=={KD YY++AAg|[+uzzF ))00FF

K yy~~$$,,V[)LHOr:   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 Nr   )r   r   r   rJ   r    roundr=   r?   r<   r!   r>   r	   r"   r#   r@   rA   )r4   r(   r)   rK   r*   r+   r,   r-   r.   r/   r&   r'   x_i32r%   rC   rD   rE   rF   rG   r6   s                       r8   #_reference_dynamic_quantized_linearrQ      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Or:   )r      rR   rR   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 Nr   r   F)	r   r   r   r   r    r!   convolutionr#   r$   )r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   stridepaddingdilation
transposedoutput_paddinggroupsr4   r5   r6   r7   s                            r8   _qdq_quantized_conv2dr\      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 Mr:   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 )Nr   r   F)r   r   r!   r<   r=   r>   r	   rU   r#   r?   r@   rA   	unsqueezerB   r    )r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   rV   rW   rX   rY   rZ   r[   rC   rD   rE   rF   rG   r7   s                              r8   _reference_quantized_conv2dr`   (  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Mr:   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_pointr0   r1   	quant_min	quant_maxr4   y_fp32r6   r7   s                 r8   _qdq_quantized_add_relurj     s     YY++AAg|Y	5::F YY++AAg|Y	5::F Hyy~~""8,HYY++??)^Y	5::F Mr:   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
    )
r=   r   r?   r	   r   r!   rB   rA   r<   r    r%   r&   r'   rd   re   rf   r0   r1   rg   rh   rP   y_i32out_i32r7   s                 r8   _reference_quantized_add_reluro     s      GGEKK EGGEKK E		!!			9		E 		!!			9		E emn,GYY^^!!'>9EHHTFMr:   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$   rc   s                 r8   _qdq_quantized_addrq     s     YY++AAg|Y	5::F YY++AAg|Y	5::F HYY++??)^Y	5::F Mr:   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)
    r   r   )r=   r   r?   rO   r   r!   r<   r    rl   s                 r8   _reference_quantized_addrs     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Mr:   c	                    d}	d}
d}d}d}t         j                  j                  j                  | ||||t         j                        }t         j                  j
                  j                  j                  ||	|
|||      \  }}t         j                  j                  j                  |||||t         j                        }|S rT   )	r   r   r   r   r    r!   max_pool2d_with_indicesr#   r$   )r%   r&   r'   r(   r)   r0   r1   r2   r3   kernel_sizerV   rW   rX   	ceil_moder4   r6   _r7   s                     r8   _qdq_quantized_max_pool2dry     s     KFGHIYY++AAg|[+uzzF ))..88@@VWh	KHa YY++??)^]M5::F Mr:   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 rT   )	r   r<   r=   r?   r   r!   ru   r#   r    )r%   r&   r'   r(   r)   r0   r1   r2   r3   rv   rV   rW   rX   rw   rP   rn   rx   r6   r7   s                      r8   _reference_quantized_max_pool2dr{   2  s     KFGHI;;t[+6DGGEKK E77??k67HiJGQ 'I-.?H{{8]MBH[[$FMr:   rR   c                 ~    t         j                  j                  j                  | ||||t         j                        }|S r   )r   r   r   r$   r    r4   scale
zero_pointrg   rh   xs         r8   _quantize_per_tensor_int8r   W  s4    		&&::z9i	A Hr:   c                     | |z  }t        j                  |      }|j                  t         j                        }||z   }t        j                  |||      }|j                  t         j
                        }|S rN   )r   rO   r=   r?   r<   r    r}   s         r8   #_reference_quantize_per_tensor_int8r   ^  sd     	A 	AA	5;;A	JAAy),A	5::AHr:   c                 ~    t         j                  j                  j                  | ||||t         j                        }|S r   )r   r   r   r   r    )r%   r~   r   rg   rh   r4   s         r8   _dequantize_per_tensor_int8r   w  s4    YY++AAeZIuzzF Mr:   c                     t         j                  j                  j                  | ||      } | j	                  t         j
                        |z
  |z  j	                  t         j
                        S rN   )r   r   r!   r<   r=   float32)r%   r~   r   rg   rh   s        r8   %_reference_dequantize_per_tensor_int8r   ~  sQ     99>>i;D WWU]]#j0E9==EMM=RRr:   c           	          t         j                  j                  j                  | |||||t         j                        }|S r   )r   r   r   quantize_per_channelr    )r4   scaleszero_pointsch_axisrg   rh   r7   s          r8   _quantize_per_channel_int8r     s8     YY++@@WiEJJF Mr:   c                 ^   t        j                  | |d      } t         j                  j                  j	                  t        j
                  | |z        j                  t         j                        |z   ||      }t        j                  ||d      }|j                  t         j                        S Nr^   )	r   	transposer   r!   r<   rO   r=   r?   r    )r4   r   r   r   rg   rh   rn   s          r8   $_reference_quantize_per_channel_int8r     s}     __VWb1Fiinn""FVO$''4{BIyG oogw3G::ejj!!r:   c           	          t         j                  j                  j                  | |||||t         j                        }|S r   )r   r   r   dequantize_per_channelr    )r%   r   r   r   rg   rh   r6   s          r8   _dequantize_per_channel_int8r     s8     yy--DDfk7Iy%**H Or:   c                 @   t         j                  j                  j                  | ||      } t        j                  | |d      } | j                  t         j                        }||z
  j                  t         j                        |z  }t        j                  ||d      }|S r   )r   r   r!   r<   r   r=   r?   float)r%   r   r   r   rg   rh   rP   r6   s           r8   &_reference_dequantize_per_channel_int8r     sy     99>>i;D??4"-DGGEKK E#''4v=Hx"5HOr:   gmc                 (    t        | dgdddd      S )Nr^   rR      r   )r   r   r   )exclude_literalsliteral_to_ph_idx)r   )r   s    r8   '_replace_ph_qdq_per_channel_replacementr     s    7
bT!!5L r:   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__r   r   __annotations__r   r   r   r   r    r:   r8   r   r     s[    
 #s(O#IM;-*D!EFMMQHX{m[.H%IJQr:   r   r   )r   r^   )r   modelreturnc                    t        |        t        D ]  }|j                  }|j                  }|j                  }|j
                  }|j                  }t        ||      }t        |       t        ||      }t        |       |r ||      }|r ||      }|j                          |j                          t        | ||      } | S r   )
r   _REWRITE_INFO_LISTr   r   r   r   r   r   	recompiler   )r   rewrite_infor   r   r   r   r   matchess           r8   r   r   (  s    &u-* ?%44&&"..)<<!-!D!D4WnM*738nU*;7(1G!0=K!%+>!?" Lr:   )Fdataclassesr   	functoolsr   typingr   r   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__randintr    randnr   zerosinttensor _QUANTIZED_LINEAR_EXAMPLE_INPUTSr9   rH   finfor   eps(_DYNAMIC_QUANTIZED_LINEAR_EXAMPLE_INPUTSrL   rQ    _QUANTIZED_CONV2d_EXAMPLE_INPUTSr\   r`   )_QUANTIZED_ADD_OR_ADD_RELU_EXAMPLE_INPUTSrj   ro   rq   rs   $_QUANTIZED_MAX_POOL2D_EXAMPLE_INPUTSry   r{   (_QUANTIZE_PER_TENSOR_INT8_EXAMPLE_INPUTSr   r   *_DEQUANTIZE_PER_TENSOR_INT8_EXAMPLE_INPUTSr   r   )_QUANTIZE_PER_CHANNEL_INT8_EXAMPLE_INPUTSr   r   +_DEQUANTIZE_PER_CHANNEL_INT8_EXAMPLE_INPUTSr   r   fxr   r   r   r   r   r:   r8   <module>r      sS	   !  1 1  7 I B  ! 6 ' EMM$V5::6EKK%EKK#ELL$uyy)ELL#eii(EMM$V5::6EKK%EKK#ELL$uyy)ELL#eii(EKK%EKK%EKK#ELL$uyy)ELL#eii($  & F2l EKKekk*EKK""EMM$V5::6EKK%EKK#ELL$uyy)ELL#eii(EKK%, (B,` EMM$\<EKK%EKK#ELL$uyy)ELL#eii(EMM$\<EKK%EKK#ELL$uyy)ELL#eii(EKK%EKK%EKK#ELL$uyy)ELL#eii($  &0fOf EMM$\<EKK%EKK#EMM$\<EKK%EKK#EKK%EKK#ELL$uyy)ELL#eii(- )4"J2&T EMM$\<EKK%EKK#ELL$uyy)ELL#eii(EKK%EKK#ELL$uyy)ELL#eii(
( $8: EKK1a%++.EKK%EKK#ELL$uyy)ELL#eii(, (" EMM$\<EKK%EKK#ELL$uyy)ELL#eii(. *
S EKK1a%++.EKK%EKK#- )" EMM$\<EKK%EKK#/ +0D0D  R R R 045:;8#QQEMM0J0N0NPQR	
 	8#QQEMM0J0N0NPQR	
 (,-23// (,-2372$O72$O 1./45
 1)*/0
 ,0167// 001:;
 223<=
 112;<// 334=>//}E PK K r:   