
    shG-                     8   S SK Jr  S SKr/ SQr " S S\R                  R
                  5      r " S S\R                  R                  5      r " S S	\R                  R                  5      r " S
 S\R                  R                  5      r	 " S S\R                  R                  5      r
 " S S\R                  R                  5      r " S S\R                  R                  R                  R                  5      r " S S\R                  R                  5      rg)    )warnN)ReLU6	HardswishELU	LeakyReLUSigmoidSoftmaxMultiheadAttentionPReLUc                   P   ^  \ rS rSrSrSU 4S jjrS rS r\SS j5       r	Sr
U =r$ )	r      a  Applies the element-wise function:

:math:`\text{ReLU6}(x) = \min(\max(x_0, x), q(6))`, where :math:`x_0` is the
zero_point, and :math:`q(6)` is the quantized representation of number 6.

Args:
    inplace: can optionally do the operation in-place. Default: ``False``

Shape:
    - Input: :math:`(N, *)` where `*` means, any number of additional
      dimensions
    - Output: :math:`(N, *)`, same shape as the input

.. image:: ../scripts/activation_images/ReLU6.png

Examples::

    >>> m = nn.quantized.ReLU6()
    >>> input = torch.randn(2)
    >>> # xdoctest: +SKIP
    >>> input = torch.quantize_per_tensor(input, 1.0, 0, dtype=torch.qint32)
    >>> output = m(input)
c                 0   > [         TU ]  U5        Xl        g N)super__init__inplace)selfr   	__class__s     |/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/torch/ao/nn/quantized/modules/activation.pyr   ReLU6.__init__,   s    !    c                 h    [         R                  R                  R                  XR                  5      $ r   )torchops	quantizedrelu6r   r   inputs     r   forwardReLU6.forward0   s!    yy""((==r   c                     g)NQuantizedReLU6 r   s    r   	_get_nameReLU6._get_name3       r   c                 ,    [        U R                  5      $ r   )r   r   )moduse_precomputed_fake_quants     r   
from_floatReLU6.from_float6   s    S[[!!r   )r   F)__name__
__module____qualname____firstlineno____doc__r   r   r%   staticmethodr+   __static_attributes____classcell__r   s   @r   r   r      s+    0>  " "r   r   c                   `   ^  \ rS rSrSrS	U 4S jjrS rS r\S
S j5       r	\
S 5       rSrU =r$ )r   ;   zThis is the quantized version of :class:`~torch.nn.Hardswish`.

Args:
    scale: quantization scale of the output tensor
    zero_point: quantization zero point of the output tensor
c                    > X4S.n[         TU ]  5         U R                  S[        R                  " U40 UD65        U R                  S[        R                  " U40 UD65        g N)devicedtypescale
zero_pointr   r   register_bufferr   tensor)r   r=   r>   r;   r<   factory_kwargsr   s         r   r   Hardswish.__init__C   sR    $*;Well5&KN&KL\5<<
+Un+UVr   c                 ~    [         R                  R                  R                  XR                  U R
                  5      $ r   )r   r   r   	hardswishr=   r>   r   s     r   r   Hardswish.forwardI   s'    yy"",,UJJPPr   c                     g)NQuantizedHardswishr#   r$   s    r   r%   Hardswish._get_nameL       #r   c                 v    U R                   R                  5       u  p#[        [        U5      [	        U5      5      $ r   )activation_post_processcalculate_qparamsr   floatintr)   r*   r=   r>   s       r   r+   Hardswish.from_floatO   s.    77IIKus:77r   c                 8    U " [        U5      [        U5      5      $ r   )rN   rO   clsr)   r=   r>   s       r   from_referenceHardswish.from_referenceT   s    5<Z11r   r#   )NNr-   r.   r/   r0   r1   r2   r   r   r%   r3   r+   classmethodrU   r4   r5   r6   s   @r   r   r   ;   sA    WQ$ 8 8 2 2r   r   c                   `   ^  \ rS rSrSrS	U 4S jjrS rS r\S
S j5       r	\
S 5       rSrU =r$ )r   Y   zThis is the quantized equivalent of :class:`~torch.nn.ELU`.

Args:
    scale: quantization scale of the output tensor
    zero_point: quantization zero point of the output tensor
    alpha: the alpha constant
c                 <   > [         TU ]  U5        Xl        X l        g r   )r   r   r=   r>   )r   r=   r>   alphar   s       r   r   ELU.__init__b   s    
$r   c                     [         R                  R                  R                  R                  R                  XR                  U R                  U R                  5      $ r   )	r   aonnr   
functionalelur=   r>   r\   r   s     r   r   ELU.forwardg   s<    xx{{$$//33::t


 	
r   c                     g)NQuantizedELUr#   r$   s    r   r%   ELU._get_namel   s    r   c                     U R                   R                  5       u  p#[        [        U5      [	        U5      U R
                  5      $ r   )rL   rM   r   rN   rO   r\   rP   s       r   r+   ELU.from_floato   s4    77IIK5<Z#))<<r   c                 N    U " [        U5      [        U5      UR                  5      $ r   )rN   rO   r\   rS   s       r   rU   ELU.from_referencet   s    5<Z#))<<r   )r=   r>   )      ?r-   rW   r6   s   @r   r   r   Y   s?    %


 = = = =r   r   c                      ^  \ rS rSrSr    SS\S\S\S\SS4
