CONTRIBUTING.md 15.8 KB
Newer Older
1 2
# Contributing guidelines

3 4
## Pull Request Checklist

M
Mark Daoust 已提交
5
Before sending your pull requests, make sure you do the following:
6

7 8 9 10 11 12 13 14
-   Read the [contributing guidelines](CONTRIBUTING.md).
-   Read the [Code of Conduct](CODE_OF_CONDUCT.md).
-   Ensure you have signed the
    [Contributor License Agreement (CLA)](https://cla.developers.google.com/).
-   Check if your changes are consistent with the
    [guidelines](#general-guidelines-and-philosophy-for-contribution).
-   Changes are consistent with the [Coding Style](#c-coding-style).
-   Run the [unit tests](#running-unit-tests).
15

16 17
## How to become a contributor and submit your own code

A
Abinash Satapathy 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
```mermaid
flowchart LR
A(New PR) ---> B{Valid}
B --> |Yes| C(Review)
B --> |No| D(Send back/Reject)
C --> E{Need change}
E --> |No| F(Approved)
E --> |Yes| G(Change requested)
G --> C
F --> H{Kokoro tests}
H --> |Fail| G
H --> |Pass| I(Copy to G3)
I --> J{Run tests}
J --> |Fail| G
J -----> |Pass| K(Merged)

click A "https://github.com/tensorflow/tensorflow/blob/master/CONTRIBUTING.md#1-new-pr" _blank
click B "https://github.com/tensorflow/tensorflow/blob/master/CONTRIBUTING.md#2-valid" _blank
click C "https://github.com/tensorflow/tensorflow/blob/master/CONTRIBUTING.md#3-review" _blank
click F "https://github.com/tensorflow/tensorflow/blob/master/CONTRIBUTING.md#4-approved" _blank
click I "https://github.com/tensorflow/tensorflow/blob/master/CONTRIBUTING.md#5-copy-to-g3" _blank
```

## Typical Pull Request Workflow

### 1. New PR
- As a contributor, you submit a New PR on GitHub. 
- We inspect every incoming PR and add certain labels to the PR such as `size:`, `comp:` etc. At this stage we check if the PR is valid and meets certain quality requirements. 
- For example: We check if the CLA is signed, PR has sufficient description, if applicable unit tests are added and if it is a reasonable contribution, i.e., it is not a single liner cosmetic PR.

### 2. Valid?
- If the PR passes all the quality checks then we go ahead and assign a reviewer.
- If the PR does not meet the validation criteria, we request for additional changes to be made to the PR to pass quality checks and send it back or on a rare occassion, we may reject it.

### 3. Review
- For a valid PR, the reviewer (person familiar with the code/functionality) checks if the PR looks good or needs additional changes. 
- If all looks good, the reviewer will approve the PR.
- If a change is needed, you (the contributor) are requested to make the suggested change.
- You make the change and submit for the review again. This cycle repeats itself till the PR gets approved.

> Note: As a friendly reminder we may reach out to you if the PR is awaiting your response for more than 2 weeks.

### 4. Approved
- Once the PR is approved, it gets the `kokoro:force-run` label applied and it initiates CI/CD tests.
- We can't move forward if these tests fail.
- If the tests fail, we may request you to make further changes to your PR for the tests to pass.
- Once the tests pass, we now bring all the code in the internal code base, using a job called "copybara".

### 5. Copy to G3
- Once the PR is in Google codebase, we make sure it integrates well with its dependencies and the rest of the system.
- Rarely, the tests might fail at this stage. If they do fail, we cannot merge the code.
- If needed, we may ask you to make some changes.
- At times, it may not be you, it may be us who may have hit a snag. Please be patient while we work to fix this.
- Once the internal tests pass, we go ahead and merge the code internally as well as externally on GitHub.

## Contributor License Agreements

We love to accept your patches! But before we can take them, we have to jump a couple of legal hurdles.
76 77 78

Please fill out either the individual or corporate Contributor License Agreement (CLA).

R
Ronald Eddy Jr 已提交
79 80
  * 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).
  * 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).
81 82 83

Follow 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.

A
Abinash Satapathy 已提交
84
> NOTE: Only original source code from you and other people that have signed the CLA can be accepted into the main repository.
85

86 87
### Contributing code

A
Abinash Satapathy 已提交
88 89 90 91 92
If you have improvements to TensorFlow, send us your pull requests! For those just getting started, Github has a [how to](https://help.github.com/articles/using-pull-requests/).

TensorFlow team members will be assigned to review your pull requests. Once the pull requests are approved and pass continuous integration checks, a TensorFlow team member will apply `ready to pull` label to your change. This means we are working on getting your pull request submitted to our internal repository. After the change has been submitted internally, your pull request will be merged automatically on GitHub.

If you want to contribute, start working through the TensorFlow codebase, navigate to the Github ["issues"](https://github.com/tensorflow/tensorflow/issues) tab and start looking through interesting issues. If you are not sure of where to start, then start by trying one of the smaller/easier issues here i.e. issues with the ["good first issue"](https://github.com/tensorflow/tensorflow/labels/good%20first%20issue) label and then take a look at the issues with the ["contributions welcome"](https://github.com/tensorflow/tensorflow/labels/stat%3Acontributions%20welcome) label. These are issues that we believe are particularly well suited for outside contributions, often because we probably won't get to them right now. If you decide to start working on an issue, leave a comment so that other people know that you're working on it. If you want to help out, but not alone, use the issue comment thread to coordinate.
V
Vijay Vasudevan 已提交
93 94 95 96 97 98 99 100 101 102

### Contribution guidelines and standards

Before sending your pull request for
[review](https://github.com/tensorflow/tensorflow/pulls),
make sure your changes are consistent with the guidelines and follow the
TensorFlow coding style.

#### General guidelines and philosophy for contribution

103 104 105 106 107 108 109 110 111 112
*   Include unit tests when you contribute new features, as they help to a)
    prove that your code works correctly, and b) guard against future breaking
    changes to lower the maintenance cost.
*   Bug fixes also generally require unit tests, because the presence of bugs
    usually indicates insufficient test coverage.
*   Keep API compatibility in mind when you change code in core TensorFlow,
    e.g., code in
    [tensorflow/core](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/core)
    and
    [tensorflow/python](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/python).
113
    TensorFlow has passed version 1.0 and hence cannot make
114
    non-backward-compatible API changes without a major release. Reviewers of
115 116
    your pull request will comment on any API compatibility issues
    [following API review practices](https://github.com/tensorflow/community/blob/master/governance/api-reviews.md).
117
*   When you contribute a new feature to TensorFlow, the maintenance burden is
118 119
    (by default) transferred to the TensorFlow team. This means that the benefit
    of the contribution must be compared against the cost of maintaining the
120 121 122 123
    feature.
*   Full new features (e.g., a new op implementing a cutting-edge algorithm)
    typically will live in
    [tensorflow/addons](https://github.com/tensorflow/addons) to get some
124 125
    airtime before a decision is made regarding whether they are to be migrated
    to the core.
R
Rajeshwar Reddy T 已提交
126
*   As every PR requires several CPU/GPU hours of CI testing, we discourage
A
Abinash Satapathy 已提交
127
    submitting PRs to fix one typo, one warning, etc. We recommend fixing the
128
    same issue at the file level at least (e.g.: fix all typos in a file, fix
A
Abinash Satapathy 已提交
129 130
    all compiler warnings in a file, etc.)
*   Tests should follow the [TensorFlow testing best practices](https://www.tensorflow.org/community/contribute/tests)
131
    guide.
A
Abinash Satapathy 已提交
132
    
V
Vijay Vasudevan 已提交
133 134 135 136 137 138 139 140 141

#### License

Include a license at the top of new files.

* [C/C++ license example](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/framework/op.cc#L1)
* [Python license example](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/nn.py#L1)
* [Java license example](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/java/src/main/java/org/tensorflow/Graph.java#L1)
* [Go license example](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/go/operation.go#L1)
A
Abinash Satapathy 已提交
142
* [Bash license example](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/ci_build/code_link_check.sh#L2)
143
* [JavaScript/TypeScript license example](https://github.com/tensorflow/tensorboard/blob/master/tensorboard/components/tf_backend/backend.ts#L1)
V
Vijay Vasudevan 已提交
144 145 146 147 148 149 150 151 152

Bazel BUILD files also need to include a license section, e.g.,
[BUILD example](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/BUILD#L61).

#### C++ coding style

Changes to TensorFlow C++ code should conform to
[Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html).

T
tucan 已提交
153
Use `clang-tidy` to check your C/C++ changes. To install `clang-tidy` on ubuntu:16.04, do:
V
Vijay Vasudevan 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

```bash
apt-get install -y clang-tidy
```

You can check a C/C++ file by doing:


```bash
clang-format <my_cc_file> --style=google > /tmp/my_cc_file.cc
diff <my_cc_file> /tmp/my_cc_file.cc
```

#### Python coding style

Changes to TensorFlow Python code should conform to
170
[Google Python Style Guide](https://github.com/google/styleguide/blob/gh-pages/pyguide.md)
V
Vijay Vasudevan 已提交
171

172 173
Use `pylint` to check your Python changes. To install `pylint` and check a file
with `pylint` against TensorFlow's custom style definition:
V
Vijay Vasudevan 已提交
174 175 176

```bash
pip install pylint
177
pylint --rcfile=tensorflow/tools/ci_build/pylintrc myfile.py
V
Vijay Vasudevan 已提交
178 179
```

A
Abinash Satapathy 已提交
180
> Note `pylint --rcfile=tensorflow/tools/ci_build/pylintrc` should run from the top level tensorflow directory.
V
Vijay Vasudevan 已提交
181 182 183 184 185

#### Coding style for other languages

* [Google Java Style Guide](https://google.github.io/styleguide/javaguide.html)
* [Google JavaScript Style Guide](https://google.github.io/styleguide/jsguide.html)
A
Abinash Satapathy 已提交
186
* [Google Shell Style Guide](https://google.github.io/styleguide/shellguide.html)
R
Ronald Eddy Jr 已提交
187
* [Google Objective-C Style Guide](https://google.github.io/styleguide/objcguide.html)
V
Vijay Vasudevan 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

#### Running sanity check

If you have Docker installed on your system, you can perform a sanity check on
your changes by running the command:

```bash
tensorflow/tools/ci_build/ci_build.sh CPU tensorflow/tools/ci_build/ci_sanity.sh
```

This will catch most license, Python coding style and BUILD file issues that
may exist in your changes.

#### Running unit tests

There are two ways to run TensorFlow unit tests.

205
1.  Using tools and libraries installed directly on your system.
V
Vijay Vasudevan 已提交
206

207
    Refer to the
208
    [CPU-only developer Dockerfile](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/dockerfiles/dockerfiles/devel-cpu.Dockerfile)
209
    and
210
    [GPU developer Dockerfile](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/dockerfiles/dockerfiles/devel-gpu.Dockerfile)
211 212
    for the required packages. Alternatively, use the said
    [Docker images](https://hub.docker.com/r/tensorflow/tensorflow/tags/), e.g.,
213 214 215 216 217
    `tensorflow/tensorflow:devel` and `tensorflow/tensorflow:devel-gpu` for
    development to avoid installing the packages directly on your system (in
    which case remember to change directory from `/root` to `/tensorflow` once
    you get into the running container so `bazel` can find the `tensorflow`
    workspace).
V
Vijay Vasudevan 已提交
218

219 220
    Once you have the packages installed, you can run a specific unit test in
    bazel by doing as follows:
V
Vijay Vasudevan 已提交
221

222 223 224
    ```bash
    export flags="--config=opt -k"
    ```
225

226 227
    If the tests are to be run on GPU, add CUDA paths to LD_LIBRARY_PATH and add
    the `cuda` option flag
V
Vijay Vasudevan 已提交
228

229 230
    ```bash
    export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64:$LD_LIBRARY_PATH"
V
Vijay Vasudevan 已提交
231

232 233
    export flags="--config=opt --config=cuda -k"
    ```
V
Vijay Vasudevan 已提交
234

235
    For example, to run all tests under tensorflow/python, do:
V
Vijay Vasudevan 已提交
236

237 238 239
    ```bash
    bazel test ${flags} //tensorflow/python/...
    ```
V
Vijay Vasudevan 已提交
240

241
    For a single component e.g. softmax op:
242

243 244 245
    ```bash
    bazel test ${flags} tensorflow/python/kernel_tests:softmax_op_test
    ```
V
Vijay Vasudevan 已提交
246

J
Jim 已提交
247
    For a single/parameterized test e.g. `test_capture_variables` in
248 249
    `tensorflow/python/saved_model/load_test.py`:

B
bhack 已提交
250
    (Requires `python>=3.7`)
251

B
bhack 已提交
252
    ```bash
B
bhack 已提交
253
    bazel test ${flags} //tensorflow/python/saved_model:load_test --test_filter=*LoadTest.test_capture_variables*
B
bhack 已提交
254
    ```
255

256 257 258 259 260 261
    **Note:** You can add `--test_sharding_strategy=disabled` to the `flags` to
    disable the sharding so that all the test outputs are in one file. However,
    it may slow down the tests for not running in parallel and may cause the
    test to timeout but it could be useful when you need to execute a single
    test or more in general your filtered/selected tests have a very low
    execution time and the sharding
J
Jim 已提交
262
    [could create an overhead on the test execution](https://github.com/bazelbuild/bazel/issues/2113#issuecomment-264054799).
V
Vijay Vasudevan 已提交
263

264
2.  Using [Docker](https://www.docker.com) and TensorFlow's CI scripts.
265

266 267 268 269
    ```bash
    # Install Docker first, then this will build and run cpu tests
    tensorflow/tools/ci_build/ci_build.sh CPU bazel test //tensorflow/...
    ```
V
Vijay Vasudevan 已提交
270

271 272 273
    See
    [TensorFlow Builds](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/tools/ci_build)
    for details.
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311

#### Running doctest for testable docstring

There are two ways to test the code in the docstring locally:

1.  If you are only changing the docstring of a class/function/method, then you
    can test it by passing that file's path to
    [tf_doctest.py](https://www.tensorflow.org/code/tensorflow/tools/docs/tf_doctest.py).
    For example:

    ```bash
    python tf_doctest.py --file=<file_path>
    ```

    This will run it using your installed version of TensorFlow. To be sure
    you're running the same code that you're testing:

    *   Use an up to date [tf-nightly](https://pypi.org/project/tf-nightly/)
        `pip install -U tf-nightly`
    *   Rebase your pull request onto a recent pull from
        [TensorFlow's](https://github.com/tensorflow/tensorflow) master branch.

2.  If you are changing the code and the docstring of a class/function/method,
    then you will need to
    [build TensorFlow from source](https://www.tensorflow.org/install/source).
    Once you are setup to build from source, you can run the tests:

    ```bash
    bazel run //tensorflow/tools/docs:tf_doctest
    ```

    or

    ```bash
    bazel run //tensorflow/tools/docs:tf_doctest -- --module=ops.array_ops
    ```

    The `--module` is relative to `tensorflow.python`.
312 313 314

#### Debug builds

A
Abinash Satapathy 已提交
315
When [building Tensorflow](https://www.tensorflow.org/install/source) from source, if `--config=dbg` is passed to Bazel, it will build *with* debugging information and *without* optimizations, allowing you to use GDB or other debuggers to debug C++ code. For
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
example, you can build the pip package with debugging information by running:

```bash
bazel build --config=dbg //tensorflow/tools/pip_package:build_pip_package
```

TensorFlow kernels and TensorFlow's dependencies are still not built with
debugging information with `--config=dbg`, as issues occur on Linux if
there is too much debug info (see [this GitHub
issue](https://github.com/tensorflow/tensorflow/issues/48919) for context). If
you want to debug a kernel, you can compile specific files with `-g` using the
`--per_file_copt` bazel option. For example, if you want to debug the Identity
op, which are in files starting with `identity_op`, you can run

```bash
bazel build --config=dbg --per_file_copt=+tensorflow/core/kernels/identity_op.*@-g //tensorflow/tools/pip_package:build_pip_package
```

A
Abinash Satapathy 已提交
334
> Note: the `--config=dbg` option is not officially supported.