{"record":{"id":"294ef97ac33f294a","repo":"Comfy-Org/ComfyUI","slug":"the-new-shape-must-be-larger-than-the-original-ten","errorCode":null,"errorMessage":"The new shape must be larger than the original tensor in all dimensions","messagePattern":"The new shape must be larger than the original tensor in all dimensions","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"comfy/lora.py","lineNumber":396,"sourceCode":"\n\ndef pad_tensor_to_shape(tensor: torch.Tensor, new_shape: list[int]) -> torch.Tensor:\n    \"\"\"\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","sourceCodeStart":378,"sourceCodeEnd":414,"githubUrl":"https://github.com/Comfy-Org/ComfyUI/blob/1c6d8d45b3693bfbb32385b410d813a7fd6be216/comfy/lora.py#L378-L414","documentation":"comfy.lora.pad_tensor zero-pads a tensor to a strictly larger shape; if new_shape is smaller than tensor.shape in any dimension it raises ValueError instead of truncating (despite the docstring note, the code enforces grow-only). This guards silent data loss when resizing LoRA weight deltas to match a model.","triggerScenarios":"pad_tensor(weight, new_shape) where any new_shape[i] < tensor.shape[i] — e.g. padding a LoRA matrix up to a base weight that is smaller, or swapped arguments.","commonSituations":"Applying a LoRA trained on a larger model (e.g. different hidden size) to a smaller one; argument order mistakes; mismatched checkpoints after model architecture changes.","solutions":["Ensure new_shape >= tensor.shape in every dimension; the pad target must be the larger, model-side shape.","Verify tensor.shape and new_shape before the call and fix the source of the mismatch (wrong LoRA for this base model).","If you genuinely need shrink-to-fit, slice the tensor yourself — this API will not truncate."],"exampleFix":"# before\npadded = pad_tensor(lora_weight, model_weight.shape)  # model dim < lora dim\n# after\npadded = pad_tensor(lora_weight, max_shape)  # ensure all dims >= lora_weight.shape","handlingStrategy":"validation","validationCode":"if any(n < t for n, t in zip(new_shape, tensor.shape)):\n    raise ValueError(f\"cannot pad {tuple(tensor.shape)} down to {tuple(new_shape)}; check base model / LoRA match\")\nout = pad_tensor(tensor, new_shape)","typeGuard":"def is_grow_only(tensor, new_shape) -> bool:\n    return all(n >= t for n, t in zip(new_shape, tensor.shape))","tryCatchPattern":null,"preventionTips":["Verify LoRA dims match the base model architecture before padding.","Derive new_shape from the model weight's shape, never hardcode smaller values."],"tags":["lora","padding","shape-mismatch","weights"],"backgroundTag":null,"analyzedSha":"1c6d8d45b3693bfbb32385b410d813a7fd6be216","analyzedAt":"2026-08-14T19:37:18.893Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}