o
    i(                  
   @   s  d dl mZmZmZmZ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mZmZmZmZ e	 ZededZed	eeeeef dZed
d%deeef d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edefddZdeeef ded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eeef d"ededeeef fd#d$Z dS )'    )CallableListOptionalTupleTypeVarUnioncast   )NumpyOps)registry)Model)Array2dFloats2dList2dPaddedRaggedValT)boundSeqTzwith_array2d.v1layerpadreturnc              	      s4   t d j dtt gd|i fdd jD dS )zTransform sequence data into a contiguous 2d array on the way into and
    out of a model. Handles a variety of sequence types: lists, padded and ragged.
    If the input is a 2d array, it is passed through unchanged.
    zwith_array()r   c                    s   i | ]}|  |qS  )maybe_get_dim).0namer   r   M/home/ubuntu/.local/lib/python3.10/site-packages/thinc/layers/with_array2d.py
<dictcomp>   s    z with_array2d.<locals>.<dictcomp>)initlayersattrsdims)r   r   forwardr    	dim_names)r   r   r   r   r   with_array2d   s   r&   modelXseqis_trainc                 C   s   t |trttttf t| ||S t |tr$ttttf t| ||S t |t	t
fs3| jd ||S ttttf t| ||S )Nr   )
isinstancer   r   r   r   r   _ragged_forwardr   _padded_forwardlisttupler!   _list_forward)r'   r(   r)   r   r   r   r$      s   

r$   NXYc                 C   sj   | j d }|j|d urt| |n||d urt| |n|d |jD ]}||}|d ur2| || q!d S )Nr   )r0   r1   )r!   
initialize
_get_arrayr%   r   set_dim)r'   r0   r1   r   dim_namevaluer   r   r   r    -   s   


r    c                 C   sl   t |tr|jS t |tr$| j|j|jjd |jjd  |jjd S t |ttfs0t	t
|S | j|S Nr      r	   )r*   r   datar   ops	reshape2fshaper-   r.   r   r   flatten)r'   r0   r   r   r   r3   ;   s   

$
r3   Xsc                    sx   | j d | jd tdd |D jj|d}||\} dtdtf fdd	}jj|d|fS )
Nr   r   c                 S   s   g | ]}t |qS r   )len)r   seqr   r   r   
<listcomp>M   s    z!_list_forward.<locals>.<listcomp>r   dYsr   c                    s*   j j| d} |}j j|dS )NrB   )r:   r=   	unflatten)rC   dYfdXfget_dXfr   lengthsr   r   r   backpropQ   s   z_list_forward.<locals>.backprop)r!   r"   	NUMPY_OPS	asarray1ir:   r=   r   rD   )r'   r>   r)   XfYfrJ   r   rG   r   r/   H   s   

r/   Xrc                    sJ   | j d }||j|\} |jjdtdtf fdd}t||j|fS )Nr   dYrr   c                    s   t  | j| jS )N)r   dataXdreshaperI   )rP   get_dXx_shaper   r   rJ   `   s   z!_ragged_forward.<locals>.backprop)r!   r9   rQ   r<   r   rI   )r'   rO   r)   r   r1   rJ   r   rS   r   r+   Y   s
   
r+   Xpc                    s   j d }j|j|jjd |jjd  |jjd }|||\} jtt||jjd |jjd |jd }dtdtf fdd}t||j	|j
|j|fS )Nr   r8   r	   dYpr   c                    s   t | tsJ j| j| jjd | jjd  | jjd } |}j|| jjd | jjd |jd }t|| j| j| j	S r7   )
r*   r   r:   reshape2r9   r<   	reshape3f	size_at_trI   indices)rW   dYdX2ddXrT   r'   r   r   rJ   r   s   $z!_padded_forward.<locals>.backprop)r!   r:   rX   r9   r<   rY   r   r   r   rZ   rI   r[   )r'   rV   r)   r   r0   Y2dr1   rJ   r   r_   r   r,   f   s   
$$r,   )r   )NN)!typingr   r   r   r   r   r   r   backendsr
   configr   r'   r   typesr   r   r   r   r   rK   r   r   r!   intr&   boolr$   r    r3   r/   r+   r,   r   r   r   r   <module>   sr   $ *












