Skip to content

Commit

Permalink
miscellaneous-cleanups (#186)
Browse files Browse the repository at this point in the history
Elo   | -0.61 +- 1.91 (95%)
SPRT  | 8.0+0.08s Threads=1 Hash=32MB
LLR   | 0.43 (-2.94, 2.94) [-3.00, 0.00]
Games | N: 62898 W: 15569 L: 15679 D: 31650
Penta | [317, 6521, 17876, 6425, 310]
http://chess.grantnet.us/test/34956/
bench: 4473743
  • Loading branch information
connormcmonigle authored Jan 6, 2024
1 parent f152efd commit a3d4aa5
Show file tree
Hide file tree
Showing 56 changed files with 166 additions and 111 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
IndentWidth: 2
ColumnLimit: 150
AlignAfterOpenBracket: AlwaysBreak
AllowAllParametersOfDeclarationOnNextLine: 'false'
AllowShortBlocksOnASingleLine: 'true'
AllowShortCaseLabelsOnASingleLine: 'true'
AllowShortFunctionsOnASingleLine: All
Expand Down
3 changes: 2 additions & 1 deletion build/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ profileuse: CXXFLAGS += -fprofile-use
profileuse: flto

.PHONY: flto
flto: CXXFLAGS += -flto -fwhole-program
flto: CXXFLAGS += -flto -flto-partition=one -fwhole-program
flto: LDFLAGS += -flto=jobserver
flto: binary

.PHONY: plain
Expand Down
2 changes: 1 addition & 1 deletion include/chess/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,4 @@ struct board {

[[maybe_unused]] std::ostream& operator<<(std::ostream& ostr, const board& bd) noexcept;

} // namespace chess
} // namespace chess
1 change: 0 additions & 1 deletion include/chess/board_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ struct manifest {
[[nodiscard]] constexpr const square_set& queen() const noexcept { return queen_; }
[[nodiscard]] constexpr const square_set& king() const noexcept { return king_; }


template <typename S>
[[maybe_unused]] manifest& add_piece(const piece_type& pt, const S& at) noexcept;

Expand Down
2 changes: 1 addition & 1 deletion include/chess/castle_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,4 @@ struct castle_info_<color::black> {
template <color c>
inline constexpr castle_info_<c> castle_info = castle_info_<c>{};

} // namespace chess
} // namespace chess
18 changes: 9 additions & 9 deletions include/chess/cuckoo_hash_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,18 @@ struct cuckoo_hash_table_impl {
return std::nullopt;
}

