Skip to content

Commit 7c1c760

Browse files
committed
Add in initial code for image.cpp and also pow1Comp ufunc
1 parent cd607cd commit 7c1c760

File tree

6 files changed

+131
-16
lines changed

6 files changed

+131
-16
lines changed

numpy/ufuncs.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ def bench_pydata_ufunc_fused(tacoBench, dim):
4242
matrix2 = safeCastPydataTensorToInts(loader.random((dim, dim), 0.01, variant=2))
4343
def bench():
4444
result = numpy.logical_xor(numpy.logical_xor(matrix, matrix1), matrix2)
45-
#print("nnz = ", result.nnz)
4645
return result
4746
tacoBench(bench)
4847

@@ -129,7 +128,7 @@ def load(self, tensor, suiteSparse):
129128
# Run benchmarks against the FROSTT collection.
130129
FROSTTTensors = TensorCollectionFROSTT()
131130
@pytest.mark.parametrize("tensor", FROSTTTensors.getTensors())
132-
@pytest.mark.parametrize("ufunc", [numpy.logical_xor, numpy.ldexp, numpy.right_shift])
131+
@pytest.mark.parametrize("ufunc", [numpy.power, numpy.logical_xor, numpy.ldexp, numpy.right_shift])
133132
def bench_pydata_frostt_ufunc_sparse(tacoBench, tensor, ufunc):
134133
frTensor, other = inputCache.load(tensor, False)
135134
def bench():

taco/bench.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,27 @@ taco::TensorBase loadRandomTensor(std::string name, std::vector<int> dims, float
6161
tensor.setName(name);
6262
return tensor;
6363
}
64+
65+
std::string constructImageTensorKey(int num, int variant) {
66+
auto path = getTacoTensorPath();
67+
std::stringstream result;
68+
result << path;
69+
if (path[path.size() - 1] != '/') {
70+
result << "/";
71+
}
72+
result << "image/";
73+
if (variant == 0) {
74+
result << "image" << num << ".tns";
75+
} else {
76+
result << "image" << num << "-" << variant << ".tns";
77+
}
78+
return result.str();
79+
}
80+
81+
taco::TensorBase loadImageTensor(std::string name, int num, taco::Format format, int variant) {
82+
// For now, just say that the python code must generate the random
83+
// tensor before use.
84+
auto tensor = taco::read(constructImageTensorKey(num, variant), format, true);
85+
tensor.setName(name);
86+
return tensor;
87+
}

taco/bench.h

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ std::string getValidationOutputPath();
5050
// cleanPath ensures that the input path ends with "/".
5151
std::string cleanPath(std::string path);
5252
taco::TensorBase loadRandomTensor(std::string name, std::vector<int> dims, float sparsity, taco::Format format, int variant=0);
53+
taco::TensorBase loadImageTensor(std::string name, int num, taco::Format format, int variant=0);
5354

