-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
Conversation
你的PR提交成功,感谢你对开源项目的贡献! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mask_tensor_remote.numpy(), mask_tensor_local.numpy() | ||
) | ||
else: | ||
assert not np.array_equal( | ||
import operator |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import operator 放在文件开始吧
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这种注释里的可以简单拿正则搜一下,手动替换一下~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好的,已修复
assert np.allclose
with np.testing.assert_allclose
and assert np.array_equal
with np.testing.assert_array_equal
assert np.allclose
with np.testing.assert_allclose
and assert np.array_equal
with np.testing.assert_array_equal
另外有一个个人的建议 希望把这里的 tips: 如果需要传递 |
好的,我改一下 |
这个 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 |
There was a problem hiding this comment.
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-5
,atol
不要超过 1e-8
,一种非常简单的方式是将所有出现精度问题的单测设置成这俩默认值即可,应该都能过
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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))
没有指定 rtol
和 atol
,所以它等价于 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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
有道理,我已经全都改过来啦
@SigureMo 现在应该ok了,剩下的那个应该还是之前那个奇怪的问题 |
revert 吧 😂 |
There was a problem hiding this 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 就好了~
|
||
assert not np.array_equal(moment1_, pre_moment1_) | ||
|
||
assert not np.array_equal(beta_pow1_, pre_beta_pow1_) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个文件看起来还没改回去?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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()) |
There was a problem hiding this 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_layers.py
Outdated
np.array_equal(conv2d1_weight_np, conv2d2.weight.numpy()) | ||
) | ||
|
||
assert not np.array_equal(conv2d1_weight_np, conv2d2.weight.numpy()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个文件也有多处
There was a problem hiding this 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_prune.py
Outdated
@@ -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) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我看很多文件有这样的修改,maybe squeeze
具体是指?
There was a problem hiding this comment.
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操作,从而不需要在测试用例中修改内容。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
话说这里的问题会不会是 exe.run 用法错了呢?
应该是
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
是什么鬼呀,因为这个才导致维度啥的出现问题吧
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
啊,好像还真就支持
Paddle/python/paddle/fluid/executor.py
Lines 1709 to 1715 in dc96ebc
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] |
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] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
res_1[0] 直接写在下面或者上面吧,这样看着很奇怪
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
还有这四处~
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM~~~
PR types
Others
PR changes
Others
Description
#55348
替换了下面四种写法
np.testing.assert_array_compare(operator.__ne__, $$$A)
这种写法参考了stackoverflow