From 3cee6881ee1639bd22d89ef43387c83c95f5067e Mon Sep 17 00:00:00 2001 From: Tomasz Sobczyk Date: Sun, 22 Nov 2020 17:18:06 +0100 Subject: [PATCH 1/2] Move the terminal position check to after qsearch, otherwise qsearch may end up in a terminal position. --- src/learn/learn.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/learn/learn.cpp b/src/learn/learn.cpp index 3942b606..cab5a9b5 100644 --- a/src/learn/learn.cpp +++ b/src/learn/learn.cpp @@ -685,10 +685,6 @@ namespace Learner int ply = 0; pos.do_move((Move)ps.move, state[ply++]); - // We want to position being trained on not to be terminal - if (MoveList(pos).size() == 0) - goto RETRY_READ; - // Evaluation value of shallow search (qsearch) const auto [_, pv] = Search::qsearch(pos); @@ -698,6 +694,10 @@ namespace Learner } } + // We want to position being trained on not to be terminal + if (MoveList(pos).size() == 0) + goto RETRY_READ; + // Since we have reached the end phase of PV, add the slope here. pos_add_grad(); } From ee13cfce67222faafca4e93f8af39fad3429d4bd Mon Sep 17 00:00:00 2001 From: Tomasz Sobczyk Date: Sun, 22 Nov 2020 17:02:33 +0100 Subject: [PATCH 2/2] Fix result assigned for a psvector when the positions are not continuous. --- src/learn/gensfen.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/learn/gensfen.cpp b/src/learn/gensfen.cpp index b265da71..5f8bbba1 100644 --- a/src/learn/gensfen.cpp +++ b/src/learn/gensfen.cpp @@ -191,7 +191,8 @@ namespace Learner PSVector& sfens, int8_t lastTurnIsWin, std::atomic& counter, - uint64_t limit); + uint64_t limit, + Color result_color); void report(uint64_t done, uint64_t new_done); @@ -291,7 +292,7 @@ namespace Learner vector move_hist_scores; auto flush_psv = [&](int8_t result) { - quit = commit_psv(th, packed_sfens, result, counter, limit); + quit = commit_psv(th, packed_sfens, result, counter, limit, pos.side_to_move()); }; for (int ply = 0; ; ++ply) @@ -717,7 +718,8 @@ namespace Learner PSVector& sfens, int8_t result, std::atomic& counter, - uint64_t limit) + uint64_t limit, + Color result_color) { if (!params.write_out_draw_game_in_training_data_generation && result == 0) { @@ -725,13 +727,17 @@ namespace Learner return false; } + auto side_to_move_from_sfen = [](auto& sfen){ + return (Color)(sfen.sfen.data[0] & 1); + }; + // From the final stage (one step before) to the first stage, give information on the outcome of the game for each stage. // The phases stored in sfens are assumed to be continuous (in order). for (auto it = sfens.rbegin(); it != sfens.rend(); ++it) { - // If is_win == 0 (draw), multiply by -1 and it will remain 0 (draw) - result = -result; - it->game_result = result; + // The side to move is packed as the lowest bit of the first byte + const Color side_to_move = side_to_move_from_sfen(*it); + it->game_result = side_to_move == result_color ? result : -result; } // Write sfens in move order to make potential compression easier