o
    i                     @   s   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dZdefddZdS )    )AnyCallableOptional   )torch)Model)PyTorchGradScalerPyTorchShimTorchScriptShim   )convert_pytorch_default_inputsconvert_pytorch_default_outputsforwardNFtorchscript_modelztorch.jit.ScriptModuleconvert_inputsconvert_outputsmixed_precisiongrad_scalerdeviceztorch.devicereturnc              
   C   sD   |du rt }|du rt}tdt||dt| |||dgd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    r   S/home/ubuntu/.local/lib/python3.10/site-packages/thinc/layers/torchscriptwrapper.pyTorchScriptWrapper_v1   s"    r   r   c           	      C   s~   | j d }t|tstd| jd }| jd }|j}t|tjjs&tdtj	
|}|j}|j}|j}t||||||d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   r   r   r   pytorch_to_torchscript_wrapperB   s(   



r+   )NNNFNN)typingr   r   r   compatr   r   r   r   r   r	   r
   pytorchwrapperr   r   r   boolr   r+   r   r   r   r   <module>   s6    

5