o
    i	                     @   sV   d dl Z d dlZd dlZd dlmZ d dlmZmZmZm	Z	m
Z
mZ G dd dZdS )    N)Path)AnyCallableDictOptionalTupleUnionc                   @   s(  e Zd ZU dZdZeed< e Z	ejed< e
ed< eed< ee ed< d*d	ed
efddZdedeeedef f fddZdedefddZdedeeedef f fddZdd Zejdd Zdd Zdedefdd Zd!eeef fd"d#Zd!eeef dd fd$d%Zd&d' Zd+d(d)Z dS ),ShimaZ  Define a basic interface for external models. Users can create subclasses
    of 'shim' to wrap external libraries. We provide shims for PyTorch.

    The Thinc Model class treats Shim objects as a sort of special type of
    sublayer: it knows they're not actual Thinc Model instances, but it also
    knows to talk to the shim instances when doing things like using transferring
    between devices, loading in parameters, optimization. It also knows Shim
    objects need to be serialized and deserialized with to/from bytes/disk,
    rather than expecting that they'll be msgpack-serializable.
    r   	global_idglobal_id_lockcfg_model
_optimizerNmodel	optimizerc                 C   sb   t j t  jd7  _t j| _W d    n1 sw   Y  |d ur&t|ni | _|| _|| _d S )N   )r	   r   r
   iddictr   r   r   )selfr   configr    r   D/home/ubuntu/.local/lib/python3.10/site-packages/thinc/shims/shim.py__init__   s   

zShim.__init__is_trainreturn.c                 C      t NNotImplementedError)r   inputsr   r   r   r   __call__#      zShim.__call__fwd_argsc                 C   s   | |dd\}}|S )NFr   r   )r   r"   Ybackpropr   r   r   predict&   s   zShim.predictc                 C   s   | |ddS )NTr#   r   )r   r"   r   r   r   begin_update*   s   zShim.begin_updatec                 C   r   r   r   )r   r   r   r   r   finish_update-   r!   zShim.finish_updatec                 c   s    d V  d S r   r   )r   paramsr   r   r   
use_params0   s   
zShim.use_paramsc                 C   s
   t | S r   )copydeepcopyr   r   r   r   r+   4   s   
z	Shim.copydevice_type	device_idc                 C   r   r   r   )r   r.   r/   r   r   r   	to_device7   r!   zShim.to_devicepathc                 C   sX   |   }t|trt|n|}|d}|| W d    d S 1 s%w   Y  d S )Nwb)to_bytes
isinstancestrr   openwrite)r   r1   
bytes_datafile_r   r   r   to_disk:   s
   "zShim.to_diskc                 C   sR   t |tr	t|n|}|d}| }W d    n1 sw   Y  | |S )Nrb)r4   r5   r   r6   read
from_bytes)r   r1   r9   r8   r   r   r   	from_disk@   s
   

zShim.from_diskc                 C   r   r   r   r-   r   r   r   r3   F   r!   zShim.to_bytesc                 C   r   r   r   )r   datar   r   r   r=   I   r!   zShim.from_bytes)NN)r   r	   )!__name__
__module____qualname____doc__r
   int__annotations__	threadingLockr   r   r   r   r   boolr   r   r    r&   r'   r(   
contextlibcontextmanagerr*   r+   r5   r0   r   r   r:   r>   r3   r=   r   r   r   r   r	      s(   
 "	"
r	   )rI   r+   rF   pathlibr   typingr   r   r   r   r   r   r	   r   r   r   r   <module>   s     