use super::*;
#[test]
fn test_dense_layer_identity() {
let mut f32_weights = AlignedVec::from_vec(vec![0.0f32; 16])
.expect("allocation should succeed for test-sized buffers");
for out_c in 0..4 {
f32_weights[out_c * 4 + out_c] = 1.0;
}
let dense = DenseLayer::<4, 4> {
weights: f32_weights,
bias: AlignedVec::from_vec(vec![0.0; 4])
.expect("allocation should succeed for test-sized buffers"),
do_bias: false,
};
let input = vec![1.5, 2.5, 3.5, 4.5];
let mut output = vec![0.0; 4];
unsafe {
dense.process_block::<crate::math::common::Avx2Math>(&input, &mut output, 1);
}
assert_eq!(output, vec![1.5, 2.5, 3.5, 4.5]);
}
#[test]
fn test_dense_layer_with_bias() {
let mut f32_weights = AlignedVec::from_vec(vec![0.0f32; 16])
.expect("allocation should succeed for test-sized buffers");
for out_c in 0..4 {
f32_weights[out_c * 4 + out_c] = 1.0;
}
let dense = DenseLayer::<4, 4> {
weights: f32_weights,
bias: AlignedVec::from_vec(vec![1.0; 4])
.expect("allocation should succeed for test-sized buffers"),
do_bias: true,
};
let input = vec![1.0, 2.0, 3.0, 4.0];
let mut output = vec![0.0; 4];
unsafe {
dense.process_block::<crate::math::common::Avx2Math>(&input, &mut output, 1);
}
assert_eq!(output, vec![2.0, 3.0, 4.0, 5.0]);
}
#[test]
fn test_dense_layer_rectangular() {
let mut f32_weights = AlignedVec::from_vec(vec![0.0f32; 32])
.expect("allocation should succeed for test-sized buffers"); f32_weights[0] = 1.0; f32_weights[4] = 2.0; f32_weights[9] = 3.0; f32_weights[13] = 4.0; f32_weights[18] = 0.5; f32_weights[31] = -1.0;
let dense = DenseLayer::<8, 4> {
weights: f32_weights,
bias: AlignedVec::from_vec(vec![0.5, -0.5, 1.0, -1.0])
.expect("allocation should succeed for test-sized buffers"),
do_bias: true,
};
let input = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
let mut output = vec![0.0; 4];
unsafe {
dense.process_block::<crate::math::common::Avx2Math>(&input, &mut output, 1);
}
assert_eq!(output[0], 5.5);
assert_eq!(output[1], 24.5);
assert_eq!(output[2], 3.5);
assert_eq!(output[3], -9.0);
}