Skip to content

onnx: LSTM / GRU / RNN, unrolled over the static sequence length - #94

Merged
sbryngelson merged 1 commit into
mainfrom
onnx/recurrent
Jul 22, 2026
Merged

onnx: LSTM / GRU / RNN, unrolled over the static sequence length#94
sbryngelson merged 1 commit into
mainfrom
onnx/recurrent

Conversation

@sbryngelson

Copy link
Copy Markdown
Owner

The last big compositional tier: the recurrent family, unrolled the same way Loop (#91) proved out — the whole recurrence becomes one fused program.

What

  • LSTM — per timestep, one fused linear over the stacked input-side gate weights and one over the recurrent side (iofc order), sliced per gate. Forward, reverse, and bidirectional; Y, Y_h, Y_c outputs.
  • GRUzrh order, both linear_before_reset forms (the before-reset form reuses the fused recurrent matmul; the after-reset form takes one extra linear over the h-gate slice).
  • RNN — vanilla tanh.
  • Shared scope, rejected loudly: non-default activations, clip, LSTM peepholes, per-sample sequence_lens, layout=1, tensor-valued weights/initial states.
  • Importer hardening these forced: ONNX marks omitted optional inputs and skipped outputs with empty-string names; "" inputs now resolve to None and "" outputs are no longer stored (previously they'd pollute the value table under the key "").
  • Docs: table rows + scope note; also updates the two Gelu entries onnx: support tanh-approximate Gelu #92 made stale (exact + tanh-approximate now).

Verification (on-device, M5)

pytest tests/test_onnx.py: 166 passed — LSTM forward/bidirectional/Y_h, GRU in both reset forms, reverse RNN, all against onnxruntime at fp16 tolerance, plus the peephole reject. One test bug caught during rebase verification: the weight seeds used Python's salted hash(), making the numerics nondeterministic across processes — now zlib.crc32, verified stable across three consecutive runs. ruff, 2-space pylint, pyright (0 errors) clean.

Each layer unrolls into the single fused program: per timestep, one linear
over the stacked input-side gate weights and one over the recurrent side
(iofc order for LSTM, zrh for GRU), sliced per gate. Forward, reverse, and
bidirectional directions; both GRU linear_before_reset forms (the
before-reset form reuses the fused recurrent matmul, the after-reset form
takes one extra linear over the h-gate slice). Weights, biases, and
initial states must be constants; default activations only; clip,
peepholes, per-sample sequence_lens, and layout=1 raise.

Importer hardening required by these ops: ONNX marks omitted optional
inputs and unused optional outputs with empty-string names - inputs now
resolve '' to None and outputs named '' are not stored (previously a
skipped output would have polluted the value table under the key '').

165 tests pass on-device (M5): LSTM forward/bidirectional/Y_h, GRU in
both reset forms, reverse RNN - all against onnxruntime at fp16
tolerance - plus the peephole reject.
@sbryngelson
sbryngelson merged commit 9c2e096 into main Jul 22, 2026
13 checks passed
@sbryngelson
sbryngelson deleted the onnx/recurrent branch July 22, 2026 23:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant