Skip to content

Commit a9a824c

Browse files
committed
WIP load tensors from file into python benchmark test for ufunc
1 parent 928adbf commit a9a824c

File tree

2 files changed

+59
-6
lines changed

2 files changed

+59
-6
lines changed

numpy/ufuncs.py

+54-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import numpy
2-
from scipy.sparse import random
2+
from scipy.sparse import random, csr_matrix
33
import sparse
44
import pytest
55

@@ -10,6 +10,7 @@ def myfunc(x, y):
1010
myufunc = numpy.frompyfunc(myfunc, 2, 1, identity=0)
1111

1212
# @pytest.mark.parametrize("dim", [5000, 10000, 20000])
13+
@pytest.mark.skip(reason="Not testing this right now")
1314
@pytest.mark.parametrize("dim", [250, 500, 750, 1000, 2500, 5000, 7500, 8000])
1415
@pytest.mark.parametrize("ufunc", [numpy.logical_xor, numpy.logical_or, numpy.right_shift, numpy.ldexp])
1516
def bench_ufunc_dense(tacoBench, dim, ufunc):
@@ -21,6 +22,7 @@ def bench():
2122

2223
# TODO (rohany): We can parametrize this over the sparsity as well.
2324
# @pytest.mark.parametrize("dim", [5000, 10000, 20000])
25+
@pytest.mark.skip(reason="Not testing this right now")
2426
@pytest.mark.parametrize("dim", [250, 500, 750, 1000, 2500, 5000, 7500, 8000])
2527
@pytest.mark.parametrize("ufunc", [numpy.logical_xor, numpy.logical_or, numpy.right_shift, numpy.ldexp])
2628
def bench_pydata_ufunc_sparse(tacoBench, dim, ufunc):
@@ -29,3 +31,54 @@ def bench_pydata_ufunc_sparse(tacoBench, dim, ufunc):
2931
def bench():
3032
C = ufunc(A, B)
3133
tacoBench(bench)
34+
35+
36+
def import_tensor(filename, dim):
37+
print(filename)
38+
with open(filename, 'r') as f:
39+
lines = f.readlines()
40+
41+
count = 0
42+
indptr = 0
43+
indices = 0
44+
data = 0
45+
for line in lines:
46+
count += 1
47+
if count == 5:
48+
indptr = line
49+
indptr = indptr[3: -2]
50+
if count == 6:
51+
indices = line
52+
indices = indices[3: -2]
53+
if count == 7:
54+
data = line
55+
data = data[1: -2]
56+
print("indptr", indptr)
57+
indptr = numpy.fromstring(indptr, dtype=int, sep=",")
58+
indices = numpy.fromstring(indices, dtype=int, sep=",")
59+
data = numpy.fromstring(data, dtype=float, sep=",")
60+
return indptr, indices, data
61+
62+
def get_ufunc_str(ufunc):
63+
if ufunc == numpy.logical_xor:
64+
return "xor"
65+
@pytest.mark.parametrize("dim", [250, 500, 750, 1000, 2500, 5000, 7500, 8000])
66+
@pytest.mark.parametrize("ufunc", [numpy.logical_xor])
67+
def bench_pydata_import_ufunc_sparse(tacoBench, dim, ufunc):
68+
filenameA = "./data/bench_ufunc_sparse_"
69+
filenameA += get_ufunc_str(ufunc)
70+
filenameA += "_" + str(dim) + "_" + "010000_A.txt"
71+
indptrA, indicesA, dataA = import_tensor(filenameA, dim)
72+
73+
A = csr_matrix((dataA, indicesA, indptrA), shape=(dim, dim)).toarray()
74+
75+
filenameB = "./data/bench_ufunc_sparse_"
76+
filenameB += get_ufunc_str(ufunc)
77+
filenameB += "_" + str(dim) + "_" + "010000_B.txt"
78+
indptrB, indicesB, dataB = import_tensor(filenameB, dim)
79+
B = csr_matrix((dataB, indicesB, indptrB), shape=(dim, dim)).toarray()
80+
def bench():
81+
C = ufunc(A, B)
82+
return C
83+
tacoBench(bench)
84+
print("Result", bench())

taco/ufuncs.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,9 @@ void printTensor(TensorBase tensor, std::string location, std::string benchName
7373

7474
std::string sparseStr = std::to_string(sparsity);
7575
sparseStr = sparseStr.substr(2, sparseStr.size());
76-
std::string filename = location + "/" + benchName + "_" + tensor.getName() + "_" + opType + "_" + \
77-
std::to_string(dim) + "_" + sparseStr + ".txt";
76+
std::string filename = location + "/" + benchName + "_" + opType + "_" + \
77+
std::to_string(dim) + "_" + sparseStr + "_" + tensor.getName() + ".txt";
7878
std::ofstream outfile(filename, std::ofstream::out);
79-
std::cout << filename << std::endl;
8079
outfile << util::toString(tensor);
8180
outfile.close();
8281
}
@@ -119,7 +118,7 @@ static void bench_ufunc_sparse(benchmark::State& state, ExtraArgs&&... extra_arg
119118
}
120119
}
121120
A.pack(); B.pack();
122-
121+
123122
// Output tensors to file
124123
printTensor(A, "./data", __FUNCTION__ , dim, extra_args...);
125124
printTensor(B, "./data", __FUNCTION__ , dim, extra_args...);
@@ -139,7 +138,8 @@ static void bench_ufunc_sparse(benchmark::State& state, ExtraArgs&&... extra_arg
139138
}
140139
}
141140
static void applyBenchSizes(benchmark::internal::Benchmark* b) {
142-
b->ArgsProduct({{250, 500, 750, 1000, 2500, 5000, 7500, 8000}});
141+
b->ArgsProduct({{250, 500, 750, 1000, 2500, 5000, 7500, 8000}});
142+
//b->ArgsProduct({{2, 4, 10}});
143143
}
144144

145145
TACO_BENCH_ARGS(bench_ufunc_sparse, xor_0.01, 0.01, "xor")->Apply(applyBenchSizes);

0 commit comments

Comments
 (0)