{"record":{"id":"c5fbf00b129a70df","repo":"huggingface/candle","slug":"cannot-reshape-tensor-of-el-count-elements-to-s","errorCode":null,"errorMessage":"cannot reshape tensor of {el_count} elements to {s:?}","messagePattern":"cannot reshape tensor of (.+?) elements to (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-core/src/shape.rs","lineNumber":496,"sourceCode":"            return Err(Error::ShapeMismatch {\n                buffer_size: el_count,\n                shape,\n            }\n            .bt());\n        }\n        Ok(shape)\n    }\n}\n\nimpl ShapeWithOneHole for ((),) {\n    fn into_shape(self, el_count: usize) -> Result<Shape> {\n        Ok(el_count.into())\n    }\n}\n\nfn hole_size(el_count: usize, prod_d: usize, s: &dyn std::fmt::Debug) -> Result<usize> {\n    if prod_d == 0 {\n        crate::bail!(\"cannot reshape tensor of {el_count} elements to {s:?}\")\n    }\n    if !el_count.is_multiple_of(prod_d) {\n        crate::bail!(\"cannot reshape tensor with {el_count} elements to {s:?}\")\n    }\n    Ok(el_count / prod_d)\n}\n\nimpl ShapeWithOneHole for ((), usize) {\n    fn into_shape(self, el_count: usize) -> Result<Shape> {\n        let ((), d1) = self;\n        Ok((hole_size(el_count, d1, &self)?, d1).into())\n    }\n}\n\nimpl ShapeWithOneHole for (usize, ()) {\n    fn into_shape(self, el_count: usize) -> Result<Shape> {\n        let (d1, ()) = self;\n        Ok((d1, hole_size(el_count, d1, &self)?).into())","sourceCodeStart":478,"sourceCodeEnd":514,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-core/src/shape.rs#L478-L514","documentation":"into_shape supports one wildcard dimension (a hole) in the target shape. hole_size computes the hole's size by dividing the tensor's element count by the product of the concrete dims. If that product is 0 (the target shape contains a 0-sized dimension alongside the hole), the hole cannot be resolved and this error fires.","triggerScenarios":"Calling tensor.into_shape(...) / reshape with a shape containing both a hole (e.g. ((), 64)) and a 0 dimension, so prod_d == 0 while the tensor is non-empty.","commonSituations":"Constructing shapes from runtime dims where a batch/seq dim was 0; passing shape tuples built programmatically with a 0 entry; misunderstanding that a 0 dim is itself a valid wildcard-free dim.","solutions":["Remove the 0-sized dimension from the target shape or fix the upstream dim that is 0","If you intended a wildcard, use () (or -1 in user-facing APIs) for the unknown dim, not 0","Compute the target shape explicitly with element counts you control"],"exampleFix":"// before\nt.into_shape((0, 64))? // prod includes 0\n// after\nt.into_shape(((), 64))? // hole infers the 0-th dim","handlingStrategy":"validation","validationCode":"let target = (0usize, 64usize); // built programmatically\nif target.0 == 0 && uses_hole(target) {\n    return Err(anyhow::anyhow!(\"0 dim alongside a reshape hole is invalid\"));\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never combine a 0-sized dim with a wildcard (hole) dim in reshape targets","Validate runtime dims (batch/seq) upstream so 0 never reaches reshape","Use () for the inferred dimension instead of 0"],"tags":["candle","reshape","shape"],"backgroundTag":"reshape-element-count-mismatch","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}