
    [ThX              
          S r SSKrSSKJrJr  SSKJr  SSKrSSKJ	r	J
r
JrJrJrJrJr  SSKJr  / SQrSS	S
\4S jrSS	S
\4S jrSS	S
\4S jrSS	S
\4S jr " S S\\5      r " S S\5      r " S S\5      r " S S\5      r\R9                  \SS\R:                  \R<                  SS9r \R9                  \SS\R@                  \RB                  SS9r" \R9                  \SSS\R:                  SS9r# \R9                  \S9r$\R9                  \
S9r%\$r&\%r'\R9                  \SS\R@                  \RP                  SSS 9r) \R9                  \\RT                  \R:                  SSSSS!9r+ \R9                  \\RT                  S\RX                  SS"9r-\R9                  \SS\R:                  \R<                  SS9r. \R9                  \SS\R:                  S#9r/ \R9                  \SS\R@                  \RB                  S$9r0 \R9                  \SS\R@                  \RP                  S$9r1 \R9                  \S%S\R@                  \RB                  S&S'9r2 \R9                  \S%S\R@                  \RP                  S&S'9r3 S( r4S) r5S* r6S+ r7S, r8g)-z6Implements modules  used to perform fake quantization.    N)ABCabstractmethod)Any)
_with_args)default_fixed_qparams_range_0to1_observer,default_fixed_qparams_range_neg1to1_observerFixedQParamsObserverHistogramObserverMovingAverageMinMaxObserver%MovingAveragePerChannelMinMaxObserver)Module)FakeQuantizeBaseFakeQuantizeFixedQParamsFakeQuantizeFusedMovingAvgObsFakeQuantizedisable_fake_quantdisable_observerenable_fake_quantenable_observerdefault_fake_quantdefault_weight_fake_quantdefault_dynamic_fake_quant.default_fixed_qparams_range_neg1to1_fake_quant+default_fixed_qparams_range_0to1_fake_quant*default_symmetric_fixed_qparams_fake_quant'default_affine_fixed_qparams_fake_quant%default_per_channel_weight_fake_quantdefault_embedding_fake_quant!default_embedding_fake_quant_4bitdefault_histogram_fake_quantdefault_fused_act_fake_quantdefault_fused_wt_fake_quant'default_fused_per_channel_wt_fake_quant(fused_wt_fake_quant_range_neg_127_to_1274fused_per_channel_wt_fake_quant_range_neg_127_to_127qschemeztorch.qschemereturnc                 f    U [         R                  [         R                  [         R                  4;   $ N)torchper_channel_symmetricper_channel_affine per_channel_affine_float_qparamsr&   s    [/var/www/auris/envauris/lib/python3.13/site-packages/torch/ao/quantization/fake_quantize.py_is_per_channelr0   2   s/    ##  ..      c                 H    U [         R                  [         R                  4;   $ r)   )r*   per_tensor_symmetricper_tensor_affiner.   s    r/   _is_per_tensorr5   :   s    u1153J3JKKKr1   c                 H    U [         R                  [         R                  4;   $ r)   )r*   r3   r+   r.   s    r/   _is_symmetric_quantr7   >   s    u1153N3NOOOr1   c                 *    U [         R                  4;   $ r)   )r*   r-   r.   s    r/   _is_float_qparamsr9   B   s    ..  r1   c                     ^  \ rS rSr% Sr\R                  \S'   \R                  \S'   SU 4S jjr\	S 5       r
\	S	 5       r\R                  R                  SS
\SS4S jj5       r\R                  R                  S 5       r\R                  R                  SS
\SS4S jj5       r\R                  R                  S 5       r\S 5       rSrU =r$ )r   H   a  Base fake quantize module.

Base fake quantize module
Any fake quantize implementation should derive from this class.

Concrete fake quantize module should follow the same API. In forward, they will update
the statistics of the observed Tensor and fake quantize the input. They should also provide a
`calculate_qparams` function that computes the quantization parameters given
the collected statistics.

fake_quant_enabledobserver_enabledr'   Nc                    > [         TU ]  5         U R                  S[        R                  " S/[        R
                  S95        U R                  S[        R                  " S/[        R
                  S95        g)z,Set fake_quant_enabled and observer_enabled.r<      dtyper=   N)super__init__register_bufferr*   tensoruint8)self	__class__s    r/   rC   FakeQuantizeBase.__init__X   sT     	15<<5;;3WX/qc1UVr1   c                     g r)    )rG   xs     r/   forwardFakeQuantizeBase.forwarda       r1   c                     g r)   rK   )rG   kwargss     r/   calculate_qparams"FakeQuantizeBase.calculate_qparamse   rO   r1   enabledc                 4    U(       a  SOSU R                   S'   g Nr?   r   )r<   rG   rT   s     r/   r   "FakeQuantizeBase.enable_fake_quanti   s    *1Qq"r1   c                 &    U R                  S5        g NF)r   rG   s    r/   r   #FakeQuantizeBase.disable_fake_quantm   s    u%r1   c                 4    U(       a  SOSU R                   S'   g rV   )r=   rW   s     r/   r    FakeQuantizeBase.enable_observerq   s    (/1Qa r1   c                 &    U R                  S5        g rZ   )r   r[   s    r/   r   !FakeQuantizeBase.disable_observeru   s    U#r1   c                 ,    [        U 40 UD6nSUl        U$ )Nz#torch.ao.quantization.fake_quantize)r   
