Skip to content

Commit 98f3bab

Browse files
kevinthesunicemelon
authored andcommitted
Modify topi tests (apache#9)
* Add pooling, reorg, softmax and vision * Add lrn
1 parent 67878be commit 98f3bab

6 files changed

Lines changed: 126 additions & 29 deletions

File tree

topi/tests/python/common.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ def get_schedule_reduce(target):
6464
return _reduce_schedule[key]
6565
return _reduce_schedule["generic"]
6666

67+
def get_schedule(target, schedule):
68+
if isinstance(target, str):
69+
target = tvm.target.create(target)
70+
for key in target.keys:
71+
if key in schedule:
72+
return schedule[key]
73+
return schedule["generic"]
74+
6775
get_schedule_broadcast = get_schedule_injective
6876
get_schedule_elemwise = get_schedule_injective
6977

topi/tests/python/test_topi_lrn.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@
2121
from topi.util import get_const_tuple
2222
import topi.testing
2323

24+
from common import get_schedule
25+
26+
_lrn_schedule = {
27+
"generic": topi.generic.schedule_lrn,
28+
"gpu": topi.cuda.schedule_lrn,
29+
"opencl": topi.cuda.schedule_lrn,
30+
"metal": topi.cuda.schedule_lrn,
31+
"rocm": topi.cuda.schedule_lrn,
32+
"vulkan": topi.cuda.schedule_lrn,
33+
"nvptx": topi.cuda.schedule_lrn,
34+
}
35+
2436
def verify_lrn(shape, size, axis, bias, alpha, beta):
2537
A = tvm.placeholder(shape, name='A')
2638
B = topi.nn.lrn(A, size, axis, alpha, beta, bias)
@@ -35,10 +47,8 @@ def check_device(device):
3547
return
3648
print("Running on target: %s" % device)
3749
with tvm.target.create(device):
38-
if device == 'llvm':
39-
s = topi.generic.schedule_lrn([B])
40-
else:
41-
s = topi.cuda.schedule_lrn([B])
50+
s_func = get_schedule(device, _lrn_schedule)
51+
s = s_func([B])
4252
ctx = tvm.context(device, 0)
4353
a = tvm.nd.array(a_np, ctx)
4454
b = tvm.nd.array(np.zeros(get_const_tuple(B.shape), dtype=dtype), ctx)

topi/tests/python/test_topi_pooling.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,26 @@
2121
import topi
2222
import topi.testing
2323
from topi.util import get_const_tuple
24-
from common import get_all_backend
24+
from common import get_all_backend, get_schedule
25+
26+
_pool_schedule = {
27+
"generic": topi.generic.schedule_pool,
28+
"cpu": topi.x86.schedule_pool,
29+
"gpu": topi.cuda.schedule_pool,
30+
"hls": topi.hls.schedule_pool,
31+
}
32+
33+
_adaptive_pool_schedule = {
34+
"generic": topi.generic.schedule_adaptive_pool,
35+
"cpu": topi.x86.schedule_adaptive_pool,
36+
"gpu": topi.cuda.schedule_adaptive_pool,
37+
"hls": topi.hls.schedule_adaptive_pool,
38+
}
39+
40+
_pool_grad_schedule = {
41+
"generic": topi.generic.schedule_pool_grad,
42+
"gpu": topi.cuda.schedule_pool_grad,
43+
}
2544

2645
def verify_pool(n, ic, ih, kh, sh, padding, pool_type, ceil_mode, count_include_pad=True):
2746
iw = ih
@@ -74,7 +93,8 @@ def check_device(device):
7493
return
7594
print("Running on target: %s" % device)
7695
with tvm.target.create(device):
77-
s = topi.generic.schedule_pool(B, layout)
96+
s_func = get_schedule(device, _pool_schedule)
97+
s = s_func(B, layout)
7898

7999
a = tvm.nd.array(a_np, ctx)
80100
b = tvm.nd.array(np.zeros(get_const_tuple(B.shape), dtype=dtype), ctx)
@@ -129,7 +149,8 @@ def check_device(device):
129149
return
130150
print("Running on target: %s" % device)
131151
with tvm.target.create(device):
132-
s = topi.generic.schedule_pool_grad(PoolGrad)
152+
s_func = get_schedule(device, _pool_grad_schedule)
153+
s = s_func(PoolGrad)
133154

134155
a = tvm.nd.array(a_np, ctx)
135156
out_grad = tvm.nd.array(out_grad_np, ctx)
@@ -201,7 +222,8 @@ def check_device(device):
201222
return
202223
print("Running on target: %s" % device)
203224
with tvm.target.create(device):
204-
s = topi.generic.schedule_adaptive_pool(B)
225+
s_func = get_schedule(device, _adaptive_pool_schedule)
226+
s = s_func(B)
205227
a = tvm.nd.array(a_np, ctx)
206228
b = tvm.nd.array(np.zeros(get_const_tuple(B.shape), dtype=B.dtype), ctx)
207229
f = tvm.build(s, [A, B], device)
@@ -255,7 +277,8 @@ def check_device(device):
255277
return
256278
print("Running on target: %s" % device)
257279
with tvm.target.create(device):
258-
s = topi.generic.schedule_adaptive_pool(out)
280+
s_func = get_schedule(device, _adaptive_pool_schedule)
281+
s = s_func(out)
259282
a = tvm.nd.array(np_data, ctx)
260283
b = tvm.nd.array(np.zeros(get_const_tuple(oshape), dtype=out.dtype), ctx)
261284
f = tvm.build(s, [data, out], device)
@@ -298,7 +321,8 @@ def check_device(device):
298321
return
299322
print("Running on target: %s" % device)
300323
with tvm.target.create(device):
301-
s = topi.generic.schedule_pool(B, layout)
324+
s_func = get_schedule(device, _pool_schedule)
325+
s = s_func(B, layout)
302326

303327
a = tvm.nd.array(input_np, ctx)
304328
b = tvm.nd.array(np.zeros(get_const_tuple(B.shape), dtype=dtype), ctx)
@@ -350,7 +374,8 @@ def check_device(device):
350374
return
351375
print("Running on target: %s" % device)
352376
with tvm.target.create(device):
353-
s = topi.generic.schedule_pool(B, layout)
377+
s_func = _pool_schedule[device] if device in _pool_schedule else _pool_schedule["generic"]
378+
s = s_func(B, layout)
354379

355380
a = tvm.nd.array(input_np, ctx)
356381
b = tvm.nd.array(np.zeros(get_const_tuple(B.shape), dtype=dtype), ctx)

topi/tests/python/test_topi_reorg.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
from topi.util import get_const_tuple
2121
import tvm
2222
import topi.testing
23+
from common import get_schedule
24+
25+
_reorg_schedule = {
26+
"generic": topi.generic.schedule_reorg,
27+
"gpu": topi.cuda.schedule_reorg,
28+
}
2329

2430
def verify_reorg(batch, in_size, in_channel, stride):
2531
'''Verify reorg operator by comparing outputs from tvm and numpy implementation'''
@@ -46,10 +52,8 @@ def check_device(device):
4652
return
4753
print("Running on target: %s" % device)
4854
with tvm.target.create(device):
49-
if device == 'llvm':
50-
s = topi.generic.schedule_reorg([B])
51-
else:
52-
s = topi.cuda.schedule_reorg([B])
55+
s_func = get_schedule(device, _reorg_schedule)
56+
s = s_func([B])
5357
a = tvm.nd.array(a_np, ctx)
5458
b = tvm.nd.array(np.zeros(get_const_tuple(B.shape), dtype=B.dtype), ctx)
5559
func = tvm.build(s, [A, B], device)

topi/tests/python/test_topi_softmax.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,15 @@
2323
import logging
2424
from topi.util import get_const_tuple
2525

26-
from common import get_all_backend
26+
from common import get_all_backend, get_schedule
27+
28+
_softmax_schedule = {
29+
"generic": topi.generic.schedule_softmax,
30+
"cpu": topi.x86.schedule_softmax,
31+
"gpu": topi.cuda.schedule_softmax,
32+
"hls": topi.hls.schedule_softmax,
33+
"opengl": topi.opengl.schedule_softmax,
34+
}
2735

2836
def check_device(A, B, a_np, b_np, device, name):
2937
ctx = tvm.context(device, 0)
@@ -32,11 +40,12 @@ def check_device(A, B, a_np, b_np, device, name):
3240
return
3341
print("Running on target: %s" % device)
3442
with tvm.target.create(device):
35-
s = topi.generic.schedule_softmax(B)
43+
s_func = get_schedule(device, _softmax_schedule)
44+
s = s_func(B)
3645

3746
a = tvm.nd.array(a_np, ctx)
3847
b = tvm.nd.array(np.zeros(get_const_tuple(B.shape), dtype=B.dtype), ctx)
39-
f = tvm.build(s, [A, B], device, name="softmax")
48+
f = tvm.build(s, [A, B], device, name=name)
4049
f(a, b)
4150
tvm.testing.assert_allclose(b.asnumpy(), b_np, rtol=1e-5)
4251

@@ -50,7 +59,7 @@ def verify_softmax(m, n, dtype="float32"):
5059
a_np = np.random.uniform(size=get_const_tuple(A.shape)).astype(A.dtype)
5160
b_np = topi.testing.softmax_python(a_np)
5261

53-
for device in ['cuda', 'opencl', 'metal', 'rocm', 'vulkan', 'nvptx']:
62+
for device in get_all_backend():
5463
check_device(A, B, a_np, b_np, device, "softmax")
5564

5665
def verify_softmax_4d(shape, dtype="float32"):
@@ -62,7 +71,7 @@ def verify_softmax_4d(shape, dtype="float32"):
6271
b_np = topi.testing.softmax_python(a_np.transpose(0, 2, 3, 1).reshape(h*w, c))
6372
b_np = b_np.reshape(1, h, w, c).transpose(0, 3, 1, 2)
6473

65-
for device in ['cuda', 'opencl', 'metal', 'rocm', 'vulkan', 'nvptx']:
74+
for device in get_all_backend():
6675
check_device(A, B, a_np, b_np, device, "softmax")
6776

6877
def test_softmax():

topi/tests/python/test_topi_vision.py

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,42 @@
2626
from topi.util import get_const_tuple
2727
from topi.vision import ssd, non_max_suppression, get_valid_counts
2828

29+
from common import get_schedule
30+
31+
_get_valid_counts_schedule = {
32+
"generic": topi.generic.schedule_get_valid_counts,
33+
"gpu": topi.cuda.schedule_get_valid_counts,
34+
}
35+
36+
_nms_schedule = {
37+
"generic": topi.generic.schedule_nms,
38+
"gpu": topi.cuda.schedule_nms,
39+
}
40+
41+
_multibox_prior_schedule = {
42+
"generic": topi.generic.schedule_multibox_prior,
43+
"gpu": topi.cuda.schedule_multibox_prior,
44+
}
45+
46+
_multibox_detection_schedule = {
47+
"generic": topi.generic.schedule_multibox_detection,
48+
"gpu": topi.cuda.schedule_multibox_detection,
49+
}
50+
51+
_roi_align_schedule = {
52+
"generic": topi.generic.schedule_roi_align,
53+
"gpu": topi.cuda.schedule_roi_align,
54+
}
55+
56+
_roi_pool_schedule = {
57+
"generic": topi.generic.schedule_roi_pool,
58+
"gpu": topi.cuda.schedule_roi_pool,
59+
}
60+
61+
_proposal_schedule = {
62+
"generic": topi.generic.schedule_proposal,
63+
"gpu": topi.cuda.schedule_proposal,
64+
}
2965

3066
def verify_get_valid_counts(dshape, score_threshold, id_index, score_index):
3167
dtype = "float32"
@@ -56,7 +92,8 @@ def check_device(device):
5692
with tvm.target.create(device):
5793
data = tvm.placeholder(dshape, name="data", dtype=dtype)
5894
outs = get_valid_counts(data, score_threshold, id_index, score_index)
59-
s = topi.generic.schedule_get_valid_counts(outs)
95+
s_func = get_schedule(device, _get_valid_counts_schedule)
96+
s = s_func(outs)
6097

6198
tvm_input_data = tvm.nd.array(np_data, ctx)
6299
tvm_out1 = tvm.nd.array(np.zeros(np_out1.shape, dtype="int32"), ctx)
@@ -68,8 +105,6 @@ def check_device(device):
68105

69106
for device in ['llvm', 'cuda', 'opencl']:
70107
# Disable gpu test for now
71-
if device != "llvm":
72-
continue
73108
check_device(device)
74109

75110

@@ -107,7 +142,8 @@ def check_device(device):
107142
return_indices=False)
108143
indices_out = topi.cuda.non_max_suppression(data, valid_count, -1, iou_threshold, force_suppress, top_k,
109144
coord_start=coord_start, score_index=score_index, id_index=id_index)
110-
s = topi.generic.schedule_nms(out)
145+
s_func = get_schedule(device, _nms_schedule)
146+
s = s_func(out)
111147
indices_s = topi.generic.schedule_nms(indices_out)
112148

