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] replace assert np.allclose with np.testing.assert_allclose and assert np.array_equal with np.testing.assert_array_equal #55385

Merged
merged 27 commits into from
Aug 1, 2023

Conversation

zrr1999
Copy link
Member

@zrr1999 zrr1999 commented Jul 12, 2023

PR types

Others

PR changes

Others

Description

#55348
替换了下面四种写法

  • assert np.allclose(...)
  • assert np.isclose(...).all()
  • assert np.array_equal(...)
  • assert not np.array_equal(...)

np.testing.assert_array_compare(operator.__ne__, $$$A) 这种写法参考了stackoverflow

@paddle-bot
Copy link

paddle-bot bot commented Jul 12, 2023

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot paddle-bot bot added contributor External developers status: proposed labels Jul 12, 2023
Copy link
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

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

image

这次挂掉的单测还挺多的,可以这个 PR 把相关单测 revert 掉,尽快合入,然后在新的 PR 慢慢解决剩下的几个单测

根据经验(#44988#45213),应该有 shape 不对齐、精度误差等问题,可参考之前的 PR 中的方案来解决~

mask_tensor_remote.numpy(), mask_tensor_local.numpy()
)
else:
assert not np.array_equal(
import operator
Copy link
Member

Choose a reason for hiding this comment

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

import operator 放在文件开始吧

Copy link
Member Author

Choose a reason for hiding this comment

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

好的,已修改,这个其实


assert np.allclose(res_3, self.res_3_np)
np.testing.assert_allclose(res_3, self.res_3_np)

# assert np.allclose(res_4, self.res_4_np)
Copy link
Member

Choose a reason for hiding this comment

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

这种注释里的可以简单拿正则搜一下,手动替换一下~

Copy link
Member Author

Choose a reason for hiding this comment

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

好的,已修复

@SigureMo SigureMo changed the title [CodeStyle][CINN] replace assert np.allclose with np.testing.assert_allclose and assert np.array_equal with np.testing.assert_array_equal [CodeStyle] replace assert np.allclose with np.testing.assert_allclose and assert np.array_equal with np.testing.assert_array_equal Jul 13, 2023
@SigureMo
Copy link
Member

另外有一个个人的建议

https://github.com/PaddlePaddle/Paddle/blob/4c5ce835d62eb156a74eb80f851034cb8624d2c0/test/dygraph_to_static/test_cycle_gan.py#L697C1-L711

希望把这里的 assert_func 换成 np.testing 下的函数~可以在下一个 PR 一起做,因为目前 PaddleSOT 发现部分情况该函数会出现精度问题,因此在 https://github.com/PaddlePaddle/PaddleSOT/blob/be49db8f688c455794204241d3a5c45f698ab6c3/tests/run_all_paddle_ci.sh#L19 暂时禁掉了,但从 log 上只知道 False is not true,具体差异多少根本看不到,希望这里也改一下~

tips: 如果需要传递 atol 或者 rtol 参数的话,可以用 functools.partial

@zrr1999
Copy link
Member Author

zrr1999 commented Jul 13, 2023

另外有一个个人的建议

https://github.com/PaddlePaddle/Paddle/blob/4c5ce835d62eb156a74eb80f851034cb8624d2c0/test/dygraph_to_static/test_cycle_gan.py#L697C1-L711

希望把这里的 assert_func 换成 np.testing 下的函数~可以在下一个 PR 一起做,因为目前 PaddleSOT 发现部分情况该函数会出现精度问题,因此在 https://github.com/PaddlePaddle/PaddleSOT/blob/be49db8f688c455794204241d3a5c45f698ab6c3/tests/run_all_paddle_ci.sh#L19 暂时禁掉了,但从 log 上只知道 False is not true,具体差异多少根本看不到,希望这里也改一下~

tips: 如果需要传递 atol 或者 rtol 参数的话,可以用 functools.partial

好的,我改一下

@zrr1999
Copy link
Member Author

zrr1999 commented Jul 13, 2023

PR types

Others

PR changes

Others

Description

#55348 替换了下面四种写法

  • assert np.allclose(...)
  • assert np.isclose(...).all()
  • assert np.array_equal(...)
  • assert not np.array_equal(...)

np.testing.assert_array_compare(operator.__ne__, $$$A) 这种写法参考了stackoverflow

这个 assert not np.array_equal(...)我给revert了,因为stackoverflow 提到的这个方法是解决 所有值均不同,这里应该是存在值不同,所以可能要后面再研究下,或者其实没必要修稿

@SigureMo
Copy link
Member

这个 assert not np.array_equal(...)我给revert了,因为stackoverflow 提到的这个方法是解决 所有值均不同,这里应该是存在值不同,所以可能要后面再研究下,或者其实没必要修稿

嗯嗯,好的

assert np.allclose(res_2, np.power(input, 3))
assert np.allclose(res_6, np.power(input, 3))
np.testing.assert_allclose(
res_1, np.power(input, 2), rtol=1e-6, atol=1e-6
Copy link
Member

Choose a reason for hiding this comment

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

这里 atol 使用 1e-6 的原因是?

#44988 对比过两者的默认值

  • np.allclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False)
  • np.testing.assert_allclose(actual, desired, rtol=1e-07, atol=0, equal_nan=True, err_msg='', verbose=True)

因此理论上 rtol=1e-05, atol=1e-08 所有单测都能通过的,因此原来默认值的情况下建议 rtol 不要超过 1e-5atol 不要超过 1e-8,一种非常简单的方式是将所有出现精度问题的单测设置成这俩默认值即可,应该都能过

Copy link
Member Author

@zrr1999 zrr1999 Jul 13, 2023

Choose a reason for hiding this comment

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

image image 因为这个当时看到很多都是1e-7+,然后就为方便起见全都统一成了1e-6的阈值了,这部分我也不知道为什么换成np.testing.assert_allclose会导致精度出现不同,看起来或许这个是一个概率问题,大部分情况只会略微比1e-7大一点

Copy link
Member

Choose a reason for hiding this comment

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

这部分我也不知道为什么换成np.testing.assert_allclose会导致精度出现不同,看起来或许这个是一个概率问题

不是的呀,我们分析一下哈:

原代码 assert np.allclose(res_2, np.power(input, 3)) 没有指定 rtolatol,所以它等价于 assert np.allclose(res_2, np.power(input, 3), rtol=1e-05, atol=1e-08),而直接转写后的代码 np.testing.assert_allclose(res_1, np.power(input, 2)) 同样没有默认值,等价于 np.testing.assert_allclose(res_1, np.power(input, 2), rtol=1e-07, atol=0),同样没有设置默认值的情况下,后者明显是比前者更「严格」的,因此会出现部分单测过不去的问题,这些单测的精度应该在 rtol: 1e-07~1e-05, atol: 0~1e-08 之间

因此为了能达到同样的效果,最便捷的方式是改成 np.testing.assert_allclose(res_1, np.power(input, 2), rtol=1e-05, atol=1e-08)

Copy link
Member Author

Choose a reason for hiding this comment

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

有道理,我已经全都改过来啦

@zrr1999
Copy link
Member Author

zrr1999 commented Jul 14, 2023

@SigureMo 现在应该ok了,剩下的那个应该还是之前那个奇怪的问题

@SigureMo
Copy link
Member

@SigureMo 现在应该ok了,剩下的那个应该还是之前那个奇怪的问题

revert 吧 😂

Copy link
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

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

其余 LGTM~

注意一下 https://github.com/PaddlePaddle/community/blob/master/pfcc/call-for-contributions/code_style/code_style_improvement_for_unittest.md#%E9%81%97%E7%95%99%E9%97%AE%E9%A2%98 提到的遗留问题,如果这几个能确定修改的是对的的话是没问题的,如果不确定的话 revert 就好了~

test/collective/fleet/test_mixed_precision.py Outdated Show resolved Hide resolved
test/dygraph_to_static/test_cinn_prim.py Outdated Show resolved Hide resolved
test/dygraph_to_static/test_cycle_gan.py Outdated Show resolved Hide resolved
test/legacy_test/test_egr_python_api.py Outdated Show resolved Hide resolved
test/legacy_test/test_egr_python_api.py Outdated Show resolved Hide resolved
test/legacy_test/test_layers.py Outdated Show resolved Hide resolved
test/legacy_test/test_layers.py Show resolved Hide resolved
test/legacy_test/test_nan_to_num_op.py Show resolved Hide resolved
test/legacy_test/test_pixel_shuffle_op.py Outdated Show resolved Hide resolved
test/legacy_test/test_prune.py Outdated Show resolved Hide resolved
@SigureMo SigureMo self-assigned this Jul 18, 2023
@zrr1999 zrr1999 requested a review from SigureMo July 23, 2023 05:53

assert not np.array_equal(moment1_, pre_moment1_)

assert not np.array_equal(beta_pow1_, pre_beta_pow1_)
Copy link
Member

Choose a reason for hiding this comment

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

这个文件看起来还没改回去?

Copy link
Member

Choose a reason for hiding this comment

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

另外,针对某几个文件,可以尝试 git checkout develop -- file1 file2 直接 revert

Copy link
Member Author

Choose a reason for hiding this comment

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

已修改

np.array_equal(linear_origin, linear.weight.numpy())
)