U 4S	 jjjrS
 r	S r
\SS j5       r\S 5       rSrU =r$ )r   y   a  This is the quantized equivalent of :class:`~torch.nn.LeakyReLU`.

Args:
    scale: quantization scale of the output tensor
    zero_point: quantization zero point of the output tensor
    negative_slope: Controls the angle of the negative slope. Default: 1e-2
Nr=   r>   negative_sloper   returnc                    > XVS.n[         TU ]  X45        U R                  S[        R                  " U40 UD65        U R                  S[        R                  " U40 UD65        g r:   r?   )	r   r=   r>   rn   r   r;   r<   rB   r   s	           r   r   LeakyReLU.__init__   sV     %+;1Well5&KN&KL\5<<
+Un+UVr   c                     [         R                  R                  R                  XR                  U R
                  U R                  U R                  5      $ r   )r   r   r   
leaky_relurn   r   r=   r>   r   s     r   r   LeakyReLU.forward   s:    yy""--&&djj$//
 	
r   c                     g)NQuantizedLeakyReLUr#   r$   s    r   r%   LeakyReLU._get_name   rJ   r   c                     UR                   R                  5       u  p4U " [        U5      [        U5      UR                  UR
                  5      $ r   )rL   rM   rN   rO   rn   r   )rT   r)   r*   r=   r>   s        r   r+   LeakyReLU.from_float   s<    77IIK5<Z#2D2DckkRRr   c                 d    U " [        U5      [        U5      UR                  UR                  5      $ r   )rN   rO   rn   r   rS   s       r   rU   LeakyReLU.from_reference   s%    5<Z#2D2DckkRRr   r#   )g{Gz?FNNr-   )r.   r/   r0   r1   r2   rN   rO   boolr   r   r%   rX   r+   rU   r4   r5   r6   s   @r   r   r   y   s     !%WW W 	W
 W 
W W

$ S S S Sr   r   c                   R   ^  \ rS rSrSrS\S\4U 4S jjrS r\	S	S j5       r
SrU =r$ )
r      zThis is the quantized equivalent of :class:`~torch.nn.Sigmoid`.

Args:
    scale: quantization scale of the output tensor
    zero_point: quantization zero point of the output tensor
output_scaleoutput_zero_pointc                 :   > [         TU ]  5         Xl        X l        g r   )r   r   r   r   )r   r   r   r   s      r   r   Sigmoid.__init__   s    (!2r   c                 ~    [         R                  R                  R                  XR                  U R
                  5      $ r   )r   r   r   sigmoidr   r   r   s     r   r   Sigmoid.forward   s0    yy""**$$d&<&<
 	
r   c                 r    UR                   R                  5       u  nnU " [        U5      [        U5      5      $ r   )rL   rM   rN   rO   )rT   r)   r*   r   r   s        r   r+   Sigmoid.from_float   s9    
 ''99;	
5&,=(>??r   )r   r   r-   )r.   r/   r0   r1   r2   rN   rO   r   r   rX   r+   r4   r5   r6   s   @r   r   r      s6    3U 3s 3


 @ @r   r   c                   `   ^  \ rS rSrSrS	U 4S jjrS rS r\S
S j5       r	\
S 5       rSrU =r$ )r	      a  This is the quantized version of :class:`~torch.nn.Softmax`.

Args:
    dim: A dimension along which Softmax will be computed (so every slice along dim will sum to 1).
    scale: quantization scale of the output tensor
    zero_point: quantization zero point of the output tensor
c                 F   > [         TU ]  5         Xl        X l        X0l        g r   )r   r   dimr=   r>   )r   r   r=   r>   r   s       r   r   Softmax.__init__   s    
$r   c                    U R                   nUc;  Sn[        R                  R                  R	                  SUR                  5       U5      n[        R
                  R                  R                  XU R                  U R                  5      $ )N   softmax)
r   r   r`   ra   _get_softmax_dimr   r   r   r=   r>   )r   r   r   
stacklevels       r   r   Softmax.forward   sg    hh;J ((%%66599;
C yy""**5tzz4??SSr   c                     g)NQuantizedSoftmaxr#   r$   s    r   r%   Softmax._get_name   s    !r   c                     U R                   R                  5       u  p#[        U R                  [	        U5      [        U5      5      $ r   )rL   rM   r	   r   rN   rO   rP   s       r   r+   Softmax.from_float   s4    77IIKswwec*o>>r   c                 N    U " UR                   [        U5      [        U5      5      $ r   )r   rN   rO   rS   s       r   rU   Softmax.from_reference   s    377E%L#j/::r   )r   r=   r>   )Nrk   r   r-   rW   r6   s   @r   r	   r	      s@    %	T" ? ? ; ;r   r	   c                       \ rS rSr\R
                  R                  R                  R                  r	S r
\S 5       r\S 5       rSrg)r
      c                     g)NQuantizedMultiheadAttentionr#   r$   s    r   r%   MultiheadAttention._get_name   s    ,r   c                     [        S5      e)NzpIt looks like you are trying to convert a non-observed MHA module. Please, see the examples on quantizable MHAs.)NotImplementedError)rT   others     r   r+   MultiheadAttention.from_float   s     "0
 	
