maxpumperla/ScalphaGoZero
Tentang
An independent implementation of DeepMind's AlphaGoZero in Scala, using Deeplearning4J (DL4J)
AI dan pembelajaran mesinPertumbuhan bintang terukur
- dalam 24 jam
- 0
- dalam 7 hari
- Mengumpulkan riwayat
- dalam 15 hari
- Mengumpulkan riwayat
- dalam 1 bulan
- Mengumpulkan riwayat
Perubahan bersih pada periode yang tumpang tindih, bukan total harian. Pengukuran yang belum tersedia dibiarkan kosong. Bintang menunjukkan perhatian, bukan kualitas.
README.md
AsliScalphaGoZero 
ScalphaGoZero is an independent implementation of DeepMind's AlphaGo Zero in Scala.
The neural net is a Dual ResNet policy/value model on Storch (LibTorch),
behind a small PolicyValueModel interface.
ScalphaGoZero is mainly an engineering effort to demonstrate how complex and successful systems in machine learning are not bound to Python anymore. With LibTorch via Storch and the mature infrastructure of the JVM, languages like Scala can offer a viable alternative for data scientists.
This project is a Scala port of the AlphaGo Zero module found in Deep Learning and the Game of Go.
Getting started
Prerequisite: Temurin / OpenJDK 21 (JDK 25+ has been seen to SIGSEGV mid-genmove with LibTorch).
cd ScalphaGoZero
sbt run
This application sets up two opponents, simulates games with AlphaGo Zero methodology, and trains one opponent from the experience. You can also accumulate training in a saved model and reuse it later.
Regenerate checked-in Dual-ResNet checkpoints (5×5 and 9×9) with:
sbt "runMain org.deeplearning4j.scalphagozero.app.GenerateStorchCheckpoints"
GTP Client (Sabaki)
Build a fat jar that speaks GTP for Go GUIs such as Sabaki:
sbt assembly
Prefer the platform wrappers (gtpClient.sh / gtpClient.bat): they pass JVM flags required for LibTorch/JavaCPP. A bare java -jar … gtp without those flags often dies with SIGSEGV in libjnitorch during Dual-ResNet construction.
GTP mode reads commands from stdin (Sabaki does this for you). A quick smoke test from the project root:
chmod +x gtpClient.sh # once, on macOS/Linux
printf 'protocol_version\nname\nboardsize 5\nclear_board\ngenmove b\nquit\n' | ./gtpClient.sh
You should see GTP replies such as = 2, = ScalphaGoZero, and a move like = a3.
For Sabaki, point the engine at the wrapper script for your OS (not a raw java -jar command). The wrappers live in the repo root and launch the fat jar in GTP mode.
macOS
- From the project root, make the shell wrapper executable (once):
chmod +x gtpClient.sh - In Sabaki, open Engines → Manage Engines and add an engine:
- Path: absolute path to
gtpClient.sh
(e.g./Users/<user>/projects/ScalphaGoZero/gtpClient.sh) - Initial directory / working directory: the project root
(e.g./Users/<user>/projects/ScalphaGoZero) so relativemodels/paths resolve. - Arguments (optional):
--layers 3for 9×9,--rounds 50for faster moves.
- Path: absolute path to
- Prefer Temurin 21.
gtpClient.shauto-picks/Library/Java/JavaVirtualMachines/temurin-21.jdk/...when present. - Prefer one engine vs human on 9×9 first (two engine instances each load a Dual-ResNet).
- GTP prefers a
*_trained.storchcheckpoint when present; otherwise the shippedmodels/model_size_*_layers_*.storch. Check Sabaki’s console for[ScalphaGoEngine] loading ….
Windows
- In Sabaki, open Engines → Manage Engines and add an engine:
- Path: absolute path to
gtpClient.bat
(e.g.C:\Users\you\projects\ScalphaGoZero\gtpClient.bat) - Initial directory / working directory: the project root
(e.g.C:\Users\you\projects\ScalphaGoZero).
- Path: absolute path to
- Ensure a JDK 21
javais on your PATH.

Then File → New. For Black, White, or both, select the ScalphaGoZero engine you added. The engine loads a checkpoint from models/ for the chosen board size (5×5: models/model_size_5_layers_2.storch; 9×9: models/model_size_9_layers_3.storch).

