Skip to content

Commit

Permalink
0.0.4 ready for publish
Browse files Browse the repository at this point in the history
  • Loading branch information
schlawg committed May 11, 2024
1 parent 00647c9 commit c520e9d
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 90 deletions.
45 changes: 31 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,56 @@ Multiple stockfish wasms for use in lichess.org web analysis

## Building
```
# Example: Make debug builds for node with SAFE_HEAP
# Example: Clean and make all debug builds for node with SAFE_HEAP
./build.py --flags='-O0 -g3 -sSAFE_HEAP' --node all
./build.py --flags='-O0 -g3 -sSAFE_HEAP' --node all clean
```
Omit `--node` for default web builds. omit `--flags` to use default em++ flags (-O3 -DNDEBUG --closure=1)
Available targets are `clean`, `all`, `sf16-40`, `sf16-linrock-7`, and `fsf14`.

`./build.py` downloads sources to the `./fishes` folder. It then applies diffs from the `./patches` folder.
Finally, it builds the targets in the `./builds` folder. Edit the Stockfish sources freely. But to share
them here, you must update the patch file.
or to avoid installing or changing your emscripten version, use `./build-with-docker.sh`:

```
# Example: Update `sf16-linrock-7.patch` with your source changes:
./build-with-docker.sh --flags='-O3' all clean
```

omit `--node` for default web builds

use `--flags` to override the default emcc flags which are `-O3 -DNDEBUG --closure=1`

check `./build.py --help` for the latest targets

`./build.py` downloads sources to the `./fishes` folder then applies diffs from the `./patches` folder.
Edit the Stockfish sources freely. But to contribute your edits, use a patch file

cd fishes/sf16-linrock-7
git diff > ../../patches/sf16-linrock-7.patch
```
# Example: Update `sf16-7.patch` with your source changes:
cd fishes/sf16-7
git diff > ../../patches/sf16-7.patch
```

## Sources

This maps targets to their ancestor repo/commit:

