Remove conditional compilation on EVAL_LEARN

This commit is contained in:
Tomasz Sobczyk
2020-09-12 16:19:24 +02:00
parent 8d499e6efa
commit d33e7a9b07
32 changed files with 6 additions and 144 deletions

View File

@@ -1,8 +1,6 @@
#ifndef _EVALUATE_COMMON_H_
#define _EVALUATE_COMMON_H_
#if defined(EVAL_LEARN)
// A common header-like function for modern evaluation functions.
#include <string>
@@ -21,6 +19,4 @@ namespace Eval
double get_eta();
}
#endif // defined(EVAL_LEARN)
#endif // _EVALUATE_KPPT_COMMON_H_

View File

@@ -43,11 +43,8 @@ namespace Eval {
return UseNNUEMode::False;
else if (mode == "true")
return UseNNUEMode::True;
#ifdef EVAL_LEARN
else if (mode == "pure")
return UseNNUEMode::Pure;
#endif
return UseNNUEMode::False;
}
@@ -955,11 +952,9 @@ make_v:
/// evaluation of the position from the point of view of the side to move.
Value Eval::evaluate(const Position& pos) {
#ifdef EVAL_LEARN
if (useNNUE == UseNNUEMode::Pure) {
return NNUE::evaluate(pos);
}
#endif
bool classical = useNNUE == UseNNUEMode::False
|| abs(eg_value(pos.psq_score())) * 16 > NNUEThreshold1 * (16 + pos.rule50_count());

View File

@@ -29,11 +29,8 @@ namespace Eval {
enum struct UseNNUEMode
{
False,
True
#ifdef EVAL_LEARN
,Pure
#endif
True,
Pure
};
std::string trace(const Position& pos);

View File

@@ -1,5 +1,3 @@
#if defined(EVAL_LEARN)
#include "convert.h"
#include "multi_think.h"
@@ -606,4 +604,3 @@ namespace Learner
convert(args);
}
}
#endif

View File

@@ -5,7 +5,6 @@
#include <string>
#include <sstream>
#if defined(EVAL_LEARN)
namespace Learner {
void convert_bin_from_pgn_extract(
const std::vector<std::string>& filenames,
@@ -32,6 +31,5 @@ namespace Learner {
void convert(std::istringstream& is);
}
#endif
#endif

View File

@@ -1,6 +1,4 @@
#if defined(EVAL_LEARN)
#include "gensfen.h"
#include "gensfen.h"
#include "packed_sfen.h"
#include "multi_think.h"
@@ -1207,4 +1205,3 @@ namespace Learner
std::cout << "gensfen finished." << endl;
}
}
#endif

View File

@@ -5,12 +5,10 @@
#include <sstream>
#if defined(EVAL_LEARN)
namespace Learner {
// Automatic generation of teacher position
void gen_sfen(Position& pos, std::istringstream& is);
}
#endif
#endif

View File

@@ -17,8 +17,6 @@
// → I will not be involved in the engine because it is a problem that the GUI should assist.
// etc..
#if defined(EVAL_LEARN)
#include "learn.h"
#include "convert.h"
@@ -2048,5 +2046,3 @@ namespace Learner
}
} // namespace Learner
#endif // EVAL_LEARN

View File

@@ -1,8 +1,6 @@
#ifndef _LEARN_H_
#define _LEARN_H_
#if defined(EVAL_LEARN)
// ----------------------
// Floating point for learning
// ----------------------
@@ -78,6 +76,4 @@ namespace Learner
void learn(Position& pos, std::istringstream& is);
}
#endif
#endif // ifndef _LEARN_H_

View File

@@ -1,7 +1,5 @@
#include "learning_tools.h"
#if defined (EVAL_LEARN)
#include "misc.h"
using namespace Eval;
@@ -18,5 +16,3 @@ namespace EvalLearningTools
uint64_t Weight::eta1_epoch;
uint64_t Weight::eta2_epoch;
}
#endif

View File

