Files
NowChessSystems/modules/official-bots/python
Janis Eccarius 9d81198108
Build & Test (NowChessSystems) TeamCity build finished
feat(official-bots): implement king-relative (HalfKP) encoding in NNUE (NCS-109)
Replace absolute 768-feature encoding with dual-perspective king-relative
encoding (HalfKP style): each piece is encoded from both the white king's
and the black king's reference frame, yielding 98304 input features
(2 × 64 king-squares × 12 piece-types × 64 squares).

Key changes:
- NNUE.scala: featureIdxWhite/featureIdxBlack replace featureIndex;
  pushAccumulator now accepts childBoard and recomputes on king moves
  (castle or normal king move) instead of using stale incremental state;
  non-king moves update both perspectives incrementally (~4 column ops).
- EvaluationNNUE.scala: pass child.board to pushAccumulator.
- python/src/train.py: fen_to_features produces 98304-dim HalfKP vector;
  NNUE model input size updated to INPUT_SIZE (98304); DEFAULT_HIDDEN_SIZES
  reduced to [512, 256, 128] appropriate for sparse high-dim input.
- nnue_weights.nbai: replaced with placeholder 98304→16→8→1 model so
  tests compile and run; replace with a retrained model via Colab notebook.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-24 19:12:25 +02:00
..
2026-04-29 22:06:01 +02:00
2026-04-29 22:06:01 +02:00
2026-04-29 22:06:01 +02:00
2026-04-29 22:06:01 +02:00
2026-04-29 22:06:01 +02:00
2026-04-29 22:06:01 +02:00
2026-04-29 22:06:01 +02:00
2026-04-29 22:06:01 +02:00

NNUE Python Pipeline

Central CLI for training and exporting chess evaluation neural networks (NNUE).

Directory Structure

python/
├── nnue.py              # Main CLI entry point
├── src/                 # Python modules
│   ├── generate.py      # Generate random chess positions
│   ├── label.py         # Label positions with Stockfish
│   ├── train.py         # Train NNUE model
│   └── export.py        # Export weights to Scala
├── data/                # Training data (gitignored)
│   ├── positions.txt
│   └── training_data.jsonl
└── weights/             # Model weights (gitignored)
    ├── nnue_weights_v1.pt
    ├── nnue_weights_v1_metadata.json
    └── ...

Quick Start

# Train a new model (500k positions, auto-detect checkpoint)
python nnue.py train

# Train from specific checkpoint
python nnue.py train --from-checkpoint 2

# Train with custom games count
python nnue.py train --games 200000

# Train with custom positions file
python nnue.py train --positions-file my_positions.txt

# Export specific version to Scala
python nnue.py export 2

# List all checkpoints
python nnue.py list

CLI Commands

train - Train NNUE model

python nnue.py train [OPTIONS]

Options:

  • --from-checkpoint N - Resume from checkpoint version N (default: uses latest)
  • --games N - Number of games to generate (default: 500000)
  • --positions-file FILE - Use existing positions file instead of generating
  • --stockfish PATH - Path to Stockfish binary (default: $STOCKFISH_PATH or /usr/games/stockfish)

Examples:

# Train with latest checkpoint
python nnue.py train

# Train from v2 with 100k games
python nnue.py train --from-checkpoint 2 --games 100000

# Train with custom positions
python nnue.py train --positions-file my_games.txt --stockfish /opt/stockfish/sf15

export - Export weights to Scala

python nnue.py export WEIGHTS [output_path]

Arguments:

  • WEIGHTS - Version number (e.g., 2) or full filename (e.g., nnue_weights_v2.pt)

Examples:

# Export version 2
python nnue.py export 2

# Export with full filename
python nnue.py export nnue_weights_v3.pt

Output goes to ../src/main/scala/de/nowchess/bot/bots/nnue/NNUEWeights_vN.scala

list - List available checkpoints

python nnue.py list

Shows all available model versions with file sizes.

Data Flow

  1. Generatedata/positions.txt

    • Random chess positions from 8-20 move openings
    • Filters out checks, game-over states, and captures
  2. Labeldata/training_data.jsonl

    • Evaluates each position with Stockfish at depth 12
    • Stores FEN + evaluation in JSONL format
  3. Trainweights/nnue_weights_vN.pt

    • Trains neural network on labeled positions
    • Auto-versioning (v1, v2, v3, etc.)
    • Saves metadata alongside weights
  4. ExportNNUEWeights_vN.scala

    • Converts weights to Scala object
    • Ready for integration into bot

Versioning

  • Models are automatically versioned (v1, v2, v3, etc.)
  • Each version gets a _metadata.json file with training info
  • Training from checkpoint uses latest version unless specified with --from-checkpoint

Files

  • data/ and weights/ are gitignored (local artifacts)
  • Documentation in docs/ explains training, debugging, and incremental improvements
  • Source modules in src/ are independent and can be imported for custom workflows