Skip to content

Commit

Permalink
Merge remote-tracking branch 'fairy/master' into nnue
Browse files Browse the repository at this point in the history
  • Loading branch information
ianfab committed Mar 18, 2024
2 parents b02c911 + 62577a4 commit 32c755c
Show file tree
Hide file tree
Showing 25 changed files with 1,058 additions and 320 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ jobs:
- uses: actions/setup-python@v3

- name: Install cibuildwheel
run: python -m pip install cibuildwheel==2.11.2
run: python -m pip install cibuildwheel==2.16.2

- name: Build wheels
run: python -m cibuildwheel --output-dir wheelhouse
# to supply options, put them in 'env', like:
env:
MACOSX_DEPLOYMENT_TARGET: "10.14"
CIBW_ARCHS_MACOS: "x86_64 arm64"
CIBW_SKIP: "pp* *-win32 *-manylinux_i686 *-musllinux_* cp36-*"
CIBW_SKIP: "pp* *-win32 *-manylinux_i686 *-musllinux_* cp36-* cp37-*"
CIBW_TEST_COMMAND: python {project}/test.py

- uses: actions/upload-artifact@v3
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ build_script:
$dummy = $nnuenet -match "(?<nnuenet>nn-[a-z0-9]{12}.nnue)"
$nnuenet = $Matches.nnuenet
Write-Host "Default net:" $nnuenet
$nnuedownloadurl = "https://tests.stockfishchess.org/api/nn/$nnuenet"
$nnuedownloadurl = "https://github.com/official-stockfish/networks/raw/master/$nnuenet"
$nnuefilepath = "src\${env:CONFIGURATION}\$nnuenet"
if (Test-Path -Path $nnuefilepath) {
Write-Host "Already available."
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
sources=sources,
extra_compile_args=args)

