
    sgG-                     8   d dl mZ d dlZg dZ G d dej                  j
                        Z G d dej                  j                        Z G d d	ej                  j                        Z G d
 dej                  j                        Z	 G d dej                  j                        Z
 G d dej                  j                        Z G d dej                  j                  j                  j                        Z G d dej                  j                        Zy)    )warnN)ReLU6	HardswishELU	LeakyReLUSigmoidSoftmaxMultiheadAttentionPReLUc                   B     e Zd ZdZd fd	Zd Zd Zedd       Z xZ	S )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                 2    t         |   |       || _        y N)super__init__inplace)selfr   	__class__s     [/var/www/html/venv/lib/python3.12/site-packages/torch/ao/nn/quantized/modules/activation.pyr   zReLU6.__init__,   s    !    c                 j    t         j                  j                  j                  || j                        S r   )torchops	quantizedrelu6r   r   inputs     r   forwardzReLU6.forward0   s#    yy""((==r   c                      y)NQuantizedReLU6 r   s    r   	_get_namezReLU6._get_name3       r   c                 ,    t        | j                        S r   )r   r   )moduse_precomputed_fake_quants     r   
from_floatzReLU6.from_float6   s    S[[!!r   F)
__name__
__module____qualname____doc__r   r   r"   staticmethodr'   __classcell__r   s   @r   r   r      s+    0>  " "r   r   c                   R     e Zd ZdZd fd	Zd Zd Zedd       Ze	d        Z
 xZS )	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                     ||d}t         |           | j                  dt        j                  |fi |       | j                  dt        j                  |fi |       y N)devicedtypescale
zero_pointr   r   register_bufferr   tensor)r   r5   r6   r3   r4   factory_kwargsr   s         r   r   zHardswish.__init__C   sT    $*U;Well5&KN&KL\5<<
+Un+UVr   c                     t         j                  j                  j                  || j                  | j
                        S r   )r   r   r   	hardswishr5   r6   r   s     r   r   zHardswish.forwardI   s)    yy"",,UDJJPPr   c                      y)NQuantizedHardswishr    r!   s    r   r"   zHardswish._get_nameL       #r   c                 x    | j                   j                         \  }}t        t        |      t	        |            S r   )activation_post_processcalculate_qparamsr   floatintr%   r&   r5   r6   s       r   r'   zHardswish.from_floatO   s0    77IIKzus:77r   c                 8     | t        |      t        |            S r   )rC   rD   clsr%   r5   r6   s       r   from_referencezHardswish.from_referenceT   s    5<Z11r   )NNr(   r)   r*   r+   r,   r   r   r"   r-   r'   classmethodrI   r.   r/   s   @r   r   r   ;   sA    WQ$ 8 8 2 2r   r   c                   R     e Zd ZdZd fd	Zd Zd Zedd       Ze	d        Z
 xZS )	r   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         |   |       || _        || _        y r   )r   r   r5   r6   )r   r5   r6   alphar   s       r   r   zELU.__init__b   s    
$r   c                     t         j                  j                  j                  j                  j                  || j                  | j                  | j                        S r   )	r   aonnr   
functionalelur5   r6   rN   r   s     r   r   zELU.forwardg   s>    xx{{$$//334::t


 	
r   c                      y)NQuantizedELUr    r!   s    r   r"   zELU._get_namel   s    r   c                     | j                   j                         \  }}t        t        |      t	        |      | j
                        S r   )rA   rB   r   rC   rD   rN   rE   s       r   r'   zELU.from_floato   s6    77IIKz5<Z#))<<r   c                 N     | t        |      t        |      |j                        S r   )rC   rD   rN   rG   s       r   rI   zELU.from_referencet   s    5<Z#))<<r   )      ?r(   rJ   r/   s   @r   r   r   Y   s?    %


 = = = =r   r   c                   p     e Zd ZdZ	 	 	 	 ddededededdf
 fdZd	 Zd
 Z	e
