{"record":{"id":"244c6170c6d7dc0d","repo":"sgl-project/sglang","slug":"a-dict-of-processors-was-passed-but-the-number-of-244c61","errorCode":null,"errorMessage":"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.","messagePattern":"A dict of processors was passed, but the number of processors (.+?) does not match the number of attention layers: (.+?)\\. Please make sure to pass (.+?) processor classes\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/runtime/models/vaes/autoencoder_kl_flux2.py","lineNumber":187,"sourceCode":"    def set_attn_processor(\n        self, processor: Union[AttentionProcessor, Dict[str, AttentionProcessor]]\n    ):\n        r\"\"\"\n        Sets the attention processor to use to compute attention.\n\n        Parameters:\n            processor (`dict` of `AttentionProcessor` or only `AttentionProcessor`):\n                The instantiated processor class or a dictionary of processor classes that will be set as the processor\n                for **all** `Attention` layers.\n\n                If `processor` is a dict, the key needs to define the path to the corresponding cross attention\n                processor. This is strongly recommended when setting trainable attention processors.\n\n        \"\"\"\n        count = len(self.attn_processors.keys())\n\n        if isinstance(processor, dict) and len(processor) != count:\n            raise ValueError(\n                f\"A dict of processors was passed, but the number of processors {len(processor)} does not match the\"\n                f\" number of attention layers: {count}. Please make sure to pass {count} processor classes.\"\n            )\n\n        def fn_recursive_attn_processor(name: str, module: torch.nn.Module, processor):\n            if hasattr(module, \"set_processor\"):\n                if not isinstance(processor, dict):\n                    module.set_processor(processor)\n                else:\n                    module.set_processor(processor.pop(f\"{name}.processor\"))\n\n            for sub_name, child in module.named_children():\n                fn_recursive_attn_processor(f\"{name}.{sub_name}\", child, processor)\n\n        for name, module in self.named_children():\n            fn_recursive_attn_processor(name, module, processor)\n\n    # Copied from diffusers.models.unets.unet_2d_condition.UNet2DConditionModel.set_default_attn_processor","sourceCodeStart":169,"sourceCodeEnd":205,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/runtime/models/vaes/autoencoder_kl_flux2.py#L169-L205","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass a single processor instance if uniform: set_attn_processor(AttnProcessor())","Rebuild the dict from the live model: {k: desired_proc for k in model.attn_processors}","Print len(model.attn_processors) and the dict keys to find the mismatch"],"exampleFix":"# before\nvae.set_attn_processor(my_procs_dict)  # wrong size\n# after\nvae.set_attn_processor({k: AttnProcessor() for k in vae.attn_processors})","handlingStrategy":"validation","validationCode":"if isinstance(procs, dict) and len(procs) != len(vae.attn_processors):\n    procs = {k: next(iter(procs.values())) for k in vae.attn_processors}\nvae.set_attn_processor(procs)","typeGuard":"def is_complete_processor_dict(model, procs) -> bool:\n    return not isinstance(procs, dict) or len(procs) == len(model.attn_processors)","tryCatchPattern":"try:\n    vae.set_attn_processor(procs)\nexcept ValueError as e:\n    if \"number of processors\" in str(e):\n        vae.set_attn_processor(AttnProcessor())\n    else:\n        raise","preventionTips":["Build processor dicts from the live model's attn_processors keys","Log len(model.attn_processors) when porting configs between VAE variants"],"tags":["attention","processor","flux2","value-error"],"backgroundTag":"dict-length-mismatch","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}