Skip to content

Commit

Permalink
Add-Multi
Browse files Browse the repository at this point in the history
Bench: 1317423
  • Loading branch information
jw1912 committed Aug 20, 2024
1 parent 8e3fa4d commit d70243d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
29 changes: 29 additions & 0 deletions src/networks/accumulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,35 @@ impl<T: AddAssign<T> + Copy + Mul<T, Output = T>, const N: usize> Accumulator<T,
}
}

impl<const N: usize> Accumulator<i16, N> {
pub fn add_multi(&mut self, adds: &[usize], weights: &[Self]) {
const REGS: usize = 8;
const PER: usize = REGS * 16;

let mut regs = [0i16; PER];

for i in 0..N / PER {
let offset = PER * i;

for (j, reg) in regs.iter_mut().enumerate() {
*reg = self.0[offset + j];
}

for &add in adds {
let this_weight = &weights[usize::from(add)];

for (j, reg) in regs.iter_mut().enumerate() {
*reg += this_weight.0[offset + j];
}
}

for (j, reg) in regs.iter().enumerate() {
self.0[offset + j] = *reg;
}
}
}
}

impl<const N: usize> Accumulator<f32, N> {
pub fn dot<T: Activation>(&self, other: &Self) -> f32 {
let mut res = 0.0;
Expand Down
9 changes: 8 additions & 1 deletion src/networks/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@ pub struct Layer<T: Copy, const M: usize, const N: usize> {

impl<const M: usize, const N: usize> Layer<i16, M, N> {
pub fn forward(&self, board: &Board) -> Accumulator<i16, N> {
let mut count = 0;
let mut feats = [0; 32];
board.map_value_features(|feat| {
feats[count] = feat;
count += 1;
});

let mut out = self.biases;

board.map_value_features(|feat| out.add(&self.weights[feat]));
out.add_multi(&feats[..count], &self.weights);

out
}
Expand Down

0 comments on commit d70243d

Please sign in to comment.