
    9i                         S SK r S SKJr  S SKrS SKJs  Jr  S SKJrJr   " S S\R                  5      r
 " S S\R                  5      r " S S	\R                  5      r " S
 S\R                  5      r " S S\R                  5      r " S S\R                  5      r " S S\R                  5      r " S S\R                  5      r " S S\5      r\\SSS.4\\
\
\SS04\\\\R&                  \R(                  \R*                  \\\R,                  S.r\" \5      rS r\" S5      r\" S5      r\" S5      r\" S 5      r\" S!5      r\" S"5      r\" S#5      r \" S$5      r!g)%    N)OrderedDict)Tensornnc                   *    \ rS rSrSrS\S\4S jrSrg)NewGELUActivation   z
Implementation of the GELU activation function currently in Google BERT repo (identical to OpenAI GPT). Also see
the Gaussian Error Linear Units paper: https://arxiv.org/abs/1606.08415
inputreturnc                     SU-  S[         R                  " [        R                  " S[        R                  -  5      US[         R
                  " US5      -  -   -  5      -   -  $ )N      ?      ?       @Hm?g      @)paddletanhmathsqrtpipowselfr	   s     o/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/paddlex/inference/models/common/vlm/activations.pyforwardNewGELUActivation.forward   s_     ++IIcDGGm,x&**UC*@@@B
	
     N__name__
__module____qualname____firstlineno____doc__r   r   __static_attributes__r   r   r   r   r      s    

V 
 
r   r   c                   \   ^  \ rS rSrSrS
S\4U 4S jjjrS\S\4S jrS\S\4S jr	S	r
U =r$ )GELUActivation+   a  
Original Implementation of the GELU activation function in Google BERT repo when initially created. For
information: OpenAI GPT's GELU is slightly different (and gives slightly different results): 0.5 * x * (1 +
torch.tanh(math.sqrt(2 / math.pi) * (x + 0.044715 * torch.pow(x, 3)))) This is now written in C in nn.functional
Also see the Gaussian Error Linear Units paper: https://arxiv.org/abs/1606.08415
use_gelu_pythonc                    > [         TU ]  5         U(       a  U R                  U l        g [        R
                  R                  U l        g N)super__init___gelu_pythonactr   
functionalgelu)r   r'   	__class__s     r   r+   GELUActivation.__init__3   s/    ((DH}}))DHr   r	   r
   c                 n    US-  S[         R                  " U[        R                  " S5      -  5      -   -  $ )Nr   r   r   )r   erfr   r   r   s     r   r,   GELUActivation._gelu_python:   s,    s{cFJJutyy~/E$FFGGr   c                 $    U R                  U5      $ r)   r-   r   s     r   r   GELUActivation.forward=   s    xxr   r6   )F)r   r   r    r!   r"   boolr+   r   r,   r   r#   __classcell__r0   s   @r   r%   r%   +   sG    * * *H& HV HV   r   r%   c                   *    \ rS rSrSrS\S\4S jrSrg)FastGELUActivationA   zu
Applies GELU approximation that is slower than QuickGELU but more accurate. See: https://github.com/hendrycks/GELUs
r	   r
   c                 ^    SU-  S[         R                  " US-  SSU-  U-  -   -  5      -   -  $ )Nr   r   g3E?r   )r   r   r   s     r   r   FastGELUActivation.forwardF   sG     ++el2cHu<Lu<T6TUVW	
r   r   Nr   r   r   r   r<   r<   A   s    
V 
 
r   r<   c                   *    \ rS rSrSrS\S\4S jrSrg)QuickGELUActivationQ   zj
Applies GELU approximation that is fast but somewhat inaccurate. See: https://github.com/hendrycks/GELUs
r	   r
   c                 :    U[         R                  " SU-  5      -  $ )NgZd;?)Fsigmoidr   s     r   r   QuickGELUActivation.forwardV   s    qyy///r   r   Nr   r   r   r   rA   rA   Q   s    0V 0 0r   rA   c                   J   ^  \ rS rSrSrS\S\4U 4S jjrS\S\4S jrS	r	U =r
$ )
ClippedGELUActivationZ   ab  
Clip the range of possible GeLU outputs between [min, max]. This is especially useful for quantization purpose, as
it allows mapping negatives values in the GeLU spectrum. For more information on this trick, please refer to
https://arxiv.org/abs/2004.09602.