@@ -3,8 +3,6 @@
// A set of machine learning tools related to the weight array used for machine learning of evaluation functions
#if defined (EVAL_LEARN)
#include "learn.h"
#include "misc.h" // PRNG , my_insertion_sort
@@ -98,5 +96,4 @@ namespace EvalLearningTools
};
}
#endif // defined (EVAL_LEARN)
#endif

View File

@@ -1,6 +1,4 @@
#if defined(EVAL_LEARN)
#include "multi_think.h"
#include "multi_think.h"
#include "tt.h"
#include "uci.h"
@@ -118,6 +116,3 @@ void MultiThink::go_think()
Options[s.first] = std::string(s.second);
}
#endif // defined(EVAL_LEARN)

View File

@@ -1,8 +1,6 @@
#ifndef _MULTI_THINK_
#define _MULTI_THINK_
#if defined(EVAL_LEARN)
#include "learn.h"
#include "misc.h"
@@ -151,6 +149,4 @@ protected:
std::mutex task_mutex;
};
#endif // defined(EVAL_LEARN) && defined(YANEURAOU_2018_OTAFUKU_ENGINE)
#endif

View File

@@ -4,7 +4,6 @@
#include <vector>
#include <cstdint>
#if defined(EVAL_LEARN)
namespace Learner {
// packed sfen
@@ -45,5 +44,3 @@ namespace Learner {
using PSVector = std::vector<PackedSfenValue>;
}
#endif
#endif

View File

@@ -1,6 +1,4 @@
#if defined (EVAL_LEARN)
#include "sfen_packer.h"
#include "sfen_packer.h"
#include "packed_sfen.h"
@@ -402,6 +400,3 @@ namespace Learner {
return sfen;
}
}
#endif // USE_SFEN_PACKER

View File

@@ -1,8 +1,6 @@
#ifndef _SFEN_PACKER_H_
#define _SFEN_PACKER_H_
#if defined(EVAL_LEARN)
#include "types.h"
#include "learn/packed_sfen.h"
@@ -19,6 +17,4 @@ namespace Learner {
PackedSfen sfen_pack(Position& pos);
}
#endif
#endif

View File

@@ -1,7 +1,5 @@
// Code for learning NNUE evaluation function
#if defined(EVAL_LEARN)
#include <random>
#include <fstream>
#include <filesystem>
@@ -238,5 +236,3 @@ double get_eta() {
}
} // namespace Eval
#endif // defined(EVAL_LEARN)

View File

@@ -3,8 +3,6 @@
#ifndef _EVALUATE_NNUE_LEARNER_H_
#define _EVALUATE_NNUE_LEARNER_H_
#if defined(EVAL_LEARN)
#include "../learn/learn.h"
namespace Eval {
@@ -41,6 +39,4 @@ void CheckHealth();
} // namespace Eval
#endif // defined(EVAL_LEARN)
#endif

View File

@@ -3,8 +3,6 @@
#ifndef _NNUE_TRAINER_H_
#define _NNUE_TRAINER_H_
#if defined(EVAL_LEARN)
#include "../nnue_common.h"
#include "../features/index_list.h"
@@ -120,6 +118,4 @@ std::shared_ptr<T> MakeAlignedSharedPtr(ArgumentTypes&&... arguments) {
} // namespace Eval
#endif // defined(EVAL_LEARN)
#endif

View File

@@ -3,8 +3,6 @@
#ifndef _NNUE_TRAINER_AFFINE_TRANSFORM_H_
#define _NNUE_TRAINER_AFFINE_TRANSFORM_H_
#if defined(EVAL_LEARN)
#include "../../learn/learn.h"
#include "../layers/affine_transform.h"
#include "trainer.h"
@@ -296,6 +294,4 @@ class Trainer<Layers::AffineTransform<PreviousLayer, OutputDimensions>> {
} // namespace Eval
#endif // defined(EVAL_LEARN)
#endif

View File

