{"record":{"id":"28b0986ade595e89","repo":"sgl-project/sglang","slug":"cu-seqlens-q-tensor-must-be-int32","errorCode":null,"errorMessage":"cu_seqlens_q tensor must be Int32","messagePattern":"cu_seqlens_q tensor must be Int32","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"python/sglang/kernels/ops/attention/flash_attn/cute/flash_fwd.py","lineNumber":217,"sourceCode":"        mCuSeqlensK_type: Type[cutlass.Numeric] | None,\n        mSeqUsedQ_type: Type[cutlass.Numeric] | None,\n        mSeqUsedK_type: Type[cutlass.Numeric] | None,\n    ):\n        # Get the data type and check if it is fp16 or bf16\n        if const_expr(self.is_split_kv):\n            # SplitKV writes float32 partial outputs; Q/K/V still fp16/bf16.\n            if const_expr(not (mQ_type == mK_type == mV_type)):\n                raise TypeError(\"Q/K/V must have the same data type\")\n            if const_expr(mO_type != Float32):\n                raise TypeError(\"SplitKV partial output (mO) must be Float32\")\n        elif const_expr(not (mQ_type == mK_type == mV_type == mO_type)):\n            raise TypeError(\"All tensors must have the same data type\")\n        if const_expr(mQ_type not in [cutlass.Float16, cutlass.BFloat16]):\n            raise TypeError(\"Only Float16 or BFloat16 is supported\")\n        if const_expr(mLSE_type not in [None, Float32]):\n            raise TypeError(\"LSE tensor must be Float32\")\n        if const_expr(mCuSeqlensQ_type not in [None, Int32]):\n            raise TypeError(\"cu_seqlens_q tensor must be Int32\")\n        if const_expr(mCuSeqlensK_type not in [None, Int32]):\n            raise TypeError(\"cu_seqlens_k tensor must be Int32\")\n        if const_expr(mSeqUsedQ_type not in [None, Int32]):\n            raise TypeError(\"seqused_q tensor must be Int32\")\n        if const_expr(mSeqUsedK_type not in [None, Int32]):\n            raise TypeError(\"seqused_k tensor must be Int32\")\n        assert mQ_type == self.dtype\n\n    def _setup_attributes(self):\n        # ///////////////////////////////////////////////////////////////////////////////\n        # Shared memory layout: Q/K/V\n        # ///////////////////////////////////////////////////////////////////////////////\n        (\n            sQ_layout_atom,\n            sK_layout_atom,\n            sV_layout_atom,\n            sO_layout_atom,\n            sP_layout_atom,","sourceCodeStart":199,"sourceCodeEnd":235,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/kernels/ops/attention/flash_attn/cute/flash_fwd.py#L199-L235","documentation":"The optional cu_seqlens_q tensor (cumulative sequence lengths for varlen batched queries) must have Int32 element type, matching the kernel's index arithmetic. Passing None is allowed when not using varlen mode.","triggerScenarios":"Supplying cu_seqlens_q as Int64 (the default for torch.cumsum output) or Int16/UInt32 to FlashAttentionForward.","commonSituations":"Computing cu_seqlens with torch.cumsum(seq_lens, dim=0) which yields int64 and passing it directly; converting data between frameworks that default to int64 indices.","solutions":["Cast to int32: cu_seqlens_q = cu_seqlens_q.to(torch.int32)","Or compute directly in int32: torch.zeros(..., dtype=torch.int32) and accumulate","Verify cu_seqlens_k has the same treatment (it has an identical check)"],"exampleFix":"// before\ncu_seqlens_q = torch.cumsum(seq_lens, 0)  # int64\n// after\ncu_seqlens_q = torch.cumsum(seq_lens, 0).to(torch.int32)","handlingStrategy":"type-guard","validationCode":"if cu_seqlens_q is not None:\n    assert cu_seqlens_q.dtype == torch.int32","typeGuard":"def int32_or_none(t) -> bool:\n    return t is None or t.dtype == torch.int32","tryCatchPattern":null,"preventionTips":["Wrap cumsum: lambda x: torch.cumsum(x, 0).to(torch.int32)","Store all attention metadata as int32 tensors in your batch preparation step"],"tags":["cuda","dtype","flash-attention","varlen","int32"],"backgroundTag":null,"analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}