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] Add CI for self.assertTrue(np.allclose(...)) #45126

Merged
merged 15 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from 13 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
103 changes: 103 additions & 0 deletions python/paddle/fluid/tests/unittests/CI_test/test_dropout_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
'''
self.assertTrue(np.allclose(x_out, x_in_np))


self.assertTrue(np.allclose(fetches[0], res_np))
self.assertTrue(np.allclose(fetches2[0], res_np2))


self.assertTrue(np.allclose(res.numpy(), res_np))
self.assertTrue(np.allclose(res10.numpy(), res_np2))


self.assertTrue(np.allclose(result.numpy(), result_np))


self.assertTrue(np.allclose(fetches[0], res_np))


self.assertTrue(np.allclose(res.numpy(), res_np))


self.assertTrue(np.allclose(result.numpy(), result_np))


self.assertTrue(np.allclose(fetches[0], res_np))


self.assertTrue(np.allclose(res.numpy(), res_np))


self.assertTrue(np.allclose(result.numpy(), result_np))


self.assertTrue(np.allclose(fetches[0], res_np))


self.assertTrue(np.allclose(fetches[0], res_np3))


self.assertTrue(np.allclose(res.numpy(), res_np))
self.assertTrue(np.allclose(res3.numpy(), res_np3))


self.assertTrue(np.allclose(result.numpy(), result_np))



self.assertTrue(np.allclose(out1, out2))



self.assertTrue(
np.allclose(input.gradient(),
self.cal_grad_upscale_train(mask.numpy(),
prob)))


self.assertTrue(
np.allclose(
input.gradient(),
self.cal_grad_upscale_train(mask.numpy(), prob)))


self.assertTrue(
np.allclose(input.gradient(),
self.cal_grad_upscale_train(mask.numpy(),
prob)))


self.assertTrue(
np.allclose(
input.gradient(),
self.cal_grad_upscale_train(mask.numpy(), prob)))


self.assertTrue(np.array_equal(static_res, dygraph_res))


self.assertEqual(np.sum(index0), 390094540)
self.assertEqual(np.sum(index1), 12871475125)
self.assertEqual(np.sum(index2), 12872777397)
self.assertEqual(np.sum(out), 16778744.0)


self.assertTrue(np.allclose(out[10, 100, 500:510], expect))


self.assertEqual(np.sum(index0), 260065137)
self.assertEqual(np.sum(index1), 8582636095)
self.assertEqual(np.sum(index2), 8582219962)
self.assertEqual(np.sum(out), 16778396.563660286)


self.assertTrue(np.allclose(out[20, 100, 500:510], expect))


self.assertEqual(np.sum(index0), 130086900)
self.assertEqual(np.sum(index1), 4291190105)
self.assertEqual(np.sum(index2), 4292243807)


self.assertTrue(np.allclose(out[0, 100, 500:510], expect))
'''
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'''
self.assertEqual((100, 100), res.shape)
self.assertTrue(numpy.allclose(res, numpy.dot(a_np, b_np)))
self.assertTrue(numpy.allclose(res_array[0], a_np))
self.assertTrue(numpy.allclose(res_array[1], b_np))
self.assertTrue(numpy.allclose(res_array[2], res))
'''
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'''
self.assertTrue(
np.allclose(expect_res.numpy(),
actual_res.to_dense().numpy(),
equal_nan=True))


self.assertTrue(
np.allclose(dense_x.grad.numpy(),
csr_x.grad.to_dense().numpy(),
equal_nan=True))
self.assertTrue(
np.allclose(dense_y.grad.numpy(),
csr_y.grad.to_dense().numpy(),
equal_nan=True))


self.assertTrue(
np.allclose(expect_res.numpy(),
actual_res.to_dense().numpy(),
equal_nan=True))
self.assertTrue(
np.allclose(dense_x.grad.numpy(),
coo_x.grad.to_dense().numpy(),
equal_nan=True))
self.assertTrue(
np.allclose(dense_y.grad.numpy(),
coo_y.grad.to_dense().numpy(),
equal_nan=True))
'''
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'''
self.assertEqual(np.allclose(out, out_ref, rtol=1e-03), True)


self.assertEqual(np.allclose(out.numpy(), out_ref, rtol=1e-03), True)
'''
24 changes: 24 additions & 0 deletions tools/check_file_diff_approvals.sh
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,30 @@ if [ "${EMPTY_GRAD_OP_REGISTERED}" != "" ] && [ "${GIT_PT_ID}" != "" ]; then
check_approval 1 43953930 46782768 22165420 22361972
fi