### sf16-40
### sf16-40 (Official Stockfish 16 release)
- repo: https://github.com/official-stockfish/Stockfish
- commit: [68e1e9b](https://github.com/official-stockfish/Stockfish/commit/68e1e9b)
- tag: sf_16
- nnue: [nn-5af11540bbfe.nnue](https://tests.stockfishchess.org/nns?network_name=nn-5af11540bbfe)

### sf16-linrock-7
### sf16-7 (Stockfish 16 linrock)
- repo: https://github.com/linrock/Stockfish
- commit: [c97f5cb](https://github.com/linrock/Stockfish/commit/c97f5cb)
- nnue: [nn-ecb35f70ff2a.nnue](https://tests.stockfishchess.org/nns?network_name=nn-ecb35f70ff2a)

### fsf14
### sf161-70 (Official Stockfish 16.1 release)
- repo: https://github.com/official-stockfish/Stockfish
- commit: [e67cc97](https://github.com/official-stockfish/Stockfish/commit/e67cc97)
- tag: sf_16.1
- big nnue: [nn-b1a57edbea57.nnue](https://tests.stockfishchess.org/nns?network_name=nn-b1a57edbea57)
- small nnue: [nn-baff1ede1f90.nnue](https://tests.stockfishchess.org/nns?network_name=nn-baff1ede1f90)

### fsf14 (Fairy-Stockfish 14)
- repo: https://github.com/fairy-stockfish/Fairy-Stockfish
- commit: [a621470](https://github.com/fairy-stockfish/Fairy-Stockfish/commit/a621470)
- commit: [a621470](https://github.com/fairy-stockfish/Fairy-Stockfish/commit/a621470)
- nnues: see repo links
10 changes: 6 additions & 4 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,11 @@ def main():
print(f"building: {', '.join(arg_targets)}{' for node.js' if args.node else ''}")
print(f"flags: {args.flags}")
print("")

for target in arg_targets:
build_target(target, args.flags, args.node)
try:
for target in arg_targets:
build_target(target, args.flags, args.node)
except Exception as e:
print(e)


def build_target(target, flags, node): # changes cwd
Expand Down Expand Up @@ -120,7 +122,7 @@ def build_target(target, flags, node): # changes cwd

def fetch_sources(target):
if target not in targets:
raise Exception(f"unknown target {target}")
raise Exception(f"unknown target: {target}")
target_dir = os.path.join(fishes_dir, target)
if not os.path.exists(target_dir):
os.makedirs(target_dir)
Expand Down
16 changes: 0 additions & 16 deletions patches/fsf14.patch
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,6 @@ index afad7f13..87763a2d 100644
UCI::init(Options);
Tune::init();
PSQT::init(variants.find(Options["UCI_Variant"])->second);
diff --git a/src/nnue/nnue_feature_transformer.h b/src/nnue/nnue_feature_transformer.h
index fbb0c455..1a36507f 100644
--- a/src/nnue/nnue_feature_transformer.h
+++ b/src/nnue/nnue_feature_transformer.h
@@ -32,10 +32,7 @@ namespace Stockfish::Eval::NNUE {
using WeightType = std::int16_t;
using PSQTWeightType = std::int32_t;

- // If vector instructions are enabled, we update and refresh the
- // accumulator tile by tile such that each tile fits in the CPU's
- // vector registers.
- #define VECTOR
+ // For WASM, let clang auto-vectorize

static_assert(PSQTBuckets % 8 == 0,
"Per feature PSQT values cannot be processed at granularity lower than 8 at a time.");
diff --git a/src/search.cpp b/src/search.cpp
index 77686ed0..b2db2bdd 100644
--- a/src/search.cpp
Expand Down
16 changes: 0 additions & 16 deletions patches/sf16-40.patch
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,6 @@ index c40e0fa3..0d5c05cd 100644
UCI::init(Options);
Tune::init();
PSQT::init();
diff --git a/src/nnue/nnue_feature_transformer.h b/src/nnue/nnue_feature_transformer.h
index 7571f398..1f5bc32b 100644
--- a/src/nnue/nnue_feature_transformer.h
+++ b/src/nnue/nnue_feature_transformer.h
@@ -33,10 +33,7 @@ namespace Stockfish::Eval::NNUE {
using WeightType = std::int16_t;
using PSQTWeightType = std::int32_t;

- // If vector instructions are enabled, we update and refresh the
- // accumulator tile by tile such that each tile fits in the CPU's
- // vector registers.
- #define VECTOR
+ // For WASM, let clang auto-vectorize

static_assert(PSQTBuckets % 8 == 0,
"Per feature PSQT values cannot be processed at granularity lower than 8 at a time.");
diff --git a/src/search.cpp b/src/search.cpp
index 740ad71e..2d4a1291 100644
--- a/src/search.cpp
Expand Down
16 changes: 0 additions & 16 deletions patches/sf16-7.patch
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,6 @@ index 413dbb3d..81e3e52f 100644
constexpr IndexType PSQTBuckets = 8;
constexpr IndexType LayerStacks = 8;

diff --git a/src/nnue/nnue_feature_transformer.h b/src/nnue/nnue_feature_transformer.h
index 7571f398..1f5bc32b 100644
--- a/src/nnue/nnue_feature_transformer.h
+++ b/src/nnue/nnue_feature_transformer.h
@@ -33,10 +33,7 @@ namespace Stockfish::Eval::NNUE {
using WeightType = std::int16_t;
using PSQTWeightType = std::int32_t;

- // If vector instructions are enabled, we update and refresh the
- // accumulator tile by tile such that each tile fits in the CPU's
- // vector registers.
- #define VECTOR
+ // For WASM, let clang auto-vectorize

static_assert(PSQTBuckets % 8 == 0,
"Per feature PSQT values cannot be processed at granularity lower than 8 at a time.");
diff --git a/src/search.cpp b/src/search.cpp
index 740ad71e..2d4a1291 100644
--- a/src/search.cpp
Expand Down
13 changes: 0 additions & 13 deletions patches/sf161-70.patch
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,6 @@ index 1d089971..c06b2572 100644
std::string pathSeparator;

// Extract the path+name of the executable binary
diff --git a/src/nnue/nnue_feature_transformer.h b/src/nnue/nnue_feature_transformer.h
index 3399b82d..41b6e274 100644
--- a/src/nnue/nnue_feature_transformer.h
+++ b/src/nnue/nnue_feature_transformer.h
@@ -43,7 +43,7 @@ using PSQTWeightType = std::int32_t;
// If vector instructions are enabled, we update and refresh the
// accumulator tile by tile such that each tile fits in the CPU's
// vector registers.
-#define VECTOR
+//#define VECTOR

static_assert(PSQTBuckets % 8 == 0,
"Per feature PSQT values cannot be processed at granularity lower than 8 at a time.");
diff --git a/src/tt.cpp b/src/tt.cpp
index f3f58979..7e6c307f 100644
--- a/src/tt.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/dualnet-initModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Module['printErr'] = data => Module['onError']?.(data);

Module['getRecommendedNnue'] = (index /** number */) => UTF8ToString(_getRecommendedNnue(index));

Module['postMessage'] = function (uci) {
Module['uci'] = function (uci) {
const sz = lengthBytesUTF8(uci) + 1;
const utf8 = _malloc(sz); // deallocated in src/wasm/dualnet-glue.cpp
if (!utf8) throw new Error(`Could not allocate ${sz} bytes`);
Expand Down
2 changes: 1 addition & 1 deletion src/initModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Module['printErr'] = data => Module['onError']?.(data);

Module['getRecommendedNnue'] = () => UTF8ToString(_getRecommendedNnue());

Module['postMessage'] = function (uci) {
Module['uci'] = function (uci) {
const sz = lengthBytesUTF8(uci) + 1;
const utf8 = _malloc(sz); // deallocated in src/wasm/glue.cpp
if (!utf8) throw new Error(`Could not allocate ${sz} bytes`);
Expand Down
4 changes: 2 additions & 2 deletions src/wasm-cli.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as readline from 'node:readline';
import * as fs from 'node:fs';

const createStockfish = await import(`../${process.argv[2] ?? 'sf16-70.js'}`);
const createStockfish = await import(`../${process.argv[2] ?? 'sf161-70.js'}`);
let history = [],
index = 0;

Expand All @@ -22,7 +22,7 @@ rl.on('line', line => {
else if (line.startsWith('big ')) sf.setNnueBuffer(fs.readFileSync(line.slice(4)), 0);
else if (line.startsWith('small ')) sf.setNnueBuffer(fs.readFileSync(line.slice(6)), 1);
else if (line === 'exit' || line === 'quit') process.exit();
else sf.postMessage(line);
else sf.uci(line);
});

process.stdin.on('keypress', (_, key) => {
Expand Down
13 changes: 6 additions & 7 deletions stockfishWeb.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
declare module 'lila-stockfish-web' {
interface StockfishWeb {
postMessage(uci: string): void;
uci(command: string): void; // send uci command, receive async response via listen

listen: (data: string) => void; // attach listener here

setNnueBuffer(data: Uint8Array, index?: number): void;
// index argument is used to select between big and small nnue if the target is a dual nnue build
// 0 for big, 1 for small. single nnue wasms should ignore this parameter

getRecommendedNnue(index?: number): string; // returns a bare filename, 0 for big, 1 for small
// index argument is 0 for big, 1 for small. single nnue wasms should ignore this parameter
// index arguments are used for dual net sf builds, 0 for big, 1 for small, otherwise ignore

setNnueBuffer(data: Uint8Array, index?: number): void; // load nnue as buffer

getRecommendedNnue(index?: number): string; // returns a bare filename

onError: (msg: string) => void; // attach error handler here
}
Expand Down

0 comments on commit c520e9d

Please sign in to comment.