[Kernel] ReplaySSM: cache SSM inputs for faster Mamba2 standard decode (#48018)

Signed-off-by: Johnny-Liou <a897111@gmail.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: tomeras91 <57313761+tomeras91@users.noreply.github.com>
Co-authored-by: Cyrus Leung <tlleungac@connect.ust.hk>
This commit is contained in:
Johnny-Liou
2026-07-24 09:39:49 -07:00
committed by GitHub
co-authored by Claude Opus 4.8 mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> tomeras91 Cyrus Leung
parent d02df748bf
commit 866fea2b99
23 changed files with 2954 additions and 31 deletions
@@ -0,0 +1,244 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Prefill == decode equivalence for the Mamba2 ReplaySSM kernels.
The SSM recurrence is path-independent: vLLM's chunked prefill (the SSD kernel
``mamba_chunk_scan_combined_varlen``) and step-by-step decode must produce the
same per-position outputs and final state over a sequence. This file feeds one
set of inputs through the production dt flow (raw dt + softplus + a per-head
dt_bias, applied inside each kernel) to:
* the exact fp32 step recurrence (``selective_state_update_ref``) -- the
ground truth,
* the chunked prefill kernel,
* the baseline decode kernel,
* the ReplaySSM output_only decode kernel,
and checks all of them agree. Prefill (a chunked scan) and decode (a step
recurrence) are different code paths, so they differ numerically: the chunked
scan carries ~2e-2 (fp32) / ~4e-2 (bf16) vs the exact recurrence, far above the
near-exact decode. We therefore anchor every path on the exact recurrence at
SSD-level tolerances (the same regime as ``test_mamba_ssm_ssd.py``), which the
chunked scan sets, keyed off the activation dtype.
State and activation/buffer precision are swept independently, including the
fp32-state + bf16-activation production config.
"""
import pytest
import torch
from tests.kernels.mamba.utils import selective_state_update_ref
from vllm.model_executor.layers.mamba.ops.mamba_ssm import selective_state_update
from vllm.model_executor.layers.mamba.ops.selective_state_update_replayssm_output_only import ( # noqa: E501
selective_state_update_replayssm_output_only,
)
from vllm.model_executor.layers.mamba.ops.ssd_combined import (
mamba_chunk_scan_combined_varlen,
)
from vllm.utils.torch_utils import set_random_seed
from vllm.v1.attention.backends.mamba2_attn import compute_varlen_chunk_metadata
def _prefill_tolerances(act_dtype: torch.dtype) -> tuple[float, float]:
# The chunked prefill scan, not the decode, sets these: it carries ~2e-2
# (fp32) / ~6e-2 (bf16) vs the exact recurrence, while ReplaySSM decode is
# near-exact (~2e-6 fp32). Keyed off the activation dtype (the outputs are
# in act_dtype). Same regime as test_mamba_ssm_ssd.py.
if act_dtype == torch.float32:
return 1e-2, 3e-2
return 6e-2, 1e-1
def _run_prefill_decode_equivalence(
*,
state_dtype: torch.dtype,
act_dtype: torch.dtype,
nheads: int,
headdim: int,
ngroups: int,
dstate: int,
seqlen: int,
chunk_size: int,
max_cache_len: int,
seed: int = 0,
) -> None:
"""Prefill the whole sequence and decode it step by step; check both match
the exact fp32 recurrence (and each other). All paths use the production dt
flow (raw dt + softplus + a per-head dt_bias), so this also checks prefill
and decode apply the softplus/bias preprocessing consistently. ``state_dtype``
is the recurrent-state precision; ``act_dtype`` the activation/buffer one."""
device = "cuda"
rtol, atol = _prefill_tolerances(act_dtype)
set_random_seed(seed)
# Production dt flow: raw dt + a per-head dt_bias (~-4 keeps softplus(dt +
# bias) small and well-conditioned). dt_bias is (nheads,) for prefill and
# (nheads, headdim) for the decode kernels/reference.
A = -torch.exp(torch.rand(nheads, device=device, dtype=act_dtype))
dt = torch.randn(seqlen, nheads, device=device, dtype=act_dtype)
dt_bias = torch.rand(nheads, device=device, dtype=act_dtype) - 4
dt_bias_hd = dt_bias.view(nheads, 1).expand(nheads, headdim)
X = torch.randn(seqlen, nheads, headdim, device=device, dtype=act_dtype)
B = torch.randn(seqlen, ngroups, dstate, device=device, dtype=act_dtype)
C = torch.randn(seqlen, ngroups, dstate, device=device, dtype=act_dtype)
A_bcast = A.view(nheads, 1, 1).expand(nheads, headdim, dstate)
# Chunked prefill over the whole sequence (implicit batch=1, varlen). The
# kernel always returns the final state in fp32, so no state_dtype plumbing.
cu_seqlens = torch.tensor((0, seqlen), device=device).cumsum(0).to(torch.int32)
cu_chunk_seqlens, last_chunk_indices, seq_idx = compute_varlen_chunk_metadata(
cu_seqlens, chunk_size
)
y_prefill = torch.empty(seqlen, nheads, headdim, device=device, dtype=act_dtype)
final_state_prefill = mamba_chunk_scan_combined_varlen(
X,
dt,
A,
B,
C,
chunk_size,
cu_seqlens=cu_seqlens,
cu_chunk_seqlens=cu_chunk_seqlens,
last_chunk_indices=last_chunk_indices,
seq_idx=seq_idx,
out=y_prefill,
D=None,
dt_bias=dt_bias,
dt_softplus=True,
)
# Step paths: exact fp32 recurrence (ground truth), baseline, ReplaySSM.
# State follows state_dtype; caches follow act_dtype (dt_cache is fp32).
state_ref = torch.zeros(
1, nheads, headdim, dstate, device=device, dtype=torch.float32
)
state_base = torch.zeros(
1, nheads, headdim, dstate, device=device, dtype=state_dtype
)
state_dec = torch.zeros(
1, nheads, headdim, dstate, device=device, dtype=state_dtype
)
x_cache = torch.zeros(
1, nheads, max_cache_len, headdim, device=device, dtype=act_dtype
)
dt_cache = torch.zeros(1, nheads, max_cache_len, device=device, dtype=torch.float32)
B_cache = torch.zeros(
1, ngroups, max_cache_len, dstate, device=device, dtype=act_dtype
)
bc_pre = torch.empty(1, ngroups, max_cache_len, device=device, dtype=torch.float32)
write_pos = torch.zeros(1, dtype=torch.int32, device=device)
# No skip connection (D=0) on any path here; the D!=0 path is covered by the
# standard-decode suite. The baseline kernel needs a D tensor, not None.
D_zero = torch.zeros(nheads, headdim, device=device)
y_ref = torch.empty(seqlen, nheads, headdim, device=device, dtype=torch.float32)
y_base = torch.empty(seqlen, nheads, headdim, device=device, dtype=act_dtype)
y_dec = torch.empty(seqlen, nheads, headdim, device=device, dtype=act_dtype)
for t in range(seqlen):
dt_t = dt[t].view(1, nheads, 1).expand(1, nheads, headdim)
is_flush = write_pos == max_cache_len - 1
y_ref[t] = selective_state_update_ref(
state_ref,
X[t : t + 1].float(),
dt_t.float(),
A_bcast.float(),
B[t : t + 1].float(),
C[t : t + 1].float(),
dt_bias=dt_bias_hd.float(),
dt_softplus=True,
)[0]
out_b = torch.empty(1, nheads, headdim, device=device, dtype=act_dtype)
selective_state_update(
state_base,
X[t : t + 1],
dt_t,
A_bcast,
B[t : t + 1],
C[t : t + 1],
D=D_zero,
dt_bias=dt_bias_hd,
dt_softplus=True,
out=out_b,
)
y_base[t] = out_b[0]
out_d = torch.empty(1, nheads, headdim, device=device, dtype=act_dtype)
common = dict(
dt_bias=dt_bias_hd,
dt_softplus=True,
x_cache=x_cache,
dt_cache=dt_cache,
B_cache=B_cache,
write_pos=write_pos,
is_flush=is_flush,
max_cache_len=max_cache_len,
out=out_d,
)
selective_state_update_replayssm_output_only(
state_dec,
X[t : t + 1],
dt_t,
A_bcast,
B[t : t + 1],
C[t : t + 1],
bc_pre=bc_pre,
**common,
)
y_dec[t] = out_d[0]
write_pos = torch.where(is_flush, torch.zeros_like(write_pos), write_pos + 1)
# Every path computes the same recurrence; anchor each on the fp32 truth.
torch.testing.assert_close(y_prefill.float(), y_ref, rtol=rtol, atol=atol)
torch.testing.assert_close(y_base.float(), y_ref, rtol=rtol, atol=atol)
torch.testing.assert_close(y_dec.float(), y_ref, rtol=rtol, atol=atol)
# Headline: ReplaySSM decode matches the chunked prefill directly.
torch.testing.assert_close(y_dec.float(), y_prefill.float(), rtol=rtol, atol=atol)
# Final state too (the recurrence ends in state_ref after the loop).
torch.testing.assert_close(
final_state_prefill[0].float(), state_ref[0], rtol=rtol, atol=atol
)
@pytest.mark.skipif(not torch.cuda.is_available(), reason="Need CUDA device")
@pytest.mark.parametrize(
"precision",
# fp32 state is the default; bf16/fp16 are reduced-footprint configs. fp16
# appears as an activation dtype (s32_afp16, sfp16_afp16) and as a finer-
# mantissa state under bf16 activations (sfp16_a16).
[
pytest.param((torch.float32, torch.float32), id="s32_a32"),
pytest.param((torch.float32, torch.bfloat16), id="s32_a16"),
pytest.param((torch.bfloat16, torch.bfloat16), id="s16_a16"),
pytest.param((torch.float32, torch.float16), id="s32_afp16"),
pytest.param((torch.float16, torch.float16), id="sfp16_afp16"),
pytest.param((torch.float16, torch.bfloat16), id="sfp16_a16"),
],
)
@pytest.mark.parametrize(
"geometry", # (nheads, headdim, dstate, ngroups)
[
pytest.param((8, 64, 64, 2), id="small"),
pytest.param((96, 80, 128, 8), id="nano4b"),
],
)
def test_replayssm_prefill_decode_equivalence(
precision: tuple[torch.dtype, torch.dtype],
geometry: tuple[int, int, int, int],
):
state_dtype, act_dtype = precision
nheads, headdim, dstate, ngroups = geometry
_run_prefill_decode_equivalence(
state_dtype=state_dtype,
act_dtype=act_dtype,
nheads=nheads,
headdim=headdim,
ngroups=ngroups,
dstate=dstate,
seqlen=16,
chunk_size=8,
max_cache_len=4,
)
@@ -0,0 +1,673 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Standard (autoregressive) decode correctness for the Mamba2 ReplaySSM kernels.
ReplaySSM caches the recent SSM inputs ``(x, dt, B)`` in a small ring buffer and
reconstructs / reads out the recurrent state on the fly, writing the full state
back to HBM only when the buffer flushes. This file checks that, over a
multi-step decode, the ReplaySSM output_only kernel reproduces the exact SSM
recurrence.
For every step we assert, against trusted oracles driven one token at a time:
* the ReplaySSM output matches its pure-PyTorch reference, which models the
kernel's exact arithmetic (including its bf16 reconstruction), at every
precision,
* when the state is fp32, the output also matches the baseline decode kernel
(``selective_state_update``); at bf16 state the baseline legitimately
differs -- it downcasts the fp32 state to bf16 every step, while ReplaySSM
accumulates a whole buffer in fp32 and is the more accurate path,
* when state and activations are both fp32, the output also matches the exact
elementwise ``selective_state_update_ref``,
* the cached inputs match the reference cache management,
* the checkpoint state matches the reference (and the baseline at fp32 state).
State and activation/buffer precision are swept independently. Nemotron-3
defaults to fp32 SSM state (``mamba_ssm_cache_dtype=float32``), but bf16 state is
also supported; the buffer dtype follows the activation dtype and ``dt_cache``
is always fp32.
"""
import pytest
import torch
from tests.kernels.mamba.utils import (
allocate_update_caches,
selective_state_update_ref,
selective_state_update_replayssm_output_only_ref,
)
from vllm.model_executor.layers.mamba.ops.mamba_ssm import selective_state_update
from vllm.model_executor.layers.mamba.ops.selective_state_update_replayssm_output_only import ( # noqa: E501
selective_state_update_replayssm_output_only,
)
from vllm.utils.torch_utils import set_random_seed
from vllm.v1.attention.backends.utils import NULL_BLOCK_ID
def _tolerances(dtype: torch.dtype) -> tuple[float, float]:
# fp32 demands true fp32 parity. A correct fp32 reconstruction (tl.dot with
# input_precision="tf32x3"/"ieee") matches the elementwise baseline to
# ~1e-5, while a TF32 reconstruction drifts to ~1e-2. atol=1e-3 sits between,
# so it flags TF32 degradation yet passes a correct fp32 kernel. bf16 stays
# loose (bf16 rounding dominates and is unaffected by the matmul precision).
if dtype == torch.float32:
return 1e-4, 1e-3
return 6e-2, 2e-1
def _tied_A(nheads: int, headdim: int, dstate: int, device: str) -> torch.Tensor:
A = -torch.rand(nheads, device=device) - 1.0
return A.view(nheads, 1, 1).expand(nheads, headdim, dstate)
def _tied_dt(
batch: int,
nheads: int,
headdim: int,
device: str,
dtype: torch.dtype,
) -> torch.Tensor:
dt = torch.randn(batch, nheads, device=device, dtype=dtype)
return dt.unsqueeze(-1).expand(batch, nheads, headdim)
def _tied_dt_bias(nheads: int, headdim: int, device: str) -> torch.Tensor:
dt_bias = torch.rand(nheads, device=device) - 4.0
return dt_bias.view(nheads, 1).expand(nheads, headdim)
def _run_standard_decode(
*,
state_dtype: torch.dtype,
act_dtype: torch.dtype,
batch: int,
nheads: int,
headdim: int,
ngroups: int,
dstate: int,
max_cache_len: int,
num_steps: int,
has_z: bool,
dt_softplus: bool,
use_dt_bias: bool,
desync_write_pos: bool = False,
seed: int = 0,
) -> None:
"""Drive ``num_steps`` decode steps and check every path against the
trusted recurrence. ``state_dtype`` is the recurrent-state precision;
``act_dtype`` is the activation/buffer precision."""
device = "cuda"
both_fp32 = state_dtype == torch.float32 and act_dtype == torch.float32
rtol, atol = _tolerances(torch.float32 if both_fp32 else torch.bfloat16)
set_random_seed(seed)
# One state copy per path; all start identical at the same precision.
state0 = torch.randn(
batch, nheads, headdim, dstate, dtype=state_dtype, device=device
)
state_anchor = state0.clone()
state_baseline = state0.clone()
state_cached = state0.clone()
state_ref = state0.clone()
A = _tied_A(nheads, headdim, dstate, device)
dt_bias = _tied_dt_bias(nheads, headdim, device) if use_dt_bias else None
D = torch.randn(nheads, headdim, device=device)
# Caches follow the activation dtype (dt_cache is forced to fp32 inside).
x_cache, dt_cache, B_cache, _ = allocate_update_caches(
batch,
nheads,
ngroups,
headdim,
dstate,
max_cache_len,
device,
act_dtype,
act_dtype,
)
x_cache_ref, dt_cache_ref, B_cache_ref, _ = allocate_update_caches(
batch,
nheads,
ngroups,
headdim,
dstate,
max_cache_len,
device,
act_dtype,
act_dtype,
)
bc_pre = torch.empty(
batch, ngroups, max_cache_len, device=device, dtype=torch.float32
)
if desync_write_pos:
# Rows start at different ring positions so they flush on different
# steps, exercising per-row write-position handling.
write_pos = (
torch.arange(batch, device=device, dtype=torch.int32) % max_cache_len
)
else:
write_pos = torch.zeros(batch, dtype=torch.int32, device=device)
for _ in range(num_steps):
x = torch.randn(batch, nheads, headdim, device=device, dtype=act_dtype)
dt = _tied_dt(batch, nheads, headdim, device, act_dtype)
B = torch.randn(batch, ngroups, dstate, device=device, dtype=act_dtype)
C = torch.randn(batch, ngroups, dstate, device=device, dtype=act_dtype)
z = torch.randn_like(x) if has_z else None
is_flush = write_pos == max_cache_len - 1
# Trusted recurrence (mutates state_anchor in place, returns output).
out_anchor = selective_state_update_ref(
state_anchor,
x,
dt,
A,
B,
C,
D=D,
z=z,
dt_bias=dt_bias,
dt_softplus=dt_softplus,
)
# Upstream baseline decode kernel.
out_baseline = torch.empty_like(x)
selective_state_update(
state_baseline,
x,
dt,
A,
B,
C,
D=D,
z=z,
dt_bias=dt_bias,
dt_softplus=dt_softplus,
out=out_baseline,
)
# ReplaySSM kernel under test + its pure-PyTorch reference.
out_cached = torch.empty_like(x)
common = dict(
D=D,
z=z,
dt_bias=dt_bias,
dt_softplus=dt_softplus,
x_cache=x_cache,
dt_cache=dt_cache,
B_cache=B_cache,
write_pos=write_pos,
is_flush=is_flush,
max_cache_len=max_cache_len,
out=out_cached,
)
selective_state_update_replayssm_output_only(
state_cached, x, dt, A, B, C, bc_pre=bc_pre, **common
)
out_ref = selective_state_update_replayssm_output_only_ref(
state_ref,
x,
dt,
A,
B,
C,
D=D,
z=z,
dt_bias=dt_bias,
dt_softplus=dt_softplus,
x_cache=x_cache_ref,
dt_cache=dt_cache_ref,
B_cache=B_cache_ref,
write_pos=write_pos,
max_cache_len=max_cache_len,
)
# The reference models the kernel's exact arithmetic (including its bf16
# reconstruction), so the kernel must match it tightly at every
# precision. At fp32 this also flags any TF32 reconstruction drift.
torch.testing.assert_close(out_cached, out_ref, rtol=rtol, atol=atol)
# When the STATE is fp32 the baseline decode kernel is a valid oracle (it
# does not downcast the state per step), so ReplaySSM must match it. At
# bf16 state the baseline legitimately differs: it downcasts the fp32
# state to bf16 every step, while ReplaySSM accumulates a whole buffer in
# fp32 and is the MORE accurate path -- so it is not a tight oracle there.
if state_dtype == torch.float32:
torch.testing.assert_close(out_cached, out_baseline, rtol=rtol, atol=atol)
# The exact elementwise reference is valid only when state AND
# activations are fp32; otherwise it downcasts the state (at readout for
# bf16 activations, or per step for bf16 state).
if both_fp32:
torch.testing.assert_close(out_cached, out_anchor, rtol=rtol, atol=atol)
# Cached inputs match the reference cache management.
torch.testing.assert_close(x_cache, x_cache_ref, rtol=rtol, atol=atol)
torch.testing.assert_close(dt_cache, dt_cache_ref, rtol=rtol, atol=atol)
torch.testing.assert_close(B_cache, B_cache_ref, rtol=rtol, atol=atol)
# Checkpoint state at flush matches the reference (and, when the state is
# fp32, the baseline kernel).
if bool(is_flush.any()):
torch.testing.assert_close(
state_cached[is_flush], state_ref[is_flush], rtol=rtol, atol=atol
)
if state_dtype == torch.float32:
torch.testing.assert_close(
state_cached[is_flush],
state_baseline[is_flush],
rtol=rtol,
atol=atol,
)
write_pos = torch.where(is_flush, torch.zeros_like(write_pos), write_pos + 1)
# State/activation precisions. fp32 state is the default; bf16/fp16 are the
# reduced-footprint configs. fp16 appears both as an activation dtype (fully-fp16
# model sfp16_afp16, or fp16 act over fp32 state s32_afp16) and as a state dtype
# under bf16 activations (sfp16_a16): fp16 has a finer mantissa than bf16 at the
# same 2 bytes, so it is a more accurate state at no extra footprint. We still
# skip fp16 state under fp32 activations (the unused low-state/high-act mix).
_PRECISIONS = [
pytest.param((torch.float32, torch.float32), id="s32_a32"),
pytest.param((torch.float32, torch.bfloat16), id="s32_a16"),
pytest.param((torch.bfloat16, torch.bfloat16), id="s16_a16"),
pytest.param((torch.float32, torch.float16), id="s32_afp16"),
pytest.param((torch.float16, torch.float16), id="sfp16_afp16"),
pytest.param((torch.float16, torch.bfloat16), id="sfp16_a16"),
]
# Small synthetic shapes for the full axis sweep (compile fast).
_SMALL_GEOMETRIES = [
pytest.param((8, 64, 64, 4), id="small"),
pytest.param((4, 64, 16, 1), id="tiny"),
]
# Production Mamba2 shapes (nheads, headdim, dstate, ngroups), TP=1.
_REAL_GEOMETRIES = [
pytest.param((96, 80, 128, 8), id="nano4b"),
pytest.param((128, 64, 128, 8), id="super120b"),
pytest.param((256, 64, 128, 8), id="ultra550b"),
]
@pytest.mark.skipif(not torch.cuda.is_available(), reason="Need CUDA device")
@pytest.mark.parametrize("precision", _PRECISIONS)
@pytest.mark.parametrize("max_cache_len", [1, 4, 16])
@pytest.mark.parametrize("geometry", _SMALL_GEOMETRIES)
@pytest.mark.parametrize("has_z", [False, True])
def test_replayssm_standard_decode_matches_reference(
precision: tuple[torch.dtype, torch.dtype],
max_cache_len: int,
geometry: tuple[int, int, int, int],
has_z: bool,
):
state_dtype, act_dtype = precision
nheads, headdim, dstate, ngroups = geometry
_run_standard_decode(
state_dtype=state_dtype,
act_dtype=act_dtype,
batch=4,
nheads=nheads,
headdim=headdim,
ngroups=ngroups,
dstate=dstate,
max_cache_len=max_cache_len,
num_steps=2 * max_cache_len + 1,
has_z=has_z,
dt_softplus=True,
use_dt_bias=True,
)
@pytest.mark.skipif(not torch.cuda.is_available(), reason="Need CUDA device")
@pytest.mark.parametrize("precision", _PRECISIONS)
@pytest.mark.parametrize("geometry", _REAL_GEOMETRIES)
def test_replayssm_standard_decode_real_geometry(
precision: tuple[torch.dtype, torch.dtype],
geometry: tuple[int, int, int, int],
):
# Production Mamba2 shapes for the Nemotron-3 family (Nano-4B / Super-120B /
# Ultra-550B), at the production buffer length (8). All three precisions,
# including the bf16 state case.
state_dtype, act_dtype = precision
nheads, headdim, dstate, ngroups = geometry
_run_standard_decode(
state_dtype=state_dtype,
act_dtype=act_dtype,
batch=4,
nheads=nheads,
headdim=headdim,
ngroups=ngroups,
dstate=dstate,
max_cache_len=8,
num_steps=17,
has_z=True,
dt_softplus=True,
use_dt_bias=True,
)
@pytest.mark.skipif(not torch.cuda.is_available(), reason="Need CUDA device")
@pytest.mark.parametrize(
"precision",
[
pytest.param((torch.float32, torch.float32), id="s32_a32"),
pytest.param((torch.float32, torch.bfloat16), id="s32_a16"),
pytest.param((torch.float32, torch.float16), id="s32_afp16"),
],
)
def test_replayssm_standard_decode_desync_write_pos(
precision: tuple[torch.dtype, torch.dtype],
):
# Rows start at staggered ring positions, so they flush on different steps
# and hold genuinely different cached histories.
state_dtype, act_dtype = precision
_run_standard_decode(
state_dtype=state_dtype,
act_dtype=act_dtype,
batch=4,
nheads=8,
headdim=64,
ngroups=4,
dstate=64,
max_cache_len=4,
num_steps=12,
has_z=True,
dt_softplus=True,
use_dt_bias=True,
desync_write_pos=True,
)
@pytest.mark.skipif(not torch.cuda.is_available(), reason="Need CUDA device")
@pytest.mark.parametrize("precision", _PRECISIONS)
@pytest.mark.parametrize("with_padding", [False, True])
def test_replayssm_standard_decode_with_batch_indices(
precision: tuple[torch.dtype, torch.dtype],
with_padding: bool,
):
# Sparse state allocation via state_batch_indices, with NULL_BLOCK_ID
# padding rows. The pure-PyTorch references do not model the sparse
# gather, so the anchor here is the upstream baseline decode kernel.
state_dtype, act_dtype = precision
device = "cuda"
both_fp32 = state_dtype == torch.float32 and act_dtype == torch.float32
rtol, atol = _tolerances(torch.float32 if both_fp32 else torch.bfloat16)
set_random_seed(0)
batch = 3
padding = 2 if with_padding else 0
padded_batch = batch + padding
total_state_slots = 16
nheads = 4
ngroups = 2
headdim = 64
dstate = 16
max_cache_len = 4
num_steps = 2 * max_cache_len
state = torch.randn(
total_state_slots, nheads, headdim, dstate, dtype=state_dtype, device=device
)
state_baseline = state.clone()
state_cached = state.clone()
state_before = state.clone()
state_indices = (
torch.randperm(total_state_slots - 1, device=device)[:batch] + 1
).to(torch.int32)
state_batch_indices = torch.cat(
[
state_indices,
torch.full((padding,), NULL_BLOCK_ID, dtype=torch.int32, device=device),
]
)
unused_states = torch.ones(total_state_slots, dtype=torch.bool, device=device)
unused_states[state_indices] = False
A = _tied_A(nheads, headdim, dstate, device)
dt_bias = _tied_dt_bias(nheads, headdim, device)
D = torch.randn(nheads, headdim, device=device)
x_cache = torch.zeros(
total_state_slots,
nheads,
max_cache_len,
headdim,
device=device,
dtype=act_dtype,
)
dt_cache = torch.zeros(
total_state_slots, nheads, max_cache_len, device=device, dtype=torch.float32
)
B_cache = torch.zeros(
total_state_slots,
ngroups,
max_cache_len,
dstate,
device=device,
dtype=act_dtype,
)
bc_pre = torch.empty(
padded_batch, ngroups, max_cache_len, device=device, dtype=torch.float32
)
write_pos = torch.zeros(padded_batch, dtype=torch.int32, device=device)
for _ in range(num_steps):
x = torch.randn(padded_batch, nheads, headdim, device=device, dtype=act_dtype)
dt = _tied_dt(padded_batch, nheads, headdim, device, act_dtype)
B = torch.randn(padded_batch, ngroups, dstate, device=device, dtype=act_dtype)
C = torch.randn(padded_batch, ngroups, dstate, device=device, dtype=act_dtype)
z = torch.randn_like(x)
is_flush = write_pos == max_cache_len - 1
out_baseline = torch.empty_like(x)
selective_state_update(
state_baseline,
x,
dt,
A,
B,
C,
D=D,
z=z,
dt_bias=dt_bias,
dt_softplus=True,
state_batch_indices=state_batch_indices,
out=out_baseline,
)
out_cached = torch.full_like(x, 42)
common = dict(
D=D,
z=z,
dt_bias=dt_bias,
dt_softplus=True,
x_cache=x_cache,
dt_cache=dt_cache,
B_cache=B_cache,
write_pos=write_pos,
is_flush=is_flush,
max_cache_len=max_cache_len,
state_batch_indices=state_batch_indices,
out=out_cached,
)
selective_state_update_replayssm_output_only(
state_cached, x, dt, A, B, C, bc_pre=bc_pre, **common
)
torch.testing.assert_close(
out_cached[:batch], out_baseline[:batch], rtol=rtol, atol=atol
)
if with_padding:
assert torch.equal(
out_cached[batch:], torch.full_like(out_cached[batch:], 42)
)
if bool(is_flush[:batch].all()):
torch.testing.assert_close(
state_cached[state_indices],
state_baseline[state_indices],
rtol=rtol,
atol=atol,
)
write_pos = torch.where(is_flush, torch.zeros_like(write_pos), write_pos + 1)
assert torch.equal(state_cached[unused_states], state_before[unused_states])
assert torch.equal(state_baseline[unused_states], state_before[unused_states])
# Geometries with (nheads, ngroups) both divisible by the tp below.
_TP_GEOMETRIES = [
pytest.param((8, 64, 64, 4), id="small"),
pytest.param((96, 80, 128, 8), id="nano4b"),
]
@pytest.mark.skipif(not torch.cuda.is_available(), reason="Need CUDA device")
@pytest.mark.parametrize("precision", _PRECISIONS)
@pytest.mark.parametrize("geometry", _TP_GEOMETRIES)
@pytest.mark.parametrize("tp", [2])
def test_replayssm_standard_decode_tp_head_shard_equivalence(
precision: tuple[torch.dtype, torch.dtype],
geometry: tuple[int, int, int, int],
tp: int,
):
"""Tensor-parallel correctness at the kernel boundary (single GPU).
The kernel has no cross-rank communication, so head sharding must be exactly
separable: one run over all heads must equal concatenating ``tp`` independent
per-rank runs on ``nheads // tp`` heads and ``ngroups // tp`` groups. This
guards the per-rank divisors and group->head mapping the state-shape wiring
relies on. The real TP1==TP2 engine check lives in the v1/e2e suite."""
state_dtype, act_dtype = precision
nheads, headdim, dstate, ngroups = geometry
assert nheads % tp == 0 and ngroups % tp == 0
device = "cuda"
both_fp32 = state_dtype == torch.float32 and act_dtype == torch.float32
rtol, atol = _tolerances(torch.float32 if both_fp32 else torch.bfloat16)
set_random_seed(0)
batch = 4
max_cache_len = 4
num_steps = 2 * max_cache_len + 1
nh_s = nheads // tp
ng_s = ngroups // tp
# Shards slice the tied params; never .contiguous() -- that would drop the
# stride-0 broadcast the kernel's TIE_HDIM asserts require.
A = _tied_A(nheads, headdim, dstate, device)
dt_bias = _tied_dt_bias(nheads, headdim, device)
D = torch.randn(nheads, headdim, device=device)
state_full = torch.randn(
batch, nheads, headdim, dstate, dtype=state_dtype, device=device
)
x_cache, dt_cache, B_cache, _ = allocate_update_caches(
batch,
nheads,
ngroups,
headdim,
dstate,
max_cache_len,
device,
act_dtype,
act_dtype,
)
bc_pre = torch.empty(
batch, ngroups, max_cache_len, device=device, dtype=torch.float32
)
# Per-rank shards, each seeded from the matching head slice so all start equal.
shard_state = []
shard_caches = []
for r in range(tp):
h0 = r * nh_s
shard_state.append(state_full[:, h0 : h0 + nh_s].contiguous())
xc, dtc, bc, _ = allocate_update_caches(
batch,
nh_s,
ng_s,
headdim,
dstate,
max_cache_len,
device,
act_dtype,
act_dtype,
)
bcp = torch.empty(
batch, ng_s, max_cache_len, device=device, dtype=torch.float32
)
shard_caches.append((xc, dtc, bc, bcp))
write_pos = torch.zeros(batch, dtype=torch.int32, device=device)
for _ in range(num_steps):
x = torch.randn(batch, nheads, headdim, device=device, dtype=act_dtype)
dt = _tied_dt(batch, nheads, headdim, device, act_dtype)
B = torch.randn(batch, ngroups, dstate, device=device, dtype=act_dtype)
C = torch.randn(batch, ngroups, dstate, device=device, dtype=act_dtype)
z = torch.randn_like(x)
is_flush = write_pos == max_cache_len - 1
out_full = torch.empty_like(x)
selective_state_update_replayssm_output_only(
state_full,
x,
dt,
A,
B,
C,
D=D,
z=z,
dt_bias=dt_bias,
dt_softplus=True,
x_cache=x_cache,
dt_cache=dt_cache,
B_cache=B_cache,
bc_pre=bc_pre,
write_pos=write_pos,
is_flush=is_flush,
max_cache_len=max_cache_len,
out=out_full,
)
for r in range(tp):
h0 = r * nh_s
g0 = r * ng_s
xc, dtc, bc, bcp = shard_caches[r]
out_shard = torch.empty(
batch, nh_s, headdim, device=device, dtype=act_dtype
)
selective_state_update_replayssm_output_only(
shard_state[r],
x[:, h0 : h0 + nh_s].contiguous(),
dt[:, h0 : h0 + nh_s],
A[h0 : h0 + nh_s],
B[:, g0 : g0 + ng_s].contiguous(),
C[:, g0 : g0 + ng_s].contiguous(),
D=D[h0 : h0 + nh_s].contiguous(),
z=z[:, h0 : h0 + nh_s].contiguous(),
dt_bias=dt_bias[h0 : h0 + nh_s],
dt_softplus=True,
x_cache=xc,
dt_cache=dtc,
B_cache=bc,
bc_pre=bcp,
write_pos=write_pos,
is_flush=is_flush,
max_cache_len=max_cache_len,
out=out_shard,
)
torch.testing.assert_close(
out_full[:, h0 : h0 + nh_s], out_shard, rtol=rtol, atol=atol
)
if bool(is_flush.any()):
torch.testing.assert_close(
state_full[:, h0 : h0 + nh_s][is_flush],
shard_state[r][is_flush],
rtol=rtol,
atol=atol,
)
write_pos = torch.where(is_flush, torch.zeros_like(write_pos), write_pos + 1)
+160
View File
@@ -76,3 +76,163 @@ def selective_state_update_ref(
if not has_heads:
out = out.squeeze(1)
return out
def selective_state_update_replayssm_output_only_ref(
state: torch.Tensor,
x: torch.Tensor,
dt: torch.Tensor,
A: torch.Tensor,
B: torch.Tensor,
C: torch.Tensor,
D: torch.Tensor | None = None,
z: torch.Tensor | None = None,
dt_bias: torch.Tensor | None = None,
dt_softplus: bool = False,
x_cache: torch.Tensor | None = None,
dt_cache: torch.Tensor | None = None,
B_cache: torch.Tensor | None = None,
write_pos: torch.Tensor | None = None,
max_cache_len: int = 16,
) -> torch.Tensor:
"""Pure-PyTorch cached-bc reference for validation."""
has_heads = state.dim() > 3
if state.dim() == 3:
state = state.unsqueeze(1)
if x.dim() == 2:
x = x.unsqueeze(1)
if dt.dim() == 2:
dt = dt.unsqueeze(1)
if A.dim() == 2:
A = A.unsqueeze(0)
if B.dim() == 2:
B = B.unsqueeze(1)
if C.dim() == 2:
C = C.unsqueeze(1)
if D is not None and D.dim() == 1:
D = D.unsqueeze(0)
if z is not None and z.dim() == 2:
z = z.unsqueeze(1)
if dt_bias is not None and dt_bias.dim() == 1:
dt_bias = dt_bias.unsqueeze(0)
batch, nheads, dim, dstate = state.shape
assert x.shape == (batch, nheads, dim)
assert dt.shape == x.shape
assert A.shape == (nheads, dim, dstate)
ngroups = B.shape[1]
assert nheads % ngroups == 0, "nheads must be divisible by ngroups"
assert B.shape == (batch, ngroups, dstate)
assert C.shape == B.shape
ratio = nheads // ngroups
dt_val = dt[:, :, 0].float()
if dt_bias is not None:
dt_val = dt_val + dt_bias[:, 0].float()
if dt_softplus:
dt_val = F.softplus(dt_val)
A_val = A[:, 0, 0].float()
C_heads = C.repeat_interleave(ratio, dim=1)
out = torch.empty(batch, nheads, dim, device=x.device, dtype=torch.float32)
assert x_cache is not None
assert dt_cache is not None
assert B_cache is not None
assert write_pos is not None
for b in range(batch):
cache_len = int(write_pos[b].item())
is_flush = cache_len == max_cache_len - 1
n_steps = cache_len + 1
dt_all = torch.zeros(nheads, n_steps, device=x.device, dtype=torch.float32)
if cache_len > 0:
dt_all[:, :cache_len] = dt_cache[b, :, :cache_len]
dt_all[:, cache_len] = dt_val[b]
cumsum = torch.cumsum(dt_all, dim=-1)
total = cumsum[:, -1]
dA_cumsum = A_val[:, None] * cumsum
dA_total = A_val * total
total_decay = torch.exp(dA_total)
scale = dt_all * torch.exp(dA_total[:, None] - dA_cumsum)
x_all = torch.zeros(nheads, dim, n_steps, device=x.device, dtype=x.dtype)
if cache_len > 0:
x_all[..., :cache_len] = x_cache[b, :, :cache_len, :].permute(0, 2, 1)
x_all[..., cache_len] = x[b]
B_all = torch.zeros(ngroups, n_steps, dstate, device=B.device, dtype=B.dtype)
if cache_len > 0:
B_all[:, :cache_len, :] = B_cache[b, :, :cache_len, :]
B_all[:, cache_len, :] = B[b]
B_heads = B_all.repeat_interleave(ratio, dim=0)
C_heads_b = C_heads[b]
if is_flush:
B_scaled = (B_heads.float() * scale[:, :, None]).to(B_heads.dtype)
delta = torch.einsum("hdk,hkn->hdn", x_all.float(), B_scaled.float())
state_new = state[b].float() * total_decay[:, None, None] + delta
state[b].copy_(state_new.to(state.dtype))
out[b] = torch.einsum("hdn,hn->hd", state_new, C_heads_b.float())
else:
checkpoint_out = torch.einsum(
"hdn,hn->hd", state[b].float(), C_heads_b.float()
)
checkpoint_out = checkpoint_out * total_decay[:, None]
BC = torch.einsum("hkn,hn->hk", B_heads.float(), C_heads_b.float())
cache_out = torch.einsum("hdk,hk->hd", x_all.float(), scale * BC)
out[b] = checkpoint_out + cache_out
x_cache[b, :, cache_len, :] = x[b]
dt_cache[b, :, cache_len] = dt_val[b]
B_cache[b, :, cache_len, :] = B[b]
if D is not None:
out = out + (x.float() * D[None]).to(out.dtype)
if z is not None:
out = out * F.silu(z.float())
out = out.to(x.dtype)
if not has_heads:
out = out.squeeze(1)
return out
def allocate_update_caches(
batch: int,
nheads: int,
ngroups: int,
dim: int,
dstate: int,
max_cache_len: int,
device: torch.device,
x_dtype: torch.dtype,
B_dtype: torch.dtype,
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
"""Allocate dense reference caches for standalone validation."""
x_cache = torch.zeros(
batch,
nheads,
max_cache_len,
dim,
device=device,
dtype=x_dtype,
)
dt_cache = torch.zeros(
batch,
nheads,
max_cache_len,
device=device,
dtype=torch.float32,
)
B_cache = torch.zeros(
batch,
ngroups,
max_cache_len,
dstate,
device=device,
dtype=B_dtype,
)
write_pos = torch.zeros(batch, dtype=torch.int32, device=device)
return x_cache, dt_cache, B_cache, write_pos