setup(name="pyffish", version="0.0.78",
setup(name="pyffish", version="0.0.81",
description="Fairy-Stockfish Python wrapper",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
45 changes: 29 additions & 16 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,10 @@ ifeq ($(optimize),yes)
endif

ifeq ($(comp),clang)
CXXFLAGS += -fexperimental-new-pass-manager
clangmajorversion = $(shell $(CXX) -dumpversion 2>/dev/null | cut -f1 -d.)
ifeq ($(shell expr $(clangmajorversion) \< 16),1)
CXXFLAGS += -fexperimental-new-pass-manager
endif
endif
endif

Expand Down Expand Up @@ -825,25 +828,35 @@ clean: objclean profileclean
net:
$(eval nnuenet := $(shell grep EvalFileDefaultName evaluate.h | grep define | sed 's/.*\(nn-[a-z0-9]\{12\}.nnue\).*/\1/'))
@echo "Default net: $(nnuenet)"
$(eval nnuedownloadurl := https://tests.stockfishchess.org/api/nn/$(nnuenet))
$(eval nnuedownloadurl1 := https://tests.stockfishchess.org/api/nn/$(nnuenet))
$(eval nnuedownloadurl2 := https://github.com/official-stockfish/networks/raw/master/$(nnuenet))
$(eval curl_or_wget := $(shell if hash curl 2>/dev/null; then echo "curl -skL"; elif hash wget 2>/dev/null; then echo "wget -qO-"; fi))
@if test -f "$(nnuenet)"; then \
echo "Already available."; \
else \
if [ "x$(curl_or_wget)" = "x" ]; then \
echo "Automatic download failed: neither curl nor wget is installed. Install one of these tools or download the net manually"; exit 1; \
else \
echo "Downloading $(nnuedownloadurl)"; $(curl_or_wget) $(nnuedownloadurl) > $(nnuenet);\
fi; \
fi;
@if [ "x$(curl_or_wget)" = "x" ]; then \
echo "Automatic download failed: neither curl nor wget is installed. Install one of these tools or download the net manually"; exit 1; \
fi
$(eval shasum_command := $(shell if hash shasum 2>/dev/null; then echo "shasum -a 256 "; elif hash sha256sum 2>/dev/null; then echo "sha256sum "; fi))
@if [ "x$(shasum_command)" != "x" ]; then \
if [ "$(nnuenet)" != "nn-"`$(shasum_command) $(nnuenet) | cut -c1-12`".nnue" ]; then \
echo "Failed download or $(nnuenet) corrupted, please delete!"; exit 1; \
fi \
else \
@if [ "x$(shasum_command)" = "x" ]; then \
echo "shasum / sha256sum not found, skipping net validation"; \
fi
@for nnuedownloadurl in "$(nnuedownloadurl1)" "$(nnuedownloadurl2)"; do \
if test -f "$(nnuenet)"; then \
echo "$(nnuenet) available."; \
else \
if [ "x$(curl_or_wget)" != "x" ]; then \
echo "Downloading $${nnuedownloadurl}"; $(curl_or_wget) $${nnuedownloadurl} > $(nnuenet);\
fi; \
fi; \
if [ "x$(shasum_command)" != "x" ]; then \
if [ "$(nnuenet)" != "nn-"`$(shasum_command) $(nnuenet) | cut -c1-12`".nnue" ]; then \
echo "Removing failed download"; rm -f $(nnuenet); \
else \
echo "Network validated"; break; \
fi; \
fi; \
done
@if ! test -f "$(nnuenet)"; then \
echo "Failed to download $(nnuenet)."; \
fi

# clean binaries and objects
objclean:
Expand Down
14 changes: 12 additions & 2 deletions src/apiutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ inline Disambiguation disambiguation_level(const Position& pos, Move m, Notation
return SQUARE_DISAMBIGUATION;
}

// A disambiguation occurs if we have more then one piece of type 'pt'
// A disambiguation occurs if we have more than one piece of type 'pt'
// that can reach 'to' with a legal move.
Bitboard b = pos.pieces(us, pt) ^ from;
Bitboard others = 0;
Expand Down Expand Up @@ -346,7 +346,7 @@ inline const std::string move_to_san(Position& pos, Move m, Notation n) {
}

// Wall square
if (pos.wall_gating())
if (pos.walling())
san += "," + square(pos, gating_square(m), n);

// Check and checkmate
Expand Down Expand Up @@ -821,6 +821,16 @@ inline Validation check_number_of_kings(const std::string& fenBoard, const std::
int nbWhiteKingsStart = piece_count(startFenBoard, WHITE, KING, v);
int nbBlackKingsStart = piece_count(startFenBoard, BLACK, KING, v);

if (nbWhiteKings > 1)
{
std::cerr << "Invalid number of white kings. Maximum: 1. Given: " << nbWhiteKings << std::endl;
return NOK;
}
if (nbBlackKings > 1)
{
std::cerr << "Invalid number of black kings. Maximum: 1. Given: " << nbBlackKings << std::endl;
return NOK;
}
if (nbWhiteKings != nbWhiteKingsStart)
{
std::cerr << "Invalid number of white kings. Expected: " << nbWhiteKingsStart << ". Given: " << nbWhiteKings << std::endl;
Expand Down
14 changes: 8 additions & 6 deletions src/bitboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ namespace {
// Some magics need to be split in order to reduce memory consumption.
// Otherwise on a 12x10 board they can be >100 MB.
#ifdef LARGEBOARDS
Bitboard RookTableH[0x11800]; // To store horizontalrook attacks
Bitboard RookTableH[0x11800]; // To store horizontal rook attacks
Bitboard RookTableV[0x4800]; // To store vertical rook attacks
Bitboard BishopTable[0x33C00]; // To store bishop attacks
Bitboard CannonTableH[0x11800]; // To store horizontal cannon attacks
Expand Down Expand Up @@ -111,7 +111,7 @@ namespace {
const std::map<Direction, int> GrasshopperDirectionsH { {EAST, 1}, {WEST, 1} };
const std::map<Direction, int> GrasshopperDirectionsD { {NORTH_EAST, 1}, {SOUTH_EAST, 1}, {SOUTH_WEST, 1}, {NORTH_WEST, 1} };

enum MovementType { RIDER, HOPPER, LAME_LEAPER, UNLIMITED_RIDER };
enum MovementType { RIDER, HOPPER, LAME_LEAPER, HOPPER_RANGE };

template <MovementType MT>
#ifdef PRECOMPUTED_MAGICS
Expand All @@ -137,7 +137,9 @@ namespace {
if (MT != HOPPER || hurdle)
{
attack |= s;
if (limit && MT != UNLIMITED_RIDER && ++count >= limit)
// For hoppers we consider limit == 1 as a grasshopper,
// but limit > 1 as a limited distance hopper
if (limit && !(MT == HOPPER_RANGE && limit == 1) && ++count >= limit)
break;
}

Expand Down Expand Up @@ -220,7 +222,7 @@ std::string Bitboards::pretty(Bitboard b) {

s += "| " + std::to_string(1 + r) + "\n+---+---+---+---+---+---+---+---+---+---+---+---+\n";
}
s += " a b c d e f g h i j k\n";
s += " a b c d e f g h i j k l\n";

return s;
}
Expand Down Expand Up @@ -300,7 +302,7 @@ void Bitboards::init_pieces() {
leaper |= safe_destination(s, c == WHITE ? d : -d);
}
pseudo |= sliding_attack<RIDER>(pi->slider[initial][modality], s, 0, c);
pseudo |= sliding_attack<UNLIMITED_RIDER>(pi->hopper[initial][modality], s, 0, c);
pseudo |= sliding_attack<HOPPER_RANGE>(pi->hopper[initial][modality], s, 0, c);
}
}
}
Expand Down Expand Up @@ -420,7 +422,7 @@ namespace {
// apply to the 64 or 32 bits word to get the index.
Magic& m = magics[s];
// The mask for hoppers is unlimited distance, even if the hopper is limited distance (e.g., grasshopper)
m.mask = (MT == LAME_LEAPER ? lame_leaper_path(directions, s) : sliding_attack<MT == HOPPER ? UNLIMITED_RIDER : MT>(directions, s, 0)) & ~edges;
m.mask = (MT == LAME_LEAPER ? lame_leaper_path(directions, s) : sliding_attack<MT == HOPPER ? HOPPER_RANGE : MT>(directions, s, 0)) & ~edges;
#ifdef LARGEBOARDS
m.shift = 128 - popcount(m.mask);
#else
Expand Down
11 changes: 7 additions & 4 deletions src/evaluate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,13 @@ namespace Eval {
exit(EXIT_FAILURE);
}

if (useNNUE)
sync_cout << "info string NNUE evaluation using " << eval_file_loaded << " enabled" << sync_endl;
else
sync_cout << "info string classical evaluation enabled" << sync_endl;
if (CurrentProtocol != XBOARD)
{
if (useNNUE)
sync_cout << "info string NNUE evaluation using " << eval_file_loaded << " enabled" << sync_endl;
else
sync_cout << "info string classical evaluation enabled" << sync_endl;
}
}
}

Expand Down
31 changes: 24 additions & 7 deletions src/movegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ namespace {
ExtMove* make_move_and_gating(const Position& pos, ExtMove* moveList, Color us, Square from, Square to, PieceType pt = NO_PIECE_TYPE) {

// Wall placing moves
if (pos.wall_gating())
//if it's "wall or move", and they chose non-null move, skip even generating wall move
if (pos.walling() && !(pos.variant()->wallOrMove && (from!=to)))
{
Bitboard b = pos.board_bb() & ~((pos.pieces() ^ from) | to);
if (T == CASTLING)
Expand All @@ -41,13 +42,23 @@ namespace {
}
if (T == EN_PASSANT)
b ^= pos.capture_square(to);
if (pos.variant()->arrowGating)

if (pos.walling_rule() == ARROW)
b &= moves_bb(us, type_of(pos.piece_on(from)), to, pos.pieces() ^ from);
if (pos.variant()->staticGating)
b &= pos.variant()->staticGatingRegion;
if (pos.variant()->pastGating)

//Any current or future wall variant must follow the walling region rule if set:
b &= pos.variant()->wallingRegion[us];

if (pos.walling_rule() == PAST)
b &= square_bb(from);
if (pos.walling_rule() == EDGE)
{
Bitboard wallsquares = pos.state()->wallSquares;

b &= (FileABB | file_bb(pos.max_file()) | Rank1BB | rank_bb(pos.max_rank())) |
( shift<NORTH >(wallsquares) | shift<SOUTH >(wallsquares)
| shift<EAST >(wallsquares) | shift<WEST >(wallsquares));
}
while (b)
*moveList++ = make_gating<T>(from, to, pt, pop_lsb(b));
return moveList;
Expand Down Expand Up @@ -431,8 +442,14 @@ namespace {
}

// Workaround for passing: Execute a non-move with any piece
if (pos.pass() && !pos.count<KING>(Us) && pos.pieces(Us))
if (pos.pass(Us) && !pos.count<KING>(Us) && pos.pieces(Us))
*moveList++ = make<SPECIAL>(lsb(pos.pieces(Us)), lsb(pos.pieces(Us)));

//if "wall or move", generate walling action with null move
if (pos.variant()->wallOrMove)
{
moveList = make_move_and_gating<SPECIAL>(pos, moveList, Us, lsb(pos.pieces(Us)), lsb(pos.pieces(Us)));
}
}

// King moves
Expand All @@ -444,7 +461,7 @@ namespace {
moveList = make_move_and_gating<NORMAL>(pos, moveList, Us, ksq, pop_lsb(b));

// Passing move by king
if (pos.pass())
if (pos.pass(Us))
*moveList++ = make<SPECIAL>(ksq, ksq);

if ((Type == QUIETS || Type == NON_EVASIONS) && pos.can_castle(Us & ANY_CASTLING))
Expand Down
Loading

0 comments on commit 32c755c

Please sign in to comment.