r   c                 8   [         R                  R                  R                  US SSS S9nXl        UR
                  be  UR                  R                  S5      n[         R                  " USS9u  pE[         R                  " X4U[         R                  5      n[        USU5        UR                  be  UR                  R                  S5      n[         R                  " WSS9u  pE[         R                  " XdU[         R                  5      n[        USU5        U?U?U$ )NFT)mappingr   remove_qconfigconvert_custom_config_dictbias_k)reduce_rangebias_v)r   r_   quantizationconvertr   r   _parameterspop_choose_qparams_per_tensorquantize_per_tensorquint8setattrr   in_proj_weightin_proj_bias)rT   r   	convertedr   sczpr   s          r   from_observed MultiheadAttention.from_observed   s   HH))11'+ 2 
	 " '**..x8F55f5QFB..v2u||LFIx0'**..x8F55UFB ..v2u||LFIx0$"r   r#   N)r.   r/   r0   r1   r   r_   r`   quantizabler
   _FLOAT_MODULEr%   rX   r+   r   r4   r#   r   r   r
   r
      sH    HHKK++>>M- 
 
  r   r
   c            	          ^  \ rS rSrSr SS\S\S\SS4U 4S jjjrS	\R                  SS4S
 jr
S\R                  S\R                  4S jrS r\SS j5       r\S 5       rSrU =r$ )r   i  a  This is the quantized equivalent of :class:`~torch.nn.PReLU`.

Args:
    scale: quantization scale of the output tensor
    zero_point: quantization zero point of the output tensor
    num_parameters: number of parameters: 1, or the number of channels at input. Default: 1
r   r   num_parametersro   Nc                    > [         TU ]  5         X0l        Xl        X l        [
        R                  " U[
        R                  S9n[
        R                  " USS[
        R                  S9nU R                  U5        g )N)r<   rk   r   )r=   r>   r<   )r   r   r   r=   r>   r   randnrN   r   r   
set_weight)r   r   r   r   wqwr   s         r   r   PReLU.__init__   sZ     	,!
+KKekk:&&qVr   r   c                     Xl         g r   )weight)r   r   s     r   r   PReLU.set_weight+  s    r   r   c                     [         R                  R                  R                  XR                  U R
                  U R                  5      $ r   )r   r   r   prelur   r=   r>   r   s     r   r   PReLU.forward.  s2    yy""((;;

DOO
 	
r   c                     g)NQuantizedPReLUr#   r$   s    r   r%   PReLU._get_name3  r'   r   c                 &   UR                   R                  5       u  p4U " [        U5      [        U5      UR                  5      nUR
                  R                  5       nUR                  R                  5       nU" U5        UR                  [        R                  :w  a  [        SUR                   35        UR                  5       u  p[        R                  " U[        U5      [        U	5      [        R                  5      n
UR                  U
5        U$ Nz9PReLU's weight observer should have dtype quint8 but got )rL   rM   rN   rO   r   r   qconfigr<   r   r   r   r   r   )rT   r)   r*   r=   r>   qprelufloat_wtobserverwt_scalewt_zpqweights              r   r+   PReLU.from_float6  s    77IIKU5\3z?C4F4FG::##%;;%%'>>U\\)KHNNK[\ #446++eHos5z5<<
 	'"r   c                    U " [        U5      [        U5      UR                  5      nUR                  R                  5       nUR                  R                  5       nU" U5        UR
                  [        R                  :w  a  [        SUR
                   35        UR                  5       u  px[        R                  " U[        U5      [        U5      [        R                  5      n	UR                  U	5        U$ r   )rN   rO   r   r   r   r<   r   r   r   rM   r   r   )
rT   r)   r=   r>   r   r   r   r   r   r   s
             r   rU   PReLU.from_referenceH  s    U5\3z?C4F4FG::##%;;%%'>>U\\)KHNNK[\ #446++eHos5z5<<
 	'"r   )r   r=   r   r>   )   r-   )r.   r/   r0   r1   r2   rN   rO   r   r   Tensorr   r   r%   rX   r+   rU   r4   r5   r6   s   @r   r   r     s     RS	!	69	KN			 	ELL T 
U\\ 
ell 

   "  r   r   )warningsr   r   __all__r`   ReLUr   r   r   r   r   r	   r_   r   r
   Moduler   r#   r   r   <module>r      s     	%"EHHMM %"P2"" 2<=%((,, =@&S"" &SR@ehh @6$;ehh $;N000CC 0f@EHHOO @r   