huggingface/candle · error

unexpected len from chunk {ys:?}

Error message

unexpected len from chunk {ys:?}

What it means

Identical guard to model.rs:250 but in the quantized (GGUF/quantized weights) Flux variant: QModulation1::forward projects the conditioning vector to 3*dim and chunks it into shift/scale/gate. The bail fires when chunk(3, D::Minus1) does not return 3 chunks, meaning the quantized modulation linear's output last dimension is not divisible by 3 — almost always a hidden_size/config mismatch or a quantized checkpoint whose linear weights were incorrectly dequantized/loaded.

Source

Thrown at candle-transformers/src/models/flux/quantized_model.rs:89

#[derive(Debug, Clone)]
struct Modulation1 {
    lin: Linear,
}

impl Modulation1 {
    fn new(dim: usize, vb: VarBuilder) -> Result<Self> {
        let lin = linear(dim, 3 * dim, vb.pp("lin"))?;
        Ok(Self { lin })
    }

    fn forward(&self, vec_: &Tensor) -> Result<ModulationOut> {
        let ys = vec_
            .silu()?
            .apply(&self.lin)?
            .unsqueeze(1)?
            .chunk(3, D::Minus1)?;
        if ys.len() != 3 {
            candle::bail!("unexpected len from chunk {ys:?}")
        }
        Ok(ModulationOut {
            shift: ys[0].clone(),
            scale: ys[1].clone(),
            gate: ys[2].clone(),
        })
    }
}

#[derive(Debug, Clone)]
struct Modulation2 {
    lin: Linear,
}

impl Modulation2 {
    fn new(dim: usize, vb: VarBuilder) -> Result<Self> {
        let lin = linear(dim, 6 * dim, vb.pp("lin"))?;
        Ok(Self { lin })

View on GitHub (pinned to d5fee525bf)

Solutions

  1. Verify the FluxConfig.hidden_size used with the quantized model matches the GGUF file (inspect tensor names/shapes with gguf metadata tools).
  2. Inspect the {ys:?} debug in the error message to see the actual chunk count and derive the real feature dim.
  3. Re-download the GGUF checkpoint from a trusted source; a corrupt or partially downloaded GGUF can yield wrong tensor shapes.
  4. If self-converted, fix the conversion script so modulation lin weights map to the correct (3*hidden_size, hidden_size) shape.

Example fix

// before: config invented for a GGUF file
let config = FluxConfig { hidden_size: 2048, .. };
let model = Flux::new(&config, gguf_content, vb)?;

// after: hidden_size matching the quantized checkpoint
let config = FluxConfig { hidden_size: 3072, .. };
let model = Flux::new(&config, gguf_content, vb)?;
Defensive patterns

Strategy: validation

Validate before calling

// Rust: verify GGUF tensor shapes against config before building the quantized model
let hidden = cfg.hidden_size;
let expected: String = format!("double_blocks.0.modulation.lin.weight");
if let Some(t) = content.tensor(&expected) {
    if t.dims() != [3 * hidden, hidden] {
        candle::bail!("gguf lin shape {:?} != [{}, {}]", t.dims(), 3 * hidden, hidden);
    }
}

Type guard

fn gguf_mod_shape_ok(dims: &[usize], hidden: usize) -> bool {
    dims == [3 * hidden, hidden]
}

Try / catch

match quantized_flux.forward(&img, &img_ids, &txt, &txt_ids, &ts, &y, guidance) {
    Ok(t) => t,
    Err(e) if e.to_string().contains("unexpected len from chunk") => {
        eprintln!("quantized checkpoint/config mismatch: {e}");
        return Err(e);
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: Running the quantized Flux model (candle-transformers::models::flux::quantized_model) with a FluxConfig whose hidden_size does not match the GGUF checkpoint; loading mismatched GGUF shards; a vec_ conditioning tensor with wrong feature width entering the block.

Common situations: Using GGUF-quantized Flux.1 files with hand-built configs; GGUF files from different model revisions (dev vs schnell metadata); bugs in custom GGUF conversion scripts producing wrong linear dimensions.

Understand the failure class

Background: Tensor shape mismatch errors ("must have shape", "expected shape ... got ..."): when tensor dimensions disagree with what an op or layer was told to expect — this error's family across 6 libraries.

Related errors


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