diff --git a/src/lib.rs b/src/lib.rs index 7d6b152..9142854 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,22 +45,17 @@ //! // Usually you would not use CUDA but let Collenchyma pick what is available on the machine. //! let backend = Backend::::default().unwrap(); //! // Initialize two SharedTensors. -//! let mut x = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); -//! let mut result = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); +//! let mut x = SharedTensor::::new(&(1, 1, 3)); +//! let mut result = SharedTensor::::new(&(1, 1, 3)); //! // Fill `x` with some data. //! let payload: &[f32] = &::std::iter::repeat(1f32).take(x.capacity()).collect::>(); //! let native = Native::new(); //! let cpu = native.new_device(native.hardwares()).unwrap(); -//! x.add_device(&cpu).unwrap(); // Add native host memory -//! x.sync(&cpu).unwrap(); // Sync to native host memory -//! write_to_memory(x.get_mut(&cpu).unwrap(), payload); // Write to native host memory. -//! x.sync(backend.device()).unwrap(); // Sync the data to the CUDA device. +//! write_to_memory(x.write_only(&cpu).unwrap(), payload); // Write to native host memory. //! // Run the sigmoid operation, provided by the NN Plugin, on your CUDA enabled GPU. //! backend.sigmoid(&mut x, &mut result).unwrap(); //! // See the result. -//! result.add_device(&cpu).unwrap(); // Add native host memory -//! result.sync(&cpu).unwrap(); // Sync the result to host memory. -//! println!("{:?}", result.get(&cpu).unwrap().as_native().unwrap().as_slice::()); +//! println!("{:?}", result.read(&cpu).unwrap().as_native().unwrap().as_slice::()); //! } //! # } //! # #[cfg(not(feature = "cuda"))] @@ -120,6 +115,9 @@ extern crate lazy_static; #[macro_use] extern crate log; +#[cfg(test)] +extern crate rand; + pub use plugin::*; mod plugin; diff --git a/src/tests/activation.rs b/src/tests/activation.rs new file mode 100644 index 0000000..9af1323 --- /dev/null +++ b/src/tests/activation.rs @@ -0,0 +1,206 @@ +// Code for relu, sigmoid and tanh is mostly the same, but ATM I can't get +// how to abstract it better. Generic function wouldn't be shorter... Macros +// would be, but they would have to accept ~15 parameters and that is qute +// evil by itself and they'd add another level of indirection. Not nice. +use std::fmt; + +use co::prelude::*; +use co::plugin::numeric_helpers::Float; + +use plugin::{Relu, ReluPointwise, Sigmoid, SigmoidPointwise, Tanh, TanhPointwise}; +use tests::{Epsilon, filled_tensor, tensor_assert_eq}; + +//----------------------------------------------------------- relu + +const RELU_DIMS: [usize; 3] = [1, 2, 2]; +const RELU_IN: [f64; 4] = [-1.0, 1.0, 8.25, -3.3]; +const RELU_OUT: [f64; 4] = [ 0.0, 1.0, 8.25, 0.0]; +const RELU_OUT_GRAD: [f64; 4] = [ 4.6, 2.5, 3.5, 7.5]; // gradient at output +const RELU_IN_GRAD: [f64; 4] = [ 0.0, 2.5, 3.5, 0.0]; // backpropagated to input + + +pub fn test_relu(backend: Backend) + where T: Float + Epsilon + fmt::Debug, + Backend: Relu + IBackend { + + let x = filled_tensor(&RELU_DIMS, &RELU_IN); + let mut r = SharedTensor::::new(&RELU_DIMS); + + backend.relu(&x, &mut r).unwrap(); + tensor_assert_eq(&r, &RELU_OUT, 3.0); +} + +pub fn test_relu_grad(backend: Backend) + where T: Float + Epsilon + fmt::Debug, + Backend: Relu + IBackend { + + let x = filled_tensor(&RELU_DIMS, &RELU_OUT); + let dx = filled_tensor(&RELU_DIMS, &RELU_OUT_GRAD); + let r = filled_tensor(&RELU_DIMS, &RELU_IN); + let mut dr = SharedTensor::new(&RELU_DIMS); + + backend.relu_grad(&x, &dx, &r, &mut dr).unwrap(); + tensor_assert_eq(&dr, &RELU_IN_GRAD, 3.0); +} + +pub fn test_relu_pointwise(backend: Backend) + where T: Float + fmt::Debug + Epsilon, + Backend: ReluPointwise + IBackend { + + let mut x = filled_tensor(&RELU_DIMS, &RELU_IN); + backend.relu_pointwise(&mut x).unwrap(); + tensor_assert_eq(&x, &RELU_OUT, 3.0); +} + +pub fn test_relu_pointwise_grad(backend: Backend) + where T: Float + fmt::Debug + Epsilon, + Backend: ReluPointwise + IBackend { + let x = filled_tensor(&RELU_DIMS, &RELU_OUT); + let mut dx = filled_tensor(&RELU_DIMS, &RELU_OUT_GRAD); + backend.relu_pointwise_grad(&x, &mut dx).unwrap(); + tensor_assert_eq(&dx, &RELU_IN_GRAD, 3.0); +} + +//----------------------------------------------------------- sigmoid + +const SIGMOID_DIMS: [usize; 2] = [1, 3]; +const SIGMOID_IN: [f64; 3] = [-1.8301, 2.0, 8.33]; +const SIGMOID_OUT: [f64; 3] = [0.13822636075814926, + 0.8807970779778823, + 0.9997588861050526]; +const SIGMOID_OUT_GRAD: [f64; 3] = [3.1, -4.93, 1.239]; // gradient at output +const SIGMOID_IN_GRAD: [f64; 3] = [0.3692714852440924, // backpropagated to input + -0.5176183760392876, + 0.00029866808544693255]; + +pub fn test_sigmoid(backend: Backend) + where T: Float + Epsilon + fmt::Debug, + Backend: Sigmoid + IBackend { + + let x = filled_tensor(&SIGMOID_DIMS, &SIGMOID_IN); + let mut r = SharedTensor::::new(&SIGMOID_DIMS); + + backend.sigmoid(&x, &mut r).unwrap(); + tensor_assert_eq(&r, &SIGMOID_OUT, 3.0); +} + +pub fn test_sigmoid_grad(backend: Backend) + where T: Float + Epsilon + fmt::Debug, + Backend: Sigmoid + IBackend { + + let x = filled_tensor(&SIGMOID_DIMS, &SIGMOID_OUT); + let dx = filled_tensor(&SIGMOID_DIMS, &SIGMOID_OUT_GRAD); + let r = filled_tensor(&SIGMOID_DIMS, &SIGMOID_IN); + let mut dr = SharedTensor::new(&SIGMOID_DIMS); + + backend.sigmoid_grad(&x, &dx, &r, &mut dr).unwrap(); + tensor_assert_eq(&dr, &SIGMOID_IN_GRAD, 3.0); +} + +pub fn test_sigmoid_pointwise(backend: Backend) + where T: Float + fmt::Debug + Epsilon, + Backend: SigmoidPointwise + IBackend { + + let mut x = filled_tensor(&SIGMOID_DIMS, &SIGMOID_IN); + backend.sigmoid_pointwise(&mut x).unwrap(); + tensor_assert_eq(&x, &SIGMOID_OUT, 3.0); +} + +pub fn test_sigmoid_pointwise_grad(backend: Backend) + where T: Float + fmt::Debug + Epsilon, + Backend: SigmoidPointwise + IBackend { + let x = filled_tensor(&SIGMOID_DIMS, &SIGMOID_OUT); + let mut dx = filled_tensor(&SIGMOID_DIMS, &SIGMOID_OUT_GRAD); + backend.sigmoid_pointwise_grad(&x, &mut dx).unwrap(); + tensor_assert_eq(&dx, &SIGMOID_IN_GRAD, 3.0); +} + +//----------------------------------------------------------- sigmoid + +const TANH_DIMS: [usize; 2] = [2, 2]; +const TANH_IN: [f64; 4] = [-1.9334, 0.23, 2.998, -0.9]; +const TANH_OUT: [f64; 4] = [-0.9590073344404966, + 0.22602835227867096, + 0.9950349822915199, + -0.7162978701990245]; +const TANH_OUT_GRAD: [f64; 4] = [1.7, 8.1, -4.33, -9.33]; +const TANH_IN_GRAD: [f64; 4] = [0.13651838523186707, + 7.68618059012613, + -0.04289031278977681, + -4.542938979514026]; + +pub fn test_tanh(backend: Backend) + where T: Float + Epsilon + fmt::Debug, + Backend: Tanh + IBackend { + + let x = filled_tensor(&TANH_DIMS, &TANH_IN); + let mut r = SharedTensor::::new(&TANH_DIMS); + + backend.tanh(&x, &mut r).unwrap(); + tensor_assert_eq(&r, &TANH_OUT, 3.0); +} + +pub fn test_tanh_grad(backend: Backend) + where T: Float + Epsilon + fmt::Debug, + Backend: Tanh + IBackend { + + let x = filled_tensor(&TANH_DIMS, &TANH_OUT); + let dx = filled_tensor(&TANH_DIMS, &TANH_OUT_GRAD); + let r = filled_tensor(&TANH_DIMS, &TANH_IN); + let mut dr = SharedTensor::new(&TANH_DIMS); + + backend.tanh_grad(&x, &dx, &r, &mut dr).unwrap(); + tensor_assert_eq(&dr, &TANH_IN_GRAD, 10.0); +} + +pub fn test_tanh_pointwise(backend: Backend) + where T: Float + fmt::Debug + Epsilon, + Backend: TanhPointwise + IBackend { + + let mut x = filled_tensor(&TANH_DIMS, &TANH_IN); + backend.tanh_pointwise(&mut x).unwrap(); + tensor_assert_eq(&x, &TANH_OUT, 3.0); +} + +pub fn test_tanh_pointwise_grad(backend: Backend) + where T: Float + fmt::Debug + Epsilon, + Backend: TanhPointwise + IBackend { + let x = filled_tensor(&TANH_DIMS, &TANH_OUT); + let mut dx = filled_tensor(&TANH_DIMS, &TANH_OUT_GRAD); + backend.tanh_pointwise_grad(&x, &mut dx).unwrap(); + tensor_assert_eq(&dx, &TANH_IN_GRAD, 10.0); +} + + +mod native { + use super::*; + test_native!(test_relu, relu_f32, relu_f64); + test_native!(test_relu_grad, relu_grad_f32, relu_grad_f64); + + test_native!(test_sigmoid, sigmoid_f32, sigmoid_f64); + test_native!(test_sigmoid_grad, sigmoid_grad_f32, sigmoid_grad_f64); + + test_native!(test_tanh, tanh_f32, tanh_f64); + test_native!(test_tanh_grad, tanh_grad_f32, tanh_grad_f64); +} + +mod cuda { + use super::*; + test_cuda!(test_relu, relu_f32, relu_f64); + test_cuda!(test_relu_grad, relu_grad_f32, relu_grad_f64); + test_cuda!(test_relu_pointwise, relu_pointwise_f32, relu_pointwise_f64); + test_cuda!(test_relu_pointwise_grad, + relu_pointwise_grad_f32, relu_pointwise_grad_f64); + + test_cuda!(test_sigmoid, sigmoid_f32, sigmoid_f64); + test_cuda!(test_sigmoid_grad, sigmoid_grad_f32, sigmoid_grad_f64); + test_cuda!(test_sigmoid_pointwise, sigmoid_pointwise_f32, sigmoid_pointwise_f64); + test_cuda!(test_sigmoid_pointwise_grad, + sigmoid_pointwise_grad_f32, sigmoid_pointwise_grad_f64); + + test_cuda!(test_tanh, tanh_f32, tanh_f64); + test_cuda!(test_tanh_grad, tanh_grad_f32, tanh_grad_f64); + test_cuda!(test_tanh_pointwise, tanh_pointwise_f32, tanh_pointwise_f64); + test_cuda!(test_tanh_pointwise_grad, + tanh_pointwise_grad_f32, tanh_pointwise_grad_f64); +} diff --git a/src/tests/mod.rs b/src/tests/mod.rs new file mode 100644 index 0000000..6d1a878 --- /dev/null +++ b/src/tests/mod.rs @@ -0,0 +1,129 @@ +use std; +use std::fmt; + +use rand::thread_rng; +use rand::distributions::{range, IndependentSample, Range}; + +use co::prelude::*; +use co::plugin::numeric_helpers::{cast, NumCast}; + +pub trait Epsilon { + fn epsilon() -> Self; +} + +impl Epsilon for f32 { + fn epsilon() -> Self { std::f32::EPSILON } +} + +impl Epsilon for f64 { + fn epsilon() -> Self { std::f64::EPSILON } +} + + +#[cfg(feature = "native")] +fn get_native_backend() -> Backend { + Backend::::default().unwrap() +} + +#[cfg(feature = "cuda")] +fn get_cuda_backend() -> Backend { + Backend::::default().unwrap() +} + +#[cfg(feature = "native")] +pub fn write_to_tensor(xs: &mut SharedTensor, data: &[f64]) + where T: ::std::marker::Copy + NumCast { + + assert_eq!(xs.desc().size(), data.len()); + + let native = get_native_backend(); + let mem = xs.write_only(native.device()).unwrap().as_mut_native().unwrap(); + let mut mem_buffer = mem.as_mut_slice::(); + for (i, x) in data.iter().enumerate() { + mem_buffer[i] = cast::<_, T>(*x).unwrap(); + } +} + +#[cfg(feature = "native")] +pub fn filled_tensor(dims: &[usize], data: &[f64]) -> SharedTensor + where T: ::std::marker::Copy + NumCast { + + let mut x = SharedTensor::new(&dims); + write_to_tensor(&mut x, data); + x +} + +#[cfg(feature = "native")] +pub fn uniform_random_fill(xs: &mut SharedTensor, low: T, high: T) + where T: Copy + PartialEq + PartialOrd + + fmt::Debug + NumCast + range::SampleRange { + + let native = get_native_backend(); + let mut mem = xs.write_only(native.device()).unwrap().as_mut_native().unwrap(); + let mem_slice = mem.as_mut_slice::(); + + let mut rng = thread_rng(); + let distr = Range::new(low, high); + for x in mem_slice { + *x = distr.ind_sample(&mut rng); + } +} + +#[cfg(feature = "native")] +pub fn tensor_assert_eq(xs: &SharedTensor, data: &[f64], epsilon_mul: f64) + where T: Copy + fmt::Debug + PartialEq + NumCast + Epsilon { + + let e = cast::<_, f64>(T::epsilon()).unwrap() * epsilon_mul; + let native = get_native_backend(); + let mem = xs.read(native.device()).unwrap().as_native().unwrap(); + let mem_slice = mem.as_slice::(); + + assert_eq!(mem_slice.len(), data.len()); + for (x1, x2) in mem_slice.iter().zip(data.iter()) { + let x1_t = cast::<_, f64>(*x1).unwrap(); + if (x1_t - x2).abs() > e * (x1_t.abs() + x2.abs()) * 0.5 { + println!("Results differ: {:?} != {:?}", mem_slice, data); + assert!(false); + } + } +} + +// All operations for Cuda and Native are provided for f32 and f64. +// Those macros remove boilerplate in test definitions. +// concat_idents! is behind feature gate at the moment, otherwise +// invocations could be made much less verbose. +macro_rules! test_cuda { + ($test_name:ident, $f32_name:ident, $f64_name:ident) => { + #[cfg(feature = "cuda")] + #[test] + fn $f32_name() { + $test_name::(::tests::get_cuda_backend()) + } + + #[cfg(feature = "cuda")] + #[test] + fn $f64_name() { + $test_name::(::tests::get_cuda_backend()) + } + } +} + +macro_rules! test_native { + ($test_name:ident, $f32_name:ident, $f64_name:ident) => { + #[cfg(feature = "native")] + #[test] + fn $f32_name() { + $test_name::(::tests::get_native_backend()) + } + + #[cfg(feature = "native")] + #[test] + fn $f64_name() { + $test_name::(::tests::get_native_backend()) + } + } +} + +mod activation; +mod softmax; + diff --git a/src/tests/relu_pointwise_specs.rs b/src/tests/relu_pointwise_specs.rs deleted file mode 100644 index 7d244bc..0000000 --- a/src/tests/relu_pointwise_specs.rs +++ /dev/null @@ -1,374 +0,0 @@ -extern crate collenchyma_nn as co_nn; -extern crate collenchyma as co; - -#[cfg(test)] -#[cfg(feature = "cuda")] -mod relu_pointwise_spec_cuda{ - - use co::prelude::*; - use co_nn::*; - use co::plugin::numeric_helpers::{cast, Float}; - - fn get_native_backend() -> Backend { - Backend::::default().unwrap() - } - - fn get_cuda_backend() -> Backend { - Backend::::default().unwrap() - } - - fn write_to_memory(mem: &mut MemoryType, data: &[T]) { - match mem { - &mut MemoryType::Native(ref mut mem) => { - let mut mem_buffer = mem.as_mut_slice::(); - for (index, datum) in data.iter().enumerate() { - mem_buffer[index] = *datum; - } - }, - #[cfg(any(feature = "opencl", feature = "cuda"))] - _ => {} - } - } - - fn get_memory(backend: &Backend, native: &Backend) -> SharedTensor{ - let val = cast::(1f64).unwrap(); - let val2 = cast::(2f64).unwrap(); - let mut x = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - x.add_device(native.device()).unwrap(); - x.sync(native.device()).unwrap(); - write_to_memory(x.get_mut(native.device()).unwrap(), &[val, val, val2]); - x.sync(backend.device()).unwrap(); - - x - } - - fn get_grad_memory(backend: &Backend, native: &Backend) -> (SharedTensor, SharedTensor){ - let val = cast::(1f64).unwrap(); - let val2 = cast::(2f64).unwrap(); - let mut x = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - x.add_device(native.device()).unwrap(); - x.sync(native.device()).unwrap(); - write_to_memory(x.get_mut(native.device()).unwrap(), &[val, val, val2]); - x.sync(backend.device()).unwrap(); - - let mut x_diff = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - x_diff.add_device(native.device()).unwrap(); - x_diff.sync(native.device()).unwrap(); - write_to_memory(x_diff.get_mut(native.device()).unwrap(), &[val, val, val2]); - x_diff.sync(backend.device()).unwrap(); - - (x, x_diff) - } - - #[test] - fn it_computes_correct_relu_on_cuda_for_f32() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let mut x = get_memory::(&backend, &native); - - match backend.relu_pointwise(&mut x) { - Ok(_) => { - x.sync(native.device()).unwrap(); - if let Some(mem) = x.get(native.device()).unwrap().as_native() { - assert_eq!(&[1f32, 1f32, 2f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_relu_on_cuda_for_f64() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let mut x = get_memory::(&backend, &native); - - match backend.relu_pointwise(&mut x) { - Ok(_) => { - x.sync(native.device()).unwrap(); - if let Some(mem) = x.get(native.device()).unwrap().as_native() { - assert_eq!(&[1f64, 1f64, 2f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_relu_on_cuda_for_f32_plain() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let mut x = get_memory::(&backend, &native); - - match backend.relu_pointwise_plain(&mut x) { - Ok(_) => { - x.sync(native.device()).unwrap(); - if let Some(mem) = x.get(native.device()).unwrap().as_native() { - assert_eq!(&[1f32, 1f32, 2f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_relu_on_cuda_for_f64_plain() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let mut x = get_memory::(&backend, &native); - - match backend.relu_pointwise_plain(&mut x) { - Ok(_) => { - x.sync(native.device()).unwrap(); - if let Some(mem) = x.get(native.device()).unwrap().as_native() { - assert_eq!(&[1f64, 1f64, 2f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_relu_grad_on_cuda_for_f32() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut x_diff) = get_grad_memory::(&backend, &native); - - match backend.relu_pointwise_grad(&mut x, &mut x_diff) { - Ok(_) => { - x_diff.sync(native.device()).unwrap(); - if let Some(mem) = x_diff.get(native.device()).unwrap().as_native() { - assert_eq!(&[1f32, 1f32, 2f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_relu_grad_on_cuda_for_f64() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut x_diff) = get_grad_memory::(&backend, &native); - - match backend.relu_pointwise_grad(&mut x, &mut x_diff) { - Ok(_) => { - x_diff.sync(native.device()).unwrap(); - if let Some(mem) = x_diff.get(native.device()).unwrap().as_native() { - assert_eq!(&[1f64, 1f64, 2f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_relu_grad_on_cuda_for_f32_plain() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut x_diff) = get_grad_memory::(&backend, &native); - - match backend.relu_pointwise_grad_plain(&mut x, &mut x_diff) { - Ok(_) => { - x_diff.sync(native.device()).unwrap(); - if let Some(mem) = x_diff.get(native.device()).unwrap().as_native() { - assert_eq!(&[1f32, 1f32, 2f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_relu_grad_on_cuda_for_f64_plain() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut x_diff) = get_grad_memory::(&backend, &native); - - match backend.relu_pointwise_grad_plain(&mut x, &mut x_diff) { - Ok(_) => { - x_diff.sync(native.device()).unwrap(); - if let Some(mem) = x_diff.get(native.device()).unwrap().as_native() { - assert_eq!(&[1f64, 1f64, 2f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } -} - -#[cfg(test)] -#[cfg(feature = "native")] -mod relu_pointwise_spec_native { - - // use co::prelude::*; - // use co_nn::*; - // use co::plugin::numeric_helpers::{cast, Float}; - // - // fn get_native_backend() -> Backend { - // Backend::::default().unwrap() - // } - // - // fn write_to_memory(mem: &mut MemoryType, data: &[T]) { - // match mem { - // &mut MemoryType::Native(ref mut mem) => { - // let mut mem_buffer = mem.as_mut_slice::(); - // for (index, datum) in data.iter().enumerate() { - // mem_buffer[index] = *datum; - // } - // }, - // #[cfg(any(feature = "opencl", feature = "cuda"))] - // _ => {} - // } - // } - // - // fn get_memory(backend: &Backend) -> (SharedTensor, SharedTensor){ - // let val = cast::(1f64).unwrap(); - // let val2 = cast::(2f64).unwrap(); - // let mut x = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - // write_to_memory(x.get_mut(backend.device()).unwrap(), &[val, val, val2]); - // - // let result = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - // - // (x, result) - // } - // - // fn get_grad_memory(backend: &Backend) -> (SharedTensor, SharedTensor, SharedTensor, SharedTensor){ - // let val = cast::(1f64).unwrap(); - // let val2 = cast::(2f64).unwrap(); - // let mut x = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - // write_to_memory(x.get_mut(backend.device()).unwrap(), &[val, val, val2]); - // - // let mut x_diff = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - // write_to_memory(x_diff.get_mut(backend.device()).unwrap(), &[val, val, val2]); - // - // let mut result = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - // write_to_memory(result.get_mut(backend.device()).unwrap(), &[val, val, val2]); - // - // let result_diff = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - // - // (x, x_diff, result, result_diff) - // } - // - // #[test] - // fn it_computes_correct_relu_on_native_for_f32() { - // let backend = get_native_backend(); - // let (mut x, mut result) = get_memory::(&backend); - // - // match backend.relu(&mut x, &mut result) { - // Ok(_) => { - // if let Some(mem) = result.get(backend.device()).unwrap().as_native() { - // assert_eq!(&[0.7310585786f32, 0.7310586f32, 0.880797f32], mem.as_slice::()); - // } else { - // println!("No result: {:?}", result); assert!(false); - // } - // }, - // Err(err) => { println!("{:?}", err); assert!(false) } - // } - // } - // - // #[test] - // fn it_computes_correct_relu_on_native_for_f64() { - // let backend = get_native_backend(); - // let (mut x, mut result) = get_memory::(&backend); - // - // match backend.relu(&mut x, &mut result) { - // Ok(_) => { - // if let Some(mem) = result.get(backend.device()).unwrap().as_native() { - // assert_eq!(&[0.7310585786300049f64, 0.7310585786300049f64, 0.8807970779778823f64], mem.as_slice::()); - // } - // }, - // Err(err) => { println!("{:?}", err); assert!(false) } - // } - // } - // - // #[test] - // fn it_computes_correct_relu_on_native_for_f32_plain() { - // let backend = get_native_backend(); - // let (mut x, mut result) = get_memory::(&backend); - // - // match backend.relu_plain(&mut x, &mut result) { - // Ok(_) => { - // if let Some(mem) = result.get(backend.device()).unwrap().as_native() { - // assert_eq!(&[0.7310585786f32, 0.7310586f32, 0.880797f32], mem.as_slice::()); - // } - // }, - // Err(err) => { println!("{:?}", err); assert!(false) } - // } - // } - // - // #[test] - // fn it_computes_correct_relu_on_native_for_f64_plain() { - // let backend = get_native_backend(); - // let (mut x, mut result) = get_memory::(&backend); - // - // match backend.relu_plain(&mut x, &mut result) { - // Ok(_) => { - // if let Some(mem) = result.get(backend.device()).unwrap().as_native() { - // assert_eq!(&[0.7310585786300049f64, 0.7310585786300049f64, 0.8807970779778823f64], mem.as_slice::()); - // } - // }, - // Err(err) => { println!("{:?}", err); assert!(false) } - // } - // } - // - // #[test] - // fn it_computes_correct_relu_grad_on_native_for_f32() { - // let backend = get_native_backend(); - // let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend); - // - // match backend.relu_grad(&mut x, &mut x_diff, &mut result, &mut result_diff) { - // Ok(_) => { - // if let Some(mem) = result_diff.get(backend.device()).unwrap().as_native() { - // assert_eq!(&[0f32, 0f32, -4f32], mem.as_slice::()); - // } - // }, - // Err(err) => { println!("{:?}", err); assert!(false) } - // } - // } - // - // #[test] - // fn it_computes_correct_relu_grad_on_native_for_f64() { - // let backend = get_native_backend(); - // let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend); - // - // match backend.relu_grad(&mut x, &mut x_diff, &mut result, &mut result_diff) { - // Ok(_) => { - // if let Some(mem) = result_diff.get(backend.device()).unwrap().as_native() { - // assert_eq!(&[0f64, 0f64, -4f64], mem.as_slice::()); - // } - // }, - // Err(err) => { println!("{:?}", err); assert!(false) } - // } - // } - // - // #[test] - // fn it_computes_correct_relu_grad_on_native_for_f32_plain() { - // let backend = get_native_backend(); - // let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend); - // - // match backend.relu_grad_plain(&mut x, &mut x_diff, &mut result, &mut result_diff) { - // Ok(_) => { - // if let Some(mem) = result_diff.get(backend.device()).unwrap().as_native() { - // assert_eq!(&[0f32, 0f32, -4f32], mem.as_slice::()); - // } - // }, - // Err(err) => { println!("{:?}", err); assert!(false) } - // } - // } - // - // #[test] - // fn it_computes_correct_relu_grad_on_native_for_f64_plain() { - // let backend = get_native_backend(); - // let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend); - // - // match backend.relu_grad_plain(&mut x, &mut x_diff, &mut result, &mut result_diff) { - // Ok(_) => { - // if let Some(mem) = result_diff.get(backend.device()).unwrap().as_native() { - // assert_eq!(&[0f64, 0f64, -4f64], mem.as_slice::()); - // } - // }, - // Err(err) => { println!("{:?}", err); assert!(false) } - // } - // } -} diff --git a/src/tests/relu_specs.rs b/src/tests/relu_specs.rs deleted file mode 100644 index 81bef1a..0000000 --- a/src/tests/relu_specs.rs +++ /dev/null @@ -1,384 +0,0 @@ -extern crate collenchyma_nn as co_nn; -extern crate collenchyma as co; - -#[cfg(test)] -#[cfg(feature = "cuda")] -mod relu_spec_cuda { - - use co::prelude::*; - use co_nn::*; - use co::plugin::numeric_helpers::{cast, Float}; - - fn get_native_backend() -> Backend { - Backend::::default().unwrap() - } - - fn get_cuda_backend() -> Backend { - Backend::::default().unwrap() - } - - fn write_to_memory(mem: &mut MemoryType, data: &[T]) { - match mem { - &mut MemoryType::Native(ref mut mem) => { - let mut mem_buffer = mem.as_mut_slice::(); - for (index, datum) in data.iter().enumerate() { - mem_buffer[index] = *datum; - } - }, - #[cfg(any(feature = "opencl", feature = "cuda"))] - _ => {} - } - } - - fn get_memory(backend: &Backend, native: &Backend) -> (SharedTensor, SharedTensor){ - let val = cast::(1f64).unwrap(); - let val2 = cast::(2f64).unwrap(); - let mut x = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - x.add_device(native.device()).unwrap(); - x.sync(native.device()).unwrap(); - write_to_memory(x.get_mut(native.device()).unwrap(), &[val, val, val2]); - x.sync(backend.device()).unwrap(); - - let mut result = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - result.add_device(native.device()).unwrap(); - - (x, result) - } - - fn get_grad_memory(backend: &Backend, native: &Backend) -> (SharedTensor, SharedTensor, SharedTensor, SharedTensor){ - let val = cast::(1f64).unwrap(); - let val2 = cast::(2f64).unwrap(); - let mut x = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - x.add_device(native.device()).unwrap(); - x.sync(native.device()).unwrap(); - write_to_memory(x.get_mut(native.device()).unwrap(), &[val, val, val2]); - x.sync(backend.device()).unwrap(); - - let mut x_diff = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - x_diff.add_device(native.device()).unwrap(); - x_diff.sync(native.device()).unwrap(); - write_to_memory(x_diff.get_mut(native.device()).unwrap(), &[val, val, val2]); - x_diff.sync(backend.device()).unwrap(); - - let mut result = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - result.add_device(native.device()).unwrap(); - result.sync(native.device()).unwrap(); - write_to_memory(result.get_mut(native.device()).unwrap(), &[val, val, val2]); - result.sync(backend.device()).unwrap(); - - let mut result_diff = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - result_diff.add_device(native.device()).unwrap(); - - (x, x_diff, result, result_diff) - } - - #[test] - fn it_computes_correct_relu_on_cuda_for_f32() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend, &native); - - match backend.relu(&mut x, &mut result) { - Ok(_) => { - result.sync(native.device()).unwrap(); - if let Some(mem) = result.get(native.device()).unwrap().as_native() { - assert_eq!(&[1f32, 1f32, 2f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_relu_on_cuda_for_f64() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend, &native); - - match backend.relu(&mut x, &mut result) { - Ok(_) => { - result.sync(native.device()).unwrap(); - if let Some(mem) = result.get(native.device()).unwrap().as_native() { - assert_eq!(&[1f64, 1f64, 2f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_relu_on_cuda_for_f32_plain() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend, &native); - - match backend.relu_plain(&mut x, &mut result) { - Ok(_) => { - result.sync(native.device()).unwrap(); - if let Some(mem) = result.get(native.device()).unwrap().as_native() { - assert_eq!(&[1f32, 1f32, 2f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_relu_on_cuda_for_f64_plain() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend, &native); - - match backend.relu_plain(&mut x, &mut result) { - Ok(_) => { - result.sync(native.device()).unwrap(); - if let Some(mem) = result.get(native.device()).unwrap().as_native() { - assert_eq!(&[1f64, 1f64, 2f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_relu_grad_on_cuda_for_f32() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend, &native); - - match backend.relu_grad(&mut x, &mut x_diff, &mut result, &mut result_diff) { - Ok(_) => { - result_diff.sync(native.device()).unwrap(); - if let Some(mem) = result_diff.get(native.device()).unwrap().as_native() { - assert_eq!(&[1f32, 1f32, 2f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_relu_grad_on_cuda_for_f64() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend, &native); - - match backend.relu_grad(&mut x, &mut x_diff, &mut result, &mut result_diff) { - Ok(_) => { - result_diff.sync(native.device()).unwrap(); - if let Some(mem) = result_diff.get(native.device()).unwrap().as_native() { - assert_eq!(&[1f64, 1f64, 2f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_relu_grad_on_cuda_for_f32_plain() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend, &native); - - match backend.relu_grad_plain(&mut x, &mut x_diff, &mut result, &mut result_diff) { - Ok(_) => { - result_diff.sync(native.device()).unwrap(); - if let Some(mem) = result_diff.get(native.device()).unwrap().as_native() { - assert_eq!(&[1f32, 1f32, 2f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_relu_grad_on_cuda_for_f64_plain() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend, &native); - - match backend.relu_grad_plain(&mut x, &mut x_diff, &mut result, &mut result_diff) { - Ok(_) => { - result_diff.sync(native.device()).unwrap(); - if let Some(mem) = result_diff.get(native.device()).unwrap().as_native() { - assert_eq!(&[1f64, 1f64, 2f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } -} - -#[cfg(test)] -#[cfg(feature = "native")] -mod relu_spec_native { - - use co::prelude::*; - use co_nn::*; - use co::plugin::numeric_helpers::{cast, Float}; - - fn get_native_backend() -> Backend { - Backend::::default().unwrap() - } - - fn write_to_memory(mem: &mut MemoryType, data: &[T]) { - match mem { - &mut MemoryType::Native(ref mut mem) => { - let mut mem_buffer = mem.as_mut_slice::(); - for (index, datum) in data.iter().enumerate() { - mem_buffer[index] = *datum; - } - }, - #[cfg(any(feature = "opencl", feature = "cuda"))] - _ => {} - } - } - - fn get_memory(backend: &Backend) -> (SharedTensor, SharedTensor){ - let val = cast::(1f64).unwrap(); - let val2 = cast::(2f64).unwrap(); - let mut x = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - write_to_memory(x.get_mut(backend.device()).unwrap(), &[val, val, val2]); - - let result = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - - (x, result) - } - - fn get_grad_memory(backend: &Backend) -> (SharedTensor, SharedTensor, SharedTensor, SharedTensor){ - let val = cast::(1f64).unwrap(); - let val2 = cast::(2f64).unwrap(); - let mut x = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - write_to_memory(x.get_mut(backend.device()).unwrap(), &[val, val, val2]); - - let mut x_diff = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - write_to_memory(x_diff.get_mut(backend.device()).unwrap(), &[val, val, val2]); - - let mut result = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - write_to_memory(result.get_mut(backend.device()).unwrap(), &[val, val, val2]); - - let result_diff = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - - (x, x_diff, result, result_diff) - } - - #[test] - fn it_computes_correct_relu_on_native_for_f32() { - let backend = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend); - - match backend.relu(&mut x, &mut result) { - Ok(_) => { - if let Some(mem) = result.get(backend.device()).unwrap().as_native() { - assert_eq!(&[1f32, 1f32, 2f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_relu_on_native_for_f64() { - let backend = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend); - - match backend.relu(&mut x, &mut result) { - Ok(_) => { - if let Some(mem) = result.get(backend.device()).unwrap().as_native() { - assert_eq!(&[1f64, 1f64, 2f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_relu_on_native_for_f32_plain() { - let backend = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend); - - match backend.relu_plain(&mut x, &mut result) { - Ok(_) => { - if let Some(mem) = result.get(backend.device()).unwrap().as_native() { - assert_eq!(&[1f32, 1f32, 2f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_relu_on_native_for_f64_plain() { - let backend = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend); - - match backend.relu_plain(&mut x, &mut result) { - Ok(_) => { - if let Some(mem) = result.get(backend.device()).unwrap().as_native() { - assert_eq!(&[1f64, 1f64, 2f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_relu_grad_on_native_for_f32() { - let backend = get_native_backend(); - let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend); - - match backend.relu_grad(&mut x, &mut x_diff, &mut result, &mut result_diff) { - Ok(_) => { - if let Some(mem) = result_diff.get(backend.device()).unwrap().as_native() { - assert_eq!(&[1f32, 1f32, 2f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_relu_grad_on_native_for_f64() { - let backend = get_native_backend(); - let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend); - - match backend.relu_grad(&mut x, &mut x_diff, &mut result, &mut result_diff) { - Ok(_) => { - if let Some(mem) = result_diff.get(backend.device()).unwrap().as_native() { - assert_eq!(&[1f64, 1f64, 2f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_relu_grad_on_native_for_f32_plain() { - let backend = get_native_backend(); - let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend); - - match backend.relu_grad_plain(&mut x, &mut x_diff, &mut result, &mut result_diff) { - Ok(_) => { - if let Some(mem) = result_diff.get(backend.device()).unwrap().as_native() { - assert_eq!(&[1f32, 1f32, 2f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_relu_grad_on_native_for_f64_plain() { - let backend = get_native_backend(); - let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend); - - match backend.relu_grad_plain(&mut x, &mut x_diff, &mut result, &mut result_diff) { - Ok(_) => { - if let Some(mem) = result_diff.get(backend.device()).unwrap().as_native() { - assert_eq!(&[1f64, 1f64, 2f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } -} diff --git a/src/tests/sigmoid_pointwise_specs.rs b/src/tests/sigmoid_pointwise_specs.rs deleted file mode 100644 index 832a096..0000000 --- a/src/tests/sigmoid_pointwise_specs.rs +++ /dev/null @@ -1,374 +0,0 @@ -extern crate collenchyma_nn as co_nn; -extern crate collenchyma as co; - -#[cfg(test)] -#[cfg(feature = "cuda")] -mod sigmoid_pointwise_spec_cuda{ - - use co::prelude::*; - use co_nn::*; - use co::plugin::numeric_helpers::{cast, Float}; - - fn get_native_backend() -> Backend { - Backend::::default().unwrap() - } - - fn get_cuda_backend() -> Backend { - Backend::::default().unwrap() - } - - fn write_to_memory(mem: &mut MemoryType, data: &[T]) { - match mem { - &mut MemoryType::Native(ref mut mem) => { - let mut mem_buffer = mem.as_mut_slice::(); - for (index, datum) in data.iter().enumerate() { - mem_buffer[index] = *datum; - } - }, - #[cfg(any(feature = "opencl", feature = "cuda"))] - _ => {} - } - } - - fn get_memory(backend: &Backend, native: &Backend) -> SharedTensor{ - let val = cast::(1f64).unwrap(); - let val2 = cast::(2f64).unwrap(); - let mut x = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - x.add_device(native.device()).unwrap(); - x.sync(native.device()).unwrap(); - write_to_memory(x.get_mut(native.device()).unwrap(), &[val, val, val2]); - x.sync(backend.device()).unwrap(); - - x - } - - fn get_grad_memory(backend: &Backend, native: &Backend) -> (SharedTensor, SharedTensor){ - let val = cast::(1f64).unwrap(); - let val2 = cast::(2f64).unwrap(); - let mut x = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - x.add_device(native.device()).unwrap(); - x.sync(native.device()).unwrap(); - write_to_memory(x.get_mut(native.device()).unwrap(), &[val, val, val2]); - x.sync(backend.device()).unwrap(); - - let mut x_diff = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - x_diff.add_device(native.device()).unwrap(); - x_diff.sync(native.device()).unwrap(); - write_to_memory(x_diff.get_mut(native.device()).unwrap(), &[val, val, val2]); - x_diff.sync(backend.device()).unwrap(); - - (x, x_diff) - } - - #[test] - fn it_computes_correct_sigmoid_on_cuda_for_f32() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let mut x = get_memory::(&backend, &native); - - match backend.sigmoid_pointwise(&mut x) { - Ok(_) => { - x.sync(native.device()).unwrap(); - if let Some(mem) = x.get(native.device()).unwrap().as_native() { - assert_eq!(&[0.7310585786f32, 0.7310586f32, 0.880797f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_sigmoid_on_cuda_for_f64() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let mut x = get_memory::(&backend, &native); - - match backend.sigmoid_pointwise(&mut x) { - Ok(_) => { - x.sync(native.device()).unwrap(); - if let Some(mem) = x.get(native.device()).unwrap().as_native() { - assert_eq!(&[0.7310585786300049f64, 0.7310585786300049f64, 0.8807970779778823f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_sigmoid_on_cuda_for_f32_plain() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let mut x = get_memory::(&backend, &native); - - match backend.sigmoid_pointwise_plain(&mut x) { - Ok(_) => { - x.sync(native.device()).unwrap(); - if let Some(mem) = x.get(native.device()).unwrap().as_native() { - assert_eq!(&[0.7310585786f32, 0.7310586f32, 0.880797f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_sigmoid_on_cuda_for_f64_plain() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let mut x = get_memory::(&backend, &native); - - match backend.sigmoid_pointwise_plain(&mut x) { - Ok(_) => { - x.sync(native.device()).unwrap(); - if let Some(mem) = x.get(native.device()).unwrap().as_native() { - assert_eq!(&[0.7310585786300049f64, 0.7310585786300049f64, 0.8807970779778823f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_sigmoid_grad_on_cuda_for_f32() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut x_diff) = get_grad_memory::(&backend, &native); - - match backend.sigmoid_pointwise_grad(&mut x, &mut x_diff) { - Ok(_) => { - x_diff.sync(native.device()).unwrap(); - if let Some(mem) = x_diff.get(native.device()).unwrap().as_native() { - assert_eq!(&[0f32, 0f32, -4f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_sigmoid_grad_on_cuda_for_f64() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut x_diff) = get_grad_memory::(&backend, &native); - - match backend.sigmoid_pointwise_grad(&mut x, &mut x_diff) { - Ok(_) => { - x_diff.sync(native.device()).unwrap(); - if let Some(mem) = x_diff.get(native.device()).unwrap().as_native() { - assert_eq!(&[0f64, 0f64, -4f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_sigmoid_grad_on_cuda_for_f32_plain() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut x_diff) = get_grad_memory::(&backend, &native); - - match backend.sigmoid_pointwise_grad_plain(&mut x, &mut x_diff) { - Ok(_) => { - x_diff.sync(native.device()).unwrap(); - if let Some(mem) = x_diff.get(native.device()).unwrap().as_native() { - assert_eq!(&[0f32, 0f32, -4f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_sigmoid_grad_on_cuda_for_f64_plain() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut x_diff) = get_grad_memory::(&backend, &native); - - match backend.sigmoid_pointwise_grad_plain(&mut x, &mut x_diff) { - Ok(_) => { - x_diff.sync(native.device()).unwrap(); - if let Some(mem) = x_diff.get(native.device()).unwrap().as_native() { - assert_eq!(&[0f64, 0f64, -4f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } -} - -#[cfg(test)] -#[cfg(feature = "native")] -mod sigmoid_pointwise_spec_native { - - // use co::prelude::*; - // use co_nn::*; - // use co::plugin::numeric_helpers::{cast, Float}; - // - // fn get_native_backend() -> Backend { - // Backend::::default().unwrap() - // } - // - // fn write_to_memory(mem: &mut MemoryType, data: &[T]) { - // match mem { - // &mut MemoryType::Native(ref mut mem) => { - // let mut mem_buffer = mem.as_mut_slice::(); - // for (index, datum) in data.iter().enumerate() { - // mem_buffer[index] = *datum; - // } - // }, - // #[cfg(any(feature = "opencl", feature = "cuda"))] - // _ => {} - // } - // } - // - // fn get_memory(backend: &Backend) -> (SharedTensor, SharedTensor){ - // let val = cast::(1f64).unwrap(); - // let val2 = cast::(2f64).unwrap(); - // let mut x = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - // write_to_memory(x.get_mut(backend.device()).unwrap(), &[val, val, val2]); - // - // let result = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - // - // (x, result) - // } - // - // fn get_grad_memory(backend: &Backend) -> (SharedTensor, SharedTensor, SharedTensor, SharedTensor){ - // let val = cast::(1f64).unwrap(); - // let val2 = cast::(2f64).unwrap(); - // let mut x = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - // write_to_memory(x.get_mut(backend.device()).unwrap(), &[val, val, val2]); - // - // let mut x_diff = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - // write_to_memory(x_diff.get_mut(backend.device()).unwrap(), &[val, val, val2]); - // - // let mut result = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - // write_to_memory(result.get_mut(backend.device()).unwrap(), &[val, val, val2]); - // - // let result_diff = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - // - // (x, x_diff, result, result_diff) - // } - // - // #[test] - // fn it_computes_correct_sigmoid_on_native_for_f32() { - // let backend = get_native_backend(); - // let (mut x, mut result) = get_memory::(&backend); - // - // match backend.sigmoid(&mut x, &mut result) { - // Ok(_) => { - // if let Some(mem) = result.get(backend.device()).unwrap().as_native() { - // assert_eq!(&[0.7310585786f32, 0.7310586f32, 0.880797f32], mem.as_slice::()); - // } else { - // println!("No result: {:?}", result); assert!(false); - // } - // }, - // Err(err) => { println!("{:?}", err); assert!(false) } - // } - // } - // - // #[test] - // fn it_computes_correct_sigmoid_on_native_for_f64() { - // let backend = get_native_backend(); - // let (mut x, mut result) = get_memory::(&backend); - // - // match backend.sigmoid(&mut x, &mut result) { - // Ok(_) => { - // if let Some(mem) = result.get(backend.device()).unwrap().as_native() { - // assert_eq!(&[0.7310585786300049f64, 0.7310585786300049f64, 0.8807970779778823f64], mem.as_slice::()); - // } - // }, - // Err(err) => { println!("{:?}", err); assert!(false) } - // } - // } - // - // #[test] - // fn it_computes_correct_sigmoid_on_native_for_f32_plain() { - // let backend = get_native_backend(); - // let (mut x, mut result) = get_memory::(&backend); - // - // match backend.sigmoid_plain(&mut x, &mut result) { - // Ok(_) => { - // if let Some(mem) = result.get(backend.device()).unwrap().as_native() { - // assert_eq!(&[0.7310585786f32, 0.7310586f32, 0.880797f32], mem.as_slice::()); - // } - // }, - // Err(err) => { println!("{:?}", err); assert!(false) } - // } - // } - // - // #[test] - // fn it_computes_correct_sigmoid_on_native_for_f64_plain() { - // let backend = get_native_backend(); - // let (mut x, mut result) = get_memory::(&backend); - // - // match backend.sigmoid_plain(&mut x, &mut result) { - // Ok(_) => { - // if let Some(mem) = result.get(backend.device()).unwrap().as_native() { - // assert_eq!(&[0.7310585786300049f64, 0.7310585786300049f64, 0.8807970779778823f64], mem.as_slice::()); - // } - // }, - // Err(err) => { println!("{:?}", err); assert!(false) } - // } - // } - // - // #[test] - // fn it_computes_correct_sigmoid_grad_on_native_for_f32() { - // let backend = get_native_backend(); - // let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend); - // - // match backend.sigmoid_grad(&mut x, &mut x_diff, &mut result, &mut result_diff) { - // Ok(_) => { - // if let Some(mem) = result_diff.get(backend.device()).unwrap().as_native() { - // assert_eq!(&[0f32, 0f32, -4f32], mem.as_slice::()); - // } - // }, - // Err(err) => { println!("{:?}", err); assert!(false) } - // } - // } - // - // #[test] - // fn it_computes_correct_sigmoid_grad_on_native_for_f64() { - // let backend = get_native_backend(); - // let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend); - // - // match backend.sigmoid_grad(&mut x, &mut x_diff, &mut result, &mut result_diff) { - // Ok(_) => { - // if let Some(mem) = result_diff.get(backend.device()).unwrap().as_native() { - // assert_eq!(&[0f64, 0f64, -4f64], mem.as_slice::()); - // } - // }, - // Err(err) => { println!("{:?}", err); assert!(false) } - // } - // } - // - // #[test] - // fn it_computes_correct_sigmoid_grad_on_native_for_f32_plain() { - // let backend = get_native_backend(); - // let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend); - // - // match backend.sigmoid_grad_plain(&mut x, &mut x_diff, &mut result, &mut result_diff) { - // Ok(_) => { - // if let Some(mem) = result_diff.get(backend.device()).unwrap().as_native() { - // assert_eq!(&[0f32, 0f32, -4f32], mem.as_slice::()); - // } - // }, - // Err(err) => { println!("{:?}", err); assert!(false) } - // } - // } - // - // #[test] - // fn it_computes_correct_sigmoid_grad_on_native_for_f64_plain() { - // let backend = get_native_backend(); - // let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend); - // - // match backend.sigmoid_grad_plain(&mut x, &mut x_diff, &mut result, &mut result_diff) { - // Ok(_) => { - // if let Some(mem) = result_diff.get(backend.device()).unwrap().as_native() { - // assert_eq!(&[0f64, 0f64, -4f64], mem.as_slice::()); - // } - // }, - // Err(err) => { println!("{:?}", err); assert!(false) } - // } - // } -} diff --git a/src/tests/sigmoid_specs.rs b/src/tests/sigmoid_specs.rs deleted file mode 100644 index 78b8fc9..0000000 --- a/src/tests/sigmoid_specs.rs +++ /dev/null @@ -1,391 +0,0 @@ -extern crate collenchyma_nn as co_nn; -extern crate collenchyma as co; - -#[cfg(test)] -#[cfg(feature = "cuda")] -mod sigmoid_spec_cuda{ - - use co::prelude::*; - use co_nn::*; - use co::plugin::numeric_helpers::{cast, Float}; - - fn get_native_backend() -> Backend { - Backend::::default().unwrap() - } - - fn get_cuda_backend() -> Backend { - Backend::::default().unwrap() - } - - fn write_to_memory(mem: &mut MemoryType, data: &[T]) { - match mem { - &mut MemoryType::Native(ref mut mem) => { - let mut mem_buffer = mem.as_mut_slice::(); - for (index, datum) in data.iter().enumerate() { - mem_buffer[index] = *datum; - } - }, - #[cfg(any(feature = "opencl", feature = "cuda"))] - _ => {} - } - } - - fn get_memory(backend: &Backend, native: &Backend) -> (SharedTensor, SharedTensor){ - let val = cast::(1f64).unwrap(); - let val2 = cast::(2f64).unwrap(); - let mut x = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - x.add_device(native.device()).unwrap(); - x.sync(native.device()).unwrap(); - write_to_memory(x.get_mut(native.device()).unwrap(), &[val, val, val2]); - x.sync(backend.device()).unwrap(); - - let mut result = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - result.add_device(native.device()).unwrap(); - - (x, result) - } - - fn get_grad_memory(backend: &Backend, native: &Backend) -> (SharedTensor, SharedTensor, SharedTensor, SharedTensor){ - let val = cast::(1f64).unwrap(); - let val2 = cast::(2f64).unwrap(); - let mut x = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - x.add_device(native.device()).unwrap(); - x.sync(native.device()).unwrap(); - write_to_memory(x.get_mut(native.device()).unwrap(), &[val, val, val2]); - x.sync(backend.device()).unwrap(); - - let mut x_diff = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - x_diff.add_device(native.device()).unwrap(); - x_diff.sync(native.device()).unwrap(); - write_to_memory(x_diff.get_mut(native.device()).unwrap(), &[val, val, val2]); - x_diff.sync(backend.device()).unwrap(); - - let mut result = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - result.add_device(native.device()).unwrap(); - result.sync(native.device()).unwrap(); - write_to_memory(result.get_mut(native.device()).unwrap(), &[val, val, val2]); - result.sync(backend.device()).unwrap(); - - let mut result_diff = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - result_diff.add_device(native.device()).unwrap(); - - (x, x_diff, result, result_diff) - } - - #[test] - fn it_computes_correct_sigmoid_on_cuda_for_f32() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend, &native); - - result.sync(native.device()).unwrap(); - result.sync(backend.device()).unwrap(); - result.sync(backend.device()).unwrap(); - result.sync(native.device()).unwrap(); - result.sync(backend.device()).unwrap(); - match backend.sigmoid(&mut x, &mut result) { - Ok(_) => { - result.sync(native.device()).unwrap(); - if let Some(mem) = result.get(native.device()).unwrap().as_native() { - assert_eq!(&[0.7310585786f32, 0.7310586f32, 0.880797f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_sigmoid_on_cuda_for_f64() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend, &native); - - match backend.sigmoid(&mut x, &mut result) { - Ok(_) => { - result.sync(native.device()).unwrap(); - if let Some(mem) = result.get(native.device()).unwrap().as_native() { - assert_eq!(&[0.7310585786300049f64, 0.7310585786300049f64, 0.8807970779778823f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_sigmoid_on_cuda_for_f32_plain() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend, &native); - - match backend.sigmoid_plain(&mut x, &mut result) { - Ok(_) => { - result.sync(native.device()).unwrap(); - if let Some(mem) = result.get(native.device()).unwrap().as_native() { - assert_eq!(&[0.7310585786f32, 0.7310586f32, 0.880797f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_sigmoid_on_cuda_for_f64_plain() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend, &native); - - match backend.sigmoid_plain(&mut x, &mut result) { - Ok(_) => { - result.sync(native.device()).unwrap(); - if let Some(mem) = result.get(native.device()).unwrap().as_native() { - assert_eq!(&[0.7310585786300049f64, 0.7310585786300049f64, 0.8807970779778823f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_sigmoid_grad_on_cuda_for_f32() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend, &native); - - match backend.sigmoid_grad(&mut x, &mut x_diff, &mut result, &mut result_diff) { - Ok(_) => { - result_diff.sync(native.device()).unwrap(); - if let Some(mem) = result_diff.get(native.device()).unwrap().as_native() { - assert_eq!(&[0f32, 0f32, -4f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_sigmoid_grad_on_cuda_for_f64() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend, &native); - - match backend.sigmoid_grad(&mut x, &mut x_diff, &mut result, &mut result_diff) { - Ok(_) => { - result_diff.sync(native.device()).unwrap(); - if let Some(mem) = result_diff.get(native.device()).unwrap().as_native() { - assert_eq!(&[0f64, 0f64, -4f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_sigmoid_grad_on_cuda_for_f32_plain() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend, &native); - - match backend.sigmoid_grad_plain(&mut x, &mut x_diff, &mut result, &mut result_diff) { - Ok(_) => { - result_diff.sync(native.device()).unwrap(); - if let Some(mem) = result_diff.get(native.device()).unwrap().as_native() { - assert_eq!(&[0f32, 0f32, -4f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_sigmoid_grad_on_cuda_for_f64_plain() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend, &native); - - match backend.sigmoid_grad_plain(&mut x, &mut x_diff, &mut result, &mut result_diff) { - Ok(_) => { - result_diff.sync(native.device()).unwrap(); - if let Some(mem) = result_diff.get(native.device()).unwrap().as_native() { - assert_eq!(&[0f64, 0f64, -4f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } -} - -#[cfg(test)] -#[cfg(feature = "native")] -mod sigmoid_spec_native { - - use co::prelude::*; - use co_nn::*; - use co::plugin::numeric_helpers::{cast, Float}; - - fn get_native_backend() -> Backend { - Backend::::default().unwrap() - } - - fn write_to_memory(mem: &mut MemoryType, data: &[T]) { - match mem { - &mut MemoryType::Native(ref mut mem) => { - let mut mem_buffer = mem.as_mut_slice::(); - for (index, datum) in data.iter().enumerate() { - mem_buffer[index] = *datum; - } - }, - #[cfg(any(feature = "opencl", feature = "cuda"))] - _ => {} - } - } - - fn get_memory(backend: &Backend) -> (SharedTensor, SharedTensor){ - let val = cast::(1f64).unwrap(); - let val2 = cast::(2f64).unwrap(); - let mut x = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - write_to_memory(x.get_mut(backend.device()).unwrap(), &[val, val, val2]); - - let result = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - - (x, result) - } - - fn get_grad_memory(backend: &Backend) -> (SharedTensor, SharedTensor, SharedTensor, SharedTensor){ - let val = cast::(1f64).unwrap(); - let val2 = cast::(2f64).unwrap(); - let mut x = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - write_to_memory(x.get_mut(backend.device()).unwrap(), &[val, val, val2]); - - let mut x_diff = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - write_to_memory(x_diff.get_mut(backend.device()).unwrap(), &[val, val, val2]); - - let mut result = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - write_to_memory(result.get_mut(backend.device()).unwrap(), &[val, val, val2]); - - let result_diff = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - - (x, x_diff, result, result_diff) - } - - #[test] - fn it_computes_correct_sigmoid_on_native_for_f32() { - let backend = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend); - - match backend.sigmoid(&mut x, &mut result) { - Ok(_) => { - if let Some(mem) = result.get(backend.device()).unwrap().as_native() { - assert_eq!(&[0.7310585786f32, 0.7310586f32, 0.880797f32], mem.as_slice::()); - } else { - println!("No result: {:?}", result); assert!(false); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_sigmoid_on_native_for_f64() { - let backend = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend); - - match backend.sigmoid(&mut x, &mut result) { - Ok(_) => { - if let Some(mem) = result.get(backend.device()).unwrap().as_native() { - assert_eq!(&[0.7310585786300049f64, 0.7310585786300049f64, 0.8807970779778823f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_sigmoid_on_native_for_f32_plain() { - let backend = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend); - - match backend.sigmoid_plain(&mut x, &mut result) { - Ok(_) => { - if let Some(mem) = result.get(backend.device()).unwrap().as_native() { - assert_eq!(&[0.7310585786f32, 0.7310586f32, 0.880797f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_sigmoid_on_native_for_f64_plain() { - let backend = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend); - - match backend.sigmoid_plain(&mut x, &mut result) { - Ok(_) => { - if let Some(mem) = result.get(backend.device()).unwrap().as_native() { - assert_eq!(&[0.7310585786300049f64, 0.7310585786300049f64, 0.8807970779778823f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_sigmoid_grad_on_native_for_f32() { - let backend = get_native_backend(); - let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend); - - match backend.sigmoid_grad(&mut x, &mut x_diff, &mut result, &mut result_diff) { - Ok(_) => { - if let Some(mem) = result_diff.get(backend.device()).unwrap().as_native() { - assert_eq!(&[0f32, 0f32, -4f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_sigmoid_grad_on_native_for_f64() { - let backend = get_native_backend(); - let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend); - - match backend.sigmoid_grad(&mut x, &mut x_diff, &mut result, &mut result_diff) { - Ok(_) => { - if let Some(mem) = result_diff.get(backend.device()).unwrap().as_native() { - assert_eq!(&[0f64, 0f64, -4f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_sigmoid_grad_on_native_for_f32_plain() { - let backend = get_native_backend(); - let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend); - - match backend.sigmoid_grad_plain(&mut x, &mut x_diff, &mut result, &mut result_diff) { - Ok(_) => { - if let Some(mem) = result_diff.get(backend.device()).unwrap().as_native() { - assert_eq!(&[0f32, 0f32, -4f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_sigmoid_grad_on_native_for_f64_plain() { - let backend = get_native_backend(); - let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend); - - match backend.sigmoid_grad_plain(&mut x, &mut x_diff, &mut result, &mut result_diff) { - Ok(_) => { - if let Some(mem) = result_diff.get(backend.device()).unwrap().as_native() { - assert_eq!(&[0f64, 0f64, -4f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } -} diff --git a/src/tests/softmax.rs b/src/tests/softmax.rs new file mode 100644 index 0000000..6c203d7 --- /dev/null +++ b/src/tests/softmax.rs @@ -0,0 +1,108 @@ +use std::fmt; + +use co::prelude::*; +use co::plugin::numeric_helpers::Float; + +use plugin::{Softmax, LogSoftmax}; +use tests::{Epsilon, filled_tensor, tensor_assert_eq}; + +const SOFTMAX_DIMS: [usize; 3] = [1, 2, 4]; + +const SOFTMAX_IN: [f64; 8] = + [-0.502472237916506445425140291397817437, -0.55873138462003144624864037234913405, + 0.794529586579360486837150107830907079, 1.60172389018669112242655927356583869, + -0.320820695309039575179150291244724373, -0.176646560012981224431432055470728985, + 0.0177079924572234721178731443460776922, -0.775950530143907562640162594743411692]; + +const SOFTMAX_OUT_GRAD: [f64; 8] = + [-1.96865455756052091978764498249620389, -3.07033836746959151598133426862725417, + -4.56274332715313060810425031755569442, 2.83656545608099720621517702776136858, + -2.40376407943410729513390463389479265, 3.55533673884051954821867328991351583, + -2.28894426489897620250392225661467921, 1.96961934042911183657200337864748056]; + +const SOFTMAX_OUT: [f64; 8] = + [0.0531023900588802915590645117630878206, 0.0501973778392971739013326487803024943, + 0.194265107994315328639602380648073481, 0.435466585179997952511961131977990542, + 0.0636801871450548350727324041571596707, 0.0735560437500126538986711531234184857, + 0.0893357879157159479986632184714617027, 0.0403965201167258164179725510785058027]; + +const SOFTMAX_IN_GRAD: [f64; 8] = + [-0.108454436003436213026743130055365196, -0.157822980656930541582438665672076116, + -0.900701093964757357072187707288570784, 1.20313125847874793721550294709773303, + -0.157766008978608662062917061946300372, 0.25609469339774033991768607873659373, + -0.21106957465002508153022142616863288, 0.0765881423772695781413189652966185918]; + +const LOG_SOFTMAX_OUT: [f64; 8] = + [-2.93553334122213461839358579757355552, -2.99179248792565961921708587852487213, + -1.638531516726267686131295398344831, -0.831337213118937050541886232609899389, + -2.75388179861466774814759579742046245, -2.60970766331860939739987756164646707, + -2.41535311084840470085057236182966039, -3.20901163344953573560860810091914977]; + +const LOG_SOFTMAX_IN_GRAD: [f64; 8] = + [-19.3849480145181171791815826907657229, -20.8204130133060354080570052246695276, + -14.2840247491852127017714465762356755, -2.09569426723756702914461718052337395, + -18.7423329101595400009357477511837454, -11.9278580397633051472959890299369641, + -16.6190484371097847874550358217626391, -17.0691997832126443597738475365379202]; + + +pub fn test_softmax(backend: Backend) + where T: Float + Epsilon + fmt::Debug, + Backend: Softmax + IBackend { + + let x = filled_tensor(&SOFTMAX_DIMS, &SOFTMAX_IN); + let mut r = SharedTensor::::new(&SOFTMAX_DIMS); + + backend.softmax(&x, &mut r).unwrap(); + tensor_assert_eq(&r, &SOFTMAX_OUT, 3.0); +} + +pub fn test_softmax_grad(backend: Backend) + where T: Float + Epsilon + fmt::Debug, + Backend: Softmax + IBackend { + + let x = filled_tensor(&SOFTMAX_DIMS, &SOFTMAX_OUT); + let dx = filled_tensor(&SOFTMAX_DIMS, &SOFTMAX_OUT_GRAD); + let mut dr = SharedTensor::new(&SOFTMAX_DIMS); + + backend.softmax_grad(&x, &dx, &mut dr).unwrap(); + tensor_assert_eq(&dr, &SOFTMAX_IN_GRAD, 3.0); +} + +pub fn test_log_softmax(backend: Backend) + where T: Float + Epsilon + fmt::Debug, + Backend: LogSoftmax + IBackend { + + let x = filled_tensor(&SOFTMAX_DIMS, &SOFTMAX_IN); + let mut r = SharedTensor::::new(&SOFTMAX_DIMS); + + backend.log_softmax(&x, &mut r).unwrap(); + tensor_assert_eq(&r, &LOG_SOFTMAX_OUT, 3.0); +} + +pub fn test_log_softmax_grad(backend: Backend) + where T: Float + Epsilon + fmt::Debug, + Backend: LogSoftmax + IBackend { + + let x = filled_tensor(&SOFTMAX_DIMS, &LOG_SOFTMAX_OUT); + let dx = filled_tensor(&SOFTMAX_DIMS, &SOFTMAX_OUT_GRAD); + let mut dr = SharedTensor::new(&SOFTMAX_DIMS); + + backend.log_softmax_grad(&x, &dx, &mut dr).unwrap(); + tensor_assert_eq(&dr, &LOG_SOFTMAX_IN_GRAD, 4.0); +} + +mod native { + use super::*; + test_native!(test_softmax, softmax_f32, softmax_f64); + test_native!(test_softmax_grad, softmax_grad_f32, softmax_grad_f64); + test_native!(test_log_softmax, log_softmax_f32, log_softmax_f64); + test_native!(test_log_softmax_grad, log_softmax_grad_f32, log_softmax_grad_f64); +} + +mod cuda { + use super::*; + test_cuda!(test_softmax, softmax_f32, softmax_f64); + test_cuda!(test_softmax_grad, softmax_grad_f32, softmax_grad_f64); + test_cuda!(test_log_softmax, log_softmax_f32, log_softmax_f64); + test_cuda!(test_log_softmax_grad, log_softmax_grad_f32, log_softmax_grad_f64); +} diff --git a/src/tests/softmax_specs.rs b/src/tests/softmax_specs.rs deleted file mode 100644 index 22c067c..0000000 --- a/src/tests/softmax_specs.rs +++ /dev/null @@ -1,509 +0,0 @@ -extern crate collenchyma_nn as co_nn; -extern crate collenchyma as co; - -#[cfg(test)] -#[cfg(feature = "cuda")] -mod softmax_spec_cuda { - - use co::prelude::*; - use co_nn::*; - use co::plugin::numeric_helpers::{cast, Float}; - - fn get_native_backend() -> Backend { - Backend::::default().unwrap() - } - - fn get_cuda_backend() -> Backend { - Backend::::default().unwrap() - } - - fn write_to_memory(mem: &mut MemoryType, data: &[T]) { - match mem { - &mut MemoryType::Native(ref mut mem) => { - let mut mem_buffer = mem.as_mut_slice::(); - for (index, datum) in data.iter().enumerate() { - mem_buffer[index] = *datum; - } - }, - #[cfg(any(feature = "opencl", feature = "cuda"))] - _ => {} - } - } - - fn get_memory(backend: &Backend, native: &Backend) -> (SharedTensor, SharedTensor){ - let val = cast::(1f64).unwrap(); - let mut x = SharedTensor::::new(backend.device(), &(1, 1, 4)).unwrap(); - x.add_device(native.device()).unwrap(); - x.sync(native.device()).unwrap(); - write_to_memory(x.get_mut(native.device()).unwrap(), &[val, val, val, val]); - x.sync(backend.device()).unwrap(); - - let mut result = SharedTensor::::new(backend.device(), &(1, 1, 4)).unwrap(); - result.add_device(native.device()).unwrap(); - - (x, result) - } - - fn get_grad_memory(backend: &Backend, native: &Backend) -> (SharedTensor, SharedTensor, SharedTensor){ - let val = cast::(1f64).unwrap(); - let val2 = cast::(2f64).unwrap(); - let mut x = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - x.add_device(native.device()).unwrap(); - x.sync(native.device()).unwrap(); - write_to_memory(x.get_mut(native.device()).unwrap(), &[val, val, val2]); - x.sync(backend.device()).unwrap(); - - let mut x_diff = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - x_diff.add_device(native.device()).unwrap(); - x_diff.sync(native.device()).unwrap(); - write_to_memory(x_diff.get_mut(native.device()).unwrap(), &[val, val, val2]); - x_diff.sync(backend.device()).unwrap(); - - let mut result_diff = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - result_diff.add_device(native.device()).unwrap(); - - (x, x_diff, result_diff) - } - - #[test] - fn it_computes_correct_softmax_on_cuda_for_f32() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend, &native); - - match backend.softmax(&mut x, &mut result) { - Ok(_) => { - result.sync(native.device()).unwrap(); - if let Some(mem) = result.get(native.device()).unwrap().as_native() { - assert_eq!(&[0.25f32, 0.25f32, 0.25f32, 0.25f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_softmax_on_cuda_for_f64() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend, &native); - - match backend.softmax(&mut x, &mut result) { - Ok(_) => { - result.sync(native.device()).unwrap(); - if let Some(mem) = result.get(native.device()).unwrap().as_native() { - assert_eq!(&[0.25f64, 0.25f64, 0.25f64, 0.25f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_softmax_on_cuda_for_f32_plain() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend, &native); - - match backend.softmax_plain(&mut x, &mut result) { - Ok(_) => { - result.sync(native.device()).unwrap(); - if let Some(mem) = result.get(native.device()).unwrap().as_native() { - assert_eq!(&[0.25f32, 0.25f32, 0.25f32, 0.25f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_softmax_on_cuda_for_f64_plain() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend, &native); - - match backend.softmax_plain(&mut x, &mut result) { - Ok(_) => { - result.sync(native.device()).unwrap(); - if let Some(mem) = result.get(native.device()).unwrap().as_native() { - assert_eq!(&[0.25f64, 0.25f64, 0.25f64, 0.25f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_softmax_grad_on_cuda_for_f32() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut x_diff, mut result_diff) = get_grad_memory::(&backend, &native); - - match backend.softmax_grad(&mut x, &mut x_diff, &mut result_diff) { - Ok(_) => { - result_diff.sync(native.device()).unwrap(); - if let Some(mem) = result_diff.get(native.device()).unwrap().as_native() { - assert_eq!(&[-5f32, -5f32, -8f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_softmax_grad_on_cuda_for_f64() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut x_diff, mut result_diff) = get_grad_memory::(&backend, &native); - - match backend.softmax_grad(&mut x, &mut x_diff, &mut result_diff) { - Ok(_) => { - result_diff.sync(native.device()).unwrap(); - if let Some(mem) = result_diff.get(native.device()).unwrap().as_native() { - assert_eq!(&[-5f64, -5f64, -8f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_softmax_grad_on_cuda_for_f32_plain() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut x_diff, mut result_diff) = get_grad_memory::(&backend, &native); - - match backend.softmax_grad_plain(&mut x, &mut x_diff, &mut result_diff) { - Ok(_) => { - result_diff.sync(native.device()).unwrap(); - if let Some(mem) = result_diff.get(native.device()).unwrap().as_native() { - assert_eq!(&[-5f32, -5f32, -8f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_softmax_grad_on_cuda_for_f64_plain() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut x_diff, mut result_diff) = get_grad_memory::(&backend, &native); - - match backend.softmax_grad_plain(&mut x, &mut x_diff, &mut result_diff) { - Ok(_) => { - result_diff.sync(native.device()).unwrap(); - if let Some(mem) = result_diff.get(native.device()).unwrap().as_native() { - assert_eq!(&[-5f64, -5f64, -8f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_log_softmax_on_cuda_for_f32_plain() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend, &native); - - match backend.log_softmax_plain(&mut x, &mut result) { - Ok(_) => { - result.sync(native.device()).unwrap(); - if let Some(mem) = result.get(native.device()).unwrap().as_native() { - assert_eq!(&[-1.3862944f32, -1.3862944f32, -1.3862944f32, -1.3862944f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_log_softmax_on_cuda_for_f64_plain() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend, &native); - - match backend.log_softmax_plain(&mut x, &mut result) { - Ok(_) => { - result.sync(native.device()).unwrap(); - if let Some(mem) = result.get(native.device()).unwrap().as_native() { - assert_eq!(&[-1.3862943611198908f64, - -1.3862943611198908f64, - -1.3862943611198908f64, - -1.3862943611198908f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_log_softmax_grad_on_cuda_for_f32_plain() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut x_diff, mut result_diff) = get_grad_memory::(&backend, &native); - - match backend.log_softmax_grad_plain(&mut x, &mut x_diff, &mut result_diff) { - Ok(_) => { - result_diff.sync(native.device()).unwrap(); - if let Some(mem) = result_diff.get(native.device()).unwrap().as_native() { - assert_eq!(&[-9.873127f32, -9.873127f32, -27.556225f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_log_softmax_grad_on_cuda_for_f64_plain() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut x_diff, mut result_diff) = get_grad_memory::(&backend, &native); - - match backend.log_softmax_grad_plain(&mut x, &mut x_diff, &mut result_diff) { - Ok(_) => { - result_diff.sync(native.device()).unwrap(); - if let Some(mem) = result_diff.get(native.device()).unwrap().as_native() { - assert_eq!(&[-9.87312731383618f64, -9.87312731383618f64, -27.5562243957226f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } -} - -#[cfg(test)] -#[cfg(feature = "native")] -mod softmax_spec_native { - - use co::prelude::*; - use co_nn::*; - use co::plugin::numeric_helpers::{cast, Float}; - - fn get_native_backend() -> Backend { - Backend::::default().unwrap() - } - - fn write_to_memory(mem: &mut MemoryType, data: &[T]) { - match mem { - &mut MemoryType::Native(ref mut mem) => { - let mut mem_buffer = mem.as_mut_slice::(); - for (index, datum) in data.iter().enumerate() { - mem_buffer[index] = *datum; - } - }, - #[cfg(any(feature = "opencl", feature = "cuda"))] - _ => {} - } - } - - - fn get_memory(backend: &Backend) -> (SharedTensor, SharedTensor){ - let val = cast::(1f64).unwrap(); - let mut x = SharedTensor::::new(backend.device(), &(1, 1, 4)).unwrap(); - write_to_memory(x.get_mut(backend.device()).unwrap(), &[val, val, val, val]); - - let result = SharedTensor::::new(backend.device(), &(1, 1, 4)).unwrap(); - - (x, result) - } - - fn get_grad_memory(backend: &Backend) -> (SharedTensor, SharedTensor, SharedTensor){ - let val = cast::(1f64).unwrap(); - let val2 = cast::(2f64).unwrap(); - let mut x = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - write_to_memory(x.get_mut(backend.device()).unwrap(), &[val, val, val2]); - - let mut x_diff = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - write_to_memory(x_diff.get_mut(backend.device()).unwrap(), &[val, val, val2]); - - let result_diff = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - - (x, x_diff, result_diff) - } - - #[test] - fn it_computes_correct_softmax_on_native_for_f32() { - let backend = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend); - - match backend.softmax(&mut x, &mut result) { - Ok(_) => { - if let Some(mem) = result.get(backend.device()).unwrap().as_native() { - assert_eq!(&[0.25f32, 0.25f32, 0.25f32, 0.25f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_softmax_on_native_for_f64() { - let backend = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend); - - match backend.softmax(&mut x, &mut result) { - Ok(_) => { - if let Some(mem) = result.get(backend.device()).unwrap().as_native() { - assert_eq!(&[0.25f64, 0.25f64, 0.25f64, 0.25f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_softmax_on_native_for_f32_plain() { - let backend = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend); - - match backend.softmax_plain(&mut x, &mut result) { - Ok(_) => { - if let Some(mem) = result.get(backend.device()).unwrap().as_native() { - assert_eq!(&[0.25f32, 0.25f32, 0.25f32, 0.25f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_softmax_on_native_for_f64_plain() { - let backend = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend); - - match backend.softmax_plain(&mut x, &mut result) { - Ok(_) => { - if let Some(mem) = result.get(backend.device()).unwrap().as_native() { - assert_eq!(&[0.25f64, 0.25f64, 0.25f64, 0.25f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_softmax_grad_on_native_for_f32() { - let backend = get_native_backend(); - let (mut x, mut x_diff, mut result_diff) = get_grad_memory::(&backend); - - match backend.softmax_grad(&mut x, &mut x_diff, &mut result_diff) { - Ok(_) => { - if let Some(mem) = result_diff.get(backend.device()).unwrap().as_native() { - assert_eq!(&[-5f32, -5f32, -8f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_softmax_grad_on_native_for_f64() { - let backend = get_native_backend(); - let (mut x, mut x_diff, mut result_diff) = get_grad_memory::(&backend); - - match backend.softmax_grad(&mut x, &mut x_diff, &mut result_diff) { - Ok(_) => { - if let Some(mem) = result_diff.get(backend.device()).unwrap().as_native() { - assert_eq!(&[-5f64, -5f64, -8f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_softmax_grad_on_native_for_f32_plain() { - let backend = get_native_backend(); - let (mut x, mut x_diff, mut result_diff) = get_grad_memory::(&backend); - - match backend.softmax_grad_plain(&mut x, &mut x_diff, &mut result_diff) { - Ok(_) => { - if let Some(mem) = result_diff.get(backend.device()).unwrap().as_native() { - assert_eq!(&[-5f32, -5f32, -8f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_softmax_grad_on_native_for_f64_plain() { - let backend = get_native_backend(); - let (mut x, mut x_diff, mut result_diff) = get_grad_memory::(&backend); - - match backend.softmax_grad_plain(&mut x, &mut x_diff, &mut result_diff) { - Ok(_) => { - if let Some(mem) = result_diff.get(backend.device()).unwrap().as_native() { - assert_eq!(&[-5f64, -5f64, -8f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_log_softmax_on_native_for_f32_plain() { - let backend = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend); - - match backend.log_softmax_plain(&mut x, &mut result) { - Ok(_) => { - if let Some(mem) = result.get(backend.device()).unwrap().as_native() { - assert_eq!(&[-1.3862944f32, -1.3862944f32, -1.3862944f32, -1.3862944f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_log_softmax_on_native_for_f64_plain() { - let backend = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend); - - match backend.log_softmax_plain(&mut x, &mut result) { - Ok(_) => { - if let Some(mem) = result.get(backend.device()).unwrap().as_native() { - assert_eq!(&[-1.3862943611198908f64, - -1.3862943611198908f64, - -1.3862943611198908f64, - -1.3862943611198908f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_log_softmax_grad_on_native_for_f32_plain() { - let backend = get_native_backend(); - let (mut x, mut x_diff, mut result_diff) = get_grad_memory::(&backend); - - match backend.log_softmax_grad_plain(&mut x, &mut x_diff, &mut result_diff) { - Ok(_) => { - if let Some(mem) = result_diff.get(backend.device()).unwrap().as_native() { - assert_eq!(&[-9.873127f32, -9.873127f32, -27.556225f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_log_softmax_grad_on_native_for_f64_plain() { - let backend = get_native_backend(); - let (mut x, mut x_diff, mut result_diff) = get_grad_memory::(&backend); - - match backend.log_softmax_grad_plain(&mut x, &mut x_diff, &mut result_diff) { - Ok(_) => { - if let Some(mem) = result_diff.get(backend.device()).unwrap().as_native() { - assert_eq!(&[-9.87312731383618f64, -9.87312731383618f64, -27.5562243957226f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - -} diff --git a/src/tests/tanh_pointwise_specs.rs b/src/tests/tanh_pointwise_specs.rs deleted file mode 100644 index 0d19801..0000000 --- a/src/tests/tanh_pointwise_specs.rs +++ /dev/null @@ -1,374 +0,0 @@ -extern crate collenchyma_nn as co_nn; -extern crate collenchyma as co; - -#[cfg(test)] -#[cfg(feature = "cuda")] -mod tanh_pointwise_spec_cuda{ - - use co::prelude::*; - use co_nn::*; - use co::plugin::numeric_helpers::{cast, Float}; - - fn get_native_backend() -> Backend { - Backend::::default().unwrap() - } - - fn get_cuda_backend() -> Backend { - Backend::::default().unwrap() - } - - fn write_to_memory(mem: &mut MemoryType, data: &[T]) { - match mem { - &mut MemoryType::Native(ref mut mem) => { - let mut mem_buffer = mem.as_mut_slice::(); - for (index, datum) in data.iter().enumerate() { - mem_buffer[index] = *datum; - } - }, - #[cfg(any(feature = "opencl", feature = "cuda"))] - _ => {} - } - } - - fn get_memory(backend: &Backend, native: &Backend) -> SharedTensor{ - let val = cast::(1f64).unwrap(); - let val2 = cast::(2f64).unwrap(); - let mut x = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - x.add_device(native.device()).unwrap(); - x.sync(native.device()).unwrap(); - write_to_memory(x.get_mut(native.device()).unwrap(), &[val, val, val2]); - x.sync(backend.device()).unwrap(); - - x - } - - fn get_grad_memory(backend: &Backend, native: &Backend) -> (SharedTensor, SharedTensor){ - let val = cast::(1f64).unwrap(); - let val2 = cast::(2f64).unwrap(); - let mut x = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - x.add_device(native.device()).unwrap(); - x.sync(native.device()).unwrap(); - write_to_memory(x.get_mut(native.device()).unwrap(), &[val, val, val2]); - x.sync(backend.device()).unwrap(); - - let mut x_diff = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - x_diff.add_device(native.device()).unwrap(); - x_diff.sync(native.device()).unwrap(); - write_to_memory(x_diff.get_mut(native.device()).unwrap(), &[val, val, val2]); - x_diff.sync(backend.device()).unwrap(); - - (x, x_diff) - } - - #[test] - fn it_computes_correct_tanh_on_cuda_for_f32() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let mut x = get_memory::(&backend, &native); - - match backend.tanh_pointwise(&mut x) { - Ok(_) => { - x.sync(native.device()).unwrap(); - if let Some(mem) = x.get(native.device()).unwrap().as_native() { - assert_eq!(&[0.7615942f32, 0.7615942f32, 0.9640276f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_tanh_on_cuda_for_f64() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let mut x = get_memory::(&backend, &native); - - match backend.tanh_pointwise(&mut x) { - Ok(_) => { - x.sync(native.device()).unwrap(); - if let Some(mem) = x.get(native.device()).unwrap().as_native() { - assert_eq!(&[0.7615941559557649f64, 0.7615941559557649f64, 0.9640275800758169f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_tanh_on_cuda_for_f32_plain() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let mut x = get_memory::(&backend, &native); - - match backend.tanh_pointwise_plain(&mut x) { - Ok(_) => { - x.sync(native.device()).unwrap(); - if let Some(mem) = x.get(native.device()).unwrap().as_native() { - assert_eq!(&[0.7615942f32, 0.7615942f32, 0.9640276f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_tanh_on_cuda_for_f64_plain() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let mut x = get_memory::(&backend, &native); - - match backend.tanh_pointwise_plain(&mut x) { - Ok(_) => { - x.sync(native.device()).unwrap(); - if let Some(mem) = x.get(native.device()).unwrap().as_native() { - assert_eq!(&[0.7615941559557649f64, 0.7615941559557649f64, 0.9640275800758169f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_tanh_grad_on_cuda_for_f32() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut x_diff) = get_grad_memory::(&backend, &native); - - match backend.tanh_pointwise_grad(&mut x, &mut x_diff) { - Ok(_) => { - x_diff.sync(native.device()).unwrap(); - if let Some(mem) = x_diff.get(native.device()).unwrap().as_native() { - assert_eq!(&[0f32, 0f32, -6f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_tanh_grad_on_cuda_for_f64() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut x_diff) = get_grad_memory::(&backend, &native); - - match backend.tanh_pointwise_grad(&mut x, &mut x_diff) { - Ok(_) => { - x_diff.sync(native.device()).unwrap(); - if let Some(mem) = x_diff.get(native.device()).unwrap().as_native() { - assert_eq!(&[0f64, 0f64, -6f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_tanh_grad_on_cuda_for_f32_plain() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut x_diff) = get_grad_memory::(&backend, &native); - - match backend.tanh_pointwise_grad_plain(&mut x, &mut x_diff) { - Ok(_) => { - x_diff.sync(native.device()).unwrap(); - if let Some(mem) = x_diff.get(native.device()).unwrap().as_native() { - assert_eq!(&[0f32, 0f32, -6f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_tanh_grad_on_cuda_for_f64_plain() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut x_diff) = get_grad_memory::(&backend, &native); - - match backend.tanh_pointwise_grad_plain(&mut x, &mut x_diff) { - Ok(_) => { - x_diff.sync(native.device()).unwrap(); - if let Some(mem) = x_diff.get(native.device()).unwrap().as_native() { - assert_eq!(&[0f64, 0f64, -6f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } -} - -#[cfg(test)] -#[cfg(feature = "native")] -mod tanh_pointwise_spec_native { - - // use co::prelude::*; - // use co_nn::*; - // use co::plugin::numeric_helpers::{cast, Float}; - // - // fn get_native_backend() -> Backend { - // Backend::::default().unwrap() - // } - // - // fn write_to_memory(mem: &mut MemoryType, data: &[T]) { - // match mem { - // &mut MemoryType::Native(ref mut mem) => { - // let mut mem_buffer = mem.as_mut_slice::(); - // for (index, datum) in data.iter().enumerate() { - // mem_buffer[index] = *datum; - // } - // }, - // #[cfg(any(feature = "opencl", feature = "cuda"))] - // _ => {} - // } - // } - // - // fn get_memory(backend: &Backend) -> (SharedTensor, SharedTensor){ - // let val = cast::(1f64).unwrap(); - // let val2 = cast::(2f64).unwrap(); - // let mut x = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - // write_to_memory(x.get_mut(backend.device()).unwrap(), &[val, val, val2]); - // - // let result = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - // - // (x, result) - // } - // - // fn get_grad_memory(backend: &Backend) -> (SharedTensor, SharedTensor, SharedTensor, SharedTensor){ - // let val = cast::(1f64).unwrap(); - // let val2 = cast::(2f64).unwrap(); - // let mut x = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - // write_to_memory(x.get_mut(backend.device()).unwrap(), &[val, val, val2]); - // - // let mut x_diff = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - // write_to_memory(x_diff.get_mut(backend.device()).unwrap(), &[val, val, val2]); - // - // let mut result = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - // write_to_memory(result.get_mut(backend.device()).unwrap(), &[val, val, val2]); - // - // let result_diff = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - // - // (x, x_diff, result, result_diff) - // } - // - // #[test] - // fn it_computes_correct_tanh_on_native_for_f32() { - // let backend = get_native_backend(); - // let (mut x, mut result) = get_memory::(&backend); - // - // match backend.tanh(&mut x, &mut result) { - // Ok(_) => { - // if let Some(mem) = result.get(backend.device()).unwrap().as_native() { - // assert_eq!(&[0.7310585786f32, 0.7310586f32, 0.880797f32], mem.as_slice::()); - // } else { - // println!("No result: {:?}", result); assert!(false); - // } - // }, - // Err(err) => { println!("{:?}", err); assert!(false) } - // } - // } - // - // #[test] - // fn it_computes_correct_tanh_on_native_for_f64() { - // let backend = get_native_backend(); - // let (mut x, mut result) = get_memory::(&backend); - // - // match backend.tanh(&mut x, &mut result) { - // Ok(_) => { - // if let Some(mem) = result.get(backend.device()).unwrap().as_native() { - // assert_eq!(&[0.7310585786300049f64, 0.7310585786300049f64, 0.8807970779778823f64], mem.as_slice::()); - // } - // }, - // Err(err) => { println!("{:?}", err); assert!(false) } - // } - // } - // - // #[test] - // fn it_computes_correct_tanh_on_native_for_f32_plain() { - // let backend = get_native_backend(); - // let (mut x, mut result) = get_memory::(&backend); - // - // match backend.tanh_plain(&mut x, &mut result) { - // Ok(_) => { - // if let Some(mem) = result.get(backend.device()).unwrap().as_native() { - // assert_eq!(&[0.7310585786f32, 0.7310586f32, 0.880797f32], mem.as_slice::()); - // } - // }, - // Err(err) => { println!("{:?}", err); assert!(false) } - // } - // } - // - // #[test] - // fn it_computes_correct_tanh_on_native_for_f64_plain() { - // let backend = get_native_backend(); - // let (mut x, mut result) = get_memory::(&backend); - // - // match backend.tanh_plain(&mut x, &mut result) { - // Ok(_) => { - // if let Some(mem) = result.get(backend.device()).unwrap().as_native() { - // assert_eq!(&[0.7310585786300049f64, 0.7310585786300049f64, 0.8807970779778823f64], mem.as_slice::()); - // } - // }, - // Err(err) => { println!("{:?}", err); assert!(false) } - // } - // } - // - // #[test] - // fn it_computes_correct_tanh_grad_on_native_for_f32() { - // let backend = get_native_backend(); - // let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend); - // - // match backend.tanh_grad(&mut x, &mut x_diff, &mut result, &mut result_diff) { - // Ok(_) => { - // if let Some(mem) = result_diff.get(backend.device()).unwrap().as_native() { - // assert_eq!(&[0f32, 0f32, -4f32], mem.as_slice::()); - // } - // }, - // Err(err) => { println!("{:?}", err); assert!(false) } - // } - // } - // - // #[test] - // fn it_computes_correct_tanh_grad_on_native_for_f64() { - // let backend = get_native_backend(); - // let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend); - // - // match backend.tanh_grad(&mut x, &mut x_diff, &mut result, &mut result_diff) { - // Ok(_) => { - // if let Some(mem) = result_diff.get(backend.device()).unwrap().as_native() { - // assert_eq!(&[0f64, 0f64, -4f64], mem.as_slice::()); - // } - // }, - // Err(err) => { println!("{:?}", err); assert!(false) } - // } - // } - // - // #[test] - // fn it_computes_correct_tanh_grad_on_native_for_f32_plain() { - // let backend = get_native_backend(); - // let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend); - // - // match backend.tanh_grad_plain(&mut x, &mut x_diff, &mut result, &mut result_diff) { - // Ok(_) => { - // if let Some(mem) = result_diff.get(backend.device()).unwrap().as_native() { - // assert_eq!(&[0f32, 0f32, -4f32], mem.as_slice::()); - // } - // }, - // Err(err) => { println!("{:?}", err); assert!(false) } - // } - // } - // - // #[test] - // fn it_computes_correct_tanh_grad_on_native_for_f64_plain() { - // let backend = get_native_backend(); - // let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend); - // - // match backend.tanh_grad_plain(&mut x, &mut x_diff, &mut result, &mut result_diff) { - // Ok(_) => { - // if let Some(mem) = result_diff.get(backend.device()).unwrap().as_native() { - // assert_eq!(&[0f64, 0f64, -4f64], mem.as_slice::()); - // } - // }, - // Err(err) => { println!("{:?}", err); assert!(false) } - // } - // } -} diff --git a/src/tests/tanh_specs.rs b/src/tests/tanh_specs.rs deleted file mode 100644 index fa2e5ff..0000000 --- a/src/tests/tanh_specs.rs +++ /dev/null @@ -1,384 +0,0 @@ -extern crate collenchyma_nn as co_nn; -extern crate collenchyma as co; - -#[cfg(test)] -#[cfg(feature = "cuda")] -mod tanh_spec_cuda { - - use co::prelude::*; - use co_nn::*; - use co::plugin::numeric_helpers::{cast, Float}; - - fn get_native_backend() -> Backend { - Backend::::default().unwrap() - } - - fn get_cuda_backend() -> Backend { - Backend::::default().unwrap() - } - - fn write_to_memory(mem: &mut MemoryType, data: &[T]) { - match mem { - &mut MemoryType::Native(ref mut mem) => { - let mut mem_buffer = mem.as_mut_slice::(); - for (index, datum) in data.iter().enumerate() { - mem_buffer[index] = *datum; - } - }, - #[cfg(any(feature = "opencl", feature = "cuda"))] - _ => {} - } - } - - fn get_memory(backend: &Backend, native: &Backend) -> (SharedTensor, SharedTensor){ - let val = cast::(1f64).unwrap(); - let val2 = cast::(2f64).unwrap(); - let mut x = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - x.add_device(native.device()).unwrap(); - x.sync(native.device()).unwrap(); - write_to_memory(x.get_mut(native.device()).unwrap(), &[val, val, val2]); - x.sync(backend.device()).unwrap(); - - let mut result = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - result.add_device(native.device()).unwrap(); - - (x, result) - } - - fn get_grad_memory(backend: &Backend, native: &Backend) -> (SharedTensor, SharedTensor, SharedTensor, SharedTensor){ - let val = cast::(1f64).unwrap(); - let val2 = cast::(2f64).unwrap(); - let mut x = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - x.add_device(native.device()).unwrap(); - x.sync(native.device()).unwrap(); - write_to_memory(x.get_mut(native.device()).unwrap(), &[val, val, val2]); - x.sync(backend.device()).unwrap(); - - let mut x_diff = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - x_diff.add_device(native.device()).unwrap(); - x_diff.sync(native.device()).unwrap(); - write_to_memory(x_diff.get_mut(native.device()).unwrap(), &[val, val, val2]); - x_diff.sync(backend.device()).unwrap(); - - let mut result = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - result.add_device(native.device()).unwrap(); - result.sync(native.device()).unwrap(); - write_to_memory(result.get_mut(native.device()).unwrap(), &[val, val, val2]); - result.sync(backend.device()).unwrap(); - - let mut result_diff = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - result_diff.add_device(native.device()).unwrap(); - - (x, x_diff, result, result_diff) - } - - #[test] - fn it_computes_correct_tanh_on_cuda_for_f32() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend, &native); - - match backend.tanh(&mut x, &mut result) { - Ok(_) => { - result.sync(native.device()).unwrap(); - if let Some(mem) = result.get(native.device()).unwrap().as_native() { - assert_eq!(&[0.7615942f32, 0.7615942f32, 0.9640276f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_tanh_on_cuda_for_f64() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend, &native); - - match backend.tanh(&mut x, &mut result) { - Ok(_) => { - result.sync(native.device()).unwrap(); - if let Some(mem) = result.get(native.device()).unwrap().as_native() { - assert_eq!(&[0.7615941559557649f64, 0.7615941559557649f64, 0.9640275800758169f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_tanh_on_cuda_for_f32_plain() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend, &native); - - match backend.tanh_plain(&mut x, &mut result) { - Ok(_) => { - result.sync(native.device()).unwrap(); - if let Some(mem) = result.get(native.device()).unwrap().as_native() { - assert_eq!(&[0.7615942f32, 0.7615942f32, 0.9640276f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_tanh_on_cuda_for_f64_plain() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend, &native); - - match backend.tanh_plain(&mut x, &mut result) { - Ok(_) => { - result.sync(native.device()).unwrap(); - if let Some(mem) = result.get(native.device()).unwrap().as_native() { - assert_eq!(&[0.7615941559557649f64, 0.7615941559557649f64, 0.9640275800758169f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_tanh_grad_on_cuda_for_f32() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend, &native); - - match backend.tanh_grad(&mut x, &mut x_diff, &mut result, &mut result_diff) { - Ok(_) => { - result_diff.sync(native.device()).unwrap(); - if let Some(mem) = result_diff.get(native.device()).unwrap().as_native() { - assert_eq!(&[0f32, 0f32, -6f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_tanh_grad_on_cuda_for_f64() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend, &native); - - match backend.tanh_grad(&mut x, &mut x_diff, &mut result, &mut result_diff) { - Ok(_) => { - result_diff.sync(native.device()).unwrap(); - if let Some(mem) = result_diff.get(native.device()).unwrap().as_native() { - assert_eq!(&[0f64, 0f64, -6f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_tanh_grad_on_cuda_for_f32_plain() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend, &native); - - match backend.tanh_grad_plain(&mut x, &mut x_diff, &mut result, &mut result_diff) { - Ok(_) => { - result_diff.sync(native.device()).unwrap(); - if let Some(mem) = result_diff.get(native.device()).unwrap().as_native() { - assert_eq!(&[0f32, 0f32, -6f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_tanh_grad_on_cuda_for_f64_plain() { - let backend = get_cuda_backend(); - let native = get_native_backend(); - let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend, &native); - - match backend.tanh_grad_plain(&mut x, &mut x_diff, &mut result, &mut result_diff) { - Ok(_) => { - result_diff.sync(native.device()).unwrap(); - if let Some(mem) = result_diff.get(native.device()).unwrap().as_native() { - assert_eq!(&[0f64, 0f64, -6f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } -} - -#[cfg(test)] -#[cfg(feature = "native")] -mod tanh_spec_native { - - use co::prelude::*; - use co_nn::*; - use co::plugin::numeric_helpers::{cast, Float}; - - fn get_native_backend() -> Backend { - Backend::::default().unwrap() - } - - fn write_to_memory(mem: &mut MemoryType, data: &[T]) { - match mem { - &mut MemoryType::Native(ref mut mem) => { - let mut mem_buffer = mem.as_mut_slice::(); - for (index, datum) in data.iter().enumerate() { - mem_buffer[index] = *datum; - } - }, - #[cfg(any(feature = "opencl", feature = "cuda"))] - _ => {} - } - } - - fn get_memory(backend: &Backend) -> (SharedTensor, SharedTensor){ - let val = cast::(1f64).unwrap(); - let val2 = cast::(2f64).unwrap(); - let mut x = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - write_to_memory(x.get_mut(backend.device()).unwrap(), &[val, val, val2]); - - let result = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - - (x, result) - } - - fn get_grad_memory(backend: &Backend) -> (SharedTensor, SharedTensor, SharedTensor, SharedTensor){ - let val = cast::(1f64).unwrap(); - let val2 = cast::(2f64).unwrap(); - let mut x = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - write_to_memory(x.get_mut(backend.device()).unwrap(), &[val, val, val2]); - - let mut x_diff = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - write_to_memory(x_diff.get_mut(backend.device()).unwrap(), &[val, val, val2]); - - let mut result = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - write_to_memory(result.get_mut(backend.device()).unwrap(), &[val, val, val2]); - - let result_diff = SharedTensor::::new(backend.device(), &(1, 1, 3)).unwrap(); - - (x, x_diff, result, result_diff) - } - - #[test] - fn it_computes_correct_tanh_on_native_for_f32() { - let backend = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend); - - match backend.tanh(&mut x, &mut result) { - Ok(_) => { - if let Some(mem) = result.get(backend.device()).unwrap().as_native() { - assert_eq!(&[0.7615942f32, 0.7615942f32, 0.9640276f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_tanh_on_native_for_f64() { - let backend = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend); - - match backend.tanh(&mut x, &mut result) { - Ok(_) => { - if let Some(mem) = result.get(backend.device()).unwrap().as_native() { - assert_eq!(&[0.7615941559557649f64, 0.7615941559557649f64, 0.9640275800758169f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_tanh_on_native_for_f32_plain() { - let backend = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend); - - match backend.tanh_plain(&mut x, &mut result) { - Ok(_) => { - if let Some(mem) = result.get(backend.device()).unwrap().as_native() { - assert_eq!(&[0.7615942f32, 0.7615942f32, 0.9640276f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_tanh_on_native_for_f64_plain() { - let backend = get_native_backend(); - let (mut x, mut result) = get_memory::(&backend); - - match backend.tanh_plain(&mut x, &mut result) { - Ok(_) => { - if let Some(mem) = result.get(backend.device()).unwrap().as_native() { - assert_eq!(&[0.7615941559557649f64, 0.7615941559557649f64, 0.9640275800758169f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_tanh_grad_on_native_for_f32() { - let backend = get_native_backend(); - let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend); - - match backend.tanh_grad(&mut x, &mut x_diff, &mut result, &mut result_diff) { - Ok(_) => { - if let Some(mem) = result_diff.get(backend.device()).unwrap().as_native() { - assert_eq!(&[0f32, 0f32, -6f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_tanh_grad_on_native_for_f64() { - let backend = get_native_backend(); - let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend); - - match backend.tanh_grad(&mut x, &mut x_diff, &mut result, &mut result_diff) { - Ok(_) => { - if let Some(mem) = result_diff.get(backend.device()).unwrap().as_native() { - assert_eq!(&[0f64, 0f64, -6f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_tanh_grad_on_native_for_f32_plain() { - let backend = get_native_backend(); - let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend); - - match backend.tanh_grad_plain(&mut x, &mut x_diff, &mut result, &mut result_diff) { - Ok(_) => { - if let Some(mem) = result_diff.get(backend.device()).unwrap().as_native() { - assert_eq!(&[0f32, 0f32, -6f32], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } - - #[test] - fn it_computes_correct_tanh_grad_on_native_for_f64_plain() { - let backend = get_native_backend(); - let (mut x, mut x_diff, mut result, mut result_diff) = get_grad_memory::(&backend); - - match backend.tanh_grad_plain(&mut x, &mut x_diff, &mut result, &mut result_diff) { - Ok(_) => { - if let Some(mem) = result_diff.get(backend.device()).unwrap().as_native() { - assert_eq!(&[0f64, 0f64, -6f64], mem.as_slice::()); - } - }, - Err(err) => { println!("{:?}", err); assert!(false) } - } - } -}