__module__)clsrQ   fake_quant_constructors      r/   	with_argsFakeQuantizeBase.with_argsy   s$    !+C!:6!: -R)%%r1   rK   )r'   N)T)__name__rb   __qualname____firstlineno____doc__r*   Tensor__annotations__rC   r   rM   rR   jitexportboolr   r   r   r   classmethodre   __static_attributes____classcell__rH   s   @r/   r   r   H   s    
 $ll"W     YY9 9 9 9 YY& & YY7t 7t 7 7 YY$ $ & &r1   r   c                     ^  \ rS rSr% Sr\R                  \S'   \R                  \S'   \SSS4U 4S jjr	\R                  R                  S 5       rS	 r\R                  R                  S
 5       rU 4S jrU 4S jrSrU =r$ )r      a  Simulate the quantize and dequantize operations in training time.

The output of this module is given by::

    x_out = (
      clamp(round(x/scale + zero_point), quant_min, quant_max) - zero_point
    ) * scale

* :attr:`is_dynamic` indicates whether the fake quantie is a placeholder for dynamic quantization
  operators (choose_qparams -> q -> dq) or static quantization operators (q -> dq)

* :attr:`scale` defines the scale factor used for quantization.

* :attr:`zero_point` specifies the quantized value to which 0 in floating point maps to

* :attr:`fake_quant_enabled` controls the application of fake quantization on tensors, note that
  statistics can still be updated.

* :attr:`observer_enabled` controls statistics collection on tensors

* :attr:`dtype` specifies the quantized dtype that is being emulated with fake-quantization,
    allowable values are torch.qint8 and torch.quint8.

Args:

    observer (module): Module for observing statistics on input tensors and calculating scale
      and zero-point.
    observer_kwargs (optional): Arguments for the observer module

Attributes:
    activation_post_process (Module): User provided module that collects statistics on the input tensor and
      provides a method to calculate scale and zero-point.

