{"record":{"id":"aff9252b9bd34f3e","repo":"huggingface/candle","slug":"only-blocks-found","errorCode":null,"errorMessage":"only {} / {} blocks found","messagePattern":"only (.+?) / (.+?) blocks found","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-transformers/src/models/beit.rs","lineNumber":328,"sourceCode":"        let xs = self.patch_embed.forward(xs)?;\n        Tensor::cat(&[&self.cls_token, &xs], 1)\n    }\n\n    fn get_intermediate_layers_not_chunked(\n        &self,\n        xs: &Tensor,\n        blocks_to_take: &[usize],\n    ) -> Result<Vec<Tensor>> {\n        let mut xs = self.prepare_tokens_with_mask(xs)?;\n        let mut output = Vec::new();\n        for (i, blk) in self.blocks.iter().enumerate() {\n            xs = blk.forward(&xs)?;\n            if blocks_to_take.contains(&i) {\n                output.push(xs.clone());\n            }\n        }\n        if output.len() != blocks_to_take.len() {\n            candle::bail!(\n                \"only {} / {} blocks found\",\n                output.len(),\n                blocks_to_take.len()\n            );\n        }\n        Ok(output)\n    }\n\n    pub fn get_intermediate_layers(\n        &self,\n        xs: &Tensor,\n        blocks_to_take: &[usize],\n        reshape: bool,\n        return_class_token: bool,\n        norm: bool,\n    ) -> Result<Tensor> {\n        let outputs = self.get_intermediate_layers_not_chunked(xs, blocks_to_take)?;\n        let outputs = if norm {","sourceCodeStart":310,"sourceCodeEnd":346,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-transformers/src/models/beit.rs#L310-L346","documentation":"BeiT's get_intermediate_layers_not_chunked collects outputs of selected transformer blocks indexed by blocks_to_take. After iterating all blocks it verifies it collected as many outputs as requested; if not (e.g. a requested block index does not exist in the model), it fails with the count of found vs requested blocks.","triggerScenarios":"Calling get_intermediate_layers with block indices >= the number of transformer blocks in the model, or duplicate/invalid indices in blocks_to_take.","commonSituations":"Copying block indices from a differently sized variant (e.g. ViT-L indices used on a ViT-B checkpoint), or off-by-one indices assuming 1-based numbering.","solutions":["Check the model's number of blocks and use indices in 0..n_blocks","Use block indices matching the loaded checkpoint's depth","If you want all blocks, pass the full 0..n range instead of hand-picked indices"],"exampleFix":"// before\nlet xs = model.get_intermediate_layers(&xs, &[0, 5, 11, 23])?; // ViT-B has 12 blocks\n// after\nlet xs = model.get_intermediate_layers(&xs, &[0, 5, 11])?; // indices < 12","handlingStrategy":"validation","validationCode":"let n_blocks = model.beit.encoder.layers.len(); // e.g. via config\nlet valid = blocks_to_take.iter().all(|&b| b < n_blocks);\nassert!(valid, \"blocks_to_take {:?} exceeds {} blocks\", blocks_to_take, n_blocks);","typeGuard":"fn blocks_exist(n_blocks: usize, blocks_to_take: &[usize]) -> bool {\n    blocks_to_take.iter().all(|&b| b < n_blocks)\n}","tryCatchPattern":"match model.get_intermediate_layers(&xs, blocks_to_take) {\n    Ok(v) => v,\n    Err(e) if e.to_string().contains(\"blocks found\") => {\n        let all: Vec<usize> = (0..n_blocks).collect();\n        model.get_intermediate_layers(&xs, &all)\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Read the number of encoder layers from config.json before picking indices","Remember block indices are 0-based","Don't copy block indices between model variants of different depth","Log the requested vs available blocks when selecting intermediate layers"],"tags":["rust","candle","beit","transformer","invalid-index"],"backgroundTag":"layer-index-out-of-range","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}