Skip to content

Commit

Permalink
[Relay][bugfix][error reporting] BiasAddRel does not check for a nega…
Browse files Browse the repository at this point in the history
…tive index being out of bounds (apache#7554)
  • Loading branch information
slyubomirsky authored and Trevor Morris committed May 6, 2021
1 parent 6c1716d commit cd34b06
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/relay/op/nn/nn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ bool BiasAddRel(const Array<Type>& types, int num_inputs, const Attrs& attrs,
if (axis < 0) {
axis = data->shape.size() + axis;
}
if (axis >= static_cast<int>(data->shape.size())) {
if (axis >= static_cast<int>(data->shape.size()) || axis < 0) {
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 "
<< "attempted to access index " << param->axis << " of "
<< PrettyPrint(data->shape));
return false;
}
Expand Down
18 changes: 10 additions & 8 deletions tests/python/relay/test_op_level1.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,16 @@ def test_bias_add():


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 assert_failure(expr):
try:
run_infer_type(expr)
except tvm._ffi.base.TVMError:
return
else:
assert False

for axis in (0, -1, -3, 1):
assert_failure(relay.nn.bias_add(relay.const(1), relay.const(2), axis=axis))


def test_expand_dims_infer_type():
Expand Down

0 comments on commit cd34b06

Please sign in to comment.