You exported a PyTorch model with an nn.LSTM inside and one of two bad things happened:
- Mode A — loud failure:
torch.onnx.exportthrew aRuntimeErrorabout control flow or scripting. - Mode B — silent failure: The export succeeded, the
.onnxfile exists, but when you run inference inonnxruntimethe outputs diverge from PyTorch's outputs by a lot (not numerical noise — real differences).
Both modes have the same root cause and the same fix.
The root cause
PyTorch's LSTM module uses internal control flow to iterate over the time dimension. When you ask ONNX export to handle a dynamic sequence length, the exporter has to capture that loop as a runtime Loop op. The capture uses torch.jit.script, which struggles with LSTM internals — sometimes failing, sometimes "succeeding" with a graph that has subtly wrong outputs.
The fix: static seq_len at export
The full guide with all the patterns is at PyTorch LSTM to ONNX.
Validating you fixed it
Don't ship to MT5 without checking PyTorch's output matches ONNX's output:
If the assert passes (max-diff under 1e-4), the export is correct. If it fails, the static-seq_len pattern wasn't applied correctly.
If you need variable seq_len at inference
Three options, none of them ideal:
- Pad to maximum length + train with masking. Most common pattern.
- Export multiple models, one per length, load all in MQL5.
- Try
dynamo=True— the newer exporter. May work, may not, always validate.
For 95% of MT5 use cases, the lookback window is constant by design. Pick a number and freeze it.