{"record":{"id":"81dc8e6476afe69c","repo":"huggingface/candle","slug":"slice-assign-upper-bound-is-out-of-range-for-dim","errorCode":null,"errorMessage":"slice-assign: upper bound is out of range for dim {i}, {end_excluded} {}","messagePattern":"slice-assign: upper bound is out of range for dim (.+?), (.+?) (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-core/src/tensor.rs","lineNumber":2900,"sourceCode":"        }\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            };\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\")?;","sourceCodeStart":2882,"sourceCodeEnd":2918,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-core/src/tensor.rs#L2882-L2918","documentation":"slice_assign resolves each range's end bound (Unbounded -> dim size, Included(v) -> v+1, Excluded(v) -> v) and requires the resulting end_excluded to fit within self's dimension size. If end_excluded exceeds that size, the target region extends past the tensor and the method bails reporting the bound and the dim size.","triggerScenarios":"Passing 0..10 to a dim of size 8, or Bound::Included(7) on a dim of size 8 (resolves to end 9 > 8).","commonSituations":"Hardcoded slice bounds from another tensor's shape; forgetting Included(v) means 'v is inside' so end becomes v+1; shape changed after a resize/reshape and bounds went stale.","solutions":["Clamp range ends to self.dims()[i] before calling","Use .. (Unbounded) for dims where you want the full extent","Recompute bounds from the tensor's current dims instead of constants"],"exampleFix":"// before\nt.slice_assign(&[0..10, 0..4], &src)?; // dim0 size 8\n// after\nlet end = t.dim(0)?.min(10);\nt.slice_assign(&[0..end, 0..4], &src)?;","handlingStrategy":"validation","validationCode":"let d0 = t.dim(0)?;\nlet range = 0..end.min(d0); // clamp before calling\nassert!(range.end <= d0);","typeGuard":null,"tryCatchPattern":"let end = end.min(t.dim(i)?);\nt.slice_assign(&[start..end, 0..d1], &src)?;","preventionTips":["Clamp all range ends to the tensor's current dims","Prefer .. (unbounded) for full-extent dims","Recompute bounds after any reshape/resize instead of caching constants"],"tags":["rust","candle","slice-assign","bounds"],"backgroundTag":"slice-range-out-of-bounds","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}