{"record":{"id":"5205c8331ae7704a","repo":"sgl-project/sglang","slug":"g-cache-must-have-shape-slots-hv-l-k","errorCode":null,"errorMessage":"`g_cache` must have shape [slots, HV, L, K].","messagePattern":"`g_cache` must have shape \\[slots, HV, L, K\\]\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/kernels/ops/attention/helion/kda_replayssm.py","lineNumber":742,"sourceCode":"        raise ValueError(\"`write_pos` must be a 1D int32 tensor.\")\n    if write_pos.shape != (batch,):\n        raise ValueError(f\"`write_pos` must have shape {(batch,)}.\")\n    if force_flush is not None and (\n        force_flush.ndim != 1\n        or force_flush.dtype is not torch.int32\n        or force_flush.shape != (batch,)\n    ):\n        raise ValueError(\"`force_flush` must be a length-B int32 tensor or None.\")\n\n    cache_length = d_cache.size(2)\n    if cache_length < 1:\n        raise ValueError(\"ReplaySSM cache length must be at least 1.\")\n    if d_cache.shape[1:] != (num_v_heads, cache_length, value_dim):\n        raise ValueError(\"`d_cache` must have shape [slots, HV, L, V].\")\n    if k_cache.shape[1:] != (num_q_heads, cache_length, key_dim):\n        raise ValueError(\"`k_cache` must have shape [slots, H, L, K].\")\n    if g_cache.shape[1:] != (num_v_heads, cache_length, key_dim):\n        raise ValueError(\"`g_cache` must have shape [slots, HV, L, K].\")\n    if g_cache.dtype is not torch.float32:\n        raise ValueError(\"`g_cache` must have dtype torch.float32.\")\n\n    device = mixed_qkv.device\n    if any(\n        tensor.device != device for tensor in (d_cache, k_cache, g_cache, write_pos)\n    ):\n        raise ValueError(\"ReplaySSM inputs must be on the same device.\")\n    if force_flush is not None and force_flush.device != device:\n        raise ValueError(\"`force_flush` must be on the same device as the inputs.\")\n\n    cache_block = helion.next_power_of_2(max(16, cache_length))\n    use_lower_bound = lower_bound is not None\n    kernel = _select_replayssm_decode_kernel(\n        is_bf16_state=initial_state.dtype is torch.bfloat16,\n        num_v_heads=num_v_heads,\n    )\n    result = kernel(","sourceCodeStart":724,"sourceCodeEnd":760,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/kernels/ops/attention/helion/kda_replayssm.py#L724-L760","documentation":"The ReplaySSM gate cache g_cache must be [slots, HV, L, K]: grouped by value heads (num_v_heads), length matching d_cache, and last dim equal to key_dim (the gate shares the key dimension even though it lives on the HV head group). The wrapper validates g_cache.shape[1:] against (num_v_heads, cache_length, key_dim) before kernel selection. A mismatch usually means the gate cache was sized with value_dim or the wrong head count.","triggerScenarios":"Calling helion_fused_recurrent_kda_replayssm_decode with g_cache whose shape[1:] != (num_v_heads, d_cache.size(2), key_dim) — e.g. allocated with value_dim as the last dim, or grouped by num_q_heads, or a different cache length than d_cache.","commonSituations":"Copy-paste allocation of d/k/g caches where g_cache accidentally uses d_cache's dims; hybrid-model state pools whose shape tuple was updated for d but not g; tests exercising the per-row flush contract with hand-built caches.","solutions":["Allocate g_cache as [slots, num_v_heads, cache_length, key_dim] with the same cache_length as d_cache","Double-check the last dim is key_dim (not value_dim) — the gate is per-key-channel decay","Ensure all three caches share the same slots and L; add an assert in your allocation helper"],"exampleFix":"# before\ng_cache = torch.empty(slots, num_v_heads, cache_len, value_dim, dtype=torch.float32, device='cuda')\n# after\ng_cache = torch.empty(slots, num_v_heads, cache_len, key_dim, dtype=torch.float32, device='cuda')","handlingStrategy":"validation","validationCode":"assert g_cache.shape[1:] == (num_v_heads, d_cache.size(2), key_dim), g_cache.shape","typeGuard":"def valid_g_cache(t: torch.Tensor, hv: int, k: int, l: int) -> bool:\n    return t.ndim == 4 and t.shape[1:] == (hv, l, k)","tryCatchPattern":null,"preventionTips":["g_cache last dim is key_dim, not value_dim","Write one shape-check helper for d/k/g caches and call it right before the kernel"],"tags":["helion","kda","replayssm","tensor-shape","gate-cache"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}