Exporting universal PyTorch models using TorchScript and ONNX
We have discussed serving PyTorch models extensively in the previous sections of this chapter, which is perhaps the most critical aspect of operationalizing PyTorch models in production systems. In this section, we will look at another important aspect – exporting PyTorch models. We have already learned how to save PyTorch models and load them back from disk in the classic Python scripting environment. But we need more ways of exporting PyTorch models. Why?
Well, for starters, the Python interpreter allows only one thread to run at a time using the global interpreter lock (GIL). This keeps us from parallelizing operations. Secondly, Python might not be supported on every system or device on which we might want to run our models. To address these problems, PyTorch offers support for exporting its models in an efficient format and in a platform- or language-agnostic manner such that a model can be run in environments...