
    i(              
          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      Z ed	eeeeef         Z ej.                  d
      ddeeef   dedeeef   fd       Zdeeef   dededeeef   fdZ	 ddeeef   dee   dee   ddfdZdedefdZdeeef   dededeeef   fdZdeeef   dededeeef   fdZdeeef   dededeeef   fdZ y)     )CallableListOptionalTupleTypeVarUnioncast   )NumpyOps)registry)Model)Array2dFloats2dList2dPaddedRaggedValT)boundSeqTzwith_array2d.v1layerpadreturnc                     t        d| j                   dt        t        | gd|i| j                  D ci c]  }|| j                  |       c}      S c c}w )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   )initlayersattrsdims)r   nameforwardr   	dim_namesmaybe_get_dim)r   r   r   s      j/var/www/vps2.regionflexible.com/Desarrollo/venv/lib/python3.12/site-packages/thinc/layers/with_array2d.pywith_array2dr$      s[     
ejj\#wcl:?//J$dE''--J  Ks   AmodelXseqis_trainc                    t        |t              r(t        t        t        t
        f   t        | ||            S t        |t              r(t        t        t        t
        f   t        | ||            S t        |t        t        f      s | 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'   s      r#   r    r       s     $E$.)?5$+QRR	D&	!E$.)?5$+QRRtUm,u||AtX..E$.)=h+OPP    NXYc                     | j                   d   }|j                  |t        | |      n||t        | |      n|       |j                  D ](  }|j	                  |      }|| j                  ||       * y )Nr   )r0   r1   )r   
initialize
_get_arrayr!   r"   set_dim)r%   r0   r1   r   dim_namevalues         r#   r   r   -   s}     &+\\!_E	"#-*UA
Q"#-*UA
Q   OO +##H-MM(E*+r/   c                    t        |t              r|j                  S t        |t              ro| j                  j                  |j                  |j                  j                  d   |j                  j                  d   z  |j                  j                  d         S t        |t        t        f      st        t        |      S | j                  j                  |      S Nr      r
   )r)   r   datar   ops	reshape2fshaper,   r-   r	   r   flatten)r%   r0   s     r#   r4   r4   ;   s    !Vvv	Av	yy""FFAFFLLOaffll1o5qvv||A
 	
 D%=)GQyy  ##r/   Xsc                 d  	
 | j                   d   | j                  d   
t        j                  |D cg c]  }t	        |       c}      	j
                  j                  |
      } ||      \  }dt        dt        f	
fd}j
                  j                  |	
      |fS c c}w )Nr   r   r   dYsr   c                     j                   j                  |       } |      }j                   j                  |      S )NrB   )r<   r?   	unflatten)rC   dYfdXfget_dXfr   lengthsr   s      r#   backpropz_list_forward.<locals>.backpropQ   sA    ii-clyy""3S"99r/   )	r   r   	NUMPY_OPS	asarray1ilenr<   r?   r   rE   )r%   r@   r'   seqXfYfrJ   rH   r   rI   r   s          @@@@r#   r.   r.   H   s     &+\\!_E
++e
C!!r":3s8":;G			23		'BH%KB:f : : :
 99r74h>> #;s   B-Xrc                     | j                   d   } ||j                  |      \  }|j                  j                  dt        dt        ffd}t	        ||j
                        |fS )Nr   dYrr   c                 n    t         | j                        j                        | j                        S )N)r   dataXdreshaperI   )rS   get_dXx_shapes    r#   rJ   z!_ragged_forward.<locals>.backprop`   s(    fSZZ(0093;;GGr/   )r   r;   rU   r>   r   rI   )r%   rQ   r'   r   r1   rJ   rW   rX   s         @@r#   r*   r*   Y   sa     &+\\!_Ebggx(IAviiooGHf H H !RZZ (**r/   Xpc                 j     j                   d   } j                  j                  |j                  |j                  j                  d   |j                  j                  d   z  |j                  j                  d         } |||      \  } j                  j                  t        t        |      |j                  j                  d   |j                  j                  d   |j                  d         }dt        dt        f fd}t        ||j                  |j                  |j                        |fS )Nr   r:   r
   dYpr   c                     t        | t              sJ j                  j                  | j                  | j                  j
                  d   | j                  j
                  d   z  | j                  j
                  d         } |      }j                  j                  || j                  j
                  d   | j                  j
                  d   |j
                  d         }t        || j                  | j                  | j                        S r9   )
r)   r   r<   reshape2r;   r>   	reshape3f	size_at_trI   indices)r[   dYdX2ddXrW   r%   s       r#   rJ   z!_padded_forward.<locals>.backpropr   s    #v&&&YYHHchhnnQ'#((..*;;SXX^^A=N
 bzYY  #((..#SXX^^A%6

1
 b#--ckkBBr/   )r   r<   r]   r;   r>   r^   r	   r   r   r_   rI   r`   )	r%   rY   r'   r   r0   Y2dr1   rJ   rW   s	   `       @r#   r+   r+   f   s     &+\\!_E		
q!BGGMM!$44bggmmA6F	A 8$KC		XsRWW]]1-rww}}Q/?1	A	Cf 	C 	C !R\\2::rzz:HDDr/   )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   r4   r.   r*   r+    r/   r#   <module>rl      s   H H H    = =J	 vW%vU6667#BCD "#dDj)  E$*<M  $t$(48
4> MQ+t+!)$+;CD>+	+
$ 
$' 
$?t?"(?48?
68?"
+t
+"(
+48
+
68
+EtE"(E48E
68Er/   