forked from coreylowman/dfdx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
171 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use crate::tensor_ops::cpu_kernels::UnaryDerivative; | ||
|
||
impl<F: num_traits::Float + std::ops::Mul<Output=F>> UnaryDerivative<F> for super::AtanKernelOp { | ||
const DF_USES_FX: bool = false; | ||
const HAS_CONST_DF: bool = false; | ||
#[inline(always)] | ||
fn f(&self, x: &F) -> F { | ||
x.atan() | ||
} | ||
#[inline(always)] | ||
fn df(&self, x: &F) -> F { | ||
let one = F::from(1.0).unwrap(); | ||
one / (one + (*x)*(*x)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
mod cpu_kernel; | ||
|
||
#[cfg(feature = "cuda")] | ||
mod cuda_kernel; | ||
|
||
use super::ops::{try_unary_op, UnaryKernel}; | ||
use crate::{shapes::*, tensor::*}; | ||
|
||
#[repr(C)] | ||
#[derive(Debug, Default, Copy, Clone)] | ||
pub struct AtanKernelOp; | ||
|
||
/// | ||
/// It's derivative is `1 / (1 + x^2)` | ||
/// | ||
/// Examples: | ||
/// ```rust | ||
/// ``` | ||
pub fn atan<S: Shape, E: Dtype, D: UnaryKernel<AtanKernelOp, E>, T: Tape<E, D>>( | ||
t: Tensor<S, E, D, T>, | ||
) -> Tensor<S, E, D, T> { | ||
t.atan() | ||
} | ||
|
||
impl<S: Shape, E: Dtype, D: UnaryKernel<AtanKernelOp, E>, T: Tape<E, D>> Tensor<S, E, D, T> { | ||
/// See [atan] | ||
pub fn atan(self) -> Self { | ||
self.try_atan().unwrap() | ||
} | ||
/// See [atan] | ||
pub fn try_atan(self) -> Result<Self, Error> { | ||
try_unary_op(AtanKernelOp, self) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::prelude::storage_traits::AsArray; | ||
use crate::tests::*; | ||
use crate::{tensor::*, tensor_ops::*}; | ||
|
||
#[test] | ||
fn test_atan() { | ||
let dev: TestDevice = Default::default(); | ||
let x = dev.tensor([-2.0, -1.0]).to_dtype::<TestDtype>(); | ||
|
||
let expected_dx_atanx = [0.2, 0.5]; | ||
let mut a = [0.0, 0.0]; | ||
for i in 0..2 { | ||
let r = x.clone().leaky_trace().atan(); | ||
let g = r.select(dev.tensor(i)).backward(); | ||
let rr = g.get(&x); | ||
a[i] = rr[[i]]; | ||
} | ||
assert_close_to_literal!(dev.tensor(a), expected_dx_atanx); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use crate::tensor_ops::cpu_kernels::{BinaryDerivative}; | ||
use num_traits::Float; | ||
|
||
impl<F: Float> BinaryDerivative<F> for super::BinaryAtan2KernelOp { | ||
const HAS_CONST_DF: bool = false; | ||
#[inline(always)] | ||
fn f(&self, &x: &F, &y: &F) -> F { | ||
x.atan2(y) | ||
} | ||
#[inline(always)] | ||
fn dfdx(&self, x: &F, y: &F) -> F { | ||
-(*x)/((*x)*(*x) + (*y)*(*y)) | ||
} | ||
|
||
#[inline(always)] | ||
fn dfdy(&self, x: &F, y: &F) -> F { | ||
*y/((*x)*(*x) + (*y)*(*y)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
mod cpu_kernel; | ||
|
||
#[cfg(feature = "cuda")] | ||
mod cuda_kernel; | ||
|
||
use super::ops::*; | ||
use crate::{shapes::*, tensor::*}; | ||
|
||
#[repr(C)] | ||
#[derive(Debug, Default, Clone, Copy)] | ||
pub struct BinaryAtan2KernelOp; | ||
|
||
|
||
pub fn atan2<S: Shape, E: Dtype, D, T: Tape<E, D> + Merge<R>, R: Default>( | ||
lhs: Tensor<S, E, D, T>, | ||
rhs: Tensor<S, E, D, R>, | ||
) -> Tensor<S, E, D, T> | ||
where | ||
D: BinaryKernel<BinaryAtan2KernelOp, E>, | ||
{ | ||
lhs.try_atan2(rhs).unwrap() | ||
} | ||
|
||
|
||
// pub fn add<S: Shape, E: Dtype, D, T: Tape<E, D> + Merge<R>, R: Default>( | ||
// lhs: Tensor<S, E, D, T>, | ||
// rhs: Tensor<S, E, D, R>, | ||
// ) -> Tensor<S, E, D, T> | ||
// where | ||
// D: BinaryKernel<BinaryAddKernelOp, E>, | ||
// { | ||
// lhs + rhs | ||
// } | ||
|
||
/// Fallible version of [std::ops::Add]. See [add] | ||
pub trait TryAtan2<Rhs = Self> { | ||
type Output; | ||
|
||
fn try_atan2(self, rhs: Rhs) -> Result<Self::Output, Error>; | ||
} | ||
|
||
impl<S: Shape, E: Dtype, D, LhsTape: Tape<E, D>, R> TryAtan2<Tensor<S, E, D, R>> | ||
for Tensor<S, E, D, LhsTape> | ||
where | ||
D: BinaryKernel<BinaryAtan2KernelOp, E>, | ||
LhsTape: Merge<R>, | ||
{ | ||
type Output = Self; | ||
|
||
/// See [add] | ||
fn try_atan2(self, rhs: Tensor<S, E, D, R>) -> Result<Self, Error> { | ||
try_binary_op(BinaryAtan2KernelOp, self, rhs) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters