flashinfer.cake_rmsnorm_train.cake_rmsnorm_train_forward

flashinfer.cake_rmsnorm_train.cake_rmsnorm_train_forward(x: Tensor, w: Tensor, eps: float, *, residual: Tensor | None = None, out: Tensor | None = None, rstd: Tensor | None = None, residual_out: Tensor | None = None, deterministic: bool = True) → tuple[Tensor, ...]

Fused BF16 RMSNorm forward with FP32 statistics.

Parameters:
  • x (torch.Tensor) – BF16 [T, H] input of any layout. Row-strided views with a contiguous feature dimension, 32-byte aligned storage and a row stride that is a multiple of 32 bytes are read in place; other layouts (odd column offset, odd row stride, transposed storage) are materialized once. With deterministic=False a tensor of more dimensions, [..., H], is normalized row by row when its feature dimension is contiguous and its leading dimensions are contiguous with one another in row-major order (stride(i) == stride(i + 1) * size(i + 1); a [T, heads, H] slice of a wider [T, heads, W] buffer qualifies): the leading dimensions collapse into prod(...) rows, the outputs take the input’s leading shape (rstd is [...]), w is shared by every row and the backward’s dw sums them all; any other N-D layout raises ValueError naming the rule. With residual this is the residual stream h and the normalized value is h_new = BF16(h + residual).

  • w (torch.Tensor) – BF16 [H] weight, read in place when contiguous and 32-byte aligned, else copied once. With deterministic=False an FP32 [H] weight – the effective weight 1 + w a zero-centred model computes in FP32 – is accepted on the routes that ship the FP32-weight programs (cake_jit.weight_dtypes), else ValueError.

  • eps (float) – Variance epsilon (FP32 inside the kernel).

  • residual (torch.Tensor, optional) – BF16 update of x’s shape added to x before normalization (same layout rules as x). Enables the fused residual-add route.

  • out (torch.Tensor, optional) – Preallocated outputs: BF16 y of x’s shape, FP32 reciprocal RMS of its leading shape ([T]) and (residual only) BF16 h_new of x’s shape. Contiguous aligned buffers are written directly; any other layout receives a copy after the launch.

  • rstd (torch.Tensor, optional) – Preallocated outputs: BF16 y of x’s shape, FP32 reciprocal RMS of its leading shape ([T]) and (residual only) BF16 h_new of x’s shape. Contiguous aligned buffers are written directly; any other layout receives a copy after the launch.

  • residual_out (torch.Tensor, optional) – Preallocated outputs: BF16 y of x’s shape, FP32 reciprocal RMS of its leading shape ([T]) and (residual only) BF16 h_new of x’s shape. Contiguous aligned buffers are written directly; any other layout receives a copy after the launch.

  • deterministic (bool) – True (default) runs the deterministic package’s forward (flashinfer.cake_rmsnorm_det_train.cake_rmsnorm_det_train_forward()) with these arguments. False runs this package’s forward programs, the deterministic package’s programs rendered here (the forward has no cross-row reduction, so both are deterministic); it is refused while torch.use_deterministic_algorithms(True) is in effect (with warn_only=True: a warning, then this package’s forward programs, which serve the inputs the deterministic package does not). A value other than a bool raises TypeError.

Returns:

  • (y, rstd) or (y, rstd, h_new) when residual is given.

  • rstd is the FP32 per-row 1 / sqrt(mean(h_new^2) + eps) the backward

  • needs; no other [T, H] temporary is produced.

Notes

Program selection by token count

