{"record":{"id":"f0c653787a472aef","repo":"sgl-project/sglang","slug":"expected-a-tensor-with-at-least-one-dimension","errorCode":null,"errorMessage":"expected a tensor with at least one dimension","messagePattern":"expected a tensor with at least one dimension","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/kernels/ops/quantization/nvfp4_gemm_swiglu_nvfp4_quant.py","lineNumber":2708,"sourceCode":"from flashinfer.utils import get_compute_capability  # noqa: E402\n\n\ndef _round_up(value: int, multiple: int) -> int:\n    return (value + multiple - 1) // multiple * multiple\n\n\ndef interleave_linear_and_gate(\n    tensor: torch.Tensor,\n    group_size: int = 64,\n    dim: int = 0,\n) -> torch.Tensor:\n    \"\"\"Rewrite ``[linear all][gate all]`` along ``dim`` as\n    ``[linear chunk][gate chunk]…`` with ``group_size`` rows per chunk.\n\n    Matches the FC1 GEMM+SwiGLU layout the fused-gemm kernel expects.\n    \"\"\"\n    if tensor.ndim == 0:\n        raise ValueError(\"expected a tensor with at least one dimension\")\n    dim = dim % tensor.ndim\n    sizes = tensor.size()\n    dim_size = sizes[dim]\n    if dim_size % (group_size * 2) != 0:\n        raise ValueError(\n            f\"dimension {dim} size {dim_size} must be divisible by \"\n            f\"2 * group_size={2 * group_size}\"\n        )\n    prev_sizes = sizes[:dim]\n    post_sizes = sizes[dim + 1 :]\n    return (\n        tensor.reshape(\n            *prev_sizes,\n            2,\n            dim_size // (group_size * 2),\n            group_size,\n            *post_sizes,\n        )","sourceCodeStart":2690,"sourceCodeEnd":2726,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/kernels/ops/quantization/nvfp4_gemm_swiglu_nvfp4_quant.py#L2690-L2726","documentation":"interleave_linear_and_gate rewrites a concatenated [linear weights; gate weights] matrix into interleaved chunks for the fused FC1 GEMM+SwiGLU kernel. It requires at least a 1-D tensor; a 0-dim (scalar) tensor has no dimension to interleave along, so it is rejected immediately.","triggerScenarios":"Calling interleave_linear_and_gate on a 0-dimensional tensor (e.g. tensor created with torch.tensor(1.0) or an over-squeezed weight).","commonSituations":"Weight-loading bugs where an accidental .squeeze()/item() collapses the FC1 weight to a scalar, or unit tests passing dummy scalar tensors.","solutions":["Inspect the tensor's shape before calling; restore the expected 2-D [2*intermediate, hidden] weight","Fix upstream .squeeze()/.item() calls that collapsed the weight to 0-dim"],"exampleFix":"// before\nt = torch.tensor(1.0)\nw = interleave_linear_and_gate(t, group_size=64)\n// after\nt = t.reshape(1, 1)\nw = interleave_linear_and_gate(t, group_size=64)","handlingStrategy":"validation","validationCode":"if tensor.ndim == 0: raise ValueError(f'weight must be >=1-D, got shape {tuple(tensor.shape)}')","typeGuard":"def is_non_scalar(t): return t.ndim >= 1","tryCatchPattern":null,"preventionTips":["Log weight shapes before repacking","Avoid unconditional .squeeze()/item() on weights"],"tags":["nvfp4","weight-loading","shape-validation"],"backgroundTag":"tensor-shape-validation","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}