constexpr void insert(zobrist::hash_type hash, cuckoo_hash_table_entry entry) noexcept {
constexpr void insert(zobrist::hash_type hash, cuckoo_hash_table_entry entry) noexcept {
std::size_t index = a_hash_function(hash);

for (;;) {
util::copy_swap(entry, entries_[index]);
util::copy_swap(hash, hashes_[index]);

if (hash == initial_hash) { break; }
const std::size_t a_index = a_hash_function(hash);
const std::size_t b_index = b_hash_function(hash);
index = (index == a_index) ? b_index : a_index;
util::copy_swap(entry, entries_[index]);
util::copy_swap(hash, hashes_[index]);

if (hash == initial_hash) { break; }

const std::size_t a_index = a_hash_function(hash);
const std::size_t b_index = b_hash_function(hash);
index = (index == a_index) ? b_index : a_index;
}
}

Expand Down
2 changes: 1 addition & 1 deletion include/chess/move.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,4 @@ struct move_hash {
[[nodiscard]] std::size_t operator()(const move& mv) const noexcept { return std::hash<std::uint32_t>{}(mv.data); }
};

} // namespace chess
} // namespace chess
2 changes: 1 addition & 1 deletion include/chess/move_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,4 @@ struct move_list {

std::ostream& operator<<(std::ostream& ostr, const move_list& mv_ls) noexcept;

} // namespace chess
} // namespace chess
2 changes: 1 addition & 1 deletion include/chess/pawn_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ struct pawn_info<color::black> {
static constexpr delta step = delta{0, -1};
};

} // namespace chess
} // namespace chess
2 changes: 1 addition & 1 deletion include/engine/bench.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,4 @@ struct bench_info {
[[nodiscard]] std::size_t perft(const chess::board& bd, const search::depth_type& depth) noexcept;
[[nodiscard]] bench_info get_perft_info(const chess::board& bd, const search::depth_type& depth) noexcept;

} // namespace engine
} // namespace engine
2 changes: 1 addition & 1 deletion include/engine/command_lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,4 @@ struct command_lexer {
[[nodiscard]] lexed_command lex(const std::string& input) const noexcept { return lexed_command(input); }
};

} // namespace engine
} // namespace engine
2 changes: 1 addition & 1 deletion include/engine/processor/condition_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ namespace def {

} // namespace def

} // namespace engine::processor
} // namespace engine::processor
2 changes: 1 addition & 1 deletion include/engine/processor/consume_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ namespace def {

} // namespace def

} // namespace engine::processor
} // namespace engine::processor
2 changes: 1 addition & 1 deletion include/engine/processor/emit_all_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ constexpr auto emit_all = emit_all_type{};

}

} // namespace engine::processor
} // namespace engine::processor
2 changes: 1 addition & 1 deletion include/engine/processor/emit_n_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ constexpr auto emit_n = emit_n_type<N>{};

}

} // namespace engine::processor
} // namespace engine::processor
2 changes: 1 addition & 1 deletion include/engine/processor/emit_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ constexpr auto emit = emit_type<T>{};

}

} // namespace engine::processor
} // namespace engine::processor
2 changes: 1 addition & 1 deletion include/engine/processor/invoke_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ template <typename I>

} // namespace def

} // namespace engine::processor
} // namespace engine::processor
2 changes: 1 addition & 1 deletion include/engine/processor/key_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ template <typename T>

} // namespace def

} // namespace engine::processor
} // namespace engine::processor
2 changes: 1 addition & 1 deletion include/engine/processor/null_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ constexpr auto null = null_type{};

}

} // namespace engine::processor
} // namespace engine::processor
2 changes: 1 addition & 1 deletion include/engine/processor/parallel_combinator_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ template <typename... Ts>

} // namespace def

} // namespace engine::processor
} // namespace engine::processor
2 changes: 1 addition & 1 deletion include/engine/processor/receiver_combinator_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ struct receiver_combinator_type {
constexpr receiver_combinator_type(const A& processor, const B& receiver) noexcept : processor_{processor}, receiver_{receiver} {}
};

} // namespace engine::processor
} // namespace engine::processor
2 changes: 1 addition & 1 deletion include/engine/processor/sequential_combinator_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ template <typename... Ts>

} // namespace def

} // namespace engine::processor
} // namespace engine::processor
2 changes: 1 addition & 1 deletion include/feature/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,4 @@ template <chess::color us, chess::color p>
return major * (ks.index() ^ mirror_constant<us>)+offset<us, p>(pt) + (sq.index() ^ mirror_constant<us>);
}

} // namespace feature::half_ka
} // namespace feature::half_ka
2 changes: 1 addition & 1 deletion include/nnue/aligned_scratchpad.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ struct aligned_scratchpad {
}
};

} // namespace nnue
} // namespace nnue
3 changes: 2 additions & 1 deletion include/nnue/aligned_slice.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ inline std::ostream& operator<<(std::ostream& ostr, const aligned_slice<T, dim>&
ostr << vec.data[dim - 1] << "])";
return ostr;
}
} // namespace nnue

} // namespace nnue
2 changes: 1 addition & 1 deletion include/nnue/aligned_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,4 @@ inline std::ostream& operator<<(std::ostream& ostr, const aligned_vector<T, dim>
return ostr;
}

} // namespace nnue
} // namespace nnue
2 changes: 1 addition & 1 deletion include/nnue/dense_relu_affine_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,4 @@ struct dense_relu_affine_layer {
}
};

} // namespace nnue
} // namespace nnue
2 changes: 1 addition & 1 deletion include/nnue/dot_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ struct dot_type_impl<std::int32_t> {
template <typename T>
using dot_type = typename dot_type_impl<T>::type;

}
} // namespace nnue
2 changes: 1 addition & 1 deletion include/nnue/embedded_weights.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ extern "C" {
INCBIN(weights_file, EVALFILE);
}

} // namespace nnue::embed
} // namespace nnue::embed
53 changes: 42 additions & 11 deletions include/nnue/eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,47 @@
#include <cstdint>
#include <iostream>
#include <string>
#include <type_traits>
#include <utility>

