From d8b26b92dfcaa1f93c40f77d21cb2a2df83bc9be Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Wed, 12 Apr 2023 04:47:23 +0200 Subject: [PATCH 1/6] Fix the casting from default double to any possible type (#3467) --- src/mlpack/methods/ann/init_rules/network_init.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/methods/ann/init_rules/network_init.hpp b/src/mlpack/methods/ann/init_rules/network_init.hpp index 4c923d6dc6..d0810b7840 100644 --- a/src/mlpack/methods/ann/init_rules/network_init.hpp +++ b/src/mlpack/methods/ann/init_rules/network_init.hpp @@ -71,7 +71,7 @@ class NetworkInitialization // Initialize the layer with the specified parameter/weight // initialization rule. const size_t weight = network[i]->WeightSize(); - arma::Mat tmp = arma::mat(parameters.memptr() + offset, + arma::Mat tmp = arma::Mat(parameters.memptr() + offset, weight, 1, false, false); initializeRule.Initialize(tmp, tmp.n_elem, 1); From 309d1d2e01fcc6866316222df6f2432967cbbfa4 Mon Sep 17 00:00:00 2001 From: Ana B <67980649+anablaz@users.noreply.github.com> Date: Wed, 12 Apr 2023 04:53:14 +0200 Subject: [PATCH 2/6] Fix links (#3462) --- README.md | 2 +- doc/user/build_windows.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3cc3641897..cfe03c21fa 100644 --- a/README.md +++ b/README.md @@ -252,7 +252,7 @@ build in parallel; e.g., `make -j4` will use 4 cores to build. mlpack's Python bindings are available on [PyPI](https://pypi.org/project/mlpack) and -[conda-forge](https://conda-forge.org/packages/mlpack), and can be installed +[conda-forge](https://anaconda.org/conda-forge/mlpack), and can be installed with either `pip install mlpack` or `conda install -c conda-forge mlpack`. These sources are recommended, as building the Python bindings by hand can be complex. diff --git a/doc/user/build_windows.md b/doc/user/build_windows.md index 61cbae34ef..69b4cbbf71 100644 --- a/doc/user/build_windows.md +++ b/doc/user/build_windows.md @@ -261,5 +261,5 @@ If you are facing issues during the build process of mlpack, you may take a look at other third-party tutorials for Windows, but they may be out of date: * [Github wiki Windows Build page](https://github.com/mlpack/mlpack/wiki/WindowsBuild) - * [Keon's tutorial for mlpack 2.0.3](http://keon.io/mlpack-on-windows) + * [Keon's tutorial for mlpack 2.0.3](https://keon.github.io/mlpack-on-windows/) * [Kirizaki's tutorial for mlpack 2](https://overdosedblog.wordpress.com/2016/08/15/once_again/) From fd7ca06b2254236f712fd82604f8b816e3c24fdf Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Fri, 14 Apr 2023 10:30:10 -0400 Subject: [PATCH 3/6] Issue error when serializing ANN models without `MLPACK_ENABLE_ANN_SERIALIZATION` (#3451) --- HISTORY.md | 6 +- README.md | 5 +- src/mlpack/methods/ann/ffn_impl.hpp | 56 +++++++++++-------- src/mlpack/methods/ann/rnn_impl.hpp | 32 +++++++---- src/mlpack/tests/CMakeLists.txt | 9 ++- .../tests/ann/feedforward_network_test.cpp | 4 +- src/mlpack/tests/ann/layer/batch_norm.cpp | 4 +- src/mlpack/tests/main.cpp | 3 + 8 files changed, 82 insertions(+), 37 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index a851154847..0a64ec6b31 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -7,7 +7,8 @@ * Adapt PReLU layer for new neural network API (#3420). - * Add CF decomposition methods: `QUIC_SVDPolicy` and `BlockKrylovSVDPolicy` (#3413, #3404). + * Add CF decomposition methods: `QUIC_SVDPolicy` and `BlockKrylovSVDPolicy` + (#3413, #3404). * Update outdated code in tutorials (#3398, #3401). @@ -19,6 +20,9 @@ * Avoid deprecation warnings in Armadillo 11.4.4+ (#3405). + * Issue runtime error when serialization of neural networks is attempted but + `MLPACK_ENABLE_ANN_SERIALIZATION` is not defined (#3451). + ### mlpack 4.0.1 ###### 2022-12-23 * Fix mapping of categorical data for Julia bindings (#3305). diff --git a/README.md b/README.md index cfe03c21fa..7ab827ded4 100644 --- a/README.md +++ b/README.md @@ -180,6 +180,8 @@ g++ -O3 -std=c++14 -o my_program my_program.cpp -larmadillo -fopenmp Note that if you want to serialize (save or load) neural networks, you should add `#define MLPACK_ENABLE_ANN_SERIALIZATION` before including ``. +If you don't define `MLPACK_ENABLE_ANN_SERIALIZATION` and your code serializes a +neural network, a compilation error will occur. See the [C++ quickstart](doc/quickstart/cpp.md) and the [examples](https://github.com/mlpack/examples) repository for some examples of @@ -198,7 +200,8 @@ reduce compilation time: * Only use the `MLPACK_ENABLE_ANN_SERIALIZATION` definition if you are serializing neural networks in your code. When this define is enabled, compilation time will increase significantly, as the compiler must generate - code for every possible type of layer. + code for every possible type of layer. (The large amount of extra + compilation overhead is why this is not enabled by default.) * If you are using mlpack in multiple .cpp files, consider using [`extern templates`](https://isocpp.org/wiki/faq/cpp11-language-templates) so that the diff --git a/src/mlpack/methods/ann/ffn_impl.hpp b/src/mlpack/methods/ann/ffn_impl.hpp index ee9a55f821..e3e1b69ddb 100644 --- a/src/mlpack/methods/ann/ffn_impl.hpp +++ b/src/mlpack/methods/ann/ffn_impl.hpp @@ -374,34 +374,46 @@ void FFN< MatType >::serialize(Archive& ar, const uint32_t /* version */) { - // Serialize the output layer and initialization rule. - ar(CEREAL_NVP(outputLayer)); - ar(CEREAL_NVP(initializeRule)); + #ifndef MLPACK_ENABLE_ANN_SERIALIZATION + // Note: if you define MLPACK_IGNORE_ANN_SERIALIZATION_WARNING, you had + // better ensure that every layer you are serializing has had + // CEREAL_REGISTER_TYPE() called somewhere. See layer/serialization.hpp for + // more information. + #ifndef MLPACK_ANN_IGNORE_SERIALIZATION_WARNING + throw std::runtime_error("Cannot serialize a neural network unless " + "MLPACK_ENABLE_ANN_SERIALIZATION is defined! See the \"Additional " + "build options\" section of the README for more information."); + #endif + #else + // Serialize the output layer and initialization rule. + ar(CEREAL_NVP(outputLayer)); + ar(CEREAL_NVP(initializeRule)); - // Serialize the network itself. - ar(CEREAL_NVP(network)); - ar(CEREAL_NVP(parameters)); + // Serialize the network itself. + ar(CEREAL_NVP(network)); + ar(CEREAL_NVP(parameters)); - // Serialize the expected input size. - ar(CEREAL_NVP(inputDimensions)); + // Serialize the expected input size. + ar(CEREAL_NVP(inputDimensions)); - // If we are loading, we need to initialize the weights. - if (cereal::is_loading()) - { - // We can clear these members, since it's not possible to serialize in the - // middle of training and resume. - predictors.clear(); - responses.clear(); + // If we are loading, we need to initialize the weights. + if (cereal::is_loading()) + { + // We can clear these members, since it's not possible to serialize in the + // middle of training and resume. + predictors.clear(); + responses.clear(); - networkOutput.clear(); - networkDelta.clear(); + networkOutput.clear(); + networkDelta.clear(); - layerMemoryIsSet = false; - inputDimensionsAreSet = false; + layerMemoryIsSet = false; + inputDimensionsAreSet = false; - // The weights in `parameters` will be correctly set for each layer in the - // first call to Forward(). - } + // The weights in `parameters` will be correctly set for each layer in the + // first call to Forward(). + } + #endif } template::serialize( Archive& ar, const uint32_t /* version */) { - ar(CEREAL_NVP(bpttSteps)); - ar(CEREAL_NVP(single)); - ar(CEREAL_NVP(network)); + #ifndef MLPACK_ENABLE_ANN_SERIALIZATION + // Note: if you define MLPACK_IGNORE_ANN_SERIALIZATION_WARNING, you had + // better ensure that every layer you are serializing has had + // CEREAL_REGISTER_TYPE() called somewhere. See layer/serialization.hpp for + // more information. + #ifndef MLPACK_IGNORE_ANN_SERIALIZATION_WARNING + throw std::runtime_error("Cannot serialize a neural network unless " + "MLPACK_ENABLE_ANN_SERIALIZATION is defined! See the \"Additional " + "build options\" section of the README for more information."); + #endif + #else + ar(CEREAL_NVP(bpttSteps)); + ar(CEREAL_NVP(single)); + ar(CEREAL_NVP(network)); - if (Archive::is_loading::value) - { - // We can clear these members, since it's not possible to serialize in the - // middle of training and resume. - predictors.clear(); - responses.clear(); - } + if (Archive::is_loading::value) + { + // We can clear these members, since it's not possible to serialize in the + // middle of training and resume. + predictors.clear(); + responses.clear(); + } + #endif } template< diff --git a/src/mlpack/tests/CMakeLists.txt b/src/mlpack/tests/CMakeLists.txt index 17704fbd45..ed8b5b9ed3 100644 --- a/src/mlpack/tests/CMakeLists.txt +++ b/src/mlpack/tests/CMakeLists.txt @@ -221,7 +221,14 @@ else() target_compile_definitions(mlpack_test PUBLIC -DMLPACK_SUPPRESS_FATAL) endif() -set_target_properties(mlpack_test PROPERTIES COTIRE_CXX_PREFIX_HEADER_INIT "../core.hpp") +# This has to be added here so that cotire picks it up (even though it is in +# individual tests). +target_compile_definitions(mlpack_test PUBLIC -DMLPACK_ENABLE_ANN_SERIALIZATION) +set_target_properties(mlpack_test PROPERTIES COTIRE_CXX_PREFIX_HEADER_INIT + "../core.hpp") +# TODO: use the source below, but this requires the DET test to be refactored +# and cleaned up. +# "../../mlpack.hpp") cotire(mlpack_test) # Copy test data into right place. diff --git a/src/mlpack/tests/ann/feedforward_network_test.cpp b/src/mlpack/tests/ann/feedforward_network_test.cpp index de959e68fb..9bcbefa94c 100644 --- a/src/mlpack/tests/ann/feedforward_network_test.cpp +++ b/src/mlpack/tests/ann/feedforward_network_test.cpp @@ -10,7 +10,9 @@ * 3-clause BSD license along with mlpack. If not, see * http://www.opensource.org/licenses/BSD-3-Clause for more information. */ -#define MLPACK_ENABLE_ANN_SERIALIZATION +#ifndef MLPACK_ENABLE_ANN_SERIALIZATION + #define MLPACK_ENABLE_ANN_SERIALIZATION +#endif #include #include #include diff --git a/src/mlpack/tests/ann/layer/batch_norm.cpp b/src/mlpack/tests/ann/layer/batch_norm.cpp index 35ef57e58b..2e73a3e135 100644 --- a/src/mlpack/tests/ann/layer/batch_norm.cpp +++ b/src/mlpack/tests/ann/layer/batch_norm.cpp @@ -10,7 +10,9 @@ * 3-clause BSD license along with mlpack. If not, see * http://www.opensource.org/licenses/BSD-3-Clause for more information. */ -#define MLPACK_ENABLE_ANN_SERIALIZATION +#ifndef MLPACK_ENABLE_ANN_SERIALIZATION + #define MLPACK_ENABLE_ANN_SERIALIZATION +#endif #include #include diff --git a/src/mlpack/tests/main.cpp b/src/mlpack/tests/main.cpp index 92f445af72..1a6292e3b4 100644 --- a/src/mlpack/tests/main.cpp +++ b/src/mlpack/tests/main.cpp @@ -8,6 +8,9 @@ * 3-clause BSD license along with mlpack. If not, see * http://www.opensource.org/licenses/BSD-3-Clause for more information. */ +#ifndef MLPACK_ENABLE_ANN_SERIALIZATION + #define MLPACK_ENABLE_ANN_SERIALIZATION +#endif #include // #define CATCH_CONFIG_MAIN // catch.hpp will define main() From 39bc6beb6bd576b7f5a652b15bf15906eaae7c43 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Wed, 26 Apr 2023 19:43:37 -0400 Subject: [PATCH 4/6] Add license to hard_tanh.cpp test. (#3474) --- src/mlpack/tests/ann/layer/hard_tanh.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/mlpack/tests/ann/layer/hard_tanh.cpp b/src/mlpack/tests/ann/layer/hard_tanh.cpp index 2276981cac..8ceb8d953f 100644 --- a/src/mlpack/tests/ann/layer/hard_tanh.cpp +++ b/src/mlpack/tests/ann/layer/hard_tanh.cpp @@ -2,8 +2,12 @@ * @file tests/ann/layer/hard_tanh.cpp * @author Vaibhav Pathak * - * Tests the hard_tanh layer + * Tests the hard_tanh layer. * + * mlpack is free software; you may redistribute it and/or modify it under the + * terms of the 3-clause BSD license. You should have received a copy of the + * 3-clause BSD license along with mlpack. If not, see + * http://www.opensource.org/licenses/BSD-3-Clause for more information. */ #include From e12bc09e3069ea109fc7284876819cbf66e2ccc0 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Wed, 26 Apr 2023 20:06:56 -0400 Subject: [PATCH 5/6] Update and release version 4.1.0. --- CMakeLists.txt | 2 +- HISTORY.md | 4 ++-- README.md | 2 +- .../sample-ml-app/sample-ml-app/sample-ml-app.vcxproj | 2 +- doc/user/sample_ml_app.md | 4 ++-- src/mlpack/core/util/version.hpp | 4 ++-- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a5d704a784..0118cf6dbc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -339,7 +339,7 @@ if (NOT DOWNLOAD_DEPENDENCIES) else() find_package(Ensmallen "${ENSMALLEN_VERSION}") if (NOT ENSMALLEN_FOUND) - get_deps(http://www.ensmallen.org/files/ensmallen-latest.tar.gz ensmallen ensmallen-latest.tar.gz) + get_deps(http://www.ensmallen.org/files/ensmallen-2.19.1.tar.gz ensmallen ensmallen-latest.tar.gz) set(ENSMALLEN_INCLUDE_DIR ${GENERIC_INCLUDE_DIR}) find_package(Ensmallen REQUIRED) endif() diff --git a/HISTORY.md b/HISTORY.md index 0a64ec6b31..94b490aff1 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,5 +1,5 @@ -### mlpack ?.?.? -###### ????-??-?? +### mlpack 4.1.0 +###### 2023-04-26 * Adapt HardTanH layer (#3454). diff --git a/README.md b/README.md index 7ab827ded4..999d68953c 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ src="https://cdn.rawgit.com/mlpack/mlpack.org/e7d36ed8/mlpack-black.svg" style="

Download: - current stable version (4.0.1) + current stable version (4.1.0)

diff --git a/doc/examples/sample-ml-app/sample-ml-app/sample-ml-app.vcxproj b/doc/examples/sample-ml-app/sample-ml-app/sample-ml-app.vcxproj index ca7b09267a..5aae6d9ce7 100644 --- a/doc/examples/sample-ml-app/sample-ml-app/sample-ml-app.vcxproj +++ b/doc/examples/sample-ml-app/sample-ml-app/sample-ml-app.vcxproj @@ -104,7 +104,7 @@ true _DEBUG;_CONSOLE;%(PreprocessorDefinitions) Default - C:\mlpack\armadillo-11.4.1\include;C:\mlpack\mlpack-4.0.1\include\;C:\mlpack\cereal-1.3.2\include;C:\mlpack\ensmallen-2.19.0\include\%(AdditionalIncludeDirectories) + C:\mlpack\armadillo-11.4.1\include;C:\mlpack\mlpack-4.1.0\include\;C:\mlpack\cereal-1.3.2\include;C:\mlpack\ensmallen-2.19.0\include\%(AdditionalIncludeDirectories) stdcpp17 false /Zc:__cplusplus %(AdditionalOptions) diff --git a/doc/user/sample_ml_app.md b/doc/user/sample_ml_app.md index 07eb62ab99..17b3a9775a 100644 --- a/doc/user/sample_ml_app.md +++ b/doc/user/sample_ml_app.md @@ -27,13 +27,13 @@ dependencies in Release Mode). - Under C/C++ > General > Additional Include Directories add: ``` - C:\mlpack\armadillo-9.800.3\include - - C:\mlpack\mlpack-4.0.1\src + - C:\mlpack\mlpack-4.1.0\src - C:\mlpack\ensmallen-2.19.0\include - C:\mlpack\cereal-3.1.2\include ``` - Under Build Events > Post-Build Event > Command Line add: ``` - - xcopy /y "C:\mlpack\mlpack-4.0.1\packages\OpenBLAS.0.2.14.1\lib\native\bin\x64\*.dll" $(OutDir) + - xcopy /y "C:\mlpack\mlpack-4.1.0\packages\OpenBLAS.0.2.14.1\lib\native\bin\x64\*.dll" $(OutDir) ``` *Note*: recent versions of Visual Studio set "Conformance Mode" enabled by diff --git a/src/mlpack/core/util/version.hpp b/src/mlpack/core/util/version.hpp index 77fa1394e3..29ee7d6d34 100644 --- a/src/mlpack/core/util/version.hpp +++ b/src/mlpack/core/util/version.hpp @@ -17,8 +17,8 @@ // The version of mlpack. If this is a git repository, this will be a version // with higher number than the most recent release. #define MLPACK_VERSION_MAJOR 4 -#define MLPACK_VERSION_MINOR 0 -#define MLPACK_VERSION_PATCH 2 +#define MLPACK_VERSION_MINOR 1 +#define MLPACK_VERSION_PATCH 0 // The name of the version (for use by --version). namespace mlpack { From 4c6af0d8b0aab5fc639845fdf2236d2218f381b0 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Wed, 26 Apr 2023 20:06:56 -0400 Subject: [PATCH 6/6] Add new block for next release to HISTORY.md. --- CMakeLists.txt | 2 +- HISTORY.md | 3 +++ src/mlpack/core/util/version.hpp | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0118cf6dbc..a5d704a784 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -339,7 +339,7 @@ if (NOT DOWNLOAD_DEPENDENCIES) else() find_package(Ensmallen "${ENSMALLEN_VERSION}") if (NOT ENSMALLEN_FOUND) - get_deps(http://www.ensmallen.org/files/ensmallen-2.19.1.tar.gz ensmallen ensmallen-latest.tar.gz) + get_deps(http://www.ensmallen.org/files/ensmallen-latest.tar.gz ensmallen ensmallen-latest.tar.gz) set(ENSMALLEN_INCLUDE_DIR ${GENERIC_INCLUDE_DIR}) find_package(Ensmallen REQUIRED) endif() diff --git a/HISTORY.md b/HISTORY.md index 94b490aff1..2a6775a9da 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,6 @@ +### mlpack ?.?.? +###### ????-??-?? + ### mlpack 4.1.0 ###### 2023-04-26 diff --git a/src/mlpack/core/util/version.hpp b/src/mlpack/core/util/version.hpp index 29ee7d6d34..9fd554d3db 100644 --- a/src/mlpack/core/util/version.hpp +++ b/src/mlpack/core/util/version.hpp @@ -18,7 +18,7 @@ // with higher number than the most recent release. #define MLPACK_VERSION_MAJOR 4 #define MLPACK_VERSION_MINOR 1 -#define MLPACK_VERSION_PATCH 0 +#define MLPACK_VERSION_PATCH 1 // The name of the version (for use by --version). namespace mlpack {