Skip to content

Commit c676fa1

Browse files
authored
Merge pull request #24 from intellistream/tug-of-war
Add TugOfWar implementation in PyTorch
2 parents 3441225 + 27d1c47 commit c676fa1

7 files changed

Lines changed: 201 additions & 0 deletions

File tree

benchmark/torchscripts/TugOfWar.pt

8.32 KB
Binary file not shown.

benchmark/torchscripts/TugOfWar.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import torch
2+
import time
3+
import os
4+
import math
5+
6+
def tug_of_war_mat(m: int, n: int) -> torch.Tensor:
7+
e = 1/math.sqrt(m)
8+
M = torch.randint(2, (m, n))
9+
return e*(2*M - 1)
10+
11+
12+
@torch.jit.script
13+
def TugOfWar(A: torch.Tensor, B: torch.Tensor, l: int):
14+
m, n = A.shape
15+
n, p = B.shape
16+
17+
delta = 0.2
18+
19+
i_iters = int(-math.log(delta))
20+
j_iters = int(2*(-math.log(delta) + math.log(-math.log(delta))))
21+
22+
z = torch.empty((i_iters,))
23+
AS = []
24+
SB = []
25+
26+
for i in range(i_iters):
27+
S = tug_of_war_mat(l, n)
28+
SB.append(S.matmul(B))
29+
AS.append(A.matmul(S.T))
30+
31+
y = torch.empty((j_iters,))
32+
33+
for j in range(j_iters):
34+
Q = tug_of_war_mat(16, p)
35+
X = A.matmul(B.matmul(Q.T))
36+
X_hat = AS[i].matmul(SB[i].matmul(Q.T))
37+
y[j] = torch.norm(X - X_hat)**2
38+
z[i] = torch.median(y)
39+
40+
i_star = torch.argmin(z)
41+
return torch.matmul(AS[i_star], SB[i_star])
42+
43+
44+
def main():
45+
width = 1000
46+
A = torch.rand(10000, width)
47+
B = torch.rand(width, 5000)
48+
49+
t = time.time()
50+
51+
aResult = TugOfWar(A, B, 500)
52+
print("approximate: " + str(time.time() - t) + "s")
53+
54+
print(aResult)
55+
56+
# exact result
57+
t = time.time()
58+
eResult = torch.matmul(A, B)
59+
print("\nExact: " + str(time.time() - t) + "s")
60+
61+
print(eResult)
62+
63+
print("\nerror: " + str(torch.norm(aResult - eResult, p='fro').item()))
64+
65+
TugOfWar_script = TugOfWar.save("TugOfWar.pt")
66+
67+
68+
if __name__ == '__main__':
69+
main()

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ add_catch_test(crs_test SystemTest/CRSTest.cpp IntelliStream)
2424
add_catch_test(ews_test SystemTest/EWSTest.cpp IntelliStream)
2525
add_catch_test(weighted_cr_test SystemTest/WeightedCRTest.cpp IntelliStream)
2626
add_catch_test(block_partition_test SystemTest/BlockPartitionTest.cpp IntelliStream)
27+
add_catch_test(tug_of_war_test SystemTest/TugOfWarTest.cpp IntelliStream)
2728

