1*da0073e9SAndroid Build Coastguard Worker# Code Coverage Tool for Pytorch 2*da0073e9SAndroid Build Coastguard Worker 3*da0073e9SAndroid Build Coastguard Worker## Overview 4*da0073e9SAndroid Build Coastguard Worker 5*da0073e9SAndroid Build Coastguard WorkerThis tool is designed for calculating code coverage for Pytorch project. 6*da0073e9SAndroid Build Coastguard WorkerIt’s an integrated tool. You can use this tool to run and generate both file-level and line-level report for C++ and Python tests. It will also be the tool we use in *CircleCI* to generate report for each main commit. 7*da0073e9SAndroid Build Coastguard Worker 8*da0073e9SAndroid Build Coastguard Worker### Simple 9*da0073e9SAndroid Build Coastguard Worker* *Simple command to run:* 10*da0073e9SAndroid Build Coastguard Worker * `python oss_coverage.py ` 11*da0073e9SAndroid Build Coastguard Worker* *Argument `--clean` will do all the messy clean up things for you* 12*da0073e9SAndroid Build Coastguard Worker 13*da0073e9SAndroid Build Coastguard Worker### But Powerful 14*da0073e9SAndroid Build Coastguard Worker 15*da0073e9SAndroid Build Coastguard Worker* *Choose your own interested folder*: 16*da0073e9SAndroid Build Coastguard Worker * Default folder will be good enough in most times 17*da0073e9SAndroid Build Coastguard Worker * Flexible: you can specify one or more folder(s) that you are interested in 18*da0073e9SAndroid Build Coastguard Worker* *Run only the test you want:* 19*da0073e9SAndroid Build Coastguard Worker * By default it will run all the c++ and python tests 20*da0073e9SAndroid Build Coastguard Worker * Flexible: you can specify one or more test(s) that you want to run 21*da0073e9SAndroid Build Coastguard Worker* *Final report:* 22*da0073e9SAndroid Build Coastguard Worker * File-Level: The coverage percentage for each file you are interested in 23*da0073e9SAndroid Build Coastguard Worker * Line-Level: The coverage details for each line in each file you are interested in 24*da0073e9SAndroid Build Coastguard Worker * Html-Report (only for `gcc`): The beautiful HTML report supported by `lcov`, combine file-level report and line-lever report into a graphical view. 25*da0073e9SAndroid Build Coastguard Worker* *More complex but flexible options:* 26*da0073e9SAndroid Build Coastguard Worker * Use different stages like *--run, --export, --summary* to achieve more flexible functionality 27*da0073e9SAndroid Build Coastguard Worker 28*da0073e9SAndroid Build Coastguard Worker## How to use 29*da0073e9SAndroid Build Coastguard WorkerThis part will introduce about the arguments you can use when run this tool. The arguments are powerful, giving you full flexibility to do different work. 30*da0073e9SAndroid Build Coastguard WorkerWe have two different compilers, `gcc` and `clang`, and this tool supports both. But it is recommended to use `gcc` because it's much faster and use less disk place. The examples will also be divided to two parts, for `gcc` and `clang`. 31*da0073e9SAndroid Build Coastguard Worker 32*da0073e9SAndroid Build Coastguard Worker## Preparation 33*da0073e9SAndroid Build Coastguard WorkerThe first step is to [build *Pytorch* from source](https://github.com/pytorch/pytorch#from-source) with `USE_CPP_CODE_COVERAGE` option `ON`. You may also want to set `BUILD_TEST` option `ON` to get the test binaries. Besides, if you are under `gcc` compiler, to get accurate result, it is recommended to also select `CMAKE_BUILD_TYPE=Debug`. 34*da0073e9SAndroid Build Coastguard WorkerSee: [how to adjust build options](https://github.com/pytorch/pytorch#adjust-build-options-optional) for reference. Following is one way to adjust build option: 35*da0073e9SAndroid Build Coastguard Worker``` 36*da0073e9SAndroid Build Coastguard Worker# in build/ folder (all build artifacts must in `build/` folder) 37*da0073e9SAndroid Build Coastguard Workercmake .. -DUSE_CPP_CODE_COVERAGE=ON -DBUILD_TEST=ON -DCMAKE_BUILD_TYPE=Debug 38*da0073e9SAndroid Build Coastguard Worker``` 39*da0073e9SAndroid Build Coastguard Worker 40*da0073e9SAndroid Build Coastguard Worker 41*da0073e9SAndroid Build Coastguard Worker## Examples 42*da0073e9SAndroid Build Coastguard WorkerThe tool will auto-detect compiler type in your operating system, but if you are using another one, you need to specify it. Besides, if you are using `clang`, `llvm` tools are required. So the first step is to set some environment value if needed: 43*da0073e9SAndroid Build Coastguard Worker```bash 44*da0073e9SAndroid Build Coastguard Worker# set compiler type, the default is auto detected, you can check it at the start of log.txt 45*da0073e9SAndroid Build Coastguard Workerexport COMPILER_TYPE="CLANG" 46*da0073e9SAndroid Build Coastguard Worker# set llvm path for clang, by default is /usr/local/opt/llvm/bin 47*da0073e9SAndroid Build Coastguard Workerexport LLVM_TOOL_PATH=... 48*da0073e9SAndroid Build Coastguard Worker``` 49*da0073e9SAndroid Build Coastguard Worker 50*da0073e9SAndroid Build Coastguard WorkerGreat, you are ready to run the code coverage tool for the first time! Start from the simple command: 51*da0073e9SAndroid Build Coastguard Worker``` 52*da0073e9SAndroid Build Coastguard Workerpython oss_coverage.py --run-only=atest 53*da0073e9SAndroid Build Coastguard Worker``` 54*da0073e9SAndroid Build Coastguard WorkerThis command will run `atest` binary in `build/bin/` folder and generate reports over the entire *Pytorch* folder. You can find the reports in `profile/summary`. But you may only be interested in the `aten` folder, in this case, try: 55*da0073e9SAndroid Build Coastguard Worker``` 56*da0073e9SAndroid Build Coastguard Workerpython oss_coverage.py --run-only=atest --interest-only=aten 57*da0073e9SAndroid Build Coastguard Worker``` 58*da0073e9SAndroid Build Coastguard WorkerIn *Pytorch*, `c++` tests located in `build/bin/` and `python` tests located in `test/`. If you want to run `python` test, try: 59*da0073e9SAndroid Build Coastguard Worker``` 60*da0073e9SAndroid Build Coastguard Workerpython oss_coverage.py --run-only=test_complex.py 61*da0073e9SAndroid Build Coastguard Worker``` 62*da0073e9SAndroid Build Coastguard Worker 63*da0073e9SAndroid Build Coastguard WorkerYou may also want to specify more than one test or interested folder, in this case, try: 64*da0073e9SAndroid Build Coastguard Worker``` 65*da0073e9SAndroid Build Coastguard Workerpython oss_coverage.py --run-only=atest c10_logging_test --interest-only aten/src/Aten c10/core 66*da0073e9SAndroid Build Coastguard Worker``` 67*da0073e9SAndroid Build Coastguard WorkerThat it is! With these two simple options, you can customize many different functionality according to your need. 68*da0073e9SAndroid Build Coastguard WorkerBy default, the tool will run all tests in `build/bin` folder (by running all executable binaries in it) and `test/` folder (by running `run_test.py`), and then collect coverage over the entire *Pytorch* folder. If this is what you want, try: 69*da0073e9SAndroid Build Coastguard Worker*(Note: It's not recommended to run default all tests in clang, because it will take too much space)* 70*da0073e9SAndroid Build Coastguard Worker```bash 71*da0073e9SAndroid Build Coastguard Workerpython oss_coverage.py 72*da0073e9SAndroid Build Coastguard Worker``` 73*da0073e9SAndroid Build Coastguard Worker 74*da0073e9SAndroid Build Coastguard Worker### For more complex arguments and functionalities 75*da0073e9SAndroid Build Coastguard Worker#### GCC 76*da0073e9SAndroid Build Coastguard WorkerThe code coverage with `gcc` compiler can be divided into 3 step: 77*da0073e9SAndroid Build Coastguard Worker1. run the tests: `--run` 78*da0073e9SAndroid Build Coastguard Worker2. run `gcov` to get json report: `--export` 79*da0073e9SAndroid Build Coastguard Worker3. summarize it to human readable file report and line report: `--summary` 80*da0073e9SAndroid Build Coastguard Worker 81*da0073e9SAndroid Build Coastguard WorkerBy default all steps will be run, but you can specify only run one of them. Following is some usage scenario: 82*da0073e9SAndroid Build Coastguard Worker 83*da0073e9SAndroid Build Coastguard Worker**1. Interested in different folder** 84*da0073e9SAndroid Build Coastguard Worker`—summary` is useful when you have different interested folder. For example, 85*da0073e9SAndroid Build Coastguard Worker```bash 86*da0073e9SAndroid Build Coastguard Worker# after run this command 87*da0073e9SAndroid Build Coastguard Workerpython oss_coverage.py --run-only=atest --interest-only=aten 88*da0073e9SAndroid Build Coastguard Worker# you may then want to learn atest's coverage over c10, instead of running the test again, you can: 89*da0073e9SAndroid Build Coastguard Workerpython oss_coverage.py --run-only=atest --interest-only=c10 --summary 90*da0073e9SAndroid Build Coastguard Worker``` 91*da0073e9SAndroid Build Coastguard Worker 92*da0073e9SAndroid Build Coastguard Worker 93*da0073e9SAndroid Build Coastguard Worker**2. Run tests yourself** 94*da0073e9SAndroid Build Coastguard WorkerWhen you are developing a new feature, you may first run the tests yourself to make sure the implementation is all right and then want to learn its coverage. But sometimes the test take very long time and you don't want to wait to run it again when doing code coverage. In this case, you can use these arguments to accelerate your development (make sure you build pytorch with the coverage option!): 95*da0073e9SAndroid Build Coastguard Worker``` 96*da0073e9SAndroid Build Coastguard Worker# run tests when you are developing a new feature, assume the test is `test_nn.py` 97*da0073e9SAndroid Build Coastguard Workerpython oss_coverage.py --run-only=test_nn.py 98*da0073e9SAndroid Build Coastguard Worker# or you can run it yourself 99*da0073e9SAndroid Build Coastguard Workercd test/ && python test_nn.py 100*da0073e9SAndroid Build Coastguard Worker# then you want to learn about code coverage, you can just run: 101*da0073e9SAndroid Build Coastguard Workerpython oss_coverage.py --run-only=test_nn.py --export --summary 102*da0073e9SAndroid Build Coastguard Worker``` 103*da0073e9SAndroid Build Coastguard Worker 104*da0073e9SAndroid Build Coastguard Worker### CLANG 105*da0073e9SAndroid Build Coastguard WorkerThe steps for `clang` is very similar to `gcc`, but the export stage is divided into two step: 106*da0073e9SAndroid Build Coastguard Worker1. run the tests: `--run` 107*da0073e9SAndroid Build Coastguard Worker2. run `gcov` to get json report: `--merge` `--export` 108*da0073e9SAndroid Build Coastguard Worker3. summarize it to human readable file report and line report: `--summary` 109*da0073e9SAndroid Build Coastguard Worker 110*da0073e9SAndroid Build Coastguard WorkerTherefore, just replace `--export` in `gcc` examples with `--merge` and `--export`, you will find it work! 111*da0073e9SAndroid Build Coastguard Worker 112*da0073e9SAndroid Build Coastguard Worker## Reference 113*da0073e9SAndroid Build Coastguard Worker 114*da0073e9SAndroid Build Coastguard WorkerFor `gcc` 115*da0073e9SAndroid Build Coastguard Worker* See about how to invoke `gcov`, read [Invoking gcov](https://gcc.gnu.org/onlinedocs/gcc/Invoking-Gcov.html#Invoking-Gcov) will be helpful 116*da0073e9SAndroid Build Coastguard Worker 117*da0073e9SAndroid Build Coastguard WorkerFor `clang` 118*da0073e9SAndroid Build Coastguard Worker* If you are not familiar with the procedure of generating code coverage report by using `clang`, read [Source-based Code Coverage](https://clang.llvm.org/docs/SourceBasedCodeCoverage.html) will be helpful. 119