{"record":{"id":"21ecbf7cd9b7e02f","repo":"sgl-project/sglang","slug":"write-pos-must-be-a-1d-int32-tensor-21ecbf","errorCode":null,"errorMessage":"`write_pos` must be a 1D int32 tensor.","messagePattern":"`write_pos` must be a 1D int32 tensor\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/kernels/ops/attention/helion/kda_replayssm.py","lineNumber":724,"sourceCode":"    if a.ndim not in (2, 3) or not a.is_contiguous():\n        raise ValueError(\"KDA `a` must be a contiguous 2D or 3D tensor.\")\n    if dt_bias.ndim not in (1, 2) or not dt_bias.is_contiguous():\n        raise ValueError(\"KDA `dt_bias` must be a contiguous 1D or 2D tensor.\")\n    flat_a = a.view(batch, -1)\n    flat_dt_bias = dt_bias.view(-1)\n    _, num_q_heads, num_v_heads, key_dim, value_dim = validate_packed_decode_inputs(\n        mixed_qkv,\n        flat_a,\n        b,\n        A_log,\n        flat_dt_bias,\n        initial_state,\n        out,\n        ssm_state_indices,\n    )\n\n    if write_pos.ndim != 1 or write_pos.dtype is not torch.int32:\n        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].\")","sourceCodeStart":706,"sourceCodeEnd":742,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/kernels/ops/attention/helion/kda_replayssm.py#L706-L742","documentation":"ReplaySSM decode writes each row's recomputed output into a per-request ring cache at position write_pos[row]; the kernel requires write_pos to be a 1D int32 tensor so its indexing is well-defined. The guard rejects other dtypes (int64, bool) and multi-dimensional tensors before the launch.","triggerScenarios":"Passing write_pos as torch.int64 (the default from torch.arange / tensor(...)), a 0-dim scalar tensor, or a [B,1] tensor to helion_fused_recurrent_kda_replayssm_decode.","commonSituations":"Creating write_pos with torch.arange without dtype=torch.int32; index arithmetic promoting to long; a caller porting from an API that accepted int64 positions.","solutions":["Cast explicitly: write_pos = write_pos.to(torch.int32)","Construct with the right dtype: torch.arange(B, dtype=torch.int32, device=...)","Squeeze stray dims: write_pos = write_pos.squeeze(-1) if it came in as [B,1]"],"exampleFix":"// before\nwrite_pos = torch.arange(batch, device=dev)  # int64\n// after\nwrite_pos = torch.arange(batch, device=dev, dtype=torch.int32)","handlingStrategy":"type-guard","validationCode":"if write_pos.dtype is not torch.int32 or write_pos.ndim != 1:\n    write_pos = write_pos.reshape(-1).to(torch.int32)","typeGuard":"def valid_write_pos(t: torch.Tensor) -> bool:\n    return isinstance(t, torch.Tensor) and t.ndim == 1 and t.dtype is torch.int32","tryCatchPattern":null,"preventionTips":["Always construct index tensors with dtype=torch.int32","Centralize cache-index creation in one helper"],"tags":["kda","replayssm","dtype","int32","tensor-ndim"],"backgroundTag":"wrong-tensor-dtype","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}