scale
zero_pointNFc                 ~  > [         TU ]  5         Ub  Ub  X#::  d   S5       eUR                  S[        R                  5      n[        US5      (       a(  [        [        US0 5      S0 5      R                  SU5      n[        R                  " U5      R                  U::  d   S5       eU[        R                  " U5      R                  ::  d   S5       eUR                  X#S.5        XES'   U" S0 UD6U l        U R                  R                  U l        U R                  R                  U l        U R                  R                  U l        [        U R                  R                   5      (       a  [        R"                  nO[        R$                  nU R'                  S	[        R(                  " S
/[        R"                  S95        U R'                  S[        R(                  " S/US95        U R                  R*                  U l        U R                  R                   U l        [        U R                  S5      (       a  U R                  R,                  OSU l        [/        U R                   5      (       d7  [1        U R                   5      (       d   S[3        U R                   5      -   5       e[/        U R                   5      U l        g )Nz1quant_min must be less than or equal to quant_maxrA   pkeywordszquant_min out of boundzquant_max out of bound)	quant_min	quant_max
is_dynamicrv   g      ?r@   rw   r   ch_axiszYOnly per channel and per tensor quantization are supported in fake quantize got qscheme: rK   )rB   rC   getr*   quint8hasattrgetattriinfominmaxupdateactivation_post_processr{   r|   r}   r9   r&   floatintrD   rE   rA   r~   r0   r5   stris_per_channel)	rG   observerr{   r|   r}   observer_kwargsrA   zero_point_dtyperH   s	           r/   rC   FakeQuantize.__init__   s@    	 Y%:&CBC&#''>Ex%%  #r :JKOOU ;;u%))Y6P8PP6E 2 6 66P8PP6""#ST(2%'/'B/'B$ 55??55??66AAT99AABB${{$yyWellC5&LM\5<<CS+TU1177
33;; t33Y?? ((00 	
 t||,,t||0L0L 	
$,, 	
L
 .dll;r1   c                 6    U R                   R                  5       $ r)   r   rR   r[   s    r/   rR   FakeQuantize.calculate_qparams       ++==??r1   c                 $   U R                   S   S:X  Ga  U R                  UR                  5       5        U R                  5       u  p#UR	                  U R
                  R                  5      UR	                  U R                  R                  5      p2U R
                  R                  UR                  :w  aJ  U R
                  R                  UR                  5        U R                  R                  UR                  5        U R
                  R                  U5        U R                  R                  U5        U R                  S   S:X  a  U R                  (       ac  [        R                  " UU R
                  U R                  U R                  U R                  R                   U R                  R"                  5      nU$ [        R$                  " UU R
                  U R                  U R                  R                   U R                  R"                  5      nU$ )Nr   r?   )r=   r   detachrR   torv   devicerw   shaperesize_copy_r<   r   r*    fake_quantize_per_channel_affiner~   r{   r|   fake_quantize_per_tensor_affine)rG   X_scale_zero_points       r/   rM   FakeQuantize.forward   s     #q(((4"&"8"8":F"())DJJ,=,=">&&AK zz6<</

""6<<0''(9(9:JJV$OO!!+.""1%*""::JJOOLL00::00::   99JJOO00::00:: r1   c                    SU R                    SU R                   SU R                  R                   SU R                  R                   SU R
                   SU R                   SU R                   SU R                   S	U R                   3$ )
Nfake_quant_enabled=, observer_enabled=, quant_min=, quant_max=, dtype=
, qscheme=z
, ch_axis=, scale=, zero_point=)
r<   r=   r   r{   r|   rA   r&   r~   rv   rw   r[   s    r/   
extra_reprFakeQuantize.extra_repr  s     "$"9"9!::MdNcNcMd e55??@TMiMiMsMsLt uZZL
4<<.
4<<. QZZLdoo->@	
r1   c                 j   > [         TU ]  XU5        U R                  XS-   '   U R                  XS-   '   g Nrv   rw   )rB   _save_to_state_dictrv   rw   )rG   destinationprefix	keep_varsrH   s       r/   r    FakeQuantize._save_to_state_dict
  s6     	#KC(,

W$%-1__\)*r1   c           	      $  > SS/nU H  n	X)-   n
X;   a  X   nU	S:X  a&  U R                   R                  UR                  5        O-U	S:X  d   eU R                  R                  UR                  5        [        R
                  R                  5       (       aH  U	S:X  a  U R                   R                  U5        M  U	S:X  d   eU R                  R                  U5        M  M  U(       d  M  UR                  U
5        M     [        TU ])  UUUUUUU5        g r   )rv   r   r   rw   r*   rm   is_scriptingr   appendrB   _load_from_state_dict)rG   
state_dictr   local_metadatastrictmissing_keysunexpected_keys
error_msgslocal_statenamekeyvalrH   s               r/   r   "FakeQuantize._load_from_state_dict  s     -D-C  o
 7?JJ&&syy1<///OO++CII6 99))++w

((-#|333--c2 , ##C(-  . 	%	
r1   )r   r~   rA   r}   r   r&   r|   r{   )rg   rb   rh   ri   rj   r*   rk   rl   r   rC   rm   rn   rR   rM   r   r   r   rq   rr   rs   s   @r/   r   r      s    !F << -1<f YY@ @B YY
 
=,
 ,
r1   r   c                      ^  \ rS rSrSrU 4S jr\R                  R                  S 5       r	\R                  R                  S 5       r
SrU =r$ )r   i@  zSimulate quantize and dequantize in training time.

Simulate quantize and dequantize with fixed quantization
parameters in training time. Only per tensor quantization
is supported.
c                   > [         TU ]  US9  [        U R                  5      [        :X  d.   U R
                  R                   S[        R                   35       eXl        U R                  R                  U l        U R                  R                  U l	        [        U R                  5      (       d   S[        U R                  5      -   5       eg )Nr   z's observer must be a zWOnly per tensor quantization is supported FixedQParamsFakeQuantize module, got qscheme:)rB   rC   typer   r	   rH   rg   _observer_ctrrv   rw   r5   r&   r   )rG   r   rH   s     r/   rC   !FixedQParamsFakeQuantize.__init__I  s    (+--.2FF	]nn%%&&<=Q=Z=Z<[\	]F%1177
66AAdll++ 	
?$,, 	
+r1   c                 2    U R                   U R                  4$ r)   )rv   rw   r[   s    r/   rR   *FixedQParamsFakeQuantize.calculate_qparamsW  s    zz4??**r1   c                     SU R                    SU R                   SU R                   SU R                   SU R                   SU R
                  R                   SU R
                  R                   SU R                   3$ )	z:Define a string representation of the object's attributes.r   r   r   r   r   r   r   r   )	r<   r=   rv   rw   rA   r   r{   r|   r&   r[   s    r/   r   #FixedQParamsFakeQuantize.extra_repr[  s     "$"9"9!::MdNcNcMd eZZLdoo-> ?ZZLT-I-I-S-S,T U55??@
4<<.Z	
r1   )r   rv   rw   )rg   rb   rh   ri   rj   rC   r*   rm   rn   rR   r   rq   rr   rs   s   @r/   r   r   @  sI    
 YY+ + YY
 
r1   r   c                   :  ^  \ rS rSrSr\SS4S\S\S\S\S	S
4
U 4S jjjr\	R                  R                  S	\\	R                  \	R                  4   4S j5       r\	R                  R                  S	\4S j5       rS\	R                  S	\	R                  4S jrSrU =r$ )r   if  a  Define a fused module to observe the tensor.

Fused module that is used to observe the input tensor (compute min/max), compute
scale/zero_point and fake_quantize the tensor.
This module uses calculation similar MovingAverageMinMaxObserver for the inputs,
to compute the min/max values in order to compute the scale/zero_point.
The qscheme input in the observer is used to differentiate between symmetric/affine
quantization scheme.

The output of this module is given by
x_out = (clamp(round(x/scale + zero_point), quant_min, quant_max)-zero_point)*scale

Similar to :class:`~torch.ao.quantization.FakeQuantize`, and accepts the same attributes as the
base class.

r      r   r{   r|   r   r'   Nc                   > [         TU ]  " XU40 UD6  [        U R                  [        [
        45      (       d   S5       eU R                  S[        R                  " S/[        R                  S95        U R                  S[        R                  " S/[        R                  S95        [        U R                  R                  5      U l        g )NzLFused observer+fake_quant module only works with MovingAverageMinMaxObserverr<   r?   r@   r=   )rB   rC   
isinstancer   r   r   rD   r*   rE   longr7   r&   is_symmetric_quant)rG   r   r{   r|   r   rH   s        r/   rC   &FusedMovingAvgObsFakeQuantize.__init__x  s     	iK?K(((*OP
 
 	Z Z	Z 
 	15<<5::3VW/qc1TU"5((00#
r1   c                 6    U R                   R                  5       $ r)   r   r[   s    r/   rR   /FusedMovingAvgObsFakeQuantize.calculate_qparams  r   r1   c                 ,   SU R                    SU R                   SU R                   SU R                   SU R                   SU R
                  R                   SU R
                  R                   SU R                   S	U R
                  R                   3$ )
Nr   r   r   r   r   r   r   r   z, reduce_range=)
r<   r=   rv   rw   rA   r   r{   r|   r&   reduce_ranger[   s    r/   r   (FusedMovingAvgObsFakeQuantize.extra_repr  s     "$"9"9!::MdNcNcMd eZZLdoo->htzzl S55??@TMiMiMsMsLt u||nOD4P4P4]4]3^`	
r1   r   c                    [         R                  " UU R                  U R                  U R                  R
                  U R                  R                  U R                  U R                  U R                  R                  U R                  R                  U R                  R                  U R                  U R                  U R                  5      $ r)   )r*   fused_moving_avg_obs_fake_quantr=   r<   r   min_valmax_valrv   rw   averaging_constantr{   r|   r~   r   r   )rG   r   s     r/   rM   %FusedMovingAvgObsFakeQuantize.forward  s    44!!##((00((00JJOO((;;((22((22LL##
 	
r1   )r   )rg   rb   rh   ri   rj   r   r   r   rC   r*   rm   rn   tuplerk   rR   r   r   rM   rq   rr   rs   s   @r/   r   r   f  s    & 4	

 
 	

 
 

 
$ YY@5u||)C#D @ @ YY
C 
 

 
%,, 
 
r1   r   r   T)r   r{   r|   rA   r&   r   i   Fr?   )r   r{   r|   r}   rA   r   r   )r   r{   r|   rA   r&   r   r~   )r   r&   rA   r{   r|   r~   r   )r   r&   r~   rA   r   )r   r{   r|   rA   )r   r{   r|   rA   r&   ig      0?)r   r{   r|   rA   r&   epsc                     [        U [        R                  R                  5      (       aR  U R                  R
                  R                  SS5      S   n[        R                  " SSU5      nUS:H  =(       d    US:H  $ g)zFReturn true if given mod is an instance of FakeQuantize script module..r?   z\.___torch_mangle_\d+ z0torch.ao.quantization.fake_quantize.FakeQuantizezAtorch.ao.quantization.fake_quantize.FusedMovingAvgObsFakeQuantizeF)	r   r*   rm   RecursiveScriptModule_cqualified_namesplitresub)modsuffixr   s      r/   _is_fake_quant_script_moduler   H  sr    #uyy6677&&,,S!4Q7vv.F;FF SRS	

 r1   c                 p    [        U [        5      (       d  [        U 5      (       a  U R                  5         gg)zDisable fake quantization for the module.

Disable fake quantization for this module, if applicable. Example usage::

  # model is any PyTorch model
  model.apply(torch.ao.quantization.disable_fake_quant)

N)r   r   r   r   r   s    r/   r   r   V  s/     #'((,H,M,M  -Nr1   c                 p    [        U [        5      (       d  [        U 5      (       a  U R                  5         gg)zEnable fake quantization for the module.

Enable fake quantization for this module, if applicable. Example usage::

  # model is any PyTorch model
  model.apply(torch.ao.quantization.enable_fake_quant)

N)r   r   r   r   r   s    r/   r   r   c  s/     #'((,H,M,M -Nr1   c                 p    [        U [        5      (       d  [        U 5      (       a  U R                  5         gg)zDisable observation for this module.

Disable observation for this module, if applicable. Example usage::

  # model is any PyTorch model
  model.apply(torch.ao.quantization.disable_observer)

N)r   r   r   r   r   s    r/   r   r   p  s/     #'((,H,M,M -Nr1   c                 p    [        U [        5      (       d  [        U 5      (       a  U R                  5         gg)zEnable observation for this module.

Enable observation for this module, if applicable. Example usage::

  # model is any PyTorch model
  model.apply(torch.ao.quantization.enable_observer)

N)r   r   r   r   r   s    r/   r   r   }  s/     #'((,H,M,M -Nr1   )9rj   r   abcr   r   typingr   r*   torch.ao.quantization.observerr   r   r   r	   r
   r   r   torch.nnr   __all__ro   r0   r5   r7   r9   r   r   r   r   re   r   r4   r   qint8r3   r   r   r   r   r   r   r+   r   r-   r   quint4x2r   r    r!   r"   r#   r$   r%   r   r   r   r   r   rK   r1   r/   <module>r      s   = 	 #     8_  LO L LP PT P 4 7&sF 7&t{
# {
|#
| #
L@
L @
F "++(
,,## ,   )22(
++&& 3  
 *33(
,, 4   2J1S1S9 2T 2 . /G.P.P6 /Q / +
 3 + +V '(4(>(>2
++'' )? ) %  ,55222
,,  6   
 %1$:$:222
.. %; % !  ,55
,,##  6   
  =FF(
,,	  G   
 <EE(
++&& F   +H*Q*Q2
++'' +R + ' ,I+R+R(
++&& ,S , (
 "++6kk++ ,  5

!
 

r1   