{"record":{"id":"32818c411b7d2704","repo":"sgl-project/sglang","slug":"unexpected-sd2-mid-block-type-mid-block-name","errorCode":null,"errorMessage":"Unexpected SD2 mid block: {type(mid_block).__name__}.","messagePattern":"Unexpected SD2 mid block: (.+?)\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/runtime/models/dits/hunyuan3d_paint.py","lineNumber":187,"sourceCode":"                f\"Expected BasicTransformerBlock, got {type(transformer).__name__}.\"\n            )\n        model.transformer_blocks[0] = Hunyuan3DPaintTransformerBlock(\n            transformer,\n            layer_name,\n            use_multiview_attention=use_multiview_attention,\n            use_reference_attention=use_reference_attention,\n            is_turbo=is_turbo,\n        )\n\n    for block_index, block in enumerate(unet.down_blocks):\n        if not isinstance(block, CrossAttnDownBlock2D):\n            continue\n        for attention_index, attention in enumerate(block.attentions):\n            replace(attention, f\"down_{block_index}_{attention_index}_0\")\n\n    mid_block = unet.mid_block\n    if not isinstance(mid_block, UNetMidBlock2DCrossAttn):\n        raise TypeError(f\"Unexpected SD2 mid block: {type(mid_block).__name__}.\")\n    replace(mid_block.attentions[0], \"mid_0_0\")\n\n    for block_index, block in enumerate(unet.up_blocks):\n        if not isinstance(block, CrossAttnUpBlock2D):\n            continue\n        for attention_index, attention in enumerate(block.attentions):\n            replace(attention, f\"up_{block_index}_{attention_index}_0\")\n\n\n@torch.no_grad()\ndef compute_voxel_grid_mask(\n    position: torch.Tensor, grid_resolution: int = 8\n) -> torch.Tensor:\n    position = position.half()\n    _, _, _, height, width = position.shape\n    if height % grid_resolution != 0 or width % grid_resolution != 0:\n        raise ValueError(\n            f\"Position map {height}x{width} is not divisible by {grid_resolution}.\"","sourceCodeStart":169,"sourceCodeEnd":205,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/runtime/models/dits/hunyuan3d_paint.py#L169-L205","documentation":"_replace_transformer_blocks walks the SD2 UNet expecting unet.mid_block to be a UNetMidBlock2DCrossAttn so it can patch its first attention block (layer 'mid_0_0'). If the mid block is any other type, this TypeError is raised before patching.","triggerScenarios":"Calling _replace_transformer_blocks on a UNet whose mid_block is not UNetMidBlock2DCrossAttn — e.g. a UNet with a different mid-block config, a custom architecture, or a non-SD2 checkpoint.","commonSituations":"Loading a SD1/SDXL or custom UNet into the Hunyuan3D Paint pipeline; diffusers version differences in mid-block classes.","solutions":["Load the Stable Diffusion 2.x UNet this pipeline expects","Pin the diffusers version listed in the repo requirements","If using a custom UNet, add support for its mid-block type before calling this function"],"exampleFix":"# before\n_replace_transformer_blocks(sd1_unet, ...)  # mid block type differs\n\n# after\nunet = UNet2DConditionModel.from_pretrained(\"stabilityai/stable-diffusion-2-1\", subfolder=\"unet\")\n_replace_transformer_blocks(unet, ...)","handlingStrategy":"type-guard","validationCode":"from diffusers.models.unets.unet_2d_blocks import UNetMidBlock2DCrossAttn\nassert isinstance(unet.mid_block, UNetMidBlock2DCrossAttn), type(unet.mid_block).__name__","typeGuard":"def is_sd2_mid_block(unet) -> bool:\n    from diffusers.models.unets.unet_2d_blocks import UNetMidBlock2DCrossAttn\n    return isinstance(unet.mid_block, UNetMidBlock2DCrossAttn)","tryCatchPattern":null,"preventionTips":["Load the exact SD2.x UNet checkpoint the pipeline declares","Pin diffusers to avoid class-layout drift","Validate UNet structure right after from_pretrained"],"tags":["initialization","diffusers","type-mismatch"],"backgroundTag":"unexpected-block-type","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}