{"record":{"id":"e366346fcbf9926f","repo":"huggingface/candle","slug":"shape-mismatch-on-path-shape-tensor-sha","errorCode":null,"errorMessage":"shape mismatch on {path}: {shape:?} <> {tensor_shape:?}","messagePattern":"shape mismatch on (.+?): (.+?) <> (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-nn/src/var_map.rs","lineNumber":108,"sourceCode":"        }\n        Ok(())\n    }\n\n    /// Retrieve or add a new variable.\n    pub fn get<S: Into<Shape>>(\n        &self,\n        shape: S,\n        path: &str,\n        init: crate::Init,\n        dtype: DType,\n        device: &Device,\n    ) -> Result<Tensor> {\n        let shape = shape.into();\n        let mut tensor_data = self.data.lock().unwrap();\n        if let Some(tensor) = tensor_data.get(path) {\n            let tensor_shape = tensor.shape();\n            if &shape != tensor_shape {\n                candle::bail!(\"shape mismatch on {path}: {shape:?} <> {tensor_shape:?}\")\n            }\n            return Ok(tensor.as_tensor().clone());\n        }\n        let var = init.var(shape, dtype, device)?;\n        let tensor = var.as_tensor().clone();\n        tensor_data.insert(path.to_string(), var);\n        Ok(tensor)\n    }\n\n    pub fn data(&self) -> &Mutex<HashMap<String, Var>> {\n        &self.data\n    }\n}\n","sourceCodeStart":90,"sourceCodeEnd":122,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-nn/src/var_map.rs#L90-L122","documentation":"VarMap::get looks up a variable by path and validates that the requested shape matches the stored tensor's shape. If a variable already exists under that path but with a different shape than requested, the library bails instead of silently reusing or reshaping it. This protects callers from accidentally sharing a cached variable that has incompatible dimensions.","triggerScenarios":"Calling VarMap::get (directly or via var/get helpers) with a shape argument that differs from the shape the variable was first created with under the same path; typically across two model builds in one process where layer sizes changed (e.g. different vocab size, hidden size, or batch dims).","commonSituations":"Re-running a model-construction function twice in one process with a different config; loading pretrained weights whose shapes don't match the model definition; typos causing two different layers to collide on the same path.","solutions":["Make the requested shape match the shape the variable was originally created with (print tensor_shape from the error to compare).","If shapes intentionally differ, use a different path/name for the new variable or clear the VarMap (create a fresh one) before rebuilding.","When loading a safetensors/var-builder map, verify the model config used to compute shapes matches the checkpoint's config.","If a reshape is what you want, fetch the existing tensor and reshape it explicitly rather than calling get with a mismatched shape."],"exampleFix":"// before\nlet w = var_map.get(( vocab, hidden ), DType::F32, \"h.model.weight\", init)?; // vocab changed to 32000\n// after\nlet w = var_map.get(( 32000, hidden ), DType::F32, \"h.model.weight\", init)?; // match stored shape","handlingStrategy":"validation","validationCode":"let stored = var_map.data.lock().unwrap().get(path).map(|t| t.shape().clone());\nif let Some(s) = &stored {\n    assert_eq!(s, &shape, \"VarMap '{path}' shape {s:?} != requested {shape:?}\");\n}","typeGuard":"fn shape_matches(stored: &candle::Shape, requested: &[usize]) -> bool {\n    stored.dims() == requested\n}","tryCatchPattern":"match var_map.get(shape, dtype, path, init) {\n    Ok(t) => t,\n    Err(e) if e.to_string().contains(\"shape mismatch\") => {\n        // rebuild with fresh VarMap or corrected shape\n        let mut fresh = VarMap::new();\n        fresh.get(shape, dtype, path, init)?\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Derive variable shapes from a single config struct so two build paths can't diverge","Keep one VarMap per model instance; never reuse across differently-configured builds","Log/checkpoint shapes on creation to compare against later get() calls","Validate checkpoint tensor shapes against model config before load"],"tags":["rust","candle","shape-mismatch","tensor"],"backgroundTag":"shape-mismatch","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}