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] 单测报错信息优化 RFC #198

Merged

Conversation

SigureMo
Copy link
Member

@SigureMo SigureMo commented Aug 7, 2022

本 RFC 对 PaddlePaddle/Paddle#44641 中提到的问题(现有单测报错信息并不友好的问题)的解决方案进行了调研与初步尝试,并进一步对具体实现方案和分工进行了明确。

@paddle-bot
Copy link

paddle-bot bot commented Aug 7, 2022

你的PR提交成功,感谢你对开源项目的贡献!
请检查PR提交格式和内容是否完备,具体请参考示例模版
Your PR has been submitted. Thanks for your contribution!
Please check its format and content. For this, you can refer to Template and Demo.

@luotao1 luotao1 self-assigned this Aug 8, 2022

对于现有代码来说 `self.assertTrue(np.allclose(...))` 是一个非常简单的模式,其前缀 `self.assertTrue(np.allclose(` 是完全可以通过正则甚至简单的文本搜索来搜索到,但如果想要无错漏地将整个模式匹配出来进行替换,可能正则表达式并不能很好地完成(要考虑到括号是可以无限嵌套的,而正则表达式是不能表达无限嵌套的,除非将其嵌套限制在一个深度,但那样写出来的正则可读性也极差)

而对于 Python 代码的解析,当然最好的方式是直接将其翻译为 Python 的语法树,然后在语法树上匹配相应的模式并进行替换即可。Python 代码到语法树的解析,我们可以利用 builtin 的 `ast` 模块,以下是一个目前实现的 `self.assertTrue(np.allclose(...))` 替换的简单 demo:
Copy link
Collaborator

Choose a reason for hiding this comment

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

使用python语法数代替grep来做替换的想法很棒


为了避免全部肉眼判断与手动替换带来的巨大工作量,这里首先将 `self.assertTrue(np.array_equal(...))` 全部自动替换为 `np.testing.assert_array_equal(...)`,之后对现存 `np.testing.assert_array_equal(...)` 进行搜索,手动将 `float` 类型数据的替换为 `np.testing.assert_allclose(...)`。

在已有的测试中(https://github.com/PaddlePaddle/Paddle/pull/44947),已经尝试将 `self.assertTrue(np.array_equal(...))` 全部自动替换为 `np.testing.assert_array_equal(...)`,CI 没有任何问题,因此该替换是安全的,因此在不进行手工替换的情况下也是没有任何问题的。但为了让测试更加严谨,应当手动再将 `float` 的替换为 `np.testing.assert_allclose(...)`。在无法立即判断的情况下保持现状。
Copy link
Collaborator

Choose a reason for hiding this comment

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

@qili93 讨论了下:
assert_array_equal可以对float进行判断,见https://numpy.org/doc/stable/reference/generated/numpy.testing.assert_array_equal.html
6622ceb83ba11279d76533b2251891c1
因此,PaddlePaddle/Paddle#44947 在CI过了的情况下,可以先合入。

  • self.assertTrue(np.array_equal(...))不管是int还是float类型,都可以用np.testing.assert_array_equal(...)代替。
  • 但由于某些硬件本身的精度问题,如果使用assert_array_equal后CI过不了,就要用np.testing.assert_allclose(...),并搭配修改atolrtol来过CI。
    @SigureMo 可以对应修改下RFC

Copy link
Member Author

Choose a reason for hiding this comment

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

已修改,我和 @Yulv-git 再商量下分工问题后找你们 review~

@SigureMo SigureMo force-pushed the rfc-for-code-style-improvement-for-unittest branch from 9e81283 to acb46c7 Compare August 8, 2022 12:38
@SigureMo SigureMo force-pushed the rfc-for-code-style-improvement-for-unittest branch from 9364aeb to 373e7b7 Compare August 11, 2022 20:06
@SigureMo SigureMo marked this pull request as ready for review August 15, 2022 06:44
@SigureMo SigureMo changed the title [WIP, Don't review][CodeStyle] 单测报错信息优化 RFC [CodeStyle] 单测报错信息优化 RFC Aug 15, 2022
@SigureMo SigureMo requested a review from luotao1 August 15, 2022 06:44
Copy link
Collaborator

@luotao1 luotao1 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 32b5841 into PaddlePaddle:master Aug 15, 2022
@SigureMo SigureMo deleted the rfc-for-code-style-improvement-for-unittest branch August 15, 2022 09:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants