diff --git a/commit.sh b/commit.sh index 17cba7b4..4ca36cd5 100755 --- a/commit.sh +++ b/commit.sh @@ -1,4 +1,4 @@ -BRANCH=NIGHTLY_TEST +BRANCH=count-sketch-cpp git init git checkout -b $BRANCH git add . diff --git a/commit_info b/commit_info index 123ecae8..3965683a 100644 --- a/commit_info +++ b/commit_info @@ -1,3 +1 @@ -1. add pure c++ algorithms -2. add normalized error and error bound evaluation at benchmark -3. update scripts +1. FIX CONFLICTS WITH MAIN \ No newline at end of file diff --git a/include/AMMBench.h b/include/AMMBench.h index e424393c..b77de81c 100755 --- a/include/AMMBench.h +++ b/include/AMMBench.h @@ -100,6 +100,7 @@ #include #include #include +#include #include #include #include diff --git a/include/CPPAlgos/CountSketchCPPAlgo.h b/include/CPPAlgos/CountSketchCPPAlgo.h new file mode 100644 index 00000000..b3548119 --- /dev/null +++ b/include/CPPAlgos/CountSketchCPPAlgo.h @@ -0,0 +1,58 @@ +// +// Created by luv on 5/28/23. +// + +#ifndef INTELLISTREAM_COUNTSKETCHCPPALGO_H +#define INTELLISTREAM_COUNTSKETCHCPPALGO_H + +#include + +namespace AMMBench { +/** + * @ingroup AMMBENCH_CppAlgos The algorithms writtrn in c++ + * @{ + */ +/** + * @class CountSketchCPPAlgo CPPAlgos/CountSketchCPPAlgo.h + * @brief The cloumn row sampling (CRS) class of c++ algos + * + */ + class CountSketchCPPAlgo : public AMMBench::AbstractCPPAlgo { + public: + CountSketchCPPAlgo() { + + } + + ~CountSketchCPPAlgo() { + + } + + /** + * @brief the virtual function provided for outside callers, rewrite in children classes + * @param A the A matrix + * @param B the B matrix + * @param sketchSize the size of sketc or sampling + * @return the output c matrix + */ + virtual torch::Tensor amm(torch::Tensor A, torch::Tensor B, int sketchSize); + + }; + +/** + * @ingroup AMMBENCH_CppAlgos + * @typedef AbstractMatrixCppAlgoPtr + * @brief The class to describe a shared pointer to @ref CountSketchCPPAlgo + + */ + typedef std::shared_ptr CountSketchCPPAlgoPtr; +/** + * @ingroup AMMBENCH_CppAlgos + * @def newCRSV2CppAlgo + * @brief (Macro) To creat a new @ref CRSV2CppAlgounder shared pointer. + */ +#define newCountSketchCPPAlgo std::make_shared +} +/** + * @} + */ +#endif //INTELLISTREAM_COUNTSKETCHCPPALGO_H diff --git a/src/CPPAlgos/CMakeLists.txt b/src/CPPAlgos/CMakeLists.txt index 9d7a8934..a8cd3898 100644 --- a/src/CPPAlgos/CMakeLists.txt +++ b/src/CPPAlgos/CMakeLists.txt @@ -7,4 +7,6 @@ add_sources( EWSCPPAlgo.cpp CoOccurringFDCPPAlgo.cpp BetaCoOFDCPPAlgo.cpp -) \ No newline at end of file + CountSketchCPPAlgo.cpp +) + diff --git a/src/CPPAlgos/CPPAlgoTable.cpp b/src/CPPAlgos/CPPAlgoTable.cpp index a1472acf..458461e9 100644 --- a/src/CPPAlgos/CPPAlgoTable.cpp +++ b/src/CPPAlgos/CPPAlgoTable.cpp @@ -5,19 +5,24 @@ #include #include #include + +#include + #include #include #include #include + namespace AMMBench { AMMBench::CPPAlgoTable::CPPAlgoTable() { algoMap["mm"] = newAbstractCPPAlgo(); algoMap["crs"] = newCRSCPPAlgo(); algoMap["crsV2"] = newCRSV2CPPAlgo(); + algoMap["count-sketch"] = newCountSketchCPPAlgo(); algoMap["bcrs"] = newBCRSCPPAlgo(); algoMap["ews"] = newEWSCPPAlgo(); algoMap["CoOFD"] = newCoOccurringFDCPPAlgo(); algoMap["bcoofd"] = newBetaCoOFDCPPAlgo(); } -} // AMMBench \ No newline at end of file +} // AMMBench diff --git a/src/CPPAlgos/CRSV2CPPAlgo.cpp b/src/CPPAlgos/CRSV2CPPAlgo.cpp index 01378f76..87413ea9 100644 --- a/src/CPPAlgos/CRSV2CPPAlgo.cpp +++ b/src/CPPAlgos/CRSV2CPPAlgo.cpp @@ -37,4 +37,4 @@ namespace AMMBench { return torch::matmul(b, B); } -} // AMMBench \ No newline at end of file +} // AMMBench diff --git a/src/CPPAlgos/CountSketchCPPAlgo.cpp b/src/CPPAlgos/CountSketchCPPAlgo.cpp new file mode 100644 index 00000000..a6161355 --- /dev/null +++ b/src/CPPAlgos/CountSketchCPPAlgo.cpp @@ -0,0 +1,33 @@ +// +// Created by haolan on 5/26/23. +// + +#include + +namespace AMMBench { + torch::Tensor AMMBench::CountSketchCPPAlgo::amm(torch::Tensor A, torch::Tensor B, int k) { + int64_t m1 = A.size(0); + int64_t n = A.size(1); + int64_t m2 = B.size(1); + + // Initialize sketch matrices + torch::Tensor Ca = torch::zeros({m1, k}); + torch::Tensor Cb = torch::zeros({k, m2}); + + torch::Tensor L = torch::randint(k, {n}); + torch::Tensor G = torch::randint(2, {n}); + + // Modify the random column with random sign + for (int64_t i = 0; i < n; ++i) { + if (G[i].item() == 1) { + Ca.index({torch::indexing::Slice(), L[i]}).add_(A.index({torch::indexing::Slice(), i})); + Cb.index({L[i], torch::indexing::Slice()}).add_(B.index({i, torch::indexing::Slice()})); + } else { + Ca.index({torch::indexing::Slice(), L[i]}).sub_(A.index({torch::indexing::Slice(), i})); + Cb.index({L[i], torch::indexing::Slice()}).sub_(B.index({i, torch::indexing::Slice()})); + } + } + + return torch::matmul(Ca, Cb); + } +} // AMMBench diff --git a/test/SystemTest/SketchTest.cpp b/test/SystemTest/SketchTest.cpp index 561660dd..ce24d1c9 100644 --- a/test/SystemTest/SketchTest.cpp +++ b/test/SystemTest/SketchTest.cpp @@ -50,10 +50,22 @@ auto B = torch::rand({(long) aCol, (long) bCol});*/ TEST_CASE("Test the counter sketch", "[short]") { int a = 0; - runSingleThreadTest("scripts/config_counterSketch.csv"); + runSingleThreadTest("scripts/config_countSketch.csv"); // place your test here REQUIRE(a == 0); } + +TEST_CASE("Test Count Sketch in cpp", "[short]") +{ + torch::manual_seed(114514); + AMMBench::CountSketchCPPAlgo cs; + auto A = torch::rand({400, 400}); + auto B = torch::rand({400, 400}); + auto realC = torch::matmul(A, B); + auto ammC = cs.amm(A, B, 20); + double froError = INTELLI::UtilityFunctions::relativeFrobeniusNorm(realC, ammC); + REQUIRE(froError < 0.5); +} TEST_CASE("Test Co-Occurring FD in cpp", "[short]") { torch::manual_seed(114514); @@ -75,4 +87,5 @@ TEST_CASE("Test Beta-Co-Occurring FD in cpp", "[short]") auto ammC = bcoofd.amm(A, B, 20); double froError = INTELLI::UtilityFunctions::relativeFrobeniusNorm(realC, ammC); REQUIRE(froError < 0.2); -} \ No newline at end of file +} + diff --git a/test/scripts/config_counterSketch.csv b/test/scripts/config_countSketch.csv similarity index 100% rename from test/scripts/config_counterSketch.csv rename to test/scripts/config_countSketch.csv