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 tensor.recip() function to calculate elementwise reciprocals #953

Merged
merged 6 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Add autodiff test for recip() call, and fix the implementation
  • Loading branch information
gzsombor committed Nov 15, 2023
commit b55995e6c322c3aefb40fdd00dae211dfeac95a8
19 changes: 14 additions & 5 deletions burn-autodiff/src/ops/tensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,16 +440,25 @@ impl<B: Backend> TensorOps<Self> for Autodiff<B> {
struct Recip;

impl<B: Backend, const D: usize> Backward<B, D, 1> for Recip {
type State = ();
type State = B::TensorPrimitive<D>;

fn backward(self, ops: Ops<Self::State, 1>, grads: &mut Gradients) {
unary::<B, D, D, _>(ops.parents, ops.node, grads, |grad| B::recip(grad));
let tensor = ops.state;
unary::<B, D, D, _>(ops.parents, ops.node, grads, |grad| {
let tmp = B::powf(tensor, -2.0);
let value = B::neg(tmp);

B::mul(grad, value)
});
}
}

Recip
.prepare([tensor.node], [tensor.graph])
.stateless(B::recip(tensor.primitive))
match Recip.prepare([tensor.node], [tensor.graph]).stateful() {
OpsKind::Tracked(prep) => {
prep.finish(tensor.primitive.clone(), B::recip(tensor.primitive))
}
OpsKind::UnTracked(prep) => prep.finish(B::recip(tensor.primitive)),
}
}

fn swap_dims<const D: usize>(
Expand Down
2 changes: 2 additions & 0 deletions burn-autodiff/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ mod mul;
mod multithread;
mod neg;
mod pow;
mod recip;
mod relu;
mod reshape;
mod select;
Expand Down Expand Up @@ -94,6 +95,7 @@ macro_rules! testgen_all {
burn_autodiff::testgen_ad_mul!();
burn_autodiff::testgen_ad_neg!();
burn_autodiff::testgen_ad_powf!();
burn_autodiff::testgen_ad_recip!();
burn_autodiff::testgen_ad_reshape!();
burn_autodiff::testgen_ad_sin!();
burn_autodiff::testgen_ad_softmax!();
Expand Down
20 changes: 20 additions & 0 deletions burn-autodiff/src/tests/recip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#[burn_tensor_testgen::testgen(ad_recip)]
mod tests {
use super::*;
use burn_tensor::Data;

#[test]
fn should_diff_recip() {
let data = Data::from([2.0, 5.0, 0.4]);

let tensor = TestAutodiffTensor::from_data(data).require_grad();
let tensor_out = tensor.clone().recip();

let grads = tensor_out.backward();
let grad = tensor.grad(&grads).unwrap();

assert_eq!(tensor_out.into_data(), Data::from([0.5, 0.2, 2.5]));
grad.to_data()
.assert_approx_eq(&Data::from([-0.25, -0.04, -6.25]), 3);
}
}
1 change: 1 addition & 0 deletions burn-candle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ mod tests {
burn_autodiff::testgen_ad_mul!();
burn_autodiff::testgen_ad_neg!();
burn_autodiff::testgen_ad_powf!();
burn_autodiff::testgen_ad_recip!();
burn_autodiff::testgen_ad_reshape!();
burn_autodiff::testgen_ad_sin!();
burn_autodiff::testgen_ad_softmax!();
Expand Down