1# Contributing guidelines 2 3## Pull Request Checklist 4 5Before sending your pull requests, make sure you do the following: 6 7- Read the [contributing guidelines](CONTRIBUTING.md). 8- Read the [Code of Conduct](CODE_OF_CONDUCT.md). 9- Ensure you have signed the 10 [Contributor License Agreement (CLA)](https://cla.developers.google.com/). 11- Check if your changes are consistent with the 12 [guidelines](#general-guidelines-and-philosophy-for-contribution). 13- Changes are consistent with the [Coding Style](#c-coding-style). 14- Run the [unit tests](#running-unit-tests). 15 16## How to become a contributor and submit your own code 17 18### Contributor License Agreements 19 20We'd love to accept your patches! Before we can take them, we have to jump a couple of legal hurdles. 21 22Please fill out either the individual or corporate Contributor License Agreement (CLA). 23 24 * If you are an individual writing original source code and you're sure you own the intellectual property, then you'll need to sign an [individual CLA](https://code.google.com/legal/individual-cla-v1.0.html). 25 * If you work for a company that wants to allow you to contribute your work, then you'll need to sign a [corporate CLA](https://code.google.com/legal/corporate-cla-v1.0.html). 26 27Follow either of the two links above to access the appropriate CLA and instructions for how to sign and return it. Once we receive it, we'll be able to accept your pull requests. 28 29***NOTE***: Only original source code from you and other people that have signed the CLA can be accepted into the main repository. 30 31### Contributing code 32 33If you have improvements to TensorFlow, send us your pull requests! For those 34just getting started, Github has a 35[how to](https://help.github.com/articles/using-pull-requests/). 36 37TensorFlow team members will be assigned to review your pull requests. Once the 38pull requests are approved and pass continuous integration checks, a TensorFlow 39team member will apply `ready to pull` label to your change. This means we are 40working on getting your pull request submitted to our internal repository. After 41the change has been submitted internally, your pull request will be merged 42automatically on GitHub. 43 44If you want to contribute, start working through the TensorFlow codebase, 45navigate to the 46[Github "issues" tab](https://github.com/tensorflow/tensorflow/issues) and start 47looking through interesting issues. If you are not sure of where to start, then 48start by trying one of the smaller/easier issues here i.e. 49[issues with the "good first issue" label](https://github.com/tensorflow/tensorflow/labels/good%20first%20issue) 50and then take a look at the 51[issues with the "contributions welcome" label](https://github.com/tensorflow/tensorflow/labels/stat%3Acontributions%20welcome). 52These are issues that we believe are particularly well suited for outside 53contributions, often because we probably won't get to them right now. If you 54decide to start on an issue, leave a comment so that other people know that 55you're working on it. If you want to help out, but not alone, use the issue 56comment thread to coordinate. 57 58### Contribution guidelines and standards 59 60Before sending your pull request for 61[review](https://github.com/tensorflow/tensorflow/pulls), 62make sure your changes are consistent with the guidelines and follow the 63TensorFlow coding style. 64 65#### General guidelines and philosophy for contribution 66 67* Include unit tests when you contribute new features, as they help to a) 68 prove that your code works correctly, and b) guard against future breaking 69 changes to lower the maintenance cost. 70* Bug fixes also generally require unit tests, because the presence of bugs 71 usually indicates insufficient test coverage. 72* Keep API compatibility in mind when you change code in core TensorFlow, 73 e.g., code in 74 [tensorflow/core](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/core) 75 and 76 [tensorflow/python](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/python). 77 TensorFlow has passed version 1.0 and hence cannot make 78 non-backward-compatible API changes without a major release. Reviewers of 79 your pull request will comment on any API compatibility issues 80 [following API review practices](https://github.com/tensorflow/community/blob/master/governance/api-reviews.md). 81* When you contribute a new feature to TensorFlow, the maintenance burden is 82 (by default) transferred to the TensorFlow team. This means that the benefit 83 of the contribution must be compared against the cost of maintaining the 84 feature. 85* Full new features (e.g., a new op implementing a cutting-edge algorithm) 86 typically will live in 87 [tensorflow/addons](https://github.com/tensorflow/addons) to get some 88 airtime before a decision is made regarding whether they are to be migrated 89 to the core. 90* As every PR requires several CPU/GPU hours of CI testing, we discourage 91 submitting PRs to fix one typo, one warning,etc. We recommend fixing the 92 same issue at the file level at least (e.g.: fix all typos in a file, fix 93 all compiler warning in a file, etc.) 94* Tests should follow the 95 [testing best practices](https://www.tensorflow.org/community/contribute/tests) 96 guide. 97 98#### License 99 100Include a license at the top of new files. 101 102* [C/C++ license example](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/framework/op.cc#L1) 103* [Python license example](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/nn.py#L1) 104* [Java license example](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/java/src/main/java/org/tensorflow/Graph.java#L1) 105* [Go license example](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/go/operation.go#L1) 106* [Bash license example](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/ci_build/ci_sanity.sh#L2) 107* [HTML license example](https://github.com/tensorflow/tensorboard/blob/master/tensorboard/components/tf_backend/tf-backend.html#L2) 108* [JavaScript/TypeScript license example](https://github.com/tensorflow/tensorboard/blob/master/tensorboard/components/tf_backend/backend.ts#L1) 109 110Bazel BUILD files also need to include a license section, e.g., 111[BUILD example](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/BUILD#L61). 112 113#### C++ coding style 114 115Changes to TensorFlow C++ code should conform to 116[Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html). 117 118Use `clang-tidy` to check your C/C++ changes. To install `clang-tidy` on ubuntu:16.04, do: 119 120```bash 121apt-get install -y clang-tidy 122``` 123 124You can check a C/C++ file by doing: 125 126 127```bash 128clang-format <my_cc_file> --style=google > /tmp/my_cc_file.cc 129diff <my_cc_file> /tmp/my_cc_file.cc 130``` 131 132#### Python coding style 133 134Changes to TensorFlow Python code should conform to 135[Google Python Style Guide](https://github.com/google/styleguide/blob/gh-pages/pyguide.md) 136 137Use `pylint` to check your Python changes. To install `pylint` and check a file 138with `pylint` against TensorFlow's custom style definition: 139 140```bash 141pip install pylint 142pylint --rcfile=tensorflow/tools/ci_build/pylintrc myfile.py 143``` 144 145Note `pylint --rcfile=tensorflow/tools/ci_build/pylintrc` should run from the 146top level tensorflow directory. 147 148#### Coding style for other languages 149 150* [Google Java Style Guide](https://google.github.io/styleguide/javaguide.html) 151* [Google JavaScript Style Guide](https://google.github.io/styleguide/jsguide.html) 152* [Google Shell Style Guide](https://google.github.io/styleguide/shell.xml) 153* [Google Objective-C Style Guide](https://google.github.io/styleguide/objcguide.html) 154 155#### Running sanity check 156 157If you have Docker installed on your system, you can perform a sanity check on 158your changes by running the command: 159 160```bash 161tensorflow/tools/ci_build/ci_build.sh CPU tensorflow/tools/ci_build/ci_sanity.sh 162``` 163 164This will catch most license, Python coding style and BUILD file issues that 165may exist in your changes. 166 167#### Running unit tests 168 169There are two ways to run TensorFlow unit tests. 170 1711. Using tools and libraries installed directly on your system. 172 173 Refer to the 174 [CPU-only developer Dockerfile](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/dockerfiles/dockerfiles/devel-cpu.Dockerfile) 175 and 176 [GPU developer Dockerfile](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/dockerfiles/dockerfiles/devel-gpu.Dockerfile) 177 for the required packages. Alternatively, use the said 178 [Docker images](https://hub.docker.com/r/tensorflow/tensorflow/tags/), e.g., 179 `tensorflow/tensorflow:devel` and `tensorflow/tensorflow:devel-gpu` for 180 development to avoid installing the packages directly on your system (in 181 which case remember to change directory from `/root` to `/tensorflow` once 182 you get into the running container so `bazel` can find the `tensorflow` 183 workspace). 184 185 Once you have the packages installed, you can run a specific unit test in 186 bazel by doing as follows: 187 188 ```bash 189 export flags="--config=opt -k" 190 ``` 191 192 If the tests are to be run on GPU, add CUDA paths to LD_LIBRARY_PATH and add 193 the `cuda` option flag 194 195 ```bash 196 export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64:$LD_LIBRARY_PATH" 197 198 export flags="--config=opt --config=cuda -k" 199 ``` 200 201 For example, to run all tests under tensorflow/python, do: 202 203 ```bash 204 bazel test ${flags} //tensorflow/python/... 205 ``` 206 207 For a single component e.g. softmax op: 208 209 ```bash 210 bazel test ${flags} tensorflow/python/kernel_tests:softmax_op_test 211 ``` 212 213 For a single/parameterized test e.g. `test_capture_variables` in 214 `tensorflow/python/saved_model/load_test.py`: 215 216 (Requires `python>=3.7`) 217 218 ```bash 219 bazel test ${flags} //tensorflow/python/saved_model:load_test --test_filter=*LoadTest.test_capture_variables* 220 ``` 221 222 **Note:** You can add `--test_sharding_strategy=disabled` to the `flags` to 223 disable the sharding so that all the test outputs are in one file. However, 224 it may slow down the tests for not running in parallel and may cause the 225 test to timeout but it could be useful when you need to execute a single 226 test or more in general your filtered/selected tests have a very low 227 execution time and the sharding 228 [could create an overhead on the test execution](https://github.com/bazelbuild/bazel/issues/2113#issuecomment-264054799). 229 2302. Using [Docker](https://www.docker.com) and TensorFlow's CI scripts. 231 232 ```bash 233 # Install Docker first, then this will build and run cpu tests 234 tensorflow/tools/ci_build/ci_build.sh CPU bazel test //tensorflow/... 235 ``` 236 237 See 238 [TensorFlow Builds](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/tools/ci_build) 239 for details. 240 241#### Running doctest for testable docstring 242 243There are two ways to test the code in the docstring locally: 244 2451. If you are only changing the docstring of a class/function/method, then you 246 can test it by passing that file's path to 247 [tf_doctest.py](https://www.tensorflow.org/code/tensorflow/tools/docs/tf_doctest.py). 248 For example: 249 250 ```bash 251 python tf_doctest.py --file=<file_path> 252 ``` 253 254 This will run it using your installed version of TensorFlow. To be sure 255 you're running the same code that you're testing: 256 257 * Use an up to date [tf-nightly](https://pypi.org/project/tf-nightly/) 258 `pip install -U tf-nightly` 259 * Rebase your pull request onto a recent pull from 260 [TensorFlow's](https://github.com/tensorflow/tensorflow) master branch. 261 2622. If you are changing the code and the docstring of a class/function/method, 263 then you will need to 264 [build TensorFlow from source](https://www.tensorflow.org/install/source). 265 Once you are setup to build from source, you can run the tests: 266 267 ```bash 268 bazel run //tensorflow/tools/docs:tf_doctest 269 ``` 270 271 or 272 273 ```bash 274 bazel run //tensorflow/tools/docs:tf_doctest -- --module=ops.array_ops 275 ``` 276 277 The `--module` is relative to `tensorflow.python`. 278 279#### Debug builds 280 281When [building Tensorflow](https://www.tensorflow.org/install/source), passing 282`--config=dbg` to Bazel will build with debugging information and without 283optimizations, allowing you to use GDB or other debuggers to debug C++ code. For 284example, you can build the pip package with debugging information by running: 285 286```bash 287bazel build --config=dbg //tensorflow/tools/pip_package:build_pip_package 288``` 289 290TensorFlow kernels and TensorFlow's dependencies are still not built with 291debugging information with `--config=dbg`, as issues occur on Linux if 292there is too much debug info (see [this GitHub 293issue](https://github.com/tensorflow/tensorflow/issues/48919) for context). If 294you want to debug a kernel, you can compile specific files with `-g` using the 295`--per_file_copt` bazel option. For example, if you want to debug the Identity 296op, which are in files starting with `identity_op`, you can run 297 298```bash 299bazel build --config=dbg --per_file_copt=+tensorflow/core/kernels/identity_op.*@-g //tensorflow/tools/pip_package:build_pip_package 300``` 301 302Note that the `--config=dbg` option is not officially supported. 303