xref: /aosp_15_r20/external/executorch/CONTRIBUTING.md (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1Thank you for your interest in contributing to ExecuTorch! We want to make
2it easy to contribute to this project.
3
4 
5
6## Dev Install
7
8Set up your environment by following the instructions at
9https://pytorch.org/executorch/stable/getting-started-setup.html to clone
10the repo and install the necessary requirements.
11
12 
13
14## Contributing workflow
15We actively welcome your pull requests (PRs).
16
171. [Claim an issue](#claiming-issues), if present, before starting work. If an
18   issue doesn't cover the work you plan to do, consider creating one to provide
19   context about it, and to build consensus about the scope and solution.
201. Create your new branch from `main` in your forked repo, with a name
21   describing the work you're completing; e.g., `add-feature-x`.
221. If you've added code that should be tested, add tests. Ensure all tests pass.
23   See the [testing section](#testing) for more information.
241. If you've changed APIs or added a new tool or feature, [update the
25   documentation](#updating-documentation).
261. If you added an experimental API or deprecated an existing API, follow the
27   [API Life Cycle and Deprecation Policy](/docs/source/api-life-cycle.md).
281. Make sure your code follows the [style guides](#coding-style) and passes the
29   [lint checks](#lintrunner).
301. If you haven't already, complete the [Contributor License Agreement ("CLA")](#contributor-license-agreement-cla).
311. Create a pull request in the `pytorch/executorch` Github repo using the
32   [instructions below](#pull-requests).
33
34 
35
36## Issues
37
38### Creating Issues
39We use GitHub issues to track public bugs and feature requests. Ensure that the
40issue title is clear and descriptive, and that the description has sufficient
41instructions to be able to reproduce the issue.
42
43Meta has a [bounty program](https://www.facebook.com/whitehat/) for the safe
44disclosure of security bugs. In those cases, please go through the process
45outlined on that page and do not file a public issue.
46
47### Claiming Issues
48We'd love your help closing out [open
49issues](https://github.com/pytorch/executorch/issues?q=sort%3Aupdated-desc+is%3Aissue+is%3Aopen)
50in the Github repo.
51
521. Find an issue with the
53   [`actionable`](https://github.com/pytorch/executorch/issues?q=sort%3Aupdated-desc+is%3Aissue+is%3Aopen+label%3Aactionable)
54   or [`good first
55   issue`](https://github.com/pytorch/executorch/issues?q=sort%3Aupdated-desc+is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22)
56   label that is not currently assigned to anyone.
57   - If you'd like to work on an issue that is assigned but hasn't been updated
58     in a while, discuss a hand-off with the current assignee in the issue
59     comments.
60   - If you'd like to work on an issue that isn't marked `actionable`, please
61     comment on the issue to ask about its status and wait for a response.
621. Set yourself as the assignee of the issue.
631. If you decide not to finish the issue, update the issue with information to
64   help the next person, then remove yourself from the assignee list.
651. When creating pull requests (PRs), mention the issue number like `#1234` in
66   the PR description details (the first comment in the PR conversation thread).
671. When the final PR has merged and resolves the issue, close the issue with the
68   button at the bottom of the issue's page.
69
70 
71
72## Coding Style
73
74Goal: Encourage standards that make it easier to read, edit, maintain, and debug
75the ExecuTorch code.
76
77### lintrunner
78
79We use [`lintrunner`](https://pypi.org/project/lintrunner/) to help make sure the
80code follows our standards. Set it up with:
81
82```
83pip install lintrunner==0.11.0
84pip install lintrunner-adapters==0.11.0
85lintrunner init
86```
87
88Then run `lintrunner` from the root of the repo to see its suggestions, or run
89`lintrunner -a` to automatically apply the suggestions.
90
91### Python Style
92
93ExecuTorch Python code follows the style used by the PyTorch core project.
94
95### C++ Style
96
97ExecuTorch code uses the [Google C++
98Style](https://google.github.io/styleguide/cppguide.html), with modifications.
99
100Rationale: Google style is close to the C++ style used by PyTorch core, although
101PyTorch core does not explicitly document its C++ style. Google style is well
102documented, and has exceptional tooling support.
103
104**Modifications** to the Google C++ style, to make it closer to the code in
105PyTorch core:
106- Function and method names should use `lower_snake_case()`. This follows the
107  convention that PyTorch core inherited from its namesake Python, and is the
108  biggest modification to the Google C++ style.
109- File names should use `lower_snake_case.cpp` (not `.cc`, and not
110  `PascalCase.cpp`). This follows the most common pattern in PyTorch core.
111- Headers should use `#pragma once` instead of manual include guards. This
112  follows the most common pattern in PyTorch core.
113- All includes should use `<angle brackets>`, not `"double quotes"`. This
114  ensures that headers are included using the compiler's include path, and not
115  relative to the local file.
116- Documentation comments should follow Doxygen syntax, either `//** ... */`
117  (multi-line) or `/// ...` (single line), with `@`-style parameters like
118  `@param`, `@retval`. Public APIs must be documented in the `.h` files that
119  declare them.
120- TODOs should prefer to reference a task or issue number like `TODO(#123):
121  <description>`, rather than a username. A task can manage much-more-nuanced
122  information, and can change ownership as people leave and join the project.
123
124See the rest of this file for other portability- and efficiency-related
125modifications to the Google C++ style guide.
126
127### C++ Portability Guidelines
128
129See also [Portable C++ Programming](/docs/source/portable-cpp-programming.md)
130for detailed advice.
131
132#### C++ language version
133
134**C++17.**
135
136Rationale: This is a compromise between being compatible with older, proprietary
137toolchains, and having access to relatively modern C++ features.
138
139#### C/C++ standard library usage
140
141**Restricted usage of the C++ standard library.**
142
143Rationale: ExecuTorch is intended to be portable to bare-metal systems that lack
144certain features, like dynamic memory, threading, and locking, required by parts
145of the standard library. It is also intended to be as small as possible, and
146some convenient stdlib features may grow the binary size unacceptably.
147
148Generally, do not instantiate types that allocate memory under the hood, like
149`std::vector` or `std::string`. Do not call `new`, `malloc()` or `mmap()`; do
150not use iostreams; do not operate on files.
151
152However, it is convenient and portable (and sometimes necessary) to use static
153standard library concepts like `std::move`, or metaprogramming helpers like
154`std::is_floating_point<>`.  Pure code like `<cmath>` and `<cstring>` is fine,
155as long as you stay away from functions that allocate memory (like `strdup()`).
156
157It is also allowed (and sometimes necessary) to use "placement `new`", but be
158careful to also manually destroy objects initialized in this way.
159
160#### C++ language features
161
162**Exceptions: Do not use.**
163- Rationale: Exceptions are not widely supported on some classes of
164  microcontrollers and DSPs, and they can significantly increase binary size.
165
166**Threads, thread_local, locking: Do not use, except in optional libraries that
167must work with threading**
168- Rationale: The core runtime must work on systems that do not have threading
169  support.
170
171**RTTI, dynamic_cast, and `<typeid>`: Do not use.**
172- Rationale: RTTI adds extra data to every virtual class. ExecuTorch doesn't
173  have a strong need for `dynamic_cast` and friends, so it's better to reduce
174  the binary size.
175
176**Templates and template metaprogramming: Be careful and avoid if possible.**
177- Rationale: Most templating results in code generation, and is one of the most
178  common sources of binary bloat. Some use of templates is fine (e.g. an
179  `ArrayRef<T>`, or code that handles multiple `ScalarType` types), but for the
180  most part avoid them if possible.
181
182&nbsp;
183
184## Testing
185
186### Writing Tests
187To help keep code quality high, ExecuTorch uses a combination of unit tests and
188end-to-end (e2e) tests. If you add a new feature or fix a bug, please add tests
189to ensure that the feature/fix works properly and continues to work properly.
190
191Most directories in the repo already contain test files. In many cases, you can
192add a test to an existing file, and the existing CI jobs will run it will run
193automatically. If you do this, please take a look at the CI job logs to ensure
194that it did actually run.
195
196If it's not clear how to add a test for your PR, take a look at the blame for
197the code you're modifying and find an author who has more context. Ask them
198for their help in the PR comments.
199
200TODO: Explain how to run tests locally without needing to push and wait for CI.
201
202### Continuous Integration
203See https://hud.pytorch.org/hud/pytorch/executorch/main for the current state of
204the CI (continuous integration) jobs. If `main` is broken, consider rebasing
205your PR onto the `viable/strict` branch, which points to the most recent
206all-green commit.
207
208&nbsp;
209
210## Updating Documentation
211
212### APIs
213ExecuTorch documents its APIs using inline code comments: doc strings for
214Python, and Doxygen comments for C++. When modifying or adding an API, be sure
215to modify or add documentation to the interfaces that you change. If the API
216doesn't have inline documentation yet, please help improve the code by adding
217documentation and describing the rest of the piece you modified.
218
219Also search for references to the API you modified under `docs/source` to see if
220any docs need to be modified to reflect your changes; these are the files that
221are published on https://pytorch.org/executorch. If you are adding a new API,
222look for places in the docs that would benefit from talking about that API, or
223even create a new document for it. A job on the PR will give you a link to a
224website preview based on your changes.
225
226&nbsp;
227
228## Pull Requests
229This repo uses Github pull requests (PRs) to stage and review code before
230merging it into the `main` branch. See the [Github
231docs](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request-from-a-fork)
232for basics.
233
2341. Push your branch to your fork of `pytorch/executorch`. Most people do not
235  have permission to push a branch directoy to the upstream repo.
2361. Create your PR
237   - Use the `main` branch as the base.
238   - Give the PR a clear and descriptive title. It will become the title of the
239     merged commit, so it needs to be useful in the output of `git log`.
240     - Bad title: "Fix a bug"
241     - Good title: "Add XYZ method to ABC"
242   - Give the PR a clear and thorough description. Don't just describe what the PR
243     does: the diff will do that. Explain *why* you are making this change, in a
244     way that will make sense to someone years from now.
245   - Explain how you have tested your changes by including repeatable instructions for
246     testing the PR.
247     - If you added tests, this can be as simple as the command you used to run the
248       tests.
249     - If you tested the PR manually, include the steps and the outputs. Help a
250       future editor understand how to test the code that you're modifying
251       today.
252   - If your PR contains or is representative of a feature/bug fix that should be
253     called out in the release notes, please add a label for "Release notes: \<area\>",
254	 where \<area\> describes which part of ExecuTorch the change pertains to, e.g.
255	 "Release notes: runtime". Here are all of the categories:
256     - `Release notes: runtime`: changes related to the core runtime which loads the program methods, initializes delegates, and runs the lowered graph.
257     - `Release notes: exir`: changes to any internal representations, such as any edge-related dialects. Also any changes to passes that may modify the exir, such as memory planning.
258     - `Release notes: quantization`: changes to quantization.
259     - `Release notes: ops & kernels`: changes to the opset and any new / changed kernel implementations.
260     - `Release notes: api`: changes to public facing apis (any interfaces, pybinded runtime methods, etc.).
261     - `Release notes: backends`: changes to any of the backend delegates.
262     - `Release notes: build`: changes related to the build system, including major dependency upgrades, notable build flags, optimizations, etc.
263     - `Release notes: devtools`: changes to any of ExecuTorch's developer tools, for example the debugger & profiler.
264     - `Release notes: examples`: changes to any code under `examples/`.
265     - `Release notes: misc`: anything notable that doesn't belong in the above categories.
266   - See https://github.com/pytorch/executorch/pull/3612 for an example PR that
267     follows this advice.
2681. Before asking for a review, ensure that all [CI (continuous integration)
269   jobs](#continuous-integration) on your pull request succeed.
270   - If the jobs on your PR are broken but you're not sure why, add a comment
271     and proceed to finding a reviewer.
272   - Not all users can trigger the CI jobs. If the jobs don't run on your PR,
273     proceed to finding a reviewer.
2741. Find reviewers
275   - If you have been working with a member of the ExecuTorch repo, add them
276     as a reviewer (*not* an "assignee").
277   - If not, look at the blame for the files that the PR modifies, and try
278     picking one or two ExecuTorch repo members as reviewers (*not*
279     "assignees").
280   - If you are unsure, leave a comment on the PR and keep it unassigned with no
281     reviewers. A member of the ExecuTorch repo will find someone to review it.
2821. Address and discuss comments left by reviewers
283   - If the reviewers have requests or questions, follow up with them.
284   - The goal of the reviewer is to ensure that the code in the `main` branch of
285     the repo is consistent, maintainable, and of high quality.
2861. Once the PR has been approved,
287   - If you have the "write permission" in this repo, you can merge it yourself
288     by clicking the "Squash and merge" button once it is green and all CI
289     signals are passing.
290   - If you don't have "write permission" in this repo, the reviewer will take
291     care of the PR. The reviewer may import the PR into Meta's internal system
292     to validate it against internal CI.
293   - If the PR is approved but not merged within 5 business days, please comment
294     on the PR to ask about its status.
295   - Note that if the `main` [CI](#continuous-integration) jobs are broken, we
296     will only merge PRs that fix the broken jobs until all critical jobs are
297     fixed.
298
299&nbsp;
300
301## For Backend Delegate Authors
302
303- Use [this](/docs/source/backend-delegates-integration.md) guide when
304  integrating your delegate with ExecuTorch.
305- Refer to [this](/docs/source/backend-delegates-dependencies.md) set of
306  guidelines when including a third-party depenency for your delegate.
307
308&nbsp;
309
310## License
311By contributing to ExecuTorch, you agree that your contributions will be
312licensed under the LICENSE file in the root directory of this source tree.
313
314&nbsp;
315
316## Contributor License Agreement ("CLA")
317In order to accept your pull request, we need you to submit a CLA. You only need
318to do this once to work on any of Meta's open source projects.
319
320Complete your CLA here: <https://code.facebook.com/cla>
321
322&nbsp;
323