{"record":{"id":"06de5397d15f45fd","repo":"Stability-AI/generative-models","slug":"input-has-x-ndim-dims-but-target-dims-is-target","errorCode":null,"errorMessage":"input has {x.ndim} dims but target_dims is {target_dims}, which is less","messagePattern":"input has (.+?) dims but target_dims is (.+?), which is less","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sgm/util.py","lineNumber":196,"sourceCode":"def get_obj_from_str(string, reload=False, invalidate_cache=True):\n    module, cls = string.rsplit(\".\", 1)\n    if invalidate_cache:\n        importlib.invalidate_caches()\n    if reload:\n        module_imp = importlib.import_module(module)\n        importlib.reload(module_imp)\n    return getattr(importlib.import_module(module, package=None), cls)\n\n\ndef append_zero(x):\n    return torch.cat([x, x.new_zeros([1])])\n\n\ndef append_dims(x, target_dims):\n    \"\"\"Appends dimensions to the end of a tensor until it has target_dims dimensions.\"\"\"\n    dims_to_append = target_dims - x.ndim\n    if dims_to_append < 0:\n        raise ValueError(\n            f\"input has {x.ndim} dims but target_dims is {target_dims}, which is less\"\n        )\n    return x[(...,) + (None,) * dims_to_append]\n\n\ndef load_model_from_config(config, ckpt, verbose=True, freeze=True):\n    print(f\"Loading model from {ckpt}\")\n    if ckpt.endswith(\"ckpt\"):\n        pl_sd = torch.load(ckpt, map_location=\"cpu\")\n        if \"global_step\" in pl_sd:\n            print(f\"Global Step: {pl_sd['global_step']}\")\n        sd = pl_sd[\"state_dict\"]\n    elif ckpt.endswith(\"safetensors\"):\n        sd = load_safetensors(ckpt)\n    else:\n        raise NotImplementedError\n\n    model = instantiate_from_config(config.model)","sourceCodeStart":178,"sourceCodeEnd":214,"githubUrl":"https://github.com/Stability-AI/generative-models/blob/e8cd657656fa5d61688191730d0e03242bf4ed44/sgm/util.py#L178-L214","documentation":"append_dims adds trailing singleton dimensions to a tensor until it reaches target_dims. This ValueError is raised when the tensor already has MORE dimensions than target_dims (dims_to_append is negative), meaning the caller passed a mismatched rank — appending cannot remove dims, so it fails loudly rather than silently reshaping.","triggerScenarios":"Calling sgm.util.append_dims(x, target_dims) where x.ndim > target_dims, e.g. append_dims on a 5D video tensor with target_dims=4, or reusing a target_dims constant tuned for 2D images on higher-rank inputs.","commonSituations":"Sampler/model code (do_img2img, forward, __call__, _forward, sampler_step, ancestral_euler_step) shaping noise or timesteps: switching between image (4D) and video (5D) models without updating target_dims, or accidentally passing an already-broadcast tensor with an extra batch/time dim.","solutions":["Increase target_dims to at least x.ndim (e.g. 5 for video tensors)","Check x.ndim before calling and squeeze unintended dims with x.squeeze(dim) if a dim was added accidentally","If you need to match a reference tensor's rank, use target_dims=x.dim() of the tensor you are broadcasting against"],"exampleFix":"// before\nnoise = append_dims(noise, 4)  # noise is 5D video latent\n// after\nnoise = append_dims(noise, 5)  # match video latent rank","handlingStrategy":"validation","validationCode":"def safe_append_dims(x, target_dims):\n    assert x.ndim <= target_dims, f\"x has {x.ndim} dims, target_dims={target_dims} too small\"\n    return append_dims(x, target_dims)","typeGuard":"def fits_target_dims(x: torch.Tensor, target_dims: int) -> bool:\n    return x.ndim <= target_dims","tryCatchPattern":"try:\n    out = append_dims(x, target_dims)\nexcept ValueError as e:\n    logger.error(\"rank mismatch: %s (x.ndim=%d, target=%d)\", e, x.ndim, target_dims)\n    raise","preventionTips":["Derive target_dims from the reference tensor's .ndim instead of hardcoding","Assert tensor rank right after model/latent creation","Add unit tests covering image (4D) and video (5D) paths"],"tags":["python","pytorch","tensor-shape","valueerror"],"backgroundTag":"tensor-rank-mismatch","analyzedSha":"e8cd657656fa5d61688191730d0e03242bf4ed44","analyzedAt":"2026-08-29T11:23:43.234Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}