Skip to content

Commit

Permalink
chore: Significantly improve docs, refactor some code and overhaul infra
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillbobyrev committed May 21, 2024
1 parent 258044e commit 5fdba74
Show file tree
Hide file tree
Showing 28 changed files with 158 additions and 133 deletions.
11 changes: 1 addition & 10 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,8 @@ jobs:
profile: minimal
toolchain: ${{ matrix.toolchain }}
override: true
- uses: actions/cache@v4
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-${{ matrix.toolchain }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Build the project
uses: actions-rs/cargo@v1
with:
command: build
args: --release --all-features
args: --release
11 changes: 1 addition & 10 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@ jobs:
toolchain: nightly
override: true
components: rustfmt, clippy
- uses: actions/cache@v4
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-nightly-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Cargo fmt
uses: actions-rs/cargo@v1
with:
Expand All @@ -34,6 +25,6 @@ jobs:
uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --all-features -- --deny clippy::all --deny clippy::pedantic
args: --all-features
- name: typos-action
uses: crate-ci/typos@v1.21.0
19 changes: 3 additions & 16 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,7 @@ jobs:
profile: minimal
toolchain: ${{ matrix.toolchain }}
override: true
- uses: actions/cache@v4
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-${{ matrix.toolchain }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Run cargo test
uses: actions-rs/cargo@v1
with:
command: test
# Using -- --ignored is not possible with actions-rs/cargo, run plain
# cargo instead.
- name: Run expensive testts
- name: Run basic tests
run: cargo test --release
- name: Run expensive tests
run: cargo test --release -- --ignored
2 changes: 0 additions & 2 deletions .ignore

This file was deleted.

3 changes: 1 addition & 2 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{
"recommendations": [
"bungcip.better-toml",
"tamasfe.even-better-toml",
"davidanson.vscode-markdownlint",
"matklad.rust-analyzer",
"redhat.vscode-yaml",
"serayuzgur.crates",
"streetsidesoftware.code-spell-checker",
"yzhang.markdown-all-in-one",
"editorconfig.editorconfig"
]
}
56 changes: 50 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,59 @@ This document describes high-level architecture of Pabi. If you want to get
familiar with the code and understand the project structure, this is a great
starting point.

## Design considerations
## Scope

Implementing a *general-purpose* chess engine that would truly excel in
different domains, such as playing with humans, being able to adjust the
strength, providing the best possible analysis for a position when given a lot
of time and, finally, playing well against other engines is an enormous task,
especially for one person. Therefore, it's crucial to choose the right scope and
design the engine with chosen limitations in mind.

Pabi chooses to be the best possible version of an engine that does well against
other chess engines in online tournaments. That means that it will prioritize
performance under the constraints put by the rules and environments of such
tournaments. Most important tournament organizers are
[TCEC](https://tcec-chess.com/) and
[CCCC](https://www.chess.com/computer-chess-championship), and the most
prominent rating to date is [CCRL](https://computerchess.org.uk/ccrl/). The
first goal is reaching 3000 ELO on the rating lists that are accepted by the
organizers of these tournaments.

Most of the competitions are in relatively fast time controls (Blitz, Bullet,
Rapid) with some exceptions in relatively short classical formats. These are the
[CCRL rules](https://computerchess.org.uk/ccrl/404/about.html) and [TCEC
rules](https://wiki.chessdom.org/Rules). In both rule sets the engines are given
opening books selected by the organizers and unknown to the participants, and
the engines start the game in pre-determined positions which creates space for
unbalance leading to decisive results.

In all cases, the testing environment's CPU is of x86_64 architecture, which is
very important because of PEXT and PDEP instructions that significantly increase
the performance of move generators. Also, the processors have multiple cores,
sometimes there as many as 256 cores as in CCC22:

```
CPUs | 2 x AMD EPYC 7H12
GPU | 2x A100 (40 GB GPU memory)
Cores | 256 cores (128 physical)
RAM | 512GB DIMM DDR4 2933 MHz (0.3 ns)
SSD | 2x Micron 5210 MTFD (2TB) in RAID1
OS | CentOS 8
```

This requires the engine to be very good at utilizing multi-threading.

Other design choices are deliberate focus on performance over most things
(except simplicity and clarity), such as error recovery (there should be minimal
one: if the error happened and the engine can reject the input, it will reject
the input and continue working) and support for arcane environments.

## Recipes

### Building

### Testing

### Fuzzing
Most commands for development, building the engine, testing, checking for errors
and fuzzing it are supported as [just](https://github.com/casey/just) recipes.
See [justfile](/justfile) for a complete list of frequently used commands.

## Code map

Expand Down
24 changes: 12 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Pabi

[![Codecov](https://codecov.io/gh/kirillbobyrev/pabi/branch/main/graph/badge.svg)](https://codecov.io/gh/kirillbobyrev/pabi)
[![Lines of Code](https://tokei.rs/b1/github/kirillbobyrev/pabi)](https://github.com/kirillbobyrev/pabi/tree/main/src)
[![Lines of Code](https://tokei.rs/b1/github/kirillbobyrev/pabi?type=Rust)](https://github.com/kirillbobyrev/pabi/tree/main/src)

[![Build](https://github.com/kirillbobyrev/pabi/actions/workflows/build.yml/badge.svg)](https://github.com/kirillbobyrev/pabi/actions/workflows/build.yml)
[![Test Suite](https://github.com/kirillbobyrev/pabi/actions/workflows/test.yml/badge.svg)](https://github.com/kirillbobyrev/pabi/actions/workflows/test.yml)
Expand All @@ -15,6 +15,9 @@ itself is implemented in this repository, training and development of the Neural
Network for position evaluation is in
[kirillbobyrev/pabi-brain](https://github.com/kirillbobyrev/pabi-brain).

For architecture, design and development process overview, please see
[CONTRIBUTING.md](/CONTRIBUTING.md).

## Goals

Pabi is inspired by existing Chess and Go engines (mainly [AlphaGo], [lc0],
Expand Down
2 changes: 1 addition & 1 deletion fuzz/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# This is a collection of commonly used recipes for development. Most of them
# are wrappers around Cargo and Cargo extensions with certain setup.

# Enable x86_64 features that are required for maximum performance.
compile_flags := "RUSTFLAGS='-C target-feature=+avx2,+fma,+bmi1,+bmi2'"

build:
{{ compile_flags }} cargo build --profile=fast

# Runs the engine and enters UCI mode.
run:
{{ compile_flags}} cargo run --profile=fast

# Checks the code for bad formatting, errors and warnings.
lint:
cargo +nightly fmt --all
cargo +nightly clippy --all-features --fix --allow-staged
typos

# Run most tests that are fast and are run by default.
test_basic:
cargo test

# Run tests that are slow and are not run by default.
test_slow:
cargo test -- --ignored

# Run all tests.
test: test_basic test_slow

bench:
{{ compile_flags }} cargo bench --profile=fast

# Lists all fuzzing targets that can be used as inputs for fuzz command.
list_fuzz_targets:
cd fuzz
cargo +nightly fuzz list

fuzz target:
cd fuzz
{{ compile_flags }} cargo +nightly fuzz run {{ target }}
6 changes: 0 additions & 6 deletions scripts/test.sh

This file was deleted.

4 changes: 1 addition & 3 deletions src/chess/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ impl Move {
}

impl fmt::Display for Move {
/// Serializes a move in [UCI format].
///
/// [UCI format]: http://wbec-ridderkerk.nl/html/UCIProtocol.html
/// Serializes a move in UCI format (used by [`pabi::uci`]).
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}{}", self.from, self.to)?;
if let Some(promotion) = self.promotion {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/chess/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use crate::chess::core::{
// TODO: Store Zobrist hash, possibly other info.
#[derive(Clone)]
pub struct Position {
board: Board,
pub(crate) board: Board,

Check warning on line 49 in src/chess/position.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, stable, false)

type `Board` is more private than the item `position::Position::board`

Check warning on line 49 in src/chess/position.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, stable, true)

type `Board` is more private than the item `position::Position::board`

Check warning on line 49 in src/chess/position.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, 1.78.0, false)

type `Board` is more private than the item `position::Position::board`

Check warning on line 49 in src/chess/position.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, nightly, true)

type `Board` is more private than the item `position::Position::board`

Check warning on line 49 in src/chess/position.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, nightly, true)

type `Board` is more private than the item `position::Position::board`

Check warning on line 49 in src/chess/position.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest, stable, true)

type `Board` is more private than the item `position::Position::board`

Check warning on line 49 in src/chess/position.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest, nightly, true)

type `Board` is more private than the item `position::Position::board`
castling: CastleRights,
side_to_move: Player,
/// [Halfmove Clock][^ply] keeps track of the number of (half-)moves
Expand Down
3 changes: 0 additions & 3 deletions src/engine.rs

This file was deleted.

File renamed without changes.
1 change: 0 additions & 1 deletion src/evaluation.rs

This file was deleted.

13 changes: 13 additions & 0 deletions src/evaluation/material.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use crate::chess::position::Position;

const PAWN_VALUE: f32 = 1.;

Check warning on line 3 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, stable, false)

constant `PAWN_VALUE` is never used

Check warning on line 3 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, stable, true)

constant `PAWN_VALUE` is never used

Check warning on line 3 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, 1.78.0, false)

constant `PAWN_VALUE` is never used

Check warning on line 3 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, nightly, true)

constant `PAWN_VALUE` is never used

Check warning on line 3 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, nightly, true)

constant `PAWN_VALUE` is never used

Check warning on line 3 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest, stable, true)

constant `PAWN_VALUE` is never used

Check warning on line 3 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest, nightly, true)

constant `PAWN_VALUE` is never used
const KNIGHT_VALUE: f32 = 3.;

Check warning on line 4 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, stable, false)

constant `KNIGHT_VALUE` is never used

Check warning on line 4 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, stable, true)

constant `KNIGHT_VALUE` is never used

Check warning on line 4 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, 1.78.0, false)

constant `KNIGHT_VALUE` is never used

Check warning on line 4 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, nightly, true)

constant `KNIGHT_VALUE` is never used

Check warning on line 4 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, nightly, true)

constant `KNIGHT_VALUE` is never used

Check warning on line 4 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest, stable, true)

constant `KNIGHT_VALUE` is never used

Check warning on line 4 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest, nightly, true)

constant `KNIGHT_VALUE` is never used
const BISHOP_VALUE: f32 = 3.;

Check warning on line 5 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, stable, false)

