
    h	                     n    S SK Jr  S SKJrJr  S SKrSSKJr  SSKJ	r	  SSK
Jr  SS	KJr   " S
 S\5      rg)    )BytesIO)AnyOptionalN   )torch)get_torch_default_device   )PyTorchShim)PyTorchGradScalerc                   p   ^  \ rS rSrSr     SS\S   S\S\S\\   S\S	   4
U 4S
 jjjr	S r
S rSrU =r$ )TorchScriptShim   a  A Thinc shim that wraps a TorchScript module.

model:
    The TorchScript module. A value of `None` is also possible to
    construct a shim to deserialize into.
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.
modelztorch.jit.ScriptModule	optimizermixed_precisiongrad_scalerdeviceztorch.devicec                    > Ub4  [        U[        R                  R                  5      (       d  [	        S5      e[
        TU ]  XX4XV5        g )NzUPyTorchScriptShim must be initialized with ScriptModule or None (for deserialization))
isinstancer   jitScriptModule
ValueErrorsuper__init__)selfr   configr   r   r   r   	__class__s          Q/home/james-whalen/.local/lib/python3.13/site-packages/thinc/shims/torchscript.pyr   TorchScriptShim.__init__   sD     Zuyy7M7M%N%Ng  		KX    c                     [        5       n[        R                  R                  U R                  U5        UR                  S5        UR                  5       nU R                  US.n[        R                  " U5      $ )Nr   )r   r   )
r   r   r   save_modelseekgetvaluecfgsrslymsgpack_dumps)r   filelikemodel_bytesmsgs       r   to_bytesTorchScriptShim.to_bytes/   sX    9		t{{H-a'')K8""3''r    c                    [        5       n[        R                  " U5      nUS   U l        [	        US   5      nUR                  S5        UR                  S:X  a  [        R                  " S5      OUn[        R                  R                  XES9U l        U R                  R                  U5        U R                  R                  U5        U $ )Nr   r   r   mpscpu)map_location)r   r'   msgpack_loadsr&   r   r$   typer   r   r   loadr#   to_grad_scalerto_)r   
bytes_datar   r+   r)   r1   s         r   
from_bytesTorchScriptShim.from_bytes7   s    )+!!*-x=3w<(a /5kkU.Bu||E*iinnXnIvf%r    )r#   r&   )NNFNN)__name__
__module____qualname____firstlineno____doc__r   r   boolr   r   r,   r9   __static_attributes____classcell__)r   s   @r   r   r      sz    *  %37+/Y01Y 	Y
 Y /0Y (Y Y ( r    r   )ior   typingr   r   r'   compatr   utilr   pytorchr
   pytorch_grad_scalerr   r    r    r   <module>rJ      s&         +   27k 7r    