Skip to content

Commit

Permalink
reset-cache-cleanup (#162)
Browse files Browse the repository at this point in the history
ELO   | -0.69 +- 1.05 (95%)
SPRT  | 8.0+0.08s Threads=1 Hash=32MB
LLR   | 2.97 (-2.94, 2.94) [-3.00, 0.00]
GAMES | N: 197768 W: 46691 L: 47083 D: 103994
bench: 3853493
  • Loading branch information
connormcmonigle authored Jun 21, 2023
1 parent 7d3da06 commit b558ca6
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 38 deletions.
26 changes: 14 additions & 12 deletions include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <board_state.h>
#include <chess_types.h>
#include <feature_util.h>
#include <piece_configuration.h>
#include <position_history.h>
#include <square.h>
#include <table_generation.h>
Expand Down Expand Up @@ -829,31 +830,32 @@ struct board {
template <color c, typename T0, typename T1>
void half_feature_partial_reset_(const move& mv, T0& feature_reset_cache, T1& sided_set) const {
namespace h_ka = feature::half_ka;

const square our_king = mv.to();
auto* entry = feature_reset_cache.template us<c>().look_up(our_king);

auto& entry = feature_reset_cache.template us<c>().look_up(our_king);
sided_piece_configuration& config = entry.config;

over_types([&](const piece_type& pt) {
square_set& them_entry_plane = entry->config.template them<c>().get_plane(pt);
square_set& us_entry_plane = entry->config.template us<c>().get_plane(pt);
const square_set them_entry_plane = config.them<c>().get_plane(pt);
const square_set us_entry_plane = config.us<c>().get_plane(pt);

const square_set them_board_plane = man_.them<c>().get_plane(pt).excluding(mv.to());
const square_set us_board_plane = [&] {
if (pt == piece_type::king) { return man_.us<c>().get_plane(pt).excluding(mv.from()).insert(mv.to()); }
if (pt == piece_type::king) { return square_set::of(our_king); }
return man_.us<c>().get_plane(pt).excluding(mv.from());
}();

for (const auto sq : (us_entry_plane & ~us_board_plane)) { entry->erase(h_ka::index<c, c>(our_king, pt, sq)); }
for (const auto sq : (them_entry_plane & ~them_board_plane)) { entry->erase(h_ka::index<c, opponent<c>>(our_king, pt, sq)); }
for (const auto sq : them_entry_plane & ~them_board_plane) { entry.erase(h_ka::index<c, opponent<c>>(our_king, pt, sq)); }
for (const auto sq : (us_entry_plane & ~us_board_plane)) { entry.erase(h_ka::index<c, c>(our_king, pt, sq)); }

for (const auto sq : (us_board_plane & ~us_entry_plane)) { entry->insert(h_ka::index<c, c>(our_king, pt, sq)); }
for (const auto sq : (them_board_plane & ~them_entry_plane)) { entry->insert(h_ka::index<c, opponent<c>>(our_king, pt, sq)); }
for (const auto sq : them_board_plane & ~them_entry_plane) { entry.insert(h_ka::index<c, opponent<c>>(our_king, pt, sq)); }
for (const auto sq : us_board_plane & ~us_entry_plane) { entry.insert(h_ka::index<c, c>(our_king, pt, sq)); }

us_entry_plane = us_board_plane;
them_entry_plane = them_board_plane;
config.them<c>().set_plane(pt, them_board_plane);
config.us<c>().set_plane(pt, us_board_plane);
});

entry->copy_state_to(sided_set.template us<c>());
entry.copy_state_to(sided_set.template us<c>());
}

template <color pov, color p, typename T>
Expand Down
32 changes: 7 additions & 25 deletions include/nnue_feature_reset_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,26 @@
#pragma once

#include <board.h>
#include <chess_types.h>
#include <nnue_model.h>
#include <nnue_util.h>
#include <search_constants.h>
#include <zobrist_util.h>
#include <piece_configuration.h>
#include <square.h>

#include <array>
#include <optional>

namespace nnue {

struct piece_configuration {
chess::square_set pawn_{};
chess::square_set knight_{};
chess::square_set bishop_{};
chess::square_set rook_{};
chess::square_set queen_{};
chess::square_set king_{};

chess::square_set& get_plane(const chess::piece_type& pt) { return chess::get_member(pt, *this); }
const chess::square_set& get_plane(const chess::piece_type& pt) const { return chess::get_member(pt, *this); }
};

struct sided_piece_configuration : chess::sided<sided_piece_configuration, piece_configuration> {
piece_configuration white;
piece_configuration black;

sided_piece_configuration() : white{}, black{} {}
};

struct feature_reset_cache_entry {
static constexpr size_t dim = weights::base_dim;

using parameter_type = weights::quantized_parameter_type;
using weights_type = big_affine<parameter_type, feature::half_ka::numel, dim>;

const weights_type* weights_;
chess::sided_piece_configuration config;
aligned_slice<parameter_type, dim> slice_;
sided_piece_configuration config;

void insert(const size_t& idx) { weights_->insert_idx(idx, slice_); }
void erase(const size_t& idx) { weights_->erase_idx(idx, slice_); }
Expand All @@ -64,12 +46,12 @@ struct feature_reset_cache_entry {
void reinitialize(const weights_type* weights, const aligned_slice<parameter_type, dim>& slice) {
weights_ = weights;
slice_ = slice;
config = sided_piece_configuration{};

slice_.copy_from(weights_->b);
config = chess::sided_piece_configuration{};
}

feature_reset_cache_entry() : slice_{nullptr}, config{} {}
feature_reset_cache_entry() : config{}, slice_{nullptr} {}
};

struct feature_reset_cache {
Expand All @@ -79,7 +61,7 @@ struct feature_reset_cache {
stack_scratchpad<entry_type::parameter_type, num_squares * entry_type::dim> scratchpad_{};
feature_reset_cache_entry entries_[num_squares]{};

feature_reset_cache_entry* look_up(const chess::square& sq) { return entries_ + sq.index(); }
feature_reset_cache_entry& look_up(const chess::square& sq) { return entries_[sq.index()]; }

void reinitialize(const weights* weights) {
for (size_t i(0); i < num_squares; ++i) {
Expand Down
44 changes: 44 additions & 0 deletions include/piece_configuration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Seer is a UCI chess engine by Connor McMonigle
Copyright (C) 2021 Connor McMonigle
Seer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Seer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <chess_types.h>
#include <square.h>

namespace chess {

struct piece_configuration {
square_set pawn_{};
square_set knight_{};
square_set bishop_{};
square_set rook_{};
square_set queen_{};
square_set king_{};

const square_set& get_plane(const piece_type& pt) const { return get_member(pt, *this); }
void set_plane(const piece_type& pt, const square_set& plane) { get_member(pt, *this) = plane; }
};

struct sided_piece_configuration : sided<sided_piece_configuration, piece_configuration> {
piece_configuration white;
piece_configuration black;

sided_piece_configuration() : white{}, black{} {}
};

} // namespace chess
10 changes: 9 additions & 1 deletion include/square.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <array>
#include <cassert>
#include <utility>
#include <cmath>
#include <cstdint>
#include <iostream>
Expand Down Expand Up @@ -210,11 +211,18 @@ struct square_set {
}

template <typename I>
constexpr bool occ(I idx) const {
constexpr bool occ(const I& idx) const {
static_assert(std::is_integral_v<I>, "idx must be of integral type");
return static_cast<bool>(data & (one << static_cast<std::uint64_t>(idx)));
}

template <typename ... Ts>
static constexpr square_set of(Ts&& ... ts) {
auto bit_board = [](auto&& sq) { return sq.bit_board(); };
auto bit_wise_or = [](auto&& ... args) { return (args | ...); };
return square_set(bit_wise_or(bit_board(std::forward<Ts>(ts))...));
}

constexpr square_set() : data{0} {}
constexpr square_set(const std::uint64_t& set) : data{set} {}
};
Expand Down

0 comments on commit b558ca6

Please sign in to comment.