From f037c54469e211387c2baa9a4a720f67043f20c3 Mon Sep 17 00:00:00 2001 From: QueensGambit Date: Tue, 17 Aug 2021 18:07:09 +0200 Subject: [PATCH 1/3] Added read_version_from_string() which allows parsing version identifier information for different input representations. It is expected to come after the substring "-v-" in the format "-v- .", e.g. "-v-1.2.onnx". If the information is missing or parsing failed, make_version<0,0,0>() will be returned. Versioning patch information is always set to 0. --- engine/src/nn/neuralnetapi.cpp | 32 ++++++++++++++++++++++++++++++++ engine/src/nn/neuralnetapi.h | 11 +++++++++++ 2 files changed, 43 insertions(+) diff --git a/engine/src/nn/neuralnetapi.cpp b/engine/src/nn/neuralnetapi.cpp index e2e8b46b..b8267238 100644 --- a/engine/src/nn/neuralnetapi.cpp +++ b/engine/src/nn/neuralnetapi.cpp @@ -25,6 +25,7 @@ #include "neuralnetapi.h" #include +#include #include "../stateobj.h" @@ -69,6 +70,8 @@ void NeuralNetAPI::initialize_nn_design() nbNNInputValues = nnDesign.inputShape.flatten() / batchSize; nbNNAuxiliaryOutputs = nnDesign.auxiliaryOutputShape.flatten() / batchSize; policyOutputLength = nnDesign.policyOutputShape.v[1] * batchSize; + version = read_version_from_string(modelName); + info_string("Input representation: ", version_to_string(version)); } void NeuralNetAPI::initialize() @@ -164,3 +167,32 @@ ostream& nn_api::operator<<(ostream &os, const nn_api::Shape &shape) os << ")"; return os; } + +Version read_version_from_string(const string &modelFileName) +{ + // pattern to detect "-v-." + const string pattern = "(-v-)[0-9]+.[0-9]+"; + + // regex expression for pattern to be searched + regex regexp(pattern); + + // flag type for determining the matching behavior (in this case on string objects) + smatch matches; + + // regex_search that searches pattern regexp in the string + regex_search(modelFileName, matches, regexp); + + if (matches.size() > 0) { + for (auto match : matches) { + if (match.length() > 3) { + const string content = match; + const size_t pointPos = content.find("."); + const string versionMajor = content.substr(3, pointPos-3); // skip "-v-" + const string versionMinor = content.substr(pointPos+1); // skip "." + return make_version(std::stoi(versionMajor), std::stoi(versionMinor), 0); + } + } + } + // unsuccessfull + return make_version<0,0,0>(); +} diff --git a/engine/src/nn/neuralnetapi.h b/engine/src/nn/neuralnetapi.h index 500cdbc3..9dd4b883 100644 --- a/engine/src/nn/neuralnetapi.h +++ b/engine/src/nn/neuralnetapi.h @@ -91,6 +91,17 @@ vector get_items_by_elment(const vector& stringVector, const str */ string get_file_ending_with(const string& dir, const string& suffix); +/** + * @brief read_version_from_string Returns the corresponding version for a given model file name. + * The version identifier is expected to come after the substring "-v-" in the format "-v-.", e.g. "-v-1.2.onnx". + * If the information is missing or parsing failed, make_version<0,0,0>() will be returned. + * Versioning patch information is always set to 0. + * The version information is used to decide between different input representations for the neural network. + * @param modelFileName + * @return Version information + */ +Version read_version_from_string(const string& modelFileName); + template /** From 76827f881c27af26f606c17fa7ab0b2b177760a3 Mon Sep 17 00:00:00 2001 From: QueensGambit Date: Tue, 17 Aug 2021 20:14:38 +0200 Subject: [PATCH 2/3] changed version identifier to prefix "-v": e.g. "-v3.0.onnx" --- engine/src/nn/neuralnetapi.cpp | 14 ++++++++++---- engine/src/nn/neuralnetapi.h | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/engine/src/nn/neuralnetapi.cpp b/engine/src/nn/neuralnetapi.cpp index b8267238..f459092d 100644 --- a/engine/src/nn/neuralnetapi.cpp +++ b/engine/src/nn/neuralnetapi.cpp @@ -170,8 +170,9 @@ ostream& nn_api::operator<<(ostream &os, const nn_api::Shape &shape) Version read_version_from_string(const string &modelFileName) { - // pattern to detect "-v-." - const string pattern = "(-v-)[0-9]+.[0-9]+"; + // pattern to detect "-v." + const string prefix = "-v"; + const string pattern = "(" + prefix + ")[0-9]+.[0-9]+"; // regex expression for pattern to be searched regex regexp(pattern); @@ -187,9 +188,14 @@ Version read_version_from_string(const string &modelFileName) if (match.length() > 3) { const string content = match; const size_t pointPos = content.find("."); - const string versionMajor = content.substr(3, pointPos-3); // skip "-v-" + try { + const string versionMajor = content.substr(prefix.size(), pointPos-prefix.size()); // skip "-v" const string versionMinor = content.substr(pointPos+1); // skip "." - return make_version(std::stoi(versionMajor), std::stoi(versionMinor), 0); + return make_version(std::stoi(versionMajor), std::stoi(versionMinor), 0); + } catch (exception e) { + info_string(e.what()); + break; + } } } } diff --git a/engine/src/nn/neuralnetapi.h b/engine/src/nn/neuralnetapi.h index 9dd4b883..c08474bd 100644 --- a/engine/src/nn/neuralnetapi.h +++ b/engine/src/nn/neuralnetapi.h @@ -93,7 +93,7 @@ string get_file_ending_with(const string& dir, const string& suffix); /** * @brief read_version_from_string Returns the corresponding version for a given model file name. - * The version identifier is expected to come after the substring "-v-" in the format "-v-.", e.g. "-v-1.2.onnx". + * The version identifier is expected to come after the substring "-v" in the format "-v.", e.g. "-v1.2.onnx". * If the information is missing or parsing failed, make_version<0,0,0>() will be returned. * Versioning patch information is always set to 0. * The version information is used to decide between different input representations for the neural network. From 45da3d2b01360c8e2ce707f0b544459d948a2d2d Mon Sep 17 00:00:00 2001 From: QueensGambit Date: Tue, 17 Aug 2021 20:15:40 +0200 Subject: [PATCH 3/3] set USE_DYNAMIC_NN_ARCH to ON by default --- engine/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index bd7ae897..1135362e 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -9,7 +9,7 @@ option(BACKEND_MXNET "Build with MXNet backend (Blas/IntelMKL/CUDA/T option(BACKEND_TORCH "Build with Torch backend (CPU/GPU) support" OFF) option(USE_960 "Build with 960 variant support" OFF) option(BUILD_TESTS "Build and run tests" OFF) -option(USE_DYNAMIC_NN_ARCH "Build with dynamic neural network architektur support" OFF) +option(USE_DYNAMIC_NN_ARCH "Build with dynamic neural network architektur support" ON) # enable a single mode for different model input / outputs option(MODE_CRAZYHOUSE "Build with crazyhouse only support" ON) option(MODE_CHESS "Build with chess + chess960 only support" OFF)