{"record":{"id":"ddf319a589aef57d","repo":"huggingface/candle","slug":"dim-is-odd","errorCode":null,"errorMessage":"{dim} is odd","messagePattern":"(.+?) is odd","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-transformers/src/models/flux/model.rs","lineNumber":123,"sourceCode":"    let x0 = x.narrow(D::Minus1, 0, 1)?;\n    let x1 = x.narrow(D::Minus1, 1, 1)?;\n    let fr0 = freq_cis.get_on_dim(D::Minus1, 0)?;\n    let fr1 = freq_cis.get_on_dim(D::Minus1, 1)?;\n    (fr0.broadcast_mul(&x0)? + fr1.broadcast_mul(&x1)?)?.reshape(dims.to_vec())\n}\n\npub(crate) fn attention(q: &Tensor, k: &Tensor, v: &Tensor, pe: &Tensor) -> Result<Tensor> {\n    let q = apply_rope(q, pe)?.contiguous()?;\n    let k = apply_rope(k, pe)?.contiguous()?;\n    let x = scaled_dot_product_attention(&q, &k, v)?;\n    x.transpose(1, 2)?.flatten_from(2)\n}\n\npub(crate) fn timestep_embedding(t: &Tensor, dim: usize, dtype: DType) -> Result<Tensor> {\n    const TIME_FACTOR: f64 = 1000.;\n    const MAX_PERIOD: f64 = 10000.;\n    if dim % 2 == 1 {\n        candle::bail!(\"{dim} is odd\")\n    }\n    let dev = t.device();\n    let half = dim / 2;\n    let t = (t * TIME_FACTOR)?;\n    let arange = Tensor::arange(0, half as u32, dev)?.to_dtype(candle::DType::F32)?;\n    let freqs = (arange * (-MAX_PERIOD.ln() / half as f64))?.exp()?;\n    let args = t\n        .unsqueeze(1)?\n        .to_dtype(candle::DType::F32)?\n        .broadcast_mul(&freqs.unsqueeze(0)?)?;\n    let emb = Tensor::cat(&[args.cos()?, args.sin()?], D::Minus1)?.to_dtype(dtype)?;\n    Ok(emb)\n}\n\n#[derive(Debug, Clone)]\npub struct EmbedNd {\n    #[allow(unused)]\n    dim: usize,","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-transformers/src/models/flux/model.rs#L105-L141","documentation":"timestep_embedding() in flux/model.rs builds sinusoidal timestep embeddings by concatenating sin and cos halves of size dim/2, which only works for even dim. An odd dim triggers bail!(\"{dim} is odd\").","triggerScenarios":"Calling timestep_embedding(t, dim, dtype) (directly or via Flux forward) with an odd dim — e.g. a custom Flux config whose embedding/hidden dimension is odd.","commonSituations":"Modifying Flux's time_step_embedding_dim / hidden_size to a nonstandard odd value, or invoking the pub(crate) helper with a hand-picked odd width in custom code.","solutions":["Pass an even dim (use the model's standard embedding size, e.g. 256).","If a custom width is needed, round it up to the next even number before calling.","Check your Flux Config for odd dimensions and align them with upstream Flux defaults."],"exampleFix":"// before\nlet emb = timestep_embedding(&t, 255, dtype)?; // bails: odd\n// after\nlet emb = timestep_embedding(&t, 256, dtype)?; // even, ok","handlingStrategy":"validation","validationCode":"let dim = config.time_step_embedding_dim;\nif dim % 2 != 0 {\n    return Err(anyhow::anyhow!(\"embedding dim {dim} must be even for sinusoidal timestep embedding\"));\n}","typeGuard":"fn even_dim(dim: usize) -> Option<usize> { if dim % 2 == 0 { Some(dim) } else { None } }","tryCatchPattern":"let dim = if dim % 2 == 0 { dim } else { dim + 1 };\nlet emb = timestep_embedding(&t, dim, dtype)\n    .map_err(|e| anyhow!(\"timestep embedding failed (check dim is even): {e}\"))?;","preventionTips":["Always pass even embedding dimensions to timestep_embedding.","Keep Flux config dimension fields at upstream defaults (multiples of 2, typically powers of two).","Round custom widths up to even values before calling the helper."],"tags":["timestep-embedding","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"}