assert not np.array_equal(linear_origin, linear.weight.numpy())
Copy link
Member

Choose a reason for hiding this comment

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

这里貌似也?

np.array_equal(conv2d1_weight_np, conv2d2.weight.numpy())
)

assert not np.array_equal(conv2d1_weight_np, conv2d2.weight.numpy())
Copy link
Member

Choose a reason for hiding this comment

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

这个文件也有多处

Copy link
Member Author

Choose a reason for hiding this comment

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

已修改

@@ -610,7 +612,8 @@ def test_prune_with_multi_optimizers(self):
)

np.testing.assert_array_equal(weight_with_prune, weight_expected)
self.assertFalse(np.array_equal(weight_without_prune, weight_expected))

assert not np.array_equal(weight_without_prune, weight_expected)
Copy link
Member

Choose a reason for hiding this comment

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

这个文件也

@@ -272,9 +272,12 @@ def test_static_graph_layer(self):
fetch_list=out_2,
use_prune=True,
)
# exe.run output maybe squeeze.
res_1 = res_1[0] if len(res_1) == 1 else res_1
res_2 = res_2[0] if len(res_2) == 1 else res_2
Copy link
Member

Choose a reason for hiding this comment

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

我看很多文件有这样的修改,maybe squeeze 具体是指?