113149
tvm_data = tvm.nd.array(np_data, ctx)
@@ -198,7 +234,8 @@ def check_device(device):
198234
out = ssd.multibox_prior(data, sizes, ratios, steps, offsets, clip)
199235
else:
200236
out = topi.cuda.ssd.multibox_prior(data, sizes, ratios, steps, offsets, clip)
201-
s = topi.generic.schedule_multibox_prior(out)
237+
s_func = get_schedule(device, _multibox_prior_schedule)
238+
s = s_func(out)
202239

203240
tvm_input_data = tvm.nd.array(input_data, ctx)
204241
tvm_out = tvm.nd.array(np.zeros(oshape, dtype=dtype), ctx)
@@ -244,7 +281,8 @@ def check_device(device):
244281
out = ssd.multibox_detection(cls_prob, loc_preds, anchors)
245282
else:
246283
out = topi.cuda.ssd.multibox_detection(cls_prob, loc_preds, anchors)
247-
s = topi.generic.schedule_multibox_detection(out)
284+
s_func = get_schedule(device, _multibox_detection_schedule)
285+
s = s_func(out)
248286

249287
tvm_cls_prob = tvm.nd.array(np_cls_prob.astype(cls_prob.dtype), ctx)
250288
tvm_loc_preds = tvm.nd.array(np_loc_preds.astype(loc_preds.dtype), ctx)
@@ -289,7 +327,8 @@ def check_device(device):
289327
b = topi.vision.rcnn.roi_align_nchw(a, rois, pooled_size=pooled_size,
290328
spatial_scale=spatial_scale,
291329
sample_ratio=sample_ratio)
292-
s = topi.generic.schedule_roi_align(b)
330+
s_func = get_schedule(device, _roi_align_schedule)
331+
s = s_func(b)
293332