dd       Ze
d        Z xZS )r   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
    Nr5   r6   negative_sloper   returnc                     ||d}t         |   ||       | j                  dt        j                  |fi |       | j                  dt        j                  |fi |       y r2   r7   )	r   r5   r6   rZ   r   r3   r4   r:   r   s	           r   r   zLeakyReLU.__init__   sZ     %+U;1Well5&KN&KL\5<<
+Un+UVr   c                     t         j                  j                  j                  || j                  | j
                  | j                  | j                        S r   )r   r   r   
leaky_relurZ   r   r5   r6   r   s     r   r   zLeakyReLU.forward   s<    yy""--4&&djj$//
 	
r   c                      y)NQuantizedLeakyReLUr    r!   s    r   r"   zLeakyReLU._get_name   r?   r   c                     |j                   j                         \  }} | t        |      t        |      |j                  |j
                        S r   )rA   rB   rC   rD   rZ   r   )rH   r%   r&   r5   r6   s        r   r'   zLeakyReLU.from_float   s>    77IIKz5<Z#2D2DckkRRr   c                 d     | t        |      t        |      |j                  |j                        S r   )rC   rD   rZ   r   rG   s       r   rI   zLeakyReLU.from_reference   s%    5<Z#2D2DckkRRr   )g{Gz?FNNr(   )r)   r*   r+   r,   rC   rD   boolr   r   r"   rK   r'   rI   r.   r/   s   @r   r   r   y   s     !%WW W 	W
 W 
W

$ S S S Sr   r   c                   D     e Zd ZdZdedef fdZd Zedd       Z	 xZ
S )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         |           || _        || _        y r   )r   r   re   rf   )r   re   rf   r   s      r   r   zSigmoid.__init__   s    (!2r   c                     t         j                  j                  j                  || j                  | j
                        S r   )r   r   r   sigmoidre   rf   r   s     r   r   zSigmoid.forward   s2    yy""**4$$d&<&<
 	
r   c                 r    |j                   j                         \  }} | t        |      t        |            S r   )rA   rB   rC   rD   )rH   r%   r&   re   rf   s        r   r'   zSigmoid.from_float   s9    
 ''99;	
5&,=(>??r   r(   )r)   r*   r+   r,   rC   rD   r   r   rK   r'   r.   r/   s   @r   r   r      s6    3U 3s 3


 @ @r   r   c                   R     e Zd ZdZd fd	Zd Zd Zedd       Ze	d        Z
 xZS )	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                 L    t         |           || _        || _        || _        y r   )r   r   dimr5   r6   )r   rm   r5   r6   r   s       r   r   zSoftmax.__init__   s#    
$r   c                    | j                   }|;d}t        j                  j                  j	                  d|j                         |      }t        j
                  j                  j                  ||| j                  | j                        S )N   softmax)
rm   r   rQ   rR   _get_softmax_dimr   r   rp   r5   r6   )r   r   rm   
stacklevels       r   r   zSoftmax.forward   si    hh;J ((%%66599;
C yy""**5#tzz4??SSr   c                      y)NQuantizedSoftmaxr    r!   s    r   r"   zSoftmax._get_name   s    !r   c                     | j                   j                         \  }}t        | j                  t	        |      t        |            S r   )rA   rB   r	   rm   rC   rD   rE   s       r   r'   zSoftmax.from_float   s6    77IIKzswwec*o>>r   c                 N     | |j                   t        |      t        |            S r   )rm   rC   rD   rG   s       r   rI   zSoftmax.from_reference   s    377E%L#j/::r   )NrX   r   r(   rJ   r/   s   @r   r	   r	      s@    %	T" ? ? ; ;r   r	   c                       e Zd Zej                  j
                  j                  j                  Zd Z	e
d        Ze
d        Zy)r
   c                      y)NQuantizedMultiheadAttentionr    r!   s    r   r"   zMultiheadAttention._get_name   s    ,r   c                     t        d      )NzpIt looks like you are trying to convert a non-observed MHA module. Please, see the examples on quantizable MHAs.)NotImplementedError)rH   others     r   r'   zMultiheadAttention.from_float   s     "0
 	
