Skip to content
Closed
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
13 changes: 8 additions & 5 deletions python/paddle/fluid/tests/unittests/mkldnn/mkldnn_op_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
import paddle.fluid as fluid


def __assert_close(test_case, tensor, np_array, msg, atol=1e-4):
test_case.assertTrue(np.allclose(np.array(tensor), np_array, atol=atol),
msg)
def __assert_close(tensor, np_array, msg, atol=1e-4, rtol=1e-5):
np.testing.assert_allclose(np.array(tensor),
np_array,
atol=atol,
rtol=rtol,
err_msg=msg)


def check_if_mkldnn_primitives_exist_in_bwd(test_case, op_type, x, out,
Expand Down Expand Up @@ -69,7 +72,7 @@ def check_if_mkldnn_primitives_exist_in_bwd(test_case, op_type, x, out,
for name in ['x', 'out@GRAD']},
fetch_list=['x@GRAD', 'out'])

__assert_close(test_case, x_grad, out[0], 'x@GRAD')
__assert_close(x_grad, out[0], 'x@GRAD')


def check_if_mkldnn_batchnorm_primitives_exist_in_bwd(test_case, var_dict,
Expand Down Expand Up @@ -144,7 +147,7 @@ def check_if_mkldnn_batchnorm_primitives_exist_in_bwd(test_case, var_dict,
},
fetch_list=test_case.fetch_list)
for id, name in enumerate(test_case.fetch_list):
__assert_close(test_case, var_dict[name], out[id], name)
__assert_close(var_dict[name], out[id], name)

print("MKLDNN op test forward passed: ", str(place), data_layout)

Expand Down
33 changes: 16 additions & 17 deletions python/paddle/fluid/tests/unittests/op_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1438,14 +1438,14 @@ def find_actual_value(self, name):
raise NotImplementedError("base class, not implement!")

def _compare_numpy(self, name, actual_np, expect_np):
self.op_test.assertTrue(
np.allclose(
actual_np,
expect_np,
atol=atol,
rtol=self.rtol if hasattr(self, 'rtol') else 1e-5,
equal_nan=equal_nan), "Output (" + name +
") has diff at " + str(place) + " in " + self.checker_name)
np.testing.assert_allclose(
actual_np,
expect_np,
atol=atol,
rtol=self.rtol if hasattr(self, 'rtol') else 1e-5,
equal_nan=equal_nan,
err_msg="Output (" + name + ") has diff at " + str(place) +
" in " + self.checker_name)

