{"record":{"id":"051a81358ff8c089","repo":"huggingface/candle","slug":"expected-some-cross-attn","errorCode":null,"errorMessage":"expected some cross-attn","messagePattern":"expected some cross-attn","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-transformers/src/models/blip_text.rs","lineNumber":295,"sourceCode":"    }\n\n    fn reset_kv_cache(&mut self) {\n        self.attention.reset_kv_cache();\n        if let Some(ca) = &mut self.cross_attention {\n            ca.reset_kv_cache()\n        }\n    }\n\n    fn forward(\n        &mut self,\n        xs: &Tensor,\n        encoder_hidden_states: &Tensor,\n        attention_mask: &Tensor,\n    ) -> Result<Tensor> {\n        let attention_output = self.attention.forward(xs, None, Some(attention_mask))?;\n        let attention_output = match &mut self.cross_attention {\n            Some(ca) => ca.forward(&attention_output, Some(encoder_hidden_states), None)?,\n            None => candle::bail!(\"expected some cross-attn\"),\n        };\n        let intermediate_output = self.intermediate.forward(&attention_output)?;\n        self.output.forward(&intermediate_output, &attention_output)\n    }\n}\n\n#[derive(Debug, Clone)]\nstruct TextEncoder {\n    layers: Vec<TextLayer>,\n}\n\nimpl TextEncoder {\n    fn new(cfg: &Config, vb: VarBuilder) -> Result<Self> {\n        let vb = vb.pp(\"layer\");\n        let mut layers = Vec::with_capacity(cfg.num_hidden_layers);\n        for i in 0..cfg.num_hidden_layers {\n            let layer = TextLayer::new(cfg, vb.pp(i))?;\n            layers.push(layer)","sourceCodeStart":277,"sourceCodeEnd":313,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-transformers/src/models/blip_text.rs#L277-L313","documentation":"In blip_text's BertEncoderLayer forward, cross-attention is optional; this error is thrown when a layer configured without cross_attention is asked to process encoder_hidden_states, which requires cross-attention. It indicates the BLIP text model was built from a config without cross-attention layers while the forward path needs them.","triggerScenarios":"Calling forward with encoder_hidden_states on a BertEncoderLayer whose cross_attention field is None, typically when the loaded config/checkpoint lacks cross-attention weights.","commonSituations":"Loading an image-text retrieval checkpoint (no cross-attention) and then running image-text matching (which needs it), or mismatched config between the BLIP variant and the task.","solutions":["Load a BLIP text checkpoint/config that includes cross-attention weights","Ensure the model is constructed with cross_attention: Some(...) for the matching task","Use the correct task API: retrieval-only models don't take encoder_hidden_states","Verify config.json / weight names contain cross-attention layers"],"exampleFix":"// before\nlet text_model = BertTextModel::load(vb, config)?; // config without x-attention\nlet out = layer.forward(&xs, &encoder_hidden_states, &mask)?;\n// after\nlet mut config = config.clone();\nconfig.add_cross_attention = true; // then reload weights\nlet text_model = BertTextModel::load(vb, &config)?;","handlingStrategy":"validation","validationCode":"assert!(config.add_cross_attention, \"task requires cross-attention; config lacks it\");","typeGuard":"fn has_cross_attention(layer: &BertEncoderLayer) -> bool {\n    layer.cross_attention.is_some()\n}","tryCatchPattern":"match layer.forward(&xs, &encoder_hidden_states, &mask) {\n    Ok(v) => v,\n    Err(e) if e.to_string().contains(\"expected some cross-attn\") => {\n        return Err(anyhow!(\"reload model with cross-attention enabled\"));\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Match the checkpoint variant to the task: matching tasks need cross-attention","Verify config.json sets add_cross_attention for image-text matching models","Check checkpoint weight names include cross-attention layers before running","Use retrieval-only APIs for retrieval-only checkpoints"],"tags":["rust","candle","blip","cross-attention","missing-component"],"backgroundTag":"missing-cross-attention-layer","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}