constant `BISHOP_VALUE` is never used

Check warning on line 5 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, stable, true)

constant `BISHOP_VALUE` is never used

Check warning on line 5 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, 1.78.0, false)

constant `BISHOP_VALUE` is never used

Check warning on line 5 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, nightly, true)

constant `BISHOP_VALUE` is never used

Check warning on line 5 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, nightly, true)

constant `BISHOP_VALUE` is never used

Check warning on line 5 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest, stable, true)

constant `BISHOP_VALUE` is never used

Check warning on line 5 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest, nightly, true)

constant `BISHOP_VALUE` is never used
const ROOK_VALUE: f32 = 5.;

Check warning on line 6 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, stable, false)

constant `ROOK_VALUE` is never used

Check warning on line 6 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, stable, true)

constant `ROOK_VALUE` is never used

Check warning on line 6 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, 1.78.0, false)

constant `ROOK_VALUE` is never used

Check warning on line 6 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, nightly, true)

constant `ROOK_VALUE` is never used

Check warning on line 6 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, nightly, true)

constant `ROOK_VALUE` is never used

Check warning on line 6 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest, stable, true)

constant `ROOK_VALUE` is never used

Check warning on line 6 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest, nightly, true)

constant `ROOK_VALUE` is never used
const QUEEN_VALUE: f32 = 9.;

