{"record":{"id":"99c06031b73d7e69","repo":"hpcaitech/Open-Sora","slug":"a-dict-of-processors-was-passed-but-the-number-of","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":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"opensora/models/hunyuan_vae/autoencoder_kl_causal_3d.py","lineNumber":235,"sourceCode":"    def set_attn_processor(\n        self, processor: Union[AttentionProcessor, Dict[str, AttentionProcessor]], _remove_lora=False\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, _remove_lora=_remove_lora)\n                else:\n                    module.set_processor(processor.pop(f\"{name}.processor\"), _remove_lora=_remove_lora)\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.unet_2d_condition.UNet2DConditionModel.set_default_attn_processor","sourceCodeStart":217,"sourceCodeEnd":253,"githubUrl":"https://github.com/hpcaitech/Open-Sora/blob/7ad6a96a135feb81f755c84fb391818718f6beb2/opensora/models/hunyuan_vae/autoencoder_kl_causal_3d.py#L217-L253","documentation":"set_attn_processor on the Hunyuan causal 3D autoencoder requires that when you pass a dict of processors, its length exactly equals the number of attention layers found by recursively walking the model (len(self.attn_processors)). The guard ensures a 1:1 mapping between layer names and processors.","triggerScenarios":"Calling model.set_attn_processor({...}) where the dict has fewer/more entries than attention layers, e.g. reusing a processor state dict from a different model variant or constructing a partial dict for only some layers.","commonSituations":"Porting attention processors or LoRA weights between model checkpoints with different layer counts; hand-building a processor dict and losing count; diffusers-version code copied over where counts differed.","solutions":["Get the exact expected keys and count via model.attn_processors and build your dict with the same keys","If you meant to apply one processor to all layers, pass a single processor instance instead of a dict","If loading from a checkpoint, verify the checkpoint matches this model architecture (layer count)"],"exampleFix":"# before\nmodel.set_attn_processor({name: AttnProcessor() for name in [\"some\", \"names\"]})\n# after\nmodel.set_attn_processor({name: AttnProcessor() for name in model.attn_processors})","handlingStrategy":"validation","validationCode":"procs = model.attn_processors\nassert len(processor_dict) == len(procs), f\"{len(processor_dict)} vs {len(procs)}\"\nassert set(processor_dict) == set(procs), \"processor keys must match layer names\"","typeGuard":"def is_valid_processor_dict(model, d) -> bool:\n    return isinstance(d, dict) and set(d) == set(model.attn_processors)","tryCatchPattern":"try:\n    model.set_attn_processor(proc_dict)\nexcept ValueError as e:\n    if \"number of processors\" in str(e):\n        model.set_attn_processor(AttnProcessor())  # uniform fallback\n    else:\n        raise","preventionTips":["Always derive processor dict keys from model.attn_processors","Pass a single instance when applying one processor to all layers","Verify checkpoint architecture matches before porting processor dicts"],"tags":["diffusers","attention","vae","config-mismatch"],"backgroundTag":"processor-count-mismatch","analyzedSha":"7ad6a96a135feb81f755c84fb391818718f6beb2","analyzedAt":"2026-08-28T16:58:37.171Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}