While the engine runs, information and errors go to output.log in the working directory.
Benchmarks
Use the non-interactive benchmark harness to measure training throughput and a small-board strength proxy against the frozen 5×5 checkpoint in models/.
# Tiny harness check (seconds–minutes)
sbt "runMain org.deeplearning4j.scalphagozero.app.ScalphaGoZero benchmark --config smoke"
# Official modernization baselines (can take a long time on CPU)
sbt "runMain org.deeplearning4j.scalphagozero.app.ScalphaGoZero benchmark --config A"
sbt "runMain org.deeplearning4j.scalphagozero.app.ScalphaGoZero benchmark --config B"
Results append to performance/benchmark-results.csv. Side-by-side interpretation: performance/COMPARISON.md. Historical 2020 GPU timings remain in performance/performance-results.csv for reference only (different hardware/stack).
| Config | Board | Blocks | MCTS rounds | Episodes | Eval games |
|---|---|---|---|---|---|
| smoke | 5 | 2 | 5 | 2 | 2 |
| A | 5 | 2 | 50 | 20 | 10 |
| B | 5 | 2 | 100 | 50 | 10 |
Optional overrides: --rounds, --episodes, --batch, --eval-games, --eval-rounds, --seed, --model.
Env: SCALPHAGO_ML_BACKEND (only storch is supported).
Smoke snapshot (same Mac aarch64 CPU): Phase 0 DL4J baseline ~26 s/episode → Storch ~0.2–1.3 s/episode (~20–100×). Strength on smoke (n=2) is noisy; use config A for a 10-game proxy. See COMPARISON.md.
Overnight 9×9 training
Chunked self-play via trainOvernight.sh (fresh JVM per chunk) writes
models/model_size_9_layers_3_trained.storch. Setup, clean restart, env overrides, and failure
cheat-sheet: docs/overnight-training.md.
Measure _trained vs the seed checkpoint (default 20 games @ 50 MCTS, stdout win rate):
sbt "runMain org.deeplearning4j.scalphagozero.app.ScalphaGoZero eval"
ML backends
Agents/GTP talk to PolicyValueModel.
The only runtime backend is Storch (*.storch checkpoints under models/).
DL4J was removed from the classpath after a Storch-first spike; see docs/ml-backend-decision.md.
CUDA is an opt-in classpath (sbt -Dscalphago.cuda=true) on Windows and Linux. Mac stays on the CPU jar. See docs/gpu.md.
AGZ fidelity
MCTS uses network-value leaves by default, Dirichlet noise + temperature on self-play, and a tanh value head in [-1, 1].
Legacy MC leaf eval remains behind useMcPlayout=true. See docs/agz-fidelity.md.
Dependencies
| Component | Version | Notes |
|---|---|---|
| Scala | 3.6.4 | Required to read storch_core TASTy (3.3 LTS cannot) |
| Storch | 0.7.6-1.5.12 | io.github.mullerhai:storch_core on Maven Central |
| LibTorch / OpenBLAS | 2.7.1-1.5.12 / 0.3.30-1.5.12 | JavaCPP natives; classifier matches host OS/arch |
| sbt | 1.11.7 | |
| Logback | 1.2.13 |
Project version is 1.2.0 (build.sbt).
Core Concepts
ScalphaGoZero aims for clean abstractions that developers can reuse beyond Go; only the basics are Go-specific.
- Basics: game primitives — Go board, player, move, game state — including Zobrist hashing for speed.
- Encoders:
Game states and moves become float tensors (
Array[Float]NCHW). AlphaGo Zero uses ZeroEncoder; other encoders can be implemented by the user. - Agents: Select the next move and track game state. AlphaGo Zero needs a ZeroAgent; simpler agents can still play decently.
- Models:
Policy and value heads in one Dual ResNet —
DualResNetNetviaStorchPolicyValueModel. - Scoring: End-of-game territory and results for reinforcement.
- Experience: Self-play data via experience collectors.
- Simulation:
Run two
ZeroAgents, store experience, and train so play improves over time.
Contributing
Ideas and planned improvements live in GitHub Issues. Pull requests that fix bugs or advance an open issue are welcome.
How to run unit and integration tests locally: docs/testing.md.
Jelajahi proyek terkait
Memuat…