Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify some file and add test #8556

Merged
merged 9 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion oneflow/user/ops/randperm_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ namespace oneflow {
/*static*/ Maybe<void> RandpermOp::InferLogicalTensorDesc(user_op::InferContext* ctx) {
Shape* out_shape = ctx->OutputShape("out", 0);
int32_t n = ctx->Attr<int32_t>("n");
CHECK_GE_OR_RETURN(n, 0);
CHECK_GE_OR_RETURN(n, 0) << Error::RuntimeError()
<< "Expected the value of n greater than or equal to zero, "
<< "but got " << n;
*out_shape = Shape({n});
return Maybe<void>::Ok();
}
Expand Down
21 changes: 17 additions & 4 deletions oneflow/user/ops/reduce_like_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@ namespace oneflow {
const auto& reduced_axes = ctx->Attr<std::vector<int32_t>>("axis");
ReduceSbpUtil::GetRegularAxes(num_axes, reduced_axes, &conf_axes);
}

const auto& like_num_axes =
ctx->LogicalTensorDesc4InputArgNameAndIndex("like", 0).shape().NumAxes();
const bool keep_dims = (num_axes == like_num_axes);
if (!keep_dims) { CHECK_EQ_OR_RETURN(conf_axes.size(), num_axes - like_num_axes); }
if (!keep_dims) {
CHECK_EQ_OR_RETURN(conf_axes.size(), num_axes - like_num_axes)
<< Error::RuntimeError()
<< "The size of axis list must be equal to the difference of the dimension "
<< "between x tensor and like tensor";
}
auto IsReducedAxis = ReduceSbpUtil::MakePredicatorIsReducedAxis(conf_axes, num_axes);
int64_t num_reduced_axes = 0;
FOR_RANGE(int64_t, i, 0, num_axes) {
Expand Down Expand Up @@ -67,7 +73,13 @@ namespace oneflow {
const user_op::TensorDesc& x_tensor = ctx->InputTensorDesc("x", 0);
const user_op::TensorDesc& like_tensor = ctx->InputTensorDesc("like", 0);
const auto& axis = ctx->Attr<std::vector<int32_t>>("axis");
if (axis.empty()) { CHECK_EQ_OR_RETURN(x_tensor.shape(), like_tensor.shape()); }
if (axis.empty()) {
CHECK_EQ_OR_RETURN(x_tensor.shape(), like_tensor.shape())
<< Error::RuntimeError()
<< "The shape of the x tensor must be consistent to the shape of the like tensor"
<< " when the input axis list is empty";
}

user_op::TensorDesc* y_tensor = ctx->OutputTensorDesc("y", 0);
*y_tensor->mut_shape() = like_tensor.shape();
*y_tensor->mut_is_dynamic() = like_tensor.is_dynamic();
Expand All @@ -79,14 +91,15 @@ namespace oneflow {
/*static*/ Maybe<void> ReduceSumLikeOp::InferDataType(user_op::InferContext* ctx) {
const user_op::TensorDesc& x_tensor = ctx->InputTensorDesc("x", 0);
const user_op::TensorDesc& like_tensor = ctx->InputTensorDesc("like", 0);
CHECK_EQ_OR_RETURN(x_tensor.data_type(), like_tensor.data_type());
CHECK_EQ_OR_RETURN(x_tensor.data_type(), like_tensor.data_type())
<< Error::TypeError() << "Tensors x and like must have the same type";
zhongshsh marked this conversation as resolved.
Show resolved Hide resolved
*ctx->OutputDType("y", 0) = like_tensor.data_type();
return Maybe<void>::Ok();
}
/*static*/ Maybe<void> ReduceSumLikeOp::ModifyInputArg(
const GetInputArgModifier& GetInputArgModifierFn, const user_op::UserOpConfWrapper&) {
user_op::InputArgModifier* like_arg_modifier = GetInputArgModifierFn("like", 0);
CHECK_OR_RETURN(like_arg_modifier != nullptr);
CHECK_OR_RETURN(like_arg_modifier != nullptr); // NOLINT(maybe-need-error-msg)
like_arg_modifier->set_requires_grad(false);
return Maybe<void>::Ok();
}
Expand Down
6 changes: 4 additions & 2 deletions oneflow/user/ops/relu_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ namespace oneflow {
const Shape& y_shape = ctx->InputShape("y", 0);
const Shape& dy_shape = ctx->InputShape("dy", 0);
Shape* dx_shape = ctx->OutputShape("dx", 0);
CHECK_OR_RETURN(dy_shape == y_shape);
CHECK_OR_RETURN(dy_shape == y_shape)
<< Error::RuntimeError() << "Tensors y and dy must have the same shape";
*dx_shape = dy_shape;
return Maybe<void>::Ok();
}
Expand All @@ -63,7 +64,8 @@ namespace oneflow {
}
/*static*/ Maybe<void> ReluGradOp::InferDataType(user_op::InferContext* ctx) {
const DataType& data_type = ctx->InputDType("y", 0);
CHECK_EQ_OR_RETURN(ctx->InputDType("dy", 0), data_type);
CHECK_EQ_OR_RETURN(ctx->InputDType("dy", 0), data_type)
<< Error::TypeError() << "Tensors dy and y must have the same type";
*ctx->OutputDType("dx", 0) = data_type;
return Maybe<void>::Ok();
}
Expand Down
8 changes: 6 additions & 2 deletions oneflow/user/ops/reshape_like_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ namespace oneflow {
/*static*/ Maybe<void> ReshapeLikeOp::InferLogicalTensorDesc(user_op::InferContext* ctx) {
const Shape& in_shape = ctx->InputShape("in", 0);
const Shape& like_shape = ctx->InputShape("like", 0);
CHECK_EQ_OR_RETURN(in_shape.elem_cnt(), like_shape.elem_cnt());
CHECK_EQ_OR_RETURN(in_shape.elem_cnt(), like_shape.elem_cnt())
<< Error::RuntimeError()
<< "The element number of in tensor must be equal to the element number of "
"like tensor, "
<< "but got " << in_shape.elem_cnt() << " and " << like_shape.elem_cnt();
zhongshsh marked this conversation as resolved.
Show resolved Hide resolved
*ctx->OutputShape("out", 0) = like_shape;
return Maybe<void>::Ok();
}
Expand All @@ -53,7 +57,7 @@ namespace oneflow {
/*static*/ Maybe<void> ReshapeLikeOp::ModifyInputArg(
const GetInputArgModifier& GetInputArgModifierFn, const user_op::UserOpConfWrapper&) {
user_op::InputArgModifier* like_modifier = GetInputArgModifierFn("like", 0);
CHECK_NOTNULL_OR_RETURN(like_modifier);
CHECK_NOTNULL_OR_RETURN(like_modifier); // NOLINT(maybe-need-error-msg)
like_modifier->set_requires_grad(false);
return Maybe<void>::Ok();
}
Expand Down
34 changes: 34 additions & 0 deletions python/oneflow/test/exceptions/test_randperm_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 TestRandpermOp(flow.unittest.TestCase):
def test_n_value_err_mes(test_case):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def test_n_value_err_mes(test_case):
def test_randperm_n_value_err_mes(test_case):

with test_case.assertRaises(RuntimeError) as ctx:
a = flow.randperm(-1)
test_case.assertTrue(
"Expected the value of n greater than or equal to zero"
in str(ctx.exception)
)

zhongshsh marked this conversation as resolved.
Show resolved Hide resolved

if __name__ == "__main__":
unittest.main()
48 changes: 48 additions & 0 deletions python/oneflow/test/exceptions/test_reduce_like_ops.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""
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 TestReduceLikeOps(flow.unittest.TestCase):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好像第一个报错类型没有检查到,即:

if (!keep_dims) {
    CHECK_EQ_OR_RETURN(conf_axes.size(), num_axes - like_num_axes)
        << Error::RuntimeError()
        << "The size of axis list must be equal to the difference of the dimension 
      ....

def test_empty_axis_case_err(test_case):
a = flow.tensor([1, 1])
b = flow.tensor([1, 1, 1])
with test_case.assertRaises(RuntimeError) as ctx:
flow._C.reduce_sum_like(a, b, [])
test_case.assertTrue(
"The shape of the x tensor must be consistent to the shape of the like tensor"
in str(ctx.exception)
)

def test_tensor_type_err(test_case):
a = flow.tensor([1, 1], dtype=flow.int64)
b = flow.tensor([1, 1], dtype=flow.float64)
with test_case.assertRaises(TypeError) as ctx:
flow._C.reduce_sum_like(a, b, [1])
test_case.assertTrue(
"Tensors x and like must have the same type"
in str(ctx.exception)
)




if __name__ == "__main__":
unittest.main()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

空行太多了,这个文件没有格式化吗?

36 changes: 36 additions & 0 deletions python/oneflow/test/exceptions/test_reshape_like_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""
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 TestReshapeLikeOp(flow.unittest.TestCase):
def test_element_number_match_err(test_case):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def test_element_number_match_err(test_case):
def test_reshape_like_size_match_err(test_case):

a = flow.tensor([1, 1])
b = flow.tensor([[1, 1, 1], [1, 1, 1]])
with test_case.assertRaises(RuntimeError) as ctx:
flow._C.reshape_like(a, b)
test_case.assertTrue(
"The element number of in tensor must be equal to the element number of like tensor"
in str(ctx.exception)
)


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