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. Withdeterministic=Falsea 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
[H]weight, read in place when contiguous and 32-byte aligned, else copied once. Withdeterministic=Falsean FP32[H]weight – the effective weight1 + wa zero-centred model computes in FP32 – is accepted on the routes that ship the FP32-weight programs (cake_jit.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.deterministic (bool) –
True(default) runs the deterministic package’s forward (flashinfer.cake_rmsnorm_det_train.cake_rmsnorm_det_train_forward()) with these arguments.Falseruns 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 whiletorch.use_deterministic_algorithms(True)is in effect (withwarn_only=True: a warning, then this package’s forward programs, which serve the inputs the deterministic package does not). A value other than aboolraisesTypeError.
- 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 one or more programs 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). The forward programs are the deterministic package’s, with its windows: within one window a row’syandrstddo not depend onT, and at a forward edge between programs of different per-row thread groupsrstdmoves by at most two FP32 ulps (three on R200 at the residual H = 6144 edge 1024) andyby 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 computesdxrow by row (bitwise reproducible, within one BF16 ulp ofmax(|dx|, rms(dx))of the deterministic backward’sdx, not always bitwise equal to it) and adds thedwsums of its CTAs into FP64 column sums in a scheduling-dependent order: itsdwis not bitwise reproducible, even for a fixedT. The top window ofbwd_h256on sm_100a and sm_107a, on sm_103a ofbwd_h1536,bwd_h2048,bwd_h4096,bwd_h5120,bwd_h6144andbwd_residual_h6144and on sm_107a ofbwd_h1536,bwd_h2048,bwd_h6144andbwd_residual_h6144(and of their FP32-weight routes), the windows above T 49152 of sm_107abwd_h512, above T 65536 of sm_107abwd_h4096and of sm_100abwd_h2048,bwd_h4096,bwd_h6144andbwd_residual_h6144, above T 56256 of sm_100abwd_h5120(both weight dtypes) and above T 78784 of sm_100abwd_h1536and above T 75776 ofbwd_w32_h1536, the lowest window of each of those routes that runs its top window’s program, the lowest window on sm_103a ofbwd_h4096,bwd_w32_h4096andbwd_w32_h5120, the lowest window ofbwd_w32_h512on sm_103a and its window from T 227 to 292 on sm_100a, and the sm_100abwd_h256window from 20480 to 92160 rows, run a twosum single kernel: each thread keeps itsdwcolumn sums exact (error-free FP32 products, TwoSum compensation) and adds them to the FP64 sums once, so thedwerror does not grow withT; itsdwis not bitwise reproducible either. On sm_103a the window ofbwd_h4096andbwd_h5120below the top window and on sm_107a the top window ofbwd_h5120(and of their FP32-weight routes) run a kblock single kernel: each thread adds thedwproducts 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 thedwerror grows slowly withT(at most 0.81 of the per-element budget at the sampledTup to 131072 on GB300 and R200; on sm_103a it exceeds the budget at largerT, so its windows there end at T 262144 onbwd_w32_h4096, T 196608 onbwd_w32_h5120, T 262144 onbwd_h4096, T 393216 onbwd_h5120, the largest sampledTat or below which it stays within the budget); itsdwis 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; onbwd_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-chunkdwsums) 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_103abwd_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 thedwbudget 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. Onbwd_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 worstdwelement exceeds 0.85 of the budget at some sampled row counts. On sm_103abwd_w32_h512runs from T 960 to 32768 the det pair the deterministic package runs from T 4096, fordwaccuracy. Fordwaccuracy, on sm_100abwd_w32_h1536from T 1024 to 4096 runs the single kernel of its window from T 32768 to 75776,bwd_w32_h2048from T 1024 to 4096 andbwd_w32_h4096from T 1792 to 3584 the single kernel whose twosum form runs their top window,bwd_w32_h2048from T 336 to 1024 the column-task kernel of its window from T 64 to 336 (its CTAs first sum whole columns ofdwover every row) andbwd_w32_h5120from T 1792 to 6144 the single kernel whose twosum form runs its top window; on sm_107abwd_w32_h6144from T 19456 to 28672 runs the det pair the deterministic package runs above T 19456,bwd_w32_h2048from T 1024 to 16384 the single kernel of its largest token counts,bwd_w32_h5120from T 4096 to 19968 a row single kernel, andbwd_w32_h1536from T 1024 to 16384 andbwd_w32_h6144from T 2304 to 19456 the single kernel whose twosum form runs their top window; each replaces a pair whosedwerror exceeds the FP32 eager reduction’s at some sampledTof that pair’s window. On sm_100abwd_h2048andbwd_w32_h2048from T 45056 to 58880 run the single kernel of their neighbouring windows, within 2 % of the det pair’s time (worstdwelement over both weight dtypes: 0.969 of the budget for the det pair, 0.800 for the single kernel; every 64thT, 120 inputs per family). On sm_107abwd_h6144from T 19456 to 30720 runs, for now, the det pair the deterministic package runs there; the pair of its window below stays within thedwbudget there and takes 0.97x to 0.99x the det pair’s time. On sm_100abwd_h512andbwd_w32_h512up 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 onbwd_h256and its FP32-weight route and 36864 rows on sm_107abwd_w32_h256), runs a single kernel fordwaccuracy: there, over 60 inputs of each of three families, the det pair’s mediandwerror, or on the FP32-weight,bwd_h5120andbwd_h256routes its worstdwerror, exceeds the FP32 eager reduction’s at some sampledT(measured on GB300 and R200 with 212 SMs). Onbwd_h1536,bwd_h2048,bwd_h4096,bwd_h5120,bwd_h6144andbwd_residual_h6144(and their FP32-weight routes) the lowest window, up to T 64 (on sm_103a up to T 32 onbwd_h4096,bwd_w32_h4096andbwd_h6144, up to T 48 onbwd_w32_h5120,bwd_w32_h6144,bwd_residual_h6144andbwd_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 itsdxanddware 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_107afwd_h6144: (4096, inf)sm_107afwd_w32_h6144: (4096, inf)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