ALL_ADDED_LINES_IN_TARGET_PATH=`git diff -U0 upstream/$BRANCH -- 'python/paddle/tests/' 'python/paddle/fluid/contrib/' 'python/paddle/fluid/tests/custom_op/' 'python/paddle/fluid/tests/unittests/' 'python/paddle/fluid/tests/*.py' | grep "^+" || true`
Yulv-git marked this conversation as resolved.
Show resolved Hide resolved
INVALID_ASSERT_CHECK=`echo "$ALL_ADDED_LINES_IN_TARGET_PATH" | grep -E "self\.assert(True|Equal)\(\s*(np|numpy)\.(allclose|array_equal)" || true`
if [ "${INVALID_ASSERT_CHECK}" != "" ] && [ "${GIT_PR_ID}" != "" ]; then
echo_line="It is recommended to use 'np.testing.assert_allclose' instead of 'self\.assert(True|Equal)\((np|numpy)\.(allclose|array_equal)'.\nPlease modify the code below. If anything is unclear, please read the specification [ https://github.com/PaddlePaddle/community/blob/master/rfcs/CodeStyle/20220805_code_style_improvement_for_unittest.md#background ]. If it is a mismatch, please request qili93 (Recommend) or luotao1 review and approve.\nThe code that do not meet the specification are as follows:\n${INVALID_ASSERT_CHECK}\n"
check_approval 1 16605440 6836917
fi
INVALID_ASSERT_CHECK=`echo "$ALL_ADDED_LINES_IN_TARGET_PATH" | grep -zoE "self\.assert(True|Equal)\(\s\+\s*(np|numpy)\.(allclose|array_equal)" || true`
if [ "${INVALID_ASSERT_CHECK}" != "" ] && [ "${GIT_PR_ID}" != "" ]; then
echo_line="It is recommended to use 'np.testing.assert_allclose' instead of 'self\.assert(True|Equal)\(\n(np|numpy)\.(allclose|array_equal)'.\nPlease modify the code below. If anything is unclear, please read the specification [ https://github.com/PaddlePaddle/community/blob/master/rfcs/CodeStyle/20220805_code_style_improvement_for_unittest.md#background ]. If it is a mismatch, please request qili93 (Recommend) or luotao1 review and approve.\nThe code that do not meet the specification are as follows:\n${INVALID_ASSERT_CHECK}\n"
check_approval 1 16605440 6836917
fi
Yulv-git marked this conversation as resolved.
Show resolved Hide resolved

# Test start
UNITTESTS_DIRS=('python/paddle/tests/'
Yulv-git marked this conversation as resolved.
Show resolved Hide resolved
'python/paddle/fluid/contrib/'
'python/paddle/fluid/tests/')
ALL_ADDED_LINES_IN_UNITTESTS_DIRS=`git diff -U0 upstream/$BRANCH -- ${UNITTESTS_DIRS[*]} | grep "^+" || true`
INVALID_UNITTEST_ASSERT_CHECK=`echo "$ALL_ADDED_LINES_IN_UNITTESTS_DIRS" | grep -zoE '\+\s+self\.assert(True|Equal)\((\s*\+\s*)?(np|numpy)\.(allclose|array_equal)[^+]*' || true`
if [ "${INVALID_UNITTEST_ASSERT_CHECK}" != "" ] && [ "${GIT_PR_ID}" != "" ]; then
echo_line="It is recommended to use 'np.testing.assert_allclose' and 'np.testing.array_equal' instead of 'self.assertTrue(np.allclose(...))' and 'self.assertTrue(np.array_equal(...))'.\nPlease modify the code below. If anything is unclear, please read the specification [ https://github.com/PaddlePaddle/community/blob/master/rfcs/CodeStyle/20220805_code_style_improvement_for_unittest.md#background ]. If it is a mismatch, please request qili93 (Recommend) or luotao1 review and approve.\nThe code that do not meet the specification are as follows:\n${INVALID_UNITTEST_ASSERT_CHECK}\n"
check_approval 1 16605440 6836917
fi
# Test end

HAS_MODIFIED_PHI_FILES=`git diff --name-only upstream/$BRANCH | grep "paddle/phi/" || true`
PHI_INCLUDE_FLUID_FILES=""
for CHANGE_FILE in ${HAS_MODIFIED_PHI_FILES}; do
Expand Down