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

Remainder operator doc #1836

Merged
merged 3 commits into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions burn-book/src/building-blocks/tensor.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ Those operations are available for numeric tensor kinds: `Float` and `Int`.
| `tensor.powf_scalar(scalar)` or `tensor.powi_scalar(intscalar)` | `tensor.pow(scalar)` |
| `tensor.prod()` | `tensor.prod()` |
| `tensor.prod_dim(dim)` | `tensor.prod(dim, keepdim=True)` |
| `tensor.rem(other)` or `tensor % other` | `tensor % other` |
| `tensor.scatter(dim, indices, values)` | `tensor.scatter_add(dim, indices, values)` |
| `tensor.select(dim, indices)` | `tensor.index_select(dim, indices)` |
| `tensor.select_assign(dim, indices, values)` | N/A |
Expand Down
14 changes: 14 additions & 0 deletions crates/burn-tensor/src/tensor/api/numeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2868,6 +2868,20 @@ where
}
}

impl<E, const D: usize, B, K> core::ops::Rem<E> for Tensor<B, D, K>
where
E: ElementConversion,
B: Backend,
K: Numeric<B>,
K::Elem: Element,
{
type Output = Self;

fn rem(self, other: E) -> Self {
Tensor::remainder_scalar(self, other)
}
}

impl<B, const D: usize, K> core::ops::Mul<Tensor<B, D, K>> for Tensor<B, D, K>
where
B: Backend,
Expand Down
13 changes: 13 additions & 0 deletions crates/burn-tensor/src/tests/ops/remainder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,17 @@ mod tests {
let data_expected = Data::from([9.0, 1.0]);
data_expected.assert_approx_eq(&data_actual, 3);
}

#[test]
fn should_support_remainder_op() {
let data = Data::from([-3.0, -2.0, -1.0, 1.0, 2.0, 3.0]);
let device = Default::default();
let tensor = Tensor::<TestBackend, 1>::from_data(data, &device);

let output = tensor % 2.0;

let data_actual = output.into_data();
let data_expected = Data::from([1.0, 0.0, 1.0, 1.0, 0.0, 1.0]);
data_expected.assert_approx_eq(&data_actual, 3);
}
}
Loading