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

When you pass a dict to set_attn_processor, its keys must cover every attention layer in the autoencoder exactly. The method counts registered processors via self.attn_processors and rejects a dict whose length differs, because some layers would be left without a processor.

Source

Thrown at python/sglang/multimodal_gen/runtime/models/vaes/autoencoder.py:236

    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 instead of a dict if all layers should use the same processor: set_attn_processor(AttnProcessor())
  2. If per-layer processors are needed, build the dict from self.attn_processors.keys(): dict(self.attn_processors) then modify values
  3. Print list(model.attn_processors.keys()) to get the exact layer names/count your dict must match

Example fix

# before
vae.set_attn_processor({"mid_block.attn1": AttnProcessor()})
# after
vae.set_attn_processor(AttnProcessor())
# or per-layer:
procs = {k: AttnProcessor() for k in vae.attn_processors}
vae.set_attn_processor(procs)
Defensive patterns

Strategy: validation

Validate before calling

if isinstance(procs, dict):
    assert set(procs) == set(vae.attn_processors), (
        f"need {len(vae.attn_processors)} processors, got {len(procs)}")
vae.set_attn_processor(procs)

Type guard

def is_complete_processor_dict(model, procs) -> bool:
    return isinstance(procs, dict) and 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(next(iter(procs.values())))  # uniform fallback
    else:
        raise

Prevention

When it happens

Trigger: Calling autoencoder.set_attn_processor({'block_0': AttnProcessor()}) when the model has more attention layers than dict entries, or passing a dict with extra entries. Also reached indirectly via set_default_attn_processor or fuse_qkv_projections.

Common situations: Porting a diffusers-style snippet that hardcodes processor names from a different architecture; adding/removing layers in a config without regenerating the processor dict; assuming a single processor instance is enough while passing it wrapped in a dict.

Related errors


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