From 44f68b8c8a71b293817ed028e87c21ff446e2ef8 Mon Sep 17 00:00:00 2001 From: tony <292224750@qq.com> Date: Mon, 5 Jun 2023 21:55:59 +0800 Subject: [PATCH] 1. fix the uint64_t bugs 2. Note; please note the int8 is just a toy --- CMakeLists.txt | 2 +- benchmark/src/Benchmark.cpp | 2 +- commit.sh | 2 +- commit_info | 3 +- include/AMMBench.h | 1 + include/CPPAlgos/BCRSCPPAlgo.h | 2 +- include/CPPAlgos/BetaCoOFDCPPAlgo.h | 2 +- include/CPPAlgos/CRSV2CPPAlgo.h | 2 +- include/CPPAlgos/CoOccurringFDCPPAlgo.h | 2 +- include/CPPAlgos/CountSketchCPPAlgo.h | 2 +- include/CPPAlgos/EWSCPPAlgo.h | 2 +- include/CPPAlgos/INT8CPPAlgo.h | 60 +++++++++++++++++++++++++ src/CPPAlgos/AbstractCPPAlgo.cpp | 1 + src/CPPAlgos/BCRSCPPAlgo.cpp | 4 +- src/CPPAlgos/BetaCoOFDCPPAlgo.cpp | 14 +++--- src/CPPAlgos/CMakeLists.txt | 1 + src/CPPAlgos/CPPAlgoTable.cpp | 9 ++-- src/CPPAlgos/CRSV2CPPAlgo.cpp | 4 +- src/CPPAlgos/CoOccurringFDCPPAlgo.cpp | 10 ++--- src/CPPAlgos/CountSketchCPPAlgo.cpp | 5 ++- src/CPPAlgos/EWSCPPAlgo.cpp | 4 +- src/CPPAlgos/INT8CPPAlgo.cpp | 45 +++++++++++++++++++ test/CMakeLists.txt | 1 + test/SystemTest/INT8Test.cpp | 24 ++++++++++ 24 files changed, 170 insertions(+), 34 deletions(-) create mode 100644 include/CPPAlgos/INT8CPPAlgo.h create mode 100644 src/CPPAlgos/INT8CPPAlgo.cpp create mode 100644 test/SystemTest/INT8Test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 26a43e4c..92c8f911 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -67,7 +67,7 @@ add_subdirectory(src) # Add Library get_sources(IntelliStream_SOURCE_FILES) get_headers(IntelliStream_HEADER_FILES) -add_library(IntelliStream SHARED ${IntelliStream_SOURCE_FILES} ${IntelliStream_HEADER_FILES} ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}) +add_library(IntelliStream SHARED ${IntelliStream_SOURCE_FILES} ${IntelliStream_HEADER_FILES} ${CMAKE_CURRENT_BINARY_DIR}) set_property(TARGET IntelliStream PROPERTY CXX_STANDARD 20) target_include_directories(IntelliStream PUBLIC "include") diff --git a/benchmark/src/Benchmark.cpp b/benchmark/src/Benchmark.cpp index 4e504899..c9597c5d 100644 --- a/benchmark/src/Benchmark.cpp +++ b/benchmark/src/Benchmark.cpp @@ -85,7 +85,7 @@ auto B = torch::rand({(long) aCol, (long) bCol});*/ AMMBench::CPPAlgoTable cppAlgoTable; std::string cppAlgoTag = cfg->tryString("cppAlgoTag", "mm", true); AMMBench::AbstractCPPAlgoPtr cppAlgoPtr = cppAlgoTable.findCppAlgo(cppAlgoTag); - INTELLI_WARNING("single thread"); + INTELLI_WARNING("single thread, algo "+cppAlgoTag); if (eMeter != nullptr) { eMeter->startMeter(); } diff --git a/commit.sh b/commit.sh index 65d2ddc7..60a35a78 100755 --- a/commit.sh +++ b/commit.sh @@ -1,4 +1,4 @@ -BRANCH=core-bind-bug +BRANCH=uint64_t-bug git init git checkout -b $BRANCH git add . diff --git a/commit_info b/commit_info index 04fc8608..e484c1b8 100644 --- a/commit_info +++ b/commit_info @@ -1 +1,2 @@ -1. update docs \ No newline at end of file +1. fix the uint64_t bugs +2. Note; please note the int8 is just a toy \ No newline at end of file diff --git a/include/AMMBench.h b/include/AMMBench.h index bfc2ce9c..1f73b8ae 100755 --- a/include/AMMBench.h +++ b/include/AMMBench.h @@ -108,6 +108,7 @@ #include #include #include +#include /** * @} * diff --git a/include/CPPAlgos/BCRSCPPAlgo.h b/include/CPPAlgos/BCRSCPPAlgo.h index a9452522..3f78b263 100644 --- a/include/CPPAlgos/BCRSCPPAlgo.h +++ b/include/CPPAlgos/BCRSCPPAlgo.h @@ -33,7 +33,7 @@ namespace AMMBench { * @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); + virtual torch::Tensor amm(torch::Tensor A, torch::Tensor B, uint64_t sketchSize); }; diff --git a/include/CPPAlgos/BetaCoOFDCPPAlgo.h b/include/CPPAlgos/BetaCoOFDCPPAlgo.h index b89b8285..0b234a5f 100644 --- a/include/CPPAlgos/BetaCoOFDCPPAlgo.h +++ b/include/CPPAlgos/BetaCoOFDCPPAlgo.h @@ -33,7 +33,7 @@ namespace AMMBench { * @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); + virtual torch::Tensor amm(torch::Tensor A, torch::Tensor B, uint64_t sketchSize); }; diff --git a/include/CPPAlgos/CRSV2CPPAlgo.h b/include/CPPAlgos/CRSV2CPPAlgo.h index 389094e6..36304067 100644 --- a/include/CPPAlgos/CRSV2CPPAlgo.h +++ b/include/CPPAlgos/CRSV2CPPAlgo.h @@ -34,7 +34,7 @@ namespace AMMBench { * @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); + virtual torch::Tensor amm(torch::Tensor A, torch::Tensor B, uint64_t sketchSize); }; diff --git a/include/CPPAlgos/CoOccurringFDCPPAlgo.h b/include/CPPAlgos/CoOccurringFDCPPAlgo.h index 6d07ac9a..60cbccf4 100644 --- a/include/CPPAlgos/CoOccurringFDCPPAlgo.h +++ b/include/CPPAlgos/CoOccurringFDCPPAlgo.h @@ -33,7 +33,7 @@ namespace AMMBench { * @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); + virtual torch::Tensor amm(torch::Tensor A, torch::Tensor B, uint64_t sketchSize); }; diff --git a/include/CPPAlgos/CountSketchCPPAlgo.h b/include/CPPAlgos/CountSketchCPPAlgo.h index b3548119..fd3ed50b 100644 --- a/include/CPPAlgos/CountSketchCPPAlgo.h +++ b/include/CPPAlgos/CountSketchCPPAlgo.h @@ -34,7 +34,7 @@ namespace AMMBench { * @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); + torch::Tensor amm(torch::Tensor A, torch::Tensor B, uint64_t sketchSize) ; }; diff --git a/include/CPPAlgos/EWSCPPAlgo.h b/include/CPPAlgos/EWSCPPAlgo.h index 09e198d1..842f9309 100644 --- a/include/CPPAlgos/EWSCPPAlgo.h +++ b/include/CPPAlgos/EWSCPPAlgo.h @@ -33,7 +33,7 @@ namespace AMMBench { * @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); + virtual torch::Tensor amm(torch::Tensor A, torch::Tensor B, uint64_t sketchSize); }; diff --git a/include/CPPAlgos/INT8CPPAlgo.h b/include/CPPAlgos/INT8CPPAlgo.h new file mode 100644 index 00000000..eb637578 --- /dev/null +++ b/include/CPPAlgos/INT8CPPAlgo.h @@ -0,0 +1,60 @@ +/*! \file INT8CPPAlgo.h*/ +// +// Created by tony on 25/05/23. +// + +#ifndef INTELLISTREAM_INCLUDE_CPPALGOS_INT8CPPALGO_H_ +#define INTELLISTREAM_INCLUDE_CPPALGOS_INT8CPPALGO_H_ +#include +#include +#include +#include +#include +#include +#include +namespace AMMBench { +/** + * @ingroup AMMBENCH_CppAlgos The algorithms written in c++ + * @{ + */ +/** + * @class INT8CPPAlgo CPPAlgos/INT8CPPAlgo.h + * @brief The INT8 MM class of c++ algos + */ +class INT8CPPAlgo: public AMMBench::AbstractCPPAlgo { + public: + INT8CPPAlgo() { + + } + ~INT8CPPAlgo() { + + } + /** + * @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, uint64_t sketchSize); + +}; +/** + * @ingroup AMMBENCH_CppAlgos + * @typedef INT8MatrixCppAlgoPtr + * @brief The class to describe a shared pointer to @ref INT8CPPAlgo + + */ +typedef std::shared_ptr INT8CPPAlgoPtr; +/** + * @ingroup AMMBENCH_CppAlgos + * @def newINT8CppAlgo + * @brief (Macro) To creat a new @ref INT8CppAlgo shared pointer. + */ +#define newINT8CPPAlgo std::make_shared +} +/** + * @} + */ + +#endif //INTELLISTREAM_INCLUDE_CPPALGOS_ABSTRACTCPPALGO_H_ diff --git a/src/CPPAlgos/AbstractCPPAlgo.cpp b/src/CPPAlgos/AbstractCPPAlgo.cpp index 44e8a535..67f0127c 100644 --- a/src/CPPAlgos/AbstractCPPAlgo.cpp +++ b/src/CPPAlgos/AbstractCPPAlgo.cpp @@ -6,5 +6,6 @@ #include torch::Tensor AMMBench::AbstractCPPAlgo::amm(torch::Tensor A, torch::Tensor B, uint64_t sketchSize) { std::cout << sketchSize; + INTELLI_INFO("I am mm"); return torch::matmul(A, B); } \ No newline at end of file diff --git a/src/CPPAlgos/BCRSCPPAlgo.cpp b/src/CPPAlgos/BCRSCPPAlgo.cpp index 6398cfa5..631dfd6f 100644 --- a/src/CPPAlgos/BCRSCPPAlgo.cpp +++ b/src/CPPAlgos/BCRSCPPAlgo.cpp @@ -4,12 +4,12 @@ #include namespace AMMBench { -torch::Tensor BCRSCPPAlgo::amm(torch::Tensor A, torch::Tensor B, int k) { +torch::Tensor BCRSCPPAlgo::amm(torch::Tensor A, torch::Tensor B, uint64_t k2) { A = A.t(); auto A_size = A.sizes(); int64_t n = A_size[0]; //int64_t m = A_size[1]; - + int64_t k=(int64_t)k2; assert(n == B.size(0)); //TORCH_CHECK(n == B.size(0)); //TORCH_CHECK(k < n); diff --git a/src/CPPAlgos/BetaCoOFDCPPAlgo.cpp b/src/CPPAlgos/BetaCoOFDCPPAlgo.cpp index f1d7d85f..05be1884 100644 --- a/src/CPPAlgos/BetaCoOFDCPPAlgo.cpp +++ b/src/CPPAlgos/BetaCoOFDCPPAlgo.cpp @@ -28,21 +28,21 @@ torch::Tensor paramerizedReduceRank(const torch::Tensor& SV, float delta, int l, return SV_shrunk; } -torch::Tensor BetaCoOFDCPPAlgo::amm(const torch::Tensor A, const torch::Tensor B, int l) { +torch::Tensor BetaCoOFDCPPAlgo::amm(const torch::Tensor A, const torch::Tensor B, uint64_t l2) { torch::Tensor B_t = B.t(); float beta = 1.0; TORCH_CHECK(A.size(1) == B_t.size(1), "Shapes of A and B are incompatible"); - int mx = A.size(0); - int my = B_t.size(0); - int n = A.size(1); - + int64_t mx = A.size(0); + int64_t my = B_t.size(0); + int64_t n = A.size(1); + int64_t l=(int64_t)l2; // Initialize sketch matrices torch::Tensor BX = torch::zeros({ mx, l }); torch::Tensor BY = torch::zeros({ my, l }); // The first l iterations - for (int i = 0; i < l; ++i) { + for (int64_t i = 0; i < l; ++i) { BX.slice(1, i, i + 1) = A.slice(1, i, i + 1); BY.slice(1, i, i + 1) = B_t.slice(1, i, i + 1); } @@ -51,7 +51,7 @@ torch::Tensor BetaCoOFDCPPAlgo::amm(const torch::Tensor A, const torch::Tensor B zero_columns = zero_columns.slice(0, 1); // Iteration l to n: insert if available, else shrink sketch matrices - for (int i = l; i < n; ++i) { + for (int64_t i = l; i < n; ++i) { // Acquire the index of a zero-valued column if (!is_empty_tensor(zero_columns)) { int idx = get_first_element(zero_columns).toInt(); diff --git a/src/CPPAlgos/CMakeLists.txt b/src/CPPAlgos/CMakeLists.txt index a8cd3898..0e5c9c44 100644 --- a/src/CPPAlgos/CMakeLists.txt +++ b/src/CPPAlgos/CMakeLists.txt @@ -8,5 +8,6 @@ add_sources( CoOccurringFDCPPAlgo.cpp BetaCoOFDCPPAlgo.cpp CountSketchCPPAlgo.cpp + INT8CPPAlgo.cpp ) diff --git a/src/CPPAlgos/CPPAlgoTable.cpp b/src/CPPAlgos/CPPAlgoTable.cpp index 458461e9..b2e1f114 100644 --- a/src/CPPAlgos/CPPAlgoTable.cpp +++ b/src/CPPAlgos/CPPAlgoTable.cpp @@ -12,17 +12,18 @@ #include #include #include - +#include namespace AMMBench { AMMBench::CPPAlgoTable::CPPAlgoTable() { - algoMap["mm"] = newAbstractCPPAlgo(); + algoMap["mm"] = newAbstractCPPAlgo(); algoMap["crs"] = newCRSCPPAlgo(); algoMap["crsV2"] = newCRSV2CPPAlgo(); - algoMap["count-sketch"] = newCountSketchCPPAlgo(); + algoMap["countSketch"] = newCountSketchCPPAlgo(); algoMap["bcrs"] = newBCRSCPPAlgo(); algoMap["ews"] = newEWSCPPAlgo(); - algoMap["CoOFD"] = newCoOccurringFDCPPAlgo(); + algoMap["CoOFD"] = newCoOccurringFDCPPAlgo(); algoMap["bcoofd"] = newBetaCoOFDCPPAlgo(); + algoMap["int8"] = newINT8CPPAlgo(); } } // AMMBench diff --git a/src/CPPAlgos/CRSV2CPPAlgo.cpp b/src/CPPAlgos/CRSV2CPPAlgo.cpp index 87413ea9..cf577020 100644 --- a/src/CPPAlgos/CRSV2CPPAlgo.cpp +++ b/src/CPPAlgos/CRSV2CPPAlgo.cpp @@ -5,12 +5,12 @@ #include namespace AMMBench { - torch::Tensor AMMBench::CRSV2CPPAlgo::amm(torch::Tensor A, torch::Tensor B, int k) { + torch::Tensor AMMBench::CRSV2CPPAlgo::amm(torch::Tensor A, torch::Tensor B, uint64_t k2) { A = A.t(); auto A_size = A.sizes(); int64_t n = A_size[0]; // int64_t m = A_size[1]; - + int64_t k=(int64_t)k2; assert(n == B.size(0)); //TORCH_CHECK(n == B.size(0)); //TORCH_CHECK(k < n); diff --git a/src/CPPAlgos/CoOccurringFDCPPAlgo.cpp b/src/CPPAlgos/CoOccurringFDCPPAlgo.cpp index 5f484268..11c52526 100644 --- a/src/CPPAlgos/CoOccurringFDCPPAlgo.cpp +++ b/src/CPPAlgos/CoOccurringFDCPPAlgo.cpp @@ -21,14 +21,14 @@ torch::Tensor medianReduceRank(const torch::Tensor& SV, float delta) { return torch::clamp(SV - delta, 0); } -torch::Tensor CoOccurringFDCPPAlgo::amm(const torch::Tensor A, const torch::Tensor B, int l) { +torch::Tensor CoOccurringFDCPPAlgo::amm(const torch::Tensor A, const torch::Tensor B, uint64_t l2) { torch::Tensor B_t = B.t(); TORCH_CHECK(A.size(1) == B_t.size(1), "Shapes of A and B are incompatible"); - int mx = A.size(0); - int my = B_t.size(0); - int n = A.size(1); - + int64_t mx = A.size(0); + int64_t my = B_t.size(0); + int64_t n = A.size(1); + int64_t l= (int64_t) l2; // Initialize sketch matrices torch::Tensor BX = torch::zeros({ mx, l }); torch::Tensor BY = torch::zeros({ my, l }); diff --git a/src/CPPAlgos/CountSketchCPPAlgo.cpp b/src/CPPAlgos/CountSketchCPPAlgo.cpp index a6161355..3c428e23 100644 --- a/src/CPPAlgos/CountSketchCPPAlgo.cpp +++ b/src/CPPAlgos/CountSketchCPPAlgo.cpp @@ -5,11 +5,12 @@ #include namespace AMMBench { - torch::Tensor AMMBench::CountSketchCPPAlgo::amm(torch::Tensor A, torch::Tensor B, int k) { + torch::Tensor AMMBench::CountSketchCPPAlgo::amm(torch::Tensor A, torch::Tensor B, uint64_t k2) { + INTELLI_INFO("I am counter sketch"); int64_t m1 = A.size(0); int64_t n = A.size(1); int64_t m2 = B.size(1); - + int64_t k=(int64_t) k2; // Initialize sketch matrices torch::Tensor Ca = torch::zeros({m1, k}); torch::Tensor Cb = torch::zeros({k, m2}); diff --git a/src/CPPAlgos/EWSCPPAlgo.cpp b/src/CPPAlgos/EWSCPPAlgo.cpp index 8ad64467..6788c5df 100644 --- a/src/CPPAlgos/EWSCPPAlgo.cpp +++ b/src/CPPAlgos/EWSCPPAlgo.cpp @@ -5,11 +5,11 @@ #include namespace AMMBench { -torch::Tensor AMMBench::EWSCPPAlgo::amm(torch::Tensor A, torch::Tensor B, int k) { +torch::Tensor AMMBench::EWSCPPAlgo::amm(torch::Tensor A, torch::Tensor B, uint64_t k2) { auto A_size = A.sizes(); int64_t m = A_size[0]; int64_t n = A_size[1]; - + int64_t k=(int64_t) k2; TORCH_CHECK(n == B.size(0)); TORCH_CHECK(k < n); diff --git a/src/CPPAlgos/INT8CPPAlgo.cpp b/src/CPPAlgos/INT8CPPAlgo.cpp new file mode 100644 index 00000000..cef801c1 --- /dev/null +++ b/src/CPPAlgos/INT8CPPAlgo.cpp @@ -0,0 +1,45 @@ + +// Created by tony on 25/05/23. +// + +#include +torch::Tensor AMMBench::INT8CPPAlgo::amm(torch::Tensor A, torch::Tensor B, uint64_t sketchSize) { + std::cout << sketchSize; + float scalingA,scalingB; + // int16_t mulab; + + /*auto AINT8=(A*scalingA).to(torch::kInt8); + auto BTINT8=(Bt*scalingB).to(torch::kInt8);*/ + // return C; + /* + for(int64_t i=0;i(); + int8_t tbk=tb[k].item(); + mulab=tak*tbk; + ru+=mulab; + } + ruf=ru; + C[i][j]=ruf; + } + }*/ + scalingA=torch::abs(A).max().item()/127.0; + scalingB=torch::abs(B).max().item()/127.0; + auto ta=(A/scalingA).to(torch::kInt8); + auto tb= (B/scalingB).to(torch::kInt8); + // torch::matmul(ta, tb); + return torch::zeros({A.size(0), B.size(1)}); + return torch::matmul(ta, tb).to(torch::kFloat)*scalingA*scalingB; + //return C*scalingA*scalingB/127.0/127.0; +} \ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 33a9ee04..eaeebe95 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -25,4 +25,5 @@ add_catch_test(ews_test SystemTest/EWSTest.cpp IntelliStream) add_catch_test(weighted_cr_test SystemTest/WeightedCRTest.cpp IntelliStream) add_catch_test(block_partition_test SystemTest/BlockPartitionTest.cpp IntelliStream) add_catch_test(tug_of_war_test SystemTest/TugOfWarTest.cpp IntelliStream) +add_catch_test(int8_test SystemTest/INT8Test.cpp IntelliStream) diff --git a/test/SystemTest/INT8Test.cpp b/test/SystemTest/INT8Test.cpp new file mode 100644 index 00000000..7778862f --- /dev/null +++ b/test/SystemTest/INT8Test.cpp @@ -0,0 +1,24 @@ +// +// Created by tony on 05/06/23. +// +#include + +#define CATCH_CONFIG_MAIN +#include "catch.hpp" +#include +#include +TEST_CASE("Test int8", "[short]") +{ + torch::manual_seed(114514); + AMMBench::INT8CPPAlgo int8mm; + auto A = torch::rand({4, 4}); + auto B = torch::rand({4, 4}); + auto realC = torch::matmul(A, B); + auto ammC = int8mm.amm(A, B, 20); + std::cout<<"int8:"<