sgl-project/sglang · error · AttributeError

Subclasses of BaseDiT must define '{attr}' instance variable

Error message

Subclasses of BaseDiT must define '{attr}' instance variable

What it means

BaseDiT.__post_init__ verifies that the model instance exposes hidden_size, num_attention_heads, and num_channels_latents. These are expected to be set during __init__/setup of the subclass (often from config.arch_config); if absent, the model is incompletely configured and the AttributeError is raised.

Source

Thrown at python/sglang/multimodal_gen/runtime/models/dits/base.py:97

            )

    @abstractmethod
    def forward(
        self,
        hidden_states: torch.Tensor,
        encoder_hidden_states: torch.Tensor | list[torch.Tensor],
        timestep: torch.LongTensor,
        encoder_hidden_states_image: torch.Tensor | list[torch.Tensor] | None = None,
        guidance=None,
        **kwargs,
    ) -> torch.Tensor:
        pass

    def __post_init__(self) -> None:
        required_attrs = ["hidden_size", "num_attention_heads", "num_channels_latents"]
        for attr in required_attrs:
            if not hasattr(self, attr):
                raise AttributeError(
                    f"Subclasses of BaseDiT must define '{attr}' instance variable"
                )

    def post_load_weights(self) -> None:
        """Run model-specific post-load weight fixups after all parameters are materialized."""
        return None

    def prepare_lora_adapter(
        self, adapter: dict[str, torch.Tensor]
    ) -> dict[str, torch.Tensor]:
        """Apply model-specific LoRA transforms after names are normalized."""
        return adapter

    @property
    def supported_attention_backends(self) -> set[AttentionBackendEnum]:
        return self._supported_attention_backends

    @property

View on GitHub (pinned to 0132848349)

Solutions

  1. In the subclass __init__, assign the three attributes from the model config, e.g. self.hidden_size = config.arch_config.hidden_size
  2. If config field names differ, map them explicitly (self.hidden_size = cfg.dim)
  3. Run the model's unit test after adding to catch the failure early

Example fix

# before
class MyDiT(BaseDiT):
    def __init__(self, config, hf_config, **kw):
        super().__init__(config, hf_config, **kw)
        self.dim = 1024
# after
class MyDiT(BaseDiT):
    def __init__(self, config, hf_config, **kw):
        super().__init__(config, hf_config, **kw)
        self.hidden_size = 1024
        self.num_attention_heads = 16
        self.num_channels_latents = 16
Defensive patterns

Strategy: validation

Validate before calling

REQUIRED_INSTANCE = ["hidden_size", "num_attention_heads", "num_channels_latents"]
assert all(hasattr(model, a) for a in REQUIRED_INSTANCE)

Type guard

def is_configured_dit(model) -> bool:
    return all(hasattr(model, a) for a in ("hidden_size", "num_attention_heads", "num_channels_latents"))

Prevention

When it happens

Trigger: A BaseDiT subclass completes construction without assigning self.hidden_size, self.num_attention_heads, or self.num_channels_latents — e.g. a custom model whose __init__ skips copying these fields from its config.

Common situations: Porting a new architecture where the config field names differ (e.g. dim vs hidden_size), so the subclass never assigns the expected attribute names; refactors that move attribute assignment out of __init__.

Related errors


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