{"record":{"id":"b343c1299408ce3c","repo":"huggingface/candle","slug":"slice-assign-the-range-for-dim-i-start-includ","errorCode":null,"errorMessage":"slice-assign: the range for dim {i} ({start_included}..{end_excluded}) does not match the size of src {}","messagePattern":"slice-assign: the range for dim (.+?) \\((.+?)\\.\\.(.+?)\\) does not match the size of src (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-core/src/tensor.rs","lineNumber":2906,"sourceCode":"                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            };\n            if end_excluded <= start_included {\n                bail!(\"slice-assign: empty range for dim {i}, {start_included} {end_excluded}\")\n            }\n            if self_dims[i] < end_excluded {\n                bail!(\n                    \"slice-assign: upper bound is out of range for dim {i}, {end_excluded} {}\",\n                    self_dims[i]\n                )\n            }\n            if end_excluded - start_included != src_dims[i] {\n                bail!(\n                    \"slice-assign: the range for dim {i} ({start_included}..{end_excluded}) does not match the size of src {}\", src_dims[i]\n                )\n            }\n            src = src.pad_with_zeros(i, start_included, self_dims[i] - end_excluded)?;\n            mask = mask.pad_with_zeros(i, start_included, self_dims[i] - end_excluded)?\n        }\n        mask.where_cond(/* on_true= */ &src, /* on_false= */ self)\n    }\n\n    /// Returns log(sum(exp(tensor), dim)).\n    pub fn log_sum_exp<D: Dims>(&self, sum_dims: D) -> Result<Self> {\n        let sum_dims = sum_dims.to_indexes(self.shape(), \"log-sum-exp\")?;\n        if sum_dims.is_empty() {\n            return Ok(self.clone());\n        }\n        let max = sum_dims[1..]\n            .iter()\n            .try_fold(self.max_keepdim(sum_dims[0])?, |max, &dim| {","sourceCodeStart":2888,"sourceCodeEnd":2924,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-core/src/tensor.rs#L2888-L2924","documentation":"After validating the range for each dim, slice_assign requires the region width (end_excluded - start_included) to equal src's size along that dim, since the whole src tensor is written into the region (padded with zeros around it). A mismatch means src cannot fit the region, so it bails reporting the range and src's dim size.","triggerScenarios":"tensor.slice_assign(&[0..5, 0..4], &src) where src.dim(0) == 3, i.e. src dims don't match the region sizes dim-by-dim.","commonSituations":"Assigning a differently-shaped patch into a larger tensor without matching region size; shape drift after preprocessing; forgetting that each dim's range width must equal the corresponding src dim.","solutions":["Reshape/narrow src so each src_dims[i] equals end_excluded - start_included","Adjust the ranges so their widths match src's dims exactly","Pad or truncate src with pad_with_zeros/narrow before the call"],"exampleFix":"// before\n// region 0..5 (width 5), src.dim(0) = 3\nt.slice_assign(&[0..5, 0..4], &src)?;\n// after\nlet src = src.pad_with_zeros(0, 0, 2)?; // now dim0 = 5\nt.slice_assign(&[0..5, 0..4], &src)?;","handlingStrategy":"validation","validationCode":"let region: Vec<usize> = ranges.iter().zip(t.dims()).map(|(r, d)| r.end.min(d) - r.start).collect();\nassert_eq!(region, src.dims(), \"src must match region sizes\");","typeGuard":null,"tryCatchPattern":"let src = match pad_or_narrow_to(&src, &region_sizes) {\n    Ok(s) => s,\n    Err(e) => { log::error!(\"src shape {} vs region {:?}\", src.dims(), region_sizes); return Err(e); }\n};","preventionTips":["Compute region widths from ranges and compare to src.dims() before the call","Pad src with pad_with_zeros to the region size when intentional","Keep a single helper for slice-assign that enforces shape invariants"],"tags":["rust","candle","slice-assign","shape-mismatch"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}