From bf5fce877c3db4312eeb2a425df860107ee4fd7a Mon Sep 17 00:00:00 2001 From: Marcus Edel Date: Sat, 10 Apr 2021 05:06:30 +0200 Subject: [PATCH 01/89] Remove unused vector. --- src/mlpack/bindings/go/print_doc_functions_impl.hpp | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/mlpack/bindings/go/print_doc_functions_impl.hpp b/src/mlpack/bindings/go/print_doc_functions_impl.hpp index 3f1dcd9a5e..1f250aafb5 100644 --- a/src/mlpack/bindings/go/print_doc_functions_impl.hpp +++ b/src/mlpack/bindings/go/print_doc_functions_impl.hpp @@ -432,13 +432,6 @@ inline std::string ProgramCall(const std::string& programName) std::ostringstream ossOptions; ossOptions << "param := mlpack." << goProgramName << "Options()\n"; oss << util::HyphenateString(ossOptions.str(), 4); - std::vector outputOptions; - for (auto it = parameters.begin(); it != parameters.end(); ++it) - { - util::ParamData& d = it->second; - if (!d.input) - outputOptions.push_back(it->first); - } std::string result = oss.str(); oss.str(""); std::ostringstream ossInputs; From f7c87020aae2f7ae91adaaa2b9ce56def661fdcb Mon Sep 17 00:00:00 2001 From: Marcus Edel Date: Sat, 10 Apr 2021 19:14:09 +0200 Subject: [PATCH 02/89] Remove unreachable code. --- src/mlpack/methods/det/dtree_impl.hpp | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/mlpack/methods/det/dtree_impl.hpp b/src/mlpack/methods/det/dtree_impl.hpp index 7df858e4d2..dfdb994049 100644 --- a/src/mlpack/methods/det/dtree_impl.hpp +++ b/src/mlpack/methods/det/dtree_impl.hpp @@ -874,19 +874,13 @@ double DTree::ComputeValue(const VecType& query) const } if (subtreeLeaves == 1) // If we are a leaf... - { return std::exp(std::log(ratio) - logVolume); - } - else - { - // Return either of the two children - left or right, depending on the - // splitValue - return (query[splitDim] <= splitValue) ? + + // Return either of the two children - left or right, depending on the + // splitValue. + return (query[splitDim] <= splitValue) ? left->ComputeValue(query) : right->ComputeValue(query); - } - - return 0.0; } // Index the buckets for possible usage later. From 4437b6b4d14e2c5d2fc8f04c566f12462ac0a4bb Mon Sep 17 00:00:00 2001 From: Marcus Edel Date: Sat, 10 Apr 2021 19:15:47 +0200 Subject: [PATCH 03/89] Remove unreachable cf code. --- src/mlpack/methods/cf/cf_model.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/mlpack/methods/cf/cf_model.cpp b/src/mlpack/methods/cf/cf_model.cpp index 226edcf1be..424726953e 100644 --- a/src/mlpack/methods/cf/cf_model.cpp +++ b/src/mlpack/methods/cf/cf_model.cpp @@ -89,31 +89,26 @@ CFWrapperBase* TrainHelper(const DecompositionPolicy& decomposition, return new CFWrapper(data, decomposition, numUsersForSimilarity, rank, maxIterations, minResidue, mit); - break; case CFModel::ITEM_MEAN_NORMALIZATION: return new CFWrapper(data, decomposition, numUsersForSimilarity, rank, maxIterations, minResidue, mit); - break; case CFModel::USER_MEAN_NORMALIZATION: return new CFWrapper(data, decomposition, numUsersForSimilarity, rank, maxIterations, minResidue, mit); - break; case CFModel::OVERALL_MEAN_NORMALIZATION: return new CFWrapper(data, decomposition, numUsersForSimilarity, rank, maxIterations, minResidue, mit); - break; case CFModel::Z_SCORE_NORMALIZATION: return new CFWrapper(data, decomposition, numUsersForSimilarity, rank, maxIterations, minResidue, mit); - break; } // This shouldn't ever happen. From 14427fd2f42af2ff5498dc9e17579e32c0ddc1bd Mon Sep 17 00:00:00 2001 From: Marcus Edel Date: Sat, 10 Apr 2021 19:17:49 +0200 Subject: [PATCH 04/89] Remove unreachable epanechnikov kernel code. --- src/mlpack/core/kernels/epanechnikov_kernel_impl.hpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/mlpack/core/kernels/epanechnikov_kernel_impl.hpp b/src/mlpack/core/kernels/epanechnikov_kernel_impl.hpp index 15c65ebb7b..eee34daaee 100644 --- a/src/mlpack/core/kernels/epanechnikov_kernel_impl.hpp +++ b/src/mlpack/core/kernels/epanechnikov_kernel_impl.hpp @@ -58,7 +58,6 @@ double EpanechnikovKernel::ConvolutionIntegral(const VecTypeA& a, (3.0 * bandwidth) + 2.0 * distance * distance * distance / (3.0 * bandwidth * bandwidth) - std::pow(distance, 5.0) / (30.0 * std::pow(bandwidth, 4.0))); - break; case 2: return 1.0 / volumeSquared * ((2.0 / 3.0 * bandwidth * bandwidth - distance * distance) * @@ -67,12 +66,10 @@ double EpanechnikovKernel::ConvolutionIntegral(const VecTypeA& a, (distance / 6.0 + 2.0 / 9.0 * distance * std::pow(distance / bandwidth, 2.0) - distance / 72.0 * std::pow(distance / bandwidth, 4.0))); - break; default: Log::Fatal << "EpanechnikovKernel::ConvolutionIntegral(): dimension " << a.n_rows << " not supported."; return -1.0; // This line will not execute. - break; } } From 5c6723a5c6fc437aa7b4cb5057803134c97f4b77 Mon Sep 17 00:00:00 2001 From: Marcus Edel Date: Sat, 10 Apr 2021 19:19:44 +0200 Subject: [PATCH 05/89] Remove unreachable spherical kernel code. --- src/mlpack/core/kernels/spherical_kernel.hpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/mlpack/core/kernels/spherical_kernel.hpp b/src/mlpack/core/kernels/spherical_kernel.hpp index 13fc549f49..48fd9365a9 100644 --- a/src/mlpack/core/kernels/spherical_kernel.hpp +++ b/src/mlpack/core/kernels/spherical_kernel.hpp @@ -56,7 +56,7 @@ class SphericalKernel * @tparam VecTypeB Type of second vector. * @param a First vector. * @param b Second vector. - * @return the convolution integral value. + * @return The convolution integral value. */ template double ConvolutionIntegral(const VecTypeA& a, const VecTypeB& b) const @@ -72,17 +72,14 @@ class SphericalKernel { case 1: return 1.0 / volumeSquared * (2.0 * bandwidth - distance); - break; case 2: return 1.0 / volumeSquared * (2.0 * bandwidth * bandwidth * acos(distance/(2.0 * bandwidth)) - distance / 4.0 * sqrt(4.0*bandwidth*bandwidth-distance*distance)); - break; default: Log::Fatal << "The spherical kernel does not support convolution\ integrals above dimension two, yet..." << std::endl; return -1.0; - break; } } double Normalizer(size_t dimension) const From 01e522906681de578ade4bba84c3cd9a822411b2 Mon Sep 17 00:00:00 2001 From: Marcus Edel Date: Sat, 10 Apr 2021 19:27:59 +0200 Subject: [PATCH 06/89] Handle potential division by zero issue and minor style fixes. --- .../simple_tolerance_termination.hpp | 62 ++++++++++--------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/src/mlpack/methods/amf/termination_policies/simple_tolerance_termination.hpp b/src/mlpack/methods/amf/termination_policies/simple_tolerance_termination.hpp index 81630e14bf..191278bf7f 100644 --- a/src/mlpack/methods/amf/termination_policies/simple_tolerance_termination.hpp +++ b/src/mlpack/methods/amf/termination_policies/simple_tolerance_termination.hpp @@ -79,7 +79,7 @@ class SimpleToleranceTermination WH = W * H; - // compute residue + // Compute residue. residueOld = residue; size_t n = V->n_rows; size_t m = V->n_cols; @@ -99,48 +99,51 @@ class SimpleToleranceTermination } } } - residue = sum / count; + + residue = sum; + if (count > 0) + residue /= count; residue = sqrt(residue); - // increment iteration count + // Increment iteration count. iteration++; Log::Info << "Iteration " << iteration << "; residue " << ((residueOld - residue) / residueOld) << ".\n"; - // if residue tolerance is not satisfied + // If residue tolerance is not satisfied. if ((residueOld - residue) / residueOld < tolerance && iteration > 4) { - // check if this is a first of successive drops + // Check if this is a first of successive drops. if (reverseStepCount == 0 && isCopy == false) { - // store a copy of W and H matrix + // Store a copy of W and H matrix. isCopy = true; this->W = W; this->H = H; - // store residue values + // Store residue values. c_index = residue; c_indexOld = residueOld; } - // increase successive drop count + // Increase successive drop count. reverseStepCount++; } - // if tolerance is satisfied + // If tolerance is satisfied. else { - // initialize successive drop count + // Initialize successive drop count. reverseStepCount = 0; - // if residue is droped below minimum scrap stored values + // If residue is droped below minimum scrap stored values. if (residue <= c_indexOld && isCopy == true) { isCopy = false; } } - // check if termination criterion is met + // Check if termination criterion is met. if (reverseStepCount == reverseStepTolerance || iteration > maxIterations) { - // if stored values are present replace them with current value as they - // represent the minimum residue point + // If stored values are present replace them with current value as they + // represent the minimum residue point. if (isCopy) { W = this->W; @@ -149,49 +152,50 @@ class SimpleToleranceTermination } return true; } - else return false; + + return false; } - //! Get current value of residue + //! Get current value of residue. const double& Index() const { return residue; } - //! Get current iteration count + //! Get current iteration count. const size_t& Iteration() const { return iteration; } - //! Access upper limit of iteration count + //! Access upper limit of iteration count. const size_t& MaxIterations() const { return maxIterations; } size_t& MaxIterations() { return maxIterations; } - //! Access tolerance value + //! Access tolerance value. const double& Tolerance() const { return tolerance; } double& Tolerance() { return tolerance; } private: - //! tolerance + //! Locally-stored tolerance. double tolerance; - //! iteration threshold + //! Locally-stored iteration threshold. size_t maxIterations; - //! pointer to matrix being factorized + //! Pointer to matrix being factorized. const MatType* V; - //! current iteration count + //! Current iteration count. size_t iteration; - //! residue values + //! Locally-stored residue values. double residueOld; double residue; - //! tolerance on successive residue drops + //! Tolerance on successive residue drops. size_t reverseStepTolerance; - //! successive residue drops + //! Successive residue drops. size_t reverseStepCount; - //! indicates whether a copy of information is available which corresponds to - //! minimum residue point + //! Indicates whether a copy of information is available which corresponds to + //! minimum residue point. bool isCopy; - //! variables to store information of minimum residue poi + //! Variables to store information of minimum residue poi. arma::mat W; arma::mat H; double c_indexOld; From 1cda50f3275d51182098460d06b222b1bb2f874d Mon Sep 17 00:00:00 2001 From: Marcus Edel Date: Sat, 10 Apr 2021 19:30:42 +0200 Subject: [PATCH 07/89] Handle potential division by zero issue. --- src/mlpack/core/metrics/bleu_impl.hpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/mlpack/core/metrics/bleu_impl.hpp b/src/mlpack/core/metrics/bleu_impl.hpp index 38e9b29437..27119bef1e 100644 --- a/src/mlpack/core/metrics/bleu_impl.hpp +++ b/src/mlpack/core/metrics/bleu_impl.hpp @@ -178,7 +178,10 @@ ElemType BLEU::Evaluate( else geometricMean = 0.0; - ratio = ElemType(translationLength) / referenceLength; + ratio = ElemType(translationLength); + if (referenceLength > 0) + ration /= referenceLength; + brevityPenalty = (ratio > 1.0) ? 1.0 : std::exp(1.0 - 1.0 / ratio); bleuScore = geometricMean * brevityPenalty; From cf9bd906dcfcff9115812e3d3e0b0be554cb38f9 Mon Sep 17 00:00:00 2001 From: Marcus Edel Date: Sat, 10 Apr 2021 20:28:36 +0200 Subject: [PATCH 08/89] Fix parameter name. --- src/mlpack/core/metrics/bleu_impl.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/core/metrics/bleu_impl.hpp b/src/mlpack/core/metrics/bleu_impl.hpp index 27119bef1e..f81d62c69a 100644 --- a/src/mlpack/core/metrics/bleu_impl.hpp +++ b/src/mlpack/core/metrics/bleu_impl.hpp @@ -180,7 +180,7 @@ ElemType BLEU::Evaluate( ratio = ElemType(translationLength); if (referenceLength > 0) - ration /= referenceLength; + ratio /= referenceLength; brevityPenalty = (ratio > 1.0) ? 1.0 : std::exp(1.0 - 1.0 / ratio); bleuScore = geometricMean * brevityPenalty; From ff5a86d08787ca15d477ffb1aca9bc3f0f9a425b Mon Sep 17 00:00:00 2001 From: Marcus Edel Date: Sat, 10 Apr 2021 20:43:57 +0200 Subject: [PATCH 09/89] Rethrow the original exception object using an empty throw. --- src/mlpack/methods/fastmks/fastmks_main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/methods/fastmks/fastmks_main.cpp b/src/mlpack/methods/fastmks/fastmks_main.cpp index 3b784f6d04..1638f64922 100644 --- a/src/mlpack/methods/fastmks/fastmks_main.cpp +++ b/src/mlpack/methods/fastmks/fastmks_main.cpp @@ -262,7 +262,7 @@ static void mlpackMain() // Delete the memory, if needed. if (IO::HasParam("reference")) delete model; - throw e; + throw; } } From 270dec8fb4eabdcb6bc2639d908d5948c991ce5a Mon Sep 17 00:00:00 2001 From: Marcus Edel Date: Mon, 12 Apr 2021 03:48:35 +0200 Subject: [PATCH 10/89] Catch2 will mark the test as failed if an exception is thrown. --- src/mlpack/tests/serialization.hpp | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/src/mlpack/tests/serialization.hpp b/src/mlpack/tests/serialization.hpp index b88ff5e957..93b293b451 100644 --- a/src/mlpack/tests/serialization.hpp +++ b/src/mlpack/tests/serialization.hpp @@ -29,14 +29,12 @@ void TestArmadilloSerialization(arma::Cube& x) // Use type_info name to get unique file name for serialization test files. std::string fileName = FilterFileName(typeid(IArchiveType).name()); std::ofstream ofs(fileName, std::ios::binary); - bool success = true; { OArchiveType o(ofs); o(CEREAL_NVP(x)); } - REQUIRE(success == true); ofs.close(); // Now load it. @@ -52,8 +50,6 @@ void TestArmadilloSerialization(arma::Cube& x) remove(fileName.c_str()); - REQUIRE(success == true); - REQUIRE(x.n_rows == orig.n_rows); REQUIRE(x.n_cols == orig.n_cols); REQUIRE(x.n_elem_slice == orig.n_elem_slice); @@ -99,19 +95,16 @@ void TestArmadilloSerialization(MatType& x) // First save it. std::string fileName = FilterFileName(typeid(IArchiveType).name()); std::ofstream ofs(fileName, std::ios::binary); - bool success = true; { OArchiveType o(ofs); o(CEREAL_NVP(x)); } - REQUIRE(success == true); ofs.close(); // Now load it. MatType orig(x); - success = true; std::ifstream ifs(fileName, std::ios::binary); { @@ -122,8 +115,6 @@ void TestArmadilloSerialization(MatType& x) remove(fileName.c_str()); - REQUIRE(success == true); - REQUIRE(x.n_rows == orig.n_rows); REQUIRE(x.n_cols == orig.n_cols); REQUIRE(x.n_elem == orig.n_elem); @@ -156,7 +147,6 @@ void SerializeObject(T& t, T& newT) { std::string fileName = FilterFileName(typeid(T).name()); std::ofstream ofs(fileName, std::ios::binary); - bool success = true; { OArchiveType o(ofs); @@ -166,8 +156,6 @@ void SerializeObject(T& t, T& newT) } ofs.close(); - REQUIRE(success == true); - std::ifstream ifs(fileName, std::ios::binary); { @@ -178,8 +166,6 @@ void SerializeObject(T& t, T& newT) ifs.close(); remove(fileName.c_str()); - - REQUIRE(success == true); } // Test mlpack serialization with all three archive types. @@ -200,7 +186,6 @@ void SerializePointerObject(T* t, T*& newT) { std::string fileName = FilterFileName(typeid(T).name()); std::ofstream ofs(fileName, std::ios::binary); - bool success = true; { OArchiveType o(ofs); @@ -208,8 +193,6 @@ void SerializePointerObject(T* t, T*& newT) } ofs.close(); - REQUIRE(success == true); - std::ifstream ifs(fileName, std::ios::binary); { @@ -218,8 +201,6 @@ void SerializePointerObject(T* t, T*& newT) } ifs.close(); remove(fileName.c_str()); - - REQUIRE(success == true); } template From 044c2a4fbdd05afa38f0efa364b5c8fdf9fbe879 Mon Sep 17 00:00:00 2001 From: Marcus Edel Date: Mon, 12 Apr 2021 03:48:53 +0200 Subject: [PATCH 11/89] Catch potential division by zero. --- src/mlpack/core/tree/rectangle_tree/x_tree_split_impl.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/mlpack/core/tree/rectangle_tree/x_tree_split_impl.hpp b/src/mlpack/core/tree/rectangle_tree/x_tree_split_impl.hpp index c977f0fecb..cc1119a845 100644 --- a/src/mlpack/core/tree/rectangle_tree/x_tree_split_impl.hpp +++ b/src/mlpack/core/tree/rectangle_tree/x_tree_split_impl.hpp @@ -435,7 +435,8 @@ bool XTreeSplit::SplitNonLeafNode(TreeType *tree, std::vector& relevels) bool useMinOverlapSplit = false; if (tiedOnOverlap) { - if (overlapBestAreaAxis / areaBestAreaAxis < MAX_OVERLAP) + if (MAX_OVERLAP > 0 && + overlapBestAreaAxis / areaBestAreaAxis < MAX_OVERLAP) { tree->numDescendants = 0; tree->bound.Clear(); From 08f0ad96d988d756f990579d0af6ddedf6615678 Mon Sep 17 00:00:00 2001 From: Marcus Edel Date: Tue, 13 Apr 2021 03:48:58 +0200 Subject: [PATCH 12/89] Remove remaining success parameter. --- src/mlpack/tests/serialization.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/mlpack/tests/serialization.hpp b/src/mlpack/tests/serialization.hpp index 93b293b451..361523f701 100644 --- a/src/mlpack/tests/serialization.hpp +++ b/src/mlpack/tests/serialization.hpp @@ -39,7 +39,6 @@ void TestArmadilloSerialization(arma::Cube& x) // Now load it. arma::Cube orig(x); - success = true; std::ifstream ifs(fileName, std::ios::binary); { From 17b05d054d15684f155edeed32f5c84addac371e Mon Sep 17 00:00:00 2001 From: Marcus Edel Date: Wed, 14 Apr 2021 04:33:52 +0200 Subject: [PATCH 13/89] Catch potential division by zero. --- src/mlpack/core/tree/rectangle_tree/x_tree_split_impl.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/core/tree/rectangle_tree/x_tree_split_impl.hpp b/src/mlpack/core/tree/rectangle_tree/x_tree_split_impl.hpp index cc1119a845..73a532b03e 100644 --- a/src/mlpack/core/tree/rectangle_tree/x_tree_split_impl.hpp +++ b/src/mlpack/core/tree/rectangle_tree/x_tree_split_impl.hpp @@ -435,7 +435,7 @@ bool XTreeSplit::SplitNonLeafNode(TreeType *tree, std::vector& relevels) bool useMinOverlapSplit = false; if (tiedOnOverlap) { - if (MAX_OVERLAP > 0 && + if (areaBestAreaAxis > 0 && overlapBestAreaAxis / areaBestAreaAxis < MAX_OVERLAP) { tree->numDescendants = 0; From a5b632c761c92e59cd7ac0e2ec7785df12cbaa9f Mon Sep 17 00:00:00 2001 From: Aakash Kaushik Date: Thu, 22 Apr 2021 00:11:32 +0530 Subject: [PATCH 14/89] tests/cmake config --- src/mlpack/tests/CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/mlpack/tests/CMakeLists.txt b/src/mlpack/tests/CMakeLists.txt index de0f56d8df..9761cd6df1 100644 --- a/src/mlpack/tests/CMakeLists.txt +++ b/src/mlpack/tests/CMakeLists.txt @@ -1,3 +1,5 @@ +include(CTest) + # mlpack test executable. add_executable(mlpack_test activation_functions_test.cpp From 22eb09e5d4811b34b57f61e6feb23b8d3c50b6fc Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Fri, 23 Apr 2021 22:10:38 -0400 Subject: [PATCH 15/89] Don't build mlpack_test as part of make. --- HISTORY.md | 3 +++ README.md | 8 +++++++- doc/guide/build.hpp | 24 ++++++++++++++---------- src/mlpack/tests/CMakeLists.txt | 1 + 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 2f7aab9538..528c1746f5 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -46,6 +46,9 @@ * Fix Python binding build when the CMake variable `USE_OPENMP` is set to `OFF` (#2884). + * The `mlpack_test` target is no longer built as part of `make all`. Use + `make mlpack_test` to build the tests. + ### mlpack 3.4.2 ###### 2020-10-26 * Added Mean Absolute Percentage Error. diff --git a/README.md b/README.md index 21b9e09e7e..b8fffc8ca8 100644 --- a/README.md +++ b/README.md @@ -234,7 +234,7 @@ also be built. OpenMP will be used for parallelization when possible by default. Once CMake is configured, building the library is as simple as typing 'make'. -This will build all library components as well as 'mlpack_test'. +This will build all library components and bindings. $ make @@ -243,6 +243,12 @@ of the build can be specified: $ make mlpack_pca mlpack_knn mlpack_kfn +If you want to build the tests, just make the `mlpack_test` target, and use +`ctest` to run the tests: + + $ make mlpack_test + $ ctest . + If the build fails and you cannot figure out why, register an account on Github and submit an issue. The mlpack developers will quickly help you figure it out: diff --git a/doc/guide/build.hpp b/doc/guide/build.hpp index 9356f725cc..a87fe8fe1f 100644 --- a/doc/guide/build.hpp +++ b/doc/guide/build.hpp @@ -170,7 +170,8 @@ The full list of options mlpack allows: - PROFILE=(ON/OFF): compile with profiling symbols (default OFF) - ARMA_EXTRA_DEBUG=(ON/OFF): compile with extra Armadillo debugging symbols (default OFF) - - BUILD_TESTS=(ON/OFF): compile the \c mlpack_test program (default ON) + - BUILD_TESTS=(ON/OFF): compile the \c mlpack_test program when `make` is run + (default ON) - BUILD_CLI_EXECUTABLES=(ON/OFF): compile the mlpack command-line executables (i.e. \c mlpack_knn, \c mlpack_kfn, \c mlpack_logistic_regression, etc.) (default ON) @@ -225,14 +226,10 @@ and libraries. These also use the '-D' flag. @section build_build Building mlpack Once CMake is configured, building the library is as simple as typing 'make'. -This will build all library components as well as 'mlpack_test'. +This will build all library components. @code $ make -Scanning dependencies of target mlpack -[ 1%] Building CXX object -src/mlpack/CMakeFiles/mlpack.dir/core/optimizers/aug_lagrangian/aug_lagrangian_test_functions.cpp.o -<...> @endcode It's often useful to specify \c -jN to the \c make command, which will build on @@ -247,17 +244,24 @@ $ make mlpack_pca mlpack_knn mlpack_kfn @endcode One particular component of interest is mlpack_test, which runs the mlpack test -suite. You can build this component with +suite. This is not built when @c make is run. You can build this component +with @code $ make mlpack_test @endcode We use Catch2 to write our tests. -To run all tests, you can simply run: +To run all tests, you can simply use CTest: @code -$ ./bin/mlpack_test +$ ctest . +@endcode + +Or, you can run the test suite manually: + +@code +$ bin/mlpack_test @endcode To run all tests in a particular file you can run: @@ -266,7 +270,7 @@ To run all tests in a particular file you can run: $ ./bin/mlpack_test "[testname]" @endcode -where testname is the name of the test suite. +where testname is the name of the test suite. For example to run all collaborative filtering tests implemented in cf_test.cpp you can run: @code diff --git a/src/mlpack/tests/CMakeLists.txt b/src/mlpack/tests/CMakeLists.txt index de0f56d8df..b6d4b937cc 100644 --- a/src/mlpack/tests/CMakeLists.txt +++ b/src/mlpack/tests/CMakeLists.txt @@ -1,5 +1,6 @@ # mlpack test executable. add_executable(mlpack_test + EXCLUDE_FROM_ALL activation_functions_test.cpp adaboost_test.cpp akfn_test.cpp From eb80b2da5ed340464d89690ab89c7d83bb5b0782 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sat, 24 Apr 2021 14:27:52 +0200 Subject: [PATCH 16/89] The Autodownloader. This pull request provides the Autodownloader for mlpack dependencies. It can be used with current and future dependencies. This one is born from working with #2531, with help from @rcurtin and @zoq. It is ready to merge, with minor reviews are always welcome. Signed-off-by: Omar Shrit --- CMake/Autodownload.cmake | 56 ++++++++++ CMakeLists.txt | 220 +++++++++++++++------------------------ 2 files changed, 140 insertions(+), 136 deletions(-) create mode 100644 CMake/Autodownload.cmake diff --git a/CMake/Autodownload.cmake b/CMake/Autodownload.cmake new file mode 100644 index 0000000000..b82ab001b9 --- /dev/null +++ b/CMake/Autodownload.cmake @@ -0,0 +1,56 @@ +## This function auto-downloads mlpack dependencies. +## You need to pass the LINK to download from, the name of +## the dependency, and the name of the compressed package such as +## armadillo.tar.gz +## At each download, this module sets a GENERIC_INCLUDE_DIR path, +## which means that you need to set the main path for the include +## directories for each package. +## Note that, the package should be compressed only as .tar.gz + +macro(get_deps LINK DEPS_NAME PACKAGE) + if (NOT EXISTS "${CMAKE_BINARY_DIR}/deps/${PACKAGE}") + file(DOWNLOAD ${LINK} + "${CMAKE_BINARY_DIR}/deps/${PACKAGE}" + STATUS DOWNLOAD_STATUS_LIST LOG DOWNLOAD_LOG + SHOW_PROGRESS) + list(GET DOWNLOAD_STATUS_LIST 0 DOWNLOAD_STATUS) + if (DOWNLOAD_STATUS EQUAL 0) + execute_process(COMMAND ${CMAKE_COMMAND} -E + tar xf "${CMAKE_BINARY_DIR}/deps/${PACKAGE}" + WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/deps/") + else () + list(GET DOWNLOAD_STATUS_LIST 1 DOWNLOAD_ERROR) + message(FATAL_ERROR + "Could not download ${DEPS_NAME}! Error code ${DOWNLOAD_STATUS}: ${DOWNLOAD_ERROR}! Error log: ${DOWNLOAD_LOG}") + endif() + endif() + # Get the name of the directory. + file (GLOB DIRECTORIES RELATIVE "${CMAKE_BINARY_DIR}/deps/" + "${CMAKE_BINARY_DIR}/deps/${DEPS_NAME}*.*") + # Clean this line when boost is removed. + if (${DEPS_NAME} MATCHES "boost") + file (GLOB DIRECTORIES RELATIVE "${CMAKE_BINARY_DIR}/deps/" + "${CMAKE_BINARY_DIR}/deps/${DEPS_NAME}*_*") + elseif(${DEPS_NAME} MATCHES "stb") + file (GLOB DIRECTORIES RELATIVE "${CMAKE_BINARY_DIR}/deps/" + "${CMAKE_BINARY_DIR}/deps/${DEPS_NAME}") + endif() + # list(FILTER) is not available on 3.5 or older, but try to keep + # configuring without filtering the list anyway + # (it works only if the file is present as .tar.gz). + if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.6.0") + list(FILTER DIRECTORIES EXCLUDE REGEX ".*\.tar\.gz") + endif () + list(LENGTH DIRECTORIES DIRECTORIES_LEN) + if (DIRECTORIES_LEN GREATER 0) + list(GET DIRECTORIES 0 DEPENDENCY_DIR) + set(GENERIC_INCLUDE_DIR "${CMAKE_BINARY_DIR}/deps/${DEPENDENCY_DIR}/include") + # Clean this line when boost is removed. + if (${DEPS_NAME} MATCHES "boost") + set(Boost_INCLUDE_DIR "${CMAKE_BINARY_DIR}/deps/${DEPENDENCY_DIR}/") + endif() + else () + message(FATAL_ERROR + "Problem unpacking ${DEPS_NAME}! Expected only one directory ${DEPS_NAME};. Try to remove the directory ${CMAKE_BINARY_DIR}/deps and reconfigure.") + endif () +endmacro() diff --git a/CMakeLists.txt b/CMakeLists.txt index 0aa70cb74a..588952a236 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,7 @@ project(mlpack C CXX) include(CMake/cotire.cmake) include(CMake/CheckHash.cmake) +include(CMake/Autodownload.cmake) # First, define all the compilation options. # We default to debugging mode for developers. @@ -24,15 +25,26 @@ set(ENSMALLEN_VERSION "2.10.0") set(BOOST_VERSION "1.58") set(CEREAL_VERSION "1.1.2") +# If BUILD_SHARED_LIBS is OFF then the mlpack library will be built statically. +# In addition, all mlpack CLI bindings will be linked statically as well. if (WIN32) option(BUILD_SHARED_LIBS - "Compile shared libraries (if OFF, static libraries are compiled)." OFF) + "Compile shared libraries (if OFF, static libraries and binaries are compiled)." OFF) set(DLL_COPY_DIRS "" CACHE STRING "List of directories (separated by ';') containing DLLs to copy for runtime.") set(DLL_COPY_LIBS "" CACHE STRING "List of DLLs (separated by ';') that should be copied for runtime.") -else () +else() option(BUILD_SHARED_LIBS - "Compile shared libraries (if OFF, static libraries are compiled)." ON) + "Compile shared libraries (if OFF, static libraries and binaries are compiled)." ON) +endif() + +# Support preference of static libs by adjusting CMAKE_FIND_LIBRARY_SUFFIXES. +if (NOT BUILD_SHARED_LIBS) + if(WIN32) + list(INSERT CMAKE_FIND_LIBRARY_SUFFIXES 0 .lib .a) + else() + set(CMAKE_FIND_LIBRARY_SUFFIXES .a) + endif() endif() # Detect whether the user passed BUILD_PYTHON_BINDINGS in order to determine if @@ -93,12 +105,6 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) # Include modules in the CMake directory. set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMake") -# Disable any downloads if needed. -if (DISABLE_DOWNLOADS) - set(DOWNLOAD_ENSMALLEN OFF) - set(DOWNLOAD_STB_IMAGE OFF) -endif () - # If we are on a Unix-like system, use the GNU install directories module. # Otherwise set the values manually. if (UNIX) @@ -112,12 +118,12 @@ else () endif () # This is as of yet unused. -#option(PGO "Use profile-guided optimization if not a debug build" ON) +# option(PGO "Use profile-guided optimization if not a debug build" ON) # Set the CFLAGS and CXXFLAGS depending on the options the user specified. # Only GCC-like compilers support -Wextra, and other compilers give tons of # output for -Wall, so only -Wall and -Wextra on GCC. -if(CMAKE_COMPILER_IS_GNUCC OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") +if (CMAKE_COMPILER_IS_GNUCC OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") # Ensure that we can't compile with clang 3.4, since this causes strange # issues. if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.5) @@ -144,7 +150,7 @@ endif () # If we are using MINGW, we need sections and big-obj, otherwise we create too # many sections. -if(CMAKE_COMPILER_IS_GNUCC AND WIN32) +if (CMAKE_COMPILER_IS_GNUCC AND WIN32) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffunction-sections -fdata-sections -Wa,-mbig-obj") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffunction-sections -fdata-sections -Wa,-mbig-obj") endif() @@ -153,7 +159,7 @@ endif() # OS (at least on some systems). Further, gcc sometimes optimizes calls to # math.h functions, making -lm unnecessary with gcc, but it may still be # necessary with clang. -if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") +if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") if (APPLE) # Detect OS X version. Use '/usr/bin/sw_vers -productVersion' to # extract V from '10.V.x'. @@ -166,7 +172,7 @@ if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") # OSX Lion (10.7) and OS X Mountain Lion (10.8) doesn't automatically # select the right stdlib. - if(${MACOSX_VERSION} LESS 9) + if (${MACOSX_VERSION} LESS 9) set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++") set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -stdlib=libc++") @@ -187,14 +193,14 @@ endif() # If we're using gcc, then we need to link against pthreads to use std::thread, # which we do in the tests. -if(CMAKE_COMPILER_IS_GNUCC) +if (CMAKE_COMPILER_IS_GNUCC) find_package(Threads) set(COMPILER_SUPPORT_LIBRARIES ${COMPILER_SUPPORT_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) endif() # Debugging CFLAGS. Turn optimizations off; turn debugging symbols on. -if(DEBUG) +if (DEBUG) if (NOT MSVC) add_definitions(-DDEBUG) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 -ftemplate-backtrace-limit=0") @@ -203,10 +209,10 @@ if(DEBUG) # mlpack uses it's own mlpack::backtrace class based on Binary File Descriptor # and linux Dynamic Loader and more portable version in future - if(CMAKE_SYSTEM_NAME STREQUAL "Linux") + if (CMAKE_SYSTEM_NAME STREQUAL "Linux") find_package(Bfd) find_package(LibDL) - if(LIBBFD_FOUND AND LIBDL_FOUND) + if (LIBBFD_FOUND AND LIBDL_FOUND) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -rdynamic") set(MLPACK_INCLUDE_DIRS ${MLPACK_INCLUDE_DIRS} ${LIBBFD_INCLUDE_DIRS} ${LIBDL_INCLUDE_DIRS}) @@ -230,19 +236,19 @@ else() endif() # Profiling CFLAGS. Turn profiling information on. -if(CMAKE_COMPILER_IS_GNUCC AND PROFILE) +if (CMAKE_COMPILER_IS_GNUCC AND PROFILE) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pg") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pg") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pg") endif() # If the user asked for running test cases with verbose output, turn that on. -if(TEST_VERBOSE) +if (TEST_VERBOSE) add_definitions(-DTEST_VERBOSE) endif() # If the user asked for extra Armadillo debugging output, turn that on. -if(ARMA_EXTRA_DEBUG) +if (ARMA_EXTRA_DEBUG) add_definitions(-DARMA_EXTRA_DEBUG) endif() @@ -254,137 +260,72 @@ endif() # ARMADILLO_INCLUDE_DIRS - directories necessary for Armadillo includes # BOOST_ROOT - root of Boost installation # BOOST_INCLUDEDIR - include directory for Boost +# CEREAL_INCLUDE_DIR - include directory for cereal # ENSMALLEN_INCLUDE_DIR - include directory for ensmallen # STB_IMAGE_INCLUDE_DIR - include directory for STB image library # MATHJAX_ROOT - root of MathJax installation -find_package(Armadillo "${ARMADILLO_VERSION}" REQUIRED) +if (DISABLE_DOWNLOADS) + find_package(Armadillo "${ARMADILLO_VERSION}" REQUIRED) +else() + find_package(Armadillo "${ARMADILLO_VERSION}") + if (NOT ARMADILLO_FOUND) + get_deps(http://files.mlpack.org/armadillo-10.3.0.tar.gz armadillo armadillo-10.3.0.tar.gz) + set(ARMADILLO_INCLUDE_DIR ${GENERIC_INCLUDE_DIR}) + find_package(Armadillo REQUIRED) + endif() +endif() # Include directories for the previous dependencies. set(MLPACK_INCLUDE_DIRS ${MLPACK_INCLUDE_DIRS} ${ARMADILLO_INCLUDE_DIRS}) set(MLPACK_LIBRARIES ${MLPACK_LIBRARIES} ${ARMADILLO_LIBRARIES}) # Find stb_image.h and stb_image_write.h. -find_package(StbImage) -# Download stb_image for image loading. -if (NOT STB_IMAGE_FOUND) - if (DOWNLOAD_STB_IMAGE) - set(STB_DIR "stb") - install(DIRECTORY DESTINATION "${CMAKE_BINARY_DIR}/deps/${STB_DIR}") - file(DOWNLOAD http://mlpack.org/files/stb-2.22/stb_image.h - "${CMAKE_BINARY_DIR}/deps/${STB_DIR}/stb_image.h" - STATUS STB_IMAGE_DOWNLOAD_STATUS_LIST LOG STB_IMAGE_DOWNLOAD_LOG - SHOW_PROGRESS) - list(GET STB_IMAGE_DOWNLOAD_STATUS_LIST 0 STB_IMAGE_DOWNLOAD_STATUS) - file(DOWNLOAD http://mlpack.org/files/stb-1.13/stb_image_write.h - "${CMAKE_BINARY_DIR}/deps/${STB_DIR}/stb_image_write.h" - STATUS STB_IMAGE_WRITE_DOWNLOAD_STATUS_LIST - LOG STB_IMAGE_WRITE_DOWNLOAD_LOG - SHOW_PROGRESS) - list(GET STB_IMAGE_WRITE_DOWNLOAD_STATUS_LIST 0 - STB_IMAGE_WRITE_DOWNLOAD_STATUS) - if (STB_IMAGE_DOWNLOAD_STATUS EQUAL 0 AND - STB_IMAGE_WRITE_DOWNLOAD_STATUS EQUAL 0) - check_hash (http://mlpack.org/files/stb/hash.md5 "${CMAKE_BINARY_DIR}/deps/${STB_DIR}" - HASH_CHECK_FAIL) - if (HASH_CHECK_FAIL EQUAL 0) - set(MLPACK_INCLUDE_DIRS ${MLPACK_INCLUDE_DIRS} - "${CMAKE_BINARY_DIR}/deps/${STB_DIR}/") - message(STATUS - "Successfully downloaded stb into ${CMAKE_BINARY_DIR}/deps/${STB_DIR}/") - # Now we have to also ensure these header files get installed. - install(FILES "${CMAKE_BINARY_DIR}/deps/${STB_DIR}/stb_image.h" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") - install(FILES "${CMAKE_BINARY_DIR}/deps/${STB_DIR}/stb_image_write.h" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") - add_definitions(-DHAS_STB) - set(STB_AVAILABLE "1") - else () - message(WARNING - "stb/stb_image.h is not installed. Image utilities will not be available!") - endif () - else () - file(REMOVE_RECURSE "${CMAKE_BINARY_DIR}/deps/${STB_DIR}/") - list(GET STB_IMAGE_DOWNLOAD_STATUS_LIST 1 STB_DOWNLOAD_ERROR) - message(WARNING - "Could not download stb! Error code ${STB_DOWNLOAD_STATUS}: ${STB_DOWNLOAD_ERROR}! Error log: ${STB_DOWNLOAD_LOG}") - message(WARNING - "stb/stb_image.h is not installed. Image utilities will not be available!") - endif () - else () - message(WARNING - "stb/stb_image.h is not installed. Image utilities will not be available!") - endif () -else () - # Already has STB installed. +if (DISABLE_DOWNLOADS) + find_package(StbImage) +else() + find_package(StbImage) + if (NOT STB_IMAGE_FOUND) + get_deps(http://mlpack.org/files/stb.tar.gz stb stb.tar.gz) + set(STB_IMAGE_INCLUDE_DIR ${GENERIC_INCLUDE_DIR}) + find_package(StbImage REQUIRED) + endif() +endif() + +if (STB_IMAGE_FOUND) add_definitions(-DHAS_STB) - set(MLPACK_INCLUDE_DIRS ${MLPACK_INCLUDE_DIRS} ${STB_IMAGE_INCLUDE_DIR}) set(STB_AVAILABLE "1") -endif () +endif() +set(MLPACK_INCLUDE_DIRS ${MLPACK_INCLUDE_DIRS} "${STB_IMAGE_INCLUDE_DIR}") # Find ensmallen. -# Once ensmallen is readily available in package repos, the automatic downloader -# here can be removed. -find_package(Ensmallen "${ENSMALLEN_VERSION}") -if (NOT ENSMALLEN_FOUND) - if (DOWNLOAD_ENSMALLEN) - file(DOWNLOAD http://www.ensmallen.org/files/ensmallen-latest.tar.gz - "${CMAKE_BINARY_DIR}/deps/ensmallen-latest.tar.gz" - STATUS ENS_DOWNLOAD_STATUS_LIST LOG ENS_DOWNLOAD_LOG - SHOW_PROGRESS) - list(GET ENS_DOWNLOAD_STATUS_LIST 0 ENS_DOWNLOAD_STATUS) - if (ENS_DOWNLOAD_STATUS EQUAL 0) - execute_process(COMMAND ${CMAKE_COMMAND} -E - tar xzf "${CMAKE_BINARY_DIR}/deps/ensmallen-latest.tar.gz" - WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/deps/") +if (DISABLE_DOWNLOADS) + find_package(Ensmallen "${ENSMALLEN_VERSION}" REQUIRED) +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) + set(ENSMALLEN_INCLUDE_DIR ${GENERIC_INCLUDE_DIR}) + find_package(Ensmallen REQUIRED) + endif() +endif() +set(MLPACK_INCLUDE_DIRS ${MLPACK_INCLUDE_DIRS} "${ENSMALLEN_INCLUDE_DIR}") - # Get the name of the directory. - file (GLOB ENS_DIRECTORIES RELATIVE "${CMAKE_BINARY_DIR}/deps/" - "${CMAKE_BINARY_DIR}/deps/ensmallen-[0-9]*.[0-9]*.[0-9]*") - # list(FILTER) is not available on 3.5 or older, but try to keep - # configuring without filtering the list anyway (it might work if only - # the file ensmallen-latest.tar.gz is present. - if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.6.0") - list(FILTER ENS_DIRECTORIES EXCLUDE REGEX "ensmallen-.*\.tar\.gz") - endif () - list(LENGTH ENS_DIRECTORIES ENS_DIRECTORIES_LEN) - if (ENS_DIRECTORIES_LEN EQUAL 1) - list(GET ENS_DIRECTORIES 0 ENSMALLEN_INCLUDE_DIR) - set(MLPACK_INCLUDE_DIRS ${MLPACK_INCLUDE_DIRS} - "${CMAKE_BINARY_DIR}/deps/${ENSMALLEN_INCLUDE_DIR}/include") - message(STATUS - "Successfully downloaded ensmallen into ${CMAKE_BINARY_DIR}/deps/${ENSMALLEN_INCLUDE_DIR}/") - - # Now we have to also ensure these header files get installed. - install(DIRECTORY "${CMAKE_BINARY_DIR}/deps/${ENSMALLEN_INCLUDE_DIR}/include/ensmallen_bits/" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/ensmallen_bits") - install(FILES "${CMAKE_BINARY_DIR}/deps/${ENSMALLEN_INCLUDE_DIR}/include/ensmallen.hpp" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") - else () - message(FATAL_ERROR "Problem unpacking ensmallen! Expected only one directory ensmallen-x.y.z/; found ${ENS_DIRECTORIES}. Try removing the directory ${CMAKE_BINARY_DIR}/deps and reconfiguring.") - endif () - else () - list(GET ENS_DOWNLOAD_STATUS_LIST 1 ENS_DOWNLOAD_ERROR) - message(FATAL_ERROR - "Could not download ensmallen! Error code ${ENS_DOWNLOAD_STATUS}: ${ENS_DOWNLOAD_ERROR}! Error log: ${ENS_DOWNLOAD_LOG}") - endif () - else () - # Release versions will have ensmallen packaged with the release so we can - # just reference that. - if (EXISTS "${CMAKE_SOURCE_DIR}/src/mlpack/core/optimizers/ensmallen/ensmallen.hpp") - set(MLPACK_INCLUDE_DIRS ${MLPACK_INCLUDE_DIRS} ${ARMADILLO_INCLUDE_DIRS} - "${CMAKE_SOURCE_DIR}/src/mlpack/core/optimizers/ensmallen") - else () - message(FATAL_ERROR - "Cannot find ensmallen headers! Try setting ENSMALLEN_INCLUDE_DIR!") - endif () - endif () -else () - set(MLPACK_INCLUDE_DIRS ${MLPACK_INCLUDE_DIRS} "${ENSMALLEN_INCLUDE_DIR}") -endif () - -find_package(cereal "${CEREAL_VERSION}" REQUIRED) +# Find cereal. +if (DISABLE_DOWNLOADS) + find_package(cereal "${CEREAL_VERSION}" REQUIRED) +else() + find_package(cereal "${CEREAL_VERSION}") + if (NOT CEREAL_FOUND) + get_deps(https://github.com/USCiLab/cereal/archive/refs/tags/v1.3.0.tar.gz cereal cereal-1.3.0.tar.gz) + set(CEREAL_INCLUDE_DIR ${GENERIC_INCLUDE_DIR}) + find_package(cereal REQUIRED) + endif() +endif() set(MLPACK_INCLUDE_DIRS ${MLPACK_INCLUDE_DIRS} ${CEREAL_INCLUDE_DIR}) # Unfortunately this configuration variable is necessary and will need to be # updated as time goes on and new versions are released. set(Boost_ADDITIONAL_VERSIONS - "1.76.0" "1.76" "1.75.0" "1.75" "1.74.0" "1.74" "1.73.0" "1.73" @@ -409,8 +350,15 @@ set(Boost_ADDITIONAL_VERSIONS # TODO for the brave: transition all mlpack's CMake to 'target-based modern # CMake'. Good luck! You'll need it. set(Boost_NO_BOOST_CMAKE 1) -find_package(Boost "${BOOST_VERSION}") - +if (DISABLE_DOWNLOADS) + find_package(Boost "${BOOST_VERSION}" REQUIRED) +else() + find_package(Boost "${BOOST_VERSION}") + if (NOT Boost_FOUND) + get_deps(https://dl.bintray.com/boostorg/release/1.75.0/source/boost_1_75_0.tar.gz boost boost_1_75_0.tar.gz) + find_package(Boost REQUIRED) + endif() +endif() set(MLPACK_INCLUDE_DIRS ${MLPACK_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS}) set(MLPACK_LIBRARIES ${MLPACK_LIBRARIES}) set(MLPACK_LIBRARY_DIRS ${MLPACK_LIBRARY_DIRS}) @@ -433,7 +381,7 @@ if (OPENMP_FOUND) add_definitions(-DHAS_OPENMP) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") - if(OpenMP_CXX_FOUND) + if (OpenMP_CXX_FOUND) set(MLPACK_LIBRARIES ${MLPACK_LIBRARIES} ${OpenMP_CXX_LIBRARIES}) endif () else () From 0020da5715e51b65be9b58a620a736c6c2e61a2d Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sat, 24 Apr 2021 14:31:37 +0200 Subject: [PATCH 17/89] Update boost version oops. Signed-off-by: Omar Shrit --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 588952a236..9a2c55aaa0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -326,6 +326,7 @@ set(MLPACK_INCLUDE_DIRS ${MLPACK_INCLUDE_DIRS} ${CEREAL_INCLUDE_DIR}) # Unfortunately this configuration variable is necessary and will need to be # updated as time goes on and new versions are released. set(Boost_ADDITIONAL_VERSIONS + "1.76.0" "1.76" "1.75.0" "1.75" "1.74.0" "1.74" "1.73.0" "1.73" From fb2bdbe15938ae43239e674bc495b62988d34aaf Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sat, 24 Apr 2021 14:48:21 +0200 Subject: [PATCH 18/89] Download all boost instead of a specific version. Signed-off-by: Omar Shrit --- .ci/linux-steps.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.ci/linux-steps.yaml b/.ci/linux-steps.yaml index f695c14fe1..18cd645043 100644 --- a/.ci/linux-steps.yaml +++ b/.ci/linux-steps.yaml @@ -21,7 +21,7 @@ steps: unset BOOST_ROOT echo "##vso[task.setvariable variable=BOOST_ROOT]"$BOOST_ROOT - sudo apt-get install -y --allow-unauthenticated libopenblas-dev g++ libboost1.70-dev xz-utils + sudo apt-get install -y --allow-unauthenticated libopenblas-dev g++ libboost-all-dev xz-utils if [ "$(binding)" == "python" ]; then export PYBIN=$(which python) @@ -41,7 +41,7 @@ steps: # Install cereal. wget https://github.com/USCiLab/cereal/archive/v1.3.0.tar.gz tar -xvzpf v1.3.0.tar.gz # Unpack into cereal-1.3.0/. - cd cereal-1.3.0/ + displayName: 'Install Build Dependencies' # Configure mlpack (CMake) From e89909cd6c695f5bf7dbe6b9ee23d0ac2ad528b5 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Sat, 24 Apr 2021 10:56:19 -0400 Subject: [PATCH 19/89] Explicitly build the tests. --- .ci/linux-steps.yaml | 4 ++-- .ci/macos-steps.yaml | 4 ++-- .ci/windows-steps.yaml | 1 + .github/workflows/main.yml | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/.ci/linux-steps.yaml b/.ci/linux-steps.yaml index f695c14fe1..9cec6f41ee 100644 --- a/.ci/linux-steps.yaml +++ b/.ci/linux-steps.yaml @@ -56,12 +56,12 @@ steps: displayName: 'CMake' # Build mlpack -- script: cd build && make +- script: cd build && make && make mlpack_test condition: eq(variables['CMakeArgs'], '-DDEBUG=ON -DPROFILE=OFF -DBUILD_PYTHON_BINDINGS=OFF -DBUILD_JULIA_BINDINGS=OFF -DBUILD_GO_BINDINGS=OFF -DBUILD_R_BINDINGS=OFF') displayName: 'Build' # Build mlpack -- script: cd build && make -j2 +- script: cd build && make -j2 && make -j2 mlpack_test condition: ne(variables['CMakeArgs'], '-DDEBUG=ON -DPROFILE=OFF -DBUILD_PYTHON_BINDINGS=OFF -DBUILD_JULIA_BINDINGS=OFF -DBUILD_GO_BINDINGS=OFF -DBUILD_R_BINDINGS=OFF') displayName: 'Build' diff --git a/.ci/macos-steps.yaml b/.ci/macos-steps.yaml index ce3c7796f5..48b15863a9 100644 --- a/.ci/macos-steps.yaml +++ b/.ci/macos-steps.yaml @@ -45,7 +45,7 @@ steps: displayName: 'CMake' # Build mlpack -- script: cd build && make -j2 +- script: cd build && make -j2 && make -j2 mlpack_test displayName: 'Build' # Run tests via ctest. @@ -65,4 +65,4 @@ steps: inputs: pathtoPublish: 'build/Testing/' artifactName: 'Tests' - displayName: 'Publish artifacts test results' \ No newline at end of file + displayName: 'Publish artifacts test results' diff --git a/.ci/windows-steps.yaml b/.ci/windows-steps.yaml index e2a9ed38e0..a6793997e6 100644 --- a/.ci/windows-steps.yaml +++ b/.ci/windows-steps.yaml @@ -88,6 +88,7 @@ steps: # Run tests via ctest. - bash: | cd build + cmake --build . --target mlpack_test -C Release CTEST_OUTPUT_ON_FAILURE=1 ctest -T Test -C Release . -j1 displayName: 'Run tests via ctest' diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 9fa25191a6..a9a2574072 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -68,7 +68,7 @@ jobs: - name: Build run: | - cd build && make -j2 + cd build && make -j2 && make -j2 mlpack_test - name: Run tests via ctest run: | From f1a01df3001ddbda146efb5047b9079894940fb7 Mon Sep 17 00:00:00 2001 From: fawwazmayda Date: Sun, 25 Apr 2021 00:29:31 +0800 Subject: [PATCH 20/89] adding new silu function fixing indentation fixing style error Update silu_function.hpp --- .../ann/activation_functions/CMakeLists.txt | 1 + .../activation_functions/silu_function.hpp | 96 +++++++++++++++++++ src/mlpack/methods/ann/layer/base_layer.hpp | 14 +++ .../tests/activation_functions_test.cpp | 23 +++++ 4 files changed, 134 insertions(+) create mode 100644 src/mlpack/methods/ann/activation_functions/silu_function.hpp diff --git a/src/mlpack/methods/ann/activation_functions/CMakeLists.txt b/src/mlpack/methods/ann/activation_functions/CMakeLists.txt index 1639817716..98b03e0a91 100644 --- a/src/mlpack/methods/ann/activation_functions/CMakeLists.txt +++ b/src/mlpack/methods/ann/activation_functions/CMakeLists.txt @@ -21,6 +21,7 @@ set(SOURCES gaussian_function.hpp hard_swish_function.hpp tanh_exponential_function.hpp + silu_function.hpp ) # Add directory name to sources. diff --git a/src/mlpack/methods/ann/activation_functions/silu_function.hpp b/src/mlpack/methods/ann/activation_functions/silu_function.hpp new file mode 100644 index 0000000000..0c063a5b59 --- /dev/null +++ b/src/mlpack/methods/ann/activation_functions/silu_function.hpp @@ -0,0 +1,96 @@ +/** + * @file methods/ann/activation_functions/silu_function.hpp + * @author Fawwaz Mayda + * + * Definition and implementation of the Sigmoid Weighted Linear Unit function (SILU). + * + * For more information see the following paper + * + * @code + * @misc{elfwing2017sigmoidweighted , + * title = {Sigmoid-Weighted Linear Units for Neural Network Function Approximation in Reinforcement Learning}, + * author = {Stefan Elfwing and Eiji Uchibe and Kenji Doya}, + * year = {2017}, + * url = {https://arxiv.org/pdf/1702.03118.pdf}, + * eprint = {1702.03118}, + * archivePrefix = {arXiv}, + * primaryClass = {cs.LG} } + * @endcode + * + * 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. + */ +#ifndef MLPACK_METHODS_ANN_ACTIVATION_FUNCTIONS_SILU_FUNCTION_HPP +#define MLPACK_METHODS_ANN_ACTIVATION_FUNCTIONS_SILU_FUNCTION_HPP + +#include + +namespace mlpack { +namespace ann /* Artificial Neural Network */ { + +/** + * The SILU function, defined by + * + * @f{eqnarray*}{ + * f(x) &=& x * \frac{1}{1 + e^{-x}}\\ + * f'(x) &=& \frac{1}{1 + e^{-x}} * (1 + x * (1-\frac{1}{1 + e^{-x}}))\\ + * @f} + */ +class SILUFunction +{ + public: + /** + * Computes the SILU function. + * + * @param x Input data. + * @return f(x). + */ + static double Fn(const double x) + { + return x / (1.0 + std::exp(-x)); + } + + /** + * Computes the SILU function. + * + * @param x Input data. + * @param y The resulting output activation. + */ + template + static void Fn(const InputVecType &x, OutputVecType &y) + { + y = x / (1.0 + arma::exp(-x)); + } + + /** + * Computes the first derivative of the SILU function. + * + * @param y Input activation. + * @return f'(x) + */ + static double Deriv(const double x) + { + double sigmoid = 1.0 / (1.0 + std::exp(-x)); + return sigmoid * (1.0 + x * (1.0 - sigmoid)); + } + + /** + * Computes the first derivatives of the SILU function. + * + * @param y Input activations. + * @param x The resulting derivatives. + */ + template + static void Deriv(const InputVecType &x, OutputVecType &y) + { + OutputVecType sigmoid = 1.0 / (1.0 + arma::exp(-x)); + y = sigmoid % (1.0 + x % (1.0 - sigmoid)); + } +}; // class SILUFunction + +} // namespace ann +} // namespace mlpack + +#endif \ No newline at end of file diff --git a/src/mlpack/methods/ann/layer/base_layer.hpp b/src/mlpack/methods/ann/layer/base_layer.hpp index 7169d5f474..9c6bd19478 100644 --- a/src/mlpack/methods/ann/layer/base_layer.hpp +++ b/src/mlpack/methods/ann/layer/base_layer.hpp @@ -29,6 +29,7 @@ #include #include #include +#include namespace mlpack { namespace ann /** Artificial Neural Network. */ { @@ -54,6 +55,7 @@ namespace ann /** Artificial Neural Network. */ { * - GaussianLayer * - HardSwishLayer * - TanhExpLayer + * - SILULayer * * @tparam ActivationFunction Activation function used for the embedding layer. * @tparam InputDataType Type of the input data (arma::colvec, arma::mat, @@ -303,6 +305,18 @@ template < using TanhExpFunctionLayer = BaseLayer< ActivationFunction, InputDataType, OutputDataType>; +/** + * Standard SILU-Layer using the SILU activation function. + */ +template < + class ActivationFunction = SILUFunction, + typename InputDataType = arma::mat, + typename OutputDataType = arma::mat +> +using SILUFunctionLayer = BaseLayer< + ActivationFunction, InputDataType,OutputDataType +>; + } // namespace ann } // namespace mlpack diff --git a/src/mlpack/tests/activation_functions_test.cpp b/src/mlpack/tests/activation_functions_test.cpp index 2ae2ea344d..a20b95c039 100644 --- a/src/mlpack/tests/activation_functions_test.cpp +++ b/src/mlpack/tests/activation_functions_test.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include "catch.hpp" @@ -1241,3 +1242,25 @@ TEST_CASE("TanhExpFunctionTest", "[ActivationFunctionsTest]") CheckActivationCorrect(activationData, desiredActivations); CheckDerivativeCorrect(desiredActivations, desiredDerivatives); } + +/** + * Basic test of the SILU(Sigmoid Weighted Linear Unit) Function + */ +TEST_CASE("SILUFunctionTest","[ActivationFunctionsTest]") +{ + // Random generated values. + const arma::colvec activationData("-2 2 4.5 -5.7 -1 1 0 10"); + + // Calculated with PyTorch. + arma::colvec desiredActivation("-0.23840583860874176 1.7615940570831299 4.450558662414551 \ + -0.01900840364396572 -0.2689414322376251 0.7310585975646973 \ + 0.0 9.99954605102539"); + + // Calculated with PyTorch. + arma::colvec desiredDerivate("0.38191673159599304 1.073788046836853 1.0392179489135742 \ + 0.49049633741378784 0.36713290214538574 0.8354039788246155 \ + 0.5 1.0004087686538696"); + + CheckActivationCorrect(activationData,desiredActivation); + CheckDerivativeCorrect(desiredActivation,desiredDerivate); +} From 06678129052afa1a91aa510949f8ba5536873d2e Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sat, 24 Apr 2021 18:41:59 +0200 Subject: [PATCH 21/89] Fix find armadillo by rcurtin. Authored-by: Ryan Curtin Signed-off-by: Omar Shrit --- CMake/FindArmadillo.cmake | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/CMake/FindArmadillo.cmake b/CMake/FindArmadillo.cmake index c684963f70..3d696b0abb 100644 --- a/CMake/FindArmadillo.cmake +++ b/CMake/FindArmadillo.cmake @@ -77,13 +77,14 @@ else() # don't link to armadillo in this case set(ARMADILLO_LIBRARY "") endif() + # Link to support libraries in either case on MSVC. if(NOT _ARMA_USE_WRAPPER OR MSVC) if(_ARMA_USE_LAPACK) if(ARMADILLO_FIND_QUIETLY OR NOT ARMADILLO_FIND_REQUIRED) find_package(LAPACK QUIET) else() - find_package(LAPCK REQUIRED) + find_package(LAPACK REQUIRED) endif() if(LAPACK_FOUND) set(_ARMA_SUPPORT_LIBRARIES "${_ARMA_SUPPORT_LIBRARIES}" "${LAPACK_LIBRARIES}") @@ -154,5 +155,6 @@ unset(__ARMA_SUPPORT_INCLUDE_DIRS) # Hide internal variables mark_as_advanced( - ARMADILLO_INCLUDE_DIR - ARMADILLO_LIBRARY) + ARMADILLO_INCLUDE_DIR + ARMADILLO_LIBRARY + ARMADILLO_LIBRARIES) From 74878b783ed963d2e96397cddfead26dea7acb05 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sat, 24 Apr 2021 19:06:14 +0200 Subject: [PATCH 22/89] Fix cover tree issues by @rcurtin fixes #2869 This pull request closes #2869 and fixes all errors from cover tree when trying to put mlpack on arm64 devices. Thanks to @rcurtin to put all effort to resolve this one. Authored-by: Ryan Curtin Signed-off-by: Omar Shrit --- .../tree/cover_tree/dual_tree_traverser.hpp | 20 +++--- .../cover_tree/dual_tree_traverser_impl.hpp | 62 +++++++++---------- .../cover_tree/single_tree_traverser_impl.hpp | 18 +++--- src/mlpack/tests/akfn_test.cpp | 1 - src/mlpack/tests/krann_search_test.cpp | 3 +- 5 files changed, 55 insertions(+), 49 deletions(-) diff --git a/src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp b/src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp index 804c647e58..ebc6729fdc 100644 --- a/src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp +++ b/src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp @@ -87,19 +87,23 @@ class CoverTree:: * Helper function for traversal of the two trees. */ void Traverse(CoverTree& queryNode, - std::map >& - referenceMap); + std::map, + std::greater>& referenceMap); //! Prepare map for recursion. void PruneMap(CoverTree& queryNode, - std::map >& - referenceMap, - std::map >& - childMap); + std::map, + std::greater>& referenceMap, + std::map, + std::greater>& childMap); void ReferenceRecursion(CoverTree& queryNode, - std::map >& - referenceMap); + std::map, + std::greater>& referenceMap); }; } // namespace tree diff --git a/src/mlpack/core/tree/cover_tree/dual_tree_traverser_impl.hpp b/src/mlpack/core/tree/cover_tree/dual_tree_traverser_impl.hpp index 5e29d09fca..cde21d8a75 100644 --- a/src/mlpack/core/tree/cover_tree/dual_tree_traverser_impl.hpp +++ b/src/mlpack/core/tree/cover_tree/dual_tree_traverser_impl.hpp @@ -43,7 +43,7 @@ DualTreeTraverser::Traverse(CoverTree& queryNode, CoverTree& referenceNode) { // Start by creating a map and adding the reference root node to it. - std::map > refMap; + std::map, std::greater> refMap; DualCoverTreeMapEntry rootRefEntry; @@ -70,7 +70,8 @@ template void CoverTree:: DualTreeTraverser::Traverse( CoverTree& queryNode, - std::map >& referenceMap) + std::map, std::greater>& + referenceMap) { if (referenceMap.size() == 0) return; // Nothing to do! @@ -85,7 +86,7 @@ DualTreeTraverser::Traverse( // Now, reduce the scale of the query node by recursing. But we can't recurse // if the query node is a leaf node. if ((queryNode.Scale() != INT_MIN) && - (queryNode.Scale() >= (*referenceMap.rbegin()).first)) + (queryNode.Scale() >= (*referenceMap.begin()).first)) { // Recurse into the non-self-children first. The recursion order cannot // affect the runtime of the algorithm, because each query child recursion's @@ -95,11 +96,15 @@ DualTreeTraverser::Traverse( for (size_t i = 1; i < queryNode.NumChildren(); ++i) { // We need a copy of the map for this child. - std::map > childMap; + std::map, std::greater> + childMap; + PruneMap(queryNode.Child(i), referenceMap, childMap); Traverse(queryNode.Child(i), childMap); } - std::map > selfChildMap; + std::map, std::greater> + selfChildMap; + PruneMap(queryNode.Child(0), referenceMap, selfChildMap); Traverse(queryNode.Child(0), selfChildMap); } @@ -111,8 +116,7 @@ DualTreeTraverser::Traverse( // evaluations to do. Log::Assert((*referenceMap.begin()).first == INT_MIN); Log::Assert(queryNode.Scale() == INT_MIN); - std::vector& pointVector = - (*referenceMap.begin()).second; + std::vector& pointVector = referenceMap[INT_MIN]; for (size_t i = 0; i < pointVector.size(); ++i) { @@ -156,25 +160,25 @@ template void CoverTree:: DualTreeTraverser::PruneMap( CoverTree& queryNode, - std::map >& referenceMap, - std::map >& childMap) + std::map, std::greater>& + referenceMap, + std::map, std::greater>& + childMap) { if (referenceMap.empty()) return; // Nothing to do. // Copy the zero set first. - if ((*referenceMap.begin()).first == INT_MIN) + if (referenceMap.count(INT_MIN) == 1) { // Get a reference to the vector representing the entries at this scale. - std::vector& scaleVector = - (*referenceMap.begin()).second; + std::vector& scaleVector = referenceMap[INT_MIN]; // Before traversing all the points in this scale, sort by score. std::sort(scaleVector.begin(), scaleVector.end()); - const int thisScale = (*referenceMap.begin()).first; - childMap[thisScale].reserve(scaleVector.size()); - std::vector& newScaleVector = childMap[thisScale]; + childMap[INT_MIN].reserve(scaleVector.size()); + std::vector& newScaleVector = childMap[INT_MIN]; // Loop over each entry in the vector. for (size_t j = 0; j < scaleVector.size(); ++j) @@ -208,13 +212,13 @@ DualTreeTraverser::PruneMap( // If we didn't add anything, then strike this vector from the map. if (newScaleVector.size() == 0) - childMap.erase((*referenceMap.begin()).first); + childMap.erase(INT_MIN); } - typename std::map >::reverse_iterator - it = referenceMap.rbegin(); + typename std::map, + std::greater>::iterator it = referenceMap.begin(); - while ((it != referenceMap.rend())) + while ((it != referenceMap.end())) { const int thisScale = (*it).first; if (thisScale == INT_MIN) // We already did it. @@ -277,28 +281,26 @@ template void CoverTree:: DualTreeTraverser::ReferenceRecursion( CoverTree& queryNode, - std::map >& referenceMap) + std::map, std::greater>& + referenceMap) { // First, reduce the maximum scale in the reference map down to the scale of // the query node. while (!referenceMap.empty()) { + const int maxScale = ((*referenceMap.begin()).first); // Hacky bullshit to imitate jl cover tree. - if (queryNode.Parent() == NULL && (*referenceMap.rbegin()).first < - queryNode.Scale()) + if (queryNode.Parent() == NULL && maxScale < queryNode.Scale()) break; - if (queryNode.Parent() != NULL && (*referenceMap.rbegin()).first <= - queryNode.Scale()) + if (queryNode.Parent() != NULL && maxScale <= queryNode.Scale()) break; // If the query node's scale is INT_MIN and the reference map's maximum // scale is INT_MIN, don't try to recurse... - if ((queryNode.Scale() == INT_MIN) && - ((*referenceMap.rbegin()).first == INT_MIN)) + if (queryNode.Scale() == INT_MIN && maxScale == INT_MIN) break; // Get a reference to the current largest scale. - std::vector& scaleVector = - (*referenceMap.rbegin()).second; + std::vector& scaleVector = referenceMap[maxScale]; // Before traversing all the points in this scale, sort by score. std::sort(scaleVector.begin(), scaleVector.end()); @@ -308,7 +310,6 @@ DualTreeTraverser::ReferenceRecursion( { // Get a reference to the current element. const DualCoverTreeMapEntry& frame = scaleVector.at(i); - CoverTree* refNode = frame.referenceNode; // Create the score for the children. @@ -344,13 +345,12 @@ DualTreeTraverser::ReferenceRecursion( newFrame.score = childScore; // Use the score of the parent. newFrame.baseCase = baseCase; newFrame.traversalInfo = rule.TraversalInfo(); - referenceMap[newFrame.referenceNode->Scale()].push_back(newFrame); } } // Now clear the memory for this scale; it isn't needed anymore. - referenceMap.erase((*referenceMap.rbegin()).first); + referenceMap.erase(maxScale); } } diff --git a/src/mlpack/core/tree/cover_tree/single_tree_traverser_impl.hpp b/src/mlpack/core/tree/cover_tree/single_tree_traverser_impl.hpp index 728e64f39d..0721c4e090 100644 --- a/src/mlpack/core/tree/cover_tree/single_tree_traverser_impl.hpp +++ b/src/mlpack/core/tree/cover_tree/single_tree_traverser_impl.hpp @@ -80,9 +80,9 @@ SingleTreeTraverser::Traverse( // and then the vector is all the nodes in that scale which need to be // investigated. Because no point in a scale can add a point in its own // scale, we know that the vector for each scale is final when we get to it. - // In addition, map is organized in such a way that rbegin() will return the - // largest scale. - std::map > mapQueue; + // In addition, the map is organized in such a way that begin() will return + // the largest scale. + std::map, std::greater> mapQueue; // Create the score for the children. double rootChildScore = rule.Score(queryIndex, referenceNode); @@ -123,14 +123,13 @@ SingleTreeTraverser::Traverse( // Now begin the iteration through the map, but only if it has anything in it. if (mapQueue.empty()) return; - typename std::map >::reverse_iterator rit = - mapQueue.rbegin(); + int maxScale = mapQueue.cbegin()->first; // We will treat the leaves differently (below). - while ((*rit).first != INT_MIN) + while (maxScale != INT_MIN) { // Get a reference to the current scale. - std::vector& scaleVector = (*rit).second; + std::vector& scaleVector = mapQueue[maxScale]; // Before traversing all the points in this scale, sort by score. std::sort(scaleVector.begin(), scaleVector.end()); @@ -170,7 +169,9 @@ SingleTreeTraverser::Traverse( // trees using TreeTraits::FirstPointIsCentroid; this is an optimization // that (theoretically) the compiler should get right. if (point != parent) + { baseCase = rule.BaseCase(queryIndex, point); + } // Don't add the self-leaf. size_t j = 0; @@ -193,7 +194,8 @@ SingleTreeTraverser::Traverse( } // Now clear the memory for this scale; it isn't needed anymore. - mapQueue.erase((*rit).first); + mapQueue.erase(maxScale); + maxScale = mapQueue.begin()->first; } // Now deal with the leaves. diff --git a/src/mlpack/tests/akfn_test.cpp b/src/mlpack/tests/akfn_test.cpp index a5f802cd7e..1808190fbb 100644 --- a/src/mlpack/tests/akfn_test.cpp +++ b/src/mlpack/tests/akfn_test.cpp @@ -241,4 +241,3 @@ TEST_CASE("AKFNDualBallTreeTest", "[AKFNTest]") for (size_t i = 0; i < neighborsBallTree.n_elem; ++i) REQUIRE_RELATIVE_ERR(distancesBallTree(i), distancesExact(i), 0.05); } - diff --git a/src/mlpack/tests/krann_search_test.cpp b/src/mlpack/tests/krann_search_test.cpp index 68214b4a25..a548b30438 100644 --- a/src/mlpack/tests/krann_search_test.cpp +++ b/src/mlpack/tests/krann_search_test.cpp @@ -368,7 +368,8 @@ TEST_CASE("DualCoverTreeTest", "[KRANNTest]") RACoverTreeSearch tsdRann(&refTree, false, 1.0, 0.95, false, false, 5); arma::Mat qrRanks; - if (!data::Load("rann_test_qr_ranks.csv", qrRanks, false, false)) // No transpose. + // No transpose. + if (!data::Load("rann_test_qr_ranks.csv", qrRanks, false, false)) FAIL("Cannot load dataset rann_test_qr_ranks.csv"); size_t numRounds = 100; From 54267705ed3c7ad595bfdb7ef48cfd8dd4e4cff3 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sat, 24 Apr 2021 19:24:32 +0200 Subject: [PATCH 23/89] If we are building mlpack statically then build executable statically too This pull request is not related to cross-compilation. Signed-off-by: Omar Shrit --- src/mlpack/bindings/cli/CMakeLists.txt | 20 +++++++++++++++----- src/mlpack/tests/CMakeLists.txt | 21 +++++++++++++++------ 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/mlpack/bindings/cli/CMakeLists.txt b/src/mlpack/bindings/cli/CMakeLists.txt index 4b94805fe5..8ebc20583a 100644 --- a/src/mlpack/bindings/cli/CMakeLists.txt +++ b/src/mlpack/bindings/cli/CMakeLists.txt @@ -50,11 +50,21 @@ if (BUILD_CLI_EXECUTABLES) add_executable(mlpack_${name} ${name}_main.cpp ) - target_link_libraries(mlpack_${name} - mlpack - ${ARMADILLO_LIBRARIES} - ${COMPILER_SUPPORT_LIBRARIES} - ) + # Build mlpack CLI binding binaries statically. + if(NOT BUILD_SHARED_LIBS) + target_link_libraries(mlpack_${name} -static + mlpack + ${ARMADILLO_LIBRARIES} + ${COMPILER_SUPPORT_LIBRARIES} + ) + else() + # Build mlpack CLI binding binaries dynamically. + target_link_libraries(mlpack_${name} + mlpack + ${ARMADILLO_LIBRARIES} + ${COMPILER_SUPPORT_LIBRARIES} + ) + endif() # Make sure that we set BINDING_TYPE to cli so the command-line program is # compiled with the correct int main() call. set_target_properties(mlpack_${name} PROPERTIES COMPILE_FLAGS diff --git a/src/mlpack/tests/CMakeLists.txt b/src/mlpack/tests/CMakeLists.txt index de0f56d8df..0695ccb7b7 100644 --- a/src/mlpack/tests/CMakeLists.txt +++ b/src/mlpack/tests/CMakeLists.txt @@ -175,12 +175,21 @@ add_executable(mlpack_test main_tests/test_helper.hpp ) -# Link dependencies of test executable. -target_link_libraries(mlpack_test - mlpack - ${ARMADILLO_LIBRARIES} - ${COMPILER_SUPPORT_LIBRARIES} -) +if(NOT BUILD_SHARED_LIBS) +# Build mlpack test executable statically. + target_link_libraries(mlpack_test -static + mlpack + ${ARMADILLO_LIBRARIES} + ${COMPILER_SUPPORT_LIBRARIES} + ) +else() + # Build mlpack test executable dynamically. + target_link_libraries(mlpack_test + mlpack + ${ARMADILLO_LIBRARIES} + ${COMPILER_SUPPORT_LIBRARIES} + ) +endif() set_target_properties(mlpack_test PROPERTIES COTIRE_CXX_PREFIX_HEADER_INIT "../core.hpp") cotire(mlpack_test) From 38b1907784a9299a02de6bf57b5c2b14ccb70e64 Mon Sep 17 00:00:00 2001 From: Aakash kaushik Date: Mon, 26 Apr 2021 00:19:35 +0530 Subject: [PATCH 24/89] Set test timeout for mlpack_test to zero. --- src/mlpack/tests/CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/mlpack/tests/CMakeLists.txt b/src/mlpack/tests/CMakeLists.txt index 9761cd6df1..aa5a156411 100644 --- a/src/mlpack/tests/CMakeLists.txt +++ b/src/mlpack/tests/CMakeLists.txt @@ -205,3 +205,5 @@ add_custom_command(TARGET mlpack_test ) add_test(NAME "catch_test" COMMAND mlpack_test WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) + +set_tests_properties(mlpack_test PROPERTIES TIMEOUT 0) From 1ed810d15128636cb0558b9c14ed1733e764d794 Mon Sep 17 00:00:00 2001 From: Aakash kaushik Date: Mon, 26 Apr 2021 00:22:06 +0530 Subject: [PATCH 25/89] Removed extra space --- src/mlpack/tests/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/tests/CMakeLists.txt b/src/mlpack/tests/CMakeLists.txt index aa5a156411..094ea41196 100644 --- a/src/mlpack/tests/CMakeLists.txt +++ b/src/mlpack/tests/CMakeLists.txt @@ -206,4 +206,4 @@ add_custom_command(TARGET mlpack_test add_test(NAME "catch_test" COMMAND mlpack_test WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) -set_tests_properties(mlpack_test PROPERTIES TIMEOUT 0) +set_tests_properties(mlpack_test PROPERTIES TIMEOUT 0) From 95fca84588c128081d9ab2ccdebfe451d8ee0a0f Mon Sep 17 00:00:00 2001 From: Marcus Edel Date: Mon, 26 Apr 2021 02:05:08 +0200 Subject: [PATCH 26/89] Remove redundant numClasses assignment. --- src/mlpack/methods/linear_svm/linear_svm_main.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/mlpack/methods/linear_svm/linear_svm_main.cpp b/src/mlpack/methods/linear_svm/linear_svm_main.cpp index f72b7c1b6f..ce645e964b 100644 --- a/src/mlpack/methods/linear_svm/linear_svm_main.cpp +++ b/src/mlpack/methods/linear_svm/linear_svm_main.cpp @@ -373,10 +373,6 @@ static void mlpackMain() oss << IO::GetPrintableParam("test"); std::string testOutput = oss.str(); - if (!IO::HasParam("training")) - { - numClasses = model->svm.NumClasses(); - } // Get the test dataset, and get predictions. testSet = std::move(IO::GetParam("test")); arma::Row predictions; From 03a10c2618748e1b7e43d4205d6deb5cbf1400d3 Mon Sep 17 00:00:00 2001 From: Marcus Edel Date: Mon, 26 Apr 2021 02:15:35 +0200 Subject: [PATCH 27/89] Initalize bestDistance at a later stage. --- .../methods/neighbor_search/neighbor_search_rules_impl.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/mlpack/methods/neighbor_search/neighbor_search_rules_impl.hpp b/src/mlpack/methods/neighbor_search/neighbor_search_rules_impl.hpp index 74d3c490cd..d9c5d2bba3 100644 --- a/src/mlpack/methods/neighbor_search/neighbor_search_rules_impl.hpp +++ b/src/mlpack/methods/neighbor_search/neighbor_search_rules_impl.hpp @@ -397,7 +397,6 @@ inline double NeighborSearchRules:: // take the better of the two. double worstDistance = SortPolicy::BestDistance(); - double bestDistance = SortPolicy::WorstDistance(); double bestPointDistance = SortPolicy::WorstDistance(); double auxDistance = SortPolicy::WorstDistance(); @@ -428,7 +427,7 @@ inline double NeighborSearchRules:: // Add triangle inequality adjustment to best distance. It is possible this // could be tighter for some certain types of trees. - bestDistance = SortPolicy::CombineWorst(auxDistance, + double bestDistance = SortPolicy::CombineWorst(auxDistance, 2 * queryNode.FurthestDescendantDistance()); // Add triangle inequality adjustment to best distance of points in node. From 73fec171ec66c85382f5f2f8b79539d1e3d355f5 Mon Sep 17 00:00:00 2001 From: Marcus Edel Date: Mon, 26 Apr 2021 02:17:05 +0200 Subject: [PATCH 28/89] Initalize auxDistance at a later stage. --- .../methods/neighbor_search/neighbor_search_rules_impl.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/mlpack/methods/neighbor_search/neighbor_search_rules_impl.hpp b/src/mlpack/methods/neighbor_search/neighbor_search_rules_impl.hpp index d9c5d2bba3..6bf8055efd 100644 --- a/src/mlpack/methods/neighbor_search/neighbor_search_rules_impl.hpp +++ b/src/mlpack/methods/neighbor_search/neighbor_search_rules_impl.hpp @@ -398,7 +398,6 @@ inline double NeighborSearchRules:: double worstDistance = SortPolicy::BestDistance(); double bestPointDistance = SortPolicy::WorstDistance(); - double auxDistance = SortPolicy::WorstDistance(); // Loop over points held in the node. for (size_t i = 0; i < queryNode.NumPoints(); ++i) @@ -410,7 +409,7 @@ inline double NeighborSearchRules:: bestPointDistance = distance; } - auxDistance = bestPointDistance; + double auxDistance = bestPointDistance; // Loop over children of the node, and use their cached information to // assemble bounds. From 25fea7c3612d22daa25ed99b5f6a480695e9bc75 Mon Sep 17 00:00:00 2001 From: Marcus Edel Date: Mon, 26 Apr 2021 02:39:19 +0200 Subject: [PATCH 29/89] Remove unused parameter (alpha). --- src/mlpack/tests/det_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/tests/det_test.cpp b/src/mlpack/tests/det_test.cpp index ab55a541cd..92e2ca00a5 100644 --- a/src/mlpack/tests/det_test.cpp +++ b/src/mlpack/tests/det_test.cpp @@ -341,7 +341,7 @@ TEST_CASE("TestComputeValue", "[DETTest]") REQUIRE(d3 == Approx(testDTree.ComputeValue(q3)).epsilon(1e-12)); REQUIRE(0.0 == Approx(testDTree.ComputeValue(q4)).epsilon(1e-12)); - alpha = testDTree.PruneAndUpdate(alpha, testData.n_cols, false); + testDTree.PruneAndUpdate(alpha, testData.n_cols, false); double d = 1.0 / exp(log(4.0) + log(7.0) + log(7.0)); From 0c4ff06969a024f5b30459e1f4e4ee44d2cced27 Mon Sep 17 00:00:00 2001 From: Marcus Edel Date: Mon, 26 Apr 2021 02:39:48 +0200 Subject: [PATCH 30/89] Remove unused parameter (alpha). --- src/mlpack/tests/det_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/tests/det_test.cpp b/src/mlpack/tests/det_test.cpp index 92e2ca00a5..8f49d39fca 100644 --- a/src/mlpack/tests/det_test.cpp +++ b/src/mlpack/tests/det_test.cpp @@ -448,7 +448,7 @@ TEST_CASE("TestSparseComputeValue", "[DETTest]") REQUIRE(d3 == Approx(testDTree.ComputeValue(q3)).epsilon(1e-12)); REQUIRE(0.0 == Approx(testDTree.ComputeValue(q4)).epsilon(1e-12)); - alpha = testDTree.PruneAndUpdate(alpha, testData.n_cols, false); + testDTree.PruneAndUpdate(alpha, testData.n_cols, false); double d = 1.0 / exp(log(4.0) + log(7.0) + log(7.0)); From b2ad544a16c0c9165c24746b313787ede6800217 Mon Sep 17 00:00:00 2001 From: Marcus Edel Date: Mon, 26 Apr 2021 04:36:52 +0200 Subject: [PATCH 31/89] Move deltaBeta into the loop. --- .../bayesian_linear_regression.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/mlpack/methods/bayesian_linear_regression/bayesian_linear_regression.cpp b/src/mlpack/methods/bayesian_linear_regression/bayesian_linear_regression.cpp index c163d0d148..92b01d80c8 100644 --- a/src/mlpack/methods/bayesian_linear_regression/bayesian_linear_regression.cpp +++ b/src/mlpack/methods/bayesian_linear_regression/bayesian_linear_regression.cpp @@ -1,6 +1,6 @@ /** * @file methods/bayesian_linear_regression/bayesian_linear_regression.cpp - * @author Clement Mercier + * @author Clement Mercier * * Implementation of Bayesian linear regression. * @@ -58,12 +58,12 @@ double BayesianLinearRegression::Train(const arma::mat& data, beta = 1 / (var(t, 1) * 0.1); unsigned short i = 0; - double deltaAlpha = 1.0, deltaBeta = 1.0, crit = 1.0; + double deltaAlpha = 1.0, crit = 1.0; while ((crit > tolerance) && (i < maxIterations)) { deltaAlpha = -alpha; - deltaBeta = -beta; + double deltaBeta = -beta; // Update the solution. omega = eigVec * diagmat(1 / (eigVal + (alpha / beta))) * eigVecInvPhitT; From 3d24936f5894efb8f00e37fdfc3acf70b2a80ece Mon Sep 17 00:00:00 2001 From: Aakash Kaushik Date: Mon, 26 Apr 2021 09:50:46 +0530 Subject: [PATCH 32/89] catch_test instead of mlpack_test --- src/mlpack/tests/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/tests/CMakeLists.txt b/src/mlpack/tests/CMakeLists.txt index 094ea41196..e05cf135a1 100644 --- a/src/mlpack/tests/CMakeLists.txt +++ b/src/mlpack/tests/CMakeLists.txt @@ -206,4 +206,4 @@ add_custom_command(TARGET mlpack_test add_test(NAME "catch_test" COMMAND mlpack_test WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) -set_tests_properties(mlpack_test PROPERTIES TIMEOUT 0) +set_tests_properties("catch_test" PROPERTIES TIMEOUT 0) From da5862bcd010b17838bdf2be9be665c403570530 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Mon, 26 Apr 2021 13:01:44 +0200 Subject: [PATCH 33/89] Add a use case for BUILD_SHARED_LIBS in docs Signed-off-by: Omar Shrit --- doc/guide/build.hpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/doc/guide/build.hpp b/doc/guide/build.hpp index 9356f725cc..21b22fd238 100644 --- a/doc/guide/build.hpp +++ b/doc/guide/build.hpp @@ -209,6 +209,14 @@ The full list of options mlpack allows: Each option can be specified to CMake with the '-D' flag. Other tools can also be used to configure CMake, but those are not documented here. +For example, if you would like to build mlpack and its CLI binding statically, then +you need to execute the following commands: + +@code +$ cd build +$ cmake -D BUILD_SHARED_LIBS=OFF ../ +@endcode + In addition, the following directories may be specified, to find include files and libraries. These also use the '-D' flag. @@ -216,10 +224,12 @@ and libraries. These also use the '-D' flag. - ARMADILLO_LIBRARY=(/path/to/armadillo/libarmadillo.so): location of Armadillo library - BOOST_ROOT=(/path/to/boost/): path to root of boost installation + - CEREAL_INCLUDE_DIR=(/path/to/cereal/include): path to include directory for + cereal - ENSMALLEN_INCLUDE_DIR=(/path/to/ensmallen/include): path to include directory for ensmallen - STB_IMAGE_INCLUDE_DIR=(/path/to/stb/include): path to include directory for - STB image library + STB image library - MATHJAX_ROOT=(/path/to/mathjax): path to root of MathJax installation @section build_build Building mlpack From 921a30ce21ea0117d795667ddc96a58790fe23ed Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Mon, 26 Apr 2021 16:45:18 +0200 Subject: [PATCH 34/89] Update indentation in src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp Co-authored-by: Ryan Curtin --- src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp b/src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp index ebc6729fdc..a3cf53645d 100644 --- a/src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp +++ b/src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp @@ -101,9 +101,8 @@ class CoverTree:: std::greater>& childMap); void ReferenceRecursion(CoverTree& queryNode, - std::map, - std::greater>& referenceMap); + std::map, + std::greater>& referenceMap); }; } // namespace tree From 993d3b040f537880b72cd808a503d2435138f58a Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Mon, 26 Apr 2021 16:45:30 +0200 Subject: [PATCH 35/89] Update indentation in src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp Co-authored-by: Ryan Curtin --- src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp b/src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp index a3cf53645d..d4611accd6 100644 --- a/src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp +++ b/src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp @@ -100,7 +100,8 @@ class CoverTree:: std::vector, std::greater>& childMap); - void ReferenceRecursion(CoverTree& queryNode, + void ReferenceRecursion( + CoverTree& queryNode, std::map, std::greater>& referenceMap); }; From 1b0e7b44db4fbe9070ecc3b29cc550b0b32cc16b Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Mon, 26 Apr 2021 16:45:44 +0200 Subject: [PATCH 36/89] Update indentation in src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp Co-authored-by: Ryan Curtin --- .../core/tree/cover_tree/dual_tree_traverser.hpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp b/src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp index d4611accd6..65ae0c8219 100644 --- a/src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp +++ b/src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp @@ -93,12 +93,10 @@ class CoverTree:: //! Prepare map for recursion. void PruneMap(CoverTree& queryNode, - std::map, - std::greater>& referenceMap, - std::map, - std::greater>& childMap); + std::map, + std::greater>& referenceMap, + std::map, + std::greater>& childMap); void ReferenceRecursion( CoverTree& queryNode, From abbc179220799fc022c5fb45b22e183a6c1e7a0b Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Mon, 26 Apr 2021 16:45:59 +0200 Subject: [PATCH 37/89] Update indentation in src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp Co-authored-by: Ryan Curtin --- src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp b/src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp index 65ae0c8219..2a901ef5b7 100644 --- a/src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp +++ b/src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp @@ -92,7 +92,8 @@ class CoverTree:: std::greater>& referenceMap); //! Prepare map for recursion. - void PruneMap(CoverTree& queryNode, + void PruneMap( + CoverTree& queryNode, std::map, std::greater>& referenceMap, std::map, From d7fa52cc4f46e6d0cc0d6f58f4127666c5cbf906 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Mon, 26 Apr 2021 16:46:14 +0200 Subject: [PATCH 38/89] Update indentation in src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp Co-authored-by: Ryan Curtin --- src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp b/src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp index 2a901ef5b7..9ddda38b98 100644 --- a/src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp +++ b/src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp @@ -87,9 +87,8 @@ class CoverTree:: * Helper function for traversal of the two trees. */ void Traverse(CoverTree& queryNode, - std::map, - std::greater>& referenceMap); + std::map, + std::greater>& referenceMap); //! Prepare map for recursion. void PruneMap( From bb4e3770733f30c625bd6eed97bac807c8285a61 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Mon, 26 Apr 2021 16:46:26 +0200 Subject: [PATCH 39/89] Update indentation in src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp Co-authored-by: Ryan Curtin --- src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp b/src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp index 9ddda38b98..2b7a8ae378 100644 --- a/src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp +++ b/src/mlpack/core/tree/cover_tree/dual_tree_traverser.hpp @@ -86,7 +86,8 @@ class CoverTree:: /** * Helper function for traversal of the two trees. */ - void Traverse(CoverTree& queryNode, + void Traverse( + CoverTree& queryNode, std::map, std::greater>& referenceMap); From b980f1617967fedfb0517e85ca219fc1196c1547 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Wed, 28 Apr 2021 17:20:35 -0400 Subject: [PATCH 40/89] Add a link to the vision document in the readme. --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index b8fffc8ca8..98c908fc7d 100644 --- a/README.md +++ b/README.md @@ -361,6 +361,9 @@ older versions of mlpack: - [Development Site (Github)](https://www.github.com/mlpack/mlpack/) - [API documentation (Doxygen)](https://www.mlpack.org/doc/mlpack-git/doxygen/index.html) +To learn about the development goals of mlpack in the short- and medium-term +future, see the [vision document](https://www.mlpack.org/papers/vision.pdf). + ### 8. Bug reporting (see also [mlpack help](https://www.mlpack.org/questions.html)) From e8fa7b0cb8b26c83401ddf1b39a90535f84d49af Mon Sep 17 00:00:00 2001 From: Abhinav Anand Date: Thu, 29 Apr 2021 21:42:45 +0530 Subject: [PATCH 41/89] Fixed ceil parameter in max and lp pooling layer --- src/mlpack/methods/ann/layer/lp_pooling.hpp | 46 +++++++++++++------ .../methods/ann/layer/lp_pooling_impl.hpp | 5 -- src/mlpack/methods/ann/layer/max_pooling.hpp | 14 ++++-- .../methods/ann/layer/max_pooling_impl.hpp | 3 -- 4 files changed, 43 insertions(+), 25 deletions(-) diff --git a/src/mlpack/methods/ann/layer/lp_pooling.hpp b/src/mlpack/methods/ann/layer/lp_pooling.hpp index 8423dda79f..40296acad1 100644 --- a/src/mlpack/methods/ann/layer/lp_pooling.hpp +++ b/src/mlpack/methods/ann/layer/lp_pooling.hpp @@ -167,9 +167,17 @@ class LpPooling for (size_t i = 0, rowidx = 0; i < output.n_rows; ++i, rowidx += strideWidth) { + size_t rowEnd = rowidx + kernelWidth - 1; + size_t colEnd = colidx + kernelHeight - 1; + + if (rowEnd > input.n_rows - 1) + rowEnd = input.n_rows - 1; + if (colEnd > input.n_cols - 1) + colEnd = input.n_cols - 1; + arma::mat subInput = input( - arma::span(rowidx, rowidx + kernelWidth - 1 - offset), - arma::span(colidx, colidx + kernelHeight - 1 - offset)); + arma::span(rowidx, rowEnd), + arma::span(colidx, colEnd)); output(i, j) = pow(arma::accu(arma::pow(subInput, normType)), 1.0 / normType); @@ -188,24 +196,39 @@ class LpPooling const arma::Mat& error, arma::Mat& output) { - const size_t rStep = input.n_rows / error.n_rows - offset; - const size_t cStep = input.n_cols / error.n_cols - offset; arma::Mat unpooledError; - for (size_t j = 0; j < input.n_cols - cStep; j += cStep) + for (size_t j = 0, colidx = 0; j < input.n_cols; j += strideHeight, colidx++) { - for (size_t i = 0; i < input.n_rows - rStep; i += rStep) + for (size_t i = 0, rowidx = 0; i < input.n_rows; i += strideWidth, rowidx++) { - const arma::Mat& inputArea = input(arma::span(i, i + rStep - 1), - arma::span(j, j + cStep - 1)); + size_t rowEnd = i + kernelWidth - 1; + size_t colEnd = j + kernelHeight - 1; + + if (rowEnd > input.n_rows - 1) + { + if (floor) + continue; + rowEnd = input.n_rows - 1; + } + + if (colEnd > input.n_cols - 1) + { + if (floor) + continue; + colEnd = input.n_cols - 1; + } + + arma::mat InputArea = input(arma::span(i, rowEnd), arma::span(j, colEnd)); + size_t sum = pow(arma::accu(arma::pow(inputArea, normType)), (normType - 1) / normType); unpooledError = arma::Mat(inputArea.n_rows, inputArea.n_cols); unpooledError.fill(error(i / rStep, j / cStep)); unpooledError %= arma::pow(inputArea, normType - 1); unpooledError /= sum; - output(arma::span(i, i + rStep - 1 - offset), - arma::span(j, j + cStep - 1 - offset)) += unpooledError; + output(arma::span(i, i + InputArea.n_rows - 1), + arma::span(j, j + InputArea.n_cols - 1)) += unpooledError; } } } @@ -249,9 +272,6 @@ class LpPooling //! Locally-stored reset parameter used to initialize the module once. bool reset; - //! Locally-stored stored rounding offset. - size_t offset; - //! Locally-stored number of input units. size_t batchSize; diff --git a/src/mlpack/methods/ann/layer/lp_pooling_impl.hpp b/src/mlpack/methods/ann/layer/lp_pooling_impl.hpp index 0abe08ada6..525789fe07 100644 --- a/src/mlpack/methods/ann/layer/lp_pooling_impl.hpp +++ b/src/mlpack/methods/ann/layer/lp_pooling_impl.hpp @@ -46,7 +46,6 @@ LpPooling::LpPooling( outputWidth(0), outputHeight(0), reset(false), - offset(0), batchSize(0) { // Nothing to do here. @@ -68,8 +67,6 @@ void LpPooling::Forward( (double) kernelWidth) / (double) strideWidth + 1); outputHeight = std::floor((inputHeight - (double) kernelHeight) / (double) strideHeight + 1); - - offset = 0; } else { @@ -77,8 +74,6 @@ void LpPooling::Forward( (double) kernelWidth) / (double) strideWidth + 1); outputHeight = std::ceil((inputHeight - (double) kernelHeight) / (double) strideHeight + 1); - - offset = 1; } outputTemp = arma::zeros >(outputWidth, outputHeight, diff --git a/src/mlpack/methods/ann/layer/max_pooling.hpp b/src/mlpack/methods/ann/layer/max_pooling.hpp index 098a9d100a..81488597f7 100644 --- a/src/mlpack/methods/ann/layer/max_pooling.hpp +++ b/src/mlpack/methods/ann/layer/max_pooling.hpp @@ -188,9 +188,17 @@ class MaxPooling for (size_t i = 0, rowidx = 0; i < output.n_rows; ++i, rowidx += strideWidth) { + size_t rowEnd = rowidx + kernelWidth - 1; + size_t colEnd = colidx + kernelHeight - 1; + + if (rowEnd > input.n_rows - 1) + rowEnd = input.n_rows - 1; + if (colEnd > input.n_cols - 1) + colEnd = input.n_cols - 1; + arma::mat subInput = input( - arma::span(rowidx, rowidx + kernelWidth - 1 - offset), - arma::span(colidx, colidx + kernelHeight - 1 - offset)); + arma::span(rowidx, rowEnd), + arma::span(colidx, colEnd)); const size_t idx = pooling.Pooling(subInput); output(i, j) = subInput(idx); @@ -264,8 +272,6 @@ class MaxPooling //! If true use maximum a posteriori during the forward pass. bool deterministic; - //! Locally-stored stored rounding offset. - size_t offset; //! Locally-stored number of input units. size_t batchSize; diff --git a/src/mlpack/methods/ann/layer/max_pooling_impl.hpp b/src/mlpack/methods/ann/layer/max_pooling_impl.hpp index cbc17904c4..9650a5f2c3 100644 --- a/src/mlpack/methods/ann/layer/max_pooling_impl.hpp +++ b/src/mlpack/methods/ann/layer/max_pooling_impl.hpp @@ -45,7 +45,6 @@ MaxPooling::MaxPooling( outputWidth(0), outputHeight(0), deterministic(false), - offset(0), batchSize(0) { // Nothing to do here. @@ -67,7 +66,6 @@ void MaxPooling::Forward( (double) kernelWidth) / (double) strideWidth + 1); outputHeight = std::floor((inputHeight - (double) kernelHeight) / (double) strideHeight + 1); - offset = 0; } else { @@ -75,7 +73,6 @@ void MaxPooling::Forward( (double) kernelWidth) / (double) strideWidth + 1); outputHeight = std::ceil((inputHeight - (double) kernelHeight) / (double) strideHeight + 1); - offset = 1; } outputTemp = arma::zeros >(outputWidth, outputHeight, From 1def9db61db2bd463d92559134a02cb240894299 Mon Sep 17 00:00:00 2001 From: Abhinav Anand Date: Thu, 29 Apr 2021 22:41:05 +0530 Subject: [PATCH 42/89] minor change --- src/mlpack/methods/ann/layer/lp_pooling.hpp | 8 ++++---- src/mlpack/methods/ann/layer/max_pooling.hpp | 5 ++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/mlpack/methods/ann/layer/lp_pooling.hpp b/src/mlpack/methods/ann/layer/lp_pooling.hpp index 40296acad1..03ef9f540e 100644 --- a/src/mlpack/methods/ann/layer/lp_pooling.hpp +++ b/src/mlpack/methods/ann/layer/lp_pooling.hpp @@ -221,11 +221,11 @@ class LpPooling arma::mat InputArea = input(arma::span(i, rowEnd), arma::span(j, colEnd)); - size_t sum = pow(arma::accu(arma::pow(inputArea, normType)), + size_t sum = pow(arma::accu(arma::pow(InputArea, normType)), (normType - 1) / normType); - unpooledError = arma::Mat(inputArea.n_rows, inputArea.n_cols); - unpooledError.fill(error(i / rStep, j / cStep)); - unpooledError %= arma::pow(inputArea, normType - 1); + unpooledError = arma::Mat(InputArea.n_rows, InputArea.n_cols); + unpooledError.fill(error(rowidx, colidx) / InputArea.n_elem); + unpooledError %= arma::pow(InputArea, normType - 1); unpooledError /= sum; output(arma::span(i, i + InputArea.n_rows - 1), arma::span(j, j + InputArea.n_cols - 1)) += unpooledError; diff --git a/src/mlpack/methods/ann/layer/max_pooling.hpp b/src/mlpack/methods/ann/layer/max_pooling.hpp index 81488597f7..2547c5596a 100644 --- a/src/mlpack/methods/ann/layer/max_pooling.hpp +++ b/src/mlpack/methods/ann/layer/max_pooling.hpp @@ -205,9 +205,8 @@ class MaxPooling if (!deterministic) { - arma::Mat subIndices = indices(arma::span(rowidx, - rowidx + kernelWidth - 1 - offset), - arma::span(colidx, colidx + kernelHeight - 1 - offset)); + arma::Mat subIndices = indices(arma::span(rowidx, rowEnd), + arma::span(colidx, colEnd)); poolingIndices(i, j) = subIndices(idx); } From e441e906de3cfa1101f65f1a89bc05c1d804caa8 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 29 Apr 2021 23:08:36 +0200 Subject: [PATCH 43/89] Update doc/guide/build.hpp Co-authored-by: Ryan Curtin --- doc/guide/build.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/guide/build.hpp b/doc/guide/build.hpp index 21b22fd238..e107edb7a2 100644 --- a/doc/guide/build.hpp +++ b/doc/guide/build.hpp @@ -209,7 +209,7 @@ The full list of options mlpack allows: Each option can be specified to CMake with the '-D' flag. Other tools can also be used to configure CMake, but those are not documented here. -For example, if you would like to build mlpack and its CLI binding statically, then +For example, if you would like to build mlpack and its CLI bindings statically, then you need to execute the following commands: @code From ab7fffb8bac12013b5f445fddff57ef448d27b48 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 29 Apr 2021 23:08:51 +0200 Subject: [PATCH 44/89] Fix indentation in src/mlpack/bindings/cli/CMakeLists.txt Co-authored-by: Ryan Curtin --- src/mlpack/bindings/cli/CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/mlpack/bindings/cli/CMakeLists.txt b/src/mlpack/bindings/cli/CMakeLists.txt index 8ebc20583a..40f8ac6d75 100644 --- a/src/mlpack/bindings/cli/CMakeLists.txt +++ b/src/mlpack/bindings/cli/CMakeLists.txt @@ -53,9 +53,9 @@ if (BUILD_CLI_EXECUTABLES) # Build mlpack CLI binding binaries statically. if(NOT BUILD_SHARED_LIBS) target_link_libraries(mlpack_${name} -static - mlpack - ${ARMADILLO_LIBRARIES} - ${COMPILER_SUPPORT_LIBRARIES} + mlpack + ${ARMADILLO_LIBRARIES} + ${COMPILER_SUPPORT_LIBRARIES} ) else() # Build mlpack CLI binding binaries dynamically. From 0830bbc6d617d15fa5539d971b9549f63a9312b7 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 29 Apr 2021 23:23:57 +0200 Subject: [PATCH 45/89] Fix documentation Signed-off-by: Omar Shrit --- README.md | 7 ++++++- doc/guide/build.hpp | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 21b9e09e7e..9d0e549f87 100644 --- a/README.md +++ b/README.md @@ -211,7 +211,7 @@ Options are specified with the -D flag. The allowed options include: BUILD_R_BINDINGS=(ON/OFF): whether or not to build R bindings R_EXECUTABLE=(/path/to/R): Path to specific R executable BUILD_TESTS=(ON/OFF): whether or not to build tests - BUILD_SHARED_LIBS=(ON/OFF): compile shared libraries as opposed to + BUILD_SHARED_LIBS=(ON/OFF): compile shared libraries and executables as opposed to static libraries DISABLE_DOWNLOADS=(ON/OFF): whether to disable all downloads during build DOWNLOAD_ENSMALLEN=(ON/OFF): If ensmallen is not found, download it @@ -224,6 +224,11 @@ Options are specified with the -D flag. The allowed options include: BUILD_DOCS=(ON/OFF): build Doxygen documentation, if Doxygen is available (default ON) +For example, to build mlpack library and CLI bindings statically the following +command can be used: + + $ cmake -D BUILD_SHARED_LIBS=OFF ../ + Other tools can also be used to configure CMake, but those are not documented here. See [this section of the build guide](https://www.mlpack.org/doc/mlpack-git/doxygen/build.html#build_config) for more details, including a full list of options, and their default values. diff --git a/doc/guide/build.hpp b/doc/guide/build.hpp index e107edb7a2..76b7682b6f 100644 --- a/doc/guide/build.hpp +++ b/doc/guide/build.hpp @@ -182,7 +182,7 @@ The full list of options mlpack allows: and Gonum exist. (default OFF) - BUILD_JULIA_BINDINGS=(ON/OFF): compile Julia bindings, if Julia is found (default OFF) - - BUILD_SHARED_LIBS=(ON/OFF): compile shared libraries as opposed to + - BUILD_SHARED_LIBS=(ON/OFF): compile shared libraries and executables as opposed to static libraries (default ON) - TEST_VERBOSE=(ON/OFF): run test cases in \c mlpack_test with verbose output (default OFF) From 9613dab9e6640b478900d111ecec0d83b2a764e4 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Fri, 30 Apr 2021 02:10:47 +0200 Subject: [PATCH 46/89] Update README.md Co-authored-by: Ryan Curtin --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9d0e549f87..7f7f861e43 100644 --- a/README.md +++ b/README.md @@ -211,8 +211,8 @@ Options are specified with the -D flag. The allowed options include: BUILD_R_BINDINGS=(ON/OFF): whether or not to build R bindings R_EXECUTABLE=(/path/to/R): Path to specific R executable BUILD_TESTS=(ON/OFF): whether or not to build tests - BUILD_SHARED_LIBS=(ON/OFF): compile shared libraries and executables as opposed to - static libraries + BUILD_SHARED_LIBS=(ON/OFF): compile shared libraries and executables as + opposed to static libraries DISABLE_DOWNLOADS=(ON/OFF): whether to disable all downloads during build DOWNLOAD_ENSMALLEN=(ON/OFF): If ensmallen is not found, download it ENSMALLEN_INCLUDE_DIR=(/path/to/ensmallen/include): path to include directory From 2349dfad81c2514fe06a2049295de824f9535e66 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Fri, 30 Apr 2021 02:10:59 +0200 Subject: [PATCH 47/89] Fix indentation in src/mlpack/bindings/cli/CMakeLists.txt Co-authored-by: Ryan Curtin --- src/mlpack/bindings/cli/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/bindings/cli/CMakeLists.txt b/src/mlpack/bindings/cli/CMakeLists.txt index 40f8ac6d75..ee64bcf331 100644 --- a/src/mlpack/bindings/cli/CMakeLists.txt +++ b/src/mlpack/bindings/cli/CMakeLists.txt @@ -56,7 +56,7 @@ if (BUILD_CLI_EXECUTABLES) mlpack ${ARMADILLO_LIBRARIES} ${COMPILER_SUPPORT_LIBRARIES} - ) + ) else() # Build mlpack CLI binding binaries dynamically. target_link_libraries(mlpack_${name} From 6ce53a9fb1ef132e258a9db92f3bd2d47e5c2ad8 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sat, 1 May 2021 14:17:39 +0200 Subject: [PATCH 48/89] Find blas, refactor OpenBLAS. Signed-off-by: Omar Shrit --- CMake/ConfigureCrossCompile.cmake | 33 +++++++++++++++++++++++++++++++ CMake/FindArmadillo.cmake | 11 ++++++----- 2 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 CMake/ConfigureCrossCompile.cmake diff --git a/CMake/ConfigureCrossCompile.cmake b/CMake/ConfigureCrossCompile.cmake new file mode 100644 index 0000000000..023efc121b --- /dev/null +++ b/CMake/ConfigureCrossCompile.cmake @@ -0,0 +1,33 @@ +if (CMAKE_CROSSCOMPILING) + include(board/flags-config.cmake) + if(NOT CMAKE_SYSROOT AND (NOT TOOLCHAIN_PREFIX)) + message(FATAL_ERROR "Neither of CMAKE_SYSROOT or TOOLCHAIN_PREFIX is set, please set both of them and try again") + elseif(NOT CMAKE_SYSROOT) + message(FATAL_ERROR "Can not proceed CMAKE_SYSROOT is not set") + elseif(NOT TOOLCHAIN_PREFIX) + message(FATAL_ERROR "Cant not proceed TOOLCHAIN_PREFIXN is not set") + elseif(NOT OPENBLAS_TARGET) + message(FATAL_ERROR "Cant not proceed, board name is not set, please refer to documentation") + endif() +endif() + +macro(search_openblas version) + set(BLA_STATIC ON) + find_package(BLAS) + if (NOT BLAS_FOUND OR (NOT BLAS_LIBRARIES)) + get_deps(https://github.com/xianyi/OpenBLAS/releases/download/v${version}/OpenBLAS-${version}.tar.gz OpenBLAS OpenBLAS-${version}.tar.gz) + if (NOT MSVC) + if (NOT EXISTS "${CMAKE_BINARY_DIR}/deps/OpenBLAS-${version}/libopenblas.a") + execute_process(COMMAND make TARGET=${OPENBLAS_TARGET} BINARY=${OPENBLAS_BINARY} HOSTCC=gcc CC=${CMAKE_C_COMPILER} FC=${CMAKE_FORTRAN_COMPILER} NO_SHARED=1 + WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/deps/OpenBLAS-${version}) + endif() + file(GLOB OPENBLAS_LIBRARIES "${CMAKE_BINARY_DIR}/deps/OpenBLAS-${version}/libopenblas.a") + set(BLAS_LIBRARIES ${OPENBLAS_LIBRARIES}) + set(BLA_VENDOR OpenBLAS) + set(BLAS_FOUND ON) + endif() + endif() + find_library(GFORTRAN NAMES libgfortran.a) + find_library(PTHREAD NAMES libpthread.a) + set(COMPILER_SUPPORT_LIBRARIES ${COMPILER_SUPPORT_LIBRARIES} ${GFORTRAN} ${PTHREAD}) +endmacro() diff --git a/CMake/FindArmadillo.cmake b/CMake/FindArmadillo.cmake index 3d696b0abb..d0b5921684 100644 --- a/CMake/FindArmadillo.cmake +++ b/CMake/FindArmadillo.cmake @@ -91,12 +91,13 @@ if(NOT _ARMA_USE_WRAPPER OR MSVC) endif() endif() if(_ARMA_USE_BLAS) - if(ARMADILLO_FIND_QUIETLY OR NOT ARMADILLO_FIND_REQUIRED) - find_package(BLAS QUIET) + if(NOT BLAS_FOUND) + if(ARMADILLO_FIND_QUIETLY OR NOT ARMADILLO_FIND_REQUIRED) + find_package(BLAS QUIET) + else() + find_package(BLAS REQUIRED) + endif() else() - find_package(BLAS REQUIRED) - endif() - if(BLAS_FOUND) set(_ARMA_SUPPORT_LIBRARIES "${_ARMA_SUPPORT_LIBRARIES}" "${BLAS_LIBRARIES}") endif() endif() From f717bd62c7e58918b598a3a21cc263e7b9c98b1e Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sat, 1 May 2021 14:18:39 +0200 Subject: [PATCH 49/89] Add crosscompile files, add flags for several architectures Signed-off-by: Omar Shrit --- board/crosscompile-toolchain.cmake | 41 +++++++++++++++++ board/flags-config.cmake | 74 ++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 board/crosscompile-toolchain.cmake create mode 100644 board/flags-config.cmake diff --git a/board/crosscompile-toolchain.cmake b/board/crosscompile-toolchain.cmake new file mode 100644 index 0000000000..5bd40d5f6b --- /dev/null +++ b/board/crosscompile-toolchain.cmake @@ -0,0 +1,41 @@ +## This file handles cross-compilation configurations for aarch64, +## known as arm64. The objective of this file is to find and assign +## cross-compiler and the entire toolchain. +## It works best with buildroot toolchain, when using it the user +## needs to set the: TOOLCHAIN_PREFIX and CMAKE_SYSROOT from the +## command line. + +set(CMAKE_SYSTEM_NAME Linux) +set(CMAKE_SYSROOT "" CACHE STRING "CMAKE_SYSROOT") +set(TOOLCHAIN_PREFIX "" CACHE STRING "TOOLCHAIN_PREFIX") + +## In some distribution, a dynamic link for aarch64-linux-gnu-gcc may not be +## found or created, instead it might be labeled with the version at the end +## For instance: aarch64-linux-gnu-gcc-5 +## Therefore, if dynamic link exists, you do not have to specify the version +set(VERSION_NUMBER "" CACHE STRING "Enter the version number of the compiler") + +# Without that flag CMake is not able to pass test compilation check +set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) + +set(CMAKE_AR "${TOOLCHAIN_PREFIX}gcc-ar${VERSION_NUMBER}" CACHE FILEPATH "" FORCE) +set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}gcc${VERSION_NUMBER}) +set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}g++${VERSION_NUMBER}) +set(CMAKE_LINKER ${TOOLCHAIN_PREFIX}ld${VERSION_NUMBER}) +set(CMAKE_C_ARCHIVE_CREATE " qcs ") +set(CMAKE_C_ARCHIVE_FINISH true) +set(CMAKE_FORTRAN_COMPILER ${TOOLCHAIN_PREFIX}gfortran) +set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER}) +set(CMAKE_OBJCOPY ${TOOLCHAIN_PREFIX}objcopy${VERSION_NUMBER} CACHE INTERNAL "objcopy tool") +set(CMAKE_SIZE_UTIL ${TOOLCHAIN_PREFIX}size${VERSION_NUMBER} CACHE INTERNAL "size tool") + +## Here are the standard ROOT_PATH if you are using the standard toolchain +## if you are using a different toolchain you have to specify that too +set(CMAKE_FIND_ROOT_PATH "${CMAKE_SYSROOT}") + +set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --sysroot=${CMAKE_SYSROOT}" CACHE INTERNAL "" FORCE) + +set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) diff --git a/board/flags-config.cmake b/board/flags-config.cmake new file mode 100644 index 0000000000..5e7eb0af59 --- /dev/null +++ b/board/flags-config.cmake @@ -0,0 +1,74 @@ +# This function provides a set of specific flags for each supported board +# Depending on the processor type. The objective is to optimize for size. +# Thus, all of the fllowing flags are chosen carefully to reduce binary +# footprints. + +# Set generic minimization flags for all platforms. +# These flags are the same for all cross-compilation cases. +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Os -fdata-sections -ffunction-sections") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fomit-frame-pointer -fno-unwind-tables") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-asynchronous-unwind-tables -fvisibility=hidden") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fshort-enums -finline-small-functions") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -findirect-inlining -fno-common") +#-flto -fuse-ld=gold # There is an issue with gold link when compiling on +# Ubuntu 16. At that point gcc linker did not integrate the flto support +# inside and it was a separate plugin that need to be added. Therefore, +# this can be added when mlpack Azure CI moves toward Ubuntu 20. + +option(RPI0 "Optimize compiler flags for Raspberry PI 0." OFF) +option(RPI1 "Optimize compiler flags for Raspberry PI 1." OFF) +option(RPI2 "Optimize compiler flags for Raspberry PI 2." OFF) +option(RPI3 "Optimize compiler flags for Raspberry PI 3." OFF) +option(RPI4 "Optimize compiler flags for Raspberry PI 4." OFF) +option(BV "Optimize compiler flags for Beagleboard V." OFF) +option(JETSONAGX "Optimize compiler flags for Nvidia Jetson AGX Xavier." OFF) +option(KATAMI "Optimize compiler flags for Pentium 3 Katami processors." OFF) +option(COPPERMINE "Optimize compiler flags for Pentium 3 Coppermine processors." OFF) +option(NORTHWOOD "Optimize compiler flags for Pentium 4 Northwood processors." OFF) + +# Set specific platforms CMAKE CXX flags. +if(RPI0 OR RPI1) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mtune=arm1176jzf-s") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") + set(OPENBLAS_TARGET "ARMV6") + set(OPENBLAS_BINARY "32") +elseif(RPI2) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mtune=cortex-a7") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") + set(OPENBLAS_TARGET "ARMV7") + set(OPENBLAS_BINARY "32") +elseif(RPI3) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mtune=cortex-a53") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") + set(OPENBLAS_TARGET "CORTEXA53") + set(OPENBLAS_BINARY "64") +elseif(RPI4) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mtune=cortex-a72") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") + set(OPENBLAS_TARGET "CORTEXA72") + set(OPENBLAS_BINARY "64") +elseif(BV) + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") + set(OPENBLAS_TARGET "RISCV64_GENERIC") + set(OPENBLAS_BINARY "64") +elseif(JETSONAGX) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -matune=cortex-a76") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") + set(OPENBLAS_TARGET "ARM8") + set(OPENBLAS_BINARY "64") +elseif(KATAMI) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=pentium3") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") + set(OPENBLAS_TARGET "KATAMI") + set(OPENBLAS_BINARY "32") +elseif(COPPERMINE) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=pentium3") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") + set(OPENBLAS_TARGET "COPPERMINE") + set(OPENBLAS_BINARY "32") +elseif(NORTHWOOD) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=pentium4") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") + set(OPENBLAS_TARGET "NORTHWOOD") + set(OPENBLAS_BINARY "32") +endif() From 5eec177b74486c11744789b397f51b9cf5a8ee2b Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sat, 1 May 2021 14:20:56 +0200 Subject: [PATCH 50/89] Add minor Cmake configuration to adapt crosscompilation Signed-off-by: Omar Shrit --- CMakeLists.txt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9a2c55aaa0..ccba88c7f8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,7 @@ project(mlpack C CXX) include(CMake/cotire.cmake) include(CMake/CheckHash.cmake) include(CMake/Autodownload.cmake) +include(CMake/ConfigureCrossCompile.cmake) # First, define all the compilation options. # We default to debugging mode for developers. @@ -33,6 +34,9 @@ if (WIN32) set(DLL_COPY_DIRS "" CACHE STRING "List of directories (separated by ';') containing DLLs to copy for runtime.") set(DLL_COPY_LIBS "" CACHE STRING "List of DLLs (separated by ';') that should be copied for runtime.") +elseif(CMAKE_CROSSCOMPILING) + option(BUILD_SHARED_LIBS + "Compile shared libraries (if OFF, static libraries and binaries are compiled)." OFF) else() option(BUILD_SHARED_LIBS "Compile shared libraries (if OFF, static libraries and binaries are compiled)." ON) @@ -265,6 +269,10 @@ endif() # STB_IMAGE_INCLUDE_DIR - include directory for STB image library # MATHJAX_ROOT - root of MathJax installation +if (CMAKE_CROSSCOMPILING) + search_openblas(0.3.13) +endif() + if (DISABLE_DOWNLOADS) find_package(Armadillo "${ARMADILLO_VERSION}" REQUIRED) else() From a9b27768af16fc86a315279287fc24aabe62f28a Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Mon, 3 May 2021 20:17:54 +0200 Subject: [PATCH 51/89] Add comments, set BLAS_openblas_LIBRARIES, reset FindArmadillo changes Signed-off-by: Omar Shrit --- CMake/ConfigureCrossCompile.cmake | 9 +++++++++ CMake/FindArmadillo.cmake | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/CMake/ConfigureCrossCompile.cmake b/CMake/ConfigureCrossCompile.cmake index 023efc121b..58757da1b2 100644 --- a/CMake/ConfigureCrossCompile.cmake +++ b/CMake/ConfigureCrossCompile.cmake @@ -1,3 +1,10 @@ +# This file adds the necessary configurations to cross compile +# mlpack for embedde system. You need to set the following variables +# from the command line: the CMAKE_SYSROOT, TOOLCHAIN_PREFIX and the +# board type. +# This file will compile OpenBLAS if it is downloaded and it is not +# available on you system in order to find the BLAS library. + if (CMAKE_CROSSCOMPILING) include(board/flags-config.cmake) if(NOT CMAKE_SYSROOT AND (NOT TOOLCHAIN_PREFIX)) @@ -23,6 +30,8 @@ macro(search_openblas version) endif() file(GLOB OPENBLAS_LIBRARIES "${CMAKE_BINARY_DIR}/deps/OpenBLAS-${version}/libopenblas.a") set(BLAS_LIBRARIES ${OPENBLAS_LIBRARIES}) + set(BLAS_openblas_LIBRARIES ${OPENBLAS_LIBRARIES}) + message(STATUS "SHOW BLAS libraries: ${BLAS_LIBRARIES}") set(BLA_VENDOR OpenBLAS) set(BLAS_FOUND ON) endif() diff --git a/CMake/FindArmadillo.cmake b/CMake/FindArmadillo.cmake index d0b5921684..e1ba5b2221 100644 --- a/CMake/FindArmadillo.cmake +++ b/CMake/FindArmadillo.cmake @@ -91,13 +91,13 @@ if(NOT _ARMA_USE_WRAPPER OR MSVC) endif() endif() if(_ARMA_USE_BLAS) - if(NOT BLAS_FOUND) +# if(NOT BLAS_FOUND) if(ARMADILLO_FIND_QUIETLY OR NOT ARMADILLO_FIND_REQUIRED) find_package(BLAS QUIET) else() find_package(BLAS REQUIRED) endif() - else() + if(BLAS_FOUND) set(_ARMA_SUPPORT_LIBRARIES "${_ARMA_SUPPORT_LIBRARIES}" "${BLAS_LIBRARIES}") endif() endif() From f877c57fbc4c50950a79bd4f4f51a427e6b05ed7 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Mon, 3 May 2021 22:43:14 +0200 Subject: [PATCH 52/89] Update ConfigureCrossCompile.cmake --- CMake/ConfigureCrossCompile.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMake/ConfigureCrossCompile.cmake b/CMake/ConfigureCrossCompile.cmake index 58757da1b2..77f73478f6 100644 --- a/CMake/ConfigureCrossCompile.cmake +++ b/CMake/ConfigureCrossCompile.cmake @@ -1,5 +1,5 @@ # This file adds the necessary configurations to cross compile -# mlpack for embedde system. You need to set the following variables +# mlpack for embedded systems. You need to set the following variables # from the command line: the CMAKE_SYSROOT, TOOLCHAIN_PREFIX and the # board type. # This file will compile OpenBLAS if it is downloaded and it is not From 921ec4fe72ce7d5e7533df93216796bdfe02702a Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Mon, 3 May 2021 23:21:37 +0200 Subject: [PATCH 53/89] Print another message to see on CI Signed-off-by: Omar Shrit --- CMake/ConfigureCrossCompile.cmake | 1 + 1 file changed, 1 insertion(+) diff --git a/CMake/ConfigureCrossCompile.cmake b/CMake/ConfigureCrossCompile.cmake index 58757da1b2..1943f4a2bc 100644 --- a/CMake/ConfigureCrossCompile.cmake +++ b/CMake/ConfigureCrossCompile.cmake @@ -39,4 +39,5 @@ macro(search_openblas version) find_library(GFORTRAN NAMES libgfortran.a) find_library(PTHREAD NAMES libpthread.a) set(COMPILER_SUPPORT_LIBRARIES ${COMPILER_SUPPORT_LIBRARIES} ${GFORTRAN} ${PTHREAD}) + message(STATUS "SHOW BLAS libraries 2: ${BLAS_LIBRARIES}") endmacro() From 52330fe587c7950c4adb065d626ee7ecb4e0b416 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Mon, 3 May 2021 23:25:50 +0200 Subject: [PATCH 54/89] Add dot in board/crosscompile-toolchain.cmake Co-authored-by: Marcus Edel --- board/crosscompile-toolchain.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/board/crosscompile-toolchain.cmake b/board/crosscompile-toolchain.cmake index 5bd40d5f6b..ce65a4e089 100644 --- a/board/crosscompile-toolchain.cmake +++ b/board/crosscompile-toolchain.cmake @@ -12,7 +12,7 @@ set(TOOLCHAIN_PREFIX "" CACHE STRING "TOOLCHAIN_PREFIX") ## In some distribution, a dynamic link for aarch64-linux-gnu-gcc may not be ## found or created, instead it might be labeled with the version at the end ## For instance: aarch64-linux-gnu-gcc-5 -## Therefore, if dynamic link exists, you do not have to specify the version +## Therefore, if dynamic link exists, you do not have to specify the version. set(VERSION_NUMBER "" CACHE STRING "Enter the version number of the compiler") # Without that flag CMake is not able to pass test compilation check From 5889fa3cef38292f4d09cd6ee817bd9ead523955 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Mon, 3 May 2021 23:26:23 +0200 Subject: [PATCH 55/89] Update CMake/ConfigureCrossCompile.cmake Co-authored-by: Marcus Edel --- CMake/ConfigureCrossCompile.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMake/ConfigureCrossCompile.cmake b/CMake/ConfigureCrossCompile.cmake index f20d168604..feaac23b33 100644 --- a/CMake/ConfigureCrossCompile.cmake +++ b/CMake/ConfigureCrossCompile.cmake @@ -3,7 +3,7 @@ # from the command line: the CMAKE_SYSROOT, TOOLCHAIN_PREFIX and the # board type. # This file will compile OpenBLAS if it is downloaded and it is not -# available on you system in order to find the BLAS library. +# available on your system in order to find the BLAS library. if (CMAKE_CROSSCOMPILING) include(board/flags-config.cmake) From b9bc2399e4897af2c10d28d520702296b72feb44 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Mon, 3 May 2021 23:26:32 +0200 Subject: [PATCH 56/89] Update CMake/ConfigureCrossCompile.cmake Co-authored-by: Marcus Edel --- CMake/ConfigureCrossCompile.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMake/ConfigureCrossCompile.cmake b/CMake/ConfigureCrossCompile.cmake index feaac23b33..540f041a4b 100644 --- a/CMake/ConfigureCrossCompile.cmake +++ b/CMake/ConfigureCrossCompile.cmake @@ -12,7 +12,7 @@ if (CMAKE_CROSSCOMPILING) elseif(NOT CMAKE_SYSROOT) message(FATAL_ERROR "Can not proceed CMAKE_SYSROOT is not set") elseif(NOT TOOLCHAIN_PREFIX) - message(FATAL_ERROR "Cant not proceed TOOLCHAIN_PREFIXN is not set") + message(FATAL_ERROR "Cant not proceed TOOLCHAIN_PREFIX is not set") elseif(NOT OPENBLAS_TARGET) message(FATAL_ERROR "Cant not proceed, board name is not set, please refer to documentation") endif() From 09324718c160150395edfa3fa2d2d17db673de44 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Mon, 3 May 2021 23:27:07 +0200 Subject: [PATCH 57/89] Fix style in CMake/ConfigureCrossCompile.cmake Co-authored-by: Marcus Edel --- CMake/ConfigureCrossCompile.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMake/ConfigureCrossCompile.cmake b/CMake/ConfigureCrossCompile.cmake index 540f041a4b..bb813754a7 100644 --- a/CMake/ConfigureCrossCompile.cmake +++ b/CMake/ConfigureCrossCompile.cmake @@ -21,7 +21,7 @@ endif() macro(search_openblas version) set(BLA_STATIC ON) find_package(BLAS) - if (NOT BLAS_FOUND OR (NOT BLAS_LIBRARIES)) + if(NOT BLAS_FOUND OR (NOT BLAS_LIBRARIES)) get_deps(https://github.com/xianyi/OpenBLAS/releases/download/v${version}/OpenBLAS-${version}.tar.gz OpenBLAS OpenBLAS-${version}.tar.gz) if (NOT MSVC) if (NOT EXISTS "${CMAKE_BINARY_DIR}/deps/OpenBLAS-${version}/libopenblas.a") From 4ad7db5a30dac398bd9d8b6e774dc98f83db02b7 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Mon, 3 May 2021 23:27:25 +0200 Subject: [PATCH 58/89] Add dot in board/crosscompile-toolchain.cmake Co-authored-by: Marcus Edel --- board/crosscompile-toolchain.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/board/crosscompile-toolchain.cmake b/board/crosscompile-toolchain.cmake index ce65a4e089..7dd36333cf 100644 --- a/board/crosscompile-toolchain.cmake +++ b/board/crosscompile-toolchain.cmake @@ -15,7 +15,7 @@ set(TOOLCHAIN_PREFIX "" CACHE STRING "TOOLCHAIN_PREFIX") ## Therefore, if dynamic link exists, you do not have to specify the version. set(VERSION_NUMBER "" CACHE STRING "Enter the version number of the compiler") -# Without that flag CMake is not able to pass test compilation check +# Without that flag CMake is not able to pass test compilation check. set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) set(CMAKE_AR "${TOOLCHAIN_PREFIX}gcc-ar${VERSION_NUMBER}" CACHE FILEPATH "" FORCE) From 9f56f2c94f5f120b12f8151bce18c436bba27a6e Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Mon, 3 May 2021 23:27:42 +0200 Subject: [PATCH 59/89] Add missing period in board/crosscompile-toolchain.cmake Co-authored-by: Marcus Edel --- board/crosscompile-toolchain.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/board/crosscompile-toolchain.cmake b/board/crosscompile-toolchain.cmake index 7dd36333cf..bcbe612af4 100644 --- a/board/crosscompile-toolchain.cmake +++ b/board/crosscompile-toolchain.cmake @@ -30,7 +30,7 @@ set(CMAKE_OBJCOPY ${TOOLCHAIN_PREFIX}objcopy${VERSION_NUMBER} CACHE INTERNAL "ob set(CMAKE_SIZE_UTIL ${TOOLCHAIN_PREFIX}size${VERSION_NUMBER} CACHE INTERNAL "size tool") ## Here are the standard ROOT_PATH if you are using the standard toolchain -## if you are using a different toolchain you have to specify that too +## if you are using a different toolchain you have to specify that too. set(CMAKE_FIND_ROOT_PATH "${CMAKE_SYSROOT}") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --sysroot=${CMAKE_SYSROOT}" CACHE INTERNAL "" FORCE) From ae5db236072a7f17a3bec2e399ff68e26121e99b Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Mon, 3 May 2021 23:37:46 +0200 Subject: [PATCH 60/89] Change back to board_name Signed-off-by: Omar Shrit --- board/flags-config.cmake | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/board/flags-config.cmake b/board/flags-config.cmake index 5e7eb0af59..ae65619e45 100644 --- a/board/flags-config.cmake +++ b/board/flags-config.cmake @@ -15,58 +15,49 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -findirect-inlining -fno-common") # inside and it was a separate plugin that need to be added. Therefore, # this can be added when mlpack Azure CI moves toward Ubuntu 20. -option(RPI0 "Optimize compiler flags for Raspberry PI 0." OFF) -option(RPI1 "Optimize compiler flags for Raspberry PI 1." OFF) -option(RPI2 "Optimize compiler flags for Raspberry PI 2." OFF) -option(RPI3 "Optimize compiler flags for Raspberry PI 3." OFF) -option(RPI4 "Optimize compiler flags for Raspberry PI 4." OFF) -option(BV "Optimize compiler flags for Beagleboard V." OFF) -option(JETSONAGX "Optimize compiler flags for Nvidia Jetson AGX Xavier." OFF) -option(KATAMI "Optimize compiler flags for Pentium 3 Katami processors." OFF) -option(COPPERMINE "Optimize compiler flags for Pentium 3 Coppermine processors." OFF) -option(NORTHWOOD "Optimize compiler flags for Pentium 4 Northwood processors." OFF) +set(BOARD_NAME "Optimize compiler flags for a specific board.") # Set specific platforms CMAKE CXX flags. -if(RPI0 OR RPI1) +if(BOARD_NAME MATCHES "RPI0" OR BOARD_NAME MATCHES "RPI1") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mtune=arm1176jzf-s") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") set(OPENBLAS_TARGET "ARMV6") set(OPENBLAS_BINARY "32") -elseif(RPI2) +elseif(BOARD_NAME MATCHES "RPI2") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mtune=cortex-a7") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") set(OPENBLAS_TARGET "ARMV7") set(OPENBLAS_BINARY "32") -elseif(RPI3) +elseif(BOARD_NAME MATCHES "RPI3") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mtune=cortex-a53") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") set(OPENBLAS_TARGET "CORTEXA53") set(OPENBLAS_BINARY "64") -elseif(RPI4) +elseif(BOARD_NAME MATCHES "RPI4") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mtune=cortex-a72") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") set(OPENBLAS_TARGET "CORTEXA72") set(OPENBLAS_BINARY "64") -elseif(BV) +elseif(BOARD_NAME MATCHES "BV") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") set(OPENBLAS_TARGET "RISCV64_GENERIC") set(OPENBLAS_BINARY "64") -elseif(JETSONAGX) +elseif(BOARD_NAME MATCHES "JETSONAGX") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -matune=cortex-a76") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") set(OPENBLAS_TARGET "ARM8") set(OPENBLAS_BINARY "64") -elseif(KATAMI) +elseif(BOARD_NAME MATCHES "KATAMI") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=pentium3") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") set(OPENBLAS_TARGET "KATAMI") set(OPENBLAS_BINARY "32") -elseif(COPPERMINE) +elseif(BOARD_NAME MATCHES "COPPERMINE") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=pentium3") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") set(OPENBLAS_TARGET "COPPERMINE") set(OPENBLAS_BINARY "32") -elseif(NORTHWOOD) +elseif(BOARD_NAME MATCHES "NORTHWOOD") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=pentium4") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") set(OPENBLAS_TARGET "NORTHWOOD") From aa989db797b146995b37d352597bf19574eedc5f Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Mon, 3 May 2021 23:51:15 +0200 Subject: [PATCH 61/89] Set up the variable correctly Signed-off-by: Omar Shrit --- board/flags-config.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/board/flags-config.cmake b/board/flags-config.cmake index ae65619e45..83cf7ac46d 100644 --- a/board/flags-config.cmake +++ b/board/flags-config.cmake @@ -15,7 +15,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -findirect-inlining -fno-common") # inside and it was a separate plugin that need to be added. Therefore, # this can be added when mlpack Azure CI moves toward Ubuntu 20. -set(BOARD_NAME "Optimize compiler flags for a specific board.") +set(BOARD_NAME "" CACHE STRING "Specify Board name to optimize for.") # Set specific platforms CMAKE CXX flags. if(BOARD_NAME MATCHES "RPI0" OR BOARD_NAME MATCHES "RPI1") From ea5a69f4d1a0080bb54a97c4a73dc75a84baae3f Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Tue, 4 May 2021 00:32:43 +0200 Subject: [PATCH 62/89] Set BLAS_openblas_LIBRARY instead of LIBRARIES Signed-off-by: Omar Shrit --- CMake/ConfigureCrossCompile.cmake | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CMake/ConfigureCrossCompile.cmake b/CMake/ConfigureCrossCompile.cmake index bb813754a7..45fb984acf 100644 --- a/CMake/ConfigureCrossCompile.cmake +++ b/CMake/ConfigureCrossCompile.cmake @@ -7,7 +7,7 @@ if (CMAKE_CROSSCOMPILING) include(board/flags-config.cmake) - if(NOT CMAKE_SYSROOT AND (NOT TOOLCHAIN_PREFIX)) + if (NOT CMAKE_SYSROOT AND (NOT TOOLCHAIN_PREFIX)) message(FATAL_ERROR "Neither of CMAKE_SYSROOT or TOOLCHAIN_PREFIX is set, please set both of them and try again") elseif(NOT CMAKE_SYSROOT) message(FATAL_ERROR "Can not proceed CMAKE_SYSROOT is not set") @@ -21,7 +21,7 @@ endif() macro(search_openblas version) set(BLA_STATIC ON) find_package(BLAS) - if(NOT BLAS_FOUND OR (NOT BLAS_LIBRARIES)) + if (NOT BLAS_FOUND OR (NOT BLAS_LIBRARIES)) get_deps(https://github.com/xianyi/OpenBLAS/releases/download/v${version}/OpenBLAS-${version}.tar.gz OpenBLAS OpenBLAS-${version}.tar.gz) if (NOT MSVC) if (NOT EXISTS "${CMAKE_BINARY_DIR}/deps/OpenBLAS-${version}/libopenblas.a") @@ -30,7 +30,7 @@ macro(search_openblas version) endif() file(GLOB OPENBLAS_LIBRARIES "${CMAKE_BINARY_DIR}/deps/OpenBLAS-${version}/libopenblas.a") set(BLAS_LIBRARIES ${OPENBLAS_LIBRARIES}) - set(BLAS_openblas_LIBRARIES ${OPENBLAS_LIBRARIES}) + set(BLAS_openblas_LIBRARY ${OPENBLAS_LIBRARIES}) message(STATUS "SHOW BLAS libraries: ${BLAS_LIBRARIES}") set(BLA_VENDOR OpenBLAS) set(BLAS_FOUND ON) From f47788e3902e33562d682663659acadf525531a0 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Tue, 4 May 2021 00:51:07 +0200 Subject: [PATCH 63/89] Remove BLAS_LIBRARIES and test without it Signed-off-by: Omar Shrit --- CMake/ConfigureCrossCompile.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMake/ConfigureCrossCompile.cmake b/CMake/ConfigureCrossCompile.cmake index 45fb984acf..87e47a6472 100644 --- a/CMake/ConfigureCrossCompile.cmake +++ b/CMake/ConfigureCrossCompile.cmake @@ -29,7 +29,7 @@ macro(search_openblas version) WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/deps/OpenBLAS-${version}) endif() file(GLOB OPENBLAS_LIBRARIES "${CMAKE_BINARY_DIR}/deps/OpenBLAS-${version}/libopenblas.a") - set(BLAS_LIBRARIES ${OPENBLAS_LIBRARIES}) + # set(BLAS_LIBRARIES ${OPENBLAS_LIBRARIES}) set(BLAS_openblas_LIBRARY ${OPENBLAS_LIBRARIES}) message(STATUS "SHOW BLAS libraries: ${BLAS_LIBRARIES}") set(BLA_VENDOR OpenBLAS) From 45a531ccbbf2a46657301285fb4f84ec6ef63f1c Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Tue, 4 May 2021 01:09:45 +0200 Subject: [PATCH 64/89] Set LAPACK_openblas_LIBRARY ONLY Signed-off-by: Omar Shrit --- CMake/ConfigureCrossCompile.cmake | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CMake/ConfigureCrossCompile.cmake b/CMake/ConfigureCrossCompile.cmake index 87e47a6472..498f71962a 100644 --- a/CMake/ConfigureCrossCompile.cmake +++ b/CMake/ConfigureCrossCompile.cmake @@ -30,7 +30,8 @@ macro(search_openblas version) endif() file(GLOB OPENBLAS_LIBRARIES "${CMAKE_BINARY_DIR}/deps/OpenBLAS-${version}/libopenblas.a") # set(BLAS_LIBRARIES ${OPENBLAS_LIBRARIES}) - set(BLAS_openblas_LIBRARY ${OPENBLAS_LIBRARIES}) + # set(BLAS_openblas_LIBRARY ${OPENBLAS_LIBRARIES}) + set(LAPACK_openblas_LIBRARY ${OPENBLAS_LIBRARIES}) message(STATUS "SHOW BLAS libraries: ${BLAS_LIBRARIES}") set(BLA_VENDOR OpenBLAS) set(BLAS_FOUND ON) From 2014891496bcd9bbbd3a62d4c120bc6c24a3962f Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Tue, 4 May 2021 01:34:14 +0200 Subject: [PATCH 65/89] Try both lapack and blas for openblas Signed-off-by: Omar Shrit --- CMake/ConfigureCrossCompile.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMake/ConfigureCrossCompile.cmake b/CMake/ConfigureCrossCompile.cmake index 498f71962a..91b61b9bfb 100644 --- a/CMake/ConfigureCrossCompile.cmake +++ b/CMake/ConfigureCrossCompile.cmake @@ -30,7 +30,7 @@ macro(search_openblas version) endif() file(GLOB OPENBLAS_LIBRARIES "${CMAKE_BINARY_DIR}/deps/OpenBLAS-${version}/libopenblas.a") # set(BLAS_LIBRARIES ${OPENBLAS_LIBRARIES}) - # set(BLAS_openblas_LIBRARY ${OPENBLAS_LIBRARIES}) + set(BLAS_openblas_LIBRARY ${OPENBLAS_LIBRARIES}) set(LAPACK_openblas_LIBRARY ${OPENBLAS_LIBRARIES}) message(STATUS "SHOW BLAS libraries: ${BLAS_LIBRARIES}") set(BLA_VENDOR OpenBLAS) From f6ae407829f26bf43b323cabbd8e080097c8ab2c Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Tue, 4 May 2021 01:45:27 +0200 Subject: [PATCH 66/89] Test LAPACK and BLAS LIBRARIES Signed-off-by: Omar Shrit --- CMake/ConfigureCrossCompile.cmake | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/CMake/ConfigureCrossCompile.cmake b/CMake/ConfigureCrossCompile.cmake index 91b61b9bfb..3078d400db 100644 --- a/CMake/ConfigureCrossCompile.cmake +++ b/CMake/ConfigureCrossCompile.cmake @@ -29,9 +29,10 @@ macro(search_openblas version) WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/deps/OpenBLAS-${version}) endif() file(GLOB OPENBLAS_LIBRARIES "${CMAKE_BINARY_DIR}/deps/OpenBLAS-${version}/libopenblas.a") - # set(BLAS_LIBRARIES ${OPENBLAS_LIBRARIES}) - set(BLAS_openblas_LIBRARY ${OPENBLAS_LIBRARIES}) - set(LAPACK_openblas_LIBRARY ${OPENBLAS_LIBRARIES}) + set(BLAS_LIBRARIES ${OPENBLAS_LIBRARIES}) + set(LAPACK_LIBRARIES ${OPENBLAS_LIBRARIES}) + # set(BLAS_openblas_LIBRARY ${OPENBLAS_LIBRARIES}) + # set(LAPACK_openblas_LIBRARY ${OPENBLAS_LIBRARIES}) message(STATUS "SHOW BLAS libraries: ${BLAS_LIBRARIES}") set(BLA_VENDOR OpenBLAS) set(BLAS_FOUND ON) From 98413832a2f814e0b4569822adf0298a5ad2eb9d Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Tue, 4 May 2021 15:42:14 +0200 Subject: [PATCH 67/89] Let us recheck LAPACK and BLAS Signed-off-by: Omar Shrit --- CMake/ConfigureCrossCompile.cmake | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/CMake/ConfigureCrossCompile.cmake b/CMake/ConfigureCrossCompile.cmake index 3078d400db..dea8e6e139 100644 --- a/CMake/ConfigureCrossCompile.cmake +++ b/CMake/ConfigureCrossCompile.cmake @@ -29,10 +29,10 @@ macro(search_openblas version) WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/deps/OpenBLAS-${version}) endif() file(GLOB OPENBLAS_LIBRARIES "${CMAKE_BINARY_DIR}/deps/OpenBLAS-${version}/libopenblas.a") - set(BLAS_LIBRARIES ${OPENBLAS_LIBRARIES}) - set(LAPACK_LIBRARIES ${OPENBLAS_LIBRARIES}) - # set(BLAS_openblas_LIBRARY ${OPENBLAS_LIBRARIES}) - # set(LAPACK_openblas_LIBRARY ${OPENBLAS_LIBRARIES}) + # set(BLAS_LIBRARIES ${OPENBLAS_LIBRARIES}) + # set(LAPACK_LIBRARIES ${OPENBLAS_LIBRARIES}) + set(BLAS_openblas_LIBRARY ${OPENBLAS_LIBRARIES}) + set(LAPACK_openblas_LIBRARY ${OPENBLAS_LIBRARIES}) message(STATUS "SHOW BLAS libraries: ${BLAS_LIBRARIES}") set(BLA_VENDOR OpenBLAS) set(BLAS_FOUND ON) From aeb09ea5af9c10eec1325c29e99f817ab99d47c9 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Tue, 4 May 2021 15:49:50 +0200 Subject: [PATCH 68/89] Add comment for crosscompiling in CMakeLists Signed-off-by: Omar Shrit --- CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index ccba88c7f8..749b1dea72 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -269,6 +269,8 @@ endif() # STB_IMAGE_INCLUDE_DIR - include directory for STB image library # MATHJAX_ROOT - root of MathJax installation +# Download and compile OpenBLAS if we are cross compiling mlpack for a specific +# architecture. The function takes the version of OpenBLAS as variable. if (CMAKE_CROSSCOMPILING) search_openblas(0.3.13) endif() From 9e7902b18df5e4ffacf10727d6cbff68dd252305 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Tue, 4 May 2021 16:01:58 +0200 Subject: [PATCH 69/89] TOUPPER string, add comments for buildroot Signed-off-by: Omar Shrit --- board/crosscompile-toolchain.cmake | 3 +++ board/flags-config.cmake | 19 ++++++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/board/crosscompile-toolchain.cmake b/board/crosscompile-toolchain.cmake index bcbe612af4..dc001ab6e8 100644 --- a/board/crosscompile-toolchain.cmake +++ b/board/crosscompile-toolchain.cmake @@ -4,6 +4,9 @@ ## It works best with buildroot toolchain, when using it the user ## needs to set the: TOOLCHAIN_PREFIX and CMAKE_SYSROOT from the ## command line. +## Currently, we recommend using buildroot toolchain for +## cross-compilation. Here is the link to download the toolchains: +## https://toolchains.bootlin.com/ set(CMAKE_SYSTEM_NAME Linux) set(CMAKE_SYSROOT "" CACHE STRING "CMAKE_SYSROOT") diff --git a/board/flags-config.cmake b/board/flags-config.cmake index 83cf7ac46d..5b0972f7c5 100644 --- a/board/flags-config.cmake +++ b/board/flags-config.cmake @@ -16,48 +16,49 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -findirect-inlining -fno-common") # this can be added when mlpack Azure CI moves toward Ubuntu 20. set(BOARD_NAME "" CACHE STRING "Specify Board name to optimize for.") +string(TOUPPER BOARD_NAME BOARD) # Set specific platforms CMAKE CXX flags. -if(BOARD_NAME MATCHES "RPI0" OR BOARD_NAME MATCHES "RPI1") +if(BOARD MATCHES "RPI0" OR BOARD MATCHES "RPI1") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mtune=arm1176jzf-s") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") set(OPENBLAS_TARGET "ARMV6") set(OPENBLAS_BINARY "32") -elseif(BOARD_NAME MATCHES "RPI2") +elseif(BOARD MATCHES "RPI2") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mtune=cortex-a7") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") set(OPENBLAS_TARGET "ARMV7") set(OPENBLAS_BINARY "32") -elseif(BOARD_NAME MATCHES "RPI3") +elseif(BOARD MATCHES "RPI3") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mtune=cortex-a53") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") set(OPENBLAS_TARGET "CORTEXA53") set(OPENBLAS_BINARY "64") -elseif(BOARD_NAME MATCHES "RPI4") +elseif(BOARD MATCHES "RPI4") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mtune=cortex-a72") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") set(OPENBLAS_TARGET "CORTEXA72") set(OPENBLAS_BINARY "64") -elseif(BOARD_NAME MATCHES "BV") +elseif(BOARD MATCHES "BV") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") set(OPENBLAS_TARGET "RISCV64_GENERIC") set(OPENBLAS_BINARY "64") -elseif(BOARD_NAME MATCHES "JETSONAGX") +elseif(BOARD MATCHES "JETSONAGX") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -matune=cortex-a76") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") set(OPENBLAS_TARGET "ARM8") set(OPENBLAS_BINARY "64") -elseif(BOARD_NAME MATCHES "KATAMI") +elseif(BOARD MATCHES "KATAMI") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=pentium3") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") set(OPENBLAS_TARGET "KATAMI") set(OPENBLAS_BINARY "32") -elseif(BOARD_NAME MATCHES "COPPERMINE") +elseif(BOARD MATCHES "COPPERMINE") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=pentium3") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") set(OPENBLAS_TARGET "COPPERMINE") set(OPENBLAS_BINARY "32") -elseif(BOARD_NAME MATCHES "NORTHWOOD") +elseif(BOARD MATCHES "NORTHWOOD") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=pentium4") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") set(OPENBLAS_TARGET "NORTHWOOD") From 256e81d80b4cca8d27fb56473e0478d874299ee2 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Tue, 4 May 2021 16:10:21 +0200 Subject: [PATCH 70/89] Move openblas check Signed-off-by: Omar Shrit --- CMake/ConfigureCrossCompile.cmake | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CMake/ConfigureCrossCompile.cmake b/CMake/ConfigureCrossCompile.cmake index dea8e6e139..41e8954802 100644 --- a/CMake/ConfigureCrossCompile.cmake +++ b/CMake/ConfigureCrossCompile.cmake @@ -13,8 +13,6 @@ if (CMAKE_CROSSCOMPILING) message(FATAL_ERROR "Can not proceed CMAKE_SYSROOT is not set") elseif(NOT TOOLCHAIN_PREFIX) message(FATAL_ERROR "Cant not proceed TOOLCHAIN_PREFIX is not set") - elseif(NOT OPENBLAS_TARGET) - message(FATAL_ERROR "Cant not proceed, board name is not set, please refer to documentation") endif() endif() @@ -22,6 +20,9 @@ macro(search_openblas version) set(BLA_STATIC ON) find_package(BLAS) if (NOT BLAS_FOUND OR (NOT BLAS_LIBRARIES)) + if(NOT OPENBLAS_TARGET) + message(FATAL_ERROR "Cant not proceed, OPENBLAS_TARGET is not set, and to either set that or BOARD_NAME") + endif() get_deps(https://github.com/xianyi/OpenBLAS/releases/download/v${version}/OpenBLAS-${version}.tar.gz OpenBLAS OpenBLAS-${version}.tar.gz) if (NOT MSVC) if (NOT EXISTS "${CMAKE_BINARY_DIR}/deps/OpenBLAS-${version}/libopenblas.a") From 40472cf7b688c4f587b6e8eb186aff6507b63a98 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Tue, 4 May 2021 16:28:21 +0200 Subject: [PATCH 71/89] Update boost link Signed-off-by: Omar Shrit --- CMakeLists.txt | 2 +- board/flags-config.cmake | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 749b1dea72..daf416f5e2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -366,7 +366,7 @@ if (DISABLE_DOWNLOADS) else() find_package(Boost "${BOOST_VERSION}") if (NOT Boost_FOUND) - get_deps(https://dl.bintray.com/boostorg/release/1.75.0/source/boost_1_75_0.tar.gz boost boost_1_75_0.tar.gz) + get_deps(https://boostorg.jfrog.io/artifactory/main/release/1.76.0/source/boost_1_76_0.tar.gz boost boost_1_76_0.tar.gz) find_package(Boost REQUIRED) endif() endif() diff --git a/board/flags-config.cmake b/board/flags-config.cmake index 5b0972f7c5..3c19e4a17b 100644 --- a/board/flags-config.cmake +++ b/board/flags-config.cmake @@ -16,7 +16,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -findirect-inlining -fno-common") # this can be added when mlpack Azure CI moves toward Ubuntu 20. set(BOARD_NAME "" CACHE STRING "Specify Board name to optimize for.") -string(TOUPPER BOARD_NAME BOARD) +string(TOUPPER ${BOARD_NAME} BOARD) # Set specific platforms CMAKE CXX flags. if(BOARD MATCHES "RPI0" OR BOARD MATCHES "RPI1") From 5c6533533a67a305d116c1e76be21e57341d9eca Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Tue, 4 May 2021 18:04:45 +0200 Subject: [PATCH 72/89] Cleaning, removing debugging symbols Signed-off-by: Omar Shrit --- CMake/ConfigureCrossCompile.cmake | 4 ---- board/crosscompile-toolchain.cmake | 22 ++++++++-------------- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/CMake/ConfigureCrossCompile.cmake b/CMake/ConfigureCrossCompile.cmake index 41e8954802..92f305394b 100644 --- a/CMake/ConfigureCrossCompile.cmake +++ b/CMake/ConfigureCrossCompile.cmake @@ -30,11 +30,8 @@ macro(search_openblas version) WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/deps/OpenBLAS-${version}) endif() file(GLOB OPENBLAS_LIBRARIES "${CMAKE_BINARY_DIR}/deps/OpenBLAS-${version}/libopenblas.a") - # set(BLAS_LIBRARIES ${OPENBLAS_LIBRARIES}) - # set(LAPACK_LIBRARIES ${OPENBLAS_LIBRARIES}) set(BLAS_openblas_LIBRARY ${OPENBLAS_LIBRARIES}) set(LAPACK_openblas_LIBRARY ${OPENBLAS_LIBRARIES}) - message(STATUS "SHOW BLAS libraries: ${BLAS_LIBRARIES}") set(BLA_VENDOR OpenBLAS) set(BLAS_FOUND ON) endif() @@ -42,5 +39,4 @@ macro(search_openblas version) find_library(GFORTRAN NAMES libgfortran.a) find_library(PTHREAD NAMES libpthread.a) set(COMPILER_SUPPORT_LIBRARIES ${COMPILER_SUPPORT_LIBRARIES} ${GFORTRAN} ${PTHREAD}) - message(STATUS "SHOW BLAS libraries 2: ${BLAS_LIBRARIES}") endmacro() diff --git a/board/crosscompile-toolchain.cmake b/board/crosscompile-toolchain.cmake index dc001ab6e8..dfa8165492 100644 --- a/board/crosscompile-toolchain.cmake +++ b/board/crosscompile-toolchain.cmake @@ -9,28 +9,22 @@ ## https://toolchains.bootlin.com/ set(CMAKE_SYSTEM_NAME Linux) -set(CMAKE_SYSROOT "" CACHE STRING "CMAKE_SYSROOT") -set(TOOLCHAIN_PREFIX "" CACHE STRING "TOOLCHAIN_PREFIX") - -## In some distribution, a dynamic link for aarch64-linux-gnu-gcc may not be -## found or created, instead it might be labeled with the version at the end -## For instance: aarch64-linux-gnu-gcc-5 -## Therefore, if dynamic link exists, you do not have to specify the version. -set(VERSION_NUMBER "" CACHE STRING "Enter the version number of the compiler") +set(CMAKE_SYSROOT) +set(TOOLCHAIN_PREFIX "" CACHE STRING "Path for Toolchain for cross compiler and other compilation tools.") # Without that flag CMake is not able to pass test compilation check. set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) -set(CMAKE_AR "${TOOLCHAIN_PREFIX}gcc-ar${VERSION_NUMBER}" CACHE FILEPATH "" FORCE) -set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}gcc${VERSION_NUMBER}) -set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}g++${VERSION_NUMBER}) -set(CMAKE_LINKER ${TOOLCHAIN_PREFIX}ld${VERSION_NUMBER}) +set(CMAKE_AR "${TOOLCHAIN_PREFIX}gcc-ar" CACHE FILEPATH "" FORCE) +set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}gcc) +set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}g++) +set(CMAKE_LINKER ${TOOLCHAIN_PREFIX}ld) set(CMAKE_C_ARCHIVE_CREATE " qcs ") set(CMAKE_C_ARCHIVE_FINISH true) set(CMAKE_FORTRAN_COMPILER ${TOOLCHAIN_PREFIX}gfortran) set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER}) -set(CMAKE_OBJCOPY ${TOOLCHAIN_PREFIX}objcopy${VERSION_NUMBER} CACHE INTERNAL "objcopy tool") -set(CMAKE_SIZE_UTIL ${TOOLCHAIN_PREFIX}size${VERSION_NUMBER} CACHE INTERNAL "size tool") +set(CMAKE_OBJCOPY ${TOOLCHAIN_PREFIX}objcopy CACHE INTERNAL "objcopy tool") +set(CMAKE_SIZE_UTIL ${TOOLCHAIN_PREFIX}size CACHE INTERNAL "size tool") ## Here are the standard ROOT_PATH if you are using the standard toolchain ## if you are using a different toolchain you have to specify that too. From e8bfd4a346edc13716d718fc22394627086874f4 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Wed, 5 May 2021 00:06:31 +0200 Subject: [PATCH 73/89] Update board/flags-config.cmake Co-authored-by: Ryan Curtin --- board/flags-config.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/board/flags-config.cmake b/board/flags-config.cmake index 3c19e4a17b..67608d25b5 100644 --- a/board/flags-config.cmake +++ b/board/flags-config.cmake @@ -1,6 +1,6 @@ # This function provides a set of specific flags for each supported board # Depending on the processor type. The objective is to optimize for size. -# Thus, all of the fllowing flags are chosen carefully to reduce binary +# Thus, all of the following flags are chosen carefully to reduce binary # footprints. # Set generic minimization flags for all platforms. From 25314fdb4a8d8463bfea544890472e783ffa8e57 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Wed, 5 May 2021 00:06:55 +0200 Subject: [PATCH 74/89] Update board/flags-config.cmake Co-authored-by: Ryan Curtin --- board/flags-config.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/board/flags-config.cmake b/board/flags-config.cmake index 67608d25b5..d3e2d981bf 100644 --- a/board/flags-config.cmake +++ b/board/flags-config.cmake @@ -1,5 +1,5 @@ # This function provides a set of specific flags for each supported board -# Depending on the processor type. The objective is to optimize for size. +# depending on the processor type. The objective is to optimize for size. # Thus, all of the following flags are chosen carefully to reduce binary # footprints. From 27d8752d9019e0ccc8f81c640afc15e396ddcece Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Wed, 5 May 2021 00:07:30 +0200 Subject: [PATCH 75/89] Update board/crosscompile-toolchain.cmake Co-authored-by: Ryan Curtin --- board/crosscompile-toolchain.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/board/crosscompile-toolchain.cmake b/board/crosscompile-toolchain.cmake index dfa8165492..2cf60770c1 100644 --- a/board/crosscompile-toolchain.cmake +++ b/board/crosscompile-toolchain.cmake @@ -12,7 +12,7 @@ set(CMAKE_SYSTEM_NAME Linux) set(CMAKE_SYSROOT) set(TOOLCHAIN_PREFIX "" CACHE STRING "Path for Toolchain for cross compiler and other compilation tools.") -# Without that flag CMake is not able to pass test compilation check. +# Ensure that CMake tries to build static libraries when testing the compiler. set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) set(CMAKE_AR "${TOOLCHAIN_PREFIX}gcc-ar" CACHE FILEPATH "" FORCE) From da96df8762d732105e593443722c946c2c319afe Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Wed, 5 May 2021 00:08:17 +0200 Subject: [PATCH 76/89] Update CMake/ConfigureCrossCompile.cmake Co-authored-by: Ryan Curtin --- CMake/ConfigureCrossCompile.cmake | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CMake/ConfigureCrossCompile.cmake b/CMake/ConfigureCrossCompile.cmake index 92f305394b..b9927a5e66 100644 --- a/CMake/ConfigureCrossCompile.cmake +++ b/CMake/ConfigureCrossCompile.cmake @@ -1,7 +1,6 @@ # This file adds the necessary configurations to cross compile # mlpack for embedded systems. You need to set the following variables -# from the command line: the CMAKE_SYSROOT, TOOLCHAIN_PREFIX and the -# board type. +# from the command line: CMAKE_SYSROOT and TOOLCHAIN_PREFIX. # This file will compile OpenBLAS if it is downloaded and it is not # available on your system in order to find the BLAS library. From 860db2558499f2f485c20c281178159f95adf9b4 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Wed, 5 May 2021 00:09:04 +0200 Subject: [PATCH 77/89] Correct comments in CMake/ConfigureCrossCompile.cmake Co-authored-by: Ryan Curtin --- CMake/ConfigureCrossCompile.cmake | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CMake/ConfigureCrossCompile.cmake b/CMake/ConfigureCrossCompile.cmake index b9927a5e66..2d29ec80a8 100644 --- a/CMake/ConfigureCrossCompile.cmake +++ b/CMake/ConfigureCrossCompile.cmake @@ -2,7 +2,10 @@ # mlpack for embedded systems. You need to set the following variables # from the command line: CMAKE_SYSROOT and TOOLCHAIN_PREFIX. # This file will compile OpenBLAS if it is downloaded and it is not -# available on your system in order to find the BLAS library. +# available on your system in order to find the BLAS library. If OpenBLAS will +# be compiled, the OPENBLAS_TARGET variable must be set. This can be done +# by, e.g., setting BOARD_NAME (which will set OPENBLAS_TARGET in +# `board/flags-config.cmake`). if (CMAKE_CROSSCOMPILING) include(board/flags-config.cmake) From 188cc167a26223ab0f960b48ffaf235861669b21 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Wed, 5 May 2021 00:09:42 +0200 Subject: [PATCH 78/89] Update board/crosscompile-toolchain.cmake Co-authored-by: Ryan Curtin --- board/crosscompile-toolchain.cmake | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/board/crosscompile-toolchain.cmake b/board/crosscompile-toolchain.cmake index 2cf60770c1..2adb64152c 100644 --- a/board/crosscompile-toolchain.cmake +++ b/board/crosscompile-toolchain.cmake @@ -1,9 +1,11 @@ ## This file handles cross-compilation configurations for aarch64, ## known as arm64. The objective of this file is to find and assign ## cross-compiler and the entire toolchain. -## It works best with buildroot toolchain, when using it the user -## needs to set the: TOOLCHAIN_PREFIX and CMAKE_SYSROOT from the -## command line. +## +## This configuration works best with the buildroot toolchain. When using this +## file, be sure to set the TOOLCHAIN_PREFIX and CMAKE_SYSROOT variables, +## preferably via the CMake configuration command (e.g. `-DCMAKE_SYSROOT=<...>`). +## ## Currently, we recommend using buildroot toolchain for ## cross-compilation. Here is the link to download the toolchains: ## https://toolchains.bootlin.com/ From 786636b1114caebd44da3d37596c7630aa3341e5 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Wed, 5 May 2021 00:17:58 +0200 Subject: [PATCH 79/89] Revert changes in FindArmadillo Signed-off-by: Omar Shrit --- CMake/FindArmadillo.cmake | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/CMake/FindArmadillo.cmake b/CMake/FindArmadillo.cmake index e1ba5b2221..3d696b0abb 100644 --- a/CMake/FindArmadillo.cmake +++ b/CMake/FindArmadillo.cmake @@ -91,12 +91,11 @@ if(NOT _ARMA_USE_WRAPPER OR MSVC) endif() endif() if(_ARMA_USE_BLAS) -# if(NOT BLAS_FOUND) - if(ARMADILLO_FIND_QUIETLY OR NOT ARMADILLO_FIND_REQUIRED) - find_package(BLAS QUIET) - else() - find_package(BLAS REQUIRED) - endif() + if(ARMADILLO_FIND_QUIETLY OR NOT ARMADILLO_FIND_REQUIRED) + find_package(BLAS QUIET) + else() + find_package(BLAS REQUIRED) + endif() if(BLAS_FOUND) set(_ARMA_SUPPORT_LIBRARIES "${_ARMA_SUPPORT_LIBRARIES}" "${BLAS_LIBRARIES}") endif() From 6564c8ec97742a2e8643b10d43f8309f3006c7d9 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Wed, 5 May 2021 00:22:24 +0200 Subject: [PATCH 80/89] Add an elseif(board) if the board is not known Signed-off-by: Omar Shrit --- board/flags-config.cmake | 2 ++ 1 file changed, 2 insertions(+) diff --git a/board/flags-config.cmake b/board/flags-config.cmake index d3e2d981bf..349c55068d 100644 --- a/board/flags-config.cmake +++ b/board/flags-config.cmake @@ -63,4 +63,6 @@ elseif(BOARD MATCHES "NORTHWOOD") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") set(OPENBLAS_TARGET "NORTHWOOD") set(OPENBLAS_BINARY "32") +elseif(BOARD) + message(FATAL_ERROR "Board type is not known, please choose a supported board from the list") endif() From 6fe628f49fc7f3a4ae874b68627452bb00a78e98 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 6 May 2021 00:29:14 +0200 Subject: [PATCH 81/89] Update CMake/ConfigureCrossCompile.cmake Co-authored-by: Ryan Curtin --- CMake/ConfigureCrossCompile.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMake/ConfigureCrossCompile.cmake b/CMake/ConfigureCrossCompile.cmake index 2d29ec80a8..49eec75b68 100644 --- a/CMake/ConfigureCrossCompile.cmake +++ b/CMake/ConfigureCrossCompile.cmake @@ -14,7 +14,7 @@ if (CMAKE_CROSSCOMPILING) elseif(NOT CMAKE_SYSROOT) message(FATAL_ERROR "Can not proceed CMAKE_SYSROOT is not set") elseif(NOT TOOLCHAIN_PREFIX) - message(FATAL_ERROR "Cant not proceed TOOLCHAIN_PREFIX is not set") + message(FATAL_ERROR "Cannot configure: TOOLCHAIN_PREFIX must be set when performing cross-compiling!") endif() endif() From 55bac6d763a60866b84b4f8e2794e7bec8c30845 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 6 May 2021 00:29:29 +0200 Subject: [PATCH 82/89] Update board/crosscompile-toolchain.cmake Co-authored-by: Ryan Curtin --- board/crosscompile-toolchain.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/board/crosscompile-toolchain.cmake b/board/crosscompile-toolchain.cmake index 2adb64152c..2b586e1113 100644 --- a/board/crosscompile-toolchain.cmake +++ b/board/crosscompile-toolchain.cmake @@ -12,7 +12,7 @@ set(CMAKE_SYSTEM_NAME Linux) set(CMAKE_SYSROOT) -set(TOOLCHAIN_PREFIX "" CACHE STRING "Path for Toolchain for cross compiler and other compilation tools.") +set(TOOLCHAIN_PREFIX "" CACHE STRING "Path for toolchain for cross compiler and other compilation tools.") # Ensure that CMake tries to build static libraries when testing the compiler. set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) From 9d29409ba279e384b215d4d8d7cbfd369f2d7721 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 6 May 2021 00:29:37 +0200 Subject: [PATCH 83/89] Update CMake/ConfigureCrossCompile.cmake Co-authored-by: Ryan Curtin --- CMake/ConfigureCrossCompile.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMake/ConfigureCrossCompile.cmake b/CMake/ConfigureCrossCompile.cmake index 49eec75b68..93fb7e4ab4 100644 --- a/CMake/ConfigureCrossCompile.cmake +++ b/CMake/ConfigureCrossCompile.cmake @@ -12,7 +12,7 @@ if (CMAKE_CROSSCOMPILING) if (NOT CMAKE_SYSROOT AND (NOT TOOLCHAIN_PREFIX)) message(FATAL_ERROR "Neither of CMAKE_SYSROOT or TOOLCHAIN_PREFIX is set, please set both of them and try again") elseif(NOT CMAKE_SYSROOT) - message(FATAL_ERROR "Can not proceed CMAKE_SYSROOT is not set") + message(FATAL_ERROR "Cannot configure: CMAKE_SYSROOT must be set when performing cross-compiling!") elseif(NOT TOOLCHAIN_PREFIX) message(FATAL_ERROR "Cannot configure: TOOLCHAIN_PREFIX must be set when performing cross-compiling!") endif() From 17cd0a89480b337e86155b89abd945deacf022f2 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 6 May 2021 00:30:10 +0200 Subject: [PATCH 84/89] Update CMake/ConfigureCrossCompile.cmake Co-authored-by: Ryan Curtin --- CMake/ConfigureCrossCompile.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMake/ConfigureCrossCompile.cmake b/CMake/ConfigureCrossCompile.cmake index 93fb7e4ab4..89610df001 100644 --- a/CMake/ConfigureCrossCompile.cmake +++ b/CMake/ConfigureCrossCompile.cmake @@ -23,7 +23,7 @@ macro(search_openblas version) find_package(BLAS) if (NOT BLAS_FOUND OR (NOT BLAS_LIBRARIES)) if(NOT OPENBLAS_TARGET) - message(FATAL_ERROR "Cant not proceed, OPENBLAS_TARGET is not set, and to either set that or BOARD_NAME") + message(FATAL_ERROR "Cannot compile OpenBLAS: OPENBLAS_TARGET is not set. Either set that variable, or set BOARD_NAME correctly!") endif() get_deps(https://github.com/xianyi/OpenBLAS/releases/download/v${version}/OpenBLAS-${version}.tar.gz OpenBLAS OpenBLAS-${version}.tar.gz) if (NOT MSVC) From 31c3cf227d29badccf50183051a48d231cf507e9 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 6 May 2021 00:31:42 +0200 Subject: [PATCH 85/89] Update CMake/ConfigureCrossCompile.cmake Co-authored-by: Ryan Curtin --- CMake/ConfigureCrossCompile.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMake/ConfigureCrossCompile.cmake b/CMake/ConfigureCrossCompile.cmake index 89610df001..75e28bc453 100644 --- a/CMake/ConfigureCrossCompile.cmake +++ b/CMake/ConfigureCrossCompile.cmake @@ -10,7 +10,7 @@ if (CMAKE_CROSSCOMPILING) include(board/flags-config.cmake) if (NOT CMAKE_SYSROOT AND (NOT TOOLCHAIN_PREFIX)) - message(FATAL_ERROR "Neither of CMAKE_SYSROOT or TOOLCHAIN_PREFIX is set, please set both of them and try again") + message(FATAL_ERROR "Neither CMAKE_SYSROOT nor TOOLCHAIN_PREFIX are set; please set both of them and try again.") elseif(NOT CMAKE_SYSROOT) message(FATAL_ERROR "Cannot configure: CMAKE_SYSROOT must be set when performing cross-compiling!") elseif(NOT TOOLCHAIN_PREFIX) From 4b90d92145346e3421e6ba9444fd95ab87914688 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 6 May 2021 00:33:10 +0200 Subject: [PATCH 86/89] Update board/flags-config.cmake Co-authored-by: Ryan Curtin --- board/flags-config.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/board/flags-config.cmake b/board/flags-config.cmake index 349c55068d..154cba387b 100644 --- a/board/flags-config.cmake +++ b/board/flags-config.cmake @@ -64,5 +64,5 @@ elseif(BOARD MATCHES "NORTHWOOD") set(OPENBLAS_TARGET "NORTHWOOD") set(OPENBLAS_BINARY "32") elseif(BOARD) - message(FATAL_ERROR "Board type is not known, please choose a supported board from the list") + message(FATAL_ERROR "Given BOARD_TYPE is not known; please choose a supported board from the list") endif() From 48daf7ca59e75ed1633610e8c9cf02d84140b522 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 6 May 2021 00:57:20 +0200 Subject: [PATCH 87/89] Update board/flags-config.cmake Co-authored-by: Ryan Curtin --- board/flags-config.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/board/flags-config.cmake b/board/flags-config.cmake index 154cba387b..aa7704a710 100644 --- a/board/flags-config.cmake +++ b/board/flags-config.cmake @@ -64,5 +64,5 @@ elseif(BOARD MATCHES "NORTHWOOD") set(OPENBLAS_TARGET "NORTHWOOD") set(OPENBLAS_BINARY "32") elseif(BOARD) - message(FATAL_ERROR "Given BOARD_TYPE is not known; please choose a supported board from the list") + message(FATAL_ERROR "Given BOARD_NAME is not known; please choose a supported board from the list") endif() From 8a9f9b4b2cc9cdf6543a711394d1e2f673bf5b20 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 6 May 2021 01:03:35 +0200 Subject: [PATCH 88/89] Add TODO message to update documentation Signed-off-by: Omar Shrit --- board/flags-config.cmake | 1 + 1 file changed, 1 insertion(+) diff --git a/board/flags-config.cmake b/board/flags-config.cmake index aa7704a710..06dfecd5cf 100644 --- a/board/flags-config.cmake +++ b/board/flags-config.cmake @@ -64,5 +64,6 @@ elseif(BOARD MATCHES "NORTHWOOD") set(OPENBLAS_TARGET "NORTHWOOD") set(OPENBLAS_BINARY "32") elseif(BOARD) + ## TODO: update documentation with a list of the supported boards. message(FATAL_ERROR "Given BOARD_NAME is not known; please choose a supported board from the list") endif() From 594c002f4f64e78890192e8abf55f16d8560424b Mon Sep 17 00:00:00 2001 From: fawwazmayda Date: Sat, 8 May 2021 13:16:02 +0800 Subject: [PATCH 89/89] adding flatten_t_swish adding test adding Flatten T Swish Update activation_functions_test.cpp Update flatten_t_swish.hpp fixing mistype fixing style improve styling Update flatten_t_swish_impl.hpp Update flatten_t_swish.hpp Update flatten_t_swish_impl.hpp Update COPYRIGHT.txt Update activation_functions_test.cpp Update activation_functions_test.cpp fix style Update flatten_t_swish_impl.hpp Update flatten_t_swish_impl.hpp Update flatten_t_swish_impl.hpp Update history.md --- COPYRIGHT.txt | 1 + HISTORY.md | 2 + src/mlpack/methods/ann/layer/CMakeLists.txt | 2 + .../methods/ann/layer/flatten_t_swish.hpp | 124 ++++++++++++++++++ .../ann/layer/flatten_t_swish_impl.hpp | 80 +++++++++++ src/mlpack/methods/ann/layer/layer.hpp | 1 + .../tests/activation_functions_test.cpp | 62 +++++++++ 7 files changed, 272 insertions(+) create mode 100644 src/mlpack/methods/ann/layer/flatten_t_swish.hpp create mode 100644 src/mlpack/methods/ann/layer/flatten_t_swish_impl.hpp diff --git a/COPYRIGHT.txt b/COPYRIGHT.txt index b7e9d6cf15..db89c05176 100644 --- a/COPYRIGHT.txt +++ b/COPYRIGHT.txt @@ -143,6 +143,7 @@ Copyright: Copyright 2020, Anmolpreet Singh Copyright 2021, Tru Hoang Copyright 2021, Mark Fischinger + Copyright 2021, Muhammad Fawwaz Mayda License: BSD-3-clause All rights reserved. diff --git a/HISTORY.md b/HISTORY.md index 528c1746f5..5e758fb023 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,5 +1,7 @@ ### mlpack ?.?.? ###### ????-??-?? + * Add Flatten T Swish activation function (`flatten-t-swish.hpp`) + * Added warm start feature to Random Forest (#2881); this feature is accessible from mlpack's bindings to different languages. diff --git a/src/mlpack/methods/ann/layer/CMakeLists.txt b/src/mlpack/methods/ann/layer/CMakeLists.txt index 2b181012c7..52dbebec75 100644 --- a/src/mlpack/methods/ann/layer/CMakeLists.txt +++ b/src/mlpack/methods/ann/layer/CMakeLists.txt @@ -36,6 +36,8 @@ set(SOURCES elu_impl.hpp fast_lstm.hpp fast_lstm_impl.hpp + flatten_t_swish.hpp + flatten_t_swish_impl.hpp flexible_relu.hpp flexible_relu_impl.hpp glimpse.hpp diff --git a/src/mlpack/methods/ann/layer/flatten_t_swish.hpp b/src/mlpack/methods/ann/layer/flatten_t_swish.hpp new file mode 100644 index 0000000000..3d891b3363 --- /dev/null +++ b/src/mlpack/methods/ann/layer/flatten_t_swish.hpp @@ -0,0 +1,124 @@ +/** + * @file methods/ann/layer/flatten_t_swish.hpp + * @author Fawwaz Mayda + * + * Definition of Flatten T Swish layer first introduced in the acoustic model, + * Hock Hung Chieng, Noorhaniza Wahid, Pauline Ong, Sai Raj Kishore Perla, + * "Flatten-T Swish: a thresholded ReLU-Swish-like activation function for deep learning", 2018 + * + * 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. + */ +#ifndef MLPACK_METHODS_ANN_LAYER_FLATTEN_T_SWISH_HPP +#define MLPACK_METHODS_ANN_LAYER_FLATTEN_T_SWISH_HPP + +#include + +namespace mlpack { +namespace ann /** Artificial Neural Network. */ { + +/** + * The Flatten T Swish activation function, defined by + * + * @f{eqnarray*}{ + * f'(x) &=& \left\{ + * \begin{array}{lr} + * frac{x}{1+exp(-x)} + T & : x \ge 0 \\ + * T & : x < 0 + * \end{array} + * \right. \\ + * f'(x) &=& \left\{ + * \begin{array}{lr} + * \sigma(x)(1 - f(x)) + f(x) & : x > 0 \\ + * 0 & : x \le 0 + * \end{array} + * \right. + * @f} + * + * @tparam InputDataType Type of the input data (arma::colvec, arma::mat, + * arma::sp_mat or arma::cube). + * @tparam OutputDataType Type of the output data (arma::colvec, arma::mat, + * arma::sp_mat or arma::cube). + */ +template < + typename InputDataType = arma::mat, + typename OutputDataType = arma::mat +> +class FlattenTSwish +{ + public: + /** + * Create the Flatten T Swish object using the specified parameters. + * The thresholded value T can be adjusted via T paramaters. + * When the x is < 0, T will be used instead of 0. + * The default value of T is -0.20 as suggested in the paper. + * @param T + */ + FlattenTSwish(const double T = -0.20); + + /** + * Ordinary feed forward pass of a neural network, evaluating the function + * f(x) by propagating the activity forward through f. + * + * @param input Input data used for evaluating the specified function. + * @param output Resulting output activation. + */ + template + void Forward(const InputType& input, OutputType& output); + + /** + * Ordinary feed backward pass of a neural network, calculating the function + * f(x) by propagating x backwards through f. Using the results from the feed + * forward pass. + * + * @param input The propagated input activation. + * @param gy The backpropagated error. + * @param g The calculated gradient. + */ + template + void Backward(const DataType& input, const DataType& gy, DataType& g); + + //! Get the output parameter. + OutputDataType const& OutputParameter() const { return outputParameter; } + //! Modify the output parameter. + OutputDataType& OutputParameter() { return outputParameter; } + + //! Get the delta. + OutputDataType const& Delta() const { return delta; } + //! Modify the delta. + OutputDataType& Delta() { return delta; } + + //! Get the T parameter. + double const& T() const { return t; } + //! Modify the T parameter. + double& T() { return t; } + + //! Get size of weights. + size_t WeightSize() const { return 0; } + + /** + * Serialize the layer. + */ + template + void serialize(Archive& ar, const uint32_t /* version */); + + private: + //! Locally-stored delta object. + OutputDataType delta; + + //! Locally-stored output parameter object. + OutputDataType outputParameter; + + //! T Parameter from paper. + double t; +}; // class FlattenTSwish + +} // namespace ann +} // namespace mlpack + +// Include implementation. +#include "flatten_t_swish_impl.hpp" + +#endif diff --git a/src/mlpack/methods/ann/layer/flatten_t_swish_impl.hpp b/src/mlpack/methods/ann/layer/flatten_t_swish_impl.hpp new file mode 100644 index 0000000000..1ce5364da8 --- /dev/null +++ b/src/mlpack/methods/ann/layer/flatten_t_swish_impl.hpp @@ -0,0 +1,80 @@ +/** + * @file methods/ann/layer/flatten_t_swish_impl.hpp + * @author Fawwaz Mayda + * + * Definition of Flatten T Swish layer first introduced in the acoustic model, + * Hock Hung Chieng, Noorhaniza Wahid, Pauline Ong, Sai Raj Kishore Perla, + * "Flatten-T Swish: a thresholded ReLU-Swish-like activation function for deep learning", 2018 + * + * 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. + */ +#ifndef MLPACK_METHODS_ANN_LAYER_FLATTEN_T_SWISH_IMPL_HPP +#define MLPACK_METHODS_ANN_LAYER_FLATTEN_T_SWISH_IMPL_HPP + +// In case it hasn't yet been included. +#include "flatten_t_swish.hpp" +#include +#include + +namespace mlpack { +namespace ann /** Artificial Neural Network. */ { + +template +FlattenTSwish::FlattenTSwish( + const double T) : t(T) +{ + // Nothing to do here. +} + +template +template +void FlattenTSwish::Forward( + const InputType& input, OutputType& output) +{ + // Placeholder for Relu values. + OutputDataType relu; + RectifierFunction::Fn(input, relu); + LogisticFunction::Fn(input, output); + // F(x) = relu * sigmoid + t. + output = relu % output + t; +} + +template +template +void FlattenTSwish::Backward( + const DataType& input, const DataType& gy, DataType& g) +{ + DataType derivate, sigmoid; + LogisticFunction::Fn(input,sigmoid); + derivate.set_size(arma::size(input)); + for(size_t i = 0; i < input.n_elem; ++i) + { + if (input(i) >= 0) + { + // F(x) = x * sigmoid(x). + // We don't put '+ t' here because this is a derivate. + derivate(i) = input(i) * sigmoid(i); + derivate(i) = sigmoid(i) * (1.0 - derivate(i)) + derivate(i); + } + else + derivate(i) = 0; + } + g = gy % derivate; +} + +template +template +void FlattenTSwish::serialize( + Archive& ar, + const uint32_t /* version */) +{ + ar(CEREAL_NVP(t)); +} + +} // namespace ann +} // namespace mlpack + +#endif \ No newline at end of file diff --git a/src/mlpack/methods/ann/layer/layer.hpp b/src/mlpack/methods/ann/layer/layer.hpp index 6d13a26772..b2f598b985 100644 --- a/src/mlpack/methods/ann/layer/layer.hpp +++ b/src/mlpack/methods/ann/layer/layer.hpp @@ -32,6 +32,7 @@ #include "dropout.hpp" #include "elu.hpp" #include "fast_lstm.hpp" +#include "flatten_t_swish.hpp" #include "flexible_relu.hpp" #include "glimpse.hpp" #include "gru.hpp" diff --git a/src/mlpack/tests/activation_functions_test.cpp b/src/mlpack/tests/activation_functions_test.cpp index a20b95c039..fcca17d2c0 100644 --- a/src/mlpack/tests/activation_functions_test.cpp +++ b/src/mlpack/tests/activation_functions_test.cpp @@ -660,6 +660,48 @@ void CheckSoftminDerivativeCorrect(const arma::colvec input, } } +/** + * Implementation of the Flatten T Swish activation function test. The function is + * implemented as Flatten T Swish layer in the file flatten_t_swish.hpp. + * + * @param input Input data used for evaluating the Flatten T Swish activation function. + * @param target Target data used to evaluate the Flatten T Swish activation. + */ +void CheckFlattenTSwishActivationCorrect(const arma::colvec input, const arma::colvec target) +{ + FlattenTSwish<> fts(0.4); + arma::colvec activations; + + fts.Forward(input,activations); + for(size_t i = 0; i < activations.n_elem; ++i) + { + REQUIRE(activations.at(i) == Approx(target.at(i)).epsilon(1e-5)); + } +} + +/** + * Implementation of the Softmin activation function derivative test. + * The function is implemented as Softmin layer in the file softmin.hpp. + * + * @param input Input data used for evaluating the Softmin activation function. + * @param target Target data used to evaluate the Softmin activation. + */ + +void CheckFlattenTSwishDerivateCorrect(const arma::colvec input, const arma::colvec target) +{ + FlattenTSwish<> fts; + + // Set the error to 1 to get the actual derivative. + arma::colvec error = arma::ones(input.n_elem); + + arma::colvec derivate; + fts.Backward(input,error,derivate); + for(size_t i = 0; i < derivate.n_elem; ++i) + { + REQUIRE(derivate.at(i) == Approx(target.at(i)).epsilon(1e-5)); + } +} + /** * Basic test of the tanh function. */ @@ -1264,3 +1306,23 @@ TEST_CASE("SILUFunctionTest","[ActivationFunctionsTest]") CheckActivationCorrect(activationData,desiredActivation); CheckDerivativeCorrect(desiredActivation,desiredDerivate); } + +/** + * Basic test of Flatten T Swish function. + */ +TEST_CASE("FlattenTSwishFunctionTest","[ActivationFunctionsTest]") +{ + // Random Value. + arma::colvec input("-4.0 -1.0 2 3 4 5 6"); + + // Hand Calculated and using PyTorch. + arma::colvec desiredActivation("0.4000000059604645 0.4000000059604645 2.1615941524505615 \ + 3.2577223777770996 4.328054904937744 5.3665361404418945 6.385164737701416"); + + // Hand Calculated and using PyTorch. + arma::colvec desiredDerivation("0.694792 0.694792 1.096893 1.079178 1.042602 \ + 1.020182 1.009048"); + + CheckFlattenTSwishActivationCorrect(input,desiredActivation); + CheckFlattenTSwishDerivateCorrect(desiredActivation,desiredDerivation); +} \ No newline at end of file