Add GRU layer (composed, trainable, StableHLO-exportable)#772
Merged
Conversation
First recurrent layer in SKaiNET. GRU is implemented as a Module composing existing primitive ops (matmul/add/sigmoid/tanh/multiply/narrow/concat), unrolled over the static sequence length at trace time — StableHLO has no loop construct, so any recurrence must unroll regardless, and a composed layer reuses every existing, already-validated converter (no new converter needed). Single-layer, unidirectional, batch-first: [B,S,D] -> [B,S,H]. PyTorch-compatible gate math/order (reset, update, new), weights stored matmul-ready: r = sigmoid(x·Wir + h·Whr + b); z = sigmoid(x·Wiz + h·Whz + b) n = tanh(x·Win + r ⊙ (h·Whn) + b); h' = (1-z) ⊙ n + z ⊙ h Trainable for free: every primitive in the unrolled cell already has a correct autodiff backward (sigmoid out*(1-out), tanh 1-out^2, matmul/add/sub/mul/ narrow/concat). - nn/Gru.kt: the layer (Module + ModuleParameters, 4 params weight_ih/hh + bias_ih/hh). - nn/dsl/NetworkBuilder.kt: GRU dsl item + GruImpl + gru(hiddenSize){} builder. - Tests: eager forward vs independent reference (GruTest), input-grad finite-diff (ConvPoolBackwardTest.gru_backward_input_matches_finite_diff), DSL smoke (GruDslTest). Bidirectional / num_layers / packed sequences are future work. End-to-end IREE export is validated separately in the conformance suite. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
📖 Documentation Preview The documentation has been built successfully for this PR. Generated Files:
Artifacts:
This comment will be updated automatically when the PR is updated. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #217.
What
The first recurrent layer in SKaiNET. GRU is implemented as a
Modulecomposing existing primitive ops, unrolled over the static sequence length at trace time.Why composed rather than a fused op + dedicated converter: StableHLO export has no control flow (no
while/scan), so a recurrence must unroll regardless of approach. A composed layer therefore emits the same unrolled primitive graph a fusedconvertGruwould — but reuses every existing, already-validated converter (matmul/add/sigmoid/tanh/multiply/narrow/concat) with zero converter changes, and is trainable for free because every one of those primitives already has a correct autodiff backward.Scope (first cut)
Single-layer, unidirectional, batch-first:
[B, S, D] → [B, S, H]. PyTorch-compatible gate math and order (reset, update, new); weights stored matmul-ready sotorch.nn.GRUweights load after a transpose:Bidirectional /
num_layers/ packed sequences are explicit future work.Changes
nn/Gru.kt— the layer (Module+ModuleParameters; paramsweight_ih/weight_hh[*,3H],bias_ih/bias_hh[3H]), unrolled forward.nn/dsl/NetworkBuilder.kt—GRUdsl item +GruImpl+gru(hiddenSize){}builder (input size inferred from the preceding layer).Tests
GruTest— eager forward vs an independent scalar reference (all gates + recurrence + hidden feedback).ConvPoolBackwardTest.gru_backward_input_matches_finite_diff— input gradient vs central finite difference (proves grads flow through the unrolled cell).GruDslTest—network { gru(8) }wires aGrumodule.All three modules' suites pass locally (
skainet-lang-core,skainet-backend-cpu,skainet-compile-dag).Follow-up
End-to-end StableHLO → IREE export validation is added in the conformance suite (gated on a release carrying this layer), same flow as the norm/upsample ops.
🤖 Generated with Claude Code