
    i$                         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	 d dl
Z
ddlmZmZmZmZ ddlmZ ddlmZ ddlmZmZ dd	lmZmZmZmZmZ d
dlmZ d
dlm Z   G d de       Z!dede"fdZ#dede"dddefdZ$y)    N)BytesIO)AnyCallableDictOptionalcast   )CupyOpscontext_poolsget_current_opsset_gpu_allocator)torch)	Optimizer)
ArgsKwargsFloatsXd)convert_recursiveget_torch_default_deviceiterate_recursivetorch2xpxp2torch   )PyTorchGradScaler)Shimc                       e Zd ZdZ	 	 	 	 	 	 	 ddedededee   ded   deeege	f      d	eeee	dgef      f fd
Z
d Zed        ZdedefdZdefdZdefdZej(                  d        ZdedefdZd Zd Z xZS )PyTorchShima  Interface between a PyTorch model and a Thinc Model. This container is
    *not* a Thinc Model subclass itself.

    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.
    serialize_model:
        Callback that receives the wrapped PyTorch model as its argument and
        returns a "bytes" representation of the same. The representation should
        contain all the necessary information to fully deserialize the model.
    deserialize_model:
        Callback that receives the default PyTorch model (passed to the constructor), the
        serialized "bytes" representation and a PyTorch device. It should return a
        fully deserialized model on the target device as its result.
    model	optimizermixed_precisiongrad_scalerdevicetorch.deviceserialize_modeldeserialize_modelc	                    t         |   |||       |
t               }||j                  |       |t	        |      }|j                  |       || _        || _        ||nt        | _	        ||nt        | _        t        j                  Yt        t               t              r@t!        j"                         }	d|	vr'ddlm}
 t)        d        |
       j+                          y y y y )Npytorchr   )get_default_memory_pool)super__init__r   tor   to__grad_scaler_mixed_precisiondefault_serialize_torch_model_serialize_modeldefault_deserialize_torch_model_deserialize_modelr
   xp
isinstancer   r   getcupyr&   r   free_all_blocks)selfr   configr   r   r   r    r"   r#   poolsr&   	__class__s              d/var/www/vps2.regionflexible.com/Desarrollo/venv/lib/python3.12/site-packages/thinc/shims/pytorch.pyr(   zPyTorchShim.__init__0   s     		2>-/FHHV+O<K' / * . 	 !, 0 	 ::!j1BG&L!%%'E%8!),')99;	 & 'M!    c                 P    |r| j                  |      S | j                  |      d fS )Nc                      y)N. )as    r:   <lambda>z&PyTorchShim.__call__.<locals>.<lambda>a   s    r;   )begin_updatepredict)r6   inputsis_trains      r:   __call__zPyTorchShim.__call__]   s*    $$V,,<<'66r;   c                 z    t        | j                  j                         d       }|
t               S |j                  S N)next_model
parametersr   r    )r6   ps     r:   r    zPyTorchShim.devicec   s2    '')409+--88Or;   rC   returnc                 x   | j                   j                          t        j                         5  t        j                  d| j
                        5   | j                   |j                  i |j                  }ddd       ddd       | j                   j                          S # 1 sw Y   -xY w# 1 sw Y   1xY w)zPass inputs through to the underlying PyTorch model, and return the
        output. No conversions are performed. The PyTorch model is set into
        evaluation mode.
        cuda)device_typeenabledN)	rI   evalr   no_gradautocastr,   argskwargstrain)r6   rC   outputss      r:   rB   zPyTorchShim.predictk   s    
 	]]_ 	EFD<Q<QR E%$++v{{DfmmDE	E 	E E	E 	Es#   "B0'B$8B0$B-	)B00B9c                      j                   j                          t        j                  d j                        5    j                   j
                  i j                  }ddd        fd}|fS # 1 sw Y   xY w)a'  Pass the inputs through to the underlying PyTorch model, keeping
        track of which items in the input are tensors requiring gradients.
        If the model returns a single value, it is converted into a one-element tuple.
        Return the outputs and a callback to backpropagate.
        rN   )rP   Nc                    j                   j                  | j                  d   d      | j                  d<   t        j                  j
                  | j                  i | j                   g }t        j                  j                  j                         t        d             D ]*  }|j                  |j                  |j                         , j                   j                  |      }|rd }nd }t        d |      S )Ngrad_tensorsT)inplacec                     t        | d      S Ngradhasattrxs    r:   r@   z<PyTorchShim.begin_update.<locals>.backprop.<locals>.<lambda>   s    GAv,> r;   c                 f    | j                   | j                   j                         S | j                   S rG   )r^   zero_ra   s    r:   r@   z<PyTorchShim.begin_update.<locals>.backprop.<locals>.<lambda>   s!    qvv7IQVV\\^ qvv r;   c                     | j                   S rG   )r^   ra   s    r:   r@   z<PyTorchShim.begin_update.<locals>.backprop.<locals>.<lambda>   s
    QVV r;   c                     t        | d      S r]   r_   ra   s    r:   r@   z<PyTorchShim.begin_update.<locals>.backprop.<locals>.<lambda>   s    wq&/A r;   )r+   scalerU   r   autogradbackwardrT   	itertoolschainrI   rJ   r   r^   appendunscaler   )gradsrZ   
