From 7a99d9bdc4466a4db8728950d5573712014dd5a9 Mon Sep 17 00:00:00 2001 From: Johannes Czech Date: Thu, 3 Jun 2021 19:02:27 +0200 Subject: [PATCH] Release 0.9.4 (#128) * removed legalActions again from get_state_planes() increased version to 0.9.4 added is_terminal() for MCTS_TB_SUPPORT=false * improved debug info logging --- engine/src/agents/mctsagent.cpp | 2 +- engine/src/agents/rawnetagent.cpp | 2 +- engine/src/constants.h | 2 +- .../environments/chess_related/boardstate.cpp | 4 ++-- .../src/environments/chess_related/boardstate.h | 2 +- .../chess_related/chessbatchstream.cpp | 2 +- .../chess_related/inputrepresentation.cpp | 6 +++--- .../chess_related/inputrepresentation.h | 2 +- engine/src/nn/tensorrtapi.cpp | 17 +++++++++++++---- engine/src/nn/tensorrtapi.h | 5 +++-- engine/src/searchthread.cpp | 4 ++-- engine/src/state.h | 2 +- engine/tests/tests.cpp | 4 ++-- 13 files changed, 32 insertions(+), 22 deletions(-) diff --git a/engine/src/agents/mctsagent.cpp b/engine/src/agents/mctsagent.cpp index b704216a..55b921ac 100644 --- a/engine/src/agents/mctsagent.cpp +++ b/engine/src/agents/mctsagent.cpp @@ -178,7 +178,7 @@ void MCTSAgent::create_new_root_node(StateObj* state) rootNode->set_value(newState->random_rollout()); rootNode->enable_has_nn_results(); #else - state->get_state_planes(true, inputPlanes, rootNode->get_legal_actions()); + state->get_state_planes(true, inputPlanes); net->predict(inputPlanes, valueOutputs, probOutputs, auxiliaryOutputs); size_t tbHits = 0; fill_nn_results(0, net->is_policy_map(), valueOutputs, probOutputs, auxiliaryOutputs, rootNode.get(), tbHits, state->side_to_move(), searchSettings, rootNode->is_tablebase()); diff --git a/engine/src/agents/rawnetagent.cpp b/engine/src/agents/rawnetagent.cpp index 201229ff..dbe7a351 100644 --- a/engine/src/agents/rawnetagent.cpp +++ b/engine/src/agents/rawnetagent.cpp @@ -53,7 +53,7 @@ void RawNetAgent::evaluate_board_state() evalInfo->pv[0] = {evalInfo->legalMoves[0]}; return; } - state->get_state_planes(true, inputPlanes, state->legal_actions()); + state->get_state_planes(true, inputPlanes); net->predict(inputPlanes, valueOutputs, probOutputs, auxiliaryOutputs); state->set_auxiliary_outputs(auxiliaryOutputs); diff --git a/engine/src/constants.h b/engine/src/constants.h index ba266c4d..bb982b14 100644 --- a/engine/src/constants.h +++ b/engine/src/constants.h @@ -53,7 +53,7 @@ const string engineName = "MultiAra"; const string engineName = "ClassicAra"; #endif -const string engineVersion = "0.9.4-Dev"; +const string engineVersion = "0.9.4"; const string engineAuthors = "Johannes Czech, Moritz Willig, Alena Beyer et al."; #define LOSS_VALUE -1 diff --git a/engine/src/environments/chess_related/boardstate.cpp b/engine/src/environments/chess_related/boardstate.cpp index 32dea170..c9da3192 100644 --- a/engine/src/environments/chess_related/boardstate.cpp +++ b/engine/src/environments/chess_related/boardstate.cpp @@ -70,9 +70,9 @@ void BoardState::set(const string &fenStr, bool isChess960, int variant) board.set(fenStr, isChess960, Variant(variant), &states->back(), nullptr); } -void BoardState::get_state_planes(bool normalize, float *inputPlanes, const vector& legalActions) const +void BoardState::get_state_planes(bool normalize, float *inputPlanes) const { - board_to_planes(&board, board.number_repetitions(), normalize, inputPlanes, legalActions); + board_to_planes(&board, board.number_repetitions(), normalize, inputPlanes); } unsigned int BoardState::steps_from_null() const diff --git a/engine/src/environments/chess_related/boardstate.h b/engine/src/environments/chess_related/boardstate.h index 9606c4d4..966eee89 100644 --- a/engine/src/environments/chess_related/boardstate.h +++ b/engine/src/environments/chess_related/boardstate.h @@ -278,7 +278,7 @@ class BoardState : public State // State interface vector legal_actions() const override; void set(const string &fenStr, bool isChess960, int variant) override; - void get_state_planes(bool normalize, float *inputPlanes, const vector& legalActions) const override; + void get_state_planes(bool normalize, float *inputPlanes) const override; unsigned int steps_from_null() const override; bool is_chess960() const override; string fen() const override; diff --git a/engine/src/environments/chess_related/chessbatchstream.cpp b/engine/src/environments/chess_related/chessbatchstream.cpp index 41a28971..2becefe8 100644 --- a/engine/src/environments/chess_related/chessbatchstream.cpp +++ b/engine/src/environments/chess_related/chessbatchstream.cpp @@ -100,7 +100,7 @@ ChessBatchStream::ChessBatchStream(int batchSize, int maxBatches): for (size_t idx = 0; idx < size_t(batchSize * maxBatches); ++idx) { states->emplace_back(); - board_to_planes(&pos, pos.number_repetitions(), true, mData.data() + StateConstants::NB_VALUES_TOTAL() * idx, pos.legal_actions()); + board_to_planes(&pos, pos.number_repetitions(), true, mData.data() + StateConstants::NB_VALUES_TOTAL() * idx); if (idx == curUciMoves.size()) { reset_to_startpos(pos, uiThread.get(), states); offset = curUciMoves.size(); diff --git a/engine/src/environments/chess_related/inputrepresentation.cpp b/engine/src/environments/chess_related/inputrepresentation.cpp index 5b01fd71..7be9e012 100644 --- a/engine/src/environments/chess_related/inputrepresentation.cpp +++ b/engine/src/environments/chess_related/inputrepresentation.cpp @@ -426,7 +426,7 @@ inline void board_to_planes_v_2_8(PlaneData& planeData, const vector& le #endif -void board_to_planes(const Board *pos, size_t boardRepetition, bool normalize, float *inputPlanes, const vector& legalMoves) +void board_to_planes(const Board *pos, size_t boardRepetition, bool normalize, float *inputPlanes) { // Fill in the piece positions // Iterate over both color starting with WHITE @@ -434,9 +434,9 @@ void board_to_planes(const Board *pos, size_t boardRepetition, bool normalize, f #if VERSION == 2 #if SUB_VERSION == 7 - board_to_planes_v_2_7(planeData, legalMoves); + board_to_planes_v_2_7(planeData, pos->legal_actions()); #elif SUB_VERSION == 8 - board_to_planes_v_2_8(planeData, legalMoves); + board_to_planes_v_2_8(planeData, pos->legal_actions()); #endif assert(planeData.currentChannel == StateConstants::NB_CHANNELS_TOTAL()); return; diff --git a/engine/src/environments/chess_related/inputrepresentation.h b/engine/src/environments/chess_related/inputrepresentation.h index eba86541..feda13a7 100644 --- a/engine/src/environments/chess_related/inputrepresentation.h +++ b/engine/src/environments/chess_related/inputrepresentation.h @@ -39,7 +39,7 @@ * @param normalize Flag, telling if the representation should be rescaled into the [0,1] range using the scaling constants from "constants.h" * @param input_planes Output where the plane representation will be stored. */ -void board_to_planes(const Board *pos, size_t boardRepetition, bool normalize, float *inputPlanes, const vector& legalAction); +void board_to_planes(const Board *pos, size_t boardRepetition, bool normalize, float *inputPlanes); /** * @brief set_bits_from_bitmap Sets the individual bits from a given bitboard on the given channel for the inputPlanes diff --git a/engine/src/nn/tensorrtapi.cpp b/engine/src/nn/tensorrtapi.cpp index 881954f3..a9048e37 100644 --- a/engine/src/nn/tensorrtapi.cpp +++ b/engine/src/nn/tensorrtapi.cpp @@ -46,7 +46,8 @@ TensorrtAPI::TensorrtAPI(int deviceID, unsigned int batchSize, const string &mod idxValueOutput(nnDesign.valueOutputIdx-nnDesign.nbInputs), idxPolicyOutput(nnDesign.policyOutputIdx-nnDesign.nbInputs), idxAuxiliaryOutput(nnDesign.auxiliaryOutputIdx-nnDesign.nbInputs), - precision(str_to_precision(strPrecision)) + precision(str_to_precision(strPrecision)), + generatedTrtFromONNX(false) { // select the requested device cudaSetDevice(deviceID); @@ -88,7 +89,7 @@ void TensorrtAPI::load_parameters() // do nothing } -bool TensorrtAPI::retrieve_indices_by_name() +bool TensorrtAPI::retrieve_indices_by_name(bool verbose) { idxInput = engine->getBindingIndex(nnDesign.inputLayerName.c_str()); if (idxInput == -1) { @@ -112,6 +113,14 @@ bool TensorrtAPI::retrieve_indices_by_name() return false; } } + if (verbose) { + info_string("Found 'idxInput' at index", idxInput); + info_string("Found 'idxValueOutput' at index", idxValueOutput); + info_string("Found 'idxPolicyOutput' at index", idxPolicyOutput); + if (nnDesign.hasAuxiliaryOutputs) { + info_string("Found 'idxAuxiliaryOutput' at index", idxAuxiliaryOutput); + } + } return true; } @@ -119,7 +128,7 @@ void TensorrtAPI:: init_nn_design() { nnDesign.hasAuxiliaryOutputs = engine->getNbBindings() > 3; - if (!retrieve_indices_by_name()) { + if (!retrieve_indices_by_name(generatedTrtFromONNX)) { info_string_important("Fallback to default indices."); idxInput = nnDesign.inputIdx; idxValueOutput = nnDesign.valueOutputIdx-nnDesign.nbInputs; @@ -240,6 +249,7 @@ ICudaEngine* TensorrtAPI::get_cuda_engine() { engine = create_cuda_engine_from_onnx(); std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); info_elapsed_time("Elapsed time for building TensorRT engine:", begin, end); + generatedTrtFromONNX = true; if (engine) { info_string("serialize engine:", trtFilePath); @@ -292,7 +302,6 @@ void TensorrtAPI::configure_network(SampleUniquePtrgetNbOutputs(); ++idx) { if (string(network->getOutput(idx)->getName()) == nnDesign.policyOutputName) { - info_string("Found policy output at index", idx); policyOutputIdx = idx; break; } diff --git a/engine/src/nn/tensorrtapi.h b/engine/src/nn/tensorrtapi.h index 1e1b7926..19857520 100644 --- a/engine/src/nn/tensorrtapi.h +++ b/engine/src/nn/tensorrtapi.h @@ -79,7 +79,7 @@ class TensorrtAPI : public NeuralNetAPI std::shared_ptr engine; SampleUniquePtr context; cudaStream_t stream; - + bool generatedTrtFromONNX; public: /** * @brief TensorrtAPI @@ -96,9 +96,10 @@ class TensorrtAPI : public NeuralNetAPI /** * @brief retrieve_indices_by_name Sets the layer name indices by names. + * @param verbose If true debug info will be shown * @return True if all layer names were found, else false */ - bool retrieve_indices_by_name(); + bool retrieve_indices_by_name(bool verbose); private: void load_model() override; diff --git a/engine/src/searchthread.cpp b/engine/src/searchthread.cpp index 1bb6f42d..d30c4847 100644 --- a/engine/src/searchthread.cpp +++ b/engine/src/searchthread.cpp @@ -220,7 +220,7 @@ Node* SearchThread::get_new_child_to_evaluate(NodeDescription& description) #else // fill a new board in the input_planes vector // we shift the index by nbNNInputValues each time - newState->get_state_planes(true, inputPlanes + newNodes->size() * nbNNInputValues, nextNode->get_legal_actions()); + newState->get_state_planes(true, inputPlanes + newNodes->size() * nbNNInputValues); // save a reference newly created list in the temporary list for node creation // it will later be updated with the evaluation of the NN newNodeSideToMove->add_element(newState->side_to_move()); @@ -231,7 +231,7 @@ Node* SearchThread::get_new_child_to_evaluate(NodeDescription& description) #ifdef MCTS_TB_SUPPORT if (nextNode->is_terminal() || (!reachedTablebases && nextNode->is_playout_node() && nextNode->is_solved())) { #else - if (nextNode->is_playout_node() && nextNode->is_solved()) { + if (nextNode->is_terminal() || (nextNode->is_playout_node() && nextNode->is_solved())) { #endif description.type = NODE_TERMINAL; currentNode->unlock(); diff --git a/engine/src/state.h b/engine/src/state.h index c9831cb0..aba8ff99 100644 --- a/engine/src/state.h +++ b/engine/src/state.h @@ -270,7 +270,7 @@ class State * @param normalize If true thw normalized represnetation should be returned, otherwise the raw representation * @param inputPlanes Pointer to the memory array where to set the state plane representation. It is assumed that the memory has already been allocated */ - virtual void get_state_planes(bool normalize, float* inputPlanes, const std::vector& legalActions) const = 0; + virtual void get_state_planes(bool normalize, float* inputPlanes) const = 0; /** * @brief steps_from_null Number of steps form the initial position (e.g. starting position) diff --git a/engine/tests/tests.cpp b/engine/tests/tests.cpp index c733a5b4..944d56f4 100644 --- a/engine/tests/tests.cpp +++ b/engine/tests/tests.cpp @@ -77,13 +77,13 @@ PlaneStatistics get_stats_from_input_planes(const float* inputPlanes) PlaneStatistics get_planes_statistics(const StateObj& state, bool normalize) { float inputPlanes[StateConstants::NB_VALUES_TOTAL()]; - state.get_state_planes(normalize, inputPlanes, state.legal_actions()); + state.get_state_planes(normalize, inputPlanes); return get_stats_from_input_planes(inputPlanes); } PlaneStatistics get_planes_statistics(const Board& pos, bool normalize) { float inputPlanes[StateConstants::NB_VALUES_TOTAL()]; - board_to_planes(&pos, pos.number_repetitions(), normalize, inputPlanes, pos.legal_actions()); + board_to_planes(&pos, pos.number_repetitions(), normalize, inputPlanes); return get_stats_from_input_planes(inputPlanes); }