r   c                 H   t         j                  j                  j                  |d ddd       }| |_        |j
                  h|j                  j                  d      }t        j                  |d      \  }}t        j                  |||t         j                        }t        |d|       |j                  h|j                  j                  d      }t        j                  d      \  }}t        j                  |||t         j                        }t        |d|       |`|`|S )NFT)mappingr   remove_qconfigconvert_custom_config_dictbias_k)reduce_rangebias_v)r   rP   quantizationconvertr   r   _parameterspop_choose_qparams_per_tensorquantize_per_tensorquint8setattrr   in_proj_weightin_proj_bias)rH   r|   	convertedr   sczpr   s          r   from_observedz MultiheadAttention.from_observed   s   HH))11'+ 2 
	 "	 '**..x8F55f5QFB..vr2u||LFIx0'**..x8F55UFB ..vr2u||LFIx0$"r   N)r)   r*   r+   r   rP   rQ   quantizabler
   _FLOAT_MODULEr"   rK   r'   r   r    r   r   r
   r
      sH    HHKK++>>M- 
 
  r   r
   c            	            e Zd ZdZ	 ddedededdf fdZdej                  ddfd	Z	d
ej                  dej                  fdZ
d Zedd       Zed        Z xZS )r   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
    re   rf   num_parametersr[   Nc                    t         |           || _        || _        || _        t        j                  |t
        j                        }t        j                  |ddt
        j                        }| j                  |       y )N)r4   rX   r   )r5   r6   r4   )r   r   r   r5   r6   r   randnrC   r   r   
set_weight)r   re   rf   r   wqwr   s         r   r   zPReLU.__init__   s`     	,!
+KKekk:&&qVr   r   c                     || _         y r   )weight)r   r   s     r   r   zPReLU.set_weight+  s	    r   r   c                     t         j                  j                  j                  || j                  | j
                  | j                        S r   )r   r   r   prelur   r5   r6   r   s     r   r   zPReLU.forward.  s4    yy""((4;;

DOO
 	
r   c                      y)NQuantizedPReLUr    r!   s    r   r"   zPReLU._get_name3  r#   r   c                 &   |j                   j                         \  }} | t        |      t        |      |j                        }|j
                  j                         }|j                  j                         } ||       |j                  t        j                  k7  rt        d|j                          |j                         \  }}	t        j                  |t        |      t        |	      t        j                        }
|j                  |
       |S Nz9PReLU's weight observer should have dtype quint8 but got )rA   rB   rC   rD   r   r   qconfigr4   r   r   r   r   r   )rH   r%   r&   r5   r6   qprelufloat_wtobserverwt_scalewt_zpqweights              r   r'   zPReLU.from_float6  s    77IIKzU5\3z?C4F4FG::##%;;%%'>>U\\)KHNNK[\ #446%++eHos5z5<<
 	'"r   c                     | t        |      t        |      |j                        }|j                  j                         }|j                  j                         } ||       |j
                  t        j                  k7  rt        d|j
                          |j                         \  }}t        j                  |t        |      t        |      t        j                        }	|j                  |	       |S r   )rC   rD   r   r   r   r4   r   r   r   rB   r   r   )
rH   r%   r5   r6   r   r   r   r   r   r   s
             r   rI   zPReLU.from_referenceH  s    U5\3z?C4F4FG::##%;;%%'>>U\\)KHNNK[\ #446%++eHos5z5<<
 	'"r   )   r(   )r)   r*   r+   r,   rC   rD   r   r   Tensorr   r   r"   rK   r'   rI   r.   r/   s   @r   r   r     s     RS	!	69	KN			ELL T 
U\\ 
ell 

   "  r   r   )warningsr   r   __all__rQ   ReLUr   r   r   r   r   r	   rP   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   