test/SystemTest/TugOfWarTest.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <vector>
2+
3+
#define CATCH_CONFIG_MAIN
4+
#include "catch.hpp"
5+
#include <AMMBench.h>
6+
using namespace std;
7+
using namespace INTELLI;
8+
using namespace torch;
9+
void runSingleThreadTest(std::string configName) {
10+
ConfigMapPtr cfg = newConfigMap();
11+
cfg->fromFile(configName);
12+
AMMBench::MatrixLoaderTable mLoaderTable;
13+
uint64_t sketchDimension;
14+
sketchDimension = cfg->tryU64("sketchDimension", 50, true);
15+
uint64_t coreBind = cfg->tryU64("coreBind", 0, true);
16+
UtilityFunctions::bind2Core((int) coreBind);
17+
torch::set_num_threads(1);
18+
std::string ptFile = cfg->tryString("ptFile", "torchscripts/FDAMM.pt", true);
19+
20+
//uint64_t customResultName = cfg->tryU64("customResultName", 0, true);
21+
INTELLI_INFO("Place me at core" + to_string(coreBind));
22+
INTELLI_INFO(
23+
"with sketch" + to_string(sketchDimension));
24+
torch::jit::script::Module module;
25+
INTELLI_INFO("Try pt file " + ptFile);
26+
module = torch::jit::load(ptFile);
27+
std::string matrixLoaderTag = cfg->tryString("matrixLoaderTag", "random", true);
28+
auto matLoaderPtr = mLoaderTable.findMatrixLoader(matrixLoaderTag);
29+
assert(matLoaderPtr);
30+
matLoaderPtr->setConfig(cfg);
31+
auto A = matLoaderPtr->getA();
32+
auto B = matLoaderPtr->getB();
33+
/*torch::manual_seed(114514);
34+
//555
35+
auto A = torch::rand({(long) aRow, (long) aCol});
36+
auto B = torch::rand({(long) aCol, (long) bCol});*/
37+
INTELLI_INFO("Generation done, conducting...");
38+
ThreadPerf pef((int) coreBind);
39+
pef.setPerfList();
40+
pef.start();
41+
auto C =module.forward({A, B, (long) sketchDimension}).toTensor();
42+
pef.end();
43+
std::string ruName = "default";
44+
45+
auto resultCsv = pef.resultToConfigMap();
46+
resultCsv->toFile(ruName + ".csv");
47+
INTELLI_INFO("Done. here is result");
48+
std::cout << resultCsv->toString() << endl;
49+
}
50+
TEST_CASE("Test the Tug of War", "[short]")
51+
{
52+
int a = 0;
53+
runSingleThreadTest("scripts/config_tugOfWar.csv");
54+
// place your test here
55+
REQUIRE(a == 0);
56+
}

test/scripts/config_tugOfWar.csv

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
key,value,type
2+
aRow,100,U64
3+
aCol,1000,U64
4+
bCol,500,U64
5+
sketchDimension,25,U64
6+
ptFile,torchscripts/CRSV2.pt,String

test/torchscripts/TugOfWar.pt

8.32 KB
Binary file not shown.

test/torchscripts/TugOfWar.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import torch
2+
import time
3+
import os
4+
import math
5+
6+
def tug_of_war_mat(m: int, n: int) -> torch.Tensor:
7+
e = 1/math.sqrt(m)
8+
M = torch.randint(2, (m, n))
9+
return e*(2*M - 1)
10+
11+
12+
@torch.jit.script
13+
def TugOfWar(A: torch.Tensor, B: torch.Tensor, l: int):
14+
m, n = A.shape
15+
n, p = B.shape
16+
17+
delta = 0.2
18+
19+
i_iters = int(-math.log(delta))
20+
j_iters = int(2*(-math.log(delta) + math.log(-math.log(delta))))
21+
22+
z = torch.empty((i_iters,))
23+
AS = []
24+
SB = []
25+
26+
for i in range(i_iters):
27+
S = tug_of_war_mat(l, n)
28+
SB.append(S.matmul(B))
29+
AS.append(A.matmul(S.T))
30+
31+
y = torch.empty((j_iters,))
32+
33+
for j in range(j_iters):
34+
Q = tug_of_war_mat(16, p)
35+
X = A.matmul(B.matmul(Q.T))
36+
X_hat = AS[i].matmul(SB[i].matmul(Q.T))
37+
y[j] = torch.norm(X - X_hat)**2
38+
z[i] = torch.median(y)
39+
40+
i_star = torch.argmin(z)
41+
return torch.matmul(AS[i_star], SB[i_star])
42+
43+
44+
def main():
45+
width = 1000
46+
A = torch.rand(10000, width)
47+
B = torch.rand(width, 5000)
48+
49+
t = time.time()
50+
51+
aResult = TugOfWar(A, B, 500)
52+
print("approximate: " + str(time.time() - t) + "s")
53+
54+
print(aResult)
55+
56+
# exact result
57+
t = time.time()
58+
eResult = torch.matmul(A, B)
59+
print("\nExact: " + str(time.time() - t) + "s")
60+
61+
print(eResult)
62+
63+
print("\nerror: " + str(torch.norm(aResult - eResult, p='fro').item()))
64+
65+
TugOfWar_script = TugOfWar.save("TugOfWar.pt")
66+
67+
68+
if __name__ == '__main__':
69+
main()

0 commit comments

Comments
 (0)