Each route – one architecture, hidden size and direction – ships one or more programs and selects one per call from the token count T: the program whose window above < T <= at_most admits it (cake_jit.program_windows returns the windows; the table below lists them). The forward programs are the deterministic package’s, with its windows: within one window a row’s y and rstd do not depend on T, and at a forward edge between programs of different per-row thread groups rstd moves by at most two FP32 ulps (three on R200 at the residual H = 6144 edge 1024) and y by at most one BF16 ulp, on up to about 32 % of the rows (measured on B200, GB300 and R200). A backward window (deterministic=False) runs one of two forms. The single kernel computes dx row by row (bitwise reproducible, within one BF16 ulp of max(|dx|, rms(dx)) of the deterministic backward’s dx, not always bitwise equal to it) and adds the dw sums of its CTAs into FP64 column sums in a scheduling-dependent order: its dw is not bitwise reproducible, even for a fixed T. The top window of bwd_h256 on sm_100a and sm_107a, on sm_103a of bwd_h1536, bwd_h2048, bwd_h4096, bwd_h5120, bwd_h6144 and bwd_residual_h6144 and on sm_107a of bwd_h1536, bwd_h2048, bwd_h6144 and bwd_residual_h6144 (and of their FP32-weight routes), the windows above T 49152 of sm_107a bwd_h512, above T 65536 of sm_107a bwd_h4096 and of sm_100a bwd_h2048, bwd_h4096, bwd_h6144 and bwd_residual_h6144, above T 56256 of sm_100a bwd_h5120 (both weight dtypes) and above T 78784 of sm_100a bwd_h1536 and above T 75776 of bwd_w32_h1536, the lowest window of each of those routes that runs its top window’s program, the lowest window on sm_103a of bwd_h4096, bwd_w32_h4096 and bwd_w32_h5120, the lowest window of bwd_w32_h512 on sm_103a and its window from T 227 to 292 on sm_100a, and the sm_100a bwd_h256 window from 20480 to 92160 rows, run a twosum single kernel: each thread keeps its dw column sums exact (error-free FP32 products, TwoSum compensation) and adds them to the FP64 sums once, so the dw error does not grow with T; its dw is not bitwise reproducible either. On sm_103a the window of bwd_h4096 and bwd_h5120 below the top window and on sm_107a the top window of bwd_h5120 (and of their FP32-weight routes) run a kblock single kernel: each thread adds the dw products of a block of 8 rows in FP32 and keeps the block sums as FP32 TwoSum pairs, which it adds to the FP64 sums once, so the dw error grows slowly with T (at most 0.81 of the per-element budget at the sampled T up to 131072 on GB300 and R200; on sm_103a it exceeds the budget at larger T, so its windows there end at T 262144 on bwd_w32_h4096, T 196608 on bwd_w32_h5120, T 262144 on bwd_h4096, T 393216 on bwd_h5120, the largest sampled T at or below which it stays within the budget); its dw is not bitwise reproducible either. A window marked (det pair) runs the deterministic package’s main kernel and fixed-order dw reduce, rendered into this package; on bwd_h5120 (both weight dtypes) the det pair windows above T 6144 on sm_100a, 5120 on sm_103a and 19968 on sm_107a run the deterministic package’s compensated programs (compensated per-chunk dw sums) up to T 56256 on sm_100a, up to T 61952 on sm_103a and up to T 49152 on sm_107a, the single kernel above. On sm_103a bwd_h256 (both weight dtypes) every window above 1080 rows runs a det pair. Their main kernels are those the deterministic package runs for those rows on a device with 152 or more SMs, except from 1080 to 2048 rows, where it is the main kernel that package runs from 2048 to 5120 rows, and from 97280 to 99840 rows, where it is the main kernel that package runs from 99840 to 720896 rows (the pair below 99840 rows exceeds 0.85 of the dw budget above 97280 rows); their fixed-order dw reduce is that package’s, except from 5120 to 12288 and from 720896 to 1572864 rows, where it is the dw reduce that package ran there earlier. On bwd_h256 (both weight dtypes) the det pair the deterministic package runs above 80896 rows on sm_100a also runs from 92160 to 524288 rows on sm_100a; the twosum single kernel runs above, where that pair’s worst dw element exceeds 0.85 of the budget at some sampled row counts. On sm_103a bwd_w32_h512 runs from T 960 to 32768 the det pair the deterministic package runs from T 4096, for dw accuracy. For dw accuracy, on sm_100a bwd_w32_h1536 from T 1024 to 4096 runs the single kernel of its window from T 32768 to 75776, bwd_w32_h2048 from T 1024 to 4096 and bwd_w32_h4096 from T 1792 to 3584 the single kernel whose twosum form runs their top window, bwd_w32_h2048 from T 336 to 1024 the column-task kernel of its window from T 64 to 336 (its CTAs first sum whole columns of dw over every row) and bwd_w32_h5120 from T 1792 to 6144 the single kernel whose twosum form runs its top window; on sm_107a bwd_w32_h6144 from T 19456 to 28672 runs the det pair the deterministic package runs above T 19456, bwd_w32_h2048 from T 1024 to 16384 the single kernel of its largest token counts, bwd_w32_h5120 from T 4096 to 19968 a row single kernel, and bwd_w32_h1536 from T 1024 to 16384 and bwd_w32_h6144 from T 2304 to 19456 the single kernel whose twosum form runs their top window; each replaces a pair whose dw error exceeds the FP32 eager reduction’s at some sampled T of that pair’s window. On sm_100a bwd_h2048 and bwd_w32_h2048 from T 45056 to 58880 run the single kernel of their neighbouring windows, within 2 % of the det pair’s time (worst dw element over both weight dtypes: 0.969 of the budget for the det pair, 0.800 for the single kernel; every 64th T, 120 inputs per family). On sm_107a bwd_h6144 from T 19456 to 30720 runs, for now, the det pair the deterministic package runs there; the pair of its window below stays within the dw budget there and takes 0.97x to 0.99x the det pair’s time. On sm_100a bwd_h512 and bwd_w32_h512 up to T 227 run the deterministic package’s column-task program of that window, which is faster there. On 18 backward routes the lowest window, from T 1 up to a token count from 64 to 229 (1056 to 1080 rows on bwd_h256 and its FP32-weight route and 36864 rows on sm_107a bwd_w32_h256), runs a single kernel for dw accuracy: there, over 60 inputs of each of three families, the det pair’s median dw error, or on the FP32-weight, bwd_h5120 and bwd_h256 routes its worst dw error, exceeds the FP32 eager reduction’s at some sampled T (measured on GB300 and R200 with 212 SMs). On bwd_h1536, bwd_h2048, bwd_h4096, bwd_h5120, bwd_h6144 and bwd_residual_h6144 (and their FP32-weight routes) the lowest window, up to T 64 (on sm_103a up to T 32 on bwd_h4096, bwd_w32_h4096 and bwd_h6144, up to T 48 on bwd_w32_h5120, bwd_w32_h6144, bwd_residual_h6144 and bwd_w32_residual_h6144), runs the fixed-claim form of the twosum column-task kernel: each CTA takes its rows and its column tasks by its index instead of by claim tickets, so no CTA waits for another, and its dx and dw are bitwise those of the ticket form.

