Skip to content
项目

关于

An independent implementation of DeepMind's AlphaGoZero in Scala, using Deeplearning4J (DL4J)

人工智能与机器学习

这些数字如何统计

实测星标增长

24小时
0
7天
正在收集历史
15天
正在收集历史
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

ScalphaGoZero CI

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

  1. From the project root, make the shell wrapper executable (once):
    chmod +x gtpClient.sh
    
  2. 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 relative models/ paths resolve.
    • Arguments (optional): --layers 3 for 9×9, --rounds 50 for faster moves.
  3. Prefer Temurin 21. gtpClient.sh auto-picks /Library/Java/JavaVirtualMachines/temurin-21.jdk/... when present.
  4. Prefer one engine vs human on 9×9 first (two engine instances each load a Dual-ResNet).
  5. GTP prefers a *_trained.storch checkpoint when present; otherwise the shipped models/model_size_*_layers_*.storch. Check Sabaki’s console for [ScalphaGoEngine] loading ….

Windows

  1. 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).
  2. Ensure a JDK 21 java is on your PATH.

add ScalphaGoZero engine to Sabaki

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).

Sabaki play with ScalphaGoZero engine

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.

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.

探索相关项目

加载中…