huggingface/candle · error
only {} / {} blocks found
Error message
only {} / {} blocks found What it means
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.
Source
Thrown at candle-transformers/src/models/beit.rs:328
let xs = self.patch_embed.forward(xs)?;
Tensor::cat(&[&self.cls_token, &xs], 1)
}
fn get_intermediate_layers_not_chunked(
&self,
xs: &Tensor,
blocks_to_take: &[usize],
) -> Result<Vec<Tensor>> {
let mut xs = self.prepare_tokens_with_mask(xs)?;
let mut output = Vec::new();
for (i, blk) in self.blocks.iter().enumerate() {
xs = blk.forward(&xs)?;
if blocks_to_take.contains(&i) {
output.push(xs.clone());
}
}
if output.len() != blocks_to_take.len() {
candle::bail!(
"only {} / {} blocks found",
output.len(),
blocks_to_take.len()
);
}
Ok(output)
}
pub fn get_intermediate_layers(
&self,
xs: &Tensor,
blocks_to_take: &[usize],
reshape: bool,
return_class_token: bool,
norm: bool,
) -> Result<Tensor> {
let outputs = self.get_intermediate_layers_not_chunked(xs, blocks_to_take)?;
let outputs = if norm {View on GitHub (pinned to d5fee525bf)
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
Example fix
// before let xs = model.get_intermediate_layers(&xs, &[0, 5, 11, 23])?; // ViT-B has 12 blocks // after let xs = model.get_intermediate_layers(&xs, &[0, 5, 11])?; // indices < 12
Defensive patterns
Strategy: validation
Validate before calling
let n_blocks = model.beit.encoder.layers.len(); // e.g. via config
let valid = blocks_to_take.iter().all(|&b| b < n_blocks);
assert!(valid, "blocks_to_take {:?} exceeds {} blocks", blocks_to_take, n_blocks); Type guard
fn blocks_exist(n_blocks: usize, blocks_to_take: &[usize]) -> bool {
blocks_to_take.iter().all(|&b| b < n_blocks)
} Try / catch
match model.get_intermediate_layers(&xs, blocks_to_take) {
Ok(v) => v,
Err(e) if e.to_string().contains("blocks found") => {
let all: Vec<usize> = (0..n_blocks).collect();
model.get_intermediate_layers(&xs, &all)
}
Err(e) => return Err(e),
} Prevention
- 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
When it happens
Trigger: Calling get_intermediate_layers with block indices >= the number of transformer blocks in the model, or duplicate/invalid indices in blocks_to_take.
Common situations: 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.
Related errors
- image height {h} is not a multiple of patch height {patch_h}
- image width {w} is not a multiple of patch width {patch_w}
- only kv-repeat = 1 is supported
- conv-block is not supported
- {} is a dummy type and cannot be constructed
AI-assisted analysis of huggingface/candle@d5fee525bf (2026-09-02).
Data as JSON: /api/errors/aff9252b9bd34f3e.
Report an issue: GitHub.