sgl-project/sglang · 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 on the Flux2 autoencoder chooses a default processor class by inspecting the currently installed processors. If they are neither recognized attention processors nor cross-attention processors (e.g. fused or custom classes), it cannot determine the correct default and raises, naming the offending class.

Source

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

            fn_recursive_attn_processor(name, module, processor)

    # Copied from diffusers.models.unets.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)

    def _encode(self, x: torch.Tensor) -> torch.Tensor:
        batch_size, num_channels, height, width = x.shape

        if self.use_tiling and (
            width > self.tile_sample_min_size or height > self.tile_sample_min_size
        ):
            return self._tiled_encode(x)

        enc = self.encoder(x)
        if self.quant_conv is not None:
            enc = self.quant_conv(enc)

        return enc

View on GitHub (pinned to 0132848349)

Solutions

  1. Restore explicitly: model.set_attn_processor(model.original_attn_processors) if fusion saved them, or set_attn_processor(AttnProcessor()) directly
  2. Match library versions between where processors were set and where you reset them
  3. Register/alias your custom processor class in the recognized processor sets if it is semantically compatible

Example fix

# before
model.set_default_attn_processor()
# after
model.set_attn_processor(AttnProcessor())
Defensive patterns

Strategy: fallback

Validate before calling

# snapshot before changing processors
snapshot = dict(model.attn_processors)
# to restore later: model.set_attn_processor(snapshot)

Try / catch

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

Prevention

When it happens

Trigger: Calling set_default_attn_processor() while fused or third-party (Flash/XFormers/custom) processors are installed, or after a version change moved processor classes out of the recognized sets.

Common situations: Attempting to reset state after experiments with custom processors; library upgrade changing class identity so `proc.__class__ in CROSS_ATTENTION_PROCESSORS` fails; mixing diffusers processor classes with local re-implementations.

Related errors


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