{"record":{"id":"ffe493c6a049823d","repo":"huggingface/candle","slug":"cannot-use-slice-set-when-self-and-src-share-their","errorCode":null,"errorMessage":"cannot use slice_set when self and src share their storage","messagePattern":"cannot use slice_set when self and src share their storage","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-core/src/tensor.rs","lineNumber":1696,"sourceCode":"            .copy_strided_src(&mut storage, 0, self.layout())?;\n        let layout = Layout::contiguous(shape);\n        storage.scatter_set(\n            &layout,\n            &indexes.storage(),\n            indexes.layout(),\n            &source.storage(),\n            source.layout(),\n            dim,\n        )?;\n        let op = BackpropOp::new3(self, indexes, source, |t1, t2, t3| {\n            Op::Scatter(t1, t2, t3, dim)\n        });\n        Ok(from_storage(storage, self.shape(), op, false))\n    }\n\n    pub fn scatter_set<D: Dim>(&self, indexes: &Self, source: &Self, dim: D) -> Result<()> {\n        if self.same_storage(source) {\n            crate::bail!(\"cannot use slice_set when self and src share their storage\")\n        }\n        let dim = dim.to_index(self.shape(), \"scatter-set\")?;\n        self.scatter_checks(indexes, source, dim)?;\n        self.storage_mut().scatter_set(\n            self.layout(),\n            &indexes.storage(),\n            indexes.layout(),\n            &source.storage(),\n            source.layout(),\n            dim,\n        )?;\n        Ok(())\n    }\n\n    pub fn scatter_add<D: Dim>(&self, indexes: &Self, source: &Self, dim: D) -> Result<Self> {\n        let dim = dim.to_index(self.shape(), \"scatter-add\")?;\n        self.scatter_checks(indexes, source, dim)?;\n        let shape = self.shape();","sourceCodeStart":1678,"sourceCodeEnd":1714,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-core/src/tensor.rs#L1678-L1714","documentation":"scatter_set mutates self's storage in place, so if self and source share the same underlying storage (e.g. one is a view of the other), in-place writing would corrupt the source data. Candle guards this aliasing case with an explicit bail. Note the message says 'slice_set' — a copy-paste from the sibling op — but it is thrown by Tensor::scatter_set.","triggerScenarios":"Calling t.scatter_set(&indexes, &src, dim) where src was derived from t via views/reshapes/slices sharing storage (e.g. src = t.clone() at view level or src = t.i(..)).","commonSituations":"Chained in-place updates on tensors that alias each other; writing updated values back into a tensor using a slice of itself as source.","solutions":["Make an independent copy of the source first: let src = src.copy()? (or to_device/to_dtype which materialize) so storages differ.","Use the non-in-place scatter API instead of scatter_set, then assign the result.","Restructure code so the destination and source are distinct buffers."],"exampleFix":"// before\nlet src = t.i(0..2)?; // shares storage with t\nt.scatter_set(&idx, &src, 0)?; // error\n// after\nlet src = t.i(0..2)?.copy()?;\nt.scatter_set(&idx, &src, 0)?;","handlingStrategy":"validation","validationCode":"if t.same_storage(&src) {\n    let src = src.copy()?;\n}\nt.scatter_set(&idx, &src, dim)?;","typeGuard":null,"tryCatchPattern":"match t.scatter_set(&idx, &src, dim) {\n    Ok(()) => (),\n    Err(e) if e.to_string().contains(\"share their storage\") => {\n        let src = src.copy()?;\n        t.scatter_set(&idx, &src, dim)?;\n    }\n    Err(e) => return Err(e.into()),\n}","preventionTips":["Remember that views (slice, transpose, reshape) share storage; copy before in-place ops.","Prefer out-of-place scatter when in doubt.","For errors from this API prefer scatter_add_set semantics carefully; note the message text mentions slice_set erroneously."],"tags":["aliasing","in-place","scatter","storage"],"backgroundTag":"shared-storage-aliasing","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}