Skip to content

Commit

Permalink
Merge pull request #5 from dsekercioglu/buckets
Browse files Browse the repository at this point in the history
Buckets
  • Loading branch information
jnlt3 authored Oct 25, 2021
2 parents 54966f8 + d6ad59a commit 94d9380
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 55 deletions.
5 changes: 3 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ pub fn from_bytes_bm(bytes: Vec<u8>) -> (Vec<usize>, Vec<Vec<i8>>, Vec<Vec<i8>>,
break;
}
}
if layer != layers[layers.len() - 1] {
if layer != 1 {
let mut index = 0;
while let Some(&weight) = bytes_iterator.next() {
let weight: i8 = unsafe { std::mem::transmute(weight) };
Expand All @@ -133,7 +133,8 @@ pub fn from_bytes_bm(bytes: Vec<u8>) -> (Vec<usize>, Vec<Vec<i8>>, Vec<Vec<i8>>,
}
}
}
let mut psqt_weights = vec![0_i32; layers[0]];
let mut psqt_weights = vec![0_i32; layers[0] * layers[layers.len() - 1]];
println!("{}", psqt_weights.len());
let mut index = 0;
while index < psqt_weights.len() {
let weight: i32 = unsafe {
Expand Down
Binary file modified nnue.bin
Binary file not shown.
16 changes: 9 additions & 7 deletions src/bm/bm_eval/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,19 +366,21 @@ impl StdEvaluator {
Color::White => 1,
Color::Black => -1,
};
#[cfg(feature = "nnue")]
{
return Evaluation::new(self.nnue.feed_forward(&board) * turn + 15);
}
reset_trace!(&mut self.trace);
trace_tempo!(&mut self.trace, board.side_to_move());

let phase = (board.pieces(Piece::Pawn).popcnt() * PAWN_PHASE
+ board.pieces(Piece::Knight).popcnt() * KNIGHT_PHASE
+ board.pieces(Piece::Bishop).popcnt() * BISHOP_PHASE
+ board.pieces(Piece::Rook).popcnt() * ROOK_PHASE
+ board.pieces(Piece::Queen).popcnt() * QUEEN_PHASE)
.min(TOTAL_PHASE as u32) as i16;
#[cfg(feature = "nnue")]
{
return Evaluation::new(
self.nnue.feed_forward(&board, 1 - (phase.saturating_sub(1) / 12) as usize) * turn + 15,
);
}
reset_trace!(&mut self.trace);
trace_tempo!(&mut self.trace, board.side_to_move());

trace_phase!(&mut self.trace, phase);

let eval = self.evaluate_psqt(board, Piece::Pawn)
Expand Down
11 changes: 0 additions & 11 deletions src/bm/bm_runner/ab_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,17 +445,6 @@ impl AbRunner {
for join_handler in join_handlers {
let (_, _, _, nodes) = join_handler.join().unwrap();
node_count += nodes;
/*
if let Some(best_move) = best_move {
if eval > final_eval {
final_move = Some(best_move);
final_eval = eval;
max_depth = u32::max(max_depth, depth);
}
} else {
println!("# Move generation failed");
}
*/
}
if final_move.is_none() {
panic!("# All move generation has failed");
Expand Down
7 changes: 4 additions & 3 deletions src/bm/nnue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Nnue {
}

#[inline]
pub fn feed_forward(&mut self, board: &Board) -> i16 {
pub fn feed_forward(&mut self, board: &Board, bucket: usize) -> i16 {
let white = *board.color_combined(Color::White);
let black = *board.color_combined(Color::Black);

Expand Down Expand Up @@ -118,8 +118,9 @@ impl Nnue {
let b_incr_layer = *self.b_input_layer.get();
let b_incr_layer = normal::clipped_relu(b_incr_layer);

let psqt_score = (self.w_res_layer.get()[0] - self.b_res_layer.get()[0]) / 128;
let psqt_score = (self.w_res_layer.get()[bucket] - self.b_res_layer.get()[bucket]) / 128;

psqt_score as i16 + normal::out(self.out_layer.ff_sym(&w_incr_layer, &b_incr_layer)[0])
psqt_score as i16
+ normal::out(self.out_layer.ff_sym(&w_incr_layer, &b_incr_layer, bucket)[bucket])
}
}
12 changes: 10 additions & 2 deletions src/bm/nnue/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,20 @@ impl<'a, const INPUT: usize, const OUTPUT: usize> Dense<'a, INPUT, OUTPUT> {
}

#[inline]
pub fn ff_sym(&self, w_inputs: &[i8; INPUT], b_inputs: &[i8; INPUT]) -> [i32; OUTPUT] {
pub fn ff_sym(
&self,
w_inputs: &[i8; INPUT],
b_inputs: &[i8; INPUT],
bucket: usize,
) -> [i32; OUTPUT] {
let mut out = self.bias;
for ((&w_input, &b_input), weights) in
w_inputs.iter().zip(b_inputs.iter()).zip(&*self.weights.0)
{
for (out, &weight) in out.iter_mut().zip(weights) {
for (out, &weight) in out[bucket..bucket + 1]
.iter_mut()
.zip(weights[bucket..bucket + 1].iter())
{
*out += weight as i32 * (w_input as i32 - b_input as i32) / 2;
}
}
Expand Down
57 changes: 33 additions & 24 deletions src/bm/uci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::bm::bm_runner::ab_runner::AbRunner;
use crate::bm::bm_runner::config::{NoInfo, Run, UciInfo};

use crate::bm::bm_runner::time::{TimeManagementInfo, TimeManager};
use crate::bm::nnue::Nnue;

const VERSION: &str = "dev";

Expand Down Expand Up @@ -115,7 +116,10 @@ impl UciAdapter {
}
UciCommand::Eval => {
let runner = &mut *self.bm_runner.lock().unwrap();
println!("{}", runner.raw_eval().raw());
let mut nnue = Nnue::new();
println!("eval : {}", runner.raw_eval().raw());
println!("bucket 0: {}", nnue.feed_forward(runner.get_board(), 0));
println!("bucket 1: {}", nnue.feed_forward(runner.get_board(), 1));
}
UciCommand::Go(commands) => self.go(commands),
UciCommand::NewGame => {
Expand Down Expand Up @@ -224,31 +228,36 @@ impl UciAdapter {
let bm_runner = &mut *self.bm_runner.lock().unwrap();

let original_board = *bm_runner.get_board();
let base_eval = bm_runner.raw_eval().raw();
let mut nnue = Nnue::new();

let mut sq_values = [None; 64];
for sq in *original_board.combined() {
let mut board_builder = BoardBuilder::from(original_board);
board_builder
.castle_rights(Color::White, chess::CastleRights::NoRights)
.castle_rights(Color::Black, chess::CastleRights::NoRights);
board_builder.clear_square(sq);
if let Ok(eval_board) = board_builder.try_into() {
bm_runner.set_board(eval_board);
sq_values[sq.to_index()] = Some(base_eval - bm_runner.raw_eval().raw());
for i in 0..2 {
println!();
println!("bucket {}", i);
let base_eval = nnue.feed_forward(&original_board, i);

let mut sq_values = [None; 64];
for sq in *original_board.combined() {
let mut board_builder = BoardBuilder::from(original_board);
board_builder
.castle_rights(Color::White, chess::CastleRights::NoRights)
.castle_rights(Color::Black, chess::CastleRights::NoRights);
board_builder.clear_square(sq);
if let Ok(eval_board) = board_builder.try_into() {
sq_values[sq.to_index()] = Some(base_eval - nnue.feed_forward(&eval_board, i));
}
}
}
for rank in 0_usize..8 {
for file in 0_usize..8 {
let index = (7 - rank) * 8 + file;
let value = if let Some(value) = sq_values[index] {
value.to_string()
} else {
"Unknown".to_string()
};
print!("{:>8}", value);
for rank in 0_usize..8 {
for file in 0_usize..8 {
let index = (7 - rank) * 8 + file;
let value = if let Some(value) = sq_values[index] {
value.to_string()
} else {
"Empty".to_string()
};
print!("{:>8}", value);
}
println!();
}
println!()
}
bm_runner.set_board(original_board);
}
Expand Down Expand Up @@ -281,7 +290,7 @@ impl UciAdapter {
let value = if let Some(value) = sq_values[index] {
value.to_string()
} else {
"Unknown".to_string()
"Empty".to_string()
};
print!("{:>8}", value);
}
Expand Down
6 changes: 0 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,3 @@ fn main() {
}
while bm_console.input(read!("{}\n")) {}
}

/*
35% HC
45% NNUE 1st gen
20% NNUE 2nd gen
*/

0 comments on commit 94d9380

Please sign in to comment.