o
    i                     @   sV  d dl mZ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 ddlmZ dd	lmZ dd
lmZ edeeef dZeZe
dddddddededee dee dee 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deeef dee dee ddf
d d!Z!dS )#    )AnyCallableDictOptionalTupleTypeVarUnioncast   )registry)uniform_init)Model)Floats1dFloats2dInts1dInts2d)partial   )ints_getitem)chainInT)boundzHashEmbed.v1N)seedcolumninitializerdropoutnOnVr   r   r   r   returnc             	   C   s   ||d}|du rt }|dur||d< tdttt|ddi| |dd|d}|du r0|j|jd< |dur@ttt	d	d|f|}||jd
< t
tttf |S )u'  
    An embedding layer that uses the “hashing trick” to map keys to distinct values.
    The hashing trick involves hashing each key four times with distinct seeds,
    to produce four likely differing values. Those values are modded into the
    table, and the resulting vectors summed to produce a single result. Because
    it’s unlikely that two different keys will collide on all four “buckets”,
    most distinct keys will receive a distinct vector under this scheme, even
    when the number of vectors in the table is very low.
    )r   r   Ndropout_rate	hashembedE)r   r   nI)initparamsdimsattrsr   r   r   )r   r   forwardr   r#   idr&   r   r   slicer	   r   OutT)r   r   r   r   r   r   r&   model r,   J/home/ubuntu/.local/lib/python3.10/site-packages/thinc/layers/hashembed.py	HashEmbed   s&   


r.   r+   idsis_trainc           	         s   t tdjd }jd }t|dkr#jjd|jd}n?jj|dd}|jd j	d }j
||| j}d  |rbj	d}t tj|f|  d urb| 9 }dtd	tf fd
d}||fS )Nr!   r   r   dtypeuint64r   r   	d_vectorsr   c                    st    d ur|  9 } j jj }j jjdd}t|jd D ]}j ||| |  qd| j }|S )Nir1   r   r!   )	opsalloc2fshape	as_contigTrangescatter_addinc_gradalloc1i)r4   dEkeysTr5   dX	drop_maskkeysr+   nNvectorsr,   r-   backpropP   s   zforward.<locals>.backprop)r	   r   	get_paramr8   lenr6   r7   r2   r9   r&   hash
gather_addgetr   get_dropout_maskr*   r   )	r+   r/   r0   r   r   outputr   r   rG   r,   rB   r-   r'   ;   s$   



r'   XYc                 C   s,   | |j |d|df}|d| d S )Nr   r   r!   )r6   get_dim	set_param)r   r+   rO   rP   r!   r,   r,   r-   r#   ^   s   r#   )NN)"typingr   r   r   r   r   r   r   r	   configr   initializersr   r+   r   typesr   r   r   r   utilr   array_getitemr   r   r   r*   layersintfloatr.   boolr'   r#   r,   r,   r,   r-   <module>   sh   ( 
+


&
