o
    i                  
   @   s   d dl mZmZmZ d dlZddlmZ ddlmZ ddl	m
Z
mZ eZe
Zedd d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deeef dee dee ddfddZdS )    )CallableOptionalTupleN   )registry)Model)Floats2dInts2dz
uniqued.v1)columnlayerr
   returnc                C   s*   t d| j dtt| gdddd|idS )a  Group inputs to a layer, so that the layer only has to compute for the
    unique values. The data is transformed back before output, and the same
    transformation is applied for the gradient. Effectively, this is a cache
    local to each minibatch.
    zuniqued()N)nOnIr
   )initlayersdimsattrs)r   nameforwardr   )r   r
    r   H/home/ubuntu/.local/lib/python3.10/site-packages/thinc/layers/uniqued.pyuniqued   s   r   modelXis_trainc                    s   | j d }| jd |jdk r||S |d d |f }t|tjs&| }jjj	|dddd\}}| j
dd|| }||\} | |jd f|jdd   }	t|jdtd	tf fd
d}
|	|
fS )Nr
   r   r   T)return_indexreturn_inversereturn_counts   dYr   c                    s8   j j }j |j |   |}|  S )N)opsalloc2fscatter_add	asarray_i)r!   dY_uniq	d_uniques	bp_Y_uniqcountsinvr   
uniq_shaper   r   backprop/   s   zforward.<locals>.backprop)r   r   size
isinstancenumpyndarraygetr"   xpunique	reshape2ireshapeshapetupleOutTInT)r   r   r   r
   keys	uniq_keysindX_uniqY_uniqYr-   r   r(   r   r      s"   



$
r   r@   c                 C   sX   | j d }|j||d |dr| d|d |dr*| d|d d S d S )Nr   )r   r@   r   r   )r   
initializehas_dimset_dimget_dim)r   r   r@   r   r   r   r   r   9   s   


r   )NN)typingr   r   r   r0   configr   r   r   typesr   r	   r:   r9   r   intr   boolr   r   r   r   r   r   <module>   s(    &*
