Skip to content

Commit 90ad764

Browse files
committed
Change some parameters to improve performance
1 parent 55dcf1a commit 90ad764

File tree

3 files changed

+92
-26
lines changed

3 files changed

+92
-26
lines changed

numpy/image.py

+80-8
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
import os
44
import pytest
55
import sparse
6-
from util import ImagePydataSparseTensorLoader, safeCastPydataTensorToInts, TnsFileDumper #plot_image
6+
from util import ImagePydataSparseTensorLoader, safeCastPydataTensorToInts, TnsFileDumper#, plot_image
77

88
# import matplotlib.pyplot as plt
99

1010
@pytest.mark.parametrize("num", list(range(1, 99)))
11-
@pytest.mark.parametrize("pt1", [0.5])
11+
@pytest.mark.parametrize("pt1", [0.75])
1212
def bench_edge_detection_pydata(tacoBench, num, pt1, plot):
1313
loader = ImagePydataSparseTensorLoader()
1414
sparse_bin_img1 = safeCastPydataTensorToInts(loader.sparse_image(num, pt1, 1))
@@ -44,7 +44,7 @@ def dense_bench():
4444
#plot_image(loader.img[num], bin_img1, bin_img2, xor_img, sparse_xor_img, t1, t2)
4545

4646
@pytest.mark.parametrize("num", list(range(1, 99)))
47-
@pytest.mark.parametrize("pt1", [0.5])
47+
@pytest.mark.parametrize("pt1", [0.75])
4848
def bench_edge_detection_dense(tacoBench, num, pt1):
4949
loader = ImagePydataSparseTensorLoader()
5050
bin_img1 = loader.dense_image(num, pt1, 1)
@@ -56,7 +56,7 @@ def dense_bench():
5656
tacoBench(dense_bench)
5757

5858
@pytest.mark.parametrize("num", list(range(1, 99)))
59-
@pytest.mark.parametrize("pt1", [0.5])
59+
@pytest.mark.parametrize("pt1", [0.75])
6060
def bench_edge_detection_fused_pydata(tacoBench, num, pt1, plot):
6161
loader = ImagePydataSparseTensorLoader()
6262
sparse_bin_img1 = safeCastPydataTensorToInts(loader.sparse_image(num, pt1, 1))
@@ -94,12 +94,12 @@ def dense_bench():
9494
sparse_xor_img = sparse_xor_img.todense()
9595
t1 = round(loader.max[num]*pt1, 2)
9696
t2 = round(loader.max[num]*(pt1 + 0.05), 2)
97-
#plot_image(loader.img[num], bin_img1, bin_img2, xor_img, sparse_xor_img, t1, t2, bin_window)
97+
plot_image(loader.img[num], bin_img1, bin_img2, xor_img, sparse_xor_img, t1, t2, bin_window)
9898

9999
assert(sparse_xor_img.nnz == np.sum(xor_img != 0))
100100

101101
@pytest.mark.parametrize("num", list(range(1, 99)))
102-
@pytest.mark.parametrize("pt1", [0.5])
102+
@pytest.mark.parametrize("pt1", [0.75])
103103
def bench_edge_detection_fused_dense(tacoBench, num, pt1):
104104
loader = ImagePydataSparseTensorLoader()
105105
bin_img1 = loader.dense_image(num, pt1, 1)
@@ -115,7 +115,7 @@ def dense_bench():
115115

116116
#TODO: Add in a benchmark that uses windowing for medical imaging as well.
117117
@pytest.mark.parametrize("num", list(range(1, 99)))
118-
@pytest.mark.parametrize("pt1", [0.5])
118+
@pytest.mark.parametrize("pt1", [0.75])
119119
@pytest.mark.parametrize("window_size", [0.25, 0.2, 0.15, 0.1])
120120
def bench_edge_detection_window_pydata(tacoBench, num, pt1, window_size, plot):
121121
loader = ImagePydataSparseTensorLoader()
@@ -168,7 +168,7 @@ def dense_bench():
168168
assert(sparse_xor_img.nnz == np.sum(xor_img != 0))
169169

