Comfy-Org/ComfyUI · error · ValueError

guide pre_filter_counts ({total_pfc}) != keyframe grid mask

Error message

guide pre_filter_counts ({total_pfc}) != keyframe grid mask length ({len(kf_grid_mask)})

What it means

After the keyframe grid mask is sliced, the sum of each guide entry's recorded pre_filter_count must exactly equal the sliced mask length. If it does not, guide_attention_entries were built against a different token layout than the current mask, and per-guide slice boundaries would be wrong.

Source

Thrown at comfy/ldm/lightricks/model.py:1145

                    "upscaling the latent."
                )
            additional_args.update({ "orig_patchified_shape": list(x.shape)})
            denoise_mask = self.patchifier.patchify(denoise_mask)[0]
            grid_mask = ~torch.any(denoise_mask < 0, dim=-1)[0]
            additional_args.update({"grid_mask": grid_mask})
            x = x[:, grid_mask, :]
            pixel_coords = pixel_coords[:, :, grid_mask, ...]

            kf_grid_mask = grid_mask[-keyframe_idxs.shape[2]:]

            # Compute per-guide surviving token counts from guide_attention_entries.
            # Each entry tracks one guide reference; they are appended in order and
            # their pre_filter_counts partition the kf_grid_mask.
            guide_entries = kwargs.get("guide_attention_entries", None)
            if guide_entries:
                total_pfc = sum(e["pre_filter_count"] for e in guide_entries)
                if total_pfc != len(kf_grid_mask):
                    raise ValueError(
                        f"guide pre_filter_counts ({total_pfc}) != "
                        f"keyframe grid mask length ({len(kf_grid_mask)})"
                    )
                resolved_entries = []
                offset = 0
                for entry in guide_entries:
                    pfc = entry["pre_filter_count"]
                    entry_mask = kf_grid_mask[offset:offset + pfc]
                    surviving = int(entry_mask.sum().item())
                    resolved_entries.append({
                        **entry,
                        "surviving_count": surviving,
                    })
                    offset += pfc
                additional_args["resolved_guide_entries"] = resolved_entries

            keyframe_idxs = keyframe_idxs[..., kf_grid_mask, :]

View on GitHub (pinned to 1c6d8d45b3)

Solutions

  1. Recompute guide_attention_entries in the same pass that rebuilds keyframe_idxs and grid_mask
  2. Ensure sum(pre_filter_count) == keyframe_idxs.shape[2] before calling forward
  3. Separate stale guide conditioning before changing masks or resolutions
Defensive patterns

Strategy: validation

Validate before calling

total = sum(e['pre_filter_count'] for e in guide_entries)
assert total == keyframe_idxs.shape[2], (total, keyframe_idxs.shape[2])

Prevention

When it happens

Trigger: Passing guide_attention_entries whose pre_filter_counts cover more/fewer tokens than keyframe_idxs.shape[2] surviving after grid_mask filtering; e.g. entries recorded before a mask change or a resolution change in the same pass as error 174.

Common situations: Reusing guide entries cached from a prior step while the denoise/grid mask evolved, or mixing guides appended at different resolutions.

Related errors


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