o
    i                  
   @   s  U d dl mZmZmZmZ ddlmZ ddlmZ ddl	m
Z
mZ ddlmZ ddlmZ eZeZd	Zeed
< eddddd	eee
e
f  dee deeef fddZdeeef dededeeef fddZ	ddeeef dee dee ddfddZdd Zdd ZdS )     )CallableOptionalTuplecast   )registry)Model)Floats2dRagged)	get_width   )noopkey_transformKEY_TRANSFORM_REFzParametricAttention.v2Nr   nOr   returnc              	   C   s4   | d u rt  } 	 tdttdd id|it| i| gdS )Nz	para-attnQr   )initparamsdimsrefslayers)r   r   forwardr   r   r    r   W/home/ubuntu/.local/lib/python3.10/site-packages/thinc/layers/parametricattention_v2.pyParametricAttention_v2   s   r   modelXris_trainc                    sr    d}t}tj|||j|j|\} tj||j|j\}dtdt	f fdd}t
||j|fS )Nr   dYrr   c                    s>   | j \}} |\}}d|  ||7 }t|| jS )Nr   )dataXdinc_gradravelr
   lengths)r    dXd_attentiondQdX2bp_attention	bp_outputr   r   r   backprop-   s
   zforward.<locals>.backprop)	get_paramget_refr   _get_attentionopsr!   r$   _apply_attentionOutTInTr
   )r   r   r   r   r   	attentionoutputr,   r   r)   r   r   $   s   

r   XYc                 C   s   |  t}|d urt|nd }|r"| d| |dr"|d| | j| d}|| jjj	
dd|j7 }| d| |d urE|jnd }|d urN|jnd }||| d S )Nr   gg?r   )r.   r   r   set_dimhas_dimr0   alloc1fget_dimxprandomuniformshape	set_paramr!   
initialize)r   r6   r7   r   widthr   X_arrayY_arrayr   r   r   r   7   s   

r   c                    sP   |||d\   dd fdd}|fS )N)r   r   c                    s<    | } j | dd}j| }|}||fS )NT)trans1)backprop_softmax_sequencesgemmr<   outer)r&   r'   dYr%   KK_bpr   r4   r$   r0   r   r   get_attention_bwdR   s
   z)_get_attention.<locals>.get_attention_bwd)rH   	reshape2fsoftmax_sequences)r0   r   r   r6   r$   r   rN   r   rK   r   r/   L   s
   r/   c                    s     } fdd}||fS )Nc                    s"    |  j ddd}|  }||fS )Nr   T)axiskeepdims)sum)d_outputr&   r%   r6   r4   r   r   apply_attention_bwd_   s   z-_apply_attention.<locals>.apply_attention_bwdr   )r0   r4   r6   r$   r5   rV   r   rU   r   r1   \   s   r1   )NN)typingr   r   r   r   configr   r   r   typesr	   r
   utilr   r   r3   r2   r   str__annotations__r   intr   boolr   r   r/   r1   r   r   r   r   <module>   s@    
*

