Skip to content
patrickfrank1Public

About

Embedding based chess position search and embedding learning for chess positions

Topics

Resources

Stars

18 stars

Watchers

1 watching

Forks

Repository files navigation

Chess Position Embeddings

Cheat Sheet

  • Generate training positions

    uv run python -m src.run.generate_hf_dataset --config configs/pipeline.yaml

  • Train a neural network

    uv run python -m src.run.train_masked_transformer

  • Evaluate a trained network with the notebooks in src/run/ (evaluate_token_models.ipynb)

Generate HuggingFace Dataset

Generates chess position datasets from PGN files and pushes them to HuggingFace Hub.

Setup

  1. Authenticate with HuggingFace Hub:

    uv run huggingface-cli login

    You'll need a HuggingFace access token (get one at https://huggingface.co/settings/tokens).

  2. Prepare PGN files:

    Place your PGN files in data/raw/. The directory already contains Lichess elite game files.

Usage

You can configure the pipeline either via CLI flags or a YAML config file.

Via YAML config

Copy and edit the template:

cp configs/pipeline.yaml my_pipeline.yaml

Run with the config:

uv run python -m src.run.generate_hf_dataset --config my_pipeline.yaml

CLI flags override YAML values when both are provided:

uv run python -m src.run.generate_hf_dataset \
  --config my_pipeline.yaml \
  --min-elo 2500 \
  --dry-run

Via CLI only

Basic usage (dry run to test locally):

uv run python -m src.run.generate_hf_dataset --repo your-username/test-dataset --dry-run --batches 1

Note: even a dry run authenticates with HuggingFace Hub (the client checks the token on startup). Use --debug for single-threaded local mode — but see the known issue below.

Push to HuggingFace Hub:

uv run python -m src.run.generate_hf_dataset --repo your-username/chesspos-positions --batches 3

Configuration reference

Precedence: CLI args > YAML file > defaults.

YAML key CLI flag Default Description
repo_name --repo required HuggingFace Hub repository name
data_path --data-path ./data/raw Directory with .pgn files
batch_size --batch-size 100000 Games per batch
train_ratio --train-ratio 0.95 Train/test split ratio
num_batches --batches 3 Number of batches to generate
dry_run --dry-run false Generate locally without pushing
output_dir --output-dir (none) Write parquet shards here in dry-run mode
resume --resume false Continue from last batch on Hub
create_card --create-card false Push a dataset card
preprocessing.worker_count --workers 4 Ray parallel workers
preprocessing.memory_limit_mb --memory 4096 Memory per worker (MB)
preprocessing.debug --debug false Single-threaded local mode (see known issue)
sampling.tiers (YAML only) see below Tiered game subsampling by player strength
sampling.min_time_control_seconds (YAML only) 300 Exclude games with base time below this (e.g. bullet)

Default sampling tiers:

sampling:
  tiers:
    - min_elo: 2400
      rate: 1.0
    - min_elo: 2000
      rate: 0.4
    - min_elo: 0
      rate: 0.001

A game is matched to the strictest tier both players qualify for (missing Elo counts as 0) and kept with that tier's rate.

Example with custom settings:

uv run python -m src.run.generate_hf_dataset \
  --repo your-username/chesspos-positions \
  --batches 5 \
  --batch-size 50000 \
  --workers 8 \
  --train-ratio 0.9 \
  --create-card

Monitoring with the Ray Dashboard

The Ray dashboard is included via the ray[default] dependency and starts automatically when a run begins:

  • Dashboard UI: http://127.0.0.1:8265 (the URL is printed when Ray starts). The Data tab shows live per-operator progress of the ETL pipeline (read → extract → limit → encode → filter); the Jobs and Cluster tabs show workers, CPU/memory usage, and errors.
  • Terminal: Ray Data prints per-stage progress bars to stdout while the pipeline runs.
  • Logs: driver and worker logs live under /tmp/ray/session_latest/logs/ (Ray Data logs in the ray-data/ subdirectory). Logs persist after the run ends until the next session starts.

