Pytorch1.0 use torch script to export and save the Model

Source: Internet
Author: User
Tags pytorch

Python's ease of use and the dynamic graph feature of pytorch make pytorch more and more popular in academic research. However, in the production environment, due to Python's Gil and other features, high concurrency and low latency requirements may not be met, and a C ++ interface is required. In addition to exporting the model to onnx, pytorch1.0 provides a new solution: pytorch Training Model-saving the model through the torch script intermediate script-C ++ Loading Model. In recent work, we need to try to make a conversion and summarize the steps and pitfalls.

There are two ways to convert the torch model into a C ++ interface readable model using torch Script: trace & script. trace is simpler than script, but it is only suitable for fixed network models, that is, there is no control flow in forward, because trace only saves the actual path when running. If the forward function has a control flow, it must be implemented in script mode.

As the name suggests, trace is to go through the data operation path. Official example:

 
import torchdef foo(x, y):    return 2*x + ytraced_foo = torch.jit.trace(foo, (torch.rand(3), torch.rand(3)))

 

 

 

 

The script is a little complicated and mainly involves three changes:

1. The model inherits from Nn. Model to torch. JIT. scriptmodule.

2. Add @ torch. JIT. script_method before the forward Function

3. Add @ torch. JIT. script before other functions to be called.

 

Pitfalls and solutions:

A. Torch script default function or method parameters are of the tensor type. If this parameter is not required, a compilation error of Type Mismatch will be reported when a non-tensor parameter is called.

Python3 can be directly:

Def example_func (param_1: tensor, param_2: int, param_3: list [int]):

 

 

Python2 needs to be annotated with type:

Def example_func (param_1, param_2, param_3 ):

# Type: (tensor, Int, list [int])-> Tensor

 

 

 

BIn the. model method, orward adds @ torch. JIT. script_method, And the _ init _ function does not need to be used.

C. As mentioned above, torch scrip supports a subset of pytorch, which means some functions are not supported, such as not Boolean, pass, list slice assignment, and CPU and GPU switching value. to (), you need to find a way to bypass. In the discussion board on GitHub, it seems that the new version has supported the not operation and has not been verified.

 

Conclusion: The current preview version of pytorch 1.0 has a lot of room for optimization, at least in the function set supported by torch script. It is not recommended to use it. Let's wait for the stable version to be released.

  

 

Original content. For more information, see the source.

 

References:

Https://pytorch.org/docs/master/jit.html

Https://pytorch.org/tutorials/beginner/deploy_seq2seq_hybrid_frontend_tutorial.html

Pytorch1.0 use torch script to export and save the Model

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.