Copy link
Member Author

Choose a reason for hiding this comment

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

这块是因为很多与res_1 对应的变量,例如 out_1_np,当第一个维度为1的时候都进行了类似squeeze的操作,所以这里我任务可能需要修改一些 exe.run 的代码,使其会自动进行squeeze操作,从而不需要在测试用例中修改内容。

Copy link
Member

Choose a reason for hiding this comment

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

话说这里的问题会不会是 exe.run 用法错了呢?

https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/static/Executor_cn.html#run-program-none-feed-none-fetch-list-none-feed-var-name-feed-fetch-var-name-fetch-scope-none-return-numpy-true-use-program-cache-false-use-prune-false

应该是

a = data('a')
b = data('b')
c = a + b
d = c * a

c_data, d_data = exe.run(
    feed={'a': a, 'b': b},
    fetch_list=[c.name, d.name]
)

但这里直接将一个结果(比如 265 行的 out_1)当成 fetch_list 是什么鬼呀,因为这个才导致维度啥的出现问题吧

Copy link
Member

Choose a reason for hiding this comment

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

啊,好像还真就支持

is_fetch_var = lambda var: isinstance(var, (Variable, str))
is_tuple_list = lambda var: isinstance(var, (tuple, list))
if fetch_list is None:
return []
if is_fetch_var(fetch_list):
return [fetch_list]

@paddle-ci-bot
Copy link

paddle-ci-bot bot commented Jul 30, 2023

Sorry to inform you that 6aa1e32's CIs have passed for more than 7 days. To prevent PR conflicts, you need to re-run all CIs manually.

@@ -272,9 +272,11 @@ def test_static_graph_layer(self):
fetch_list=out_2,
use_prune=True,
)
res_1 = res_1[0]
Copy link
Member

Choose a reason for hiding this comment

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

res_1[0] 直接写在下面或者上面吧,这样看着很奇怪

Copy link
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

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

还有这四处~

test/legacy_test/test_group_norm_op_v2.py Outdated Show resolved Hide resolved
test/legacy_test/test_group_norm_op_v2.py Outdated Show resolved Hide resolved
test/legacy_test/test_number_count_op.py Outdated Show resolved Hide resolved
@@ -106,7 +106,8 @@ def test_static_mode(self):
feed={"x": np_x, "mask": np_mask},
fetch_list=[out],
)
self.assertEqual(np.allclose(res, np_out), True)
res = res[0] if len(res) == 1 else res
Copy link
Member

Choose a reason for hiding this comment

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

4

Copy link
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

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

LGTM~~~

@luotao1 luotao1 merged commit 744e1ea into PaddlePaddle:develop Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers
Projects
None yet
3 participants