Comfy-Org/ComfyUI · error · ValueError

len(args) should be 0, 1 or 2, but got

Error message

len(args) should be 0, 1 or 2, but got {len(args)}

What it means

Raised by the HyDiT _to_tuple_range helper used to build 2D positional-embedding grids: it accepts 0, 1, or 2 positional range arguments (start-only, start+stop, start+stop+num) and rejects anything else. It guards internal grid math, not user input.

Solutions

  1. Call the helper with at most (stop) or (stop, num) positional args alongside start
  2. Prefer the public calc_sizes/init_image_posemb wrappers instead of the low-level grid function
  3. Check the function signature of the vendored posemb_layers version you are calling

Example fix

# before
grid = get_2d_rotary_pos_embed(dim, (start, stop, num))  # 3 args -> ValueError
# after
grid = get_2d_rotary_pos_embed(dim, stop, num)
Defensive patterns

Strategy: type-guard

Validate before calling

assert len(args) <= 2, f"too many range args: {args}"

Type guard

def valid_posemb_args(start, args) -> bool:
    return len(args) in (0, 1, 2)

Prevention

When it happens

Trigger: Internal callers pass the wrong arity when computing get_fill_resize_and_crop sub_args for 'base' rope_img modes; externally reachable only through patched/misused code paths calling the helper directly with 3+ args.

Common situations: Monkey-patching or reusing posemb helpers with signatures from a different upstream version, or hand-rolling rope calls with extra arguments.

Related errors


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

Appendix: source

Thrown at comfy/ldm/hydit/posemb_layers.py:51

def get_meshgrid(start, *args):
    if len(args) == 0:
        # start is grid_size
        num = _to_tuple(start)
        start = (0, 0)
        stop = num
    elif len(args) == 1:
        # start is start, args[0] is stop, step is 1
        start = _to_tuple(start)
        stop = _to_tuple(args[0])
        num = (stop[0] - start[0], stop[1] - start[1])
    elif len(args) == 2:
        # start is start, args[0] is stop, args[1] is num
        start = _to_tuple(start)
        stop = _to_tuple(args[0])
        num = _to_tuple(args[1])
    else:
        raise ValueError(f"len(args) should be 0, 1 or 2, but got {len(args)}")

    grid_h = np.linspace(start[0], stop[0], num[0], endpoint=False, dtype=np.float32)
    grid_w = np.linspace(start[1], stop[1], num[1], endpoint=False, dtype=np.float32)
    grid = np.meshgrid(grid_w, grid_h)  # here w goes first
    grid = np.stack(grid, axis=0)   # [2, W, H]
    return grid

#################################################################################
#                   Sine/Cosine Positional Embedding Functions                  #
#################################################################################
# https://github.com/facebookresearch/mae/blob/main/util/pos_embed.py

def get_2d_sincos_pos_embed(embed_dim, start, *args, cls_token=False, extra_tokens=0):
    """
    grid_size: int of the grid height and width
    return:
    pos_embed: [grid_size*grid_size, embed_dim] or [1+grid_size*grid_size, embed_dim] (w/ or w/o cls_token)
    """

View on GitHub (pinned to 1c6d8d45b3)