
    h0                        S SK JrJrJrJrJrJr  SSKJr  SSK	J
r
  SSKJr  SSKJrJr  SSKJrJrJr  SSKJrJrJrJrJrJr  \
R6                  " S	5        S(S\S\\   S\\   S\\\4   4S jj5       r\
R6                  " S5        S(S\S\\   S\\   S\\\4   4S jj5       r\
R6                  " S5           S)S\S\\   S\\   S\S\\   S\S   S\\\4   4S jj5       r\
R6                  " S5             S*SSS\\   S\\   S\S\\   S\S   S\\\/\ 4      S\\\\ S/\4      S\\\4   4S jj5       r!S\S\S\S\\\4   4S  jr"S\S\S\S\\\\/\4   4   4S! jr#S\S"\S\4S# jr$S\S$\S\4S% jr%S\S&\4S' jr&g
)+    )AnyCallableDictOptionalTuplecast   )torch)registry)Model)PyTorchGradScalerPyTorchShim)
ArgsKwargsFloats3dPadded)convert_recursiveis_torch_arrayis_xp_arraypartialtorch2xpxp2torchzPyTorchRNNWrapper.v1Npytorch_modelconvert_inputsconvert_outputsreturnc           	      r    Uc  [         nUc  [        n[        [        [        [        4   [        U UUS95      $ )z*Wrap a PyTorch RNN model for use in Thinc.r   r   )convert_rnn_inputsconvert_rnn_outputsr   r   r   PyTorchWrapperr   r   r   s      U/home/james-whalen/.local/lib/python3.13/site-packages/thinc/layers/pytorchwrapper.pyPyTorchRNNWrapperr#      sD     +-ffn)+	
     zPyTorchWrapper.v1c           	      f    Uc  [         nUc  [        n[        S[        XS.[	        U 5      /SSS.S9$ )a  Wrap a PyTorch model, so that it has the same API as Thinc models.
To optimize the model, you'll need to create a PyTorch optimizer and call
optimizer.step() after each batch. See examples/wrap_pytorch.py

Your PyTorch model's forward method can take arbitrary args and kwargs,
but must return either a single tensor as output or a tuple. You may find the
PyTorch register_forward_hook helpful if you need to adapt the output.

The convert functions are used to map inputs and outputs to and from your
PyTorch model. Each function should return the converted output, and a callback
to use during the backward pass. So:

    Xtorch, get_dX = convert_inputs(X)
    Ytorch, torch_backprop = model.shims[0](Xtorch, is_train)
    Y, get_dYtorch = convert_outputs(Ytorch)

To allow maximum flexibility, the PyTorchShim expects ArgsKwargs objects
on the way into the forward and backward passed. The ArgsKwargs objects
will be passed straight into the model in the forward pass, and straight
into `torch.autograd.backward` during the backward pass.
Npytorchr   nInOattrsshimsdimsconvert_pytorch_default_inputsconvert_pytorch_default_outputsr   forwardr   r!   s      r"   r    r    '   sF    6 79!/T=)*% r$   zPyTorchWrapper.v2mixed_precisiongrad_scalerdeviceztorch.devicec                 h    Uc  [         nUc  [        n[        S[        XS.[	        U UUUS9/SSS.S9$ )a  Wrap a PyTorch model, so that it has the same API as Thinc models.
To optimize the model, you'll need to create a PyTorch optimizer and call
optimizer.step() after each batch. See examples/wrap_pytorch.py

Your PyTorch model's forward method can take arbitrary args and kwargs,
but must return either a single tensor as output or a tuple. You may find the
PyTorch register_forward_hook helpful if you need to adapt the output.

The convert functions are used to map inputs and outputs to and from your
PyTorch model. Each function should return the converted output, and a callback
to use during the backward pass. So:

    Xtorch, get_dX = convert_inputs(X)
    Ytorch, torch_backprop = model.shims[0](Xtorch, is_train)
    Y, get_dYtorch = convert_outputs(Ytorch)

To allow maximum flexibility, the PyTorchShim expects ArgsKwargs objects
on the way into the forward and backward passed. The ArgsKwargs objects
will be passed straight into the model in the forward pass, and straight
into `torch.autograd.backward` during the backward pass.

mixed_precision:
    Enable mixed-precision. This changes whitelisted ops to run
    in half precision for better performance and lower memory use.
grad_scaler:
    The gradient scaler to use for mixed-precision training. If this
    argument is set to "None" and mixed precision is enabled, a gradient
    scaler with the default configuration is used.
device:
    The PyTorch device to run the model on. When this argument is
    set to "None", the default device for the currently active Thinc
    ops is used.
Nr&   r   )r2   r3   r4   r'   r*   r.   )r   r   r   r2   r3   r4   s         r"   PyTorchWrapper_v2r6   O   sW    T 79!/T /'	
 % r$   zPyTorchWrapper.v3ztorch.nn.Moduleserialize_modeldeserialize_modelc                 l    Uc  [         nUc  [        n[        S[        XS.[	        U UUUUUS9/SSS.S9$ )a  Wrap a PyTorch model, so that it has the same API as Thinc models.
To optimize the model, you'll need to create a PyTorch optimizer and call
optimizer.step() after each batch. See examples/wrap_pytorch.py

Your PyTorch model's forward method can take arbitrary args and kwargs,
but must return either a single tensor or a tuple. You may find the
PyTorch register_forward_hook helpful if you need to adapt the output.

The convert functions are used to map inputs and outputs to and from your
PyTorch model. Each function should return the converted output, and a callback
to use during the backward pass. So:

    Xtorch, get_dX = convert_inputs(X)
    Ytorch, torch_backprop = model.shims[0](Xtorch, is_train)
    Y, get_dYtorch = convert_outputs(Ytorch)

To allow maximum flexibility, the PyTorchShim expects ArgsKwargs objects
on the way into the forward and backward passed. The ArgsKwargs objects
will be passed straight into the model in the forward pass, and straight
into `torch.autograd.backward` during the backward pass.

mixed_precision:
    Enable mixed-precision. This changes whitelisted ops to run
    in half precision for better performance and lower memory use.
grad_scaler:
    The gradient scaler to use for mixed-precision training. If this
    argument is set to "None" and mixed precision is enabled, a gradient
    scaler with the default configuration is used.
device:
    The PyTorch device to run the model on. When this argument is
    set to "None", the default device for the currently active Thinc
    ops is used.
serialize_model:
    Callback that receives the wrapped PyTorch model as its argument and
    returns a "bytes" representation of the same. The representation should
    contain all the necessary information to fully deserialize the model.
    When set to "None", the default serializer serializes the model's parameters.
deserialize_model:
    Callback that receives the default PyTorch model (passed to the constructor), the
    serialized "bytes" representation and a PyTorch device. It should return a
    fully deserialized model on the target device as its result.
    When set to "None", the default deserializer deserializes the model's parameters.
Nr&   r   )r2   r3   r4   r7   r8   r'   r*   r.   )r   r   r   r2   r3   r4   r7   r8   s           r"   PyTorchWrapper_v3r:      s]    l 79!/T /' /"3	
 % r$   modelXis_trainc                    ^	^
^ U R                   S   nU R                   S   nU" XU5      u  nm	U R                  S   " XR5      u  nmU" XU4U5      u  nm
S[        S[        4U	U
U4S jjnXx4$ )zwReturn the output of the wrapped PyTorch model for the given input,
along with a callback to handle the backward pass.
r   r   r   dYr   c                 8   > T" U 5      nT" U5      nT" U5      nU$ N )r?   dYtorchdXtorchdXget_dXget_dYtorchtorch_backprops       r"   backpropforward.<locals>.backprop   s$    b/ )G_	r$   )r+   r,   r   )r;   r<   r=   r   r   XtorchYtorchYrI   rF   rG   rH   s            @@@r"   r1   r1      s     [[!12Nkk"34O#Eh7NFF"[[^F=FN$UKBNA{S S   ;r$   c                   ^^ [        [        U R                  S   5      mUU4S jn[        [        X15      n[        U[        5      (       a  S nXE4$ [        U[        5      (       a  S n[        [        5       US9U4$ [        U[        [        45      (       a  S n[        [        U5      0 S9U4$ S n[        U40 S9U4$ )Nr   c                 .   > [        U TTR                  S9$ )Nrequires_gradr4   )r   r4   )xr=   shims    r"   <lambda>0convert_pytorch_default_inputs.<locals>.<lambda>   s    (1HT[[Qr$   c                 ,    [        [        [        U 5      $ rA   )r   r   r   )rD   s    r"   reverse_conversion:convert_pytorch_default_inputs.<locals>.reverse_conversion   s    $^XwGGr$   c                 D    [        [        [        U 5      nUR                  $ rA   )r   r   r   kwargsrD   rE   s     r"   rW   rX      s    ">8WEB99r$   argsrZ   c                 D    [        [        [        U 5      nUR                  $ rA   r   r   r   r]   r[   s     r"   rW   rX     s    ">8WEB77Nr$   c                 J    [        [        [        U 5      nUR                  S   $ Nr   r_   r[   s     r"   rW   rX     s    ">8WEB771:r$   )
r   r   r,   r   r   
isinstancer   dicttuplelist)r;   r<   r=   	xp2torch_	convertedrW   rS   s     `   @r"   r/   r/      s     U[[^,DQI!+y<I)Z((	H ,,	It	$	$	 uwy9;MMM	It}	-	-	 uY/;=OOO	 	|B79KKKr$   X_Ytorchc                    ^^ [        [        U R                  S   5      mUu  nm[        [        [
        T5      nS[        S[        4UU4S jjnXE4$ )Nr   r?   r   c                 n   > [        [        [        [        TR                  S9U 5      n[        T44SU0S9$ )N)r4   grad_tensorsr\   )r   r   r   r   r4   r   )r?   rC   rL   rS   s     r"   rW   ;convert_pytorch_default_outputs.<locals>.reverse_conversion  s7    #$++>
 	|^W4MNNr$   )r   r   r,   r   r   r   r   r   )r;   rh   r=   r<   rM   rW   rL   rS   s         @@r"   r0   r0     sU    U[[^,DIAv.(F;AOs Oz O O   r$   Xpc                   ^^^ [        [        U R                  S   5      nUR                  mUR                  mUR
                  mS[        S[        4UUU4S jjn[        [        UR                  SUR                  S9S 40 S9nXT4$ )Nr   d_inputsr   c                 P   > [        U R                  S   5      n[        UTTT5      $ ra   )r   r]   r   )ro   rE   indiceslengths	size_at_ts     r"   convert_from_torch_backward7convert_rnn_inputs.<locals>.convert_from_torch_backward*  s'    hmmA&'b)Wg66r$   TrP   r\   )r   r   r,   rs   rr   rq   r   r   r   datar4   )	r;   rm   r=   rS   rt   outputrq   rr   rs   s	         @@@r"   r   r   $  s}    U[[^,DIjjGjjG7j 7V 7 7 rwwd4;;GNF ..r$   inputs_outputsc                   ^^	 [        [        U R                  S   5      m	Uu  nu  mnS[        S[        4UU	4S jjn[        [
        [        T5      5      n[        XcR                  UR                  UR                  5      nXu4$ )Nr   dYpr   c                 \   > [        U R                  STR                  S9n[        T4SU0S9$ )NTrP   rk   r\   )r   rv   r4   r   )rz   rC   rL   rS   s     r"   convert_for_torch_backward7convert_rnn_outputs.<locals>.convert_for_torch_backward9  s-    3884Ly.'1JKKr$   )
r   r   r,   r   r   r   r   rs   rr   rq   )
r;   rx   r=   rm   _r|   rM   YprL   rS   s
           @@r"   r   r   5  sv    U[[^,D$OBL L: L L 	Xx'(A	<<RZZ	8B))r$   )NN)NNFNN)NNFNNNN)'typingr   r   r   r   r   r   compatr
   configr   r;   r   r,   r   r   typesr   r   r   utilr   r   r   r   r   r   layersr#   r    boolr6   bytesr:   r1   r/   r0   r   r   rB   r$   r"   <module>r      s   = =    2 0 0  
'( *.*.X& h' 66>	 )( 
$% *.*.$$X&$ h'$ 38_	$ &$N 
$% *.*.!/3'+::X&: h': 	:
 +,: ^$: 38_: &:z 
$% *.*.!/3'+8<OSH$HX&H h'H 	H
 +,H ^$H hue|45H  #un)Es)J KLH 38_H &HV5 S D U3=5I . L L L$( L
:xc 1223 LF!5 !C !4 !"/e / /4 /"
*u 
*e 
*r$   