@@ -3,8 +3,6 @@
#ifndef _NNUE_TRAINER_CLIPPED_RELU_H_
#define _NNUE_TRAINER_CLIPPED_RELU_H_
#if defined(EVAL_LEARN)
#include "../../learn/learn.h"
#include "../layers/clipped_relu.h"
#include "trainer.h"
@@ -137,6 +135,4 @@ class Trainer<Layers::ClippedReLU<PreviousLayer>> {
} // namespace Eval
#endif // defined(EVAL_LEARN)
#endif

View File

@@ -3,8 +3,6 @@
#ifndef _NNUE_TRAINER_FEATURE_TRANSFORMER_H_
#define _NNUE_TRAINER_FEATURE_TRANSFORMER_H_
#if defined(EVAL_LEARN)
#include "../../learn/learn.h"
#include "../nnue_feature_transformer.h"
#include "trainer.h"
@@ -372,6 +370,4 @@ class Trainer<FeatureTransformer> {
} // namespace Eval
#endif // defined(EVAL_LEARN)
#endif

View File

@@ -3,8 +3,6 @@
#ifndef _NNUE_TRAINER_INPUT_SLICE_H_
#define _NNUE_TRAINER_INPUT_SLICE_H_
#if defined(EVAL_LEARN)
#include "../../learn/learn.h"
#include "../layers/input_slice.h"
#include "trainer.h"
@@ -246,6 +244,4 @@ class Trainer<Layers::InputSlice<OutputDimensions, Offset>> {
} // namespace Eval
#endif // defined(EVAL_LEARN)
#endif

View File

@@ -3,8 +3,6 @@
#ifndef _NNUE_TRAINER_SUM_H_
#define _NNUE_TRAINER_SUM_H_
#if defined(EVAL_LEARN)
#include "../../learn/learn.h"
#include "../layers/sum.h"
#include "trainer.h"
@@ -185,6 +183,4 @@ class Trainer<Layers::Sum<PreviousLayer>> {
} // namespace Eval
#endif // defined(EVAL_LEARN)
#endif

View File

@@ -32,10 +32,8 @@
#include "uci.h"
#include "syzygy/tbprobe.h"
#if defined(EVAL_LEARN)
#include "learn/packed_sfen.h"
#include "learn/sfen_packer.h"
#endif
using std::string;
@@ -1352,8 +1350,6 @@ bool Position::pos_is_ok() const {
return true;
}
#if defined(EVAL_LEARN)
// Add a function that directly unpacks for speed. It's pretty tough.
// Write it by combining packer::unpack() and Position::set().
// If there is a problem with the passed phase and there is an error, non-zero is returned.
@@ -1385,5 +1381,3 @@ void Position::sfen_pack(Learner::PackedSfen& sfen)
{
sfen = Learner::sfen_pack(*this);
}
#endif

View File

@@ -30,10 +30,8 @@
#include "nnue/nnue_accumulator.h"
#if defined(EVAL_LEARN)
#include "learn/packed_sfen.h"
#include "learn/sfen_packer.h"
#endif
/// StateInfo struct stores information needed to restore a Position object to
@@ -177,7 +175,6 @@ public:
// Used by NNUE
StateInfo* state() const;
#if defined(EVAL_LEARN)
// --sfenization helper
friend int Learner::set_from_packed_sfen(Position& pos, const Learner::PackedSfen& sfen, StateInfo* si, Thread* th, bool mirror);
@@ -199,7 +196,6 @@ public:
// Returns the position of the ball on the c side.
Square king_square(Color c) const { return pieceList[make_piece(c, KING)][0]; }
#endif // EVAL_LEARN
private:
// Initialization helpers (used while setting up a position)

View File

