From: Steinar H. Gunderson Date: Fri, 13 Sep 2024 13:12:36 +0000 (+0200) Subject: Merge commit '299707d2c2cbf1694bb21ed4a375b54ef35d719e^' X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=4c5480bd6f3d707f4de03b0b2eeb74c23d0d2695;p=stockfish Merge commit '299707d2c2cbf1694bb21ed4a375b54ef35d719e^' --- 4c5480bd6f3d707f4de03b0b2eeb74c23d0d2695 diff --cc src/hash_probe_impl.cpp index 90510f97,00000000..e7e7684a mode 100644,000000..100644 --- a/src/hash_probe_impl.cpp +++ b/src/hash_probe_impl.cpp @@@ -1,214 -1,0 +1,214 @@@ +#include +#include +#include +#include +#include + +#include "bitboard.h" +#include "evaluate.h" +#include "misc.h" +#include "position.h" +#include "search.h" +#include "thread.h" +#include "tune.h" +#include "types.h" +#include "uci.h" + +#include +#include +#include +#include "hashprobe.h" +#include "hashprobe.grpc.pb.h" +#include "tt.h" + +using grpc::Server; +using grpc::ServerBuilder; +using grpc::ServerContext; +using grpc::Status; +using grpc::StatusCode; +using namespace hashprobe; +using namespace Stockfish; + +Status HashProbeImpl::Probe(ServerContext* context, + const HashProbeRequest* request, + HashProbeResponse *response) { + Position pos; + StateInfo st; + pos.set(request->fen(), /*isChess960=*/false, &st); + if (!pos.pos_is_ok()) { + return Status(StatusCode::INVALID_ARGUMENT, "Invalid FEN"); + } + + bool invert = (pos.side_to_move() == BLACK); + StateListPtr setup_states = StateListPtr(new std::deque(1)); + + ProbeMove(&pos, setup_states.get(), invert, response->mutable_root()); + + MoveList moves(pos); + for (const ExtMove* em = moves.begin(); em != moves.end(); ++em) { + HashProbeLine *line = response->add_line(); + FillMove(&pos, *em, line->mutable_move()); + setup_states->push_back(StateInfo()); + pos.do_move(*em, setup_states->back()); + ProbeMove(&pos, setup_states.get(), !invert, line); + pos.undo_move(*em); + } + + return Status::OK; +} + +void HashProbeImpl::FillMove(Position *pos, Move move, HashProbeMove* decoded) { + if (!move.is_ok()) return; + + Square from = move.from_sq(); + Square to = move.to_sq(); + + if (move.type_of() == CASTLING) { + to = make_square(to > from ? FILE_G : FILE_C, rank_of(from)); + } + + Piece moved_piece = pos->moved_piece(move); + std::string pretty; + if (move.type_of() == CASTLING) { + if (to > from) { + pretty = "O-O"; + } else { + pretty = "O-O-O"; + } + } else if (type_of(moved_piece) == PAWN) { + if (move.type_of() == EN_PASSANT || pos->piece_on(to) != NO_PIECE) { + // Capture. + pretty = char('a' + file_of(from)); + pretty += "x"; + } + pretty += UCI::square(to); + if (move.type_of() == PROMOTION) { + pretty += "="; + pretty += " PNBRQK"[move.promotion_type()]; + } + } else { + pretty = " PNBRQK"[type_of(moved_piece)]; + Bitboard attackers = pos->attackers_to(to) & pos->pieces(color_of(moved_piece), type_of(moved_piece)); + if (more_than_one(attackers)) { + // Remove all illegal moves to disambiguate. + Bitboard att_copy = attackers; + while (att_copy) { + Square s = pop_lsb(att_copy); + Move m(s, to); + if (!pos->pseudo_legal(m) || !pos->legal(m)) { + attackers &= ~square_bb(s); + } + } + } + if (more_than_one(attackers)) { + // Disambiguate by file if possible. + Bitboard attackers_this_file = attackers & file_bb(file_of(from)); + if (attackers != attackers_this_file) { + pretty += char('a' + file_of(from)); + attackers = attackers_this_file; + } + if (more_than_one(attackers)) { + // Still ambiguous, so need to disambiguate by rank. + pretty += char('1' + rank_of(from)); + } + } + + if (move.type_of() == EN_PASSANT || pos->piece_on(to) != NO_PIECE) { + pretty += "x"; + } + + pretty += UCI::square(to); + } + + if (pos->gives_check(move)) { + // Check if mate. + StateInfo si; + pos->do_move(move, si, true); + if (MoveList(*pos).size() > 0) { + pretty += "+"; + } else { + pretty += "#"; + } + pos->undo_move(move); + } + + decoded->set_pretty(pretty); +} + +void HashProbeImpl::ProbeMove(Position* pos, std::deque* setup_states, bool invert, HashProbeLine* response) { + bool found; + TTEntry *entry = tt.probe(pos->key(), found); + response->set_found(found); + if (found) { + TTEntry entry_copy = *entry; + Value value = entry_copy.value(); + Value eval = entry_copy.eval(); + Bound bound = entry_copy.bound(); + + if (invert) { + value = -value; + eval = -eval; + if (bound == BOUND_UPPER) { + bound = BOUND_LOWER; + } else if (bound == BOUND_LOWER) { + bound = BOUND_UPPER; + } + } + + response->set_depth(entry_copy.depth()); - FillValue(eval, response->mutable_eval()); ++ FillValue(eval, response->mutable_eval(), *pos); + if (entry_copy.depth() > DEPTH_NONE) { - FillValue(value, response->mutable_value()); ++ FillValue(value, response->mutable_value(), *pos); + } + response->set_bound(HashProbeLine::ValueBound(bound)); + + // Follow the PV until we hit an illegal move. + std::stack pv; + std::set seen; + while (entry_copy.move().is_ok() && + pos->pseudo_legal(entry_copy.move()) && + pos->legal(entry_copy.move())) { + FillMove(pos, entry_copy.move(), response->add_pv()); + if (seen.count(pos->key())) break; + pv.push(entry_copy.move()); + seen.insert(pos->key()); + setup_states->push_back(StateInfo()); + pos->do_move(entry_copy.move(), setup_states->back()); + entry = tt.probe(pos->key(), found); + if (!found) { + break; + } + entry_copy = *entry; + } + + // Unroll the PV back again, so the Position object remains unchanged. + while (!pv.empty()) { + pos->undo_move(pv.top()); + pv.pop(); + } + } +} + - void HashProbeImpl::FillValue(Value value, HashProbeScore* score) { ++void HashProbeImpl::FillValue(Value value, HashProbeScore* score, Position& pos) { + if (abs(value) < VALUE_MATE - MAX_PLY) { + score->set_score_type(HashProbeScore::SCORE_CP); - score->set_score_cp(UCI::to_cp(value)); ++ score->set_score_cp(UCI::to_cp(value, pos)); + } else { + score->set_score_type(HashProbeScore::SCORE_MATE); + score->set_score_mate((value > 0 ? VALUE_MATE - value + 1 : -VALUE_MATE - value) / 2); + } +} + +HashProbeThread::HashProbeThread(const std::string &server_address, TranspositionTable &tt) + : service(tt) { + builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); + builder.RegisterService(&service); + server = std::move(builder.BuildAndStart()); + std::cout << "Server listening on " << server_address << std::endl; + std::thread([this]{ server->Wait(); }).detach(); +} + +void HashProbeThread::Shutdown() { + server->Shutdown(); +} + diff --cc src/hashprobe.h index 4e9438de,00000000..a227c891 mode 100644,000000..100644 --- a/src/hashprobe.h +++ b/src/hashprobe.h @@@ -1,43 -1,0 +1,43 @@@ +#ifndef HASHPROBE_H_INCLUDED +#define HASHPROBE_H_INCLUDED + +#include "position.h" +#include "types.h" +#include "tt.h" + +#include +#include + +#include +#include +#include +#include "hashprobe.grpc.pb.h" + +class HashProbeImpl final : public hashprobe::HashProbe::Service { +public: + HashProbeImpl(Stockfish::TranspositionTable& tt) : tt(tt) {} + + grpc::Status Probe(grpc::ServerContext* context, + const hashprobe::HashProbeRequest* request, + hashprobe::HashProbeResponse *response); + +private: + void FillMove(Stockfish::Position* pos, Stockfish::Move move, hashprobe::HashProbeMove* decoded); + void ProbeMove(Stockfish::Position* pos, std::deque* setup_states, bool invert, hashprobe::HashProbeLine* response); - void FillValue(Stockfish::Value value, hashprobe::HashProbeScore* score); ++ void FillValue(Stockfish::Value value, hashprobe::HashProbeScore* score, Stockfish::Position& pos); + + Stockfish::TranspositionTable& tt; +}; + +class HashProbeThread { +public: + HashProbeThread(const std::string &server_address, Stockfish::TranspositionTable& tt); + void Shutdown(); + +private: + HashProbeImpl service; + grpc::ServerBuilder builder; + std::unique_ptr server; +}; + +#endif diff --cc src/uci.cpp index a767a6ff,ee95d5be..361cf2cc --- a/src/uci.cpp +++ b/src/uci.cpp @@@ -46,13 -45,12 +47,12 @@@ namespace Stockfish { - constexpr auto StartFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; - constexpr int NormalizeToPawnValue = 356; - constexpr int MaxHashMB = Is64Bit ? 33554432 : 2048; + constexpr auto StartFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; + constexpr int MaxHashMB = Is64Bit ? 33554432 : 2048; - namespace NN = Eval::NNUE; +std::unique_ptr hash_probe_thread; UCI::UCI(int argc, char** argv) : networks(NN::Networks(