Skip to content

[Feature] Add rmsnorm backward (+ forward saves rstd) to enable training / downstream (quack) integration #769

Description

@jhinpan

Context

Spun out of RFC #749 (a contribution path for FlyDSL kernels into downstream OSS libraries). While analyzing the quack rmsnorm interface gap, we confirmed FlyDSL's norm family is forward-only:

  • kernels/rmsnorm_kernel.py and kernels/layernorm_kernel.py expose only build_* / launch_* forward paths.
  • There are no *bwd* / *backward* / *grad* files under kernels/, and a repo-wide autograd search returns 0.
  • FlyDSL's own tests/kernels/test_rmsnorm.py exercises forward only (no gradient checks).

This is expected for an inference kernel library, but it is the blocker for training use cases and for backing a training-capable library (quack).

Goal

Add a correct, performant rmsnorm backward so FlyDSL can serve training, and so the downstream-backend PoC in #749 can target fwd+bwd rather than forward-only.

Scope / tasks

  1. Backward kernel — compute dx, dweight (and dbias if bias is added). Accumulate weight/bias partials in fp32 for numerical accuracy (cf. quack's rmsnorm_bwd: per-SM fp32 partials, then reduce).
  2. Forward must save backward state — the current forward does not emit rstd (the per-row 1/RMS). Backward needs it, so add an optional store_rstd / rstd output to the forward path instead of recomputing.
  3. Autograd wrapper — a torch.autograd.Function (forward saves x, weight, rstd; backward calls the new kernel) so quack-style call sites get gradients transparently.
  4. Fused-add (residual / prenorm) backwardkernels/rmsnorm_kernel.py already has a separate fused-add forward module; backward should handle the residual path too (grad w.r.t. residual_out).
  5. Tests — extend tests/kernels/test_rmsnorm.py with gradient checks against a torch reference (bf16 / fp16 / fp32; several M/N including the N<=2048 small-N path). Reuse quack's tolerance style.
  6. (stretch) layernorm backward — same pattern; can be a follow-up.

Why now

Direct enabler for #749 (training-capable downstream integration). Without this, any "fwd+bwd parity" with quack is net-new kernel work rather than interface adaptation.

Refs

  • quack: rmsnorm_bwd (quack/rmsnorm.py:1252), RMSNormFunction (:1368).
  • FlyDSL: build_rmsnorm_module (kernels/rmsnorm_kernel.py:113), forward has no rstd output; launch_fused_add_rmsnorm (:604).
  • Related: [RFC] FlyDSL kernels in downstream libraries #749.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions