Skip to content

Commit 2d06c42

Browse files
committed
taco: add the stateful random data loader for TACO
Add similar logic as in python to load the same random tensor from disk.
1 parent 88bc694 commit 2d06c42

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

taco/bench.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "bench.h"
2+
3+
#include <cstdlib>
4+
#include <iostream>
5+
6+
#include "taco/tensor.h"
7+
#include "taco/util/strings.h"
8+
9+
std::string constructRandomTensorKey(std::vector<int> dims, float sparsity) {
10+
auto path = std::getenv("TACO_RANDOM_TENSOR_PATH");
11+
if (path == nullptr) {
12+
std::cout << "TACO_RANDOM_TENSOR_PATH is unset" << std::endl;
13+
assert(false);
14+
}
15+
std::string pathStr(path);
16+
std::stringstream result;
17+
result << pathStr;
18+
if (pathStr[pathStr.size() - 1] != '/') {
19+
result << "/";
20+
}
21+
result << taco::util::join(dims, "x") << "-" << sparsity << ".tns";
22+
return result.str();
23+
}
24+
25+
taco::TensorBase loadRandomTensor(std::string name, std::vector<int> dims, float sparsity, taco::Format format) {
26+
// For now, just say that the python code must generate the random
27+
// tensor before use.
28+
auto tensor = taco::read(constructRandomTensorKey(dims, sparsity), format, true);
29+
tensor.setName(name);
30+
return tensor;
31+
}

taco/bench.h

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define TACO_BENCH_BENCH_H
33

44
#include "benchmark/benchmark.h"
5+
#include "taco/tensor.h"
56

67
// Register a benchmark with the following options:
78
// * Millisecond output display
@@ -37,4 +38,6 @@
3738
->ReportAggregatesOnly(true) \
3839
->UseRealTime()
3940

41+
taco::TensorBase loadRandomTensor(std::string name, std::vector<int> dims, float sparsity, taco::Format format);
42+
4043
#endif //TACO_BENCH_BENCH_H

taco/windowing.cpp

+2-13
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,13 @@ static void applyBenchSizes(benchmark::internal::Benchmark* b) {
1717
static void bench_add_sparse_window(benchmark::State& state, const Format& f) {
1818
int dim = state.range(0);
1919
auto sparsity = 0.01;
20-
Tensor<float> matrix("A", {dim, dim}, f);
21-
22-
srand(4357);
23-
for (int i = 0; i < dim; i++) {
24-
for (int j = 0; j < dim; j++) {
25-
float rand_float = (float)rand()/(float)(RAND_MAX);
26-
if (rand_float < sparsity) {
27-
matrix.insert({i, j}, (float) ((int) (rand_float*3/sparsity)));
28-
}
29-
}
30-
}
20+
Tensor<double> matrix = loadRandomTensor("A", {dim, dim}, sparsity, f);
3121
matrix.pack();
3222

33-
3423
for (auto _ : state) {
3524
// Setup.
3625
state.PauseTiming();
37-
Tensor<float> result("B", {dim-2, dim-2}, f);
26+
Tensor<double> result("B", {dim-2, dim-2}, f);
3827
IndexVar i, j;
3928
result(i, j) = matrix(i(1, dim-1), j(1, dim-1)) + matrix(i(1, dim-1), j(1, dim-1));
4029
result.compile();

0 commit comments

Comments
 (0)