The windows per architecture and route, (above, at_most] and the program (the first 8 hex digits of its name). The mark after a window names the measured outcome at its upper edge: = bitwise-identical outputs on the common rows across it, ~ the forward’s ulp differences (see above), ? not yet measured.

The FP32-weight routes (fwd_w32_...) select among the FP32-weight twins of the same programs over the same windows and are not listed; a route re-cut for the FP32 weight (another twin in a window) is listed with its own windows.

Programs that need a whole SM wait beside any kernel resident on every SM, whatever its carveout: the deterministic package’s whole-SM programs where this package runs them (WHOLE_SM: det-pair windows and forward programs; no single-kernel window). Beside those, leave some SMs free of the resident kernel.

  • sm_107a fwd_h6144: (4096, inf)

  • sm_107a fwd_w32_h6144: (4096, inf)

  • sm_100a fwd_h1536: (0, 1536] 1ea88523 ~ (1536, 3584] 2fdbab2a = (3584, 6144] 7de1e510 = (6144, inf) 2fdbab2a

  • sm_100a fwd_h2048: (0, 1024] b44107e0 ~ (1024, 2048] 780b9833 ~ (2048, 4096] b44107e0 ~ (4096, inf) 780b9833

  • sm_100a fwd_h256: (0, 8192] 0489c784 = (8192, 40960] 1b284bae = (40960, 81920] 86d51eff = (81920, 114688] 1388ab77 = (114688, 196608] 466331ff = (196608, 327680] ed9483d8 = (327680, inf) b8f32dd4

  • sm_100a fwd_h4096: (0, 640] 8392d1a0 ~ (640, 1024] fd913ecd = (1024, 6144] d447b7f6 = (6144, inf) fd913ecd

  • sm_100a fwd_h512: (0, 1024] 8757f6da ~ (1024, 4096] 7673978d = (4096, 24576] 3d20ab4a = (24576, inf) 3430e5d9

  • sm_100a fwd_h5120: (0, 640] 5eef28cd = (640, 6144] b5e173f5 = (6144, inf) 5eef28cd

  • sm_100a fwd_w32_h5120: (0, 640] ba28d2c0 = (640, 6144] 9ecb62c2 = (6144, 16384] b49de9b4 = (16384, inf) 9ecb62c2

  • sm_100a fwd_h6144: (0, 768] bad4db91 = (768, 1024] bad4db91 = (1024, inf) 288d7b1d

  • sm_100a fwd_w32_h6144: (0, 768] 2763cd5e = (768, 1024] 2763cd5e = (1024, 6144] 3d619cd1 = (6144, inf) 5f89e5fd

  • sm_100a fwd_residual_h6144: (0, 256] 308da182 = (256, 1024] 03bdf566 ~ (1024, inf) e646bbdb

  • sm_103a fwd_h1536: (0, 1536] 1ea88523 ~ (1536, 3584] 2fdbab2a = (3584, 6144] 7de1e510 = (6144, inf) 2fdbab2a

  • sm_103a fwd_h2048: (0, 1024] c5a8099e = (1024, 4096] b44107e0 ~ (4096, inf) 780b9833

  • sm_103a fwd_h256: (0, 8192] 0489c784 = (8192, 20480] bbb05314 = (20480, 40960] 1b284bae = (40960, 81920] 86d51eff = (81920, 114688] 466331ff = (114688, 196608] 86d51eff = (196608, 327680] ed9483d8 = (327680, 368640] 37f66318 = (368640, inf) b8f32dd4

  • sm_103a fwd_h4096: (0, 640] 8392d1a0 ~ (640, 1024] fd913ecd = (1024, 3072] d447b7f6 = (3072, 6144] d447b7f6 = (6144, inf) fd913ecd

  • sm_103a fwd_h512: (0, 1024] 8757f6da ~ (1024, 4096] 7673978d = (4096, 16384] 3d20ab4a = (16384, inf) 3430e5d9

  • sm_103a fwd_h5120: (0, 640] 5eef28cd = (640, 6144] b5e173f5 = (6144, inf) 5eef28cd

  • sm_103a fwd_w32_h5120: (0, 640] 1defffdf = (640, 6144] 9ecb62c2 = (6144, inf) 9ecb62c2

  • sm_103a fwd_h6144: (0, 1024] bad4db91 = (1024, inf) 288d7b1d

  • sm_103a fwd_w32_h6144: (0, 1024] 2763cd5e = (1024, 6144] 2763cd5e = (6144, inf) 5f89e5fd

  • sm_103a fwd_residual_h6144: (0, 256] 308da182 = (256, 1024] 03bdf566 ~ (1024, inf) e646bbdb

  • sm_107a fwd_h1536: (0, 2048] 1ea88523 ~ (2048, 12288] 4cdc4c49 = (12288, inf) 682675bd

  • sm_107a fwd_h2048: (0, 1024] b44107e0 ~ (1024, 8192] a4da8513 = (8192, 12288] 0ad4d53c = (12288, 16384] 0ad4d53c = (16384, inf) bde56bd5

  • sm_107a fwd_w32_h2048: (0, 1024] 0da8e18d ~ (1024, 8192] a72c6ece = (8192, 12288] 595f5060 = (12288, 16384] 40210950 = (16384, inf) 40210950

  • sm_107a fwd_h256: (0, 6144] c5ca409f = (6144, 32768] bbb05314 = (32768, 98304] 466331ff = (98304, inf) 37f66318

  • sm_107a fwd_h4096: (0, 512] 8392d1a0 ~ (512, 1024] fd913ecd = (1024, 4096] d447b7f6 = (4096, 20480] bf933e16 = (20480, 49152] bf933e16 = (49152, inf) fd913ecd

  • sm_107a fwd_w32_h4096: (0, 512] 4371270e ~ (512, 1024] f0db3068 = (1024, 4096] f9f3423b = (4096, 20480] f9f3423b = (20480, 49152] f9f3423b = (49152, inf) f9f3423b

  • sm_107a fwd_h512: (0, 256] 7673978d = (256, 6144] 2a42e5c0 = (6144, inf) 60f47c5d

  • sm_107a fwd_h5120: (0, 512] fd1097c4 = (512, 1152] 5eef28cd = (1152, 6144] b5e173f5 = (6144, 20480] 044d4a2a = (20480, 49152] 044d4a2a = (49152, inf) 5eef28cd

  • sm_107a fwd_w32_h5120: (0, 512] ba28d2c0 = (512, 1152] 1defffdf = (1152, 6144] 9ecb62c2 = (6144, 20480] b49de9b4 = (20480, 49152] b49de9b4 = (49152, inf) b49de9b4

  • sm_107a fwd_h6144: (0, 1024] bad4db91 = (1024, 4096] cf1d0554 = (4096, inf) e7f4221a

  • sm_107a fwd_residual_h6144: (0, 256] e646bbdb ~ (256, 1024] 03bdf566 ~ (1024, inf) e646bbdb