From 7c500fec6dfddbf3e321f76406cfc8afe86e533e Mon Sep 17 00:00:00 2001 From: "Steven S. Lyubomirsky" Date: Wed, 17 Feb 2021 21:29:27 -0800 Subject: [PATCH 1/3] Fix off-by-one in BiasAddRel, use new reporting --- src/relay/op/nn/nn.cc | 10 ++++++++-- tests/python/relay/test_op_level1.py | 13 +++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/relay/op/nn/nn.cc b/src/relay/op/nn/nn.cc index 3e3d94c614c3..ce57c78a253c 100644 --- a/src/relay/op/nn/nn.cc +++ b/src/relay/op/nn/nn.cc @@ -61,8 +61,14 @@ bool BiasAddRel(const Array& types, int num_inputs, const Attrs& attrs, if (axis < 0) { axis = data->shape.size() + axis; } - ICHECK_LE(axis, static_cast(data->shape.size())) - << "axis " << param->axis << " is out of range"; + if (axis >= static_cast(data->shape.size())) { + reporter->GetDiagCtx().EmitFatal( + Diagnostic::Error(reporter->GetSpan()) + << "The axis in bias_add must be in range for the shape; " + << "attempted to access index " + << axis << " of " << PrettyPrint(data->shape)); + return false; + } // assign output type reporter->Assign(types[1], TensorType({data->shape[axis]}, data->dtype)); diff --git a/tests/python/relay/test_op_level1.py b/tests/python/relay/test_op_level1.py index 54d04da5e092..adbd2a64ba62 100644 --- a/tests/python/relay/test_op_level1.py +++ b/tests/python/relay/test_op_level1.py @@ -201,6 +201,18 @@ def test_bias_add(): np.testing.assert_allclose(op_res.asnumpy(), ref_res, rtol=rtol) +@pytest.mark.xfail +def test_bias_add_type_failure(): + # the axis is out of range + try: + b_add = relay.nn.bias_add(relay.const(1), relay.const(2), axis=0) + run_infer_type(b_add) + except tvm._ffi.base.TVMError: + pass + else: + assert False + + def test_expand_dims_infer_type(): for dtype in ["float16", "float32"]: n, t, d = te.size_var("n"), te.size_var("t"), 100 @@ -484,6 +496,7 @@ def test_bitserial_dense(): if __name__ == "__main__": test_concatenate() test_bias_add() + test_bias_add_type_failure() test_unary_op() test_binary_op() test_expand_dims_infer_type() From 7178723348622f250e0289c3ebe0c0109a02c35a Mon Sep 17 00:00:00 2001 From: "Steven S. Lyubomirsky" Date: Wed, 17 Feb 2021 21:33:30 -0800 Subject: [PATCH 2/3] No need to mark xfail if the exception is caught --- tests/python/relay/test_op_level1.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/python/relay/test_op_level1.py b/tests/python/relay/test_op_level1.py index adbd2a64ba62..ea5dd6948b11 100644 --- a/tests/python/relay/test_op_level1.py +++ b/tests/python/relay/test_op_level1.py @@ -201,7 +201,6 @@ def test_bias_add(): np.testing.assert_allclose(op_res.asnumpy(), ref_res, rtol=rtol) -@pytest.mark.xfail def test_bias_add_type_failure(): # the axis is out of range try: From 30ebfcd082825eae4126295c17e39a8dfe386560 Mon Sep 17 00:00:00 2001 From: "Steven S. Lyubomirsky" Date: Wed, 17 Feb 2021 21:46:52 -0800 Subject: [PATCH 3/3] lint --- src/relay/op/nn/nn.cc | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/relay/op/nn/nn.cc b/src/relay/op/nn/nn.cc index ce57c78a253c..97460ba4a98b 100644 --- a/src/relay/op/nn/nn.cc +++ b/src/relay/op/nn/nn.cc @@ -62,11 +62,10 @@ bool BiasAddRel(const Array& types, int num_inputs, const Attrs& attrs, axis = data->shape.size() + axis; } if (axis >= static_cast(data->shape.size())) { - reporter->GetDiagCtx().EmitFatal( - Diagnostic::Error(reporter->GetSpan()) + reporter->GetDiagCtx().EmitFatal(Diagnostic::Error(reporter->GetSpan()) << "The axis in bias_add must be in range for the shape; " - << "attempted to access index " - << axis << " of " << PrettyPrint(data->shape)); + << "attempted to access index " << axis << " of " + << PrettyPrint(data->shape)); return false; }