PyTorch models are Python code—trainable but not portable. TorchScript converts Python to an intermediate representation (IR) that can run without Python. Two methods: scripting (convert Python directly) and tracing (record a forward pass). ONNX is a format for exporting models to other frameworks. This post covers all three and their limitations.
TorchScript via Scripting
import torch
import torch.nn as nn
class SimpleModel(nn.Module):
def forward(self, x):
if x.mean() > 0:
return x * 2
else:
return x * -1
model = SimpleModel()
scripted = torch.jit.script(model)
x = torch.randn(4, 10)
output = scripted(x)
print(f"Scripted output: {output.shape}")
Output:
Scripted output: torch.Size([4, 10])
Scripting converts Python to TorchScript IR, supporting control flow (if/for/while) and Python operations.
Conclusion
TorchScript enables portable, production-friendly models. Scripting handles control flow; tracing is faster but brittle. ONNX exports to other frameworks. Understanding each method's strengths helps you pick the right deployment strategy. Next: we'll explore hooks for model surgery and feature extraction.