Gaussian Error Linear Unit. Original Implementation of the gelu activation function in Google Bert repo when
initially created.

For information: OpenAI GPT's gelu is slightly different (and gives slightly different results): 0.5 * x * (1 +
torch.tanh(math.sqrt(2 / math.pi) * (x + 0.044715 * torch.pow(x, 3)))). See https://arxiv.org/abs/1606.08415
minmaxc                 h   > X:  a  [        SU SU S35      e[        TU ]	  5         Xl        X l        g )Nzmin should be < max (got min: z, max: ))
ValueErrorr*   r+   rJ   rK   )r   rJ   rK   r0   s      r   r+   ClippedGELUActivation.__init__g   s8    9=cU'#aPQQr   xr
   c                 l    [         R                  " [        U5      U R                  U R                  5      $ r)   )r   clipr/   rJ   rK   )r   rP   s     r   r   ClippedGELUActivation.forwardo   s!    {{47DHHdhh77r   )rK   rJ   )r   r   r    r!   r"   floatr+   r   r   r#   r9   r:   s   @r   rH   rH   Z   s3    
E  8 8F 8 8r   rH   c                   *    \ rS rSrSrS\S\4S jrSrg)SiLUActivations   a  
See Gaussian Error Linear Units (Hendrycks et al., https://arxiv.org/abs/1606.08415) where the SiLU (Sigmoid Linear
Unit) was originally introduced and coined, and see Sigmoid-Weighted Linear Units for Neural Network Function
Approximation in Reinforcement Learning (Elfwing et al., https://arxiv.org/abs/1702.03118) and Swish: a Self-Gated
Activation Function (Ramachandran et al., https://arxiv.org/abs/1710.05941v1) where the SiLU was experimented with
later.
r	   r
   c                 .    [         R                  " U5      $ r)   )rD   silur   s     r   r   SiLUActivation.forward|       vve}r   r   Nr   r   r   r   rV   rV   s   s    V  r   rV   c                   *    \ rS rSrSrS\S\4S jrSrg)MishActivation   z
See Mish: A Self-Regularized Non-Monotonic Activation Function (Misra., https://arxiv.org/abs/1908.08681). Also
visit the official repository for the paper: https://github.com/digantamisra98/Mish
r	   r
   c                 .    [         R                  " U5      $ r)   )rD   mishr   s     r   r   MishActivation.forward   r[   r   r   Nr   r   r   r   r]   r]      s    
V  r   r]   c                   *    \ rS rSrSrS\S\4S jrSrg)LinearActivation   zS
Applies the linear activation function, i.e. forwarding input directly to output.
r	   r
   c                     U$ r)   r   r   s     r   r   LinearActivation.forward   s    r   r   Nr   r   r   r   rc   rc      s    V  r   rc   c                   (   ^  \ rS rSrU 4S jrSrU =r$ )ClassInstantier   c                 l   > [         TU ]  U5      n[        U[        5      (       a  UOU0 4u  p4U" S0 UD6$ )Nr   )r*   __getitem__
isinstancetuple)r   keycontentclskwargsr0   s        r   rk   ClassInstantier.__getitem__   s7    '%c*!+GU!;!;g'2}V}r   r   )r   r   r    r!   rk   r#   r9   r:   s   @r   rh   rh      s     r   rh   i
   )rJ   rK   r'   T)r/   gelu_10	gelu_fastgelu_newgelu_pytorch_tanhgelu_pythonlinearr`   
quick_gelurelurelu6rE   rY   swishr   c           	          U [         ;   a	  [         U    $ [        SU  S[        [         R                  5       5       35      e)Nz	function z not found in ACT2FN mapping )ACT2FNKeyErrorlistkeys)activation_strings    r   get_activationr      sB    F"'(()**GV[[]H[G\]
 	
r   rx   rv   r/   ru   rz   rY   r`   ry   )"r   collectionsr   r   paddle.nn.functionalr   r.   rD   r   Layerr   r%   r<   rA   rH   rV   r]   rc   rh   ReLUReLU6SigmoidTanhACT2CLSr   r   rx   rv   r/   ru   rz   rY   r`   
linear_actr   r   r   <module>r      sc    #      
 
(RXX ,
 
 0"(( 08BHH 82
RXX 
RXX rxx k  %s2'>?#!*"%6$=>%GGXXzzGG!$ 
	!
 ]+*%f;'	L)
ffH%
r   