Gradient Check Is Required for Op Test (English) - PaddlePaddle/Paddle GitHub Wiki

OP Test Specification:Gradient check is required for Op test


单测规范:反向Op必须调用check_grad (中文版))


Specification summary::

  • Section 1, Background
  • Section 2, Details of The Specification
  • Section 2, Instructions of CI
  • Section 2, Suggestions Of Check Failure

Supplementary Note:

You may find some aspects that are not taken into account in the existing specifications, which need to be continuously supplemented and improved during the implementation process. Please feel free to give your feedback.

1. Background

At present, there is a problem in the OP unit tests that the gradient check is not performed or is disabled. In order to ensure the correct implementation of all Grad Ops, this specification is proposed, and corresponding checks are added in CI. Except for special cases, all Op unit tests need to call the check_grad method.

2. Details of The Specification

In Op's unit test, all cases that inherit OpTest need to call the check_grad method for gradient checking except for some special cases.

special cases

number case reason
1 Grad Op is not registered Grad check is meaningless
2 Python API test Grad check is not required
3 other cases Grad check is not necessary for some reasons
  • For case 1:Exempt it by whitelist EMPTY_GRAD_OP_LIST
  • For case 2:Test cases need to inherit unittest.TestCase
  • For case 3:use @skip_check_grad_ci and describe the reason for skipping check_grad CI.

3. Instructions of CI

  • When the unit test is decorated by skip_check_grad_ci, the no_need_check_grad attribute is added to the test case.
  • When the unit test calls the check_grad or check_grad_with_place method, the attribute exist_check_grad is added to the test case.

After the execution of the unit test, if the check_grad or check_grad_with_place method is not called, the AssertionError will be raised. Except for following cases:

  • If the test case has the attribute no_need_check_grad, or op_type is in the EMPTY_GRAD_OP_LIST whitelist
  • The data type of input is int8 or bool

4. Suggestions Of Check Failure

When the check fails, you can modify unit test by referring to the following suggestions:

  • If it is not Op test,for example, Python API test,the test case needs to inherit unittest.TestCase
  • If test case do not need to check grad for some reason, please use @skip_check_grad_ci and describe the reason. The use of this decorator requires the approval of specific reviewers.

If you encounter other problems, please contact @zhangting2020

⚠️ **GitHub.com Fallback** ⚠️