Comfy-Org/ComfyUI · error · ValueError

Unknown x_format {self.blocks[0].x_format}

Error message

Unknown x_format {self.blocks[0].x_format}

What it means

After per-block processing, the Cosmos forward reshapes x back according to the x_format attribute of block0, which must be either 'THWBD' or 'BTHWD'. Any other string raises ValueError. The value is set by the block classes themselves, so this error indicates a custom/incompatible block implementation was injected into self.blocks.

Source

Thrown at comfy/ldm/cosmos/model.py:410

            crossattn_mask = crossattn_mask[:, None, None, :]  # .to(dtype=torch.bool)  # [B, 1, 1, length]
        else:
            crossattn_mask = None

        if self.blocks["block0"].x_format == "THWBD":
            x = rearrange(x_B_T_H_W_D, "B T H W D -> T H W B D")
            if extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D is not None:
                extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D = rearrange(
                    extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D, "B T H W D -> T H W B D"
                )
            crossattn_emb = rearrange(crossattn_emb, "B M D -> M B D")

            if crossattn_mask:
                crossattn_mask = rearrange(crossattn_mask, "B M -> M B")

        elif self.blocks["block0"].x_format == "BTHWD":
            x = x_B_T_H_W_D
        else:
            raise ValueError(f"Unknown x_format {self.blocks[0].x_format}")
        output = {
            "x": x,
            "affline_emb_B_D": affline_emb_B_D,
            "crossattn_emb": crossattn_emb,
            "crossattn_mask": crossattn_mask,
            "rope_emb_L_1_1_D": rope_emb_L_1_1_D,
            "adaln_lora_B_3D": adaln_lora_B_3D,
            "original_shape": original_shape,
            "extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D": extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D,
        }
        return output

    def forward(
        self,
        x: torch.Tensor,
        timesteps: torch.Tensor,
        context: torch.Tensor,
        attention_mask: Optional[torch.Tensor] = None,

View on GitHub (pinned to 1c6d8d45b3)

Solutions

  1. Ensure every block in self.blocks uses x_format 'THWBD' or 'BTHWD' consistently with block0.
  2. If you introduce a new layout in a custom block, also add a matching branch in the forward() reshape dispatch.
  3. Use ComfyUI's model patcher hooks instead of swapping block classes, so stock block implementations are preserved.

Example fix

# before (custom block)
class MyBlock(CrossAttention): x_format = "BTDHW"

# after
class MyBlock(CrossAttention): x_format = "BTHWD"  # keep a layout the dispatch knows
Defensive patterns

Strategy: validation

Validate before calling

fmt = model.blocks["block0"].x_format
assert fmt in ("THWBD", "BTHWD"), f"block x_format {fmt!r} unsupported by Cosmos forward"

Type guard

def blocks_use_known_layout(model) -> bool:
    return model.blocks["block0"].x_format in ("THWBD", "BTHWD")

Prevention

When it happens

Trigger: Replacing the transformer blocks list with custom blocks whose x_format attribute is neither 'THWBD' nor 'BTHWD' (e.g. a ported block using a different layout label), then running forward(). Note the format check reads self.blocks["block0"].x_format while the error message formats self.blocks[0].x_format — a custom dict-like blocks object without integer indexing would itself fail first.

Common situations: Custom nodes that patch or replace Cosmos blocks for attention modifications; forks that add a new layout variant without updating this dispatch; inconsistent block sets where block0 differs from later blocks.

Related errors


AI-assisted analysis of Comfy-Org/ComfyUI@1c6d8d45b3 (2026-08-14). Data as JSON: /api/errors/74b341fe33e130f1. Report an issue: GitHub.