pytest-cov icon indicating copy to clipboard operation
pytest-cov copied to clipboard

pytorch subclass of torch.autograd.Function's backward can be traced by pytest-cov

Open Peeta586 opened this issue 3 years ago • 1 comments

Summary

According to the log, the backward function in subclass of torch.autograd.Function has been executed. but pytest-cov reports this function is missing. image

please help me check this problem, why it happened. Thank you very much.

Versions

pytest=7.1.2 pytest-cov=3.0.0

Config

setup.cfg

# pytest-cov config
[coverage:run]
branch = True
omit =
    # exclude some file that should not be tested
    */test_*.py
    */*_test.py

[coverage:report]
# result precision
precision = 2
exclude_lines =
    # Don't complain about missing debug-only code:
    if __name__ == .__main__.:
 
[coverage:html]
# report output dir
directory = ./tests/report/cov

Code


class Test(torch.autograd.Function):
    @staticmethod
    def forward(ctx, x):
         return x
    @staticmethod
    def backward(ctx,grad_out):
       return grad_out

test = Test.apply
input = torch.randn(2,3)
output = test(input)

output.backward(torch.ones_like(output))

Peeta586 avatar May 27 '22 12:05 Peeta586

This will be a problem due to coverage.py and its interactions with PyTorch. I thought there was an issue already about it, but I don't see it now. Feel free to open an issue there: https://github.com/nedbat/coveragepy/issues/new/choose

nedbat avatar May 27 '22 16:05 nedbat