Skip to content

[Feature]: LSTM layer (Lstm + explicit-state step API) in skainet-lang-coreΒ #823

Description

@michalharakal

🧠 D: DOCUMENT β€” Problem & Opportunity

SKaiNET ships a Gru recurrent layer but no LSTM. Two concrete consumers are blocked:

  1. Parakeet FastConformer-TDT ASR (SKaiNET-parakeet): the TDT prediction network is a
    2-layer LSTM (hidden 640). This blocks running nvidia/parakeet-tdt-0.6b-v3 and the German
    primeline/parakeet-primeline on the SKaiNET stack.
  2. Silero VAD (coraldevboard PLAN item 4.1): "needs new LSTM layer upstream (GRU is the
    template)" β€” the last runtime-Python piece of the SL2610 demo.

Transducer-style decoding additionally needs a single-step form: one token at a time with
the recurrent state (h, c) carried by the caller β€” which is also exactly the shape that lowers
to a fixed-shape StableHLO graph (state as graph I/O, no loop construct needed).

Summary: Add Lstm to skainet-lang-core mirroring Gru (unrolled, primitive-ops-only),
plus an explicit-state step() API (LstmState) required by transducer decoders.

πŸ” A: ASSESS β€” Feasibility & Impact

βœ”οΈ Feasibility

  • Direct analog of Gru.kt: built from existing primitives (matmul/add/narrow/sigmoid/tanh/ multiply/reshape/unsqueeze/concat) β€” no new TensorOps op, no new converter; traces to
    StableHLO exactly like Gru.
  • One extra state tensor (cell c) vs Gru; 4 gates vs 3.

βœ”οΈ Expected Impact

  • Unblocks Parakeet-TDT and Silero VAD; completes the recurrent-layer pair (GRU+LSTM).
  • The step()/LstmState pattern establishes the idiom for streaming/transducer models.

βœ”οΈ Risks / Constraints

  • Gate order: PyTorch order i, f, g, o must be pinned in docs and tests, or imported
    weights silently produce garbage.
  • Dual biases: PyTorch keeps b_ih and b_hh separately; keep both for weight-map fidelity
    (mathematically summable).
  • API consistency: constructor/parameter naming mirrors Gru exactly (matmul-ready [in, 4H]
    / [H, 4H] weights).

βœ”οΈ Dependencies

  • None beyond skainet-lang-core primitives. Autodiff via the standard tape (unrolled).

πŸ“š R: RESEARCH β€” What Must Be Understood First?

  • torch.nn.LSTM contract: gate order i,f,g,o; weight_ih_l{k} [4H, in], weight_hh_l{k} [4H, H], dual biases; recurrence
    c' = fβŠ™c + iβŠ™tanh(g), h' = oβŠ™tanh(c') β€” https://docs.pytorch.org/docs/stable/generated/torch.nn.LSTM.html
  • Hochreiter & Schmidhuber (1997), Long Short-Term Memory, Neural Computation 9(8).
  • Gru.kt as the in-repo design template (unroll-at-trace-time, matmul-ready weights).
  • NeMo RNNT/TDT prediction network uses torch.nn.LSTM (stacked, batch-first) β€” weight
    import must be layer-by-layer single-layer cells to keep the step() API.
  • Open question: also add a symmetric Gru.step()/GruState in the same PR? (proposed: yes,
    follow-up acceptable)

πŸ› οΈ C: CODE β€” Implementation Plan

Development Tasks

  • skainet-lang-core/.../nn/Lstm.kt: Lstm<T, V> module β€” sequence forward
    [B,S,in] β†’ [B,S,H] (unrolled, h0=c0=0) + step(xt, state, ctx): Pair<out, LstmState>
    + LstmState<T, V>(h, c) + initialState(batch, ctx)
  • Numeric test vs independent scalar reference (mirrors GruTest in skainet-backend-cpu)
  • step() consistency test: sequence forward == iterated step() outputs
  • KDoc with gate math + PyTorch import notes (transpose to matmul-ready)
  • .api dump sync (apiCheck)
  • PR referencing this issue (Gitflow: feature/lstm-layer β†’ develop)

Acceptance Criteria

  • Parity vs torch.nn.LSTM golden ≀ 1e-5 (G2 gate of SKaiNET-parakeet)
  • Traces to StableHLO with zero Unsupported ops (conformance row lstm-step planned)
  • KtLint/Detekt/CI green; no regressions

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions