Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions .github/workflows/dotnet-quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,28 @@ jobs:
with:
global-json-file: global.json

- name: Restore
run: dotnet restore src/Sapling.sln
- name: Restore test projects
run: |
dotnet restore src/Sapling.Engine.Tests/Sapling.Engine.Tests.csproj
dotnet restore src/Sapling.WebHost.Tests/Sapling.WebHost.Tests.csproj

- name: Test with coverage
- name: Test engine and UCI with coverage
run: >-
dotnet test src/Sapling.sln
dotnet test src/Sapling.Engine.Tests/Sapling.Engine.Tests.csproj
--no-restore
--configuration Release
--collect:"XPlat Code Coverage"
--settings .github/coverlet.runsettings
--results-directory artifacts/TestResults
--results-directory artifacts/TestResults/engine

- name: Test web host with coverage
run: >-
dotnet test src/Sapling.WebHost.Tests/Sapling.WebHost.Tests.csproj
--no-restore
--configuration Release
--collect:"XPlat Code Coverage"
--settings .github/coverlet.runsettings
--results-directory artifacts/TestResults/webhost

- name: Upload Cobertura coverage reports
if: always()
Expand Down
13 changes: 11 additions & 2 deletions .github/workflows/release-pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,27 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4

- name: Require the default branch for releases
env:
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
run: |
if [[ "$GITHUB_REF" != "refs/heads/$DEFAULT_BRANCH" ]]; then
echo "Release workflow must be dispatched from the default branch ($DEFAULT_BRANCH)." >&2
exit 1
fi

- name: Setup .NET Core
uses: actions/setup-dotnet@v4
with:
global-json-file: global.json

# Get the version from the .csproj file
# Read the release version from shared Directory.Build.props via MSBuild.
- name: Get .NET application version
working-directory: src/Sapling
id: get_version
run: |
VERSION=$(dotnet msbuild Sapling.csproj -getProperty:Version)
# Assembly-style 2.0.0.0 maps to the release tag Sapling-2.0.0.
# The three-part application version maps directly to the release tag.
if [[ "$VERSION" =~ ^([0-9]+\.[0-9]+\.[0-9]+)\.0$ ]]; then
VERSION="${BASH_REMATCH[1]}"
fi
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
</div>
</p>

### Sapling v2.0.0 — a .NET UCI chess engine
### Sapling v2.1.0 — a .NET UCI chess engine

Play it here -> https://timmoth.com/sapling/play/

Expand Down
18 changes: 18 additions & 0 deletions Scripts/test_web_worker.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ assert.equal(hintsDone.hintId, 12);
const hintUpdates = backlog.filter((message) => message.type === 'hint-progress' && message.hintId === 12);
assert.ok(hintUpdates.some((message) => message.scores.some((score) => score.move === 'e2e4')));
assert.ok(hintUpdates.some((message) => message.scores.some((score) => score.move === 'd2d4')));
for (const update of hintUpdates) {
for (const score of update.scores) {
assert.ok(Number.isFinite(score.lossCp) && score.lossCp >= 0, 'hint supplies global move-quality loss');
}
}


const mateHintTask = self.onmessage({ data: {
type: 'hints', hintId: 13, fen: '7k/5Q2/6K1/8/8/8/8/8 w - - 0 1',
moves: ['f7g7'], history: [],
} });
await waitFor('hints-done', (message) => message.hintId === 13);
await mateHintTask;
const mateHint = backlog.filter((message) => message.type === 'hint-progress' && message.hintId === 13)
.flatMap((message) => message.scores).find((score) => score.move === 'f7g7');
assert.equal(mateHint.scoreType, 'mate');
assert.equal(mateHint.score, 0, 'a move delivering checkmate retains the resulting-position M0 distance');
assert.equal(mateHint.lossCp, 0);

