2008-08-31 23:59:13 -06:00
|
|
|
/*
|
2008-10-19 10:56:28 -06:00
|
|
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Add NNUE evaluation
This patch ports the efficiently updatable neural network (NNUE) evaluation to Stockfish.
Both the NNUE and the classical evaluations are available, and can be used to
assign a value to a position that is later used in alpha-beta (PVS) search to find the
best move. The classical evaluation computes this value as a function of various chess
concepts, handcrafted by experts, tested and tuned using fishtest. The NNUE evaluation
computes this value with a neural network based on basic inputs. The network is optimized
and trained on the evalutions of millions of positions at moderate search depth.
The NNUE evaluation was first introduced in shogi, and ported to Stockfish afterward.
It can be evaluated efficiently on CPUs, and exploits the fact that only parts
of the neural network need to be updated after a typical chess move.
[The nodchip repository](https://github.com/nodchip/Stockfish) provides additional
tools to train and develop the NNUE networks.
This patch is the result of contributions of various authors, from various communities,
including: nodchip, ynasu87, yaneurao (initial port and NNUE authors), domschl, FireFather,
rqs, xXH4CKST3RXx, tttak, zz4032, joergoster, mstembera, nguyenpham, erbsenzaehler,
dorzechowski, and vondele.
This new evaluation needed various changes to fishtest and the corresponding infrastructure,
for which tomtor, ppigazzini, noobpwnftw, daylen, and vondele are gratefully acknowledged.
The first networks have been provided by gekkehenker and sergiovieri, with the latter
net (nn-97f742aaefcd.nnue) being the current default.
The evaluation function can be selected at run time with the `Use NNUE` (true/false) UCI option,
provided the `EvalFile` option points the the network file (depending on the GUI, with full path).
The performance of the NNUE evaluation relative to the classical evaluation depends somewhat on
the hardware, and is expected to improve quickly, but is currently on > 80 Elo on fishtest:
60000 @ 10+0.1 th 1
https://tests.stockfishchess.org/tests/view/5f28fe6ea5abc164f05e4c4c
ELO: 92.77 +-2.1 (95%) LOS: 100.0%
Total: 60000 W: 24193 L: 8543 D: 27264
Ptnml(0-2): 609, 3850, 9708, 10948, 4885
40000 @ 20+0.2 th 8
https://tests.stockfishchess.org/tests/view/5f290229a5abc164f05e4c58
ELO: 89.47 +-2.0 (95%) LOS: 100.0%
Total: 40000 W: 12756 L: 2677 D: 24567
Ptnml(0-2): 74, 1583, 8550, 7776, 2017
At the same time, the impact on the classical evaluation remains minimal, causing no significant
regression:
sprt @ 10+0.1 th 1
https://tests.stockfishchess.org/tests/view/5f2906a2a5abc164f05e4c5b
LLR: 2.94 (-2.94,2.94) {-6.00,-4.00}
Total: 34936 W: 6502 L: 6825 D: 21609
Ptnml(0-2): 571, 4082, 8434, 3861, 520
sprt @ 60+0.6 th 1
https://tests.stockfishchess.org/tests/view/5f2906cfa5abc164f05e4c5d
LLR: 2.93 (-2.94,2.94) {-6.00,-4.00}
Total: 10088 W: 1232 L: 1265 D: 7591
Ptnml(0-2): 49, 914, 3170, 843, 68
The needed networks can be found at https://tests.stockfishchess.org/nns
It is recommended to use the default one as indicated by the `EvalFile` UCI option.
Guidelines for testing new nets can be found at
https://github.com/glinscott/fishtest/wiki/Creating-my-first-test#nnue-net-tests
Integration has been discussed in various issues:
https://github.com/official-stockfish/Stockfish/issues/2823
https://github.com/official-stockfish/Stockfish/issues/2728
The integration branch will be closed after the merge:
https://github.com/official-stockfish/Stockfish/pull/2825
https://github.com/official-stockfish/Stockfish/tree/nnue-player-wip
closes https://github.com/official-stockfish/Stockfish/pull/2912
This will be an exciting time for computer chess, looking forward to seeing the evolution of
this approach.
Bench: 4746616
2020-08-05 09:11:15 -06:00
|
|
|
Copyright (C) 2004-2020 The Stockfish developers (see AUTHORS file)
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2008-10-19 10:56:28 -06:00
|
|
|
Stockfish is free software: you can redistribute it and/or modify
|
2008-08-31 23:59:13 -06:00
|
|
|
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.
|
2009-01-07 06:28:04 -07:00
|
|
|
|
2008-10-19 10:56:28 -06:00
|
|
|
Stockfish is distributed in the hope that it will be useful,
|
2008-08-31 23:59:13 -06:00
|
|
|
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.
|
2009-01-07 06:28:04 -07:00
|
|
|
|
2008-08-31 23:59:13 -06:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2013-07-23 07:31:57 -06:00
|
|
|
#ifndef TYPES_H_INCLUDED
|
2008-08-31 23:59:13 -06:00
|
|
|
#define TYPES_H_INCLUDED
|
|
|
|
|
2015-01-09 04:35:44 -07:00
|
|
|
/// When compiling with provided Makefile (e.g. for Linux and OSX), configuration
|
|
|
|
/// is done automatically. To get started type 'make help'.
|
2011-12-30 09:08:07 -07:00
|
|
|
///
|
2015-01-09 04:35:44 -07:00
|
|
|
/// When Makefile is not used (e.g. with Microsoft Visual Studio) some switches
|
|
|
|
/// need to be set manually:
|
2011-12-30 09:08:07 -07:00
|
|
|
///
|
2015-01-09 04:35:44 -07:00
|
|
|
/// -DNDEBUG | Disable debugging mode. Always use this for release.
|
2011-12-30 09:08:07 -07:00
|
|
|
///
|
2015-01-09 04:35:44 -07:00
|
|
|
/// -DNO_PREFETCH | Disable use of prefetch asm-instruction. You may need this to
|
|
|
|
/// | run on some very old machines.
|
2011-12-30 09:08:07 -07:00
|
|
|
///
|
|
|
|
/// -DUSE_POPCNT | Add runtime support for use of popcnt asm-instruction. Works
|
2015-01-09 04:35:44 -07:00
|
|
|
/// | only in 64-bit mode and requires hardware with popcnt support.
|
2015-01-21 11:53:26 -07:00
|
|
|
///
|
|
|
|
/// -DUSE_PEXT | Add runtime support for use of pext asm-instruction. Works
|
|
|
|
/// | only in 64-bit mode and requires hardware with pext support.
|
2011-12-30 09:08:07 -07:00
|
|
|
|
2012-12-05 03:05:32 -07:00
|
|
|
#include <cassert>
|
2012-05-09 01:33:25 -06:00
|
|
|
#include <cctype>
|
2015-01-18 00:00:50 -07:00
|
|
|
#include <cstdint>
|
2011-01-15 02:47:11 -07:00
|
|
|
#include <cstdlib>
|
2019-09-21 01:59:32 -06:00
|
|
|
#include <algorithm>
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2015-01-18 00:00:50 -07:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
// Disable some silly and noisy warning from MSVC compiler
|
|
|
|
#pragma warning(disable: 4127) // Conditional expression is constant
|
|
|
|
#pragma warning(disable: 4146) // Unary minus operator applied to unsigned type
|
|
|
|
#pragma warning(disable: 4800) // Forcing value to bool 'true' or 'false'
|
|
|
|
#endif
|
2012-03-03 01:35:56 -07:00
|
|
|
|
2015-01-09 04:35:44 -07:00
|
|
|
/// Predefined macros hell:
|
|
|
|
///
|
|
|
|
/// __GNUC__ Compiler is gcc, Clang or Intel on Linux
|
|
|
|
/// __INTEL_COMPILER Compiler is Intel
|
|
|
|
/// _MSC_VER Compiler is MSVC or Intel on Windows
|
|
|
|
/// _WIN32 Building on Windows (any)
|
|
|
|
/// _WIN64 Building on Windows 64 bit
|
2013-07-15 13:09:06 -06:00
|
|
|
|
2015-06-28 02:24:10 -06:00
|
|
|
#if defined(_WIN64) && defined(_MSC_VER) // No Makefile used
|
2016-05-01 10:18:10 -06:00
|
|
|
# include <intrin.h> // Microsoft header for _BitScanForward64()
|
2011-12-30 09:08:07 -07:00
|
|
|
# define IS_64BIT
|
2010-05-21 13:17:59 -06:00
|
|
|
#endif
|
2009-07-03 01:40:41 -06:00
|
|
|
|
2016-05-01 02:57:50 -06:00
|
|
|
#if defined(USE_POPCNT) && (defined(__INTEL_COMPILER) || defined(_MSC_VER))
|
2016-05-01 10:18:10 -06:00
|
|
|
# include <nmmintrin.h> // Intel and Microsoft header for _mm_popcnt_u64()
|
2011-01-05 03:02:05 -07:00
|
|
|
#endif
|
|
|
|
|
2015-01-09 04:35:44 -07:00
|
|
|
#if !defined(NO_PREFETCH) && (defined(__INTEL_COMPILER) || defined(_MSC_VER))
|
|
|
|
# include <xmmintrin.h> // Intel and Microsoft header for _mm_prefetch()
|
|
|
|
#endif
|
|
|
|
|
2014-04-07 13:04:34 -06:00
|
|
|
#if defined(USE_PEXT)
|
2014-04-10 12:07:40 -06:00
|
|
|
# include <immintrin.h> // Header for _pext_u64() intrinsic
|
2015-01-20 14:17:22 -07:00
|
|
|
# define pext(b, m) _pext_u64(b, m)
|
2014-04-07 13:04:34 -06:00
|
|
|
#else
|
2017-05-07 02:26:09 -06:00
|
|
|
# define pext(b, m) 0
|
2014-04-07 13:04:34 -06:00
|
|
|
#endif
|
|
|
|
|
2013-07-23 07:31:57 -06:00
|
|
|
#ifdef USE_POPCNT
|
2018-03-18 16:38:58 -06:00
|
|
|
constexpr bool HasPopCnt = true;
|
2011-01-29 05:59:23 -07:00
|
|
|
#else
|
2018-03-18 16:38:58 -06:00
|
|
|
constexpr bool HasPopCnt = false;
|
2011-01-29 05:59:23 -07:00
|
|
|
#endif
|
|
|
|
|
2014-04-07 08:27:14 -06:00
|
|
|
#ifdef USE_PEXT
|
2018-03-18 16:38:58 -06:00
|
|
|
constexpr bool HasPext = true;
|
2014-04-07 08:27:14 -06:00
|
|
|
#else
|
2018-03-18 16:38:58 -06:00
|
|
|
constexpr bool HasPext = false;
|
2014-04-07 08:27:14 -06:00
|
|
|
#endif
|
|
|
|
|
2013-07-23 07:31:57 -06:00
|
|
|
#ifdef IS_64BIT
|
2018-03-18 16:38:58 -06:00
|
|
|
constexpr bool Is64Bit = true;
|
2011-01-29 05:59:23 -07:00
|
|
|
#else
|
2018-03-18 16:38:58 -06:00
|
|
|
constexpr bool Is64Bit = false;
|
2011-01-29 05:59:23 -07:00
|
|
|
#endif
|
|
|
|
|
2011-02-23 13:27:22 -07:00
|
|
|
typedef uint64_t Key;
|
|
|
|
typedef uint64_t Bitboard;
|
|
|
|
|
2018-03-18 16:38:58 -06:00
|
|
|
constexpr int MAX_MOVES = 256;
|
2019-05-15 01:52:27 -06:00
|
|
|
constexpr int MAX_PLY = 246;
|
2011-04-01 06:35:39 -06:00
|
|
|
|
2011-12-04 02:53:40 -07:00
|
|
|
/// A move needs 16 bits to be stored
|
|
|
|
///
|
|
|
|
/// bit 0- 5: destination square (from 0 to 63)
|
|
|
|
/// bit 6-11: origin square (from 0 to 63)
|
|
|
|
/// bit 12-13: promotion piece type - 2 (from KNIGHT-2 to QUEEN-2)
|
2013-12-01 02:25:10 -07:00
|
|
|
/// bit 14-15: special move flag: promotion (1), en passant (2), castling (3)
|
2014-02-14 01:42:50 -07:00
|
|
|
/// NOTE: EN-PASSANT bit is set only when a pawn can be captured
|
2011-12-04 02:53:40 -07:00
|
|
|
///
|
|
|
|
/// Special cases are MOVE_NONE and MOVE_NULL. We can sneak these in because in
|
|
|
|
/// any normal move destination square is always different from origin square
|
|
|
|
/// while MOVE_NONE and MOVE_NULL have the same origin and destination square.
|
|
|
|
|
2016-10-20 22:48:07 -06:00
|
|
|
enum Move : int {
|
2013-06-23 03:15:58 -06:00
|
|
|
MOVE_NONE,
|
2011-12-04 02:53:40 -07:00
|
|
|
MOVE_NULL = 65
|
|
|
|
};
|
|
|
|
|
2012-06-24 03:08:16 -06:00
|
|
|
enum MoveType {
|
2013-06-23 03:15:58 -06:00
|
|
|
NORMAL,
|
2012-06-24 03:08:16 -06:00
|
|
|
PROMOTION = 1 << 14,
|
|
|
|
ENPASSANT = 2 << 14,
|
2013-12-01 02:25:10 -07:00
|
|
|
CASTLING = 3 << 14
|
2011-12-04 02:53:40 -07:00
|
|
|
};
|
|
|
|
|
2014-03-02 03:53:17 -07:00
|
|
|
enum Color {
|
2017-08-19 06:32:31 -06:00
|
|
|
WHITE, BLACK, COLOR_NB = 2
|
2014-03-02 03:53:17 -07:00
|
|
|
};
|
|
|
|
|
2019-08-12 08:42:28 -06:00
|
|
|
enum CastlingRights {
|
2013-12-01 02:25:10 -07:00
|
|
|
NO_CASTLING,
|
2013-06-23 03:15:58 -06:00
|
|
|
WHITE_OO,
|
2015-01-09 04:35:44 -07:00
|
|
|
WHITE_OOO = WHITE_OO << 1,
|
|
|
|
BLACK_OO = WHITE_OO << 2,
|
|
|
|
BLACK_OOO = WHITE_OO << 3,
|
2019-01-01 06:13:08 -07:00
|
|
|
|
2019-08-12 08:42:28 -06:00
|
|
|
KING_SIDE = WHITE_OO | BLACK_OO,
|
|
|
|
QUEEN_SIDE = WHITE_OOO | BLACK_OOO,
|
|
|
|
WHITE_CASTLING = WHITE_OO | WHITE_OOO,
|
|
|
|
BLACK_CASTLING = BLACK_OO | BLACK_OOO,
|
2019-01-01 06:13:08 -07:00
|
|
|
ANY_CASTLING = WHITE_CASTLING | BLACK_CASTLING,
|
|
|
|
|
2014-03-08 07:08:55 -07:00
|
|
|
CASTLING_RIGHT_NB = 16
|
2011-12-30 09:08:07 -07:00
|
|
|
};
|
|
|
|
|
2012-10-21 01:12:02 -06:00
|
|
|
enum Phase {
|
2013-06-23 03:15:58 -06:00
|
|
|
PHASE_ENDGAME,
|
2012-10-21 03:49:05 -06:00
|
|
|
PHASE_MIDGAME = 128,
|
|
|
|
MG = 0, EG = 1, PHASE_NB = 2
|
2012-10-21 01:12:02 -06:00
|
|
|
};
|
|
|
|
|
2011-12-30 09:08:07 -07:00
|
|
|
enum ScaleFactor {
|
2013-12-31 13:45:20 -07:00
|
|
|
SCALE_FACTOR_DRAW = 0,
|
|
|
|
SCALE_FACTOR_NORMAL = 64,
|
|
|
|
SCALE_FACTOR_MAX = 128,
|
|
|
|
SCALE_FACTOR_NONE = 255
|
2011-12-30 09:08:07 -07:00
|
|
|
};
|
|
|
|
|
2012-01-27 11:47:56 -07:00
|
|
|
enum Bound {
|
2013-06-23 03:15:58 -06:00
|
|
|
BOUND_NONE,
|
|
|
|
BOUND_UPPER,
|
|
|
|
BOUND_LOWER,
|
2012-12-15 03:11:38 -07:00
|
|
|
BOUND_EXACT = BOUND_UPPER | BOUND_LOWER
|
2011-02-23 13:27:22 -07:00
|
|
|
};
|
|
|
|
|
2015-01-18 00:00:50 -07:00
|
|
|
enum Value : int {
|
2011-02-23 13:27:22 -07:00
|
|
|
VALUE_ZERO = 0,
|
|
|
|
VALUE_DRAW = 0,
|
2014-01-27 12:08:31 -07:00
|
|
|
VALUE_KNOWN_WIN = 10000,
|
2014-03-29 04:12:37 -06:00
|
|
|
VALUE_MATE = 32000,
|
|
|
|
VALUE_INFINITE = 32001,
|
|
|
|
VALUE_NONE = 32002,
|
2011-04-01 06:35:39 -06:00
|
|
|
|
Fix for incorrect VALUE_MATE_IN_MAX_PLY usage.
Fixes #2533, fixes #2543, fixes #2423.
the code that prevents false mate announcements depending on the TT
state (GHI), incorrectly used VALUE_MATE_IN_MAX_PLY. The latter
constant, however, also includes, counterintuitively, the TB win range.
This patch fixes that, by restoring the behavior for TB win scores,
while retaining the false mate correctness, and improving the mate
finding ability. In particular
no alse mates are announced with the poisened hash testcase
```
position fen 8/8/8/3k4/8/8/6K1/7R w - - 0 1
go depth 40
position fen 8/8/8/3k4/8/8/6K1/7R w - - 76 1
go depth 20
ucinewgame
```
mates are found with the testcases reported in #2543
```
position fen 4k3/3pp3/8/8/8/8/2PPP3/4K3 w - - 0 1
setoption name Hash value 1024
go depth 55
ucinewgame
```
and
```
position fen 4k3/4p3/8/8/8/8/3PP3/4K3 w - - 0 1
setoption name Hash value 1024
go depth 45
ucinewgame
```
furthermore, on the mate finding benchmark (ChestUCI_23102018.epd),
performance improves over master, roughly reaching performance with the
false mate protection reverted
```
Analyzing 6566 mate positions for best and found mates:
----------------best ---------------found
nodes master revert fixed master revert fixed
16000000 4233 4236 4235 5200 5201 5199
32000000 4583 4585 4585 5417 5424 5418
64000000 4852 4853 4855 5575 5584 5579
128000000 5071 5068 5066 5710 5720 5716
256000000 5280 5282 5279 5819 5827 5826
512000000 5471 5468 5468 5919 5935 5932
```
On a testcase with TB enabled, progress is made consistently, contrary
to master
```
setoption name SyzygyPath value ../../../syzygy/3-4-5/
setoption name Hash value 2048
position fen 1R6/3k4/8/K2p4/4n3/2P5/8/8 w - - 0 1
go depth 58
ucinewgame
```
The PR (prior to a rewrite for clarity)
passed STC:
LLR: 2.94 (-2.94,2.94) {-1.50,0.50}
Total: 65405 W: 12454 L: 12384 D: 40567
Ptnml(0-2): 920, 7256, 16285, 7286, 944
http://tests.stockfishchess.org/tests/view/5e441a3be70d848499f63d15
passed LTC:
LLR: 2.94 (-2.94,2.94) {-1.50,0.50}
Total: 27096 W: 3477 L: 3413 D: 20206
Ptnml(0-2): 128, 2215, 8776, 2292, 122
http://tests.stockfishchess.org/tests/view/5e44e277e70d848499f63d63
The incorrectly named VALUE_MATE_IN_MAX_PLY and VALUE_MATED_IN_MAX_PLY
were renamed into VALUE_TB_WIN_IN_MAX_PLY and VALUE_TB_LOSS_IN_MAX_PLY,
and correclty defined VALUE_MATE_IN_MAX_PLY and VALUE_MATED_IN_MAX_PLY
were introduced.
One further (corner case) mistake using these constants was fixed (go
mate X), which could lead to a premature return if X > MAX_PLY / 2,
but TB were present.
Thanks to @svivanov72 for one of the reports and help fixing the issue.
closes https://github.com/official-stockfish/Stockfish/pull/2552
Bench: 4932981
2020-02-11 12:42:32 -07:00
|
|
|
VALUE_TB_WIN_IN_MAX_PLY = VALUE_MATE - 2 * MAX_PLY,
|
2020-04-12 12:30:08 -06:00
|
|
|
VALUE_TB_LOSS_IN_MAX_PLY = -VALUE_TB_WIN_IN_MAX_PLY,
|
Fix for incorrect VALUE_MATE_IN_MAX_PLY usage.
Fixes #2533, fixes #2543, fixes #2423.
the code that prevents false mate announcements depending on the TT
state (GHI), incorrectly used VALUE_MATE_IN_MAX_PLY. The latter
constant, however, also includes, counterintuitively, the TB win range.
This patch fixes that, by restoring the behavior for TB win scores,
while retaining the false mate correctness, and improving the mate
finding ability. In particular
no alse mates are announced with the poisened hash testcase
```
position fen 8/8/8/3k4/8/8/6K1/7R w - - 0 1
go depth 40
position fen 8/8/8/3k4/8/8/6K1/7R w - - 76 1
go depth 20
ucinewgame
```
mates are found with the testcases reported in #2543
```
position fen 4k3/3pp3/8/8/8/8/2PPP3/4K3 w - - 0 1
setoption name Hash value 1024
go depth 55
ucinewgame
```
and
```
position fen 4k3/4p3/8/8/8/8/3PP3/4K3 w - - 0 1
setoption name Hash value 1024
go depth 45
ucinewgame
```
furthermore, on the mate finding benchmark (ChestUCI_23102018.epd),
performance improves over master, roughly reaching performance with the
false mate protection reverted
```
Analyzing 6566 mate positions for best and found mates:
----------------best ---------------found
nodes master revert fixed master revert fixed
16000000 4233 4236 4235 5200 5201 5199
32000000 4583 4585 4585 5417 5424 5418
64000000 4852 4853 4855 5575 5584 5579
128000000 5071 5068 5066 5710 5720 5716
256000000 5280 5282 5279 5819 5827 5826
512000000 5471 5468 5468 5919 5935 5932
```
On a testcase with TB enabled, progress is made consistently, contrary
to master
```
setoption name SyzygyPath value ../../../syzygy/3-4-5/
setoption name Hash value 2048
position fen 1R6/3k4/8/K2p4/4n3/2P5/8/8 w - - 0 1
go depth 58
ucinewgame
```
The PR (prior to a rewrite for clarity)
passed STC:
LLR: 2.94 (-2.94,2.94) {-1.50,0.50}
Total: 65405 W: 12454 L: 12384 D: 40567
Ptnml(0-2): 920, 7256, 16285, 7286, 944
http://tests.stockfishchess.org/tests/view/5e441a3be70d848499f63d15
passed LTC:
LLR: 2.94 (-2.94,2.94) {-1.50,0.50}
Total: 27096 W: 3477 L: 3413 D: 20206
Ptnml(0-2): 128, 2215, 8776, 2292, 122
http://tests.stockfishchess.org/tests/view/5e44e277e70d848499f63d63
The incorrectly named VALUE_MATE_IN_MAX_PLY and VALUE_MATED_IN_MAX_PLY
were renamed into VALUE_TB_WIN_IN_MAX_PLY and VALUE_TB_LOSS_IN_MAX_PLY,
and correclty defined VALUE_MATE_IN_MAX_PLY and VALUE_MATED_IN_MAX_PLY
were introduced.
One further (corner case) mistake using these constants was fixed (go
mate X), which could lead to a premature return if X > MAX_PLY / 2,
but TB were present.
Thanks to @svivanov72 for one of the reports and help fixing the issue.
closes https://github.com/official-stockfish/Stockfish/pull/2552
Bench: 4932981
2020-02-11 12:42:32 -07:00
|
|
|
VALUE_MATE_IN_MAX_PLY = VALUE_MATE - MAX_PLY,
|
2020-04-12 12:30:08 -06:00
|
|
|
VALUE_MATED_IN_MAX_PLY = -VALUE_MATE_IN_MAX_PLY,
|
2012-08-19 17:04:42 -06:00
|
|
|
|
Tuned pawn values
Passed STC:
https://tests.stockfishchess.org/tests/view/5f2aa49fa5abc164f05e4d1b
LLR: 2.95 (-2.94,2.94) {-0.50,1.50}
Total: 40888 W: 7977 L: 7726 D: 25185
Ptnml(0-2): 665, 4806, 9333, 4893, 747
Passed LTC:
https://tests.stockfishchess.org/tests/view/5f2b1059b3ebe5cbfee85ae7
LLR: 2.98 (-2.94,2.94) {0.25,1.75}
Total: 51264 W: 6445 L: 6134 D: 38685
Ptnml(0-2): 328, 4564, 15580, 4789, 371
closes https://github.com/official-stockfish/Stockfish/pull/2920
bench: 4314943
2020-08-06 18:37:54 -06:00
|
|
|
PawnValueMg = 126, PawnValueEg = 208,
|
2019-10-31 23:58:11 -06:00
|
|
|
KnightValueMg = 781, KnightValueEg = 854,
|
|
|
|
BishopValueMg = 825, BishopValueEg = 915,
|
|
|
|
RookValueMg = 1276, RookValueEg = 1380,
|
|
|
|
QueenValueMg = 2538, QueenValueEg = 2682,
|
2020-03-01 01:31:17 -07:00
|
|
|
Tempo = 28,
|
2014-04-27 01:23:45 -06:00
|
|
|
|
2016-08-30 12:15:51 -06:00
|
|
|
MidgameLimit = 15258, EndgameLimit = 3915
|
2011-02-23 13:27:22 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
enum PieceType {
|
2013-06-23 03:15:58 -06:00
|
|
|
NO_PIECE_TYPE, PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING,
|
|
|
|
ALL_PIECES = 0,
|
2012-10-21 02:41:23 -06:00
|
|
|
PIECE_TYPE_NB = 8
|
2011-02-23 13:27:22 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
enum Piece {
|
2013-06-23 03:15:58 -06:00
|
|
|
NO_PIECE,
|
|
|
|
W_PAWN = 1, W_KNIGHT, W_BISHOP, W_ROOK, W_QUEEN, W_KING,
|
|
|
|
B_PAWN = 9, B_KNIGHT, B_BISHOP, B_ROOK, B_QUEEN, B_KING,
|
2012-10-21 02:41:23 -06:00
|
|
|
PIECE_NB = 16
|
2011-02-23 13:27:22 -07:00
|
|
|
};
|
|
|
|
|
Add NNUE evaluation
This patch ports the efficiently updatable neural network (NNUE) evaluation to Stockfish.
Both the NNUE and the classical evaluations are available, and can be used to
assign a value to a position that is later used in alpha-beta (PVS) search to find the
best move. The classical evaluation computes this value as a function of various chess
concepts, handcrafted by experts, tested and tuned using fishtest. The NNUE evaluation
computes this value with a neural network based on basic inputs. The network is optimized
and trained on the evalutions of millions of positions at moderate search depth.
The NNUE evaluation was first introduced in shogi, and ported to Stockfish afterward.
It can be evaluated efficiently on CPUs, and exploits the fact that only parts
of the neural network need to be updated after a typical chess move.
[The nodchip repository](https://github.com/nodchip/Stockfish) provides additional
tools to train and develop the NNUE networks.
This patch is the result of contributions of various authors, from various communities,
including: nodchip, ynasu87, yaneurao (initial port and NNUE authors), domschl, FireFather,
rqs, xXH4CKST3RXx, tttak, zz4032, joergoster, mstembera, nguyenpham, erbsenzaehler,
dorzechowski, and vondele.
This new evaluation needed various changes to fishtest and the corresponding infrastructure,
for which tomtor, ppigazzini, noobpwnftw, daylen, and vondele are gratefully acknowledged.
The first networks have been provided by gekkehenker and sergiovieri, with the latter
net (nn-97f742aaefcd.nnue) being the current default.
The evaluation function can be selected at run time with the `Use NNUE` (true/false) UCI option,
provided the `EvalFile` option points the the network file (depending on the GUI, with full path).
The performance of the NNUE evaluation relative to the classical evaluation depends somewhat on
the hardware, and is expected to improve quickly, but is currently on > 80 Elo on fishtest:
60000 @ 10+0.1 th 1
https://tests.stockfishchess.org/tests/view/5f28fe6ea5abc164f05e4c4c
ELO: 92.77 +-2.1 (95%) LOS: 100.0%
Total: 60000 W: 24193 L: 8543 D: 27264
Ptnml(0-2): 609, 3850, 9708, 10948, 4885
40000 @ 20+0.2 th 8
https://tests.stockfishchess.org/tests/view/5f290229a5abc164f05e4c58
ELO: 89.47 +-2.0 (95%) LOS: 100.0%
Total: 40000 W: 12756 L: 2677 D: 24567
Ptnml(0-2): 74, 1583, 8550, 7776, 2017
At the same time, the impact on the classical evaluation remains minimal, causing no significant
regression:
sprt @ 10+0.1 th 1
https://tests.stockfishchess.org/tests/view/5f2906a2a5abc164f05e4c5b
LLR: 2.94 (-2.94,2.94) {-6.00,-4.00}
Total: 34936 W: 6502 L: 6825 D: 21609
Ptnml(0-2): 571, 4082, 8434, 3861, 520
sprt @ 60+0.6 th 1
https://tests.stockfishchess.org/tests/view/5f2906cfa5abc164f05e4c5d
LLR: 2.93 (-2.94,2.94) {-6.00,-4.00}
Total: 10088 W: 1232 L: 1265 D: 7591
Ptnml(0-2): 49, 914, 3170, 843, 68
The needed networks can be found at https://tests.stockfishchess.org/nns
It is recommended to use the default one as indicated by the `EvalFile` UCI option.
Guidelines for testing new nets can be found at
https://github.com/glinscott/fishtest/wiki/Creating-my-first-test#nnue-net-tests
Integration has been discussed in various issues:
https://github.com/official-stockfish/Stockfish/issues/2823
https://github.com/official-stockfish/Stockfish/issues/2728
The integration branch will be closed after the merge:
https://github.com/official-stockfish/Stockfish/pull/2825
https://github.com/official-stockfish/Stockfish/tree/nnue-player-wip
closes https://github.com/official-stockfish/Stockfish/pull/2912
This will be an exciting time for computer chess, looking forward to seeing the evolution of
this approach.
Bench: 4746616
2020-08-05 09:11:15 -06:00
|
|
|
// An ID used to track the pieces. Max. 32 pieces on board.
|
|
|
|
enum PieceId {
|
|
|
|
PIECE_ID_ZERO = 0,
|
|
|
|
PIECE_ID_KING = 30,
|
|
|
|
PIECE_ID_WKING = 30,
|
|
|
|
PIECE_ID_BKING = 31,
|
|
|
|
PIECE_ID_NONE = 32
|
|
|
|
};
|
|
|
|
|
|
|
|
inline PieceId operator++(PieceId& d, int) {
|
|
|
|
|
|
|
|
PieceId x = d;
|
|
|
|
d = PieceId(int(d) + 1);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2020-03-01 01:31:17 -07:00
|
|
|
constexpr Value PieceValue[PHASE_NB][PIECE_NB] = {
|
|
|
|
{ VALUE_ZERO, PawnValueMg, KnightValueMg, BishopValueMg, RookValueMg, QueenValueMg, VALUE_ZERO, VALUE_ZERO,
|
|
|
|
VALUE_ZERO, PawnValueMg, KnightValueMg, BishopValueMg, RookValueMg, QueenValueMg, VALUE_ZERO, VALUE_ZERO },
|
|
|
|
{ VALUE_ZERO, PawnValueEg, KnightValueEg, BishopValueEg, RookValueEg, QueenValueEg, VALUE_ZERO, VALUE_ZERO,
|
|
|
|
VALUE_ZERO, PawnValueEg, KnightValueEg, BishopValueEg, RookValueEg, QueenValueEg, VALUE_ZERO, VALUE_ZERO }
|
|
|
|
};
|
2016-09-04 07:29:11 -06:00
|
|
|
|
2019-09-28 14:27:23 -06:00
|
|
|
typedef int Depth;
|
2011-02-23 13:27:22 -07:00
|
|
|
|
2019-09-28 14:27:23 -06:00
|
|
|
enum : int {
|
|
|
|
DEPTH_QS_CHECKS = 0,
|
|
|
|
DEPTH_QS_NO_CHECKS = -1,
|
|
|
|
DEPTH_QS_RECAPTURES = -5,
|
2011-02-23 13:27:22 -07:00
|
|
|
|
2019-09-28 14:27:23 -06:00
|
|
|
DEPTH_NONE = -6,
|
2020-03-14 10:04:50 -06:00
|
|
|
DEPTH_OFFSET = DEPTH_NONE
|
2011-02-23 13:27:22 -07:00
|
|
|
};
|
|
|
|
|
2017-12-04 09:52:31 -07:00
|
|
|
enum Square : int {
|
2011-02-23 13:27:22 -07:00
|
|
|
SQ_A1, SQ_B1, SQ_C1, SQ_D1, SQ_E1, SQ_F1, SQ_G1, SQ_H1,
|
|
|
|
SQ_A2, SQ_B2, SQ_C2, SQ_D2, SQ_E2, SQ_F2, SQ_G2, SQ_H2,
|
|
|
|
SQ_A3, SQ_B3, SQ_C3, SQ_D3, SQ_E3, SQ_F3, SQ_G3, SQ_H3,
|
|
|
|
SQ_A4, SQ_B4, SQ_C4, SQ_D4, SQ_E4, SQ_F4, SQ_G4, SQ_H4,
|
|
|
|
SQ_A5, SQ_B5, SQ_C5, SQ_D5, SQ_E5, SQ_F5, SQ_G5, SQ_H5,
|
|
|
|
SQ_A6, SQ_B6, SQ_C6, SQ_D6, SQ_E6, SQ_F6, SQ_G6, SQ_H6,
|
|
|
|
SQ_A7, SQ_B7, SQ_C7, SQ_D7, SQ_E7, SQ_F7, SQ_G7, SQ_H7,
|
|
|
|
SQ_A8, SQ_B8, SQ_C8, SQ_D8, SQ_E8, SQ_F8, SQ_G8, SQ_H8,
|
|
|
|
SQ_NONE,
|
|
|
|
|
Add NNUE evaluation
This patch ports the efficiently updatable neural network (NNUE) evaluation to Stockfish.
Both the NNUE and the classical evaluations are available, and can be used to
assign a value to a position that is later used in alpha-beta (PVS) search to find the
best move. The classical evaluation computes this value as a function of various chess
concepts, handcrafted by experts, tested and tuned using fishtest. The NNUE evaluation
computes this value with a neural network based on basic inputs. The network is optimized
and trained on the evalutions of millions of positions at moderate search depth.
The NNUE evaluation was first introduced in shogi, and ported to Stockfish afterward.
It can be evaluated efficiently on CPUs, and exploits the fact that only parts
of the neural network need to be updated after a typical chess move.
[The nodchip repository](https://github.com/nodchip/Stockfish) provides additional
tools to train and develop the NNUE networks.
This patch is the result of contributions of various authors, from various communities,
including: nodchip, ynasu87, yaneurao (initial port and NNUE authors), domschl, FireFather,
rqs, xXH4CKST3RXx, tttak, zz4032, joergoster, mstembera, nguyenpham, erbsenzaehler,
dorzechowski, and vondele.
This new evaluation needed various changes to fishtest and the corresponding infrastructure,
for which tomtor, ppigazzini, noobpwnftw, daylen, and vondele are gratefully acknowledged.
The first networks have been provided by gekkehenker and sergiovieri, with the latter
net (nn-97f742aaefcd.nnue) being the current default.
The evaluation function can be selected at run time with the `Use NNUE` (true/false) UCI option,
provided the `EvalFile` option points the the network file (depending on the GUI, with full path).
The performance of the NNUE evaluation relative to the classical evaluation depends somewhat on
the hardware, and is expected to improve quickly, but is currently on > 80 Elo on fishtest:
60000 @ 10+0.1 th 1
https://tests.stockfishchess.org/tests/view/5f28fe6ea5abc164f05e4c4c
ELO: 92.77 +-2.1 (95%) LOS: 100.0%
Total: 60000 W: 24193 L: 8543 D: 27264
Ptnml(0-2): 609, 3850, 9708, 10948, 4885
40000 @ 20+0.2 th 8
https://tests.stockfishchess.org/tests/view/5f290229a5abc164f05e4c58
ELO: 89.47 +-2.0 (95%) LOS: 100.0%
Total: 40000 W: 12756 L: 2677 D: 24567
Ptnml(0-2): 74, 1583, 8550, 7776, 2017
At the same time, the impact on the classical evaluation remains minimal, causing no significant
regression:
sprt @ 10+0.1 th 1
https://tests.stockfishchess.org/tests/view/5f2906a2a5abc164f05e4c5b
LLR: 2.94 (-2.94,2.94) {-6.00,-4.00}
Total: 34936 W: 6502 L: 6825 D: 21609
Ptnml(0-2): 571, 4082, 8434, 3861, 520
sprt @ 60+0.6 th 1
https://tests.stockfishchess.org/tests/view/5f2906cfa5abc164f05e4c5d
LLR: 2.93 (-2.94,2.94) {-6.00,-4.00}
Total: 10088 W: 1232 L: 1265 D: 7591
Ptnml(0-2): 49, 914, 3170, 843, 68
The needed networks can be found at https://tests.stockfishchess.org/nns
It is recommended to use the default one as indicated by the `EvalFile` UCI option.
Guidelines for testing new nets can be found at
https://github.com/glinscott/fishtest/wiki/Creating-my-first-test#nnue-net-tests
Integration has been discussed in various issues:
https://github.com/official-stockfish/Stockfish/issues/2823
https://github.com/official-stockfish/Stockfish/issues/2728
The integration branch will be closed after the merge:
https://github.com/official-stockfish/Stockfish/pull/2825
https://github.com/official-stockfish/Stockfish/tree/nnue-player-wip
closes https://github.com/official-stockfish/Stockfish/pull/2912
This will be an exciting time for computer chess, looking forward to seeing the evolution of
this approach.
Bench: 4746616
2020-08-05 09:11:15 -06:00
|
|
|
SQUARE_ZERO = 0,
|
|
|
|
SQUARE_NB = 64
|
2017-12-04 09:52:31 -07:00
|
|
|
};
|
2012-10-21 02:41:23 -06:00
|
|
|
|
2017-12-04 09:52:31 -07:00
|
|
|
enum Direction : int {
|
2016-09-23 11:28:34 -06:00
|
|
|
NORTH = 8,
|
|
|
|
EAST = 1,
|
2017-05-07 02:26:09 -06:00
|
|
|
SOUTH = -NORTH,
|
|
|
|
WEST = -EAST,
|
2016-09-23 11:28:34 -06:00
|
|
|
|
|
|
|
NORTH_EAST = NORTH + EAST,
|
|
|
|
SOUTH_EAST = SOUTH + EAST,
|
|
|
|
SOUTH_WEST = SOUTH + WEST,
|
|
|
|
NORTH_WEST = NORTH + WEST
|
2011-02-23 13:27:22 -07:00
|
|
|
};
|
|
|
|
|
2016-10-20 22:48:07 -06:00
|
|
|
enum File : int {
|
2013-06-23 03:15:58 -06:00
|
|
|
FILE_A, FILE_B, FILE_C, FILE_D, FILE_E, FILE_F, FILE_G, FILE_H, FILE_NB
|
2011-02-23 13:27:22 -07:00
|
|
|
};
|
|
|
|
|
2016-10-20 22:48:07 -06:00
|
|
|
enum Rank : int {
|
2013-06-23 03:15:58 -06:00
|
|
|
RANK_1, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8, RANK_NB
|
2011-02-23 13:27:22 -07:00
|
|
|
};
|
|
|
|
|
Add NNUE evaluation
This patch ports the efficiently updatable neural network (NNUE) evaluation to Stockfish.
Both the NNUE and the classical evaluations are available, and can be used to
assign a value to a position that is later used in alpha-beta (PVS) search to find the
best move. The classical evaluation computes this value as a function of various chess
concepts, handcrafted by experts, tested and tuned using fishtest. The NNUE evaluation
computes this value with a neural network based on basic inputs. The network is optimized
and trained on the evalutions of millions of positions at moderate search depth.
The NNUE evaluation was first introduced in shogi, and ported to Stockfish afterward.
It can be evaluated efficiently on CPUs, and exploits the fact that only parts
of the neural network need to be updated after a typical chess move.
[The nodchip repository](https://github.com/nodchip/Stockfish) provides additional
tools to train and develop the NNUE networks.
This patch is the result of contributions of various authors, from various communities,
including: nodchip, ynasu87, yaneurao (initial port and NNUE authors), domschl, FireFather,
rqs, xXH4CKST3RXx, tttak, zz4032, joergoster, mstembera, nguyenpham, erbsenzaehler,
dorzechowski, and vondele.
This new evaluation needed various changes to fishtest and the corresponding infrastructure,
for which tomtor, ppigazzini, noobpwnftw, daylen, and vondele are gratefully acknowledged.
The first networks have been provided by gekkehenker and sergiovieri, with the latter
net (nn-97f742aaefcd.nnue) being the current default.
The evaluation function can be selected at run time with the `Use NNUE` (true/false) UCI option,
provided the `EvalFile` option points the the network file (depending on the GUI, with full path).
The performance of the NNUE evaluation relative to the classical evaluation depends somewhat on
the hardware, and is expected to improve quickly, but is currently on > 80 Elo on fishtest:
60000 @ 10+0.1 th 1
https://tests.stockfishchess.org/tests/view/5f28fe6ea5abc164f05e4c4c
ELO: 92.77 +-2.1 (95%) LOS: 100.0%
Total: 60000 W: 24193 L: 8543 D: 27264
Ptnml(0-2): 609, 3850, 9708, 10948, 4885
40000 @ 20+0.2 th 8
https://tests.stockfishchess.org/tests/view/5f290229a5abc164f05e4c58
ELO: 89.47 +-2.0 (95%) LOS: 100.0%
Total: 40000 W: 12756 L: 2677 D: 24567
Ptnml(0-2): 74, 1583, 8550, 7776, 2017
At the same time, the impact on the classical evaluation remains minimal, causing no significant
regression:
sprt @ 10+0.1 th 1
https://tests.stockfishchess.org/tests/view/5f2906a2a5abc164f05e4c5b
LLR: 2.94 (-2.94,2.94) {-6.00,-4.00}
Total: 34936 W: 6502 L: 6825 D: 21609
Ptnml(0-2): 571, 4082, 8434, 3861, 520
sprt @ 60+0.6 th 1
https://tests.stockfishchess.org/tests/view/5f2906cfa5abc164f05e4c5d
LLR: 2.93 (-2.94,2.94) {-6.00,-4.00}
Total: 10088 W: 1232 L: 1265 D: 7591
Ptnml(0-2): 49, 914, 3170, 843, 68
The needed networks can be found at https://tests.stockfishchess.org/nns
It is recommended to use the default one as indicated by the `EvalFile` UCI option.
Guidelines for testing new nets can be found at
https://github.com/glinscott/fishtest/wiki/Creating-my-first-test#nnue-net-tests
Integration has been discussed in various issues:
https://github.com/official-stockfish/Stockfish/issues/2823
https://github.com/official-stockfish/Stockfish/issues/2728
The integration branch will be closed after the merge:
https://github.com/official-stockfish/Stockfish/pull/2825
https://github.com/official-stockfish/Stockfish/tree/nnue-player-wip
closes https://github.com/official-stockfish/Stockfish/pull/2912
This will be an exciting time for computer chess, looking forward to seeing the evolution of
this approach.
Bench: 4746616
2020-08-05 09:11:15 -06:00
|
|
|
// unique number for each piece type on each square
|
|
|
|
enum PieceSquare : uint32_t {
|
|
|
|
PS_NONE = 0,
|
|
|
|
PS_W_PAWN = 1,
|
|
|
|
PS_B_PAWN = 1 * SQUARE_NB + 1,
|
|
|
|
PS_W_KNIGHT = 2 * SQUARE_NB + 1,
|
|
|
|
PS_B_KNIGHT = 3 * SQUARE_NB + 1,
|
|
|
|
PS_W_BISHOP = 4 * SQUARE_NB + 1,
|
|
|
|
PS_B_BISHOP = 5 * SQUARE_NB + 1,
|
|
|
|
PS_W_ROOK = 6 * SQUARE_NB + 1,
|
|
|
|
PS_B_ROOK = 7 * SQUARE_NB + 1,
|
|
|
|
PS_W_QUEEN = 8 * SQUARE_NB + 1,
|
|
|
|
PS_B_QUEEN = 9 * SQUARE_NB + 1,
|
|
|
|
PS_W_KING = 10 * SQUARE_NB + 1,
|
|
|
|
PS_END = PS_W_KING, // pieces without kings (pawns included)
|
|
|
|
PS_B_KING = 11 * SQUARE_NB + 1,
|
|
|
|
PS_END2 = 12 * SQUARE_NB + 1
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ExtPieceSquare {
|
|
|
|
PieceSquare from[COLOR_NB];
|
|
|
|
};
|
|
|
|
|
|
|
|
// Array for finding the PieceSquare corresponding to the piece on the board
|
|
|
|
extern ExtPieceSquare kpp_board_index[PIECE_NB];
|
|
|
|
|
|
|
|
constexpr bool is_ok(PieceId pid);
|
|
|
|
constexpr Square rotate180(Square sq);
|
|
|
|
|
|
|
|
// Structure holding which tracked piece (PieceId) is where (PieceSquare)
|
|
|
|
class EvalList {
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Max. number of pieces without kings is 30 but must be a multiple of 4 in AVX2
|
|
|
|
static const int MAX_LENGTH = 32;
|
|
|
|
|
|
|
|
// Array that holds the piece id for the pieces on the board
|
|
|
|
PieceId piece_id_list[SQUARE_NB];
|
|
|
|
|
|
|
|
// List of pieces, separate from White and Black POV
|
|
|
|
PieceSquare* piece_list_fw() const { return const_cast<PieceSquare*>(pieceListFw); }
|
|
|
|
PieceSquare* piece_list_fb() const { return const_cast<PieceSquare*>(pieceListFb); }
|
|
|
|
|
|
|
|
// Place the piece pc with piece_id on the square sq on the board
|
|
|
|
void put_piece(PieceId piece_id, Square sq, Piece pc)
|
|
|
|
{
|
|
|
|
assert(is_ok(piece_id));
|
|
|
|
if (pc != NO_PIECE)
|
|
|
|
{
|
|
|
|
pieceListFw[piece_id] = PieceSquare(kpp_board_index[pc].from[WHITE] + sq);
|
|
|
|
pieceListFb[piece_id] = PieceSquare(kpp_board_index[pc].from[BLACK] + rotate180(sq));
|
|
|
|
piece_id_list[sq] = piece_id;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pieceListFw[piece_id] = PS_NONE;
|
|
|
|
pieceListFb[piece_id] = PS_NONE;
|
|
|
|
piece_id_list[sq] = piece_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the specified piece_id piece to ExtPieceSquare type and return it
|
|
|
|
ExtPieceSquare piece_with_id(PieceId piece_id) const
|
|
|
|
{
|
|
|
|
ExtPieceSquare eps;
|
|
|
|
eps.from[WHITE] = pieceListFw[piece_id];
|
|
|
|
eps.from[BLACK] = pieceListFb[piece_id];
|
|
|
|
return eps;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
PieceSquare pieceListFw[MAX_LENGTH];
|
|
|
|
PieceSquare pieceListFb[MAX_LENGTH];
|
|
|
|
};
|
|
|
|
|
|
|
|
// For differential evaluation of pieces that changed since last turn
|
|
|
|
struct DirtyPiece {
|
|
|
|
|
|
|
|
// Number of changed pieces
|
|
|
|
int dirty_num;
|
|
|
|
|
|
|
|
// The ids of changed pieces, max. 2 pieces can change in one move
|
|
|
|
PieceId pieceId[2];
|
|
|
|
|
|
|
|
// What changed from the piece with that piece number
|
|
|
|
ExtPieceSquare old_piece[2];
|
|
|
|
ExtPieceSquare new_piece[2];
|
|
|
|
};
|
2011-02-23 13:27:22 -07:00
|
|
|
|
2018-04-02 16:13:35 -06:00
|
|
|
/// Score enum stores a middlegame and an endgame value in a single integer (enum).
|
|
|
|
/// The least significant 16 bits are used to store the middlegame value and the
|
|
|
|
/// upper 16 bits are used to store the endgame value. We have to take care to
|
|
|
|
/// avoid left-shifting a signed int to avoid undefined behavior.
|
2015-01-18 00:00:50 -07:00
|
|
|
enum Score : int { SCORE_ZERO };
|
2011-06-29 10:40:33 -06:00
|
|
|
|
2017-11-25 20:33:34 -07:00
|
|
|
constexpr Score make_score(int mg, int eg) {
|
2016-10-20 22:48:07 -06:00
|
|
|
return Score((int)((unsigned int)eg << 16) + mg);
|
2015-01-09 04:35:44 -07:00
|
|
|
}
|
2011-02-23 13:27:22 -07:00
|
|
|
|
2014-06-05 15:55:18 -06:00
|
|
|
/// Extracting the signed lower and upper 16 bits is not so trivial because
|
|
|
|
/// according to the standard a simple cast to short is implementation defined
|
|
|
|
/// and so is a right shift of a signed integer.
|
2016-09-16 10:18:53 -06:00
|
|
|
inline Value eg_value(Score s) {
|
|
|
|
union { uint16_t u; int16_t s; } eg = { uint16_t(unsigned(s + 0x8000) >> 16) };
|
|
|
|
return Value(eg.s);
|
2014-05-01 15:08:07 -06:00
|
|
|
}
|
2011-02-23 13:27:22 -07:00
|
|
|
|
2016-09-16 10:18:53 -06:00
|
|
|
inline Value mg_value(Score s) {
|
|
|
|
union { uint16_t u; int16_t s; } mg = { uint16_t(unsigned(s)) };
|
|
|
|
return Value(mg.s);
|
2011-12-30 09:08:07 -07:00
|
|
|
}
|
|
|
|
|
2017-11-25 20:33:34 -07:00
|
|
|
#define ENABLE_BASE_OPERATORS_ON(T) \
|
Add NNUE evaluation
This patch ports the efficiently updatable neural network (NNUE) evaluation to Stockfish.
Both the NNUE and the classical evaluations are available, and can be used to
assign a value to a position that is later used in alpha-beta (PVS) search to find the
best move. The classical evaluation computes this value as a function of various chess
concepts, handcrafted by experts, tested and tuned using fishtest. The NNUE evaluation
computes this value with a neural network based on basic inputs. The network is optimized
and trained on the evalutions of millions of positions at moderate search depth.
The NNUE evaluation was first introduced in shogi, and ported to Stockfish afterward.
It can be evaluated efficiently on CPUs, and exploits the fact that only parts
of the neural network need to be updated after a typical chess move.
[The nodchip repository](https://github.com/nodchip/Stockfish) provides additional
tools to train and develop the NNUE networks.
This patch is the result of contributions of various authors, from various communities,
including: nodchip, ynasu87, yaneurao (initial port and NNUE authors), domschl, FireFather,
rqs, xXH4CKST3RXx, tttak, zz4032, joergoster, mstembera, nguyenpham, erbsenzaehler,
dorzechowski, and vondele.
This new evaluation needed various changes to fishtest and the corresponding infrastructure,
for which tomtor, ppigazzini, noobpwnftw, daylen, and vondele are gratefully acknowledged.
The first networks have been provided by gekkehenker and sergiovieri, with the latter
net (nn-97f742aaefcd.nnue) being the current default.
The evaluation function can be selected at run time with the `Use NNUE` (true/false) UCI option,
provided the `EvalFile` option points the the network file (depending on the GUI, with full path).
The performance of the NNUE evaluation relative to the classical evaluation depends somewhat on
the hardware, and is expected to improve quickly, but is currently on > 80 Elo on fishtest:
60000 @ 10+0.1 th 1
https://tests.stockfishchess.org/tests/view/5f28fe6ea5abc164f05e4c4c
ELO: 92.77 +-2.1 (95%) LOS: 100.0%
Total: 60000 W: 24193 L: 8543 D: 27264
Ptnml(0-2): 609, 3850, 9708, 10948, 4885
40000 @ 20+0.2 th 8
https://tests.stockfishchess.org/tests/view/5f290229a5abc164f05e4c58
ELO: 89.47 +-2.0 (95%) LOS: 100.0%
Total: 40000 W: 12756 L: 2677 D: 24567
Ptnml(0-2): 74, 1583, 8550, 7776, 2017
At the same time, the impact on the classical evaluation remains minimal, causing no significant
regression:
sprt @ 10+0.1 th 1
https://tests.stockfishchess.org/tests/view/5f2906a2a5abc164f05e4c5b
LLR: 2.94 (-2.94,2.94) {-6.00,-4.00}
Total: 34936 W: 6502 L: 6825 D: 21609
Ptnml(0-2): 571, 4082, 8434, 3861, 520
sprt @ 60+0.6 th 1
https://tests.stockfishchess.org/tests/view/5f2906cfa5abc164f05e4c5d
LLR: 2.93 (-2.94,2.94) {-6.00,-4.00}
Total: 10088 W: 1232 L: 1265 D: 7591
Ptnml(0-2): 49, 914, 3170, 843, 68
The needed networks can be found at https://tests.stockfishchess.org/nns
It is recommended to use the default one as indicated by the `EvalFile` UCI option.
Guidelines for testing new nets can be found at
https://github.com/glinscott/fishtest/wiki/Creating-my-first-test#nnue-net-tests
Integration has been discussed in various issues:
https://github.com/official-stockfish/Stockfish/issues/2823
https://github.com/official-stockfish/Stockfish/issues/2728
The integration branch will be closed after the merge:
https://github.com/official-stockfish/Stockfish/pull/2825
https://github.com/official-stockfish/Stockfish/tree/nnue-player-wip
closes https://github.com/official-stockfish/Stockfish/pull/2912
This will be an exciting time for computer chess, looking forward to seeing the evolution of
this approach.
Bench: 4746616
2020-08-05 09:11:15 -06:00
|
|
|
constexpr T operator+(T d1, int d2) { return T(int(d1) + d2); } \
|
|
|
|
constexpr T operator-(T d1, int d2) { return T(int(d1) - d2); } \
|
2017-11-25 20:33:34 -07:00
|
|
|
constexpr T operator-(T d) { return T(-int(d)); } \
|
Add NNUE evaluation
This patch ports the efficiently updatable neural network (NNUE) evaluation to Stockfish.
Both the NNUE and the classical evaluations are available, and can be used to
assign a value to a position that is later used in alpha-beta (PVS) search to find the
best move. The classical evaluation computes this value as a function of various chess
concepts, handcrafted by experts, tested and tuned using fishtest. The NNUE evaluation
computes this value with a neural network based on basic inputs. The network is optimized
and trained on the evalutions of millions of positions at moderate search depth.
The NNUE evaluation was first introduced in shogi, and ported to Stockfish afterward.
It can be evaluated efficiently on CPUs, and exploits the fact that only parts
of the neural network need to be updated after a typical chess move.
[The nodchip repository](https://github.com/nodchip/Stockfish) provides additional
tools to train and develop the NNUE networks.
This patch is the result of contributions of various authors, from various communities,
including: nodchip, ynasu87, yaneurao (initial port and NNUE authors), domschl, FireFather,
rqs, xXH4CKST3RXx, tttak, zz4032, joergoster, mstembera, nguyenpham, erbsenzaehler,
dorzechowski, and vondele.
This new evaluation needed various changes to fishtest and the corresponding infrastructure,
for which tomtor, ppigazzini, noobpwnftw, daylen, and vondele are gratefully acknowledged.
The first networks have been provided by gekkehenker and sergiovieri, with the latter
net (nn-97f742aaefcd.nnue) being the current default.
The evaluation function can be selected at run time with the `Use NNUE` (true/false) UCI option,
provided the `EvalFile` option points the the network file (depending on the GUI, with full path).
The performance of the NNUE evaluation relative to the classical evaluation depends somewhat on
the hardware, and is expected to improve quickly, but is currently on > 80 Elo on fishtest:
60000 @ 10+0.1 th 1
https://tests.stockfishchess.org/tests/view/5f28fe6ea5abc164f05e4c4c
ELO: 92.77 +-2.1 (95%) LOS: 100.0%
Total: 60000 W: 24193 L: 8543 D: 27264
Ptnml(0-2): 609, 3850, 9708, 10948, 4885
40000 @ 20+0.2 th 8
https://tests.stockfishchess.org/tests/view/5f290229a5abc164f05e4c58
ELO: 89.47 +-2.0 (95%) LOS: 100.0%
Total: 40000 W: 12756 L: 2677 D: 24567
Ptnml(0-2): 74, 1583, 8550, 7776, 2017
At the same time, the impact on the classical evaluation remains minimal, causing no significant
regression:
sprt @ 10+0.1 th 1
https://tests.stockfishchess.org/tests/view/5f2906a2a5abc164f05e4c5b
LLR: 2.94 (-2.94,2.94) {-6.00,-4.00}
Total: 34936 W: 6502 L: 6825 D: 21609
Ptnml(0-2): 571, 4082, 8434, 3861, 520
sprt @ 60+0.6 th 1
https://tests.stockfishchess.org/tests/view/5f2906cfa5abc164f05e4c5d
LLR: 2.93 (-2.94,2.94) {-6.00,-4.00}
Total: 10088 W: 1232 L: 1265 D: 7591
Ptnml(0-2): 49, 914, 3170, 843, 68
The needed networks can be found at https://tests.stockfishchess.org/nns
It is recommended to use the default one as indicated by the `EvalFile` UCI option.
Guidelines for testing new nets can be found at
https://github.com/glinscott/fishtest/wiki/Creating-my-first-test#nnue-net-tests
Integration has been discussed in various issues:
https://github.com/official-stockfish/Stockfish/issues/2823
https://github.com/official-stockfish/Stockfish/issues/2728
The integration branch will be closed after the merge:
https://github.com/official-stockfish/Stockfish/pull/2825
https://github.com/official-stockfish/Stockfish/tree/nnue-player-wip
closes https://github.com/official-stockfish/Stockfish/pull/2912
This will be an exciting time for computer chess, looking forward to seeing the evolution of
this approach.
Bench: 4746616
2020-08-05 09:11:15 -06:00
|
|
|
inline T& operator+=(T& d1, int d2) { return d1 = d1 + d2; } \
|
2020-06-21 07:21:46 -06:00
|
|
|
inline T& operator-=(T& d1, int d2) { return d1 = d1 - d2; }
|
2017-12-04 09:52:31 -07:00
|
|
|
|
|
|
|
#define ENABLE_INCR_OPERATORS_ON(T) \
|
|
|
|
inline T& operator++(T& d) { return d = T(int(d) + 1); } \
|
|
|
|
inline T& operator--(T& d) { return d = T(int(d) - 1); }
|
2017-11-25 20:33:34 -07:00
|
|
|
|
|
|
|
#define ENABLE_FULL_OPERATORS_ON(T) \
|
|
|
|
ENABLE_BASE_OPERATORS_ON(T) \
|
|
|
|
constexpr T operator*(int i, T d) { return T(i * int(d)); } \
|
|
|
|
constexpr T operator*(T d, int i) { return T(int(d) * i); } \
|
|
|
|
constexpr T operator/(T d, int i) { return T(int(d) / i); } \
|
|
|
|
constexpr int operator/(T d1, T d2) { return int(d1) / int(d2); } \
|
|
|
|
inline T& operator*=(T& d, int i) { return d = T(int(d) * i); } \
|
2013-09-15 01:02:09 -06:00
|
|
|
inline T& operator/=(T& d, int i) { return d = T(int(d) / i); }
|
2011-02-23 13:27:22 -07:00
|
|
|
|
2014-04-27 02:13:59 -06:00
|
|
|
ENABLE_FULL_OPERATORS_ON(Value)
|
2017-12-04 09:52:31 -07:00
|
|
|
ENABLE_FULL_OPERATORS_ON(Direction)
|
|
|
|
|
Add NNUE evaluation
This patch ports the efficiently updatable neural network (NNUE) evaluation to Stockfish.
Both the NNUE and the classical evaluations are available, and can be used to
assign a value to a position that is later used in alpha-beta (PVS) search to find the
best move. The classical evaluation computes this value as a function of various chess
concepts, handcrafted by experts, tested and tuned using fishtest. The NNUE evaluation
computes this value with a neural network based on basic inputs. The network is optimized
and trained on the evalutions of millions of positions at moderate search depth.
The NNUE evaluation was first introduced in shogi, and ported to Stockfish afterward.
It can be evaluated efficiently on CPUs, and exploits the fact that only parts
of the neural network need to be updated after a typical chess move.
[The nodchip repository](https://github.com/nodchip/Stockfish) provides additional
tools to train and develop the NNUE networks.
This patch is the result of contributions of various authors, from various communities,
including: nodchip, ynasu87, yaneurao (initial port and NNUE authors), domschl, FireFather,
rqs, xXH4CKST3RXx, tttak, zz4032, joergoster, mstembera, nguyenpham, erbsenzaehler,
dorzechowski, and vondele.
This new evaluation needed various changes to fishtest and the corresponding infrastructure,
for which tomtor, ppigazzini, noobpwnftw, daylen, and vondele are gratefully acknowledged.
The first networks have been provided by gekkehenker and sergiovieri, with the latter
net (nn-97f742aaefcd.nnue) being the current default.
The evaluation function can be selected at run time with the `Use NNUE` (true/false) UCI option,
provided the `EvalFile` option points the the network file (depending on the GUI, with full path).
The performance of the NNUE evaluation relative to the classical evaluation depends somewhat on
the hardware, and is expected to improve quickly, but is currently on > 80 Elo on fishtest:
60000 @ 10+0.1 th 1
https://tests.stockfishchess.org/tests/view/5f28fe6ea5abc164f05e4c4c
ELO: 92.77 +-2.1 (95%) LOS: 100.0%
Total: 60000 W: 24193 L: 8543 D: 27264
Ptnml(0-2): 609, 3850, 9708, 10948, 4885
40000 @ 20+0.2 th 8
https://tests.stockfishchess.org/tests/view/5f290229a5abc164f05e4c58
ELO: 89.47 +-2.0 (95%) LOS: 100.0%
Total: 40000 W: 12756 L: 2677 D: 24567
Ptnml(0-2): 74, 1583, 8550, 7776, 2017
At the same time, the impact on the classical evaluation remains minimal, causing no significant
regression:
sprt @ 10+0.1 th 1
https://tests.stockfishchess.org/tests/view/5f2906a2a5abc164f05e4c5b
LLR: 2.94 (-2.94,2.94) {-6.00,-4.00}
Total: 34936 W: 6502 L: 6825 D: 21609
Ptnml(0-2): 571, 4082, 8434, 3861, 520
sprt @ 60+0.6 th 1
https://tests.stockfishchess.org/tests/view/5f2906cfa5abc164f05e4c5d
LLR: 2.93 (-2.94,2.94) {-6.00,-4.00}
Total: 10088 W: 1232 L: 1265 D: 7591
Ptnml(0-2): 49, 914, 3170, 843, 68
The needed networks can be found at https://tests.stockfishchess.org/nns
It is recommended to use the default one as indicated by the `EvalFile` UCI option.
Guidelines for testing new nets can be found at
https://github.com/glinscott/fishtest/wiki/Creating-my-first-test#nnue-net-tests
Integration has been discussed in various issues:
https://github.com/official-stockfish/Stockfish/issues/2823
https://github.com/official-stockfish/Stockfish/issues/2728
The integration branch will be closed after the merge:
https://github.com/official-stockfish/Stockfish/pull/2825
https://github.com/official-stockfish/Stockfish/tree/nnue-player-wip
closes https://github.com/official-stockfish/Stockfish/pull/2912
This will be an exciting time for computer chess, looking forward to seeing the evolution of
this approach.
Bench: 4746616
2020-08-05 09:11:15 -06:00
|
|
|
ENABLE_INCR_OPERATORS_ON(Piece)
|
|
|
|
ENABLE_INCR_OPERATORS_ON(PieceSquare)
|
|
|
|
ENABLE_INCR_OPERATORS_ON(PieceId)
|
2017-12-04 09:52:31 -07:00
|
|
|
ENABLE_INCR_OPERATORS_ON(PieceType)
|
|
|
|
ENABLE_INCR_OPERATORS_ON(Square)
|
|
|
|
ENABLE_INCR_OPERATORS_ON(File)
|
|
|
|
ENABLE_INCR_OPERATORS_ON(Rank)
|
2014-04-27 02:13:59 -06:00
|
|
|
|
2015-01-09 04:35:44 -07:00
|
|
|
ENABLE_BASE_OPERATORS_ON(Score)
|
|
|
|
|
2014-04-27 02:13:59 -06:00
|
|
|
#undef ENABLE_FULL_OPERATORS_ON
|
2017-12-04 09:52:31 -07:00
|
|
|
#undef ENABLE_INCR_OPERATORS_ON
|
2014-04-27 02:13:59 -06:00
|
|
|
#undef ENABLE_BASE_OPERATORS_ON
|
2011-02-23 13:27:22 -07:00
|
|
|
|
2017-12-04 09:52:31 -07:00
|
|
|
/// Additional operators to add a Direction to a Square
|
2018-07-16 14:53:50 -06:00
|
|
|
constexpr Square operator+(Square s, Direction d) { return Square(int(s) + int(d)); }
|
|
|
|
constexpr Square operator-(Square s, Direction d) { return Square(int(s) - int(d)); }
|
|
|
|
inline Square& operator+=(Square& s, Direction d) { return s = s + d; }
|
|
|
|
inline Square& operator-=(Square& s, Direction d) { return s = s - d; }
|
2017-12-04 09:52:31 -07:00
|
|
|
|
2011-12-30 09:08:07 -07:00
|
|
|
/// Only declared but not defined. We don't want to multiply two scores due to
|
|
|
|
/// a very high risk of overflow. So user should explicitly convert to integer.
|
2017-12-04 09:01:17 -07:00
|
|
|
Score operator*(Score, Score) = delete;
|
2011-02-23 13:27:22 -07:00
|
|
|
|
2011-12-30 09:08:07 -07:00
|
|
|
/// Division of a Score must be handled separately for each term
|
|
|
|
inline Score operator/(Score s, int i) {
|
|
|
|
return make_score(mg_value(s) / i, eg_value(s) / i);
|
|
|
|
}
|
|
|
|
|
2017-01-11 10:11:17 -07:00
|
|
|
/// Multiplication of a Score by an integer. We check for overflow in debug mode.
|
|
|
|
inline Score operator*(Score s, int i) {
|
2017-05-07 02:26:09 -06:00
|
|
|
|
2017-01-11 10:11:17 -07:00
|
|
|
Score result = Score(int(s) * i);
|
|
|
|
|
|
|
|
assert(eg_value(result) == (i * eg_value(s)));
|
|
|
|
assert(mg_value(result) == (i * mg_value(s)));
|
2018-07-16 14:53:50 -06:00
|
|
|
assert((i == 0) || (result / i) == s);
|
2017-01-11 10:11:17 -07:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-10-18 18:20:38 -06:00
|
|
|
/// Multiplication of a Score by a boolean
|
2019-09-11 12:36:58 -06:00
|
|
|
inline Score operator*(Score s, bool b) {
|
2020-04-12 12:30:08 -06:00
|
|
|
return b ? s : SCORE_ZERO;
|
2019-09-11 12:36:58 -06:00
|
|
|
}
|
|
|
|
|
2017-11-25 20:33:34 -07:00
|
|
|
constexpr Color operator~(Color c) {
|
2016-09-04 07:29:11 -06:00
|
|
|
return Color(c ^ BLACK); // Toggle color
|
2012-01-12 11:31:18 -07:00
|
|
|
}
|
|
|
|
|
2020-06-24 14:19:58 -06:00
|
|
|
constexpr Square flip_rank(Square s) { // Swap A1 <-> A8
|
2020-03-01 02:03:36 -07:00
|
|
|
return Square(s ^ SQ_A8);
|
|
|
|
}
|
|
|
|
|
2020-06-24 14:19:58 -06:00
|
|
|
constexpr Square flip_file(Square s) { // Swap A1 <-> H1
|
2020-03-01 02:03:36 -07:00
|
|
|
return Square(s ^ SQ_H1);
|
2012-01-12 11:31:18 -07:00
|
|
|
}
|
|
|
|
|
2017-11-25 20:33:34 -07:00
|
|
|
constexpr Piece operator~(Piece pc) {
|
2020-06-24 14:19:58 -06:00
|
|
|
return Piece(pc ^ 8); // Swap color of piece B_KNIGHT <-> W_KNIGHT
|
2016-09-03 10:14:01 -06:00
|
|
|
}
|
|
|
|
|
2019-08-12 08:42:28 -06:00
|
|
|
constexpr CastlingRights operator&(Color c, CastlingRights cr) {
|
|
|
|
return CastlingRights((c == WHITE ? WHITE_CASTLING : BLACK_CASTLING) & cr);
|
2014-03-02 03:53:17 -07:00
|
|
|
}
|
|
|
|
|
2017-11-25 20:33:34 -07:00
|
|
|
constexpr Value mate_in(int ply) {
|
2011-02-23 13:27:22 -07:00
|
|
|
return VALUE_MATE - ply;
|
|
|
|
}
|
|
|
|
|
2017-11-25 20:33:34 -07:00
|
|
|
constexpr Value mated_in(int ply) {
|
2011-02-23 13:27:22 -07:00
|
|
|
return -VALUE_MATE + ply;
|
|
|
|
}
|
|
|
|
|
2017-11-25 20:33:34 -07:00
|
|
|
constexpr Square make_square(File f, Rank r) {
|
2016-09-17 00:19:06 -06:00
|
|
|
return Square((r << 3) + f);
|
2014-03-22 16:35:30 -06:00
|
|
|
}
|
|
|
|
|
2017-11-25 20:33:34 -07:00
|
|
|
constexpr Piece make_piece(Color c, PieceType pt) {
|
2016-09-03 10:14:01 -06:00
|
|
|
return Piece((c << 3) + pt);
|
2011-02-23 13:27:22 -07:00
|
|
|
}
|
|
|
|
|
2017-11-25 20:33:34 -07:00
|
|
|
constexpr PieceType type_of(Piece pc) {
|
2014-04-06 02:50:27 -06:00
|
|
|
return PieceType(pc & 7);
|
2011-02-23 13:27:22 -07:00
|
|
|
}
|
|
|
|
|
2014-04-06 02:50:27 -06:00
|
|
|
inline Color color_of(Piece pc) {
|
|
|
|
assert(pc != NO_PIECE);
|
|
|
|
return Color(pc >> 3);
|
2011-02-23 13:27:22 -07:00
|
|
|
}
|
|
|
|
|
Add NNUE evaluation
This patch ports the efficiently updatable neural network (NNUE) evaluation to Stockfish.
Both the NNUE and the classical evaluations are available, and can be used to
assign a value to a position that is later used in alpha-beta (PVS) search to find the
best move. The classical evaluation computes this value as a function of various chess
concepts, handcrafted by experts, tested and tuned using fishtest. The NNUE evaluation
computes this value with a neural network based on basic inputs. The network is optimized
and trained on the evalutions of millions of positions at moderate search depth.
The NNUE evaluation was first introduced in shogi, and ported to Stockfish afterward.
It can be evaluated efficiently on CPUs, and exploits the fact that only parts
of the neural network need to be updated after a typical chess move.
[The nodchip repository](https://github.com/nodchip/Stockfish) provides additional
tools to train and develop the NNUE networks.
This patch is the result of contributions of various authors, from various communities,
including: nodchip, ynasu87, yaneurao (initial port and NNUE authors), domschl, FireFather,
rqs, xXH4CKST3RXx, tttak, zz4032, joergoster, mstembera, nguyenpham, erbsenzaehler,
dorzechowski, and vondele.
This new evaluation needed various changes to fishtest and the corresponding infrastructure,
for which tomtor, ppigazzini, noobpwnftw, daylen, and vondele are gratefully acknowledged.
The first networks have been provided by gekkehenker and sergiovieri, with the latter
net (nn-97f742aaefcd.nnue) being the current default.
The evaluation function can be selected at run time with the `Use NNUE` (true/false) UCI option,
provided the `EvalFile` option points the the network file (depending on the GUI, with full path).
The performance of the NNUE evaluation relative to the classical evaluation depends somewhat on
the hardware, and is expected to improve quickly, but is currently on > 80 Elo on fishtest:
60000 @ 10+0.1 th 1
https://tests.stockfishchess.org/tests/view/5f28fe6ea5abc164f05e4c4c
ELO: 92.77 +-2.1 (95%) LOS: 100.0%
Total: 60000 W: 24193 L: 8543 D: 27264
Ptnml(0-2): 609, 3850, 9708, 10948, 4885
40000 @ 20+0.2 th 8
https://tests.stockfishchess.org/tests/view/5f290229a5abc164f05e4c58
ELO: 89.47 +-2.0 (95%) LOS: 100.0%
Total: 40000 W: 12756 L: 2677 D: 24567
Ptnml(0-2): 74, 1583, 8550, 7776, 2017
At the same time, the impact on the classical evaluation remains minimal, causing no significant
regression:
sprt @ 10+0.1 th 1
https://tests.stockfishchess.org/tests/view/5f2906a2a5abc164f05e4c5b
LLR: 2.94 (-2.94,2.94) {-6.00,-4.00}
Total: 34936 W: 6502 L: 6825 D: 21609
Ptnml(0-2): 571, 4082, 8434, 3861, 520
sprt @ 60+0.6 th 1
https://tests.stockfishchess.org/tests/view/5f2906cfa5abc164f05e4c5d
LLR: 2.93 (-2.94,2.94) {-6.00,-4.00}
Total: 10088 W: 1232 L: 1265 D: 7591
Ptnml(0-2): 49, 914, 3170, 843, 68
The needed networks can be found at https://tests.stockfishchess.org/nns
It is recommended to use the default one as indicated by the `EvalFile` UCI option.
Guidelines for testing new nets can be found at
https://github.com/glinscott/fishtest/wiki/Creating-my-first-test#nnue-net-tests
Integration has been discussed in various issues:
https://github.com/official-stockfish/Stockfish/issues/2823
https://github.com/official-stockfish/Stockfish/issues/2728
The integration branch will be closed after the merge:
https://github.com/official-stockfish/Stockfish/pull/2825
https://github.com/official-stockfish/Stockfish/tree/nnue-player-wip
closes https://github.com/official-stockfish/Stockfish/pull/2912
This will be an exciting time for computer chess, looking forward to seeing the evolution of
this approach.
Bench: 4746616
2020-08-05 09:11:15 -06:00
|
|
|
constexpr bool is_ok(PieceId pid) {
|
|
|
|
return pid < PIECE_ID_NONE;
|
|
|
|
}
|
|
|
|
|
2017-11-25 20:33:34 -07:00
|
|
|
constexpr bool is_ok(Square s) {
|
2011-06-11 08:50:14 -06:00
|
|
|
return s >= SQ_A1 && s <= SQ_H8;
|
|
|
|
}
|
|
|
|
|
2017-11-25 20:33:34 -07:00
|
|
|
constexpr File file_of(Square s) {
|
2011-05-22 01:52:39 -06:00
|
|
|
return File(s & 7);
|
2011-02-23 13:27:22 -07:00
|
|
|
}
|
|
|
|
|
2017-11-25 20:33:34 -07:00
|
|
|
constexpr Rank rank_of(Square s) {
|
2011-05-22 01:52:39 -06:00
|
|
|
return Rank(s >> 3);
|
2011-02-23 13:27:22 -07:00
|
|
|
}
|
|
|
|
|
2017-11-25 20:33:34 -07:00
|
|
|
constexpr Square relative_square(Color c, Square s) {
|
2011-05-22 01:52:39 -06:00
|
|
|
return Square(s ^ (c * 56));
|
2011-02-23 13:27:22 -07:00
|
|
|
}
|
|
|
|
|
2017-11-25 20:33:34 -07:00
|
|
|
constexpr Rank relative_rank(Color c, Rank r) {
|
2011-05-22 01:52:39 -06:00
|
|
|
return Rank(r ^ (c * 7));
|
2011-02-23 13:27:22 -07:00
|
|
|
}
|
|
|
|
|
2017-11-25 20:33:34 -07:00
|
|
|
constexpr Rank relative_rank(Color c, Square s) {
|
2011-10-02 01:33:40 -06:00
|
|
|
return relative_rank(c, rank_of(s));
|
2011-02-23 13:27:22 -07:00
|
|
|
}
|
|
|
|
|
2017-12-04 09:52:31 -07:00
|
|
|
constexpr Direction pawn_push(Color c) {
|
2016-09-23 11:28:34 -06:00
|
|
|
return c == WHITE ? NORTH : SOUTH;
|
2011-04-09 12:50:56 -06:00
|
|
|
}
|
|
|
|
|
2017-11-25 20:33:34 -07:00
|
|
|
constexpr Square from_sq(Move m) {
|
2011-12-04 02:53:40 -07:00
|
|
|
return Square((m >> 6) & 0x3F);
|
|
|
|
}
|
|
|
|
|
2017-11-25 20:33:34 -07:00
|
|
|
constexpr Square to_sq(Move m) {
|
2011-12-04 02:53:40 -07:00
|
|
|
return Square(m & 0x3F);
|
|
|
|
}
|
|
|
|
|
Add NNUE evaluation
This patch ports the efficiently updatable neural network (NNUE) evaluation to Stockfish.
Both the NNUE and the classical evaluations are available, and can be used to
assign a value to a position that is later used in alpha-beta (PVS) search to find the
best move. The classical evaluation computes this value as a function of various chess
concepts, handcrafted by experts, tested and tuned using fishtest. The NNUE evaluation
computes this value with a neural network based on basic inputs. The network is optimized
and trained on the evalutions of millions of positions at moderate search depth.
The NNUE evaluation was first introduced in shogi, and ported to Stockfish afterward.
It can be evaluated efficiently on CPUs, and exploits the fact that only parts
of the neural network need to be updated after a typical chess move.
[The nodchip repository](https://github.com/nodchip/Stockfish) provides additional
tools to train and develop the NNUE networks.
This patch is the result of contributions of various authors, from various communities,
including: nodchip, ynasu87, yaneurao (initial port and NNUE authors), domschl, FireFather,
rqs, xXH4CKST3RXx, tttak, zz4032, joergoster, mstembera, nguyenpham, erbsenzaehler,
dorzechowski, and vondele.
This new evaluation needed various changes to fishtest and the corresponding infrastructure,
for which tomtor, ppigazzini, noobpwnftw, daylen, and vondele are gratefully acknowledged.
The first networks have been provided by gekkehenker and sergiovieri, with the latter
net (nn-97f742aaefcd.nnue) being the current default.
The evaluation function can be selected at run time with the `Use NNUE` (true/false) UCI option,
provided the `EvalFile` option points the the network file (depending on the GUI, with full path).
The performance of the NNUE evaluation relative to the classical evaluation depends somewhat on
the hardware, and is expected to improve quickly, but is currently on > 80 Elo on fishtest:
60000 @ 10+0.1 th 1
https://tests.stockfishchess.org/tests/view/5f28fe6ea5abc164f05e4c4c
ELO: 92.77 +-2.1 (95%) LOS: 100.0%
Total: 60000 W: 24193 L: 8543 D: 27264
Ptnml(0-2): 609, 3850, 9708, 10948, 4885
40000 @ 20+0.2 th 8
https://tests.stockfishchess.org/tests/view/5f290229a5abc164f05e4c58
ELO: 89.47 +-2.0 (95%) LOS: 100.0%
Total: 40000 W: 12756 L: 2677 D: 24567
Ptnml(0-2): 74, 1583, 8550, 7776, 2017
At the same time, the impact on the classical evaluation remains minimal, causing no significant
regression:
sprt @ 10+0.1 th 1
https://tests.stockfishchess.org/tests/view/5f2906a2a5abc164f05e4c5b
LLR: 2.94 (-2.94,2.94) {-6.00,-4.00}
Total: 34936 W: 6502 L: 6825 D: 21609
Ptnml(0-2): 571, 4082, 8434, 3861, 520
sprt @ 60+0.6 th 1
https://tests.stockfishchess.org/tests/view/5f2906cfa5abc164f05e4c5d
LLR: 2.93 (-2.94,2.94) {-6.00,-4.00}
Total: 10088 W: 1232 L: 1265 D: 7591
Ptnml(0-2): 49, 914, 3170, 843, 68
The needed networks can be found at https://tests.stockfishchess.org/nns
It is recommended to use the default one as indicated by the `EvalFile` UCI option.
Guidelines for testing new nets can be found at
https://github.com/glinscott/fishtest/wiki/Creating-my-first-test#nnue-net-tests
Integration has been discussed in various issues:
https://github.com/official-stockfish/Stockfish/issues/2823
https://github.com/official-stockfish/Stockfish/issues/2728
The integration branch will be closed after the merge:
https://github.com/official-stockfish/Stockfish/pull/2825
https://github.com/official-stockfish/Stockfish/tree/nnue-player-wip
closes https://github.com/official-stockfish/Stockfish/pull/2912
This will be an exciting time for computer chess, looking forward to seeing the evolution of
this approach.
Bench: 4746616
2020-08-05 09:11:15 -06:00
|
|
|
// Return relative square when turning the board 180 degrees
|
|
|
|
constexpr Square rotate180(Square sq) {
|
|
|
|
return (Square)(sq ^ 0x3F);
|
|
|
|
}
|
|
|
|
|
2017-11-25 20:33:34 -07:00
|
|
|
constexpr int from_to(Move m) {
|
2017-05-26 00:42:50 -06:00
|
|
|
return m & 0xFFF;
|
|
|
|
}
|
|
|
|
|
2017-11-25 20:33:34 -07:00
|
|
|
constexpr MoveType type_of(Move m) {
|
2012-06-24 03:08:16 -06:00
|
|
|
return MoveType(m & (3 << 14));
|
2011-12-04 02:53:40 -07:00
|
|
|
}
|
|
|
|
|
2017-11-25 20:33:34 -07:00
|
|
|
constexpr PieceType promotion_type(Move m) {
|
2015-02-07 11:34:24 -07:00
|
|
|
return PieceType(((m >> 12) & 3) + KNIGHT);
|
2011-12-04 02:53:40 -07:00
|
|
|
}
|
|
|
|
|
2018-07-16 14:53:50 -06:00
|
|
|
constexpr Move make_move(Square from, Square to) {
|
2016-09-17 00:19:06 -06:00
|
|
|
return Move((from << 6) + to);
|
2011-12-04 02:53:40 -07:00
|
|
|
}
|
|
|
|
|
2019-09-11 05:46:08 -06:00
|
|
|
constexpr Move reverse_move(Move m) {
|
|
|
|
return make_move(to_sq(m), from_sq(m));
|
|
|
|
}
|
|
|
|
|
2012-06-25 00:57:28 -06:00
|
|
|
template<MoveType T>
|
2017-11-25 20:33:34 -07:00
|
|
|
constexpr Move make(Square from, Square to, PieceType pt = KNIGHT) {
|
2016-09-17 00:19:06 -06:00
|
|
|
return Move(T + ((pt - KNIGHT) << 12) + (from << 6) + to);
|
2011-12-04 02:53:40 -07:00
|
|
|
}
|
|
|
|
|
2017-11-25 20:33:34 -07:00
|
|
|
constexpr bool is_ok(Move m) {
|
2015-01-09 04:35:44 -07:00
|
|
|
return from_sq(m) != to_sq(m); // Catch MOVE_NULL and MOVE_NONE
|
2011-12-04 02:53:40 -07:00
|
|
|
}
|
|
|
|
|
Fix fragile code to use proper random 64 bit keys.
This fixes an old issue where we want to make a position unique but only
change a small number of bits in the key instead of all 64 of them randomly.
This is fragile and can lead to non uniqueness issues in the TT.
Key make_key(uint64_t seed) takes any integer and produces a unique random 64 bit key.
It is computationally efficient and is based on a congruential pseudo random number
generator using well tested constants by Donald Knuth
(see https://en.wikipedia.org/wiki/Linear_congruential_generator)
STC https://tests.stockfishchess.org/tests/view/5ef6c78f761b685b4c724bb6
LLR: 2.95 (-2.94,2.94) {-1.50,0.50}
Total: 154320 W: 29343 L: 29376 D: 95601
Ptnml(0-2): 2543, 18170, 35891, 17889, 2667
LTC https://tests.stockfishchess.org/tests/view/5ef7d1a9020eec13834a940e
LLR: 2.95 (-2.94,2.94) {-1.50,0.50}
Total: 53488 W: 6629 L: 6584 D: 40275
Ptnml(0-2): 372, 4878, 16183, 4955, 356
closes https://github.com/official-stockfish/Stockfish/pull/2773
bench: 4626776
2020-06-26 18:26:46 -06:00
|
|
|
/// Based on a congruential pseudo random number generator
|
|
|
|
constexpr Key make_key(uint64_t seed) {
|
|
|
|
return seed * 6364136223846793005ULL + 1442695040888963407ULL;
|
|
|
|
}
|
|
|
|
|
2013-07-23 07:31:57 -06:00
|
|
|
#endif // #ifndef TYPES_H_INCLUDED
|
Fishtest Tuning Framework
The purpose of the code is to allow developers to easily and flexibly
setup SF for a tuning session. Mainly you have just to remove 'const'
qualifiers from the variables you want to tune and flag them for
tuning, so if you have:
int myKing = 10;
Score myBonus = S(5, 15);
Value myValue[][2] = { { V(100), V(20) }, { V(7), V(78) } };
and at the end of the update you may want to call
a post update function:
void my_post_update();
If instead of default Option's min-max values,
you prefer your custom ones, returned by:
std::pair<int, int> my_range(int value)
Or you jus want to set the range directly, you can
simply add below:
TUNE(SetRange(my_range), myKing, SetRange(-200, 200), myBonus, myValue, my_post_update);
And all the magic happens :-)
At startup all the parameters are printed in a
format suitable to be copy-pasted in fishtest.
In case the post update function is slow and you have many
parameters to tune, you can add:
UPDATE_ON_LAST();
And the values update, including post update function call, will
be done only once, after the engine receives the last UCI option.
The last option is the one defined and created as the last one, so
this assumes that the GUI sends the options in the same order in
which have been defined.
closes https://github.com/official-stockfish/Stockfish/pull/2654
No functional change.
2015-04-04 00:54:15 -06:00
|
|
|
|
|
|
|
#include "tune.h" // Global visibility to tuning setup
|