o
    i                     @   s   d dl mZmZ d dlZd dlmZ d dlmZ d dlm	Z	 ddl
mZmZmZ ded	efd
dZdedeee eee	  f defddZded	efddZdedeee ee	 f defddZdS )    )ListUnionN)Doc)Model)Ragged   )PoolingModelTWithRaggedLastLayerModelTWithRaggedLayersModelTpoolingreturnc                 C      t dt| gdS )z:Concatenate all Docs and layers into a Ragged for pooling.with_ragged_layerslayers)r   with_ragged_layers_forwardr    r   ]/home/ubuntu/.local/lib/python3.10/site-packages/spacy_curated_transformers/models/pooling.pyr      s   r   modelXis_trainc                    s  | j d }| jjg }g }g }g } D ]0}t|tr|jjjn|}	|	D ]}
||
j	 ||
j
 q#|t|	 |t|	d j
 qtj|ddj|dd}|||\}tdd t||D }fddt|t|d d |D } fdd}||fS )	Nr   axisc                 S   s   g | ]\}}|| qS r   r   ).0	doc_layerdoc_lenr   r   r   
<listcomp>,   s    z.with_ragged_layers_forward.<locals>.<listcomp>c                    s   g | ]
\}}  ||qS r   )split)r   	doc_arrayr   )xpr   r   r   1   s    
c                    s    dd | D }|j}g }  D ]3}g }t|tr!|jjjn|}|D ]}|jjd }|t	|d | |j
 ||d  }q%| | q| S )Nc                 S   s   g | ]	}|D ]}|qqS r   r   )r   dY_docdY_layerr   r   r   r   ;   s    z@with_ragged_layers_forward.<locals>.backprop.<locals>.<listcomp>r   )concatenatedataXd
isinstancer   _trf_dataall_outputsshapeappendr   lengths)dYdY_pooled_flatdY_flatX_docr#   X_doc_layersX_layerdoc_unpooled_lenr   pooling_backpropr    r   r   backprop9   s   
z,with_ragged_layers_forward.<locals>.backprop)r   opsr    r&   r   r'   r(   r)   r+   r%   r,   lenr   r$   numpyasarrayzipr   cumsum)r   r   r   r   dataslensdoc_layer_countsdoc_token_countsr0   r1   r2   X_flatY_pooleddoc_layer_lensYr6   r   r4   r   r      s8   

r   c                 C   r   )zCConcatenate the last layers for all Docs into a Ragged for pooling.with_ragged_last_layerr   )r   with_ragged_last_layer_forwardr   r   r   r   rE   R   s   rE   c                    s   | j d }| jjg }g }g } D ]"}t|tr|jjjn|}||j	 ||j
 |t|j
 qtj|ddj|dd}	||	|\}
|
t|d d } fdd}||fS )Nr   r   r!   c                    sp    | }|j}g }  D ]'}t|tr|jjjn|}|jjd }| t	|d | |j
 ||d  }q| S )Nr   )r$   r%   r&   r   r'   r(   last_hidden_layer_statesr*   r+   r   r,   )r-   r.   r/   r0   X_doc_layerr3   r4   r   r   r6   r   s   

z0with_ragged_last_layer_forward.<locals>.backprop)r   r7   r    r&   r   r'   r(   last_hidden_layer_stater+   r%   r,   r8   r   r$   r   r9   r<   )r   r   r   r   r=   r>   doc_lensr0   rH   rA   rB   rD   r6   r   r4   r   rF   [   s$   
rF   )typingr   r   r9   spacy.tokensr   	thinc.apir   thinc.typesr   typesr   r	   r
   r   boolr   rE   rF   r   r   r   r   <module>   s<    

@
	