{"record":{"id":"36ff8c2973e463a7","repo":"huggingface/candle","slug":"slice-assign-requires-input-with-the-same-rank-as","errorCode":null,"errorMessage":"slice-assign requires input with the same rank as there are ranges {} <> {}","messagePattern":"slice-assign requires input with the same rank as there are ranges (.+?) <> (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-core/src/tensor.rs","lineNumber":2877,"sourceCode":"\n    /// Returns a copy of `self` where the values within `ranges` have been replaced with the\n    /// content of `src`.\n    pub fn slice_assign<D: std::ops::RangeBounds<usize>>(\n        &self,\n        ranges: &[D],\n        src: &Tensor,\n    ) -> Result<Self> {\n        let src_dims = src.dims();\n        let self_dims = self.dims();\n        if self_dims.len() != src_dims.len() {\n            bail!(\n                \"slice-assign requires input with the same rank {} <> {}\",\n                self_dims.len(),\n                src_dims.len()\n            )\n        }\n        if self_dims.len() != ranges.len() {\n            bail!(\n                \"slice-assign requires input with the same rank as there are ranges {} <> {}\",\n                self_dims.len(),\n                ranges.len()\n            )\n        }\n        let mut src = src.clone();\n        let mut mask = Self::ones(src.shape(), DType::U8, src.device())?;\n        for (i, range) in ranges.iter().enumerate() {\n            let start_included = match range.start_bound() {\n                std::ops::Bound::Unbounded => 0,\n                std::ops::Bound::Included(v) => *v,\n                std::ops::Bound::Excluded(v) => *v + 1,\n            };\n            let end_excluded = match range.end_bound() {\n                std::ops::Bound::Unbounded => self_dims[i],\n                std::ops::Bound::Included(v) => *v + 1,\n                std::ops::Bound::Excluded(v) => *v,\n            };","sourceCodeStart":2859,"sourceCodeEnd":2895,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-core/src/tensor.rs#L2859-L2895","documentation":"slice_assign needs exactly one range per dimension of self: the ranges slice defines the rectangular region being replaced. If the number of ranges doesn't equal self's rank, the region is underspecified, so it bails reporting self's rank and the number of ranges.","triggerScenarios":"tensor.slice_assign(&[(0..2)?], &src) on a rank-2 tensor (1 range vs rank 2), or passing an empty ranges slice.","commonSituations":"Copy-pasted slice_assign calls after the tensor gained a dimension; building ranges programmatically and dropping a dim; using a 1-range pattern for a 3-D tensor.","solutions":["Supply one range per dimension of self, using 0..dim for dims you don't want to restrict","Generate ranges from self.dims() to guarantee the count matches","Split the operation into narrow + assign if per-dim ranges are unwieldy"],"exampleFix":"// before\nt.slice_assign(&[0..2], &src)?; // t is rank 2\n// after\nlet d0 = t.dim(0)?;\nt.slice_assign(&[0..2, 0..d0], &src)?;","handlingStrategy":"validation","validationCode":"assert_eq!(ranges.len(), t.dims().len(), \"one range per dim required\");","typeGuard":null,"tryCatchPattern":"let ranges = full_ranges_or(ranges, t.dims()); // fill missing dims with 0..dim\nt.slice_assign(&ranges, &src)?;","preventionTips":["Build ranges from t.dims() programmatically to guarantee the count","Default unconstrained dims to 0..dim_i","Review slice_assign calls after adding dimensions to tensors"],"tags":["rust","candle","slice-assign","ranges"],"backgroundTag":"tensor-rank-mismatch","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}