@@ -54,9 +54,7 @@ using std::string;
using Eval::evaluate;
using namespace Search;
#if defined(EVAL_LEARN)
bool Search::prune_at_shallow_depth_on_pv_node = false;
#endif
namespace {
@@ -991,9 +989,7 @@ moves_loop: // When in check, search starts from here
ss->moveCount = ++moveCount;
if (rootNode && thisThread == Threads.main() && Time.elapsed() > 3000
#if defined(EVAL_LEARN)
&& !Limits.silent
#endif
)
sync_cout << "info depth " << depth
<< " currmove " << UCI::move(move, pos.is_chess960())
@@ -1011,9 +1007,7 @@ moves_loop: // When in check, search starts from here
// Step 12. Pruning at shallow depth (~200 Elo)
if ( !rootNode
#ifdef EVAL_LEARN
&& (PvNode ? prune_at_shallow_depth_on_pv_node : true)
#endif
&& pos.non_pawn_material(us)
&& bestValue > VALUE_TB_LOSS_IN_MAX_PLY)
{
@@ -1564,10 +1558,8 @@ moves_loop: // When in check, search starts from here
// Check for legality just before making the move
if (
#if defined(EVAL_LEARN)
// HACK: pos.piece_on(from_sq(m)) sometimes will be NO_PIECE during machine learning.
!pos.pseudo_legal(move) ||
#endif // EVAL_LEARN
!pos.legal(move)
)
{
@@ -1978,7 +1970,6 @@ void Tablebases::rank_root_moves(Position& pos, Search::RootMoves& rootMoves) {
// --- expose the functions such as fixed depth search used for learning to the outside
#if defined (EVAL_LEARN)
namespace Learner
{
@@ -2278,4 +2269,3 @@ namespace Learner
}
}
#endif

View File

@@ -32,10 +32,7 @@ namespace Search {
/// Threshold used for countermoves based pruning
constexpr int CounterMovePruneThreshold = 0;
#if defined(EVAL_LEARN)
extern bool prune_at_shallow_depth_on_pv_node;
#endif
/// Stack struct keeps track of the information we need to remember from nodes
/// shallower and deeper in the tree during the search. Each search thread has
@@ -90,9 +87,7 @@ struct LimitsType {
time[WHITE] = time[BLACK] = inc[WHITE] = inc[BLACK] = npmsec = movetime = TimePoint(0);
movestogo = depth = mate = perft = infinite = 0;
nodes = 0;
#if defined (EVAL_LEARN)
silent = false;
#endif
}
bool use_time_management() const {
@@ -103,11 +98,9 @@ struct LimitsType {
TimePoint time[COLOR_NB], inc[COLOR_NB], npmsec, movetime, startTime;
int movestogo, depth, mate, perft, infinite;
int64_t nodes;
#if defined (EVAL_LEARN)
// Silent mode that does not output to the screen (for continuous self-play in process)
// Do not output PV at this time.
bool silent;
#endif
};
extern LimitsType Limits;
@@ -117,7 +110,6 @@ void clear();
} // namespace Search
#if defined(EVAL_LEARN)
namespace Learner {
// A pair of reader and evaluation value. Returned by Learner::search(),Learner::qsearch().
@@ -126,6 +118,5 @@ namespace Learner {
ValueAndPV qsearch(Position& pos);
ValueAndPV search(Position& pos, int depth_, size_t multiPV = 1, uint64_t nodesLimit = 0);
}
#endif
#endif // #ifndef SEARCH_H_INCLUDED

View File

@@ -28,9 +28,7 @@
TranspositionTable TT; // Our global transposition table
#ifdef EVAL_LEARN
bool TranspositionTable::enable_transposition_table = true;
#endif
/// TTEntry::save() populates the TTEntry with a new node's data, possibly
/// overwriting an old position. Update is not atomic and can be racy.
@@ -120,12 +118,10 @@ void TranspositionTable::clear() {
TTEntry* TranspositionTable::probe(const Key key, bool& found) const {
#ifdef EVAL_LEARN
if (!enable_transposition_table) {
found = false;
return first_entry(0);
}
#endif
TTEntry* const tte = first_entry(key);
const uint16_t key16 = (uint16_t)key; // Use the low 16 bits as key inside the cluster

View File

@@ -84,9 +84,7 @@ public:
return &table[mul_hi64(key, clusterCount)].entry[0];
}
#ifdef EVAL_LEARN
static bool enable_transposition_table;
#endif
private:
friend struct TTEntry;

View File

@@ -245,7 +245,6 @@ double UCI::win_rate_model_double(double v, int ply) {
// Call qsearch(),search() directly for testing
// --------------------
#if defined(EVAL_LEARN)
void qsearch_cmd(Position& pos)
{
cout << "qsearch : ";
@@ -277,8 +276,6 @@ void search_cmd(Position& pos, istringstream& is)
cout << endl;
}
#endif
/// UCI::loop() waits for a command from stdin, parses it and calls the appropriate
/// function. Also intercepts EOF from stdin to ensure gracefully exiting if the
/// GUI dies unexpectedly. When called with some command line arguments, e.g. to
@@ -334,7 +331,7 @@ void UCI::loop(int argc, char* argv[]) {
else if (token == "d") sync_cout << pos << sync_endl;
else if (token == "eval") trace_eval(pos);
else if (token == "compiler") sync_cout << compiler_info() << sync_endl;
#if defined (EVAL_LEARN)
else if (token == "gensfen") Learner::gen_sfen(pos, is);
else if (token == "learn") Learner::learn(pos, is);
else if (token == "convert") Learner::convert(is);
@@ -343,8 +340,6 @@ void UCI::loop(int argc, char* argv[]) {
else if (token == "qsearch") qsearch_cmd(pos);
else if (token == "search") search_cmd(pos, is);
#endif
// test command
else if (token == "test") test_cmd(pos, is);
else

View File

@@ -42,14 +42,12 @@ void on_threads(const Option& o) { Threads.set(size_t(o)); }
void on_tb_path(const Option& o) { Tablebases::init(o); }
void on_use_NNUE(const Option& ) { Eval::init_NNUE(); }
void on_eval_file(const Option& ) { Eval::init_NNUE(); }
#ifdef EVAL_LEARN
void on_prune_at_shallow_depth_on_pv_node(const Option& o) {
Search::prune_at_shallow_depth_on_pv_node = o;
}
void on_enable_transposition_table(const Option& o) {
TranspositionTable::enable_transposition_table = o;
}
#endif
/// Our case insensitive less() function as required by UCI protocol
bool CaseInsensitiveLess::operator() (const string& s1, const string& s2) const {
@@ -86,11 +84,7 @@ void init(OptionsMap& o) {
o["SyzygyProbeDepth"] << Option(1, 1, 100);
o["Syzygy50MoveRule"] << Option(true);
o["SyzygyProbeLimit"] << Option(7, 0, 7);
#if defined(EVAL_LEARN)
o["Use NNUE"] << Option("true var true var false var pure", "true", on_use_NNUE);
#else
o["Use NNUE"] << Option("true var true var false", "true", on_use_NNUE);
#endif
// The default must follow the format nn-[SHA256 first 12 digits].nnue
// for the build process (profile-build and fishtest) to work.
o["EvalFile"] << Option("nn-82215d0fd0df.nnue", on_eval_file);
@@ -102,7 +96,6 @@ void init(OptionsMap& o) {
o["SkipLoadingEval"] << Option(false);
// how many moves to use a fixed move
// o["BookMoves"] << Option(16, 0, 10000);
#if defined(EVAL_LEARN)
// When learning the evaluation function, you can change the folder to save the evaluation function.
// Evalsave by default. This folder shall be prepared in advance.
// Automatically create a folder under this folder like "0/", "1/", ... and save the evaluation function file there.
@@ -111,7 +104,6 @@ void init(OptionsMap& o) {
o["PruneAtShallowDepthOnPvNode"] << Option(false, on_prune_at_shallow_depth_on_pv_node);
// Enable transposition table.
o["EnableTranspositionTable"] << Option(true, on_enable_transposition_table);
#endif
}