{"record":{"id":"60df901fbc81732a","repo":"huggingface/candle","slug":"expand-incompatible-shapes-for-broadcast-an","errorCode":null,"errorMessage":"Expand: incompatible shapes for broadcast, {:?} and {:?}","messagePattern":"Expand: incompatible shapes for broadcast, (.+?) and (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-onnx/src/eval.rs","lineNumber":2592,"sourceCode":"            None => bail!(\"cannot find output {}\", output.name),\n            Some(value) => Ok((output.name.clone(), value)),\n        })\n        .collect()\n}\n\nfn broadcast_shape(shape_a: &[usize], shape_b: &[usize]) -> Result<Vec<usize>> {\n    let (longest, shortest) = if shape_a.len() > shape_b.len() {\n        (shape_a, shape_b)\n    } else {\n        (shape_b, shape_a)\n    };\n    let diff = longest.len() - shortest.len();\n    let mut target_shape = longest[0..diff].to_vec();\n    for (dim1, dim2) in longest[diff..].iter().zip(shortest.iter()) {\n        if *dim1 == *dim2 || *dim2 == 1 || *dim1 == 1 {\n            target_shape.push(usize::max(*dim1, *dim2));\n        } else {\n            bail!(\n                \"Expand: incompatible shapes for broadcast, {:?} and {:?}\",\n                shape_a,\n                shape_b\n            );\n        }\n    }\n    Ok(target_shape)\n}\n\nfn broadcast_shape_from_many(shapes: &[&[usize]]) -> Result<Vec<usize>> {\n    if shapes.is_empty() {\n        return Ok(Vec::new());\n    }\n    let mut shape_out = shapes[0].to_vec();\n    for shape in shapes[1..].iter() {\n        shape_out = broadcast_shape(&shape_out, shape)?;\n    }\n    Ok(shape_out)","sourceCodeStart":2574,"sourceCodeEnd":2610,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-onnx/src/eval.rs#L2574-L2610","documentation":"broadcast_shape implements NumPy-style broadcasting used by the Expand operator: dimensions must be equal, or one of them must be 1. When two aligned dimensions differ and neither is 1, the shapes cannot be broadcast and this error fires, echoing both shapes.","triggerScenarios":"Calling Expand with a target shape incompatible with the input shape, e.g. expanding [3,1] to [4,4] (dim 3 vs 4), or broadcast_shape_from_many receiving mutually incompatible shapes.","commonSituations":"Models where the expand `shape` input is computed dynamically and ends up wrong; exporter miscalculations; hand-written shapes with typos; rank mismatches beyond left-padding rules.","solutions":["Correct the Expand `shape` input so each dim equals the input dim or is 1","Fix upstream ops producing the wrong target shape","Validate shape compatibility (dims equal or 1 after left-alignment) before running the graph"],"exampleFix":"# before\nExpand: input [3,1], shape [4,4]  -> error\n# after\nExpand: input [3,1], shape [3,4]  # dims align (3==3, 1 broadcasts to 4)","handlingStrategy":"validation","validationCode":"fn can_broadcast(a: &[usize], b: &[usize]) -> bool {\n    let (long, short) = if a.len() >= b.len() { (a, b) } else { (b, a) };\n    let d = long.len() - short.len();\n    long[d..].iter().zip(short).all(|(&x, &y)| x == y || x == 1 || y == 1)\n}","typeGuard":"fn expand_target_ok(input: &Tensor, target: &[usize]) -> bool {\n    can_broadcast(input.dims(), target)\n}","tryCatchPattern":"match eval(...) {\n    Err(e) if e.contains(\"incompatible shapes for broadcast\") => eprintln!(\"Expand shape incompatible with input\"),\n    other => other,\n}","preventionTips":["Validate Expand shape inputs (equal dim or 1) at model load","Check dynamically computed shape tensors for correctness","Run shape inference before inference to surface mismatches"],"tags":["onnx","expand","broadcast","shape"],"backgroundTag":"broadcast-shape-mismatch","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}