
    h                         S SK JrJrJr  SSKJr  SSKJr  SSKJ	r	J
r
Jr  SSKJrJrJr        SS	\S
   S\\   S\\   S\S\\	   S\S   S\\\4   4S jjrS\4S jrg)    )AnyCallableOptional   )torch)Model)PyTorchGradScalerPyTorchShimTorchScriptShim   )convert_pytorch_default_inputsconvert_pytorch_default_outputsforwardNtorchscript_modelztorch.jit.ScriptModuleconvert_inputsconvert_outputsmixed_precisiongrad_scalerdeviceztorch.devicereturnc                 h    Uc  [         nUc  [        n[        S[        XS.[	        U UUUS9/SSS.S9$ )a  Wrap a TorchScript model, so that it has the same API as Thinc models.

torchscript_model:
    The TorchScript module. A value of `None` is also possible to
    construct a shim to deserialize into.
convert_inputs:
    Function that converts inputs and gradients that should be passed
    to the model to Torch tensors.
convert_outputs:
    Function that converts model outputs and gradients from Torch tensors
    Thinc arrays.
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.
Npytorch_script)r   r   )modelr   r   r   )nInO)attrsshimsdims)r   r   r   r   r   )r   r   r   r   r   r   s         Y/home/james-whalen/.local/lib/python3.13/site-packages/thinc/layers/torchscriptwrapper.pyTorchScriptWrapper_v1r       sW    @ 79!/T' /'	
 %     r   c           	         U R                   S   n[        U[        5      (       d  [        S5      eU R                  S   nU R                  S   nUR
                  n[        U[        R                  R                  5      (       d  [        S5      e[        R                  R                  U5      nUR                  nUR                  nUR                  n[        UUUUUUS9$ )zrConvert a PyTorch wrapper to a TorchScript wrapper. The embedded PyTorch
`Module` is converted to `ScriptModule`.
r   z6Expected PyTorchShim when converting a PyTorch wrapperr   r   z*PyTorchShim does not wrap a PyTorch module)r   r   r   r   r   )r   
isinstancer
   
ValueErrorr   _modelr   nnModulejitscript_grad_scaler_mixed_precisionr   r    )	r   shimr   r   pytorch_modelr   r   r   r   s	            r   pytorch_to_torchscript_wrapperr.   B   s     ;;q>DdK((QRR[[!12Nkk"34OKKMmUXX__55EFF		((7##K++O[[F %'' r!   )NNNFNN)typingr   r   r   compatr   r   r   r   r	   r
   r   pytorchwrapperr   r   r   boolr    r.    r!   r   <module>r4      s    * *   C C  =A)-*.!/3'+2 892X&2 h'2 	2
 +,2 ^$2 38_2j% r!   