Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

📦 NEW: Use SCreLU for value net #354

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/chess/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ impl Board {
board.ep = ep;
board.castling = castling;

board.hash = board.generate_zobrist_hash();

for (idx, pc) in pieces.iter().enumerate() {
for sq in *pc {
board.piece_at[sq] = Piece::from(idx);
}
}

board.hash = board.generate_zobrist_hash();

board
}

Expand Down
25 changes: 25 additions & 0 deletions src/nets.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
use bytemuck::{self, Pod, Zeroable};
use goober::activation::Activation;
use std::fs;
use std::io::Write;
use std::path::Path;

// Workaround for error in how goober handles an activation such as SCReLU
#[derive(Clone, Copy)]
pub struct SCReLU;

impl Activation for SCReLU {
fn activate(x: f32) -> f32 {
let clamped = x.clamp(0.0, 1.0);
clamped * clamped
}

fn derivative(x: f32) -> f32 {
if 0.0 < x && x < 1.0 {
2.0 * x.sqrt()
} else {
0.0
}
}
}

#[derive(Clone, Copy, Debug, Zeroable)]
#[repr(C)]
pub struct Accumulator<const H: usize> {
Expand Down Expand Up @@ -33,6 +53,11 @@ pub fn relu(x: i16) -> i32 {
i32::from(x).max(0)
}

pub fn screlu(x: i16, q: i32) -> i32 {
let clamped = i32::from(x).clamp(0, q);
clamped * clamped
}

pub fn q_i16(x: f32, q: i32) -> i16 {
let quantized = x * q as f32;
assert!(f32::from(i16::MIN) < quantized && quantized < f32::from(i16::MAX),);
Expand Down
Binary file modified src/nets/value.bin
Binary file not shown.
14 changes: 8 additions & 6 deletions src/value.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bytemuck::{allocation, Pod, Zeroable};
use goober::activation::{ReLU, Tanh};
use goober::activation::Tanh;
use goober::layer::{DenseConnected, SparseConnected};
use goober::{FeedForwardNetwork, OutputLayer, SparseVector, Vector};
use std::boxed::Box;
Expand All @@ -9,7 +9,7 @@ use std::path::Path;

use crate::math::{randomize_dense, randomize_sparse, Rng};
use crate::mem::Align64;
use crate::nets::{q_i16, q_i32, relu, save_to_bin, Accumulator};
use crate::nets::{q_i16, q_i32, save_to_bin, screlu, Accumulator, SCReLU};
use crate::state::{self, State};

const INPUT_SIZE: usize = state::VALUE_NUMBER_FEATURES;
Expand All @@ -20,7 +20,7 @@ const QA: i32 = 256;
const QB: i32 = 256;
const QAB: i32 = QA * QB;

type Feature = SparseConnected<ReLU, INPUT_SIZE, HIDDEN_SIZE>;
type Feature = SparseConnected<SCReLU, INPUT_SIZE, HIDDEN_SIZE>;
type Output = DenseConnected<Tanh, { HIDDEN_SIZE * 2 }, OUTPUT_SIZE>;

type QuantizedFeatureWeights = [Align64<Accumulator<HIDDEN_SIZE>>; INPUT_SIZE];
Expand Down Expand Up @@ -287,16 +287,18 @@ impl QuantizedValueNetwork {
}
});

let mut result: i32 = self.output_bias;
let mut result: i32 = 0;

for (&x, w) in stm.vals.iter().zip(self.output_weights[0].vals) {
result += relu(x) * i32::from(w);
result += screlu(x, QA) * i32::from(w);
}

for (&x, w) in nstm.vals.iter().zip(self.output_weights[1].vals) {
result += relu(x) * i32::from(w);
result += screlu(x, QA) * i32::from(w);
}

result = result / QA + self.output_bias;

// Fifty move rule dampening
// Constants are chosen to make the max effect more significant at higher levels and max 50%
let hmc = state.halfmove_clock();
Expand Down
Loading