{"record":{"id":"e050cf7ffe859c83","repo":"sgl-project/sglang","slug":"write-pos-must-be-a-1d-int32-tensor","errorCode":null,"errorMessage":"`write_pos` must be a 1D int32 tensor.","messagePattern":"`write_pos` must be a 1D int32 tensor\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/kernels/ops/attention/fla/fused_recurrent_linear_replayssm.py","lineNumber":490,"sourceCode":"    Allocates nothing persistent: the caller owns the ring tensors and is\n    responsible for advancing / resetting ``write_pos`` (e.g. ``(write_pos+1) %\n    L`` after each step).  This is a STANDALONE kernel; the memory-pool / cache\n    integration is a later phase.\n    \"\"\"\n    if mixed_qkv.ndim != 2:\n        raise ValueError(f\"`mixed_qkv` must be 2D (got ndim={mixed_qkv.ndim}).\")\n    if mixed_qkv.stride(-1) != 1:\n        raise ValueError(\"`mixed_qkv` must be contiguous in the last dim.\")\n    if b.ndim != 2:\n        raise ValueError(f\"`b` must be 2D (got b.ndim={b.ndim}).\")\n    if A_log.ndim != 1:\n        raise ValueError(\"`A_log` must be a 1D tensor.\")\n    if initial_state.ndim != 4:\n        raise ValueError(f\"`initial_state` must be 4D (got ndim={initial_state.ndim}).\")\n    if not out.is_contiguous():\n        raise ValueError(\"`out` must be contiguous.\")\n    if write_pos.ndim != 1 or write_pos.dtype != torch.int32:\n        raise ValueError(\"`write_pos` must be a 1D int32 tensor.\")\n    if force_flush is not None and (\n        force_flush.ndim != 1 or force_flush.dtype != torch.int32\n    ):\n        raise ValueError(\"`force_flush` must be a 1D int32 tensor or None.\")\n\n    B = mixed_qkv.shape[0]\n    num_state_slots, HV, V, K = initial_state.shape\n    qkv_dim = mixed_qkv.shape[1]\n    q_dim = (qkv_dim - HV * V) // 2\n    if q_dim <= 0 or q_dim % K != 0:\n        raise ValueError(\n            f\"Invalid packed `mixed_qkv` last dim={qkv_dim} for HV={HV}, V={V}, K={K}.\"\n        )\n    H = q_dim // K\n    if H <= 0 or HV % H != 0:\n        raise ValueError(\n            f\"Invalid head config inferred from mixed_qkv: H={H}, HV={HV}.\"\n        )","sourceCodeStart":472,"sourceCodeEnd":508,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/kernels/ops/attention/fla/fused_recurrent_linear_replayssm.py#L472-L508","documentation":"write_pos — the per-row ring write position for the replaySSM decode kernel — must be a 1D tensor of dtype torch.int32. Wrong rank (scalar/2D) or dtype (int64/int16) is rejected because the Triton kernel loads it directly as int32 offsets.","triggerScenarios":"Passing a Python int, an int64 tensor (e.g. from torch.arange default), or a per-head [N, HV] position tensor.","commonSituations":"Creating positions with torch.arange without dtype=torch.int32; advancing write_pos with arithmetic that promotes to int64; graph-capture tests reusing stale dtype buffers.","solutions":["Create/convert as: write_pos = write_pos.to(torch.int32) and ensure .ndim == 1 (one per row/slot)","Pin dtype explicitly at allocation: torch.zeros(N, dtype=torch.int32, device=dev)","Also make force_flush 1D int32 if used"],"exampleFix":"// before\nwrite_pos = torch.arange(N, device=dev)  # int64\n// after\nwrite_pos = torch.arange(N, device=dev, dtype=torch.int32)","handlingStrategy":"validation","validationCode":"write_pos = write_pos.to(torch.int32).reshape(-1)\nassert write_pos.ndim == 1 and write_pos.dtype == torch.int32","typeGuard":"def valid_write_pos(t: torch.Tensor) -> bool:\n    return t.ndim == 1 and t.dtype == torch.int32","tryCatchPattern":null,"preventionTips":["Always allocate control tensors with explicit dtype=torch.int32","Watch for dtype promotion when advancing positions (wrap with .to(torch.int32))"],"tags":["pytorch","dtype","replayssm","ring-buffer"],"backgroundTag":"wrong-tensor-dtype","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}