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

Align round op to support round half to even #9135

Merged
merged 5 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion oneflow/core/ep/common/primitive/unary_functor.h
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ template<DeviceType device, typename Dst, typename Src>
struct UnaryFunctor<device, UnaryOp::kRound, Dst, Src> {
OF_DEVICE_FUNC UnaryFunctor(Scalar attr0, Scalar attr1) {}

OF_DEVICE_FUNC Dst operator()(Src src) const { return static_cast<Dst>(round(src)); }
OF_DEVICE_FUNC Dst operator()(Src src) const { return static_cast<Dst>(nearbyint(src)); }
};

template<DeviceType device, typename Dst, typename Src>
Expand Down
4 changes: 4 additions & 0 deletions python/oneflow/framework/docstr/math_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,10 @@
add_docstr(
oneflow.round,
r"""This operator rounds the value of Blob to the nearest integer.

.. note::
This function implements the "round half to even" to break ties when a number is equidistant from two integers (e.g. `round(2.5)` is 2).

Args:
input (oneflow.Tensor): A Tensor
Returns:
Expand Down
3 changes: 0 additions & 3 deletions python/oneflow/test/modules/test_global_round.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ def _test_round_impl(test_case, ndim, placement, sbp):
return z


@unittest.skip(
"TODO: special input is not align with pytorch: https://github.com/Oneflow-Inc/oneflow/issues/9074"
)
class TestRoundGlobal(flow.unittest.TestCase):
@globaltest
def test_round(test_case):
Expand Down
12 changes: 12 additions & 0 deletions python/oneflow/test/modules/test_round.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ def test_flow_round_with_0dim_data(test_case):
y = torch.round(x)
return y

@autotest(check_graph=True)
def test_flow_round_half_to_even(test_case):
device = random_device()
random_shape = [random(1, 10).to(int).value() for _ in range(4)]
random_tenosr = np.random.randint(-99999, 99999, size=random_shape)
x = torch.tensor(random_tenosr).to(device)
y = torch.full(x.shape, 0.5).to(device)
y += x
y = y.requires_grad_()
z = torch.round(y)
return z


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