README.md
1# Code Coverage Tool for Pytorch
2
3## Overview
4
5This tool is designed for calculating code coverage for Pytorch project.
6It’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
8### Simple
9* *Simple command to run:*
10 * `python oss_coverage.py `
11* *Argument `--clean` will do all the messy clean up things for you*
12
13### But Powerful
14
15* *Choose your own interested folder*:
16 * Default folder will be good enough in most times
17 * Flexible: you can specify one or more folder(s) that you are interested in
18* *Run only the test you want:*
19 * By default it will run all the c++ and python tests
20 * Flexible: you can specify one or more test(s) that you want to run
21* *Final report:*
22 * File-Level: The coverage percentage for each file you are interested in
23 * Line-Level: The coverage details for each line in each file you are interested in
24 * 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* *More complex but flexible options:*
26 * Use different stages like *--run, --export, --summary* to achieve more flexible functionality
27
28## How to use
29This 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.
30We 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
32## Preparation
33The 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`.
34See: [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```
36# in build/ folder (all build artifacts must in `build/` folder)
37cmake .. -DUSE_CPP_CODE_COVERAGE=ON -DBUILD_TEST=ON -DCMAKE_BUILD_TYPE=Debug
38```
39
40
41## Examples
42The 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```bash
44# set compiler type, the default is auto detected, you can check it at the start of log.txt
45export COMPILER_TYPE="CLANG"
46# set llvm path for clang, by default is /usr/local/opt/llvm/bin
47export LLVM_TOOL_PATH=...
48```
49
50Great, you are ready to run the code coverage tool for the first time! Start from the simple command:
51```
52python oss_coverage.py --run-only=atest
53```
54This 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```
56python oss_coverage.py --run-only=atest --interest-only=aten
57```
58In *Pytorch*, `c++` tests located in `build/bin/` and `python` tests located in `test/`. If you want to run `python` test, try:
59```
60python oss_coverage.py --run-only=test_complex.py
61```
62
63You may also want to specify more than one test or interested folder, in this case, try:
64```
65python oss_coverage.py --run-only=atest c10_logging_test --interest-only aten/src/Aten c10/core
66```
67That it is! With these two simple options, you can customize many different functionality according to your need.
68By 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*(Note: It's not recommended to run default all tests in clang, because it will take too much space)*
70```bash
71python oss_coverage.py
72```
73
74### For more complex arguments and functionalities
75#### GCC
76The code coverage with `gcc` compiler can be divided into 3 step:
771. run the tests: `--run`
782. run `gcov` to get json report: `--export`
793. summarize it to human readable file report and line report: `--summary`
80
81By default all steps will be run, but you can specify only run one of them. Following is some usage scenario:
82
83**1. Interested in different folder**
84`—summary` is useful when you have different interested folder. For example,
85```bash
86# after run this command
87python oss_coverage.py --run-only=atest --interest-only=aten
88# you may then want to learn atest's coverage over c10, instead of running the test again, you can:
89python oss_coverage.py --run-only=atest --interest-only=c10 --summary
90```
91
92
93**2. Run tests yourself**
94When 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```
96# run tests when you are developing a new feature, assume the test is `test_nn.py`
97python oss_coverage.py --run-only=test_nn.py
98# or you can run it yourself
99cd test/ && python test_nn.py
100# then you want to learn about code coverage, you can just run:
101python oss_coverage.py --run-only=test_nn.py --export --summary
102```
103
104### CLANG
105The steps for `clang` is very similar to `gcc`, but the export stage is divided into two step:
1061. run the tests: `--run`
1072. run `gcov` to get json report: `--merge` `--export`
1083. summarize it to human readable file report and line report: `--summary`
109
110Therefore, just replace `--export` in `gcc` examples with `--merge` and `--export`, you will find it work!
111
112## Reference
113
114For `gcc`
115* See about how to invoke `gcov`, read [Invoking gcov](https://gcc.gnu.org/onlinedocs/gcc/Invoking-Gcov.html#Invoking-Gcov) will be helpful
116
117For `clang`
118* 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