Skip to content

Commit

Permalink
General cleanups (liamt19#46)
Browse files Browse the repository at this point in the history
bench: 7141099
  • Loading branch information
liamt19 authored Jun 22, 2024
1 parent 01311a4 commit db99487
Show file tree
Hide file tree
Showing 18 changed files with 136 additions and 412 deletions.
61 changes: 13 additions & 48 deletions Logic/Core/StateInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,55 +15,20 @@ namespace Lizard.Logic.Core
public unsafe struct StateInfo
{
public static readonly nuint StateCopySize = (nuint)(sizeof(StateInfo) - sizeof(Accumulator*));
static StateInfo()
{
int accOffset = ((FieldOffsetAttribute)typeof(StateInfo).GetField("Accumulator").GetCustomAttributes(typeof(FieldOffsetAttribute), true)[0]).Value;
Assert(accOffset == (int)StateCopySize,
$"StateInfo's Accumulator pointer is {accOffset} / {sizeof(StateInfo)}, should be {StateCopySize}");
}

[FieldOffset(0)]
public fixed ulong CheckSquares[PieceNB];

[FieldOffset(48)]
public fixed int KingSquares[2];

[FieldOffset(56)]
public fixed ulong BlockingPieces[2];

[FieldOffset(72)]
public fixed ulong Pinners[2];

[FieldOffset(88)]
public ulong Hash = 0;

[FieldOffset(96)]
public ulong Checkers = 0;

[FieldOffset(104)]
public CastlingStatus CastleStatus = CastlingStatus.None;

/// <summary>
/// The first number in the FEN, which starts at 0 and resets to 0 every time a pawn moves or a piece is captured.
/// If this reaches 100, the game is a draw by the 50-move rule.
/// </summary>
[FieldOffset(108)]
public int HalfmoveClock = 0;

[FieldOffset(112)]
public int EPSquare = EPNone;

[FieldOffset(116)]
public int CapturedPiece = None;

[FieldOffset(120)]
public int PliesFromNull = 0;

[FieldOffset(124)]
private int _align = 0;

[FieldOffset(128)]
public Accumulator* Accumulator;
[FieldOffset( 0)] public fixed ulong CheckSquares[PieceNB];
[FieldOffset( 48)] public fixed ulong BlockingPieces[2];
[FieldOffset( 64)] public fixed ulong Pinners[2];
[FieldOffset( 80)] public fixed int KingSquares[2];
[FieldOffset( 88)] public ulong Hash = 0;
[FieldOffset( 96)] public ulong Checkers = 0;
[FieldOffset(104)] public int HalfmoveClock = 0;
[FieldOffset(108)] public int EPSquare = EPNone;
[FieldOffset(112)] public int CapturedPiece = None;
[FieldOffset(116)] public int PliesFromNull = 0;
[FieldOffset(120)] public CastlingStatus CastleStatus = CastlingStatus.None;
[FieldOffset(124)] private fixed byte _pad0[4];
[FieldOffset(128)] public Accumulator* Accumulator;

public StateInfo()
{
Expand Down
13 changes: 13 additions & 0 deletions Logic/Data/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ public static class Files
public const int H = 7;
}

public static class Direction
{
public const int NORTH = 8;
public const int EAST = 1;
public const int SOUTH = -NORTH;
public const int WEST = -EAST;

public const int NORTH_EAST = NORTH + EAST;
public const int SOUTH_EAST = SOUTH + EAST;
public const int SOUTH_WEST = SOUTH + WEST;
public const int NORTH_WEST = NORTH + WEST;
}

public static class Squares
{
public const int A1 = 0;
Expand Down
7 changes: 6 additions & 1 deletion Logic/Data/Move.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
using System.Text;

#pragma warning disable CS0660 // Type defines operator == or operator != but does not override Object.Equals(object o)
#pragma warning disable CS0661 // Type defines operator == or operator != but does not override Object.GetHashCode()

using System.Text;

namespace Lizard.Logic.Data
{


public unsafe struct Move
{
public static readonly Move Null = new Move();
Expand Down
6 changes: 0 additions & 6 deletions Logic/NN/Accumulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,8 @@ namespace Lizard.Logic.NN
/// </summary>
public unsafe struct Accumulator
{
// Intellisense incorrectly marks "var accumulation = accumulator[perspectives[p]]" in FeatureTransformer.TransformFeatures
// as an error when this uses a primary constructor with "size" as a parameter.
// https://github.com/dotnet/roslyn/issues/69663

public const int ByteSize = Bucketed768.HiddenSize * sizeof(short);

public static int VectorCount => Bucketed768.HiddenSize / VSize.Short;

public readonly Vector256<short>* White;
public readonly Vector256<short>* Black;

Expand Down
20 changes: 8 additions & 12 deletions Logic/NN/Bucketed768.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace Lizard.Logic.NN
{
[SkipStaticConstructor]
public static unsafe partial class Bucketed768
{
public const int InputBuckets = 5;
Expand All @@ -18,19 +17,14 @@ public static unsafe partial class Bucketed768

public const int QA = 255;
public const int QB = 64;
private const int QAB = QA * QB;

public const int OutputScale = 400;

public static readonly int SIMD_CHUNKS_512 = HiddenSize / Vector512<short>.Count;
public static readonly int SIMD_CHUNKS_256 = HiddenSize / Vector256<short>.Count;

/// <summary>
/// (768x5 -> 1536)x2 -> 8
/// </summary>
public const string NetworkName = "L1536x5x8_g75_s20-550.bin";


public static readonly short* FeatureWeights;
public static readonly short* FeatureBiases;
public static readonly short* LayerWeights;
Expand All @@ -42,7 +36,7 @@ public static unsafe partial class Bucketed768
private const int LayerWeightElements = HiddenSize * 2 * OutputBuckets;
private const int LayerBiasElements = OutputBuckets;

public static long ExpectedNetworkSize => (FeatureWeightElements + FeatureBiasElements + LayerWeightElements + LayerBiasElements) * sizeof(short);
private const long ExpectedNetworkSize = (FeatureWeightElements + FeatureBiasElements + LayerWeightElements + LayerBiasElements) * sizeof(short);

private static ReadOnlySpan<int> KingBuckets =>
[
Expand All @@ -64,7 +58,7 @@ static Bucketed768()
FeatureBiases = (short*)AlignedAllocZeroed(sizeof(short) * FeatureBiasElements);

LayerWeights = (short*)AlignedAllocZeroed(sizeof(short) * LayerWeightElements);
LayerBiases = (short*)AlignedAllocZeroed(sizeof(short) * (nuint)Math.Max(LayerBiasElements, VSize.Short));
LayerBiases = (short*)AlignedAllocZeroed(sizeof(short) * (nuint)Math.Max(LayerBiasElements, Vector512<short>.Count));

string networkToLoad = NetworkName;

Expand All @@ -88,7 +82,7 @@ public static void Initialize(string networkToLoad, bool exitIfFail = true)
if (stream.Position + toRead > stream.Length)
{
Console.WriteLine("Bucketed768's BinaryReader doesn't have enough data for all weights and biases to be read!");
Console.WriteLine("It expects to read " + toRead + " bytes, but the stream's position is " + stream.Position + "/" + stream.Length);
Console.WriteLine($"It expects to read {toRead} bytes, but the stream's position is {stream.Position} / {stream.Length}");
Console.WriteLine("The file being loaded is either not a valid 768 network, or has different layer sizes than the hardcoded ones.");
if (exitIfFail)
{
Expand Down Expand Up @@ -242,6 +236,8 @@ public static int GetEvaluation(Position pos)
Vector256<short> zeroVec = Vector256<short>.Zero;
Vector256<int> sum = Vector256<int>.Zero;

int SimdChunks = HiddenSize / Vector256<short>.Count;

// Formula from BlackMarlin
int occ = (int)popcount(pos.bb.Occupancy);
int outputBucket = Math.Min((63 - occ) * (32 - occ) / 225, 7);
Expand All @@ -251,7 +247,7 @@ public static int GetEvaluation(Position pos)
var ourWeights = (Vector256<short>*)(LayerWeights + (outputBucket * (HiddenSize * 2)));
var theirWeights = (Vector256<short>*)(LayerWeights + (outputBucket * (HiddenSize * 2)) + HiddenSize);

for (int i = 0; i < SIMD_CHUNKS_256; i++)
for (int i = 0; i < SimdChunks; i++)
{
Vector256<short> clamp = Vector256.Min(maxVec, Vector256.Max(zeroVec, ourData[i]));
Vector256<short> mult = clamp * ourWeights[i];
Expand All @@ -262,7 +258,7 @@ public static int GetEvaluation(Position pos)
sum = Vector256.Add(sum, Vector256.Add(loMult * loClamp, hiMult * hiClamp));
}

for (int i = 0; i < SIMD_CHUNKS_256; i++)
for (int i = 0; i < SimdChunks; i++)
{
Vector256<short> clamp = Vector256.Min(maxVec, Vector256.Max(zeroVec, theirData[i]));
Vector256<short> mult = clamp * theirWeights[i];
Expand All @@ -275,7 +271,7 @@ public static int GetEvaluation(Position pos)

int output = Vector256.Sum(sum);

return (output / QA + LayerBiases[outputBucket]) * OutputScale / QAB;
return (output / QA + LayerBiases[outputBucket]) * OutputScale / (QA * QB);
}

private static int FeatureIndexSingle(int pc, int pt, int sq, int kingSq, int perspective)
Expand Down
3 changes: 2 additions & 1 deletion Logic/NN/Bucketed768Unroll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using VShort = System.Runtime.Intrinsics.Vector256<short>;
#endif

#pragma warning disable CS0162 // Unreachable code detected

namespace Lizard.Logic.NN
{
Expand Down Expand Up @@ -501,7 +502,7 @@ public static int GetEvaluationUnrolled512(Position pos)

int output = NNUE.SumVectorNoHadd(sumVec);

return (output / QA + LayerBiases[outputBucket]) * 400 / (QA * QB);
return (output / QA + LayerBiases[outputBucket]) * OutputScale / (QA * QB);
}

}
Expand Down
4 changes: 3 additions & 1 deletion Logic/NN/FunUnrollThings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using VectorT = System.Runtime.Intrinsics.Vector256;
#endif

#pragma warning disable CS0162 // Unreachable code detected

namespace Lizard.Logic.NN
{
public static unsafe class FunUnrollThings
Expand Down Expand Up @@ -593,4 +595,4 @@ public static void UnrollSubtract(short* src, short* dst, short* sub1)
}

}
}
}
29 changes: 0 additions & 29 deletions Logic/NN/VSize.cs

This file was deleted.

5 changes: 4 additions & 1 deletion Logic/Search/SearchInformation.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace Lizard.Logic.Search

#pragma warning disable CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

namespace Lizard.Logic.Search
{
public struct SearchInformation
{
Expand Down
Loading

0 comments on commit db99487

Please sign in to comment.