sgl-project/sglang · error · ValueError

A dict of processors was passed, but the number of processor

Error message

A dict of processors was passed, but the number of processors {len(processor)} does not match the number of attention layers: {count}. Please make sure to pass {count} processor classes.

What it means

The Flux2 KL autoencoder's set_attn_processor requires that a dict of processors have exactly as many entries as the model has attention layers (counted via self.attn_processors). A mismatched dict would leave layers unconfigured, so it is rejected up front.

Source

Thrown at python/sglang/multimodal_gen/runtime/models/vaes/autoencoder_kl_flux2.py:187

    def set_attn_processor(
        self, processor: Union[AttentionProcessor, Dict[str, AttentionProcessor]]
    ):
        r"""
        Sets the attention processor to use to compute attention.

        Parameters:
            processor (`dict` of `AttentionProcessor` or only `AttentionProcessor`):
                The instantiated processor class or a dictionary of processor classes that will be set as the processor
                for **all** `Attention` layers.

                If `processor` is a dict, the key needs to define the path to the corresponding cross attention
                processor. This is strongly recommended when setting trainable attention processors.

        """
        count = len(self.attn_processors.keys())

        if isinstance(processor, dict) and len(processor) != count:
            raise ValueError(
                f"A dict of processors was passed, but the number of processors {len(processor)} does not match the"
                f" number of attention layers: {count}. Please make sure to pass {count} processor classes."
            )

        def fn_recursive_attn_processor(name: str, module: torch.nn.Module, processor):
            if hasattr(module, "set_processor"):
                if not isinstance(processor, dict):
                    module.set_processor(processor)
                else:
                    module.set_processor(processor.pop(f"{name}.processor"))

            for sub_name, child in module.named_children():
                fn_recursive_attn_processor(f"{name}.{sub_name}", child, processor)

        for name, module in self.named_children():
            fn_recursive_attn_processor(name, module, processor)

    # Copied from diffusers.models.unets.unet_2d_condition.UNet2DConditionModel.set_default_attn_processor

View on GitHub (pinned to 0132848349)

Solutions

  1. Pass a single processor instance if uniform: set_attn_processor(AttnProcessor())
  2. Rebuild the dict from the live model: {k: desired_proc for k in model.attn_processors}
  3. Print len(model.attn_processors) and the dict keys to find the mismatch

Example fix

# before
vae.set_attn_processor(my_procs_dict)  # wrong size
# after
vae.set_attn_processor({k: AttnProcessor() for k in vae.attn_processors})
Defensive patterns

Strategy: validation

Validate before calling

if isinstance(procs, dict) and len(procs) != len(vae.attn_processors):
    procs = {k: next(iter(procs.values())) for k in vae.attn_processors}
vae.set_attn_processor(procs)

Type guard

def is_complete_processor_dict(model, procs) -> bool:
    return not isinstance(procs, dict) or len(procs) == len(model.attn_processors)

Try / catch

try:
    vae.set_attn_processor(procs)
except ValueError as e:
    if "number of processors" in str(e):
        vae.set_attn_processor(AttnProcessor())
    else:
        raise

Prevention

When it happens

Trigger: Calling set_attn_processor with a dict built for a different number of layers, e.g. reusing a processor-name dict from another VAE config, or passing {name: proc} for only a subset of layers. Also triggered via set_default_attn_processor.

Common situations: Copying diffusers processor-mapping snippets across architectures; per-layer mixed-precision or LoRA-style processor assignment written against an older layer count; config change altering depth and invalidating hardcoded names.

Related errors


AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28). Data as JSON: /api/errors/244c6170c6d7dc0d. Report an issue: GitHub.