294333
tvm_a = tvm.nd.array(a_np, ctx)
295334
tvm_rois = tvm.nd.array(rois_np, ctx)
@@ -338,7 +377,8 @@ def check_device(device):
338377
with tvm.target.create(device):
339378
b = topi.vision.rcnn.roi_pool_nchw(a, rois, pooled_size=pooled_size,
340379
spatial_scale=spatial_scale)
341-
s = topi.generic.schedule_roi_pool(b)
380+
s_func = get_schedule(device, _roi_pool_schedule)
381+
s = s_func(b)
342382

343383
tvm_a = tvm.nd.array(a_np, ctx)
344384
tvm_rois = tvm.nd.array(rois_np, ctx)
@@ -369,7 +409,8 @@ def check_device(device):
369409
print("Running on target: %s" % device)
370410
with tvm.target.create(device):
371411
out = topi.vision.proposal(cls_prob, bbox_pred, im_info, **attrs)
372-
s = topi.generic.schedule_proposal(out)
412+
s_func = get_schedule(device, _proposal_schedule)
413+
s = s_func(out)
373414
f = tvm.build(s, [cls_prob, bbox_pred, im_info, out], device)
374415
tvm_cls_prob = tvm.nd.array(np_cls_prob, ctx=ctx)
375416
tvm_bbox_pred = tvm.nd.array(np_bbox_pred, ctx=ctx)

0 commit comments

Comments
 (0)