huggingface/candle · error

Input size is too large for the position embedding

Error message

Input size is too large for the position embedding

What it means

PatchEmbed's get_cropped_pos_embed crops the learned positional embedding grid to (h,w) patch coords, but the grid has a fixed maximum size (pos_embed_max_size). If the requested height or width in patches exceeds that size, there is no positional embedding available and it bails.

Source

Thrown at candle-transformers/src/models/mmdit/embedding.rs:103

        pos_embed_max_size: usize,
        vb: nn::VarBuilder,
    ) -> Result<Self> {
        let pos_embed = vb.get(
            (1, pos_embed_max_size * pos_embed_max_size, hidden_size),
            "pos_embed",
        )?;
        Ok(Self {
            pos_embed,
            patch_size,
            pos_embed_max_size,
        })
    }
    pub fn get_cropped_pos_embed(&self, h: usize, w: usize) -> Result<Tensor> {
        let h = (h + 1) / self.patch_size;
        let w = (w + 1) / self.patch_size;

        if h > self.pos_embed_max_size || w > self.pos_embed_max_size {
            bail!("Input size is too large for the position embedding")
        }

        let top = (self.pos_embed_max_size - h) / 2;
        let left = (self.pos_embed_max_size - w) / 2;

        let pos_embed =
            self.pos_embed
                .reshape((1, self.pos_embed_max_size, self.pos_embed_max_size, ()))?;
        let pos_embed = pos_embed.narrow(1, top, h)?.narrow(2, left, w)?;
        pos_embed.reshape((1, h * w, ()))
    }
}

pub struct TimestepEmbedder {
    mlp: nn::Sequential,
    frequency_embedding_size: usize,
}

View on GitHub (pinned to d5fee525bf)

Solutions

  1. Reduce height/width so that ceil(dim/patch_size) <= pos_embed_max_size
  2. Load/construct the model with a larger pos_embed_max_size matching your checkpoint
  3. Interpolate or fine-tune positional embeddings for larger resolutions

Example fix

// before
let h = 2048; // h/8 = 256 > pos_embed_max_size 128
let x = patch_embed.forward(&xs, h, w)?; // bails
// after
let h = 1024; // 1024/8 = 128 <= 128
let x = patch_embed.forward(&xs, h, w)?;
Defensive patterns

Strategy: validation

Validate before calling

let (ph, pw) = ((h + 1) / patch_size, (w + 1) / patch_size);
if ph > pos_embed_max_size || pw > pos_embed_max_size {
    return Err(anyhow::anyhow!("requested {h}x{w} exceeds max {}", pos_embed_max_size * patch_size));
}

Type guard

fn fits_pos_embed(h: usize, w: usize, patch_size: usize, max_size: usize) -> bool {
    (h + 1) / patch_size <= max_size && (w + 1) / patch_size <= max_size
}

Try / catch

let xs = match patch_embed.forward(&latents, h, w) {
    Ok(x) => x,
    Err(e) if e.to_string().contains("Input size is too large") => {
        return Err(anyhow::anyhow!("reduce resolution; max is pos_embed_max_size * patch_size"))
    }
    Err(e) => return Err(e.into()),
};

Prevention

When it happens

Trigger: Calling get_cropped_pos_embed(h, w) (via PatchEmbed forward) where ceil(h/patch_size) or ceil(w/patch_size) exceeds pos_embed_max_size, i.e. generating images larger than the model's trained resolution.

Common situations: Requesting SD3/Stable Diffusion 3 mmdit generation at resolutions above the max the positional embedding grid supports (e.g. 1024x1024 for max_size 128 with patch_size 8); mis-set height/width values.

Related errors


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