Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/AMMBench.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
#include <CPPAlgos/AbstractCPPAlgo.h>
#include <CPPAlgos/CPPAlgoTable.h>
#include <CPPAlgos/CRSCPPAlgo.h>
#include <CPPAlgos/CRSV2CPPAlgo.h>
/**
* @}
*
Expand Down
58 changes: 58 additions & 0 deletions include/CPPAlgos/CRSV2CPPAlgo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// Created by haolan on 5/26/23.
//

#ifndef INTELLISTREAM_CRSV2CPPALGO_H
#define INTELLISTREAM_CRSV2CPPALGO_H

#include <CPPAlgos/AbstractCPPAlgo.h>

namespace AMMBench {
/**
* @ingroup AMMBENCH_CppAlgos The algorithms writtrn in c++
* @{
*/
/**
* @class CRSCPPlgo CPPAlgos/CRSV2CPPAlgo.h
* @brief The cloumn row sampling (CRS) class of c++ algos
*
*/
class CRSV2CPPAlgo : public AMMBench::AbstractCPPAlgo {
public:
CRSV2CPPAlgo() {

}

~CRSV2CPPAlgo() {

}

/**
* @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 CRSV2CppAlgo

*/
typedef std::shared_ptr<class AMMBench::CRSV2CPPAlgo> CRSV2CPPAlgoPtr;
/**
* @ingroup AMMBENCH_CppAlgos
* @def newCRSV2CppAlgo
* @brief (Macro) To creat a new @ref CRSV2CppAlgounder shared pointer.
*/
#define newCRSV2CPPAlgo std::make_shared<AMMBench::CRSV2CPPAlgo>
}
/**
* @}
*/
#endif //INTELLISTREAM_CRSV2CPPALGO_H
1 change: 1 addition & 0 deletions src/CPPAlgos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ add_sources(
AbstractCPPAlgo.cpp
CRSCPPAlgo.cpp
CPPAlgoTable.cpp
CRSV2CPPAlgo.cpp
)
2 changes: 2 additions & 0 deletions src/CPPAlgos/CPPAlgoTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

#include <CPPAlgos/CPPAlgoTable.h>
#include <CPPAlgos/CRSCPPAlgo.h>
#include <CPPAlgos/CRSV2CPPAlgo.h>
namespace AMMBench {
AMMBench::CPPAlgoTable::CPPAlgoTable() {
algoMap["mm"] = newAbstractCPPAlgo();
algoMap["crs"] = newCRSCPPAlgo();
algoMap["crsV2"] = newCRSV2CPPAlgo();
}

} // AMMBench
40 changes: 40 additions & 0 deletions src/CPPAlgos/CRSV2CPPAlgo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// Created by haolan on 5/26/23.
//

#include <CPPAlgos/CRSV2CPPAlgo.h>

namespace AMMBench {
torch::Tensor AMMBench::CRSV2CPPAlgo::amm(torch::Tensor A, torch::Tensor B, int k) {
A = A.t();
auto A_size = A.sizes();
int64_t n = A_size[0];
// int64_t m = A_size[1];

assert(n == B.size(0));
//TORCH_CHECK(n == B.size(0));
//TORCH_CHECK(k < n);

//INTELLI_INFO("Running CRS V2 CPP");

// probability distribution
torch::Tensor sample = torch::rand({n}); // default: uniform

// diagonal scaling matrix D (nxn)
sample = sample.div(sample.sum());
torch::Tensor D = torch::diag(1.0 / torch::sqrt(k * sample));

// sampling matrix S (kxn)
torch::Tensor column_indices = torch::multinomial(sample, k, true);
torch::Tensor S = torch::zeros({k, n});
for (int64_t row = 0; row < k; row++) {
int64_t col = column_indices[row].item<int64_t>();
S[row][col] = 1;
}

torch::Tensor a = torch::matmul(torch::matmul(A.t(), D), S.t());
torch::Tensor b = torch::matmul(torch::matmul(a, S), D);

return torch::matmul(b, B);
}
} // AMMBench
13 changes: 12 additions & 1 deletion test/SystemTest/CRSTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ TEST_CASE("Test the COLUMN ROW SAMPLINGS, V2", "[short]")
// place your test here
REQUIRE(a == 0);
}
TEST_CASE("Test pure cpp versiom", "[short]")
TEST_CASE("Test CRS in cpp", "[short]")
{
torch::manual_seed(114514);
AMMBench::CRSCPPAlgo crs;
Expand All @@ -71,4 +71,15 @@ TEST_CASE("Test pure cpp versiom", "[short]")
auto ammC = crs.amm(A, B, 20);
double froError = INTELLI::UtilityFunctions::relativeFrobeniusNorm(realC, ammC);
REQUIRE(froError < 0.5);
}
TEST_CASE("Test CRS v2 in cpp", "[short]")
{
torch::manual_seed(114514);
AMMBench::CRSV2CPPAlgo crs;
auto A = torch::rand({400, 400});
auto B = torch::rand({400, 400});
auto realC = torch::matmul(A, B);
auto ammC = crs.amm(A, B, 20);
double froError = INTELLI::UtilityFunctions::relativeFrobeniusNorm(realC, ammC);
REQUIRE(froError < 0.5);
}