
    i                         d dl mZmZmZ ddlmZ ddlmZ ddlm	Z	m
Z
mZ ddlmZmZmZ 	 	 	 	 	 	 dd	ed
   dee   dee   dedee	   ded   deeef   fdZdefdZy)    )AnyCallableOptional   )torch)Model)PyTorchGradScalerPyTorchShimTorchScriptShim   )convert_pytorch_default_inputsconvert_pytorch_default_outputsforwardNtorchscript_modelztorch.jit.ScriptModuleconvert_inputsconvert_outputsmixed_precisiongrad_scalerdeviceztorch.devicereturnc                 r    |t         }|t        }t        dt        ||dt	        | |||      gddd      S )a  Wrap a TorchScript model, so that it has the same API as Thinc models.

    torchscript_model:
        The TorchScript module. A value of `None` is also possible to
        construct a shim to deserialize into.
    convert_inputs:
        Function that converts inputs and gradients that should be passed
        to the model to Torch tensors.
    convert_outputs:
        Function that converts model outputs and gradients from Torch tensors
        Thinc arrays.
    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.
    Npytorch_script)r   r   )modelr   r   r   )nInO)attrsshimsdims)r   r   r   r   r   )r   r   r   r   r   r   s         p/var/www/vps2.regionflexible.com/Desarrollo/venv/lib/python3.12/site-packages/thinc/layers/torchscriptwrapper.pyTorchScriptWrapper_v1r       sY    @ 79!/OT' /'	
 %     r   c                    | j                   d   }t        |t              st        d      | j                  d   }| j                  d   }|j
                  }t        |t        j                  j                        st        d      t        j                  j                  |      }|j                  }|j                  }|j                  }t        ||||||      S )zzConvert a PyTorch wrapper to a TorchScript wrapper. The embedded PyTorch
    `Module` is converted to `ScriptModule`.
    r   z6Expected PyTorchShim when converting a PyTorch wrapperr   r   z*PyTorchShim does not wrap a PyTorch module)r   r   r   r   r   )r   
isinstancer
   
ValueErrorr   _modelr   nnModulejitscript_grad_scaler_mixed_precisionr   r    )	r   shimr   r   pytorch_modelr   r   r   r   s	            r   pytorch_to_torchscript_wrapperr.   B   s     ;;q>DdK(QRR[[!12Nkk"34OKKMmUXX__5EFF		((7##K++O[[F %'' r!   )NNNFNN)typingr   r   r   compatr   r   r   r   r	   r
   r   pytorchwrapperr   r   r   boolr    r.    r!   r   <module>r4      s    * *   C C  =A)-*.!/3'+2 892X&2 h'2 	2
 +,2 ^$2 38_2j% r!   