{"record":{"id":"dd4ff635532b3bde","repo":"sgl-project/sglang","slug":"sd3-clip-postprocessing-requires-hidden-states-fro","errorCode":null,"errorMessage":"SD3 CLIP postprocessing requires hidden_states from encoder output.","messagePattern":"SD3 CLIP postprocessing requires hidden_states from encoder output\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/configs/pipeline_configs/stablediffusion3.py","lineNumber":34,"sourceCode":"    CLIPTextConfig,\n)\nfrom sglang.multimodal_gen.configs.models.encoders.t5 import (\n    T5ArchConfig,\n    T5Config,\n)\nfrom sglang.multimodal_gen.configs.models.vaes.stablediffusion3 import (\n    StableDiffusion3VAEConfig,\n)\nfrom sglang.multimodal_gen.configs.pipeline_configs.base import (\n    ModelTaskType,\n    SpatialImagePipelineConfig,\n)\n\n\ndef sd3_clip_postprocess_text(outputs: BaseEncoderOutput, _text_inputs) -> torch.Tensor:\n    \"\"\"Extract pre-final hidden state for SD3 CLIP encoders.\"\"\"\n    if outputs.hidden_states is None:\n        raise ValueError(\n            \"SD3 CLIP postprocessing requires hidden_states from encoder output.\"\n        )\n    return outputs.hidden_states[-2]\n\n\ndef t5_postprocess_text(outputs: BaseEncoderOutput, _text_inputs) -> torch.Tensor:\n    return outputs.last_hidden_state\n\n\ndef select_sd3_vae_weight_files(\n    safetensors_list: list[str],\n    component_model_path: str,\n    component_name: str,\n    vae_precision: str,\n) -> list[str]:\n    \"\"\"Select SD3 VAE checkpoint file candidates with minimal policy.\"\"\"\n    if component_name not in (\"vae\", \"video_vae\"):\n        return safetensors_list","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/configs/pipeline_configs/stablediffusion3.py#L16-L52","documentation":"SD3 CLIP text postprocessing extracts the penultimate hidden_states layer (hidden_states[-2]) as the pre-final representation for Stable Diffusion 3. If the encoder was run with output_hidden_states=False (or a path that doesn't populate them), hidden_states is None and this ValueError fires.","triggerScenarios":"Calling sd3_clip_postprocess_text with an encoder output produced while output_hidden_states=False; swapping in a custom CLIP encoder wrapper that returns only pooler_output/last_hidden_state; transformers version behavior change where the flag isn't propagated.","commonSituations":"Upgrading transformers where hidden-state emission depends on an explicit flag; building a custom CLIPTextModel forward that discards hidden_states; memory optimizations that disabled hidden-state collection.","solutions":["Re-run the CLIP encoder with output_hidden_states=True so outputs.hidden_states is populated","If using a custom forward, return hidden_states from all layers (tuple), not just the last","Pin/verify the transformers version and pass the flag through your encode helper"],"exampleFix":"# before\noutputs = clip_encoder(input_ids, attention_mask=mask)  # hidden_states=None\nemb = sd3_clip_postprocess_text(outputs, text_inputs)  # error\n\n# after\noutputs = clip_encoder(input_ids, attention_mask=mask, output_hidden_states=True)\nemb = sd3_clip_postprocess_text(outputs, text_inputs)","handlingStrategy":"validation","validationCode":"outputs = clip_encoder(input_ids, attention_mask=mask, output_hidden_states=True)\nassert outputs.hidden_states is not None and len(outputs.hidden_states) >= 2","typeGuard":"def has_hidden_states(outputs) -> bool:\n    return outputs.hidden_states is not None and len(outputs.hidden_states) >= 2","tryCatchPattern":"try:\n    emb = sd3_clip_postprocess_text(outputs, text_inputs)\nexcept ValueError:\n    outputs = clip_encoder(input_ids, attention_mask=mask, output_hidden_states=True)\n    emb = sd3_clip_postprocess_text(outputs, text_inputs)","preventionTips":["Always pass output_hidden_states=True when encoding for SD3","Wrap custom encoders to return full hidden_states tuples","Pin transformers version and test after upgrades"],"tags":["stable-diffusion-3","clip","text-encoding","hidden-states"],"backgroundTag":"missing-hidden-states","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}