def _compare_list(self, name, actual, expect):
""" if expect is a tuple, we need to compare list.
Expand Down Expand Up @@ -1584,15 +1584,14 @@ def _compare_numpy(self, name, actual_np, expect_np):
1) == 0:
pass
else:
self.op_test.assertTrue(
np.allclose(
actual_np,
expect_np,
atol=atol,
rtol=self.rtol if hasattr(self, 'rtol') else 1e-5,
equal_nan=equal_nan),
"Output (" + name + ") has diff at " + str(place) +
" in " + self.checker_name)
np.testing.assert_allclose(
actual_np,
expect_np,
atol=atol,
rtol=self.rtol if hasattr(self, 'rtol') else 1e-5,
equal_nan=equal_nan,
err_msg="Output (" + name + ") has diff at " +
str(place) + " in " + self.checker_name)

class EagerChecker(DygraphChecker):

Expand Down
12 changes: 5 additions & 7 deletions python/paddle/fluid/tests/unittests/test_isclose_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,11 @@ def setUp(self):
self.attrs = {'equal_nan': self.equal_nan}
self.outputs = {
'Out':
np.array([
np.isclose(self.inputs['Input'],
self.inputs['Other'],
rtol=self.rtol,
atol=self.atol,
equal_nan=self.equal_nan)
])
np.isclose(self.inputs['Input'],
self.inputs['Other'],
rtol=self.rtol,
atol=self.atol,
equal_nan=self.equal_nan)
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

2022-10-01 17:28:28 ======================================================================
2022-10-01 17:28:28 FAIL: test_check_output (test_isclose_op.TestIscloseOpFloat64)
2022-10-01 17:28:28 ----------------------------------------------------------------------
2022-10-01 17:28:28 Traceback (most recent call last):
2022-10-01 17:28:28   File "/workspace/Paddle/build/python/paddle/fluid/tests/unittests/test_isclose_op.py", line 228, in test_check_output
2022-10-01 17:28:28     self.check_output(check_eager=True)
2022-10-01 17:28:28   File "/workspace/Paddle/build/python/op_test.py", line 1799, in check_output
2022-10-01 17:28:28     check_eager=check_eager)
2022-10-01 17:28:28   File "/workspace/Paddle/build/python/op_test.py", line 1664, in check_output_with_place
2022-10-01 17:28:28     static_checker.check()
2022-10-01 17:28:28   File "/workspace/Paddle/build/python/op_test.py", line 1494, in check
2022-10-01 17:28:28     self.compare_outputs_with_expects()
2022-10-01 17:28:28   File "/workspace/Paddle/build/python/op_test.py", line 1484, in compare_outputs_with_expects
2022-10-01 17:28:28     self.compare_single_output_with_expect(out_name, expect)
2022-10-01 17:28:28   File "/workspace/Paddle/build/python/op_test.py", line 1465, in compare_single_output_with_expect
2022-10-01 17:28:28     self._compare_numpy(name, actual_np, expect_np)
2022-10-01 17:28:28   File "/workspace/Paddle/build/python/op_test.py", line 1448, in _compare_numpy
2022-10-01 17:28:28     " in " + self.checker_name)
2022-10-01 17:28:28   File "/opt/_internal/cpython-3.7.0/lib/python3.7/site-packages/numpy/testing/_private/utils.py", line 1531, in assert_allclose
2022-10-01 17:28:28     verbose=verbose, header=header, equal_nan=equal_nan)
2022-10-01 17:28:28   File "/opt/_internal/cpython-3.7.0/lib/python3.7/site-packages/numpy/testing/_private/utils.py", line 763, in assert_array_compare
2022-10-01 17:28:28     raise AssertionError(msg)
2022-10-01 17:28:28 AssertionError: 
2022-10-01 17:28:28 Not equal to tolerance rtol=1e-05, atol=0
2022-10-01 17:28:28 Output (Out) has diff at Place(cpu) in static checker
2022-10-01 17:28:28 (shapes (1,), (1, 1) mismatch)
2022-10-01 17:28:28  x: array([ True])
2022-10-01 17:28:28  y: array([[ True]])
2022-10-01 17:28:28 

所有的 expect 都比 actual 多一个维度,因此删掉外层 np.array([...])


def test_check_output(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,8 @@ def test_static(self):
fetch_list=[results, results_fp64])
np_res = res_func(np_input_data, q=0.5, axis=1)
np_res_fp64 = res_func(np_input_data_fp64, q=0.5, axis=1)
self.assertTrue(
np.allclose(paddle_res, np_res)
and np.allclose(paddle_res_fp64, np_res_fp64))
np.testing.assert_allclose(paddle_res, np_res)
np.testing.assert_allclose(paddle_res_fp64, np_res_fp64)


if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions python/paddle/fluid/tests/unittests/test_reduce_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ def setUp(self):
'X': np.random.randint(0, 2,
(2, 5, 3, 2, 2, 3, 4, 2)).astype("bool")
}
self.attrs = {'reduce_all': True, 'dim': (2, 3, 4)}
self.attrs = {'dim': (2, 3, 4)}

@SigureMo SigureMo Oct 4, 2022 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

2022-10-01 17:31:33 ======================================================================
2022-10-01 17:31:33 FAIL: test_check_output (test_reduce_op.TestAll8DOp)
2022-10-01 17:31:33 ----------------------------------------------------------------------
2022-10-01 17:31:33 Traceback (most recent call last):
2022-10-01 17:31:33   File "/workspace/Paddle/build/python/paddle/fluid/tests/unittests/test_reduce_op.py", line 365, in test_check_output
2022-10-01 17:31:33     self.check_output(check_eager=True)
2022-10-01 17:31:33   File "/workspace/Paddle/build/python/op_test.py", line 1799, in check_output
2022-10-01 17:31:33     check_eager=check_eager)
2022-10-01 17:31:33   File "/workspace/Paddle/build/python/op_test.py", line 1664, in check_output_with_place
2022-10-01 17:31:33     static_checker.check()
2022-10-01 17:31:33   File "/workspace/Paddle/build/python/op_test.py", line 1494, in check
2022-10-01 17:31:33     self.compare_outputs_with_expects()
2022-10-01 17:31:33   File "/workspace/Paddle/build/python/op_test.py", line 1484, in compare_outputs_with_expects
2022-10-01 17:31:33     self.compare_single_output_with_expect(out_name, expect)
2022-10-01 17:31:33   File "/workspace/Paddle/build/python/op_test.py", line 1465, in compare_single_output_with_expect
2022-10-01 17:31:33     self._compare_numpy(name, actual_np, expect_np)
2022-10-01 17:31:33   File "/workspace/Paddle/build/python/op_test.py", line 1448, in _compare_numpy
2022-10-01 17:31:33     " in " + self.checker_name)
2022-10-01 17:31:33   File "/opt/_internal/cpython-3.7.0/lib/python3.7/site-packages/numpy/testing/_private/utils.py", line 1531, in assert_allclose
2022-10-01 17:31:33     verbose=verbose, header=header, equal_nan=equal_nan)
2022-10-01 17:31:33   File "/opt/_internal/cpython-3.7.0/lib/python3.7/site-packages/numpy/testing/_private/utils.py", line 763, in assert_array_compare
2022-10-01 17:31:33     raise AssertionError(msg)
2022-10-01 17:31:33 AssertionError: 
2022-10-01 17:31:33 Not equal to tolerance rtol=1e-05, atol=1e-05
2022-10-01 17:31:33 Output (Out) has diff at Place(cpu) in static checker
2022-10-01 17:31:33 (shapes (1,), (2, 5, 3, 4, 2) mismatch)
2022-10-01 17:31:33  x: array([False])
2022-10-01 17:31:33  y: array([[[[[False, False],
2022-10-01 17:31:33           [False, False],
2022-10-01 17:31:33           [False, False],...

reduce_all 会使维度为 [1]

std::vector<int64_t> out_dim_vector;
if (keep_dim) {
for (int64_t i = 0; i < x.dims().size(); ++i) {
if (reduce_all || dims_set.find(i) != dims_set.end()) {
out_dim_vector.push_back(1);
} else {
out_dim_vector.push_back(x.dims().at(i));
}
}
} else {
for (int64_t i = 0; i < x.dims().size(); ++i) {
if (reduce_all || dims_set.find(i) != dims_set.end()) {
continue;
} else {
out_dim_vector.push_back(x.dims().at(i));
}
}
if (out_dim_vector.size() == 0) {
out_dim_vector.push_back(1);
}
}

因此这里需要移除 reduce_all 参数(或者设为默认值 False)才能与 NumPy 计算的 expect 一致

self.outputs = {'Out': self.inputs['X'].all(axis=self.attrs['dim'])}

def test_check_output(self):
Expand Down Expand Up @@ -464,7 +464,7 @@ def setUp(self):
'X': np.random.randint(0, 2,
(2, 5, 3, 2, 2, 3, 4, 2)).astype("bool")
}
self.attrs = {'reduce_all': True, 'dim': (3, 5, 4)}
self.attrs = {'dim': (3, 5, 4)}
self.outputs = {'Out': self.inputs['X'].any(axis=self.attrs['dim'])}

def test_check_output(self):
Expand Down