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 SqrtKernelOp;
pub fn sqrt<S: Shape, E: Dtype, D: UnaryKernel<SqrtKernelOp, E>, T: Tape<E, D>>(
t: Tensor<S, E, D, T>,
) -> Tensor<S, E, D, T> {
t.sqrt()
}
impl<S: Shape, E: Dtype, D: UnaryKernel<SqrtKernelOp, E>, T: Tape<E, D>> Tensor<S, E, D, T> {
pub fn sqrt(self) -> Self {
self.try_sqrt().unwrap()
}
pub fn try_sqrt(self) -> Result<Self, D::Err> {
try_unary_op(SqrtKernelOp, self)
}
}
#[cfg(test)]
mod tests {
use crate::{tensor::*, tensor_ops::*, tests::*};
#[test]
fn test_sqrt() {
let dev: TestDevice = Default::default();
let x = dev.tensor([-1.0, 0.0, 1.0, 4.0]).to_dtype::<TestDtype>();
let r = x.leaky_trace().sqrt();
let r_array = r.array();
assert!(r_array[0].is_nan());
assert_eq!(
&r_array[1..],
[0.0, 1.0, 2.0]
.map(NumCast::from)
.map(Option::<TestDtype>::unwrap)
);
let g = r.mean().backward();
let g = g.get(&x).array();
assert!(g[0].is_nan());
assert_eq!(
&g[1..],
[f64::INFINITY, 0.5 / 4.0, 0.25 / 4.0]
.map(NumCast::from)
.map(Option::<TestDtype>::unwrap)
);
}
}