From e7db851712531df2fac25f21eae2a2ad4fa64307 Mon Sep 17 00:00:00 2001 From: Lutetium-Vanadium Date: Mon, 29 May 2023 19:21:21 +0800 Subject: [PATCH 1/2] Add C++ implementation of the Count Sketch algorithm --- include/AMMBench.h | 1 + include/CPPAlgos/CountSketchCPPAlgo.h | 58 +++++++++++++++++++ src/CPPAlgos/CMakeLists.txt | 3 +- src/CPPAlgos/CPPAlgoTable.cpp | 7 ++- src/CPPAlgos/CRSV2CPPAlgo.cpp | 2 +- src/CPPAlgos/CountSketchCPPAlgo.cpp | 33 +++++++++++ test/SystemTest/SketchTest.cpp | 15 ++++- ...unterSketch.csv => config_countSketch.csv} | 0 8 files changed, 113 insertions(+), 6 deletions(-) create mode 100644 include/CPPAlgos/CountSketchCPPAlgo.h create mode 100644 src/CPPAlgos/CountSketchCPPAlgo.cpp rename test/scripts/{config_counterSketch.csv => config_countSketch.csv} (100%) diff --git a/include/AMMBench.h b/include/AMMBench.h index 757fffea..7d874a45 100755 --- a/include/AMMBench.h +++ b/include/AMMBench.h @@ -100,6 +100,7 @@ #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 79f5a9d4..25eabdd8 100644 --- a/src/CPPAlgos/CMakeLists.txt +++ b/src/CPPAlgos/CMakeLists.txt @@ -3,4 +3,5 @@ add_sources( CRSCPPAlgo.cpp CPPAlgoTable.cpp CRSV2CPPAlgo.cpp -) \ No newline at end of file + CountSketchCPPAlgo.cpp +) diff --git a/src/CPPAlgos/CPPAlgoTable.cpp b/src/CPPAlgos/CPPAlgoTable.cpp index af137843..66c8c693 100644 --- a/src/CPPAlgos/CPPAlgoTable.cpp +++ b/src/CPPAlgos/CPPAlgoTable.cpp @@ -5,11 +5,14 @@ #include #include #include +#include + namespace AMMBench { AMMBench::CPPAlgoTable::CPPAlgoTable() { algoMap["mm"] = newAbstractCPPAlgo(); algoMap["crs"] = newCRSCPPAlgo(); - algoMap["crsV2"] = newCRSV2CPPAlgo(); + algoMap["crsV2"] = newCRSV2CPPAlgo(); + algoMap["count-sketch"] = newCountSketchCPPAlgo(); } -} // 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 408dde15..0ab5f34e 100644 --- a/test/SystemTest/SketchTest.cpp +++ b/test/SystemTest/SketchTest.cpp @@ -50,7 +50,18 @@ 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); -} \ No newline at end of file +} +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); +} 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 From fb9c4a1c59d09f54fe675d4f582fa60a78a83c2e Mon Sep 17 00:00:00 2001 From: tony <292224750@qq.com> Date: Wed, 31 May 2023 09:24:04 +0800 Subject: [PATCH 2/2] 1. FIX CONFLICTS WITH MAIN --- commit.sh | 2 +- commit_info | 4 +--- include/AMMBench.h | 1 + test/SystemTest/SketchTest.cpp | 1 - 4 files changed, 3 insertions(+), 5 deletions(-) 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 bc6300c5..b77de81c 100755 --- a/include/AMMBench.h +++ b/include/AMMBench.h @@ -99,6 +99,7 @@ #include #include #include +#include #include #include #include diff --git a/test/SystemTest/SketchTest.cpp b/test/SystemTest/SketchTest.cpp index 4ce5aa06..ce24d1c9 100644 --- a/test/SystemTest/SketchTest.cpp +++ b/test/SystemTest/SketchTest.cpp @@ -66,7 +66,6 @@ TEST_CASE("Test Count Sketch in cpp", "[short]") double froError = INTELLI::UtilityFunctions::relativeFrobeniusNorm(realC, ammC); REQUIRE(froError < 0.5); } -======= TEST_CASE("Test Co-Occurring FD in cpp", "[short]") { torch::manual_seed(114514);