Skip to content

Commit 3759281

Browse files
gemfieldWei Chen
authored andcommitted
Enhance upsample operator to adapt onnx opset version 9 for nnvm comp… (apache#2968)
* Enhance upsample operator to adapt onnx opset version 9 for nnvm compiler * Add upsample test case for newer opset in nnvm * re-trigger the CI
1 parent dde0f8b commit 3759281

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

nnvm/python/nnvm/frontend/onnx.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,14 @@ class Upsample(OnnxOpConverter):
404404
"""
405405

406406
@classmethod
407-
def _impl_v7(cls, inputs, attr, params):
407+
def _impl_v9(cls, inputs, attr, params):
408408
scales = attr.get('scales')
409+
if not scales:
410+
#Here we are going to higher OPSET version.
411+
assert len(inputs) == 2, "Upsample op take 2 inputs, {} given".format(len(inputs))
412+
input_name = inputs[1].list_input_names()[0]
413+
scales = params[input_name].asnumpy()
414+
inputs = inputs[:1]
409415
assert len(scales) == 4 and scales[0] == 1.0 and scales[1] == 1.0 and scales[2] == scales[3]
410416
mode = attr.get('mode')
411417
if mode == b'nearest':

nnvm/tests/python/frontend/onnx/test_forward.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,9 +438,41 @@ def _test_upsample_bilinear():
438438
tvm_out = get_tvm_output(model, in_array, target, ctx, out_shape, 'float32')
439439
tvm.testing.assert_allclose(out_array, tvm_out, rtol=1e-5, atol=1e-5)
440440

441+
def _test_upsample_bilinear_opset9():
442+
scale = 2
443+
in_shape = (1, 1, 3, 3)
444+
out_shape = (1, 1, 3*scale, 3*scale)
445+
y = helper.make_node("Upsample", ['in','scales'], ['out'], mode='linear')
446+
scales=[1.0, 1.0, 2.0, 2.0]
447+
in_array = np.random.uniform(size=in_shape).astype(np.float32)
448+
out_array = topi.testing.bilinear_resize_python(in_array, (3*scale, 3*scale), "NCHW")
449+
450+
ref_array = np.array(scales)
451+
ref_node = helper.make_node('Constant',
452+
inputs=[],
453+
outputs=['scales'],
454+
value=onnx.helper.make_tensor(name = 'const_tensor',
455+
data_type = TensorProto.FLOAT,
456+
dims = ref_array.shape,
457+
vals = ref_array.flatten().astype(float)))
458+
459+
graph = helper.make_graph([ref_node, y],
460+
'upsample_bilinear_opset9_test',
461+
inputs = [helper.make_tensor_value_info("in", TensorProto.FLOAT, list(in_shape))],
462+
outputs = [helper.make_tensor_value_info("out", TensorProto.FLOAT, list(out_shape))])
463+
464+
model = helper.make_model(graph, producer_name='upsample_bilinear_opset9_test')
465+
inputs = []
466+
inputs.append(in_array)
467+
468+
for target, ctx in ctx_list():
469+
tvm_out = get_tvm_output(model, inputs, target, ctx, out_shape, 'float32')
470+
tvm.testing.assert_allclose(out_array, tvm_out, rtol=1e-5, atol=1e-5)
471+
441472
def test_upsample():
442473
_test_upsample_nearest()
443474
_test_upsample_bilinear()
475+
_test_upsample_bilinear_opset9()
444476

445477
def _test_softmax(inshape, axis):
446478
opname = 'Softmax'

0 commit comments

Comments
 (0)