170170
@pytest.mark.parametrize("num", list(range(1, 99)))
171-
@pytest.mark.parametrize("pt1", [0.5])
171+
@pytest.mark.parametrize("pt1", [0.75])
172172
@pytest.mark.parametrize("window_size", [0.25, 0.2, 0.15, 0.1])
173173
def bench_edge_detection_window_dense(tacoBench, num, pt1, window_size):
174174
loader = ImagePydataSparseTensorLoader()
@@ -192,6 +192,7 @@ def dense_bench():
192192
# USED FOR TESTING ITTERATION LATTICE CONSTRUCTION TACO CODE ONLY
193193
def testOp(a, b, c):
194194
return np.logical_and(np.logical_not(np.logical_and(a, c).astype('int')).astype('int'), np.logical_not(np.logical_and(b, c).astype('int')).astype('int')).astype('int')
195+
195196
@pytest.mark.skip(reason="Used for verification only")
196197
@pytest.mark.parametrize("num", list(range(1, 11)))
197198
@pytest.mark.parametrize("pt1", [0.5])
@@ -233,3 +234,74 @@ def dense_bench():
233234
print("Sparse xor NNF = ", sparse_xor_img.nnz, "\t", "Dense xor NNF = ", np.sum(xor_img != int(f)))
234235
print("Dense xor NNZ = ", np.sum(xor_img != 0))
235236
assert(sparse_xor_img.nnz == np.sum(xor_img != 1))
237+
238+
@pytest.mark.skip(reason="for getting the input matrices statistics only")
239+
@pytest.mark.parametrize("num", list(range(1, 99)))
240+
@pytest.mark.parametrize("pt1", [0.75])
241+
def bench_edge_detection_statistics(tacoBench, num, pt1):
242+
loader = ImagePydataSparseTensorLoader()
243+
sparse_bin_img1 = safeCastPydataTensorToInts(loader.sparse_image(num, pt1, 1))
244+
sparse_bin_img2 = safeCastPydataTensorToInts(loader.sparse_image(num, pt1+0.05, 2))
245+
sparse_bin_window = loader.sparse_window(num, 3)
246+
247+
print(sparse_bin_img1.shape)
248+
print(sparse_bin_img2.shape)
249+
extra_info = dict()
250+
extra_info['nnz1'] = sparse_bin_img1.nnz
251+
extra_info['nnz2'] = sparse_bin_img2.nnz
252+
extra_info['nnz3'] = sparse_bin_window.nnz
253+
extra_info['dimx'] = sparse_bin_window.shape[0]
254+
extra_info['dimy'] = sparse_bin_window.shape[1]
255+
256+
def sparse_bench():
257+
sbi1 = np.logical_and(sparse_bin_img1, sparse_bin_window)
258+
sbi2 = np.logical_and(sparse_bin_img2, sparse_bin_window)
259+
sparse_xor_img = np.logical_xor(sbi1, sbi2).astype('int')
260+
return sparse_xor_img
261+
262+
tacoBench(sparse_bench, extra_info)
263+
264+
@pytest.mark.skip(reasoun="For image generation only")
265+
@pytest.mark.parametrize("num", [42, 44, 50, 63, 92])
266+
@pytest.mark.parametrize("pt1", [0.75])
267+
def bench_edge_detection_fused_pydata(tacoBench, num, pt1, plot):
268+
loader = ImagePydataSparseTensorLoader()
269+
sparse_bin_img1 = safeCastPydataTensorToInts(loader.sparse_image(num, pt1, 1))
270+
sparse_bin_img2 = safeCastPydataTensorToInts(loader.sparse_image(num, pt1+0.05, 2))
271+
sparse_bin_window = loader.sparse_window(num, 3)
272+
bin_img1 = loader.dense_image(num, pt1, 1)
273+
bin_img2 = loader.dense_image(num, pt1 + 0.05, 2)
274+
bin_window = loader.dense_window(num)
275+
276+
if plot:
277+
print(sparse_bin_img1.shape)
278+
print(sparse_bin_img2.shape)
279+
280+
def sparse_bench():
281+
sbi1 = np.logical_and(sparse_bin_img1, sparse_bin_window)
282+
sbi2 = np.logical_and(sparse_bin_img2, sparse_bin_window)
283+
sparse_xor_img = np.logical_xor(sbi1, sbi2).astype('int')
284+
return sparse_xor_img
285+
286+
def dense_bench():
287+
bi1 = np.logical_and(bin_img1, bin_window).astype('int')
288+
bi2 = np.logical_and(bin_img2, bin_window).astype('int')
289+
xor_img = np.logical_xor(bi1, bi2).astype('int')
290+
return xor_img
291+
ret = tacoBench(sparse_bench)
292+
sparse_xor_img = sparse_bench()
293+
xor_img = dense_bench()
294+
295+
if plot:
296+
num_elements = float(np.prod(bin_img1.shape))
297+
print("Sparse xor NNZ = ", sparse_xor_img.nnz, "\t", "Dense xor NNZ = ", np.sum(xor_img != 0))
298+
print("Sparsity img 1 ", np.sum(bin_img1 != 0) / num_elements)
299+
print("Sparsity img 2 ", np.sum(bin_img2 != 0) / num_elements)
300+
print("Sparsity xor ", np.sum(xor_img != 0) / num_elements)
301+
sparse_xor_img = sparse_xor_img.todense()
302+
t1 = round(loader.max[num]*pt1, 2)
303+
t2 = round(loader.max[num]*(pt1 + 0.05), 2)
304+
#plot_image(loader.img[num], bin_img1, bin_img2, xor_img, sparse_xor_img, t1, t2, bin_window)
305+
306+
assert(sparse_xor_img.nnz == np.sum(xor_img != 0))
307+

