huggingface/candle · error

alpha has to be contiguous

Error message

alpha has to be contiguous

What it means

The CPU RMSNorm kernel also requires the learnable alpha/scale parameter to be a contiguous 1-D slice; if alpha's layout is not contiguous the op bails with this message. The alpha vector is indexed directly against the last dimension.

Source

Thrown at candle-nn/src/ops.rs:478

        let eps = self.eps;
        fn inner<
            T: candle::WithDType
                + num_traits::Float
                + num_traits::AsPrimitive<f32>
                + num_traits::FromPrimitive,
        >(
            src: &[T],
            layout: &Layout,
            alpha: &[T],
            alpha_layout: &Layout,
            eps: f32,
        ) -> Result<(CpuStorage, Shape)> {
            let src = match layout.contiguous_offsets() {
                None => candle::bail!("input has to be contiguous"),
                Some((o1, o2)) => &src[o1..o2],
            };
            let alpha = match alpha_layout.contiguous_offsets() {
                None => candle::bail!("alpha has to be contiguous"),
                Some((o1, o2)) => &alpha[o1..o2],
            };
            let el_count = layout.shape().elem_count();
            let dims = layout.shape().dims();
            let dim_m1 = dims[dims.len() - 1];
            let n_rows = el_count / dim_m1;
            let mut dst = vec![T::zero(); el_count];

            fn rms_row<
                T: candle::WithDType
                    + num_traits::Float
                    + num_traits::AsPrimitive<f32>
                    + num_traits::FromPrimitive,
            >(
                src: &[T],
                alpha: &[T],
                n: usize,
                eps: f32,

View on GitHub (pinned to d5fee525bf)

Solutions

  1. Make alpha contiguous: alpha.contiguous()? before the call
  2. Store per-layer alphas as separate 1-D contiguous tensors
  3. Verify alpha.ndim()==1 and alpha.is_contiguous() when assembling weights

Example fix

// before
let out = rms_norm(&x, &weights.slice(0, layer, layer+1)?, eps)?;
// after
let alpha = weights.slice(0, layer, layer+1)?.contiguous()?;
let out = rms_norm(&x, &alpha, eps)?;
Defensive patterns

Strategy: validation

Validate before calling

if alpha.layout().contiguous_offsets().is_none() {
    alpha = alpha.contiguous()?;
}
let out = rms_norm(&x, &alpha, eps)?;

Type guard

fn alpha_ready(a: &Tensor) -> bool { a.rank() == 1 && a.layout().contiguous_offsets().is_some() }

Try / catch

let alpha = alpha.contiguous()?; // cheap no-op if already contiguous
let out = rms_norm(&x, &alpha, eps)?;

Prevention

When it happens

Trigger: Calling rms_norm on CPU with an alpha tensor that is a non-contiguous view (e.g. sliced from a larger weight tensor or permuted).

Common situations: Sharing one big parameter buffer and slicing per-layer alphas out of it; loading weights with views instead of copies; alpha produced by another op returning a strided view.

Related errors


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