TorchScript (.ts) files are the portable, runtime-independent format for deploying PyTorch models without Python
TorchScript is a subset of Python that PyTorch can compile and serialize to a .ts file. Unlike a Python script, a .ts file can be loaded by the libtorch C++ runtime with no Python interpreter present — enabling use in Max/MSP, Pure Data, and other non-Python environments. export_to_ts() calls torch.jit.script() to compile the model, then saves it. This means the model’s logic must be expressible in TorchScript’s constrained type system (typed annotations are mandatory, Python-native features like class inspection are limited). The .ts format is the lingua franca connecting training (PyTorch/Python) to performance (Max/Pd/C++).
Examples
Call model.export_to_ts('rave_piano.ts') after training to produce the deployable file. Load in Max: nn~ rave_piano.ts forward. Any version of libtorch that matches the PyTorch training version can load it.
Assessment
Explain why @torch.jit.export is required on each method you want accessible from nn~. What Python feature would prevent a method from being successfully TorchScript-compiled, and how would you work around it?