{"record":{"id":"7be20e8eff98be38","repo":"pytorch/pytorch","slug":"split-expects-at-least-a-1-dimension-tensor","errorCode":null,"errorMessage":"split expects at least a 1-dimension tensor","messagePattern":"split expects at least a 1-dimension tensor","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"functorch/dim/__init__.py","lineNumber":1269,"sourceCode":"            raise TypeError(\n                \"when dim is specified as a Dim object, split sizes must also be dimensions.\"\n            )\n        return _Tensor._torch_function_fallback(\n            torch.Tensor.split,\n            (type(tensor),),\n            (tensor, split_size_or_sections),\n            {\"dim\": dim},\n        )\n\n    if not all_dims:\n        raise TypeError(\"split list must be ints or dims but got a mix\")\n\n    # All are Dim objects - handle first-class dimension split\n    self_info = TensorInfo.create(tensor, ensure_batched=False, ensure_present=False)\n    ndim = self_info.ndim()\n\n    if not dim_is_object and ndim == 0:\n        raise TypeError(\"split expects at least a 1-dimension tensor\")\n\n    # Wrap the dimension\n    dim_l = _wrap_dim(dim, ndim, False) if dim is not None else DimEntry(-ndim)\n\n    # Find the index of the dimension in levels\n    idx = None\n    for i, level in enumerate(self_info.levels):\n        if level == dim_l:\n            idx = i\n            break\n\n    if idx is None:\n        if dim is None:\n            dim = 0\n        raise TypeError(f\"tensor does not contain dimension {dim}\")\n\n    # Calculate split indices\n    indices = []","sourceCodeStart":1251,"sourceCodeEnd":1287,"githubUrl":"https://github.com/pytorch/pytorch/blob/dcd2ecae775af66439b7ede4e7a82540b058c59c/functorch/dim/__init__.py#L1251-L1287","documentation":"On the all-Dims split path, the wrapper computes the tensor's ndim from its levels and refuses to split a 0-dimensional tensor when dim was not given as a Dim object. With no axes there is nothing to locate, and the default dim resolution (DimEntry(-ndim) i.e. -0) would be meaningless, so it raises TypeError.","triggerScenarios":"Calling split with Dim sizes on a scalar (0-d) tensor without passing an explicit dim, or passing dim=None: t = torch.tensor(3.0); t.split([d1, d2]).","commonSituations":"A reduction upstream (e.g. .sum() or indexing away all dims) leaving a scalar that then flows into a split; variable-length pipelines where a size-0 or scalar edge case was not covered.","solutions":["Guard for scalars before splitting: if t.ndim == 0: handle separately.","Fix the producer so the tensor keeps at least one dimension (e.g. keepdim=True on reductions).","Pass an explicit Dim as dim if you genuinely intend named-dim semantics — though a scalar still has no axis, so this usually indicates a logic bug upstream."],"exampleFix":"t = loss.sum()  # 0-d\np = t.split([d1, d2])\n\n# after\nt = loss.sum(keepdim=True)  # still 1-d\np = t.split([d1, d2])","handlingStrategy":"validation","validationCode":"if tensor.ndim == 0:\n    raise ValueError('cannot split a scalar tensor')\npieces = tensor.split(sections)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use keepdim=True on reductions feeding a split.","Cover scalar edge cases in shape-driven pipeline tests.","Log tensor.ndim before splitting in debugging builds."],"tags":["torchdim","split","zero-dim-tensor","edge-case"],"backgroundTag":null,"analyzedSha":"dcd2ecae775af66439b7ede4e7a82540b058c59c","analyzedAt":"2026-08-14T19:21:26.615Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}