Check warning on line 7 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, stable, false)

constant `QUEEN_VALUE` is never used

Check warning on line 7 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, stable, true)

constant `QUEEN_VALUE` is never used

Check warning on line 7 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, 1.78.0, false)

constant `QUEEN_VALUE` is never used

Check warning on line 7 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, nightly, true)

constant `QUEEN_VALUE` is never used

Check warning on line 7 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, nightly, true)

constant `QUEEN_VALUE` is never used

Check warning on line 7 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest, stable, true)

constant `QUEEN_VALUE` is never used

Check warning on line 7 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest, nightly, true)

constant `QUEEN_VALUE` is never used

fn material_advantage(position: &Position) -> f32 {

Check warning on line 9 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, stable, false)

unused variable: `position`

Check warning on line 9 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, stable, false)

function `material_advantage` is never used

Check warning on line 9 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, stable, true)

unused variable: `position`

Check warning on line 9 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, stable, true)

function `material_advantage` is never used

Check warning on line 9 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, 1.78.0, false)

unused variable: `position`

Check warning on line 9 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, 1.78.0, false)

function `material_advantage` is never used

Check warning on line 9 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, nightly, true)

unused variable: `position`

Check warning on line 9 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, nightly, true)

function `material_advantage` is never used

Check warning on line 9 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, nightly, true)

unused variable: `position`

Check warning on line 9 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest, nightly, true)

function `material_advantage` is never used

Check warning on line 9 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest, stable, true)

unused variable: `position`

Check warning on line 9 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest, stable, true)

function `material_advantage` is never used

Check warning on line 9 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest, nightly, true)

unused variable: `position`

Check warning on line 9 in src/evaluation/material.rs

View workflow job for this annotation

GitHub Actions / Test Suite (windows-latest, nightly, true)

function `material_advantage` is never used
0.
}

// TODO: Test.
1 change: 1 addition & 0 deletions src/evaluation/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod material;
29 changes: 0 additions & 29 deletions src/evaluation/trivial.rs

This file was deleted.

1 change: 1 addition & 0 deletions src/interface/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod uci;
8 changes: 4 additions & 4 deletions src/uci.rs → src/interface/uci.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Implementation of [Universal Chess Interface] (UCI) protocol for communication
//! between the engine and the UCI client (e.g. GUI, tournament runner).
//! Implementation of [Universal Chess Interface] (UCI) protocol for
//! communication between the engine and the UCI client (e.g. GUI, tournament
//! runner).
//!
//! The implementation here does not aim to be complete and exhaustive, because
//! the main goal is to make the engine work in relatively simple setups, making
Expand All @@ -14,7 +15,6 @@ use crate::VERSION;

/// Reads UCI commands from the input stream and executes them accordingly while
/// writing the responses to the output stream.
///
// TODO: Document the expected behavior.
// > The engine must always be able to process input from stdin, even while
// > thinking.
Expand Down Expand Up @@ -128,7 +128,7 @@ pub fn run_loop(input: &mut impl BufRead, output: &mut impl Write) {
// This is practically no-op for now, maybe always. Not sure
// what should change when the new game is started.
},
// position [fen <fenstring> | startpos ] moves <move1> .... <movei>
// position [fen <fenstring> | startpos ] moves <move1> .... <move_i>
//
// Set up the position described in `fenstring` on the internal board and
// play the moves on the internal chess board.
Expand Down
Loading

0 comments on commit 5fdba74

Please sign in to comment.