Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement scatter #9

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions candle-core/src/tensor_indexing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,4 +376,19 @@ impl Tensor {
let op = BackpropOp::new2(self, indexes, |t1, t2| Op::IndexSelect(t1, t2, dim));
Ok(from_storage(storage, dims, op, false))
}

pub fn scatter<D: Dim>(&self, indexes: &Self, source: &Self, dim: D) -> Result<Self> {
if self.dims().len() != indexes.dims().len() {
bail!("Self and indexes must have the same number of dimensions");
}
let dim = dim.to_index(self.shape(), "gather")?;
for (i, (&index_d, &self_d)) in indexes.dims().iter().zip(self.dims().iter()).enumerate() {
if i != dim && index_d > self_d {
bail!("Required that index dim <= self dim at every dim except than `dim`, got {index_d} > {self_d}");
}
}
let zeroed =
self.index_add(indexes, &(self.index_select(indexes, dim)? * -1.0f64)?, dim)?;
zeroed.index_add(indexes, source, dim)
}
}
Loading