flashinfer.cake_rmsnorm_det_train.cake_rmsnorm_det_train_forward¶
- flashinfer.cake_rmsnorm_det_train.cake_rmsnorm_det_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) 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. 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 intoprod(...)rows, the outputs take the input’s leading shape (rstdis[...]),wis shared by every row and the backward’sdwsums them all; any other N-D layout raisesValueErrornaming the rule. Withresidualthis is the residual streamhand the normalized value ish_new = BF16(h + residual).w (torch.Tensor) – BF16 or FP32
[H]weight. BF16 is read in place when contiguous and 32-byte aligned (the forward kernels load it with 32-byte vectors), FP32 – the effective weight1 + wa zero-centred model computes in FP32 – when contiguous and 32-byte aligned; any other layout is copied once. An FP32 weight needs the FP32-weight route of the width on the device (weight_dtypes()), elseValueError.eps (float) – Variance epsilon (FP32 inside the kernel).
residual (torch.Tensor, optional) – BF16 update of
x’s shape added toxbefore normalization (same layout rules asx). Enables the fused residual-add route.out (torch.Tensor, optional) – Preallocated outputs: BF16
yofx’s shape, FP32 reciprocal RMS of its leading shape ([T]) and (residual only) BF16h_newofx’s shape. Contiguous aligned buffers are written directly; any other layout receives a copy after the launch.rstd (torch.Tensor, optional) – Preallocated outputs: BF16
yofx’s shape, FP32 reciprocal RMS of its leading shape ([T]) and (residual only) BF16h_newofx’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
yofx’s shape, FP32 reciprocal RMS of its leading shape ([T]) and (residual only) BF16h_newofx’s shape. Contiguous aligned buffers are written directly; any other layout receives a copy after the launch.
- Returns:
(y, rstd)or(y, rstd, h_new)whenresidualis given.rstdis the FP32 per-row1 / sqrt(mean(h_new^2) + eps)the backwardneeds; no other
[T, H]temporary is produced.
Notes
Program selection by token count
Each route – one architecture, hidden size and direction – ships several programs of one kernel and selects one per call from the token count
T: the program whose windowabove < T <= at_mostadmits it (cake_jit.program_windowsreturns the windows; the table below lists them). Within one window a row’sy,rstdanddxdo not depend onT: two calls whose token counts fall in the same forward window (and, for the backward, the same backward window) return bitwise-identical results on their common rows. Across a forward edge between programs of different per-row thread groups the sum of squares is reduced in another order. On the rows both programs compute,rstdthen moves by at most two FP32 ulps (three on SM107 at the residual route’s edge 1024) on 10.8-18.2 % of the rows (24.0-31.6 % on the residual route);ymoves by one BF16 ulp, only on rows whoserstdmoved, on at most 2.34 % of the rows (4.30 % on the residual route);dxfollows on those rows and the residual outputh_newdoes not change. Across every other forward edge, every edge of H = 256 and H = 5120 among them, the outputs are bitwise identical. Measured over three random inputs per edge, with BF16 and FP32 weights: on B200 (sm_100a) at most two ulps on up to 18.0 % of the rows (27.3 % on the residual route),yon up to 2.03 % (2.83 %); on GB300 (sm_103a) at most two ulps on up to 17.3 % (27.8 %),yon up to 2.03 % (2.34 %); on R200 (sm_107a, 212- and 216-SM boards) at most three ulps on up to 18.2 % (31.6 %),yon up to 2.34 % (4.30 %). Across a backward-only edgedxis bitwise identical: the H = 6144 route, which switched between the base backward kernel and the bulk-copy one withdxdiffering on about 1-2 % of the rows, now runs the bulk-copy kernel at everyTon every architecture.dwsums every row and depends onTby definition; for a fixedTit is bitwise reproducible on devices with the same SM count. The backward splits the rows overctas_per_smx SM count CTAs, so devices of one architecture with different SM counts (B200 with 148 and GB200 with 152 SMs, R200 boards with 212 and 216 SMs) cut different chunk tables and can give differentdwbits. In the window of its smallest token counts a backward route other than H = 256 runs a column-task program instead: one CTA sums each column ofdwover every row in a fixed order (exact products and TwoSum, one FP32 rounding), so theredwdoes not depend on the SM count. Every backward route but H = 512 and the residual H = 6144 switches from plain to compensateddwsums at an accuracy edge (ACCURACY_EDGES) measured on B200 (148 SMs), GB300 (152 SMs) and R200 (212 SMs; the H = 5120 and H = 256 edges also on 216 SMs). A device with fewer SMs thanACCURACY_MIN_SMSgives each CTA more rows to sum at the sameT; there these routes select their programs as ifTwere the edge plus one, so their compensated programs run at everyT. Measured on B200 (sm_100a), GB300 (sm_103a) and R200 (sm_107a).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, which the package tests hold:=bitwise-identical outputs on the common rows across it,~the forward’s measured differences (yby at most one BF16 ulp,rstdby at most two FP32 ulps, three on R200 (216 SMs) at the residual route’s edge 1024),dxthe backward kernel switch,?not yet measured (the tests bound the differences there).The FP32-weight routes (
fwd_w32_...) select among the FP32-weight twins of the same programs over the same windows, with the same outcome at every edge, and are not listed; a route re-cut for the FP32 weight (another twin in a window) is listed with its own windows.sm_100afwd_h1536: (0, 1536] 1ea88523 ~ (1536, 3584] 2fdbab2a = (3584, 6144] 7de1e510 = (6144, inf) 2fdbab2asm_100afwd_h2048: (0, 1024] b44107e0 ~ (1024, 2048] 780b9833 ~ (2048, 4096] b44107e0 ~ (4096, inf) 780b9833sm_100afwd_h256: (0, 8192] 0489c784 = (8192, 40960] 1b284bae = (40960, 81920] 86d51eff = (81920, 114688] 1388ab77 = (114688, 196608] 466331ff = (196608, 327680] ed9483d8 = (327680, inf) b8f32dd4sm_100afwd_h4096: (0, 640] 8392d1a0 ~ (640, 1024] fd913ecd = (1024, 6144] d447b7f6 = (6144, inf) fd913ecdsm_100afwd_h512: (0, 1024] 8757f6da ~ (1024, 4096] 7673978d = (4096, 24576] 3d20ab4a = (24576, inf) 3430e5d9sm_100afwd_h5120: (0, 640] 5eef28cd = (640, 6144] b5e173f5 = (6144, inf) 5eef28cdsm_100afwd_w32_h5120: (0, 640] ba28d2c0 = (640, 6144] 9ecb62c2 = (6144, 16384] b49de9b4 = (16384, inf) 9ecb62c2sm_100afwd_h6144: (0, 768] bad4db91 = (768, 1024] bad4db91 = (1024, inf) 288d7b1dsm_100afwd_w32_h6144: (0, 768] 2763cd5e = (768, 1024] 2763cd5e = (1024, 6144] 3d619cd1 = (6144, inf) 5f89e5fdsm_100afwd_residual_h6144: (0, 256] 308da182 = (256, 1024] 03bdf566 ~ (1024, inf) e646bbdbsm_103afwd_h1536: (0, 1536] 1ea88523 ~ (1536, 3584] 2fdbab2a = (3584, 6144] 7de1e510 = (6144, inf) 2fdbab2asm_103afwd_h2048: (0, 1024] c5a8099e = (1024, 4096] b44107e0 ~ (4096, inf) 780b9833sm_103afwd_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) b8f32dd4sm_103afwd_h4096: (0, 640] 8392d1a0 ~ (640, 1024] fd913ecd = (1024, 3072] d447b7f6 = (3072, 6144] d447b7f6 = (6144, inf) fd913ecdsm_103afwd_h512: (0, 1024] 8757f6da ~ (1024, 4096] 7673978d = (4096, 16384] 3d20ab4a = (16384, inf) 3430e5d9sm_103afwd_h5120: (0, 640] 5eef28cd = (640, 6144] b5e173f5 = (6144, inf) 5eef28cdsm_103afwd_w32_h5120: (0, 640] 1defffdf = (640, 6144] 9ecb62c2 = (6144, inf) 9ecb62c2sm_103afwd_h6144: (0, 1024] bad4db91 = (1024, inf) 288d7b1dsm_103afwd_w32_h6144: (0, 1024] 2763cd5e = (1024, 6144] 2763cd5e = (6144, inf) 5f89e5fdsm_103afwd_residual_h6144: (0, 256] 308da182 = (256, 1024] 03bdf566 ~ (1024, inf) e646bbdbsm_107afwd_h1536: (0, 2048] 1ea88523 ~ (2048, 12288] 4cdc4c49 = (12288, inf) 682675bdsm_107afwd_h2048: (0, 1024] b44107e0 ~ (1024, 8192] a4da8513 = (8192, 12288] 0ad4d53c = (12288, 16384] 0ad4d53c = (16384, inf) bde56bd5sm_107afwd_w32_h2048: (0, 1024] 0da8e18d ~ (1024, 8192] a72c6ece = (8192, 12288] 595f5060 = (12288, 16384] 40210950 = (16384, inf) 40210950sm_107afwd_h256: (0, 6144] c5ca409f = (6144, 32768] bbb05314 = (32768, 98304] 466331ff = (98304, inf) 37f66318sm_107afwd_h4096: (0, 512] 8392d1a0 ~ (512, 1024] fd913ecd = (1024, 4096] d447b7f6 = (4096, 20480] bf933e16 = (20480, 49152] bf933e16 = (49152, inf) fd913ecdsm_107afwd_w32_h4096: (0, 512] 4371270e ~ (512, 1024] f0db3068 = (1024, 4096] f9f3423b = (4096, 20480] f9f3423b = (20480, 49152] f9f3423b = (49152, inf) f9f3423bsm_107afwd_h512: (0, 256] 7673978d = (256, 6144] 2a42e5c0 = (6144, inf) 60f47c5dsm_107afwd_h5120: (0, 512] fd1097c4 = (512, 1152] 5eef28cd = (1152, 6144] b5e173f5 = (6144, 20480] 044d4a2a = (20480, 49152] 044d4a2a = (49152, inf) 5eef28cdsm_107afwd_w32_h5120: (0, 512] ba28d2c0 = (512, 1152] 1defffdf = (1152, 6144] 9ecb62c2 = (6144, 20480] b49de9b4 = (20480, 49152] b49de9b4 = (49152, inf) b49de9b4sm_107afwd_h6144: (0, 1024] bad4db91 = (1024, 4096] cf1d0554 = (4096, inf) e7f4221asm_107afwd_residual_h6144: (0, 256] e646bbdb ~ (256, 1024] 03bdf566 ~ (1024, inf) e646bbdb