torch_data	found_infgrad_getrC   r6   s        r:   backpropz*PyTorchShim.begin_update.<locals>.backprop   s     ,0+<+<+B+B^,d ,C ,ELL( NN##UZZ@5<<@ L'oo&&(!">G 9
 ??. ''
89 ))11,?I U+$%A8VTTr;   )rI   rV   r   rS   r,   rT   rU   )r6   rC   outputrr   s   ``  r:   rA   zPyTorchShim.begin_updatew   su     	 ^^FD,A,AB 	@ T[[&++??F	@	U> xE	@ 	@s   'A77B c                    | j                   j                         D ]  \  }}|j                  | j                  j                  sx || j
                  |ft        t        t        |j                              t        t        t        |j                                    \  }}t        |d|j                        |_	        |j                  j                           | j                  j                          y )NT)requires_gradr    )rI   named_parametersr^   r+   rp   idr   r   r   datar   r    rd   update)r6   r   namero   paramr^   s         r:   finish_updatezPyTorchShim.finish_update   s     $ < < > 	(D**))33"+$Xx
'@AXx
'@A#KE4
 '/T*:K:K'JO %%'	( 	  "r;   c              #     K   d| j                    d}i }|j                         D ]N  \  }}t        |d      s|j                  |      s%t	        || j
                        ||j                  |d      <   P |r| j                  j                         j                         D ci c]  \  }}||j                          }}}| j                  j                  |       d  | j                  j                  |       y d  y c c}}w w)Npytorch__
startswith)r     )rw   itemsr`   r   r   r    replacerI   
state_dictcloneload_state_dict)r6   params
key_prefixr   kvbackups          r:   
use_paramszPyTorchShim.use_params   s     y*

LLN 	XDAqq,'ALL,D8@4;;8W
199Z45	X /3{{/E/E/G/M/M/OPtq!alPFPKK''
3KK''/ Qs   5DD
AD#D>A	DrO   	device_idc                     |dk(  r| j                   j                          y |dk(  r| j                   j                  |       y d| d}t        |      )NcpugpuzInvalid device_type: z. Try 'cpu' or 'gpu')rI   r   rN   
ValueError)r6   rO   r   msgs       r:   	to_devicezPyTorchShim.to_device   sK    %KKOOE!KKY')+6JKCS/!r;   c                     | j                  | j                        }| j                  |d}t        j                  |      S )N)r7   state)r.   rI   cfgsrslymsgpack_dumps)r6   model_bytesr   s      r:   to_byteszPyTorchShim.to_bytes   s5    ++DKK8K8""3''r;   c                     t               }t        j                  |      }|d   | _        | j	                  | j
                  |d   |      | _        | j                  j                  |       | S )Nr7   r   )r   r   msgpack_loadsr   r0   rI   r+   r*   )r6   
bytes_datar    r   s       r:   
from_byteszPyTorchShim.from_bytes   s\    )+!!*-x=--dkk3w<Pf%r;   )NNFNNNN)__name__
__module____qualname____doc__r   boolr   r   r   bytesr(   rE   propertyr    r   rB   rA   r   r|   
contextlibcontextmanagerr   strintr   r   r   __classcell__)r9   s   @r:   r   r      s   6  %37+/<@SW+<+< 	+<
 +< /0+< (+< "(C5%<"89+< $Hc5.-I3-N$OP+<Z7  
j 
S 
+ : + Z#y #$  "S "S "(
r;   r   r   rL   c                     t               }t        j                  | j                         |       |j	                  d       |j                         S )zSerializes the parameters of the wrapped PyTorch model to bytes.

    model:
        Wrapped PyTorch model.

    Returns:
        A `bytes` object that encapsulates the serialized model parameters.
    r   )r   r   saver   seekgetvalue)r   filelikes     r:   r-   r-      s=     yH	JJu!8,MM!r;   state_bytesr    r!   c                     t        |      }|j                  d       | j                  t        j                  ||             | j                  |       | S )aA  Deserializes the parameters of the wrapped PyTorch model and
    moves it to the specified device.

    model:
        Wrapped PyTorch model.
    state_bytes:
        Serialized parameters as a byte stream.
    device:
        PyTorch device to which the model is bound.

    Returns:
        The deserialized model.
    r   )map_location)r   r   r   r   loadr)   )r   r   r    r   s       r:   r/   r/      sD      {#HMM!	%**XFCD	HHVLr;   )%r   rj   ior   typingr   r   r   r   r   r   backendsr
   r   r   r   compatr   
optimizersr   typesr   r   utilr   r   r   r   r   pytorch_grad_scalerr   shimr   r   r   r-   r/   r>   r;   r:   <module>r      s       6 6  Q Q  " (  3 B$ BJ  ",:r;   