Paddle icon indicating copy to clipboard operation
Paddle copied to clipboard

[WIP][CodeStyle] use np.testing.assert_allclose instead of self.assertTrue(np.allclose(...))

Open SigureMo opened this issue 3 years ago • 2 comments

PR types

Others

PR changes

Others

Describe

使用 np.testing.assert_allclose(...) 替换 self.assertTrue(np.allclose(...)),以优化单测报错信息

  • 问题来源于 #44641
  • RFC:https://github.com/PaddlePaddle/community/pull/198

Changes

  • [ ] self.assertTrue(np.allclose(...)) -> np.testing.assert_allclose(...)
  • [ ] 等价情况 self.assertEqual(np.allclose(...), True) -> np.testing.assert_allclose(...)
  • [ ] 等价情况 self.assertTrue(numpy.allclose(...)) -> np.testing.assert_allclose(...)
  • [ ] 部分原来代码里的错误使用 self.assertTrue(np.allclose(...), True) 移除 True
  • [ ] 等价情况 self.assertTrue(np.isclose(...).all()) -> np.testing.assert_allclose(...)
  • [ ] 语法树替换后有些 1e-2 变成 0.01 了,明显 1e-2 更好一些
  • [ ] 清理冗余的 err_msg,部分 err_msg 只是打印了 a 和 b,np.testing.allclose 本就会打印它们,因此可以清理

目前直接替换后发现大量测试无法通过(375),这基本是没有办法手动一个个修改的……问题主要在以下几个方面:

  • 精度误差(本 PR 将集中解决本问题)

    这是由于两者默认值不同

    • 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)

    明显 np.testing.assert_allclose 精度要求更高,所以这里暂时 np.allclosertol(即设为默认值)时将值设为 1e-5,使其与原来行为一致,atol 暂时不改看看情况

    修改后测试失败大大降低(375 -> 195),基本降了一半,余量已经很少了,经检查,余量大多都是些误差非常小的(1e-10 以下),针对这些手动加上 atol=1e-8 应可解决

  • shape 不对齐

    这是由于 np.allclose 在比较时会自动 broadcast,而 np.testing.allclose 不会,因此需要手动对这些数据进行检查及修改

    修改精度问题后,本问题占了 90% 以上,也就是将近 200 个需要考虑本问题,逐个手动修复,需要进一步评估成本

    目前发现的问题:

    • 静态图返回是一个 list,但有的开发者直接将返回值进行比较,这会在比较时认为静态图的 shape 比预期值多一维度,如下面这个(找个问题好麻烦呀……),该问题占了很大一部分https://github.com/PaddlePaddle/Paddle/blob/c91aaced74aa1a34c8bde2e53b3072baf8012e73/python/paddle/fluid/tests/unittests/test_softmax2d.py#L39-L41

    • 误操作,这里的 expected_dx 不需要添加 [0] https://github.com/PaddlePaddle/Paddle/blob/c91aaced74aa1a34c8bde2e53b3072baf8012e73/python/paddle/fluid/tests/unittests/test_imperative_double_grad.py#L83-L86

  • 静态图代码报错(shape 不对齐问题导致测试不通过而无法转为静态图,进而后续静态图测试全部无法通过,本质上是问题二)

SigureMo avatar Aug 08 '22 12:08 SigureMo

你的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[bot] avatar Aug 08 '22 12:08 paddle-bot[bot]

静态图代码报错。应当可用测试代码开始时 paddle.enable_static(),测试代码结束时 paddle.disable_static() 包裹该段测试代码(还没测试)

可以贴一下哪些单测的静态图代码报错么?

luotao1 avatar Aug 09 '22 07:08 luotao1

很抱歉,经过我们的反复讨论,你的PR暂未达到合入标准,请阅读飞桨原生算子开发规范,你可以重新提交新的PR,我们先将此PR关闭,感谢你的贡献。 Sorry to inform you that through our discussion, your PR fails to meet the merging standard (Reference: Paddle Custom Operator Design Doc). You can also submit an new one. Thank you.

paddle-bot[bot] avatar Aug 10 '22 14:08 paddle-bot[bot]

@SigureMo @qili93 需要在IPU/XPU的机器上跑一下看看,跑完后回复这个PR

luotao1 avatar Aug 15 '22 01:08 luotao1

@SigureMo @qili93 需要在IPU/XPU的机器上跑一下看看,跑完后回复这个PR

好的,如果有问题我恢复相关修改

SigureMo avatar Aug 15 '22 03:08 SigureMo

@SigureMo

由于对于XPU相关修改比较多,和昆仑同学确认,请提交一条以“xxxx, test=kunlun” 结尾的的commit msg,会自动触发昆仑流水线进行单测检查。

qili93 avatar Aug 16 '22 02:08 qili93

CI passed for MLU and IPU.

qili93 avatar Aug 16 '22 05:08 qili93

由于对于XPU相关修改比较多,和昆仑同学确认,请提交一条以“xxxx, test=kunlun” 结尾的的commit msg,会自动触发昆仑流水线进行单测检查。

最新一次 commit 包含了 test=kunlun,目前除了两个非 Required 其余全部都通过了~

SigureMo avatar Aug 16 '22 20:08 SigureMo