numpy/util.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,8 @@ def dense_window(self, num):
400400
result_np = numpy.zeros(shape)
401401
m0 = int(shape[0] / 2)
402402
m1 = int(shape[1] / 2)
403-
dm0 = int(0.2*m0)
404-
dm1 = int(0.2*m1)
403+
dm0 = int(0.1*m0)
404+
dm1 = int(0.1*m1)
405405
result_np[m0+dm0:m0+3*dm0, m1+dm1:m1+3*dm1] = 1
406406
result_np[m0-3*dm0:m0-dm0, m1+dm1:m1+3*dm1] = 1
407407
result_np[m0-3*dm0:m0-dm0, m1-3*dm1:m1-dm1] = 1

taco/image.cpp

+10-16
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ Func andOp1("logical_and", Boolean(), andAlgebra());
6868
Func xorAndOp("fused_xor_and", Boolean(), xorAndAlgebra());
6969
Func testOp("test", Boolean(), testConstructionAlgebra());
7070
static void bench_image_xor(benchmark::State& state, const Format& f) {
71-
auto t1 = 0.5;
72-
auto t2 = 0.55;
71+
auto t1 = 0.75;
72+
auto t2 = 0.80;
7373

7474
auto num_str = getEnvVar("IMAGE_NUM");
7575
if (num_str == "") {
@@ -78,7 +78,7 @@ static void bench_image_xor(benchmark::State& state, const Format& f) {
7878
}
7979

8080
int num = std::stoi(num_str);
81-
81+
// int num = state.range(0);
8282
taco::Tensor<int64_t> matrix1, matrix2;
8383
try {
8484
matrix1 = castToTypeZero<int64_t>("A", loadImageTensor("A", num, f, t1, 1 /* variant */));
@@ -101,12 +101,6 @@ static void bench_image_xor(benchmark::State& state, const Format& f) {
101101
result.compile();
102102
state.ResumeTiming();
103103
result.compute();
104-
result = result.removeExplicitZeros(result.getFormat());
105-
106-
int nnz = 0;
107-
for (auto& it : iterate<int64_t>(result)) {
108-
nnz++;
109-
}
110104
// std::cout << "Result NNZ = " << nnz << std::endl;
111105
// std::cout << result << std::endl;
112106
}
@@ -115,11 +109,11 @@ static void CustomArguments(benchmark::internal::Benchmark* b) {
115109
for (int i = 1; i <= 98; ++i)
116110
b->Args({i});
117111
}
118-
TACO_BENCH_ARGS(bench_image_xor, csr, CSR);
112+
TACO_BENCH_ARGS(bench_image_xor, csr, CSR);//->Apply(CustomArguments);
119113

120114
static void bench_image_fused(benchmark::State& state, const Format& f) {
121115
// int num = state.range(0);
122-
auto t1 = 0.5;
116+
auto t1 = 0.75;
123117
auto t2 = 0.55;
124118

125119
auto num_str = getEnvVar("IMAGE_NUM");
@@ -174,7 +168,7 @@ static void bench_image_fused(benchmark::State& state, const Format& f) {
174168
result.compile();
175169
state.ResumeTiming();
176170
result.compute();
177-
result = result.removeExplicitZeros(result.getFormat());
171+
// result = result.removeExplicitZeros(result.getFormat());
178172

179173
// int nnz = 0;
180174
// for (auto& it : iterate<int64_t>(result)) {
@@ -192,8 +186,8 @@ TACO_BENCH_ARGS(bench_image_fused, csr, CSR);
192186

193187
static void bench_image_window(benchmark::State& state, const Format& f, double window_size) {
194188
// int num = state.range(0);
195-
auto t1 = 0.5;
196-
auto t2 = 0.55;
189+
auto t1 = 0.75;
190+
auto t2 = 0.80;
197191

198192
auto num_str = getEnvVar("IMAGE_NUM");
199193
if (num_str == "") {
@@ -233,7 +227,7 @@ static void bench_image_window(benchmark::State& state, const Format& f, double
233227
result.compile();
234228
state.ResumeTiming();
235229
result.compute();
236-
result = result.removeExplicitZeros(result.getFormat());
230+
// result = result.removeExplicitZeros(result.getFormat());
237231

238232
// int nnz = 0;
239233
// for (auto& it : iterate<int64_t>(result)) {
@@ -251,4 +245,4 @@ static void bench_image_window(benchmark::State& state, const Format& f, double
251245
TACO_BENCH_ARGS(bench_image_window, csr/0.25, CSR, 0.25);
252246
TACO_BENCH_ARGS(bench_image_window, csr/0.2, CSR, 0.2);
253247
TACO_BENCH_ARGS(bench_image_window, csr/0.15, CSR, 0.15);
254-
TACO_BENCH_ARGS(bench_image_window, csr/0.1, CSR, 0.1);
248+
TACO_BENCH_ARGS(bench_image_window, csr/0.1, CSR, 0.1);

0 commit comments

Comments
 (0)