Skip to content

Commit

Permalink
Modify bias_add_op.cpp,delete unnecessary checks and improve the erro…
Browse files Browse the repository at this point in the history
…r message (#8524)

* Delete unnecessary checks and improve the error message

* Add error information

* Adjust the order

* Add a space to "]"

* modify the format->[ 0,1 ] to [0, 1]

* modity "RuntimeError()" into "IndexError()" in   CHECK_LT_OR_RETURN(bias_add_axis, a_tensor_desc.shape().NumAxes())

* auto format by CI

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: oneflow-ci-bot <ci-bot@oneflow.org>
  • Loading branch information
3 people authored Jun 30, 2022
1 parent 2b8edb2 commit 43d8c30
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
17 changes: 13 additions & 4 deletions oneflow/user/ops/bias_add_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,19 @@ namespace oneflow {
const auto& a_tensor_desc = ctx->InputTensorDesc("a", 0);
const auto& b_tensor_desc = ctx->InputTensorDesc("b", 0);
const auto bias_add_axis = ctx->Attr<int32_t>("axis");
CHECK_EQ_OR_RETURN(b_tensor_desc.shape().NumAxes(), 1);
CHECK_GE_OR_RETURN(bias_add_axis, 0);
CHECK_LT_OR_RETURN(bias_add_axis, a_tensor_desc.shape().NumAxes());
CHECK_EQ_OR_RETURN(a_tensor_desc.shape().At(bias_add_axis), b_tensor_desc.shape().At(0));
CHECK_EQ_OR_RETURN(b_tensor_desc.shape().NumAxes(), 1)
<< Error::RuntimeError() << "Bias tensor has to be a one-dimensional vector";
CHECK_GE_OR_RETURN(bias_add_axis, 0)
<< Error::RuntimeError() << "The size of the axis must greater than or equal to 0, "
<< "but got " << bias_add_axis;
CHECK_LT_OR_RETURN(bias_add_axis, a_tensor_desc.shape().NumAxes())
<< Error::IndexError() << "Dimension out of range (expected to be in range of [0"
<< ", " << a_tensor_desc.shape().NumAxes() - 1 << "],"
<< " but got " << bias_add_axis << ")";
CHECK_EQ_OR_RETURN(a_tensor_desc.shape().At(bias_add_axis), b_tensor_desc.shape().At(0))
<< Error::RuntimeError() << "The size of tensor " << a_tensor_desc.shape().ToString()
<< " must match the size of tensor " << b_tensor_desc.shape().ToString() << " at dimension "
<< bias_add_axis;
*ctx->OutputShape("out", 0) = ctx->InputShape("a", 0);
*ctx->OutputIsDynamic("out", 0) = ctx->InputIsDynamic("a", 0);
return Maybe<void>::Ok();
Expand Down
34 changes: 34 additions & 0 deletions python/oneflow/test/exceptions/test_bias_add_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
Copyright 2020 The OneFlow Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import unittest
import oneflow as flow
import oneflow.unittest


class TestBiasAdd(flow.unittest.TestCase):
def test_b_tensor_numaxes_err(test_case):
with test_case.assertRaises(RuntimeError) as context:
x = flow.tensor([[1, 1], [2, 2]])
y = flow.tensor([[2, 2], [1, 1]])
out = flow._C.bias_add(y, x, axis=0)
test_case.assertTrue(
"Bias tensor has to be a one-dimensional vector" in str(context.exception)
)


if __name__ == "__main__":
unittest.main()

0 comments on commit 43d8c30

Please sign in to comment.