Skip to content

Commit d8b37ab

Browse files
committed
taco: adjust caching mechanism to also cache the casted and shifted tensors
Doing this has a noticeable speedup when running the benchmarks locally.
1 parent e9c77c4 commit d8b37ab

File tree

2 files changed

+36
-33
lines changed

2 files changed

+36
-33
lines changed

taco/bench.h

+3-26
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
->ReportAggregatesOnly(true) \
3939
->UseRealTime()
4040

41+
std::string getTacoTensorPath();
42+
taco::TensorBase loadRandomTensor(std::string name, std::vector<int> dims, float sparsity, taco::Format format);
43+
4144
template<typename T>
4245
taco::Tensor<T> castToType(std::string name, taco::Tensor<double> tensor) {
4346
taco::Tensor<T> result(name, tensor.getDimensions(), tensor.getFormat());
@@ -52,32 +55,6 @@ taco::Tensor<T> castToType(std::string name, taco::Tensor<double> tensor) {
5255
return result;
5356
}
5457

55-
struct TacoTensorFileCache {
56-
template<typename T>
57-
taco::Tensor<double> read(std::string path, T format) {
58-
if (this->lastPath == path) {
59-
return lastLoaded;
60-
}
61-
// TODO (rohany): Not worrying about whether the format was the same as what was asked for.
62-
this->lastLoaded = taco::read(path, format);
63-
this->lastPath = path;
64-
return this->lastLoaded;
65-
}
66-
67-
template<typename T, typename U>
68-
taco::Tensor<T> readIntoType(std::string name, std::string path, U format) {
69-
auto tensor = this->read<U>(path, format);
70-
return castToType<T>(name, tensor);
71-
}
72-
73-
taco::Tensor<double> lastLoaded;
74-
std::string lastPath;
75-
};
76-
77-
std::string getTacoTensorPath();
78-
taco::TensorBase loadRandomTensor(std::string name, std::vector<int> dims, float sparsity, taco::Format format);
79-
80-
// TODO (rohany): Cache the tensor shifts too.
8158
template<typename T, typename T2>
8259
taco::Tensor<T> shiftLastMode(std::string name, taco::Tensor<T2> original) {
8360
taco::Tensor<T> result(name, original.getDimensions(), original.getFormat());

taco/ufuncs.cpp

+33-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <fstream>
22
// We're using c++14, so wer're stuck with experimental filesystem.
33
#include <experimental/filesystem>
4+
#include <tuple>
45

56
#include "bench.h"
67
#include "benchmark/benchmark.h"
@@ -154,9 +155,34 @@ static void applyBenchSizes(benchmark::internal::Benchmark* b) {
154155
TACO_BENCH_ARGS(bench_ufunc_sparse, xor_0.01, 0.01, "xor")->Apply(applyBenchSizes);
155156
TACO_BENCH_ARGS(bench_ufunc_sparse, rightShift_0.01, 0.01, ">>")->Apply(applyBenchSizes);
156157

157-
// fileCache is a cache of paths to tensors so that reads of the same tensor
158-
// don't need to perform duplicate disk IO.
159-
TacoTensorFileCache fileCache;
158+
// UfuncInputCache is a cache for the input to ufunc benchmarks. These benchmarks
159+
// operate on a tensor loaded from disk and the same tensor shifted slightly. Since
160+
// these operations are run multiple times, we can save alot in benchmark startup
161+
// time from caching these inputs.
162+
struct UfuncInputCache {
163+
template<typename U>
164+
std::pair<taco::Tensor<int64_t>, taco::Tensor<int64_t>> getUfuncInput(std::string path, U format) {
165+
// See if the paths match.
166+
if (this->lastPath == path) {
167+
// TODO (rohany): Not worrying about whether the format was the same as what was asked for.
168+
return std::make_pair(this->inputTensor, this->otherTensor);
169+
}
170+
171+
// Otherwise, we missed the cache. Load in the target tensor and process it.
172+
this->lastPath = path;
173+
this->lastLoaded = taco::read(path, format);
174+
this->inputTensor = castToType<int64_t>("A", this->lastLoaded);
175+
this->otherTensor = shiftLastMode<int64_t, int64_t>("B", this->inputTensor);
176+
return std::make_pair(this->inputTensor, this->otherTensor);
177+
}
178+
179+
taco::Tensor<double> lastLoaded;
180+
std::string lastPath;
181+
182+
taco::Tensor<int64_t> inputTensor;
183+
taco::Tensor<int64_t> otherTensor;
184+
};
185+
UfuncInputCache inputCache;
160186

161187
static void bench_frostt_ufunc(benchmark::State& state, std::string tnsPath, Func op) {
162188
auto path = getTacoTensorPath();
@@ -168,8 +194,8 @@ static void bench_frostt_ufunc(benchmark::State& state, std::string tnsPath, Fun
168194
frosttTensorPath += tnsPath;
169195

170196
// TODO (rohany): What format do we want to do here?
171-
auto frosttTensor = fileCache.readIntoType<int64_t>("frostt", frosttTensorPath, Sparse);
172-
Tensor<int64_t> other = shiftLastMode<int64_t, int64_t>("other", frosttTensor);
197+
Tensor<int64_t> frosttTensor, other;
198+
std::tie(frosttTensor, other) = inputCache.getUfuncInput(frosttTensorPath, Sparse);
173199

174200
for (auto _ : state) {
175201
state.PauseTiming();
@@ -243,8 +269,8 @@ static void bench_suitesparse_ufunc(benchmark::State& state, Func op) {
243269
auto tensorName = taco::util::split(filename, ".")[0];
244270
state.SetLabel(tensorName);
245271

246-
auto ssTensor = fileCache.readIntoType<int64_t>("ssTensor", tensorPath, CSR);
247-
auto other = shiftLastMode<int64_t, int64_t>("other", ssTensor);
272+
taco::Tensor<int64_t> ssTensor, other;
273+
std::tie(ssTensor, other) = inputCache.getUfuncInput(tensorPath, CSR);
248274

249275
state.counters["dimx"] = ssTensor.getDimension(0);
250276
state.counters["dimy"] = ssTensor.getDimension(1);

0 commit comments

Comments
 (0)