diff --git a/benchmark/torchscripts/EWS.pt b/benchmark/torchscripts/EWS.pt new file mode 100644 index 00000000..384ba8ed Binary files /dev/null and b/benchmark/torchscripts/EWS.pt differ diff --git a/benchmark/torchscripts/ElementWiseSampling.py b/benchmark/torchscripts/ElementWiseSampling.py new file mode 100644 index 00000000..32724758 --- /dev/null +++ b/benchmark/torchscripts/ElementWiseSampling.py @@ -0,0 +1,63 @@ +import torch +import time +import os +import math + +@torch.jit.script +def EWS(A: torch.Tensor, B: torch.Tensor, k: int): + # Get the dimension of A + m, n = A.shape + + assert n == B.shape[0] + assert k < n + + p = B.shape[1] + + # probability distribution + probs = torch.rand(m, n) + + # S matrix that samples A with scaling + mask = torch.rand_like(probs) < probs + S = torch.zeros_like(A) + S[mask] = A[mask] / probs[mask] + + # R matrix that samples B with scaling + probs = torch.rand(n, p) # a diffrent probabilistic distribution + mask = torch.rand_like(probs) < probs + R = torch.zeros_like(B) + R[mask] = B[mask] / probs[mask] + + return torch.matmul(S, R) + + +def main(): + + width = 2000 + A = torch.rand(5000, width) + B = torch.rand(width, 5000) + + + t = time.time() + + aResult = EWS(A, B, 500) + print("approximate: " + str(time.time() - t) + "s") + + print(aResult) + + # exact result + t = time.time() + eResult = torch.matmul(A, B) + print("\nExact: " + str(time.time() - t) + "s") + + print(eResult) + + + difference = aResult - eResult + print("\nFrobenius norm error: " + str(torch.linalg.norm(difference, ord='fro').item())) + print("\nSpectral norm bound: " + str(torch.linalg.norm(difference, ord=2).item())) + + FDAMM_script = EWS.save("EWS.pt") + +if __name__ == '__main__': + main() + \ No newline at end of file diff --git a/test/SystemTest/EWSTest.cpp b/test/SystemTest/EWSTest.cpp new file mode 100644 index 00000000..e63cce6f --- /dev/null +++ b/test/SystemTest/EWSTest.cpp @@ -0,0 +1,56 @@ +#include + +#define CATCH_CONFIG_MAIN +#include "catch.hpp" +#include +using namespace std; +using namespace INTELLI; +using namespace torch; +void runSingleThreadTest(std::string configName) { + ConfigMapPtr cfg = newConfigMap(); + cfg->fromFile(configName); + AMMBench::MatrixLoaderTable mLoaderTable; + uint64_t sketchDimension; + sketchDimension = cfg->tryU64("sketchDimension", 50, true); + uint64_t coreBind = cfg->tryU64("coreBind", 0, true); + UtilityFunctions::bind2Core((int) coreBind); + torch::set_num_threads(1); + std::string ptFile = cfg->tryString("ptFile", "torchscripts/FDAMM.pt", true); + + //uint64_t customResultName = cfg->tryU64("customResultName", 0, true); + INTELLI_INFO("Place me at core" + to_string(coreBind)); + INTELLI_INFO( + "with sketch" + to_string(sketchDimension)); + torch::jit::script::Module module; + INTELLI_INFO("Try pt file " + ptFile); + module = torch::jit::load(ptFile); + std::string matrixLoaderTag = cfg->tryString("matrixLoaderTag", "random", true); + auto matLoaderPtr = mLoaderTable.findMatrixLoader(matrixLoaderTag); + assert(matLoaderPtr); + matLoaderPtr->setConfig(cfg); + auto A = matLoaderPtr->getA(); + auto B = matLoaderPtr->getB(); + /*torch::manual_seed(114514); +//555 +auto A = torch::rand({(long) aRow, (long) aCol}); +auto B = torch::rand({(long) aCol, (long) bCol});*/ + INTELLI_INFO("Generation done, conducting..."); + ThreadPerf pef((int) coreBind); + pef.setPerfList(); + pef.start(); + auto C =module.forward({A, B, (long) sketchDimension}).toTensor(); + pef.end(); + std::string ruName = "default"; + + auto resultCsv = pef.resultToConfigMap(); + resultCsv->toFile(ruName + ".csv"); + INTELLI_INFO("Done. here is result"); + std::cout << resultCsv->toString() << endl; +} +TEST_CASE("Test the COLUMN ROW SAMPLINGS", "[short]") +{ + int a = 0; + runSingleThreadTest("scripts/config_EWS.csv"); + // place your test here + REQUIRE(a == 0); +} \ No newline at end of file diff --git a/test/scripts/config_EWS.csv b/test/scripts/config_EWS.csv new file mode 100644 index 00000000..c8e45cfa --- /dev/null +++ b/test/scripts/config_EWS.csv @@ -0,0 +1,6 @@ +key,value,type +aRow,100,U64 +aCol,1000,U64 +bCol,500,U64 +sketchDimension,25,U64 +ptFile,torchscripts/EWS.pt,String diff --git a/test/torchscripts/EWS.pt b/test/torchscripts/EWS.pt new file mode 100644 index 00000000..384ba8ed Binary files /dev/null and b/test/torchscripts/EWS.pt differ diff --git a/test/torchscripts/ElementWiseSampling.py b/test/torchscripts/ElementWiseSampling.py new file mode 100644 index 00000000..32724758 --- /dev/null +++ b/test/torchscripts/ElementWiseSampling.py @@ -0,0 +1,63 @@ +import torch +import time +import os +import math + +@torch.jit.script +def EWS(A: torch.Tensor, B: torch.Tensor, k: int): + # Get the dimension of A + m, n = A.shape + + assert n == B.shape[0] + assert k < n + + p = B.shape[1] + + # probability distribution + probs = torch.rand(m, n) + + # S matrix that samples A with scaling + mask = torch.rand_like(probs) < probs + S = torch.zeros_like(A) + S[mask] = A[mask] / probs[mask] + + # R matrix that samples B with scaling + probs = torch.rand(n, p) # a diffrent probabilistic distribution + mask = torch.rand_like(probs) < probs + R = torch.zeros_like(B) + R[mask] = B[mask] / probs[mask] + + return torch.matmul(S, R) + + +def main(): + + width = 2000 + A = torch.rand(5000, width) + B = torch.rand(width, 5000) + + + t = time.time() + + aResult = EWS(A, B, 500) + print("approximate: " + str(time.time() - t) + "s") + + print(aResult) + + # exact result + t = time.time() + eResult = torch.matmul(A, B) + print("\nExact: " + str(time.time() - t) + "s") + + print(eResult) + + + difference = aResult - eResult + print("\nFrobenius norm error: " + str(torch.linalg.norm(difference, ord='fro').item())) + print("\nSpectral norm bound: " + str(torch.linalg.norm(difference, ord=2).item())) + + FDAMM_script = EWS.save("EWS.pt") + +if __name__ == '__main__': + main() + \ No newline at end of file