From 77db8396d09864111343dd13bdf5c42a251556fe Mon Sep 17 00:00:00 2001 From: Laurent Mazare Date: Wed, 22 Jan 2025 21:31:49 +0100 Subject: [PATCH] Explicit error when slice-set is called with the same src and dst. (#2733) --- candle-core/src/tensor_cat.rs | 3 +++ candle-core/tests/tensor_tests.rs | 2 ++ 2 files changed, 5 insertions(+) diff --git a/candle-core/src/tensor_cat.rs b/candle-core/src/tensor_cat.rs index be6dfe61..20b805c7 100644 --- a/candle-core/src/tensor_cat.rs +++ b/candle-core/src/tensor_cat.rs @@ -248,6 +248,9 @@ impl Tensor { if !self.is_contiguous() || !src.is_contiguous() { Err(Error::RequiresContiguous { op: "slice-set" }.bt())? } + if self.same_storage(src) { + crate::bail!("cannot use slice_set when self and src share their storage") + } if self.dtype() != src.dtype() { Err(Error::DTypeMismatchBinaryOp { lhs: self.dtype(), diff --git a/candle-core/tests/tensor_tests.rs b/candle-core/tests/tensor_tests.rs index e3246a33..17238dcd 100644 --- a/candle-core/tests/tensor_tests.rs +++ b/candle-core/tests/tensor_tests.rs @@ -729,6 +729,8 @@ fn slice_set(device: &Device) -> Result<()> { .sum_all()? .to_vec0::()?; assert_eq!(diff, 0.); + // This used to create a deadlock rather than returning an actual error. + assert!(cache.slice_set(&cache, 0, 0).is_err()); Ok(()) }