From 2c564463357afb52e9f70f00e882d11bc766ff6e Mon Sep 17 00:00:00 2001 From: haolan Date: Tue, 6 Jun 2023 10:04:03 +0000 Subject: [PATCH 1/3] add poisson matrix loader --- include/AMMBench.h | 1 + include/MatrixLoader/PoissonMatrixLoader.h | 90 ++++++++++++++++++++++ src/MatrixLoader/CMakeLists.txt | 1 + src/MatrixLoader/MatrixLoaderTable.cpp | 2 + src/MatrixLoader/PoissonMatrixLoader.cpp | 32 ++++++++ 5 files changed, 126 insertions(+) create mode 100644 include/MatrixLoader/PoissonMatrixLoader.h create mode 100644 src/MatrixLoader/PoissonMatrixLoader.cpp diff --git a/include/AMMBench.h b/include/AMMBench.h index 30851aed..25a80f49 100755 --- a/include/AMMBench.h +++ b/include/AMMBench.h @@ -90,6 +90,7 @@ #include #include #include +#include #include /** * @} diff --git a/include/MatrixLoader/PoissonMatrixLoader.h b/include/MatrixLoader/PoissonMatrixLoader.h new file mode 100644 index 00000000..579fc7ce --- /dev/null +++ b/include/MatrixLoader/PoissonMatrixLoader.h @@ -0,0 +1,90 @@ +// +// Created by haolan on 6/6/23. +// + +#ifndef INTELLISTREAM_POISSONMATRIXLOADER_H +#define INTELLISTREAM_POISSONMATRIXLOADER_H +#include +namespace AMMBench { +/** + * @ingroup AMMBENCH_MatrixLOADER + * @{ + */ +/** + * @ingroup AMMBENCH_MatrixLOADER_Poisson The Poisson generator + * @{ + */ +/** + * @class PoissonMatrixLoader MatrixLoader/PoissonMatrixLoader.h + * @brief The Poisson class of matrix loader + * @ingroup AMMBENCH_MatrixLOADER_Poisson + * @note: + * - Must have a global config by @ref setConfig + * @note Default behavior +* - create +* - call @ref setConfig, this function will also generate the tensor A and B correspondingly +* - call @ref getA and @ref getB (assuming we are benchmarking torch.mm(A,B)) + * @note: require config parameters and default values + * - "aRow" The rows in matrix A, U64, 100 + * - "aCol" The cols in matrix B, U64, 1000 + * - "bCol" The rows in matrix B, U64, 500 + * - "seed" The seed of inline random generator,U64,114514 + * @note: default name tags + * "random": @ref PoissonMatrixLoader + */ + class PoissonMatrixLoader : public AbstractMatrixLoader { + protected: + torch::Tensor A, B; + uint64_t aRow, aCol, bCol, seed; + /** + * @brief Inline logic of reading a config file + * @param cfg the config + */ + void paraseConfig(INTELLI::ConfigMapPtr cfg); + /** + * @brief inline logic of generating A and B + */ + void generateAB(); + public: + PoissonMatrixLoader() = default; + + ~PoissonMatrixLoader() = default; + /** + * @brief Set the GLOBAL config map related to this loader + * @param cfg The config map + * @return bool whether the config is successfully set + * @note + */ + virtual bool setConfig(INTELLI::ConfigMapPtr cfg); + /** + * @brief get the A matrix + * @return the generated A matrix + */ + virtual torch::Tensor getA(); + /** + * @brief get the B matrix + * @return the generated B matrix + */ + virtual torch::Tensor getB(); + }; +/** + * @ingroup AMMBENCH_MatrixLOADER_Poisson + * @typedef PoissonMatrixLoaderPtr + * @brief The class to describe a shared pointer to @ref PoissonMatrixLoader + + */ + typedef std::shared_ptr PoissonMatrixLoaderPtr; +/** + * @ingroup AMMBENCH_MatrixLOADER_Poisson + * @def newPoissonMatrixLoader + * @brief (Macro) To creat a new @ref PoissonMatrixLoader under shared pointer. + */ +#define newPoissonMatrixLoader std::make_shared +/** + * @} + */ +/** + * @} + */ +} +#endif //INTELLISTREAM_POISSONMATRIXLOADER_H diff --git a/src/MatrixLoader/CMakeLists.txt b/src/MatrixLoader/CMakeLists.txt index fd98726c..2b81d010 100644 --- a/src/MatrixLoader/CMakeLists.txt +++ b/src/MatrixLoader/CMakeLists.txt @@ -5,5 +5,6 @@ add_sources( GaussianMatrixLoader.cpp ExponentialMatrixLoader.cpp BinomialMatrixLoader.cpp + PoissonMatrixLoader.cpp MatrixLoaderTable.cpp ) \ No newline at end of file diff --git a/src/MatrixLoader/MatrixLoaderTable.cpp b/src/MatrixLoader/MatrixLoaderTable.cpp index a6b84de0..fdb5e858 100644 --- a/src/MatrixLoader/MatrixLoaderTable.cpp +++ b/src/MatrixLoader/MatrixLoaderTable.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include namespace AMMBench { /** @@ -18,6 +19,7 @@ AMMBench::MatrixLoaderTable::MatrixLoaderTable() { loaderMap["gaussian"] = newGaussianMatrixLoader(); loaderMap["exponential"] = newExponentialMatrixLoader(); loaderMap["binomial"] = newBinomialMatrixLoader(); + loaderMap["poisson"] = newPoissonMatrixLoader(); } } // AMMBench \ No newline at end of file diff --git a/src/MatrixLoader/PoissonMatrixLoader.cpp b/src/MatrixLoader/PoissonMatrixLoader.cpp new file mode 100644 index 00000000..99f0c3be --- /dev/null +++ b/src/MatrixLoader/PoissonMatrixLoader.cpp @@ -0,0 +1,32 @@ +// +// Created by haolan on 6/6/23. +// +#include +#include +void AMMBench::PoissonMatrixLoader::paraseConfig(INTELLI::ConfigMapPtr cfg) { + aRow = cfg->tryU64("aRow", 100, true); + aCol = cfg->tryU64("aCol", 1000, true); + bCol = cfg->tryU64("bCol", 500, true); + seed = cfg->tryU64("seed", 114514, true); + INTELLI_INFO( + "Generating [" + to_string(aRow) + "x" + to_string(aCol) + "]*[" + to_string(aCol) + "x" + to_string(bCol) + + "]"); +} +void AMMBench::PoissonMatrixLoader::generateAB() { + torch::manual_seed(seed); + A = torch::poisson(torch::ones({(long) aRow, (long) aCol})); + B = torch::poisson(torch::ones({(long) aCol, (long) bCol})); +} + +//do nothing in abstract class +bool AMMBench::PoissonMatrixLoader::setConfig(INTELLI::ConfigMapPtr cfg) { + paraseConfig(cfg); + generateAB(); + return true; +} +torch::Tensor AMMBench::PoissonMatrixLoader::getA() { + return A; +} +torch::Tensor AMMBench::PoissonMatrixLoader::getB() { + return B; +} \ No newline at end of file From f22bba4d0c7e302e7c723ad631d1a77d49cc0c9c Mon Sep 17 00:00:00 2001 From: haolan Date: Tue, 6 Jun 2023 11:10:57 +0000 Subject: [PATCH 2/3] add beta matrix loader --- include/AMMBench.h | 1 + include/MatrixLoader/BetaMatrixLoader.h | 91 +++++++++++++++++++++++++ src/MatrixLoader/BetaMatrixLoader.cpp | 48 +++++++++++++ src/MatrixLoader/CMakeLists.txt | 1 + src/MatrixLoader/MatrixLoaderTable.cpp | 2 + 5 files changed, 143 insertions(+) create mode 100644 include/MatrixLoader/BetaMatrixLoader.h create mode 100644 src/MatrixLoader/BetaMatrixLoader.cpp diff --git a/include/AMMBench.h b/include/AMMBench.h index 25a80f49..de97c413 100755 --- a/include/AMMBench.h +++ b/include/AMMBench.h @@ -91,6 +91,7 @@ #include #include #include +#include #include /** * @} diff --git a/include/MatrixLoader/BetaMatrixLoader.h b/include/MatrixLoader/BetaMatrixLoader.h new file mode 100644 index 00000000..c5b5eb6f --- /dev/null +++ b/include/MatrixLoader/BetaMatrixLoader.h @@ -0,0 +1,91 @@ +// +// Created by haolan on 6/6/23. +// + +#ifndef INTELLISTREAM_BETAMATRIXLOADER_H +#define INTELLISTREAM_BETAMATRIXLOADER_H +#include +namespace AMMBench { +/** + * @ingroup AMMBENCH_MatrixLOADER + * @{ + */ +/** + * @ingroup AMMBENCH_MatrixLOADER_Beta The Beta generator + * @{ + */ +/** + * @class BetaMatrixLoader MatrixLoader/BetaMatrixLoader.h + * @brief The Beta class of matrix loader + * @ingroup AMMBENCH_MatrixLOADER_Beta + * @note: + * - Must have a global config by @ref setConfig + * @note Default behavior +* - create +* - call @ref setConfig, this function will also generate the tensor A and B correspondingly +* - call @ref getA and @ref getB (assuming we are benchmarking torch.mm(A,B)) + * @note: require config parameters and default values + * - "aRow" The rows in matrix A, U64, 100 + * - "aCol" The cols in matrix B, U64, 1000 + * - "bCol" The rows in matrix B, U64, 500 + * - "seed" The seed of inline random generator,U64,114514 + * @note: default name tags + * "random": @ref BetaMatrixLoader + */ + class BetaMatrixLoader : public AbstractMatrixLoader { + protected: + torch::Tensor A, B; + uint64_t aRow, aCol, bCol, seed; + double a, b; + /** + * @brief Inline logic of reading a config file + * @param cfg the config + */ + void paraseConfig(INTELLI::ConfigMapPtr cfg); + /** + * @brief inline logic of generating A and B + */ + void generateAB(); + public: + BetaMatrixLoader() = default; + + ~BetaMatrixLoader() = default; + /** + * @brief Set the GLOBAL config map related to this loader + * @param cfg The config map + * @return bool whether the config is successfully set + * @note + */ + virtual bool setConfig(INTELLI::ConfigMapPtr cfg); + /** + * @brief get the A matrix + * @return the generated A matrix + */ + virtual torch::Tensor getA(); + /** + * @brief get the B matrix + * @return the generated B matrix + */ + virtual torch::Tensor getB(); + }; +/** + * @ingroup AMMBENCH_MatrixLOADER_Beta + * @typedef BetaMatrixLoaderPtr + * @brief The class to describe a shared pointer to @ref BetaMatrixLoader + + */ + typedef std::shared_ptr BetaMatrixLoaderPtr; +/** + * @ingroup AMMBENCH_MatrixLOADER_Beta + * @def newBetaMatrixLoader + * @brief (Macro) To creat a new @ref BetaMatrixLoader under shared pointer. + */ +#define newBetaMatrixLoader std::make_shared +/** + * @} + */ +/** + * @} + */ +} +#endif //INTELLISTREAM_BETAMATRIXLOADER_H diff --git a/src/MatrixLoader/BetaMatrixLoader.cpp b/src/MatrixLoader/BetaMatrixLoader.cpp new file mode 100644 index 00000000..92aad95e --- /dev/null +++ b/src/MatrixLoader/BetaMatrixLoader.cpp @@ -0,0 +1,48 @@ +// +// Created by haolan on 6/6/23. +// +#include +#include +void AMMBench::BetaMatrixLoader::paraseConfig(INTELLI::ConfigMapPtr cfg) { + aRow = cfg->tryU64("aRow", 100, true); + aCol = cfg->tryU64("aCol", 1000, true); + bCol = cfg->tryU64("bCol", 500, true); + a = cfg->tryDouble("a", 2, true); + b = cfg->tryDouble("b", 2, true); + seed = cfg->tryU64("seed", 114514, true); + INTELLI_INFO( + "Generating [" + to_string(aRow) + "x" + to_string(aCol) + "]*[" + to_string(aCol) + "x" + to_string(bCol) + + "]" + " Parameter: " + to_string(a) + ", " + to_string(b)); +} +void AMMBench::BetaMatrixLoader::generateAB() { + torch::manual_seed(seed); + + auto tensor1 = torch::randn({(long) aRow, (long) aCol}).abs_(); + auto tensor2 = torch::randn({(long) aRow, (long) aCol}).abs_(); + + tensor1 = tensor1.pow(1. / a); + tensor2 = tensor2.pow(1. / b); + + A = tensor1 / (tensor1 + tensor2); + + tensor1 = torch::randn({(long) aCol, (long) bCol}).abs_(); + tensor2 = torch::randn({(long) aCol, (long) bCol}).abs_(); + + tensor1 = tensor1.pow(1. / a); + tensor2 = tensor2.pow(1. / b); + + B = tensor1 / (tensor1 + tensor2); +} + +//do nothing in abstract class +bool AMMBench::BetaMatrixLoader::setConfig(INTELLI::ConfigMapPtr cfg) { + paraseConfig(cfg); + generateAB(); + return true; +} +torch::Tensor AMMBench::BetaMatrixLoader::getA() { + return A; +} +torch::Tensor AMMBench::BetaMatrixLoader::getB() { + return B; +} \ No newline at end of file diff --git a/src/MatrixLoader/CMakeLists.txt b/src/MatrixLoader/CMakeLists.txt index 2b81d010..5a829ca1 100644 --- a/src/MatrixLoader/CMakeLists.txt +++ b/src/MatrixLoader/CMakeLists.txt @@ -6,5 +6,6 @@ add_sources( ExponentialMatrixLoader.cpp BinomialMatrixLoader.cpp PoissonMatrixLoader.cpp + BetaMatrixLoader.cpp MatrixLoaderTable.cpp ) \ No newline at end of file diff --git a/src/MatrixLoader/MatrixLoaderTable.cpp b/src/MatrixLoader/MatrixLoaderTable.cpp index fdb5e858..37979e2f 100644 --- a/src/MatrixLoader/MatrixLoaderTable.cpp +++ b/src/MatrixLoader/MatrixLoaderTable.cpp @@ -9,6 +9,7 @@ #include #include #include +#include namespace AMMBench { /** * @note revise me if you need new loader @@ -20,6 +21,7 @@ AMMBench::MatrixLoaderTable::MatrixLoaderTable() { loaderMap["exponential"] = newExponentialMatrixLoader(); loaderMap["binomial"] = newBinomialMatrixLoader(); loaderMap["poisson"] = newPoissonMatrixLoader(); + loaderMap["beta"] = newBetaMatrixLoader(); } } // AMMBench \ No newline at end of file From 837ccbff02abf62b33396e6554b67c9cc057d6a4 Mon Sep 17 00:00:00 2001 From: haolan Date: Tue, 6 Jun 2023 11:52:37 +0000 Subject: [PATCH 3/3] fix binomial matrix loader parameter parsing --- include/MatrixLoader/BetaMatrixLoader.h | 2 ++ include/MatrixLoader/BinomialMatrixLoader.h | 5 ++++- src/MatrixLoader/BinomialMatrixLoader.cpp | 7 +++---- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/include/MatrixLoader/BetaMatrixLoader.h b/include/MatrixLoader/BetaMatrixLoader.h index c5b5eb6f..e0fc7210 100644 --- a/include/MatrixLoader/BetaMatrixLoader.h +++ b/include/MatrixLoader/BetaMatrixLoader.h @@ -29,6 +29,8 @@ namespace AMMBench { * - "aCol" The cols in matrix B, U64, 1000 * - "bCol" The rows in matrix B, U64, 500 * - "seed" The seed of inline random generator,U64,114514 + * - "a" parameters of beta distribution, Double, 2.0 + * - "b" parameters of beta distribution, Double, 2.0 * @note: default name tags * "random": @ref BetaMatrixLoader */ diff --git a/include/MatrixLoader/BinomialMatrixLoader.h b/include/MatrixLoader/BinomialMatrixLoader.h index 58850a4c..a7096bff 100644 --- a/include/MatrixLoader/BinomialMatrixLoader.h +++ b/include/MatrixLoader/BinomialMatrixLoader.h @@ -29,13 +29,16 @@ namespace AMMBench { * - "aCol" The cols in matrix B, U64, 1000 * - "bCol" The rows in matrix B, U64, 500 * - "seed" The seed of inline random generator,U64,114514 + * - "trials" parameters of binomial distribution, U64, 10 + * - "probability" parameters of binomial distribution, Double, 0.5 * @note: default name tags * "random": @ref BinomialMatrixLoader */ class BinomialMatrixLoader : public AbstractMatrixLoader { protected: torch::Tensor A, B; - uint64_t aRow, aCol, bCol, seed; + uint64_t aRow, aCol, bCol, seed, trials; + double probability; /** * @brief Inline logic of reading a config file * @param cfg the config diff --git a/src/MatrixLoader/BinomialMatrixLoader.cpp b/src/MatrixLoader/BinomialMatrixLoader.cpp index e21923dc..a29e4bbe 100644 --- a/src/MatrixLoader/BinomialMatrixLoader.cpp +++ b/src/MatrixLoader/BinomialMatrixLoader.cpp @@ -8,17 +8,16 @@ void AMMBench::BinomialMatrixLoader::paraseConfig(INTELLI::ConfigMapPtr cfg) { aCol = cfg->tryU64("aCol", 1000, true); bCol = cfg->tryU64("bCol", 500, true); seed = cfg->tryU64("seed", 114514, true); + trials = cfg->tryU64("trials", 10, true); + probability = cfg->tryDouble("probability", 0.5, true); INTELLI_INFO( "Generating [" + to_string(aRow) + "x" + to_string(aCol) + "]*[" + to_string(aCol) + "x" + to_string(bCol) - + "]"); + + "]" + " Parameter: " + to_string(trials) + ", " + to_string(probability)); } void AMMBench::BinomialMatrixLoader::generateAB() { torch::manual_seed(seed); A = torch::zeros({(long) aRow, (long) aCol}); B = torch::zeros({(long) aCol, (long) bCol}); - // parameter - const int trials = 10; - const double probability = 0.5; for(int i = 0; i < trials; i++) { // Create a tensor filled with random numbers between 0 and 1