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

[CodeStyle][py2] remove compat module (round) #46923

Merged
merged 2 commits into from
Oct 13, 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
28 changes: 0 additions & 28 deletions python/paddle/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.

import six
import math

__all__ = []

Expand Down Expand Up @@ -197,30 +196,3 @@ def _to_bytes(obj, encoding):
return obj
else:
return six.b(obj)


# math related functions
def round(x, d=0):
"""
Compatible round which act the same behaviour in Python3.

Args:
x(float) : The number to round halfway.

Returns:
round result of x
"""
if six.PY3:
# The official walkaround of round in Python3 is incorrect
# we implement according this answer: https://www.techforgeek.info/round_python.html
if x > 0.0:
p = 10**d
return float(math.floor((x * p) + math.copysign(0.5, x))) / p
elif x < 0.0:
p = 10**d
return float(math.ceil((x * p) + math.copysign(0.5, x))) / p
else:
return math.copysign(0.0, x)
else:
import __builtin__
return __builtin__.round(x, d)
12 changes: 0 additions & 12 deletions python/paddle/fluid/tests/unittests/test_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,18 +236,6 @@ def test_to_bytes(self):
for i in l2:
self.assertTrue(isinstance(i, bytes))

def test_round(self):
self.assertEqual(3.0, cpt.round(3.4))
self.assertEqual(4.0, cpt.round(3.5))
self.assertEqual(0.0, cpt.round(0.1))
self.assertEqual(0.0, cpt.round(0.0))
self.assertEqual(-0.0, cpt.round(-0.0))
self.assertEqual(-0.0, cpt.round(-0.1))
self.assertEqual(-3.0, cpt.round(-3.4))
self.assertEqual(-4.0, cpt.round(-3.5))
self.assertEqual(5.0, cpt.round(5))
self.assertRaises(TypeError, cpt.round, None)


if __name__ == "__main__":
unittest.main()
20 changes: 15 additions & 5 deletions python/paddle/fluid/tests/unittests/test_roi_pool_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,20 @@
import numpy as np
import math
import sys
import paddle.compat as cpt
from op_test import OpTest
import paddle.fluid as fluid

from decimal import Decimal, ROUND_HALF_UP


def _round(x):
"""In Python3 round function rounds to the nearest even number,
we use this function to make the result always round up when the
remainder is 0.5. See more at:
https://stackoverflow.com/questions/33019698/how-to-properly-round-up-half-float-numbers
"""
return Decimal(x).to_integral_value(rounding=ROUND_HALF_UP)


class TestROIPoolOp(OpTest):

Expand Down Expand Up @@ -67,10 +77,10 @@ def calc_roi_pool(self):
for i in range(self.rois_num):
roi = self.rois[i]
roi_batch_id = int(roi[0])
roi_start_w = int(cpt.round(roi[1] * self.spatial_scale))
roi_start_h = int(cpt.round(roi[2] * self.spatial_scale))
roi_end_w = int(cpt.round(roi[3] * self.spatial_scale))
roi_end_h = int(cpt.round(roi[4] * self.spatial_scale))
roi_start_w = int(_round(roi[1] * self.spatial_scale))
roi_start_h = int(_round(roi[2] * self.spatial_scale))
roi_end_w = int(_round(roi[3] * self.spatial_scale))
roi_end_h = int(_round(roi[4] * self.spatial_scale))

roi_height = int(max(roi_end_h - roi_start_h + 1, 1))
roi_width = int(max(roi_end_w - roi_start_w + 1, 1))
Expand Down