hpcaitech/Open-Sora · error · ValueError

Cannot call `set_default_attn_processor` when attention proc

Error message

Cannot call `set_default_attn_processor` when attention processors are of type {next(iter(self.attn_processors.values()))}

What it means

set_default_attn_processor restores the stock processor by checking whether ALL current processors are in ADDED_KV_ATTENTION_PROCESSORS or all in CROSS_ATTENTION_PROCESSORS. If the current processors are a mix, or of a custom/unknown class (e.g. fused or LoRA-patched processors not in either tuple), it refuses to guess and raises.

Source

Thrown at opensora/models/hunyuan_vae/autoencoder_kl_causal_3d.py:263

                    module.set_processor(processor.pop(f"{name}.processor"), _remove_lora=_remove_lora)

            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.unet_2d_condition.UNet2DConditionModel.set_default_attn_processor
    def set_default_attn_processor(self):
        """
        Disables custom attention processors and sets the default attention implementation.
        """
        if all(proc.__class__ in ADDED_KV_ATTENTION_PROCESSORS for proc in self.attn_processors.values()):
            processor = AttnAddedKVProcessor()
        elif all(proc.__class__ in CROSS_ATTENTION_PROCESSORS for proc in self.attn_processors.values()):
            processor = AttnProcessor()
        else:
            raise ValueError(
                f"Cannot call `set_default_attn_processor` when attention processors are of type {next(iter(self.attn_processors.values()))}"
            )

        self.set_attn_processor(processor, _remove_lora=True)

    @apply_forward_hook
    def encode(
        self,
        x: torch.FloatTensor,
        sample_posterior: bool = True,
        return_posterior: bool = False,
        generator: Optional[torch.Generator] = None,
    ) -> Union[torch.FloatTensor, Tuple[DiagonalGaussianDistribution]]:
        """
        Encode a batch of images/videos into latents.

        Args:
            x (`torch.FloatTensor`): Input batch of images/videos.

View on GitHub (pinned to 7ad6a96a13)

Solutions

  1. Manually set a concrete processor: model.set_attn_processor(AttnProcessor()) (or AttnAddedKVProcessor() as appropriate)
  2. Ensure all layers use one consistent processor family before calling set_default_attn_processor
  3. Unfuse/unload LoRA modifications first so processors are back in the known sets

Example fix

# before
model.set_default_attn_processor()
# after
from diffusers.models.attention_processor import AttnProcessor
model.set_attn_processor(AttnProcessor())
Defensive patterns

Strategy: fallback

Validate before calling

known = lambda p: all(proc.__class__ in ADDED_KV_ATTENTION_PROCESSORS or proc.__class__ in CROSS_ATTENTION_PROCESSORS for proc in model.attn_processors.values())

Try / catch

try:
    model.set_default_attn_processor()
except ValueError:
    model.set_attn_processor(AttnProcessor())

Prevention

When it happens

Trigger: Calling set_default_attn_processor after setting custom attention processor classes, or when a mixture of added-KV and cross-attention processors is installed, or after LoRA fusion left nonstandard processor types.

Common situations: Cleanup code after attention experiments (ring/context-parallel processors from distributed.py, LoRA processors) tries to reset to defaults without un-doing the custom classes first.

Related errors


AI-assisted analysis of hpcaitech/Open-Sora@7ad6a96a13 (2026-08-28). Data as JSON: /api/errors/0608f74ca74c2b35. Report an issue: GitHub.