5455
template<typename T>
5556
taco::Tensor<T> castToType(std::string name, taco::Tensor<double> tensor) {

taco/image.cpp

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#include "bench.h"
2+
#include "benchmark/benchmark.h"
3+
4+
#include "taco/tensor.h"
5+
#include "taco/format.h"
6+
#include "taco/index_notation/index_notation.h"
7+
#include "taco/index_notation/tensor_operator.h"
8+
9+
using namespace taco;
10+
11+
// XOR Op and Algebra
12+
struct GeneralAdd {
13+
ir::Expr operator()(const std::vector<ir::Expr> &v) {
14+
taco_iassert(v.size() >= 1) << "Add operator needs at least one operand";
15+
if (v.size() == 1)
16+
return ir::Add::make(v[0], ir::Literal::zero(v[0].type()));
17+
ir::Expr add = ir::Add::make(v[0], v[1]);
18+
for (size_t idx = 2; idx < v.size(); ++idx) {
19+
add = ir::Add::make(add, v[idx]);
20+
}
21+
return add;
22+
}
23+
};
24+
25+
struct xorAlgebra {
26+
IterationAlgebra operator()(const std::vector<IndexExpr>& regions) {
27+
IterationAlgebra noIntersect = Complement(Intersect(regions[0], regions[1]));
28+
return Intersect(noIntersect, Union(regions[0], regions[1]));
29+
}
30+
};
31+
32+
struct andAlgebra {
33+
IterationAlgebra operator()(const std::vector<IndexExpr>& regions) {
34+
return Intersect(regions[0], regions[1]);
35+
}
36+
};
37+
38+
Func xorOp1("logical_xor", GeneralAdd(), xorAlgebra());
39+
Func andOp1("logical_and", GeneralAdd(), andAlgebra());
40+
41+
static void bench_imaging_xor(benchmark::State& state, const Format& f) {
42+
int dim = state.range(0);
43+
auto sparsity = 0.01;
44+
Tensor<int64_t> matrix = castToType<int64_t>("A", loadImageTensor("A", 0, f));
45+
Tensor<int64_t> matrix1 = castToType<int64_t>("B", loadImageTensor("B", 0, f, 1 /* variant */));
46+
47+
for (auto _ : state) {
48+
state.PauseTiming();
49+
Tensor<int64_t> result("result", {dim, dim}, f, 1);
50+
IndexVar i("i"), j("j");
51+
result(i, j) = xorOp1(matrix(i, j), matrix1(i, j));
52+
result.setAssembleWhileCompute(true);
53+
result.compile();
54+
state.ResumeTiming();
55+
result.compute();
56+
result = result.removeExplicitZeros(result.getFormat());
57+
58+
int nnz = 0;
59+
for (auto& it : iterate<int64_t>(result)) {
60+
nnz++;
61+
}
62+
std::cout << "Result NNZ = " << nnz << std::endl;
63+
64+
}
65+
}
66+
TACO_BENCH_ARGS(bench_imaging_xor, csr, CSR)
67+
->ArgsProduct({{5000, 10000, 20000}});
68+
69+
static void bench_imaging_fused(benchmark::State& state, const Format& f) {
70+
int dim = state.range(0);
71+
auto sparsity = 0.01;
72+
Tensor<int64_t> matrix = castToType<int64_t>("A", loadImageTensor("A", 0, f));
73+
Tensor<int64_t> matrix1 = castToType<int64_t>("B", loadImageTensor("B", 0, f, 1 /* variant */));
74+
Tensor<int64_t> matrix2 = castToType<int64_t>("C", loadImageTensor("C", 0, f, 2 /* variant */));
75+
76+
for (auto _ : state) {
77+
state.PauseTiming();
78+
Tensor<int64_t> result("result", {dim, dim}, f, 1);
79+
IndexVar i("i"), j("j");
80+
result(i, j) = xorOp1(andOp1(matrix(i, j), matrix2(i, j)), andOp1(matrix1(i, j), matrix2(i, j)));
81+
result.setAssembleWhileCompute(false);
82+
result.compile();
83+
state.ResumeTiming();
84+
result.assemble();
85+
result.compute();
86+
result = result.removeExplicitZeros(result.getFormat());
87+
88+
int nnz = 0;
89+
for (auto& it : iterate<int64_t>(result)) {
90+
nnz++;
91+
}
92+
std::cout << "Result NNZ = " << nnz << std::endl;
93+
}
94+
}
95+
TACO_BENCH_ARGS(bench_imaging_fused, csr, CSR)
96+
->ArgsProduct({{5000, 10000, 20000}});

taco/taco

taco/ufuncs.cpp

+8-13
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ struct Power {
9696

9797
struct unionRightCompAlgebra {
9898
IterationAlgebra operator()(const std::vector<IndexExpr>& regions) {
99-
return Union(regions[0], Complement(regions[1]));
99+
return Union(Intersect(regions[0], regions[1]), Complement(regions[1]));
100100
}
101101
};
102102

103103
struct rightIncAlgebra {
104104
IterationAlgebra operator()(const std::vector<IndexExpr>& regions) {
105-
return regions[1];
105+
return Union(Intersect(regions[0], regions[1]), regions[1]);
106106
}
107107
};
108108

@@ -229,10 +229,10 @@ static void bench_ufunc_fused(benchmark::State& state, const Format& f) {
229229
Tensor<int64_t> result("result", {dim, dim}, f);
230230
IndexVar i("i"), j("j");
231231
result(i, j) = nestedXorOp(matrix(i, j), matrix1(i, j), matrix2(i, j));
232-
result.setAssembleWhileCompute(true);
232+
result.setAssembleWhileCompute(false);
233233
result.compile();
234234
state.ResumeTiming();
235-
235+
result.assemble();
236236
result.compute();
237237
result = result.removeExplicitZeros(result.getFormat());
238238

@@ -287,7 +287,7 @@ std::string ufuncBenchKey(std::string tensorName, std::string funcName) {
287287
return tensorName + "-" + funcName + "-taco";
288288
}
289289

290-
static void bench_frostt_ufunc(benchmark::State& state, std::string tnsPath, Func op) {
290+
static void bench_frostt_ufunc(benchmark::State& state, std::string tnsPath, Func op, int fill_value = 0) {
291291
auto frosttTensorPath = getTacoTensorPath();
292292
frosttTensorPath += "FROSTT/";
293293
frosttTensorPath += tnsPath;
@@ -303,7 +303,7 @@ static void bench_frostt_ufunc(benchmark::State& state, std::string tnsPath, Fun
303303

304304
for (auto _ : state) {
305305
state.PauseTiming();
306-
Tensor<int64_t> result("result", frosttTensor.getDimensions(), frosttTensor.getFormat());
306+
Tensor<int64_t> result("result", frosttTensor.getDimensions(), frosttTensor.getFormat(), fill_value);
307307
result.setAssembleWhileCompute(true);
308308
switch (frosttTensor.getOrder()) {
309309
case 3: {
@@ -331,11 +331,6 @@ static void bench_frostt_ufunc(benchmark::State& state, std::string tnsPath, Fun
331331
result.compute();
332332

333333
state.PauseTiming();
334-
// int nnz = 0;
335-
// for (auto& it : iterate<int64_t>(result)) {
336-
// nnz++;
337-
// }
338-
// std::cout << "Result NNZ = " << nnz << std::endl;
339334

340335
if (auto validationPath = getValidationOutputPath(); validationPath != "") {
341336
auto key = ufuncBenchKey(tensorName, op.getName());
@@ -368,8 +363,8 @@ static void bench_frostt_ufunc(benchmark::State& state, std::string tnsPath, Fun
368363
TACO_BENCH_ARGS(bench_frostt_ufunc, name/xor, path, xorOp); \
369364
TACO_BENCH_ARGS(bench_frostt_ufunc, name/ldExp, path, ldExp); \
370365
TACO_BENCH_ARGS(bench_frostt_ufunc, name/rightShift, path, rightShift); \
371-
//TACO_BENCH_ARGS(bench_frostt_ufunc, name/pow0Comp, path, pow0Comp); \
372-
//TACO_BENCH_ARGS(bench_frostt_ufunc, name/pow1Comp, path, pow1Comp); \
366+
TACO_BENCH_ARGS(bench_frostt_ufunc, name/pow1Comp, path, pow1Comp, 1);\
367+
//TACO_BENCH_ARGS(bench_frostt_ufunc, name/pow0Comp, path, pow0Comp, 0); \
373368
374369
FOREACH_FROSTT_TENSOR(DECLARE_FROSTT_UFUNC_BENCH)
375370

0 commit comments

Comments
 (0)