const interruptedSearch = self.onmessage({ data: {
type: 'search', requestId: 20, maxDepth: 120, sliceNodes: 12_000,
Expand Down
6 changes: 3 additions & 3 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project>
<!-- Shared release identity for the CLI, engine package and supporting projects. -->
<PropertyGroup>
<Version>2.0.0</Version>
<Version>2.1.0</Version>
<PackageVersion>$(Version)</PackageVersion>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<FileVersion>2.0.0.0</FileVersion>
<AssemblyVersion>2.1.0</AssemblyVersion>
<FileVersion>2.1.0</FileVersion>
</PropertyGroup>
<!-- Append build symbols without preventing project-specific constants from being added.
Separate multiple symbols with %3B on the command line. -->
Expand Down
6 changes: 3 additions & 3 deletions src/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
### Sapling v2.0.0 — a .NET UCI chess engine
### Sapling v2.1.0 — a .NET UCI chess engine

Play it here -> https://iblunder.com/

Expand All @@ -7,9 +7,9 @@ Or challenge it on Lichess -> https://lichess.org/@/sapling-bot
## Releases
You can browse all windows, linux or mac releases [here](https://github.com/Timmoth/Sapling/releases)

## v2.0.0
## v2.1.0

The planned release tag is `Sapling-2.0.0`. Version metadata is shared in [`Directory.Build.props`](Directory.Build.props); see the [release workflow](../.github/workflows/release-pipeline.yaml).
The planned release tag is `Sapling-2.1.0`. Version metadata is shared in [`Directory.Build.props`](Directory.Build.props); see the [release workflow](../.github/workflows/release-pipeline.yaml).

## Requirements
- Use an executable matching your OS and architecture. The normal build selects AVX2, Vector128 or scalar NNUE at runtime and uses magic bitboards when BMI2/PEXT is unavailable. Dedicated AVX512 builds select that backend when supported; explicitly forcing `SAPLING_BACKEND=Avx512` requires AVX512F and AVX512BW.
Expand Down
3 changes: 2 additions & 1 deletion src/Sapling.Engine.Tests/AllocationHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,12 @@ public void CalculateTranspositionTableSize_AbsurdSizes_Throw()
[Fact]
public void CalculateSizeInMb_RoundTripsKnownSizes()
{
foreach (var mb in new[] { 64L, 256, 1024 })
foreach (var mb in new[] { 16L, 32, 64, 256, 1024 })
{
var count = TranspositionTableExtensions.CalculateTranspositionTableSize(mb);
var inverted = TranspositionTableExtensions.CalculateSizeInMb((uint)count);
Assert.Equal(count, TranspositionTableExtensions.CalculateTranspositionTableSize(inverted));
Assert.Equal(mb, inverted);
}
}

Expand Down
147 changes: 144 additions & 3 deletions src/Sapling.Engine.Tests/HumanPlayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public void EmbeddedPolicy_MatchesPyTorchGoldenMoveDistributions()
using var fixture = JsonDocument.Parse(File.ReadAllText(
Path.Combine(AppContext.BaseDirectory, "human_policy_inference_golden.json")));
Assert.Equal(1, fixture.RootElement.GetProperty("schema_version").GetInt32());
Assert.Equal(fixture.RootElement.GetProperty("model_sha256").GetString(), HumanMovePolicy.ModelSha256);

foreach (var testCase in fixture.RootElement.GetProperty("cases").EnumerateArray())
{
Expand Down Expand Up @@ -47,6 +48,59 @@ public void EmbeddedPolicy_MatchesPyTorchGoldenMoveDistributions()
}
}

[Fact]
public void EmbeddedPolicy_MultiRatingScoringMatchesSingleRatingScoring()
{
using var state = GameState.InitialState();
var board = state.Board;
var boardFeatures = HumanPolicyEncoding.EncodeBoard(ref board);
var legalMoves = new List<uint>();
board.GenerateLegalMoves(legalMoves, captureOnly: false);
var moveFeatures = new float[legalMoves.Count * HumanPolicyEncoding.MoveFeatures];
var ratings = new[] { 500, 800, 1500, 2200 };
var multiRatingLogits = HumanMovePolicy.ScoreCandidatesForRatings(
boardFeatures, moveFeatures, legalMoves.Count, ratings);

Assert.Equal(ratings.Length, multiRatingLogits.Length);
for (var i = 0; i < ratings.Length; i++)
Assert.Equal(HumanMovePolicy.ScoreCandidates(boardFeatures, moveFeatures,
legalMoves.Count, ratings[i]), multiRatingLogits[i]);
Assert.Throws<ArgumentException>(() => HumanMovePolicy.ScoreCandidatesForRatings(
boardFeatures, moveFeatures, legalMoves.Count, Array.Empty<int>()));
Assert.Throws<ArgumentOutOfRangeException>(() => HumanMovePolicy.ScoreCandidatesForRatings(
boardFeatures, moveFeatures, legalMoves.Count, new[] { 499 }));
}

[Fact]
public void HumanPlayCalibration_AppliesStaticDepthEightHumanDataCalibration()
{
Assert.True(HumanPlayCalibration.AppliesToCurrentModel);
Assert.Equal(new HumanPlaySettings(500, 1f, .5f), HumanPlayCalibration.ForDisplayElo(500));
Assert.Equal(new HumanPlaySettings(875, 1f, .6875f), HumanPlayCalibration.ForDisplayElo(800));
Assert.Equal(new HumanPlaySettings(1000, 1f, .75f), HumanPlayCalibration.ForDisplayElo(900));
Assert.Equal(new HumanPlaySettings(1088, 1f, .75f), HumanPlayCalibration.ForDisplayElo(1000));
Assert.Equal(new HumanPlaySettings(1500, 1f, .625f), HumanPlayCalibration.ForDisplayElo(1500));
Assert.Equal(new HumanPlaySettings(1700, 1f, .375f), HumanPlayCalibration.ForDisplayElo(1700));
Assert.Equal(new HumanPlaySettings(1750, 1.0125f, .5625f), HumanPlayCalibration.ForDisplayElo(1800));
Assert.Equal(new HumanPlaySettings(2050, 1.05f, .75f), HumanPlayCalibration.ForDisplayElo(2200));
Assert.Equal(new HumanPlaySettings(0, 1f, 0f), HumanPlayCalibration.ForDisplayElo(0));
}

[Theory]
[InlineData("6rk/8/8/8/8/8/4R3/K7 w - - 0 1", "e2g2", true)]
[InlineData("6rk/8/8/8/8/8/4R3/5K2 w - - 0 1", "e2g2", false)]
public void DirectCaptureRisk_RequiresLegalCaptureWithoutImmediateRecapture(string fen, string uci, bool expected)
{
var board = BoardStateExtensions.CreateBoardFromFen(fen);
var originalHash = board.Hash;
var legalMoves = new List<uint>();
board.GenerateLegalMoves(legalMoves, captureOnly: false);
var move = Assert.Single(legalMoves, candidate => candidate.ToUciMoveName() == uci);

Assert.Equal(expected, DirectCaptureRisk.IsRiskAfterMove(ref board, move));
Assert.Equal(originalHash, board.Hash);
}

[Fact]
public void DisabledHumanPlay_LeavesEngineBestMoveSelected()
{
Expand All @@ -60,6 +114,24 @@ public void DisabledHumanPlay_LeavesEngineBestMoveSelected()
Assert.Equal(1f, probability);
}

[Fact]
public void SelectMove_PreservesThePolicyChoiceWhenItIsRankZero()
{
HumanPlay.Reset();
HumanPlay.SetElo(1500);
using var state = GameState.InitialState();
using var searcher = new ParallelSearcher(1024);
var ranked = searcher.RankRootMoves(state, 2);
var board = state.Board;

var selection = HumanPlay.SelectMove(new ZeroRandom(), ref board, ranked);

Assert.Equal(0, selection.Rank);
Assert.Equal(ranked[0].Move, selection.Move);
Assert.True(selection.Probability > 0f);
HumanPlay.Reset();
}

[Fact]
public void EmbeddedPolicySampler_AlwaysSelectsARankedLegalMove()
{
Expand Down Expand Up @@ -124,6 +196,63 @@ public void EmbeddedPolicy_UsesRatingConditioning()
.Sum(pair => Math.Abs(pair.First - pair.Second)) > 1e-6f);
}

[Fact]
public void PolicyProbabilities_TemperatureChangesEntropyAndStayNormalized()
{
var logits = new float[] { 2f, 1f, -1f };
var cool = HumanMovePolicy.Probabilities(logits, 0.5f);
var baseLine = HumanMovePolicy.Probabilities(logits);
var warm = HumanMovePolicy.Probabilities(logits, 2f);

Assert.InRange(cool.Sum(), 0.99999f, 1.00001f);
Assert.InRange(baseLine.Sum(), 0.99999f, 1.00001f);
Assert.InRange(warm.Sum(), 0.99999f, 1.00001f);
Assert.True(cool[0] > baseLine[0]);
Assert.True(baseLine[0] > warm[0]);
}

[Fact]
public void PolicySampler_SoftPenaltyReducesMarkedCandidateProbability()
{
var board = BoardStateExtensions.CreateBoardFromFen(Constants.InitialState);
var boardFeatures = HumanPolicyEncoding.EncodeBoard(ref board);
var moves = new List<uint>();
board.GenerateLegalMoves(moves, captureOnly: false);
var moveFeatures = new float[moves.Count * HumanPolicyEncoding.MoveFeatures];
var penaltyMask = new bool[moves.Count];
penaltyMask[0] = true;

var baselineIndex = HumanMovePolicy.Sample(new ZeroRandom(), boardFeatures, moveFeatures,
moves.Count, 1500, 1f, out var baselineProbability);
var penalizedIndex = HumanMovePolicy.Sample(new ZeroRandom(), boardFeatures, moveFeatures,
moves.Count, 1500, 1f, out var penalizedProbability, penaltyMask, 1f);

Assert.Equal(0, baselineIndex);
Assert.Equal(0, penalizedIndex);
Assert.True(penalizedProbability < baselineProbability);
}

[Theory]
[InlineData(0f)]
[InlineData(-1f)]
[InlineData(float.NaN)]
[InlineData(float.PositiveInfinity)]
public void PolicyProbabilities_RejectInvalidTemperature(float temperature)
{
Assert.Throws<ArgumentOutOfRangeException>(() => HumanMovePolicy.Probabilities(new float[] { 1f, 0f }, temperature));
}

[Fact]
public void HumanPlay_ResetRestoresDefaultTemperature()
{
HumanPlay.SetTemperature(1.75f);
Assert.Equal(1.75f, HumanPlay.Temperature);

HumanPlay.Reset();

Assert.Equal(1f, HumanPlay.Temperature);
}

[Fact]
public void EmbeddedPolicySampler_IsSeededAndReturnsSelectedSoftmaxProbability()
{
Expand All @@ -146,6 +275,13 @@ public void EmbeddedPolicySampler_IsSeededAndReturnsSelectedSoftmaxProbability()
Assert.Equal(repeated, selected);
Assert.Equal(repeatedProbability, probability, 1e-7f);
Assert.Equal(expectedProbabilities[selected], probability, 1e-6f);

const float temperature = 1.6f;
var temperatureProbabilities = HumanMovePolicy.Probabilities(
HumanMovePolicy.ScoreCandidates(boardFeatures, moveFeatures, legalMoves.Count, 1500), temperature);
var temperatureSelected = HumanMovePolicy.Sample(new Random(8128), boardFeatures, moveFeatures,
legalMoves.Count, 1500, temperature, out var temperatureProbability);
Assert.Equal(temperatureProbabilities[temperatureSelected], temperatureProbability, 1e-6f);
}

[Fact]
Expand All @@ -156,7 +292,7 @@ public void EmbeddedPolicy_RejectsMalformedTensorShapesAndOutOfRangeRatings()

var shortBoard = () => HumanMovePolicy.ScoreCandidates(board.AsSpan(1), moves, 1, 1500);
var wrongMoveCount = () => HumanMovePolicy.ScoreCandidates(board, moves, 2, 1500);
var invalidRating = () => HumanMovePolicy.ScoreCandidates(board, moves, 1, 799);
var invalidRating = () => HumanMovePolicy.ScoreCandidates(board, moves, 1, 499);

Assert.Throws<ArgumentException>(shortBoard);
Assert.Throws<ArgumentException>(wrongMoveCount);
Expand All @@ -166,8 +302,8 @@ public void EmbeddedPolicy_RejectsMalformedTensorShapesAndOutOfRangeRatings()
[Theory]
[InlineData(-100, 0)]
[InlineData(0, 0)]
[InlineData(1, 800)]
[InlineData(300, 800)]
[InlineData(1, 500)]
[InlineData(300, 500)]
[InlineData(1500, 1500)]
[InlineData(3000, 2200)]
public void HumanElo_MapsToTheTrainedPolicyRange(int requested, int expected)
Expand Down Expand Up @@ -203,4 +339,9 @@ private static float[] Softmax(float[] logits)
var sum = values.Sum();
return values.Select(value => (float)(value / sum)).ToArray();
}

private sealed class ZeroRandom : Random
{
public override double NextDouble() => 0d;
}
}
3 changes: 2 additions & 1 deletion src/Sapling.Engine.Tests/HumanPolicyEncodingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public void Encoders_MatchSharedGoldenFixtures()
}

