Skip to content

NOTE (old)

Hiroshi Ogawa edited this page Jul 30, 2021 · 3 revisions

Disclaimer: These notes might not be relevant to the current code base since these were used when developing the branch emscripten-76daa88c-2.0.11.


Mischellaneous things written down during development.

I took these notes when I was making this PR https://github.com/niklasf/stockfish.wasm/pull/30.

Originally at https://github.com/hi-ogawa/stockfish.wasm/wiki/NOTE, but now it's copied to here.

Build and Run

# Build (after enabled emscripten tools e.g. by emsdk_env.sh)
cd src
make build -j 4 wasm_simd_post_mvp=yes

# Run on NodeJS
# NOTE: emscripten's node (emsdk/node/12.18.1_64bit/bin/node) is old and doesn't seem to compatible with latest simd (cf. https://github.com/emscripten-core/emscripten/issues/11484).
#       So, I use locally installed node with this version.
node -e 'console.log(`node: ${process.version}\nv8: ${process.versions.v8}`)'
node: v15.8.0
v8: 8.6.395.17-node.23

# Example benchmark
node --experimental-wasm-{simd,threads} --experimental-repl-await --wasm-simd-post-mvp
> const sf = await require("./stockfish")();
> sf.postMessage("bench 16 1 22 current depth classical");
Total time (ms) : 5056
Nodes searched  : 3478378
Nodes/second    : 687970

> sf.postMessage("bench 16 1 22 current depth NNUE");
Total time (ms) : 6424
Nodes searched  : 2670151
Nodes/second    : 415652

Comparing to other build type

# [ with `wasm_simd=yes` ]
> sf.postMessage("bench 16 1 22 current depth NNUE");
Total time (ms) : 9472
Nodes searched  : 2670151
Nodes/second    : 281899

# [ without `wasm_simd` ]
> sf.postMessage("bench 16 1 22 current depth NNUE");
Total time (ms) : 54008
Nodes searched  : 2670151
Nodes/second    : 49439

Run on browser

python misc/server.py # then open http://localhost:8000/misc/test.html

Benchmark evaluate (essentially this measures the performance of matrix(32x512)-vector(512) multiplication)

/usr/bin/node --experimental-wasm-{simd,threads} --experimental-repl-await --wasm-simd-post-mvp
> const sf = await require("./stockfish")();
> sf.postMessage("setoption name Use NNUE value true")
> sf.postMessage("bench_eval")
1.860234 usec (stddev: 0.050028, min: 1.810873, max: 1.951648, n: 80000, r: 10)
> sf.postMessage("setoption name Use NNUE value false") # Non-NNUE case is not so relevant because Stockfish caches a lot of classical evaluation
> sf.postMessage("bench_eval")
447.088706 nsec (stddev: 10.019180, min: 434.404873, max: 465.951900, n: 400000, r: 10)

Feature detection for i32x4.dot_i16x8_s (see misc/feature_detection.wat for how these bytes are generated)

/usr/bin/node --experimental-wasm-simd
> data = 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])
> WebAssembly.validate(data)
false

/usr/bin/node --experimental-wasm-simd --wasm-simd-post-mvp
> data = 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])
> WebAssembly.validate(data)
true

Trigger match.yml action from REST API

node
> const { Octokit } = require("@octokit/core");
> const octokit = new Octokit({ auth: "<secret token>" });
> octokit.request('POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches', {
  owner: 'hi-ogawa',
  repo: 'stockfish.wasm',
  workflow_id: 'match.yml',
  ref: 'nnue-wasm-simd',
  inputs: {
    rounds: "10",
    concurrency: "2",
    time_control: "10+0.1",
    build_args: "wasm_simd_post_mvp=yes"
  }
}).then(console.log)

References

Clone this wiki locally