huggingface/candle · error

unexpected len from chunk {ys:?}

Error message

unexpected len from chunk {ys:?}

What it means

Flux's Modulation1::forward projects the conditioning vector through a linear layer mapped to 3*dim and splits it into 3 chunks (shift/scale/gate). This bail fires when candle's Tensor::chunk(3, D::Minus1) returns a Vec whose length is not 3, meaning the last dimension of the projected tensor is not evenly divisible by 3 — practically always a symptom of a hidden_size/config mismatch between the loaded weights and the FluxConfig used, or a malformed conditioning vector.

Source

Thrown at candle-transformers/src/models/flux/model.rs:250

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

impl Modulation1 {
    fn new(dim: usize, vb: VarBuilder) -> Result<Self> {
        let lin = candle_nn::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 = candle_nn::linear(dim, 6 * dim, vb.pp("lin"))?;
        Ok(Self { lin })

View on GitHub (pinned to d5fee525bf)

Solutions

  1. Verify the FluxConfig (hidden_size, num_heads) matches the checkpoint actually loaded in VarBuilder; use the config the model was published with (dev vs schnell).
  2. Confirm `vec_` fed to the block has last dimension == cfg.hidden_size; check timestep_embedding output (256) passed through time_in and y through vector_in.
  3. Print the chunked tensor shape from the error message {ys:?} and check the linear layer's weight shape (out_dim should be 3*hidden_size) with var_builder retrieval.
  4. If using a custom/quantized checkpoint, re-export it with correct modulation layer shapes or regenerate safetensors from the reference implementation.

Example fix

// before: config mismatch
let config = FluxConfig::dev(); // but loading schnell weights
let model = Flux::new(&config, vb)?;

// after: matching config to checkpoint
let config = FluxConfig::schnell(); // matches loaded weights
let model = Flux::new(&config, vb)?;
Defensive patterns

Strategy: validation

Validate before calling

// Rust: before constructing/running the model
fn validate_dim(config_hidden: usize, vec_dim: usize) -> candle::Result<()> {
    if vec_dim != config_hidden {
        candle::bail!("vec_ dim {} != config hidden_size {}", vec_dim, config_hidden);
    }
    if (3 * config_hidden) % 3 != 0 { /* always true; kept for symmetry */ }
    Ok(())
}
// call: validate_dim(cfg.hidden_size, vec_.dim(D::Minus1)?)?;

Type guard

fn has_hidden_dim(t: &candle_core::Tensor, hidden: usize) -> bool {
    t.rank() >= 1 && t.dim(candle_core::D::Minus1).map(|d| d == hidden).unwrap_or(false)
}

Try / catch

match model_forward(...) {
    Ok(out) => out,
    Err(e) if e.to_string().contains("unexpected len from chunk") => {
        eprintln!("config/weights mismatch in modulation layer: {e}");
        return Err(e);
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: Calling Flux forward paths where the timesteps/class-label conditioning vector `vec_` has a last dimension that does not match the configured hidden_size, so `lin` output (3*dim) is not divisible by 3; loading Flux weights with a FluxConfig whose hidden_size disagrees with the checkpoint (e.g. using dev config for schnell-style partial weights or a custom checkpoint); feeding a `vec_` tensor of the wrong feature size into Modulation1 directly.

Common situations: Mixing Flux.1-dev and Flux.1-schnell checkpoints with the wrong config struct; hand-edited or quantized checkpoints whose modulation linear weights have unexpected dimensions; running modified pipeline code that reshapes the timestep embedding incorrectly.

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/9a6d4dc5b978f8fb. Report an issue: GitHub.