Skip to content

Commit a58fc6c

Browse files
committed
*: add python logic to parametrize over FROSTT tensors
It'll be easier to write these python FROSTT tests than the C++ ones.
1 parent 96a4aab commit a58fc6c

File tree

5 files changed

+51
-8
lines changed

5 files changed

+51
-8
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ BENCHFLAGS := #"--benchmark-group-by=func"
55
IGNORE += taco
66
IGNORE_FLAGS := $(addprefix --ignore=, $(IGNORE))
77

8-
export TACO_RANDOM_TENSOR_PATH = data/random/
8+
export TACO_TENSOR_PATH = data/
99

1010
# To group benchmark output by benchmark, use BENCHFLAGS=--benchmark-group-by=func.
1111
# To additionally group by a parameterized value, add on ",param:<paramname>" to the

numpy/ufuncs.py

+15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from scipy.sparse import random, csr_matrix
33
import sparse
44
import pytest
5+
from util import TensorCollectionFROSTT
56

67
# TODO (rohany): Ask hameer about this. pydata/sparse isn't happy when
78
# given this ufunc to evaluate.
@@ -85,3 +86,17 @@ def bench():
8586
return C
8687
tacoBench(bench)
8788
print("Result", bench())
89+
90+
# Run benchmarks against the FROSTT collection.
91+
FROSTTTensors = TensorCollectionFROSTT()
92+
@pytest.mark.skip(reason="way too slow")
93+
@pytest.mark.parametrize("tensor", FROSTTTensors.getTensors(), ids=FROSTTTensors.getTensorNames())
94+
def bench_pydata_frostt_ufunc_sparse(tacoBench, tensor):
95+
frTensor = tensor.load()
96+
# TODO (rohany): Replace this with a call to the random generator.
97+
other = sparse.random(frTensor.shape, density=0.001)
98+
def bench():
99+
print("Beginning ldexp")
100+
c = numpy.ldexp(frTensor, other)
101+
return c
102+
tacoBench(bench)

numpy/util.py

+31-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import scipy.sparse
22
import sparse
33
import os
4+
import glob
5+
6+
# Get the path to the directory holding random tensors. Error out
7+
# if this isn't set.
8+
TENSOR_PATH = os.environ['TACO_TENSOR_PATH']
49

510
# TnsFileLoader loads a tensor stored in .tns format.
611
class TnsFileLoader:
@@ -87,12 +92,10 @@ def load(self, path):
8792
# sparsity. For example, a 250 by 250 tensor with sparsity 0.01
8893
# would have a key of 250x250-0.01.tns.
8994
def construct_random_tensor_key(shape, sparsity):
90-
# Get the path to the directory holding random tensors. Error out
91-
# if this isn't set.
92-
path = os.environ['TACO_RANDOM_TENSOR_PATH']
95+
path = TENSOR_PATH
9396
dims = "x".join([str(dim) for dim in shape])
9497
key = "{}-{}.tns".format(dims, sparsity)
95-
return os.path.join(path, key)
98+
return os.path.join(path, "random", key)
9699

97100
# RandomPydataSparseTensorLoader should be used to generate
98101
# random pydata.sparse tensors. It caches the loaded tensors
@@ -134,3 +137,27 @@ def random(self, shape, sparsity):
134137
dok = scipy.sparse.dok_matrix(result)
135138
TnsFileDumper().dump_dict_to_file(shape, dict(dok.items()), key)
136139
return result
140+
141+
# FROSTTTensor represents a tensor in the FROSTT dataset.
142+
class FROSTTTensor:
143+
def __init__(self, path):
144+
self.path = path
145+
146+
def __str__(self):
147+
f = os.path.split(self.path)[1]
148+
return f.replace(".tns", "")
149+
150+
def load(self):
151+
return PydataSparseTensorLoader().load(self.path)
152+
153+
# TensorCollectionFROSTT represents the set of all FROSTT tensors.
154+
class TensorCollectionFROSTT:
155+
def __init__(self):
156+
data = os.path.join(TENSOR_PATH, "FROSTT")
157+
frostttensors = glob.glob(os.path.join(data, "*.tns"))
158+
self.tensors = [FROSTTTensor(t) for t in frostttensors]
159+
160+
def getTensors(self):
161+
return self.tensors
162+
def getTensorNames(self):
163+
return [str(tensor) for tensor in self.getTensors()]

taco/bench.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
#include "taco/util/strings.h"
88

99
std::string constructRandomTensorKey(std::vector<int> dims, float sparsity) {
10-
auto path = std::getenv("TACO_RANDOM_TENSOR_PATH");
10+
auto path = std::getenv("TACO_TENSOR_PATH");
1111
if (path == nullptr) {
12-
std::cout << "TACO_RANDOM_TENSOR_PATH is unset" << std::endl;
12+
std::cout << "TACO_TENSOR_PATH is unset" << std::endl;
1313
assert(false);
1414
}
1515
std::string pathStr(path);
@@ -18,6 +18,7 @@ std::string constructRandomTensorKey(std::vector<int> dims, float sparsity) {
1818
if (pathStr[pathStr.size() - 1] != '/') {
1919
result << "/";
2020
}
21+
result << "random/";
2122
result << taco::util::join(dims, "x") << "-" << sparsity << ".tns";
2223
return result.str();
2324
}

taco/bench.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
BENCHMARK(bench) \
1414
->Unit(benchmark::kMillisecond) \
1515
->Repetitions(10) \
16-
->Iterations(5) \
16+
->Iterations(1) \
1717
->ReportAggregatesOnly(true) \
1818
->UseRealTime()
1919

0 commit comments

Comments
 (0)