o
    i8                  
   @   s   d dl mZmZmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ e	Ze	Zedd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   )registry)Model)Ragged)	get_widthzParametricAttention.v1NnOreturnc                 C   s   t dttddid| idS )z/Weight inputs by similarity to a learned vectorz	para-attnQNr
   )initparamsdims)r   forwardr   )r
    r   T/home/ubuntu/.local/lib/python3.10/site-packages/thinc/layers/parametricattention.pyParametricAttention   s   r   modelXris_trainc                    sd    d}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_attentionopsr   r   _apply_attentionOutTInTr   )r   r   r   r   	attentionoutputr#   r   r    r   r      s
   
r   XYc                 C   sT   |d ur|  dt| | j| d}|| jjjdd|j7 }| 	d| d S )Nr
   gg?r   )
set_dimr	   r&   alloc1fget_dimxprandomuniformshape	set_param)r   r,   r-   r   r   r   r   r   !   s
   r   c                    s>     dd fdd}|fS )N   c                    s4    | } j| dd}j|  }||fS )NT)trans1)backprop_softmax_sequencesgemmr1   outer)r   r   r   r   r,   r*   r   r&   r   r   get_attention_bwd0   s   z)_get_attention.<locals>.get_attention_bwd)r:   	reshape2fsoftmax_sequences)r&   r   r,   r   r=   r   r<   r   r%   ,   s   r%   c                    s     } fdd}||fS )Nc                    s"    |  j ddd}|  }||fS )Nr7   T)axiskeepdims)sum)d_outputr   r   r,   r*   r   r   apply_attention_bwd<   s   z-_apply_attention.<locals>.apply_attention_bwdr   )r&   r*   r,   r   r+   rE   r   rD   r   r'   9   s   r'   )N)NN)typingr   r   r   configr   r   r   typesr   utilr	   r)   r(   layersintr   boolr   r   r%   r'   r   r   r   r   <module>   s,    "*

