Ceval with Stockfish NNUE

pull/8154/head
Hiroshi Ogawa 2021-02-11 20:58:25 +09:00
parent 032bbac58e
commit 18fc042ae2
8 changed files with 38 additions and 9 deletions

1
.gitignore vendored
View File

@ -16,6 +16,7 @@ public/vendor/highcharts-4.2.5
public/vendor/hopscotch
public/vendor/tagify
public/vendor/stockfish.wasm
public/vendor/stockfish-nnue.wasm
public/vendor/stockfish-mv.wasm
public/vendor/stockfish.js
public/css/

View File

@ -65,6 +65,11 @@ export default function (opts: CevalOpts): CevalCtrl {
const sharedMem = sharedWasmMemory(8, 16);
if (sharedMem) {
technology = 'wasmx';
// prettier-ignore
const source_with_simd = Uint8Array.from([0, 97, 115, 109, 1, 0, 0, 0, 1, 5, 1, 96, 0, 1, 123, 3, 2, 1, 0, 7, 8, 1, 4, 116, 101, 115, 116, 0, 0, 10, 15, 1, 13, 0, 65, 0, 253, 17, 65, 0, 253, 17, 253, 186, 1, 11]);
if (officialStockfish(opts.variant.key) && WebAssembly.validate(source_with_simd)) {
technology = 'nnue';
}
try {
sharedMem.grow(8);
growableSharedMem = true;
@ -105,12 +110,13 @@ export default function (opts: CevalOpts): CevalCtrl {
wasmx: officialStockfish(opts.variant.key)
? 'vendor/stockfish.wasm/stockfish.js'
: 'vendor/stockfish-mv.wasm/stockfish.js',
nnue: 'vendor/stockfish-nnue.wasm/stockfish.js',
},
{
minDepth,
variant: opts.variant.key,
threads: technology == 'wasmx' && (() => Math.min(parseInt(threads()), maxThreads)),
hashSize: technology == 'wasmx' && (() => Math.min(parseInt(hashSize()), maxHashSize)),
threads: (technology == 'wasmx' || technology == 'nnue') && (() => Math.min(parseInt(threads()), maxThreads)),
hashSize: (technology == 'wasmx' || technology == 'nnue') && (() => Math.min(parseInt(hashSize()), maxHashSize)),
}
);
@ -141,6 +147,8 @@ export default function (opts: CevalOpts): CevalCtrl {
if (knps > 3500) depth = 25;
if (knps > 5000) depth = 26;
if (knps > 7000) depth = 27;
// TODO: Maybe we want to get deeper for slow NNUE?
depth += 2 * Number(technology === 'nnue');
maxDepth(depth);
if (values.length > 40) values.shift();
}
@ -252,8 +260,8 @@ export default function (opts: CevalOpts): CevalCtrl {
possible: opts.possible,
enabled,
multiPv,
threads: technology == 'wasmx' ? threads : undefined,
hashSize: technology == 'wasmx' ? hashSize : undefined,
threads: technology == 'wasmx' || technology == 'nnue' ? threads : undefined,
hashSize: technology == 'wasmx' || technology == 'nnue' ? hashSize : undefined,
maxThreads,
maxHashSize,
infinite,

View File

@ -131,9 +131,11 @@ export class Pool {
warmup(): void {
if (this.workers.length) return;
if (this.poolOpts.technology == 'wasmx')
if (this.poolOpts.technology == 'nnue') {
this.workers.push(new ThreadedWasmWorker(this.poolOpts.nnue, this.poolOpts, this.protocolOpts));
} else if (this.poolOpts.technology == 'wasmx') {
this.workers.push(new ThreadedWasmWorker(this.poolOpts.wasmx, this.poolOpts, this.protocolOpts));
else {
} else {
for (let i = 1; i <= 2; i++)
this.workers.push(
new WebWorker(

View File

@ -2,7 +2,7 @@ import { Outcome } from 'chessops/types';
import { Prop } from 'common';
import { StoredProp, StoredBooleanProp } from 'common/storage';
export type CevalTechnology = 'asmjs' | 'wasm' | 'wasmx';
export type CevalTechnology = 'asmjs' | 'wasm' | 'wasmx' | 'nnue';
export interface Eval {
cp?: number;
@ -33,6 +33,7 @@ export interface PoolOpts {
wasm: string;
wasmx: string;
asmjs: string;
nnue: string;
}
export interface CevalOpts {

View File

@ -77,9 +77,11 @@ function engineName(ctrl: CevalCtrl): VNode[] {
h(
'span',
version ? { attrs: { title: `${version} (classical eval)` } } : {},
ctrl.technology == 'wasmx' ? 'Stockfish 12+' : 'Stockfish 10+'
ctrl.technology == 'wasmx' || ctrl.technology == 'nnue' ? 'Stockfish 12+' : 'Stockfish 10+'
),
ctrl.technology == 'wasmx'
ctrl.technology == 'nnue'
? h('span.wasmx', { attrs: { title: 'Multi-threaded WebAssembly NNUE (strongest)' } }, 'nnue')
: ctrl.technology == 'wasmx'
? h('span.wasmx', { attrs: { title: 'Multi-threaded WebAssembly (fastest)' } }, 'wasmx')
: ctrl.technology == 'wasm'
? h('span.wasm', { attrs: { title: 'Single-threaded WebAssembly fallback (second fastest)' } }, 'wasm')

View File

@ -21,6 +21,7 @@
"hopscotch": "^0.3.1",
"jquery-bar-rating": "^1.2.2",
"stockfish-mv.wasm": "^0.5.2",
"stockfish-nnue.wasm": "0.0.1",
"stockfish.js": "^10.0.2",
"stockfish.wasm": "^0.9.1",
"tablesort": "^5.1",

View File

@ -61,6 +61,15 @@ export default rollupProject({
].map(require.resolve),
dest: '../../public/vendor/stockfish-mv.wasm',
},
// stockfish-nnue.wasm
{
src: [
'stockfish-nnue.wasm/stockfish.js',
'stockfish-nnue.wasm/stockfish.wasm',
'stockfish-nnue.wasm/stockfish.worker.js',
].map(require.resolve),
dest: '../../public/vendor/stockfish-nnue.wasm',
},
],
}),
replace({

View File

@ -4137,6 +4137,11 @@ stockfish-mv.wasm@^0.5.2:
resolved "https://registry.yarnpkg.com/stockfish-mv.wasm/-/stockfish-mv.wasm-0.5.2.tgz#08faca73ae2f5b69fb28bd04c20074e443cdaa7e"
integrity sha512-MujfrAVAzDkRoVEiYMkGaiAw9wei2hRR8sxD5l7emHDyCbmc0KdlqoIZtS+coQ9CVNaZa1o5JokTKJVv8wJhUQ==
stockfish-nnue.wasm@0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/stockfish-nnue.wasm/-/stockfish-nnue.wasm-0.0.1.tgz#945a9904c5a1152bab98b9fdde7c517b1342e83f"
integrity sha512-ePulPKbGimjJEtTUYECh4986JnZv1rYVDsSlBOXu+1tvjWKjJSzoHhKpmtdGDG59iVtIBRamix593rfYqMqK2Q==
stockfish.js@^10.0.2:
version "10.0.2"
resolved "https://registry.yarnpkg.com/stockfish.js/-/stockfish.js-10.0.2.tgz#c8206aa3b0290171ace52b463a74112ea1f83989"