[Theory]
[InlineData(500, -1.4285715f)]
[InlineData(800, -1f)]
[InlineData(1500, 0f)]
[InlineData(2200, 1f)]
Expand All @@ -75,7 +76,7 @@ public void RatingEncoder_UsesSpecifiedScale(int rating, float expected)
}

[Theory]
[InlineData(799)]
[InlineData(499)]
[InlineData(2201)]
public void RatingEncoder_RejectsOutOfRangeValues(int rating)
{
Expand Down
21 changes: 21 additions & 0 deletions src/Sapling.Engine.Tests/TimeManagementTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ public void FlaggedClock_GetsSmallFiniteBudget()
Assert.True(budget.HardLimitMs >= budget.SoftLimitMs);
}

[Theory]
[InlineData(100, 1, 4)]
[InlineData(200, 10, 40)]
[InlineData(1_000, 23, 92)]
public void LowBulletClock_LeavesLargerResponseReserve(int timeMs, int softMs, int hardMs)
{
var budget = TimeManagement.Calculate(timeMs, 0, 0, 20, true);

Assert.Equal(softMs, budget.SoftLimitMs);
Assert.Equal(hardMs, budget.HardLimitMs);
}

[Theory]
[InlineData(10_000, 0, 0)]
[InlineData(10_000, 100, 10)]
Expand All @@ -55,6 +67,15 @@ public void ExplicitMovesToGo_AllocatesMoreThanEmergencyClock()
Assert.True(fiveMoves.SoftLimitMs > thirtyMoves.SoftLimitMs);
}

[Fact]
public void HardLimit_IsBoundedByFourSoftBudgetsOnLongClock()
{
var budget = TimeManagement.Calculate(287_710, 2_000, 0, 98, true);

Assert.Equal(10_706, budget.SoftLimitMs);
Assert.Equal(42_824, budget.HardLimitMs);
}

[Fact]
public void SoftLimitAdjustment_SpendsLessOnStableMoves()
{
Expand Down
Loading
Loading