Comfy-Org/ComfyUI · error · ValueError

The new shape must be larger than the original tensor in all

Error message

The new shape must be larger than the original tensor in all dimensions

What it means

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.

Source

Thrown at comfy/lora.py:396


def pad_tensor_to_shape(tensor: torch.Tensor, new_shape: list[int]) -> torch.Tensor:
    """
    Pad a tensor to a new shape with zeros.

    Args:
        tensor (torch.Tensor): The original tensor to be padded.
        new_shape (List[int]): The desired shape of the padded tensor.

    Returns:
        torch.Tensor: A new tensor padded with zeros to the specified shape.

    Note:
        If the new shape is smaller than the original tensor in any dimension,
        the original tensor will be truncated in that dimension.
    """
    if any([new_shape[i] < tensor.shape[i] for i in range(len(new_shape))]):
        raise ValueError("The new shape must be larger than the original tensor in all dimensions")

    if len(new_shape) != len(tensor.shape):
        raise ValueError("The new shape must have the same number of dimensions as the original tensor")

    # Create a new tensor filled with zeros
    padded_tensor = torch.zeros(new_shape, dtype=tensor.dtype, device=tensor.device)

    # Create slicing tuples for both tensors
    orig_slices = tuple(slice(0, dim) for dim in tensor.shape)
    new_slices = tuple(slice(0, dim) for dim in tensor.shape)

    # Copy the original tensor into the new tensor
    padded_tensor[new_slices] = tensor[orig_slices]

    return padded_tensor

def calculate_shape(patches, weight, key, original_weights=None):
    current_shape = weight.shape

View on GitHub (pinned to 1c6d8d45b3)

Solutions

  1. Ensure new_shape >= tensor.shape in every dimension; the pad target must be the larger, model-side shape.
  2. Verify tensor.shape and new_shape before the call and fix the source of the mismatch (wrong LoRA for this base model).
  3. If you genuinely need shrink-to-fit, slice the tensor yourself — this API will not truncate.

Example fix

# before
padded = pad_tensor(lora_weight, model_weight.shape)  # model dim < lora dim
# after
padded = pad_tensor(lora_weight, max_shape)  # ensure all dims >= lora_weight.shape
Defensive patterns

Strategy: validation

Validate before calling

if any(n < t for n, t in zip(new_shape, tensor.shape)):
    raise ValueError(f"cannot pad {tuple(tensor.shape)} down to {tuple(new_shape)}; check base model / LoRA match")
out = pad_tensor(tensor, new_shape)

Type guard

def is_grow_only(tensor, new_shape) -> bool:
    return all(n >= t for n, t in zip(new_shape, tensor.shape))

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of Comfy-Org/ComfyUI@1c6d8d45b3 (2026-08-14). Data as JSON: /api/errors/294ef97ac33f294a. Report an issue: GitHub.