{"record":{"id":"8df463be3e16f69d","repo":"huggingface/candle","slug":"dim-dim-is-odd","errorCode":null,"errorMessage":"dim {dim} is odd","messagePattern":"dim (.+?) is odd","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-transformers/src/models/flux/model.rs","lineNumber":82,"sourceCode":"fn scaled_dot_product_attention(q: &Tensor, k: &Tensor, v: &Tensor) -> Result<Tensor> {\n    let dim = q.dim(D::Minus1)?;\n    let scale_factor = 1.0 / (dim as f64).sqrt();\n    let mut batch_dims = q.dims().to_vec();\n    batch_dims.pop();\n    batch_dims.pop();\n    let q = q.flatten_to(batch_dims.len() - 1)?;\n    let k = k.flatten_to(batch_dims.len() - 1)?;\n    let v = v.flatten_to(batch_dims.len() - 1)?;\n    let attn_weights = (q.matmul(&k.t()?)? * scale_factor)?;\n    let attn_scores = candle_nn::ops::softmax_last_dim(&attn_weights)?.matmul(&v)?;\n    batch_dims.push(attn_scores.dim(D::Minus2)?);\n    batch_dims.push(attn_scores.dim(D::Minus1)?);\n    attn_scores.reshape(batch_dims)\n}\n\nfn rope(pos: &Tensor, dim: usize, theta: usize) -> Result<Tensor> {\n    if dim % 2 == 1 {\n        candle::bail!(\"dim {dim} is odd\")\n    }\n    let dev = pos.device();\n    let theta = theta as f64;\n    let inv_freq: Vec<_> = (0..dim)\n        .step_by(2)\n        .map(|i| 1f32 / theta.powf(i as f64 / dim as f64) as f32)\n        .collect();\n    let inv_freq_len = inv_freq.len();\n    let inv_freq = Tensor::from_vec(inv_freq, (1, 1, inv_freq_len), dev)?;\n    let inv_freq = inv_freq.to_dtype(pos.dtype())?;\n    let freqs = pos.unsqueeze(2)?.broadcast_mul(&inv_freq)?;\n    let cos = freqs.cos()?;\n    let sin = freqs.sin()?;\n    let out = Tensor::stack(&[&cos, &sin.neg()?, &sin, &cos], 3)?;\n    let (b, n, d, _ij) = out.dims4()?;\n    out.reshape((b, n, d, 2, 2))\n}\n","sourceCodeStart":64,"sourceCodeEnd":100,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-transformers/src/models/flux/model.rs#L64-L100","documentation":"The rope() helper in flux/model.rs computes rotary position embeddings by splitting the dimension into even/odd halves, so it requires dim to be even. If an odd head dimension reaches it, bail! returns Err(\"dim {dim} is odd\") instead of producing a broken frequency table.","triggerScenarios":"Calling rope(pos, dim, theta) with an odd dim — practically, a Fluxmodel built with config.hidden_size not divisible by the number of attention heads, giving an odd per-head head_dim that flows through forward -> rope.","commonSituations":"Hand-editing Flux model config (hidden_size or num_attention_heads) so hidden_size/num_heads is odd; using a custom/modified Flux variant with an unusual embedding width.","solutions":["Make hidden_size divisible by num_attention_heads such that head_dim is even (e.g. hidden_size 3072 / heads 12 -> 256).","Check the Config you pass to FluxModel and revert any custom modifications to standard Flux dimensions.","If you call rope directly, ensure you pass an even dim (typically the even head_dim from the attention layer)."],"exampleFix":"// before\nlet config = Config { hidden_size: 1537, num_attention_heads: 4, .. };\n// head_dim = 1537/4 -> odd downstream\n// after\nlet config = Config { hidden_size: 1536, num_attention_heads: 4, .. };\n// head_dim = 384 (even), rope() succeeds","handlingStrategy":"validation","validationCode":"let head_dim = config.hidden_size / config.num_attention_heads;\nif head_dim % 2 != 0 {\n    return Err(anyhow::anyhow!(\"head_dim {head_dim} is odd; rope requires even dim\"));\n}","typeGuard":"fn is_even_dim(dim: usize) -> bool { dim % 2 == 0 }","tryCatchPattern":"match rope(&pos, head_dim, theta) {\n    Ok(freqs) => freqs,\n    Err(e) if e.to_string().ends_with(\"is odd\") => {\n        anyhow::bail!(\"fix model config so hidden_size/num_heads is even: {e}\")\n    }\n    Err(e) => return Err(e.into()),\n}","preventionTips":["Keep hidden_size divisible by num_attention_heads with an even quotient.","Use upstream Flux default dimensions unless you've verified compatibility.","Add an assert!(head_dim % 2 == 0) when constructing attention layers."],"tags":["rope","flux","tensor-shape","candle"],"backgroundTag":"invalid-dimension-shape","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}