{"record":{"id":"598f1ae6bf229a9a","repo":"huggingface/candle","slug":"embedding-dimension-must-be-even","errorCode":null,"errorMessage":"Embedding dimension must be even","messagePattern":"Embedding dimension must be even","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-transformers/src/models/mmdit/embedding.rs","lineNumber":145,"sourceCode":"    ) -> Result<Self> {\n        let mlp = nn::seq()\n            .add(nn::linear(\n                frequency_embedding_size,\n                hidden_size,\n                vb.pp(\"mlp.0\"),\n            )?)\n            .add(nn::Activation::Silu)\n            .add(nn::linear(hidden_size, hidden_size, vb.pp(\"mlp.2\"))?);\n\n        Ok(Self {\n            mlp,\n            frequency_embedding_size,\n        })\n    }\n\n    fn timestep_embedding(t: &Tensor, dim: usize, max_period: f64) -> Result<Tensor> {\n        if !dim.is_multiple_of(2) {\n            bail!(\"Embedding dimension must be even\")\n        }\n\n        if t.dtype() != DType::F32 && t.dtype() != DType::F64 {\n            bail!(\"Input tensor must be floating point\")\n        }\n\n        let half = dim / 2;\n        let freqs = Tensor::arange(0f32, half as f32, t.device())?\n            .to_dtype(candle::DType::F32)?\n            .mul(&Tensor::full(\n                (-f64::ln(max_period) / half as f64) as f32,\n                half,\n                t.device(),\n            )?)?\n            .exp()?;\n\n        let args = t\n            .unsqueeze(1)?","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-transformers/src/models/mmdit/embedding.rs#L127-L163","documentation":"The mmdit timestep_embedding helper builds sinusoidal embeddings by pairing sin/cos frequency channels, which requires dim to be even. An odd embedding dimension cannot be split into two halves, so it bails immediately.","triggerScenarios":"Calling timestep_embedding (from Timesteps/TimestepEmbedding forward) with an odd dim, usually from a misconfigured frequency_embedding_size or embedding dimension in the mmdit config.","commonSituations":"Typo in config (odd hidden/frequency embedding size); modifying TimestepEmbedding params and picking an odd dim; deriving dims from arithmetic that yields odd values.","solutions":["Make the timestep embedding dimension even (e.g. round up to the next multiple of 2)","Check that frequency_embedding_size / inner embedding dims in the config are even","Match the checkpoint's expected embedding dimension"],"exampleFix":"// before\ntimestep_embedding(&t, 257, max_period)?; // odd dim bails\n// after\ntimestep_embedding(&t, 256, max_period)?;","handlingStrategy":"validation","validationCode":"if dim % 2 != 0 {\n    return Err(anyhow::anyhow!(\"timestep embedding dim must be even, got {dim}\"));\n}\nlet emb = timestep_embedding(&t, dim, max_period)?;","typeGuard":"fn is_even_dim(dim: usize) -> bool { dim % 2 == 0 }","tryCatchPattern":"let emb = timestep_embedding(&t, dim, max_period)\n    .map_err(|e| if e.to_string().contains(\"Embedding dimension must be even\") {\n        anyhow::anyhow!(\"use an even embedding dim (round up to next even value)\")\n    } else { e.into() })?;","preventionTips":["Round embedding dims up to the nearest even number in config code","Also ensure timestep tensors are F32/F64 (the same helper enforces this)","Add a config sanity check that all embedding dims are even"],"tags":["rust","candle","mmdit","diffusion","tensor-shape"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}