flashinfer.cake_rmsnorm_silu_gate_train.cake_rmsnorm_silu_gate_train_backward¶
- flashinfer.cake_rmsnorm_silu_gate_train.cake_rmsnorm_silu_gate_train_backward(grad_out: Tensor, x: Tensor, z: Tensor, weight: Tensor, eps: float = 1e-06, *, grad_x: Tensor | None = None, grad_z: Tensor | None = None, grad_weight: Tensor | None = None, accumulate_grad_weight: bool = False, compute_grad_weight: bool = True, workspace: Tensor | None = None, deterministic: bool = True) tuple[Tensor, Tensor, Tensor | None]¶
SiLU-gated RMSNorm backward:
dx,dzand the weight gradient.With
n = x * rstd,s = sigmoid(z)anda = dy * silu(z):dz = BF16(dy * (n * weight) * s * (1 + z * (1 - s))) dx = BF16(rstd * a * weight - x * rstd^3 * mean(a * weight * x)) dw = sum over every token and head of a * n
in FP32, the statistics and the gate recomputed from
xandz.- Parameters:
grad_out (torch.Tensor) – BF16 tensors of one
[..., 128]shape (any strides, see the module).x (torch.Tensor) – BF16 tensors of one
[..., 128]shape (any strides, see the module).z (torch.Tensor) – BF16 tensors of one
[..., 128]shape (any strides, see the module).weight (torch.Tensor) – BF16
[128].eps (float) – The forward’s epsilon.
grad_x (torch.Tensor, optional) – Preallocated BF16 outputs of
x’s shape.grad_z (torch.Tensor, optional) – Preallocated BF16 outputs of
x’s shape.grad_weight (torch.Tensor, optional) – A BF16 or FP32
[128]tensor that receives the weight gradient (the FP64 sum rounded to FP32, and to BF16 from there for a BF16 tensor). Without it a new BF16 tensor is returned.accumulate_grad_weight (bool) – Add the gradient to
grad_weight’s current value before the rounding (in FP64; needsgrad_weight). A call with no rows leaves the accumulator as it is.compute_grad_weight (bool) –
Falseskips the weight-gradient reduction and returnsNonefor it; the non-deterministic kernel then touches neithergrad_weightnor the workspace’s accumulators, but its CTAs still take the arrival ticket (and past one wave of row blocks claim their blocks from the claim counter), so it uses a workspace too (and leaves it zero); the one-cluster backward of up to 256 rows uses none.workspace (torch.Tensor, optional) – Caller-owned buffer from
cake_rmsnorm_silu_gate_train_backward_workspace()called with the samedeterministicflag: the deterministic package’s FP32 partial buffer (sized for at least this many rows), or the zero-filled FP64 buffer of the non-deterministic backward, which the kernel leaves zero (zero it withcake_rmsnorm_silu_gate_train_reset_workspace()after anything else wrote into it). When omitted: the deterministic package allocates its buffer per call; the non-deterministic backward uses one zero-filled buffer per device, stream and backward kernel, kept for later calls (during CUDA Graph capture, one zero-filled buffer per capture and capture stream, from a reserve those calls keep). One buffer per backward that may run at the same time on another stream.deterministic (bool) –
True(default) runs the deterministic package’s backward (flashinfer.cake_rmsnorm_silu_gate_det_train.cake_rmsnorm_silu_gate_det_train_backward()) with these arguments:dwaccumulated in FP32 per CTA chunk and in FP64 over the chunks in an order fixed by the row count, rounded to FP32 (and to BF16 from there for BF16), so repeated calls return the same bits.Falseruns one kernel of the non-deterministic package: the samedxanddzbits, and each CTA sums the rows of the blocks it takes (its own block when the grid has a CTA for every block, else the chunks it claims) in FP32 runs of at most 16 rows folded into FP64 (a CTA of a few blocks adds its runs across its warps in FP32 and converts once: at most 16 FP32 roundings a term either way) and adds its FP64 column sums into FP64 accumulators with atomics; the CTA that arrives last rounds the sum intodw(addinggrad_weight’s value first when accumulating). The claimed blocks, and so the FP32 runs, and the order of the FP64 additions depend on scheduling:dwcan differ between calls, always within the deterministic backward’s error bound|dw64| * 2^-7 + sum|terms| * 2^-20per column while the gate, the terms and the sums are FP32 normal numbers or zero (subnormal values and infinities: see the module documentation). On SM100 from 800 rows per SM and on SM103 from 700, up to 12000, the runs are folded, and a CTA’s column sums kept, as FP32 two-sum pairs, converted to FP64 once per column (that fold’s own error bound and its finite-overflow case: see the module documentation). Up to 256 rows one cluster of 16 CTAs runs instead: the CTAs’ FP32 partials of a column are added in a fixed order in FP64, sodwis the same bits on every call (within the same bound), and the workspace is not used.Falseis refused withRuntimeErrorwhiletorch.use_deterministic_algorithms(True)is in effect; withwarn_only=Trueit warns and runs the deterministic backward (which then allocates its own buffer in place of a non-deterministicworkspace).
- Return type:
(grad_x, grad_z, grad_weight).