namespace nnue {

struct void_encoding {};

struct void_final_output_encoder {
template <typename... Ts>
[[maybe_unused]] constexpr void_encoding operator()(Ts&&...) const noexcept {
return void_encoding{};
}
};

template <typename T>
struct propagate_data {
T final_output_encoding;
weights::parameter_type result;

constexpr propagate_data(const T& final_output_encoding, const weights::parameter_type& result) noexcept
: final_output_encoding{final_output_encoding}, result{result} {}
};

template <typename T>
struct evaluate_data {
T final_output_encoding;
search::score_type result;

constexpr evaluate_data(const T& final_output_encoding, const search::score_type& result) noexcept
: final_output_encoding{final_output_encoding}, result{result} {}
};

struct eval : public chess::sided<eval, feature_transformer<weights::quantized_parameter_type, feature::half_ka::numel, weights::base_dim>> {
static constexpr std::size_t base_dim = weights::base_dim;
static constexpr std::size_t feature_transformer_dim = 2 * base_dim;
static constexpr std::size_t scratchpad_depth = 256;
static constexpr std::size_t final_layer_dimension = 24;

using parameter_type = weights::parameter_type;
using quantized_parameter_type = weights::quantized_parameter_type;
using final_output_type = aligned_vector<parameter_type, final_layer_dimension>;
using scratchpad_type = aligned_scratchpad<quantized_parameter_type, scratchpad_depth * feature_transformer_dim>;

const quantized_weights* weights_;
Expand All @@ -58,31 +88,29 @@ struct eval : public chess::sided<eval, feature_transformer<weights::quantized_p
feature_transformer<quantized_parameter_type, feature::half_ka::numel, weights::base_dim> white;
feature_transformer<quantized_parameter_type, feature::half_ka::numel, weights::base_dim> black;

[[nodiscard]] inline std::pair<zobrist::quarter_hash_type, parameter_type> propagate(const bool pov) const noexcept {
template <typename F>
[[nodiscard]] inline propagate_data<std::invoke_result_t<F, final_output_type>> propagate(const bool pov, F&& final_output_encoder) const noexcept {
const auto x1 = (pov ? weights_->white_fc0 : weights_->black_fc0).forward(base_).dequantized<parameter_type>(weights::dequantization_scale);
const auto x2 = concat(x1, weights_->fc1.forward(x1));
const auto x3 = concat(x2, weights_->fc2.forward(x2));

constexpr std::size_t dimension = decltype(x3)::dimension;
const zobrist::quarter_hash_type quarter_hash = zobrist::zobrist_hasher<zobrist::quarter_hash_type, dimension>.compute_hash(
[&x3](const std::size_t& i) { return x3.data[i] > parameter_type{}; });

return std::pair(quarter_hash, weights_->fc3.forward(x3).item());
return propagate_data(final_output_encoder(x3), weights_->fc3.forward(x3).item());
}

[[nodiscard]] inline std::pair<zobrist::hash_type, search::score_type> evaluate(const bool pov, const parameter_type& phase) const noexcept {
template <typename F = void_final_output_encoder>
[[nodiscard]] inline evaluate_data<std::invoke_result_t<F, final_output_type>>
evaluate(const bool pov, const parameter_type& phase, F&& final_output_encoder = void_final_output_encoder{}) const noexcept {
constexpr auto one = static_cast<parameter_type>(1.0);
constexpr auto mg = static_cast<parameter_type>(0.7);
constexpr auto eg = static_cast<parameter_type>(0.55);

const auto [hash, prediction] = propagate(pov);
const auto [final_output_encoding, prediction] = propagate(pov, std::forward<F>(final_output_encoder));
const parameter_type eval = phase * mg * prediction + (one - phase) * eg * prediction;

const parameter_type value =
search::logit_scale<parameter_type> * std::clamp(eval, search::min_logit<parameter_type>, search::max_logit<parameter_type>);

const auto score = static_cast<search::score_type>(value);
return std::pair(hash, score);
return evaluate_data(final_output_encoding, score);
}

[[nodiscard]] eval next_child() const noexcept {
Expand All @@ -91,7 +119,10 @@ struct eval : public chess::sided<eval, feature_transformer<weights::quantized_p
}

eval(
const quantized_weights* src, scratchpad_type* scratchpad, const std::size_t& parent_scratchpad_idx, const std::size_t& scratchpad_idx) noexcept
const quantized_weights* src,
scratchpad_type* scratchpad,
const std::size_t& parent_scratchpad_idx,
const std::size_t& scratchpad_idx) noexcept
: weights_{src},
scratchpad_{scratchpad},
scratchpad_idx_{scratchpad_idx},
Expand Down
2 changes: 1 addition & 1 deletion include/nnue/feature_transformer.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ struct feature_transformer {
: weights_{src}, parent_slice_{parent_slice}, slice_{slice} {}
};

} // namespace nnue
} // namespace nnue
16 changes: 8 additions & 8 deletions include/nnue/simd.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ struct int16_add_add_sub_sub_x128 {
static constexpr std::size_t num_units = 4;
static constexpr bool available = divides<dim, num_units * per_unit<vector_512, std::int16_t>>;

static inline void f(
const std::int16_t* a_0, const std::int16_t* a_1, const std::int16_t* s_0, const std::int16_t* s_1, std::int16_t* out) noexcept {
static inline void
f(const std::int16_t* a_0, const std::int16_t* a_1, const std::int16_t* s_0, const std::int16_t* s_1, std::int16_t* out) noexcept {
for (std::size_t i(0); i < dim; i += num_units * per_unit<vector_512, std::int16_t>) {
{
const __m512i a_0_0 = _mm512_load_si512((__m512i*)(a_0 + i + 0 * per_unit<vector_512, std::int16_t>));
Expand Down Expand Up @@ -292,8 +292,8 @@ struct int16_add_add_sub_sub_x128 {
};

template <std::size_t dim>
inline void add_add_sub_sub(
const std::int16_t* a_0, const std::int16_t* a_1, const std::int16_t* s_0, const std::int16_t* s_1, std::int16_t* out) noexcept {
inline void
add_add_sub_sub(const std::int16_t* a_0, const std::int16_t* a_1, const std::int16_t* s_0, const std::int16_t* s_1, std::int16_t* out) noexcept {
return overload_set<int16_add_add_sub_sub_x128<dim>>::f(a_0, a_1, s_0, s_1, out);
}

Expand Down Expand Up @@ -541,8 +541,8 @@ struct int16_add_add_sub_sub_x64 {
static constexpr std::size_t num_units = 4;
static constexpr bool available = divides<dim, num_units * per_unit<vector_256, std::int16_t>>;

static inline void f(
const std::int16_t* a_0, const std::int16_t* a_1, const std::int16_t* s_0, const std::int16_t* s_1, std::int16_t* out) noexcept {
static inline void
f(const std::int16_t* a_0, const std::int16_t* a_1, const std::int16_t* s_0, const std::int16_t* s_1, std::int16_t* out) noexcept {
for (std::size_t i(0); i < dim; i += num_units * per_unit<vector_256, std::int16_t>) {
{
const __m256i a_0_0 = _mm256_load_si256((__m256i*)(a_0 + i + 0 * per_unit<vector_256, std::int16_t>));
Expand Down Expand Up @@ -584,8 +584,8 @@ struct int16_add_add_sub_sub_x64 {
};

template <std::size_t dim>
inline void add_add_sub_sub(
const std::int16_t* a_0, const std::int16_t* a_1, const std::int16_t* s_0, const std::int16_t* s_1, std::int16_t* out) noexcept {
inline void
add_add_sub_sub(const std::int16_t* a_0, const std::int16_t* a_1, const std::int16_t* s_0, const std::int16_t* s_1, std::int16_t* out) noexcept {
return overload_set<int16_add_add_sub_sub_x64<dim>>::f(a_0, a_1, s_0, s_1, out);
}

Expand Down
7 changes: 5 additions & 2 deletions include/nnue/sparse_affine_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ struct sparse_affine_layer {
}

void insert_erase_idx(
const std::size_t insert_idx, const std::size_t erase_idx, const aligned_slice<T, b_numel>& src, aligned_slice<T, b_numel> dst) const {
const std::size_t insert_idx,
const std::size_t erase_idx,
const aligned_slice<T, b_numel>& src,
aligned_slice<T, b_numel> dst) const {
const T* insert_mem_region = W + insert_idx * dim1;
const T* erase_mem_region = W + erase_idx * dim1;
simd::add_add_sub<b_numel>(src.data, insert_mem_region, erase_mem_region, dst.data);
Expand Down Expand Up @@ -116,4 +119,4 @@ struct sparse_affine_layer {
}
};

} // namespace nnue
} // namespace nnue
Loading

0 comments on commit a3d4aa5

Please sign in to comment.