
    i	                     n    d dl mZ d dlmZmZ d dlZddlmZ ddlm	Z	 ddl
mZ dd	lmZ  G d
 de      Zy)    )BytesIO)AnyOptionalN   )torch)get_torch_default_device   )PyTorchShim)PyTorchGradScalerc                   b     e Zd ZdZ	 	 	 	 	 dded   dededee   ded   f
 fd	Zd
 Z	d Z
 xZS )TorchScriptShima  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                     |/t        |t        j                  j                        st	        d      t
        |   ||||||       y )NzUPyTorchScriptShim must be initialized with ScriptModule or None (for deserialization))
isinstancer   jitScriptModule
ValueErrorsuper__init__)selfr   configr   r   r   r   	__class__s          h/var/www/vps2.regionflexible.com/Desarrollo/venv/lib/python3.12/site-packages/thinc/shims/torchscript.pyr   zTorchScriptShim.__init__   sH     Zuyy7M7M%Ng  		?KQWX    c                     t               }t        j                  j                  | j                  |       |j                  d       |j                         }| j                  |d}t        j                  |      S )Nr   )r   r   )
r   r   r   save_modelseekgetvaluecfgsrslymsgpack_dumps)r   filelikemodel_bytesmsgs       r   to_byteszTorchScriptShim.to_bytes/   sX    9		t{{H-a'')K8""3''r   c                    t               }t        j                  |      }|d   | _        t	        |d         }|j                  d       |j                  dk(  rt        j                  d      n|}t        j                  j                  ||      | _        | j                  j                  |       | j                  j                  |       | S )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'   r.   s         r   
from_byteszTorchScriptShim.from_bytes7   s    )+!!*-x=3w<(a /5kkU.Bu||E*iinnXLnIvf%r   )NNFNN)__name__
__module____qualname____doc__r   r   boolr   r   r*   r6   __classcell__)r   s   @r   r   r      so    *  %37+/Y01Y 	Y
 Y /0Y (Y (r   r   )ior   typingr   r   r%   compatr   utilr   pytorchr
   pytorch_grad_scalerr   r    r   r   <module>rD      s&         +   27k 7r   