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.

Solutions

  1. Check the model's number of blocks and use indices in 0..n_blocks
  2. Use block indices matching the loaded checkpoint's depth
  3. 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

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


AI-assisted analysis of huggingface/candle@d5fee525bf (2026-09-02). Data as JSON: /api/errors/aff9252b9bd34f3e. Report an issue: GitHub.

Appendix: 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)