To watch a long run in the dashboard as a proper job, start a long-lived local cluster first and submit through it:

uv run ray start --head
uv run ray job submit --address http://127.0.0.1:8265 -- \
  python -m src.run.generate_hf_dataset --config configs/pipeline.yaml

The job then appears in the dashboard's Jobs tab, including stdout/stderr, even if the submitting terminal is closed. Stop the cluster afterwards with uv run ray stop.

Full local run over data/raw as a Ray job

Example: process everything in data/raw/ in a single batch and write the Parquet shards locally (dry run, no Hub push):

uv run ray job submit --address http://127.0.0.1:8265 \
  --runtime-env-json '{"working_dir": ".", "excludes": ["data", "test", "docs", "notebooks"]}' \
  -- python -m src.run.generate_hf_dataset \
    --repo local/full-run \
    --data-path "$PWD/data/raw" \
    --output-dir "$PWD/data/processed" \
    --dry-run --batches 1 --batch-size 5000000 --workers 12

Two details matter here:

  • --runtime-env-json with excludes: job mode packages the working directory and uploads it to the cluster, with a 500 MiB limit for a local working_dir. data/ (several GB) must be excluded from that package.
  • Absolute paths for --data-path and --output-dir: the job runs in an uploaded copy of the repo, not in the real directory, so relative paths point into the copy. Absolute paths read/write the real data/ on the machine (for a single-node local cluster this is what you want).

A submitted job keeps running even if the submitting terminal is closed; stream the logs with uv run ray job logs --follow <job-id> or watch them in the dashboard.

Known issue: --debug (Ray local_mode=True) currently fails at ray.init on Ray 2.54 with a working_dir URI validation error. Run without --debug (a normal local cluster with --workers 1 works fine).

Training on a Ray cluster

The training job (src/run/train_ray.py) runs on the same Ray cluster as the ETL via Ray Train (TorchTrainer). Data is snapshot-downloaded from the HuggingFace dataset repo inside each training worker, so the ETL stays a separate job on the same cluster.

On the training machine (e.g. a GPU box), start/join the cluster and submit:

uv run ray start --head   # or point RAY_ADDRESS at the existing head node
uv run ray job submit --address http://127.0.0.1:8265 \
  --runtime-env-json '{"working_dir": ".", "excludes": ["data", ".venv", "ray_results"]}' \
  -- python -m src.run.train_ray --config configs/training.yaml

Notes:

  • Ray Train resolves checkpoints into --storage-path (default ray_results/); use a path on a shared filesystem for multi-node clusters.
  • --num-training-workers > 1 enables DDP; with one GPU per worker keep --use-gpu (auto-detected on the training machine).
  • Run the submit command through uv run --extra gpu (or uv run --active): when the driver runs under uv run, Ray replicates the same uv run invocation on the workers; without the extra they would build an environment without torch.
  • MLflow tracking and checkpoint upload to the HF bucket reuse the same flags as the standalone trainer (--mlflow-uri, --upload-bucket).

Tools

  • Start ML Flow UI, in correct python venv

    mlflow ui

  • Interactive position evaluator with a clickable board, selectable score/sacrifice checkpoints, Stockfish 19 running locally in the browser, and per-square occlusion/KernelSHAP attribution:

    uv run python -m src.app.chess_eval --host 127.0.0.1 --port 8000

    See docs/chess_eval_widget.md and docs/attribution.md.

  • Position similarity search over the vector index, with a FEN box, example middlegames, and rendered boards:

    uv run python -m src.run.serve_position_search
    --index data/vector/ae-20k
    --checkpoint data/models/run_ae_20m_100k/last.pt

    See docs/position_vector_index.md.

About

Embedding based chess position search and embedding learning for chess positions

Topics

Resources

Stars

18 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages