
    h	                     \    S SK r S SKrS SKrS SKJr  S SKJrJrJrJ	r	J
r
Jr   " S S5      rg)    N)Path)AnyCallableDictOptionalTupleUnionc                      \ rS rSr% SrSr\\S'   \R                  " 5       r
\R                  \S'   \\S'   \\S'   \\   \S'   S S
\S\4S jjrS\S\\\S\4   4   4S jrS\S\4S jrS\S\\\S\4   4   4S jrS r\R.                  S 5       rS rS\S\4S jrS\\\4   4S jrS\\\4   SS 4S jrS r S!S jr!Sr"g	)"Shim   a:  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                     [         R                     [         =R                  S-  sl        [         R                  U l        S S S 5        Ub  [	        U5      O0 U l        Xl        X0l        g ! , (       d  f       N0= f)N   )r   r   r   iddictr   r   r   )selfr   configr   s       J/home/james-whalen/.local/lib/python3.13/site-packages/thinc/shims/shim.py__init__Shim.__init__   sP      NNaNnnDG ! $*#54<2# ! s   /A**
A8is_trainreturn.c                     [         eNNotImplementedError)r   inputsr   s      r   __call__Shim.__call__#       !!    fwd_argsc                     U " USS9u  p#U$ )NFr    )r   r(   Ybackprops       r   predictShim.predict&   s    8e4r'   c                     U " USS9$ )NTr*   r+   )r   r(   s     r   begin_updateShim.begin_update*   s    Ht,,r'   c                     [         er    r!   )r   r   s     r   finish_updateShim.finish_update-   r&   r'   c              #      #    S v   g 7fr    r+   )r   paramss     r   
use_paramsShim.use_params0   s     s   c                 .    [         R                  " U 5      $ r    )copydeepcopyr   s    r   r;   	Shim.copy4   s    }}T""r'   device_type	device_idc                     [         er    r!   )r   r?   r@   s      r   	to_deviceShim.to_device7   r&   r'   pathc                     U R                  5       n[        U[        5      (       a  [        U5      OUnUR	                  S5       nUR                  U5        S S S 5        g ! , (       d  f       g = f)Nwb)to_bytes
isinstancestrr   openwrite)r   rD   
bytes_datafile_s       r   to_diskShim.to_disk:   sG    ]]_
'c22tDzYYt_KK
# __s   A
A-c                     [        U[        5      (       a  [        U5      OUnUR                  S5       nUR	                  5       nS S S 5        U R                  W5      $ ! , (       d  f       N= f)Nrb)rH   rI   r   rJ   read
from_bytes)r   rD   rM   rL   s       r   	from_diskShim.from_disk@   sM    'c22tDzYYt_J z** _s   A
A,c                     [         er    r!   r=   s    r   rG   Shim.to_bytesF   r&   r'   c                     [         er    r!   )r   datas     r   rS   Shim.from_bytesI   r&   r'   )r   r   r   r   )NN)r   r   )#__name__
__module____qualname____firstlineno____doc__r   int__annotations__	threadingLockr   r   r   r   r   boolr   r   r$   r.   r1   r4   
contextlibcontextmanagerr8   r;   rI   rB   r	   r   rN   rT   rG   rS   __static_attributes__r+   r'   r   r   r      s    	 Is%.^^%5NINN5	IK$c $3 $" "%Xc3h=O8O2P "  -S -U3c8J3J-K -"  #"S "S "$E#t), $+eCI. +6 +""r'   r   )re   r;   rb   pathlibr   typingr   r   r   r   r   r	   r   r+   r'   r   <module>rj      s#        > >B" B"r'   