{"record":{"id":"af5c5539afdad2f3","repo":"sgl-project/sglang","slug":"lse-tensor-must-have-2-or-3-dimensions-batch-se","errorCode":null,"errorMessage":"LSE tensor must have 2 or 3 dimensions: (batch, seqlen, nheads) or (total_q, nheads)","messagePattern":"LSE tensor must have 2 or 3 dimensions: \\(batch, seqlen, nheads\\) or \\(total_q, nheads\\)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/kernels/ops/attention/flash_attn/cute/flash_fwd_combine.py","lineNumber":243,"sourceCode":"            raise TypeError(\"LSE partial tensor must be Float32\")\n        if const_expr(mLSE is not None and mLSE.element_type not in [Float32]):\n            raise TypeError(\"LSE tensor must be Float32\")\n\n        # Shape validation - input tensors are in user format, need to be converted to kernel format\n        if const_expr(len(mO_partial.shape) not in [4, 5]):\n            raise ValueError(\n                \"O partial tensor must have 4 or 5 dimensions: (num_splits, batch, seqlen, nheads, headdim) or (num_splits, total_q, nheads, headdim)\"\n            )\n        if const_expr(len(mLSE_partial.shape) not in [3, 4]):\n            raise ValueError(\n                \"LSE partial tensor must have 3 or 4 dimensions: (num_splits, batch, seqlen, nheads) or (num_splits, total_q, nheads)\"\n            )\n        if const_expr(len(mO.shape) not in [3, 4]):\n            raise ValueError(\n                \"O tensor must have 3 or 4 dimensions: (batch, seqlen, nheads, headdim) or (total_q, nheads, headdim)\"\n            )\n        if const_expr(mLSE is not None and len(mLSE.shape) not in [2, 3]):\n            raise ValueError(\n                \"LSE tensor must have 2 or 3 dimensions: (batch, seqlen, nheads) or (total_q, nheads)\"\n            )\n\n        mO_partial, mO = [assume_tensor_aligned(t) for t in (mO_partial, mO)]\n        # (num_splits, b, seqlen, h, d) -> (seqlen, d, num_splits, h, b)\n        # or (num_splits, total_q, h, d) -> (total_q, d, num_splits, h)\n        O_partial_layout_transpose = (\n            [2, 4, 0, 3, 1] if const_expr(cu_seqlens is None) else [1, 3, 0, 2]\n        )\n        # (b, seqlen, h, d) -> (seqlen, d, h, b) or (total_q, h, d) -> (total_q, d, h)\n        mO_partial = cute.make_tensor(\n            mO_partial.iterator,\n            cute.select(mO_partial.layout, mode=O_partial_layout_transpose),\n        )\n        O_layout_transpose = (\n            [1, 3, 2, 0] if const_expr(cu_seqlens is None) else [0, 2, 1]\n        )\n        mO = cute.make_tensor(","sourceCodeStart":225,"sourceCodeEnd":261,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/kernels/ops/attention/flash_attn/cute/flash_fwd_combine.py#L225-L261","documentation":"The optional final LSE tensor must be 2D (batch, nheads) batched or 3D (total_q, nheads) varlen — again one rank below the LSE partials.","triggerScenarios":"Passing mLSE (non-None) with rank other than 2 or 3.","commonSituations":"Carrying over the num_splits axis, or allocating LSE with an extra seqlen dim in varlen mode.","solutions":["Batched: mLSE shape (batch, nheads); varlen: (total_q, nheads)","Or pass mLSE=None if LSE output is not needed"],"exampleFix":"// before\nmLSE = torch.empty(num_splits, b, h)\n// after\nmLSE = torch.empty(b, h, dtype=torch.float32)","handlingStrategy":"validation","validationCode":"if mLSE is not None:\n    assert mLSE.dim() in (2, 3) and mLSE.dim() == mLSE_partial.dim() - 1","typeGuard":"def final_lse_ok(lse, lse_partial) -> bool:\n    return lse is None or (lse.dim() in (2, 3) and lse.dim() == lse_partial.dim() - 1)","tryCatchPattern":null,"preventionTips":["Mirror the rank relation: final = partial minus the num_splits axis"],"tags":["flash-attention","shape-mismatch","validation"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}