{"record":{"id":"05c586a8efc71d83","repo":"Comfy-Org/ComfyUI","slug":"the-new-shape-must-have-the-same-number-of-dimensi","errorCode":null,"errorMessage":"The new shape must have the same number of dimensions as the original tensor","messagePattern":"The new shape must have the same number of dimensions as the original tensor","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"comfy/lora.py","lineNumber":399,"sourceCode":"    \"\"\"\n    Pad a tensor to a new shape with zeros.\n\n    Args:\n        tensor (torch.Tensor): The original tensor to be padded.\n        new_shape (List[int]): The desired shape of the padded tensor.\n\n    Returns:\n        torch.Tensor: A new tensor padded with zeros to the specified shape.\n\n    Note:\n        If the new shape is smaller than the original tensor in any dimension,\n        the original tensor will be truncated in that dimension.\n    \"\"\"\n    if any([new_shape[i] < tensor.shape[i] for i in range(len(new_shape))]):\n        raise ValueError(\"The new shape must be larger than the original tensor in all dimensions\")\n\n    if len(new_shape) != len(tensor.shape):\n        raise ValueError(\"The new shape must have the same number of dimensions as the original tensor\")\n\n    # Create a new tensor filled with zeros\n    padded_tensor = torch.zeros(new_shape, dtype=tensor.dtype, device=tensor.device)\n\n    # Create slicing tuples for both tensors\n    orig_slices = tuple(slice(0, dim) for dim in tensor.shape)\n    new_slices = tuple(slice(0, dim) for dim in tensor.shape)\n\n    # Copy the original tensor into the new tensor\n    padded_tensor[new_slices] = tensor[orig_slices]\n\n    return padded_tensor\n\ndef calculate_shape(patches, weight, key, original_weights=None):\n    current_shape = weight.shape\n\n    for p in patches:\n        v = p[1]","sourceCodeStart":381,"sourceCodeEnd":417,"githubUrl":"https://github.com/Comfy-Org/ComfyUI/blob/1c6d8d45b3693bfbb32385b410d813a7fd6be216/comfy/lora.py#L381-L417","documentation":"pad_tensor requires new_shape to have the same number of dimensions as the input tensor; a rank mismatch raises ValueError before any allocation. Padding is elementwise-per-dimension, so a 3-element shape for a 4-D tensor is meaningless.","triggerScenarios":"pad_tensor(torch.zeros(1,64,64), [1,64,64,64]) or any call where len(new_shape) != tensor.ndim.","commonSituations":"Hardcoding a target shape that assumes a different rank (image vs video tensors, conv1d vs conv2d weights); passing a full weight shape to pad a bias vector.","solutions":["Build new_shape as list(tensor.shape) with only the padded dims increased: shape = list(t.shape); shape[-1] += extra.","Validate len(new_shape) == tensor.dim() before calling.","Double-check which weight (bias vs weight) you are padding."],"exampleFix":"# before\nnew_shape = [1, 128, 128]  # for a 4-D conv weight\npadded = pad_tensor(w, new_shape)\n# after\nnew_shape = list(w.shape); new_shape[1] = 128; new_shape[2] = 128; new_shape[3] = 128\npadded = pad_tensor(w, new_shape)","handlingStrategy":"validation","validationCode":"if len(new_shape) != tensor.dim():\n    raise ValueError(f\"new_shape has {len(new_shape)} dims, tensor has {tensor.dim()}\")\nout = pad_tensor(tensor, new_shape)","typeGuard":"def same_rank(tensor, new_shape) -> bool:\n    return len(new_shape) == tensor.dim()","tryCatchPattern":null,"preventionTips":["Build the target shape from list(tensor.shape) and grow only the dims you need.","Keep bias and weight padding code paths separate."],"tags":["lora","padding","rank-mismatch","weights"],"backgroundTag":null,"analyzedSha":"1c6d8d45b3693bfbb32385b410d813a7fd6be216","analyzedAt":"2026-08-14T19:37:18.893Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}