1*523fa7a6SAndroid Build Coastguard Worker# ExecuTorch Documentation 2*523fa7a6SAndroid Build Coastguard Worker 3*523fa7a6SAndroid Build Coastguard WorkerWelcome to the ExecuTorch documentation! This README.md will provide an overview 4*523fa7a6SAndroid Build Coastguard Workerof the ExecuTorch docs and its features, as well as instructions on how to 5*523fa7a6SAndroid Build Coastguard Workercontribute and build locally. 6*523fa7a6SAndroid Build Coastguard Worker 7*523fa7a6SAndroid Build Coastguard WorkerAll current documentation is located in the `docs/source` directory. 8*523fa7a6SAndroid Build Coastguard Worker 9*523fa7a6SAndroid Build Coastguard Worker<!-- toc --> 10*523fa7a6SAndroid Build Coastguard Worker 11*523fa7a6SAndroid Build Coastguard Worker- [Toolchain Overview](#toolchain-overview) 12*523fa7a6SAndroid Build Coastguard Worker- [Building Locally](#building-locally) 13*523fa7a6SAndroid Build Coastguard Worker- [Using Custom Variables](#using-custom-variables) 14*523fa7a6SAndroid Build Coastguard Worker- [Including READMEs to the Documentation Build](#including-readmes-to-the-documentation-build) 15*523fa7a6SAndroid Build Coastguard Worker- [Contributing](#contributing) 16*523fa7a6SAndroid Build Coastguard Worker- [Adding Tutorials](#adding-tutorials) 17*523fa7a6SAndroid Build Coastguard Worker- [Auto-generated API documentation](#auto-generated-api-documentation) 18*523fa7a6SAndroid Build Coastguard Worker - [Python APIs](#python-apis) 19*523fa7a6SAndroid Build Coastguard Worker - [C++ APIs](#c-apis) 20*523fa7a6SAndroid Build Coastguard Worker <!-- tocstop --> 21*523fa7a6SAndroid Build Coastguard Worker 22*523fa7a6SAndroid Build Coastguard Worker## Toolchain Overview 23*523fa7a6SAndroid Build Coastguard Worker 24*523fa7a6SAndroid Build Coastguard WorkerWe are using [sphinx](https://www.sphinx-doc.org/en/master/) with 25*523fa7a6SAndroid Build Coastguard Worker[myst_parser](https://myst-parser.readthedocs.io/en/latest/), 26*523fa7a6SAndroid Build Coastguard Worker[sphinx-gallery](https://sphinx-gallery.github.io/stable/index.html), and 27*523fa7a6SAndroid Build Coastguard Worker[sphinx_design](https://sphinx-design.readthedocs.io/en/latest/) in this 28*523fa7a6SAndroid Build Coastguard Workerdocumentation set. 29*523fa7a6SAndroid Build Coastguard Worker 30*523fa7a6SAndroid Build Coastguard WorkerWe support both `.rst` and `.md` files but prefer the content to be authored in 31*523fa7a6SAndroid Build Coastguard Worker`.md` as much as possible. 32*523fa7a6SAndroid Build Coastguard Worker 33*523fa7a6SAndroid Build Coastguard Worker## Building Locally 34*523fa7a6SAndroid Build Coastguard Worker 35*523fa7a6SAndroid Build Coastguard WorkerDocumentation dependencies are stored in 36*523fa7a6SAndroid Build Coastguard Worker[.ci/docker/requirements-ci.txt](https://github.com/pytorch/executorch/blob/main/.ci/docker/requirements-ci.txt). 37*523fa7a6SAndroid Build Coastguard Worker 38*523fa7a6SAndroid Build Coastguard WorkerTo build the documentation locally: 39*523fa7a6SAndroid Build Coastguard Worker 40*523fa7a6SAndroid Build Coastguard Worker1. Clone the ExecuTorch repo to your machine. 41*523fa7a6SAndroid Build Coastguard Worker 42*523fa7a6SAndroid Build Coastguard Worker1. If you don't have it already, start a conda environment: 43*523fa7a6SAndroid Build Coastguard Worker 44*523fa7a6SAndroid Build Coastguard Worker ```{note} 45*523fa7a6SAndroid Build Coastguard Worker The below command generates a completely new environment and resets 46*523fa7a6SAndroid Build Coastguard Worker any existing dependencies. If you have an environment already, skip 47*523fa7a6SAndroid Build Coastguard Worker the `conda create` command. 48*523fa7a6SAndroid Build Coastguard Worker ``` 49*523fa7a6SAndroid Build Coastguard Worker 50*523fa7a6SAndroid Build Coastguard Worker ```bash 51*523fa7a6SAndroid Build Coastguard Worker conda create -yn executorch python=3.10.0 52*523fa7a6SAndroid Build Coastguard Worker conda activate executorch 53*523fa7a6SAndroid Build Coastguard Worker ``` 54*523fa7a6SAndroid Build Coastguard Worker 55*523fa7a6SAndroid Build Coastguard Worker1. Install dependencies: 56*523fa7a6SAndroid Build Coastguard Worker 57*523fa7a6SAndroid Build Coastguard Worker ```bash 58*523fa7a6SAndroid Build Coastguard Worker pip3 install -r ./.ci/docker/requirements-ci.txt 59*523fa7a6SAndroid Build Coastguard Worker ``` 60*523fa7a6SAndroid Build Coastguard Worker1. Update submodules 61*523fa7a6SAndroid Build Coastguard Worker 62*523fa7a6SAndroid Build Coastguard Worker ```bash 63*523fa7a6SAndroid Build Coastguard Worker git submodule sync && git submodule update --init 64*523fa7a6SAndroid Build Coastguard Worker ``` 65*523fa7a6SAndroid Build Coastguard Worker1. Run: 66*523fa7a6SAndroid Build Coastguard Worker 67*523fa7a6SAndroid Build Coastguard Worker ```bash 68*523fa7a6SAndroid Build Coastguard Worker bash install_requirements.sh 69*523fa7a6SAndroid Build Coastguard Worker ``` 70*523fa7a6SAndroid Build Coastguard Worker 71*523fa7a6SAndroid Build Coastguard Worker1. Go to the `docs/` directory. 72*523fa7a6SAndroid Build Coastguard Worker 73*523fa7a6SAndroid Build Coastguard Worker1. Build the documentation set: 74*523fa7a6SAndroid Build Coastguard Worker 75*523fa7a6SAndroid Build Coastguard Worker ``` 76*523fa7a6SAndroid Build Coastguard Worker make html 77*523fa7a6SAndroid Build Coastguard Worker ``` 78*523fa7a6SAndroid Build Coastguard Worker 79*523fa7a6SAndroid Build Coastguard Worker This should build both documentation and tutorials. The build will be placed 80*523fa7a6SAndroid Build Coastguard Worker in the `_build` directory. 81*523fa7a6SAndroid Build Coastguard Worker 82*523fa7a6SAndroid Build Coastguard Worker1. You can preview locally by using 83*523fa7a6SAndroid Build Coastguard Worker [sphinx-serve](https://pypi.org/project/sphinx-serve/). To install 84*523fa7a6SAndroid Build Coastguard Worker sphinx-serve, run: `pip3 install sphinx-serve`. To serve your documentation: 85*523fa7a6SAndroid Build Coastguard Worker 86*523fa7a6SAndroid Build Coastguard Worker ``` 87*523fa7a6SAndroid Build Coastguard Worker sphinx-serve -b _build 88*523fa7a6SAndroid Build Coastguard Worker ``` 89*523fa7a6SAndroid Build Coastguard Worker 90*523fa7a6SAndroid Build Coastguard Worker Open http://0.0.0.0:8081/ in your browser to preview your updated 91*523fa7a6SAndroid Build Coastguard Worker documentation. 92*523fa7a6SAndroid Build Coastguard Worker 93*523fa7a6SAndroid Build Coastguard Worker## Using Custom Variables 94*523fa7a6SAndroid Build Coastguard Worker 95*523fa7a6SAndroid Build Coastguard WorkerYou can use custom variables in your `.md` and `.rst` files. The variables take 96*523fa7a6SAndroid Build Coastguard Workertheir values from the files listed in the `./.ci/docker/ci_commit_pins/` 97*523fa7a6SAndroid Build Coastguard Workerdirectory. For example, to insert a variable that specifies the latest PyTorch 98*523fa7a6SAndroid Build Coastguard Workerversion, use the following syntax: 99*523fa7a6SAndroid Build Coastguard Worker 100*523fa7a6SAndroid Build Coastguard Worker``` 101*523fa7a6SAndroid Build Coastguard WorkerThe current version of PyTorch is ${executorch_version:pytorch}. 102*523fa7a6SAndroid Build Coastguard Worker``` 103*523fa7a6SAndroid Build Coastguard Worker 104*523fa7a6SAndroid Build Coastguard WorkerThis will result in the following output: 105*523fa7a6SAndroid Build Coastguard Worker 106*523fa7a6SAndroid Build Coastguard Worker<img src="./source/_static/img/s_custom_variables_extension.png" width="300"> 107*523fa7a6SAndroid Build Coastguard Worker 108*523fa7a6SAndroid Build Coastguard WorkerRight now we only support PyTorch version as custom variable, but will support others in the future. 109*523fa7a6SAndroid Build Coastguard Worker 110*523fa7a6SAndroid Build Coastguard WorkerYou can use the variables in both regular text and code blocks. 111*523fa7a6SAndroid Build Coastguard Worker 112*523fa7a6SAndroid Build Coastguard Worker## Including READMEs to the Documentation Build 113*523fa7a6SAndroid Build Coastguard Worker 114*523fa7a6SAndroid Build Coastguard WorkerYou might want to include some of the `README.md` files from various directories 115*523fa7a6SAndroid Build Coastguard Workerin this repositories in your documentation build. To do that, create an `.md` 116*523fa7a6SAndroid Build Coastguard Workerfile and use the `{include}` directive to insert your `.md` files. Example: 117*523fa7a6SAndroid Build Coastguard Worker 118*523fa7a6SAndroid Build Coastguard Worker```` 119*523fa7a6SAndroid Build Coastguard Worker```{include} ../README.md 120*523fa7a6SAndroid Build Coastguard Worker```` 121*523fa7a6SAndroid Build Coastguard Worker 122*523fa7a6SAndroid Build Coastguard Worker**NOTE:** Many `README.md` files are written as placeholders with limited 123*523fa7a6SAndroid Build Coastguard Workerinformation provided. Some of that content you might want to keep in the 124*523fa7a6SAndroid Build Coastguard Workerrepository rather than on the website. If you still want to add it, make sure to 125*523fa7a6SAndroid Build Coastguard Workercheck the content for accuracy, structure, and overall quality. 126*523fa7a6SAndroid Build Coastguard Worker 127*523fa7a6SAndroid Build Coastguard Worker## Contributing 128*523fa7a6SAndroid Build Coastguard Worker 129*523fa7a6SAndroid Build Coastguard WorkerUse the 130*523fa7a6SAndroid Build Coastguard Worker[PyTorch contributing guidelines](https://github.com/pytorch/pytorch/blob/main/CONTRIBUTING.md#writing-documentation) 131*523fa7a6SAndroid Build Coastguard Workerto contribute to the documentation. 132*523fa7a6SAndroid Build Coastguard Worker 133*523fa7a6SAndroid Build Coastguard WorkerIn addition to that, see 134*523fa7a6SAndroid Build Coastguard Worker[Markdown in Sphinx Tips and Tricks](https://pytorch.org/executorch/markdown-sphinx-tips-tricks.html) 135*523fa7a6SAndroid Build Coastguard Workerfor tips on how to author high-quality markdown pages with Myst Parser. 136*523fa7a6SAndroid Build Coastguard Worker 137*523fa7a6SAndroid Build Coastguard Worker## Adding Tutorials 138*523fa7a6SAndroid Build Coastguard Worker 139*523fa7a6SAndroid Build Coastguard WorkerYou can add both interactive (`.py`) and non-interactive tutorials (`.md`) to 140*523fa7a6SAndroid Build Coastguard Workerthis documentation. All tutorials should go to the `tutorials_source/` 141*523fa7a6SAndroid Build Coastguard Workerdirectory. Use one of the following templates: 142*523fa7a6SAndroid Build Coastguard Worker 143*523fa7a6SAndroid Build Coastguard Worker- [Python Template](https://github.com/pytorch/executorch/blob/main/docs/source/tutorials_source/template_tutorial.py) 144*523fa7a6SAndroid Build Coastguard Worker- [Markdown template](https://github.com/pytorch/executorch/blob/main/docs/source/tutorial-template.md) 145*523fa7a6SAndroid Build Coastguard Worker 146*523fa7a6SAndroid Build Coastguard WorkerAfter creating a tutorial, make sure to add the corresponding path in the 147*523fa7a6SAndroid Build Coastguard Worker[index.rst](./source/index.rst) file in the following places: 148*523fa7a6SAndroid Build Coastguard Worker 149*523fa7a6SAndroid Build Coastguard Worker- In the 150*523fa7a6SAndroid Build Coastguard Worker [tutorials torctree](https://github.com/pytorch/executorch/blob/main/docs/source/index.rst?plain=1#L183) 151*523fa7a6SAndroid Build Coastguard Worker- In the 152*523fa7a6SAndroid Build Coastguard Worker [customcard section](https://github.com/pytorch/executorch/blob/main/docs/source/index.rst?plain=1#L201) 153*523fa7a6SAndroid Build Coastguard Worker 154*523fa7a6SAndroid Build Coastguard WorkerIf you want to include a Markdown tutorial that is stored in another directory 155*523fa7a6SAndroid Build Coastguard Workeroutside of the `docs/source` directory, complete the following steps: 156*523fa7a6SAndroid Build Coastguard Worker 157*523fa7a6SAndroid Build Coastguard Worker1. Create an `.md` file under `source/tutorials_source`. Name that file after 158*523fa7a6SAndroid Build Coastguard Worker your tutorial. 159*523fa7a6SAndroid Build Coastguard Worker2. Include the following in that file: 160*523fa7a6SAndroid Build Coastguard Worker 161*523fa7a6SAndroid Build Coastguard Worker ```` 162*523fa7a6SAndroid Build Coastguard Worker ```{include} ../path-to-your-file/outside-of-the-docs-dir.md``` 163*523fa7a6SAndroid Build Coastguard Worker ```` 164*523fa7a6SAndroid Build Coastguard Worker 165*523fa7a6SAndroid Build Coastguard Worker **NOTE:** Your tutorial source file needs to follow the tutorial template. 166*523fa7a6SAndroid Build Coastguard Worker 167*523fa7a6SAndroid Build Coastguard Worker3. Add the file that you have created in **Step 1** to the `index.rst` toctree 168*523fa7a6SAndroid Build Coastguard Worker and add a `customcarditem` with the link to that file. 169*523fa7a6SAndroid Build Coastguard Worker 170*523fa7a6SAndroid Build Coastguard WorkerFor example, if I wanted to include the `README.md` file from 171*523fa7a6SAndroid Build Coastguard Worker`examples/selective_build` as a tutorial under 172*523fa7a6SAndroid Build Coastguard Worker`pytorch.org/executorch/tutorials`, I could create a file called 173*523fa7a6SAndroid Build Coastguard Worker`tutorials_source/selective-build-tutorial.md` and add the following to that 174*523fa7a6SAndroid Build Coastguard Workerfile: 175*523fa7a6SAndroid Build Coastguard Worker 176*523fa7a6SAndroid Build Coastguard Worker```` 177*523fa7a6SAndroid Build Coastguard Worker```{include} ../../../examples/selective_build/README.md 178*523fa7a6SAndroid Build Coastguard Worker```` 179*523fa7a6SAndroid Build Coastguard Worker 180*523fa7a6SAndroid Build Coastguard WorkerIn the `index.rst` file, I would add `tutorials/selective-build-tutorial` in 181*523fa7a6SAndroid Build Coastguard Workerboth the `toctree` and the `cusotmcarditem` sections. 182*523fa7a6SAndroid Build Coastguard Worker 183*523fa7a6SAndroid Build Coastguard Worker# Auto-generated API documentation 184*523fa7a6SAndroid Build Coastguard Worker 185*523fa7a6SAndroid Build Coastguard WorkerWe use Sphinx to generate both Python and C++ documentation in the form of HTML 186*523fa7a6SAndroid Build Coastguard Workerpages. 187*523fa7a6SAndroid Build Coastguard Worker 188*523fa7a6SAndroid Build Coastguard Worker### Python APIs 189*523fa7a6SAndroid Build Coastguard Worker 190*523fa7a6SAndroid Build Coastguard WorkerWe generate Python API documentation through Sphinx and `sphinx.ext.autodoc`. 191*523fa7a6SAndroid Build Coastguard Worker 192*523fa7a6SAndroid Build Coastguard WorkerThe setup for Python documentation lies within `source/`. Sphinx uses the 193*523fa7a6SAndroid Build Coastguard Worker`conf.py` configuration file where `sphinx.ext.autodoc` is configured as 194*523fa7a6SAndroid Build Coastguard Workerextension. During the build, Sphinx generates the API documentation from the 195*523fa7a6SAndroid Build Coastguard Workerdocstrings defined in your Python files. 196*523fa7a6SAndroid Build Coastguard Worker 197*523fa7a6SAndroid Build Coastguard WorkerTo define which API documentation to generate, you need to set up `.rst` files 198*523fa7a6SAndroid Build Coastguard Workerthat reference the modules you want to build documentation for. To auto-generate 199*523fa7a6SAndroid Build Coastguard WorkerAPIs for a specific module, the `automodule` tag is needed to tell Sphinx what 200*523fa7a6SAndroid Build Coastguard Workerspecific module to document. For example, if we wanted a page to display 201*523fa7a6SAndroid Build Coastguard Workerauto-generated documentation for everything in `exir/__init__.py` (relative to 202*523fa7a6SAndroid Build Coastguard Workerthe root of the repo), the RST file would look something like the following: 203*523fa7a6SAndroid Build Coastguard Worker 204*523fa7a6SAndroid Build Coastguard Worker``` 205*523fa7a6SAndroid Build Coastguard Workerexecutorch.exir 206*523fa7a6SAndroid Build Coastguard Worker======================= 207*523fa7a6SAndroid Build Coastguard Worker 208*523fa7a6SAndroid Build Coastguard Worker.. automodule:: exir 209*523fa7a6SAndroid Build Coastguard Worker :members: 210*523fa7a6SAndroid Build Coastguard Worker :undoc-members: 211*523fa7a6SAndroid Build Coastguard Worker :show-inheritance: 212*523fa7a6SAndroid Build Coastguard Worker``` 213*523fa7a6SAndroid Build Coastguard Worker 214*523fa7a6SAndroid Build Coastguard WorkerThese separate `.rst` files should all be linked together, with the initial 215*523fa7a6SAndroid Build Coastguard Workerlanding page under `index.rst`. 216*523fa7a6SAndroid Build Coastguard Worker 217*523fa7a6SAndroid Build Coastguard Worker### C++ APIs 218*523fa7a6SAndroid Build Coastguard Worker 219*523fa7a6SAndroid Build Coastguard WorkerFollowing Pytorch's way of generating C++ documentation, we generate C++ API 220*523fa7a6SAndroid Build Coastguard Workerdocumentation through Doxygen, which is then converted into 221*523fa7a6SAndroid Build Coastguard Worker[Sphinx](http://www.sphinx-doc.org/) using 222*523fa7a6SAndroid Build Coastguard Worker[Breathe](https://github.com/michaeljones/breathe). 223*523fa7a6SAndroid Build Coastguard Worker 224*523fa7a6SAndroid Build Coastguard WorkerSpecifically, we use Doxygen to generate C++ documentation in the form of XML 225*523fa7a6SAndroid Build Coastguard Workerfiles, and through configs set in Sphinx's `conf.py` file, we use Breathe and 226*523fa7a6SAndroid Build Coastguard WorkerExhale to use the XML files and generate RST files which are then used to 227*523fa7a6SAndroid Build Coastguard Workergenerate HTML files. 228*523fa7a6SAndroid Build Coastguard Worker 229*523fa7a6SAndroid Build Coastguard WorkerTo configure Doxygen, we can run `doxygen Doxyfile` in the root of our 230*523fa7a6SAndroid Build Coastguard Workerrepository (ex. `docs/source`) which will generate a `Doxyfile` containing 231*523fa7a6SAndroid Build Coastguard Workerconfigurations for generating c++ documentation. Specifically, the most 232*523fa7a6SAndroid Build Coastguard Workerimportant/relevant parts are: 233*523fa7a6SAndroid Build Coastguard Worker 234*523fa7a6SAndroid Build Coastguard Worker- `OUTPUT_DIRECTORY` specifies where to output the auto-generated XML files 235*523fa7a6SAndroid Build Coastguard Worker- `INPUT` specifies which files to generate documenation for 236*523fa7a6SAndroid Build Coastguard Worker- `GENERATE_XML = YES` 237*523fa7a6SAndroid Build Coastguard Worker 238*523fa7a6SAndroid Build Coastguard WorkerIf you need to include new files, simply add them to the `INPUT` in the 239*523fa7a6SAndroid Build Coastguard Worker`Doxyfile`. The generated output is included to the ExecuTorch documentation 240*523fa7a6SAndroid Build Coastguard Workerbuild and referenced in `index.rst`. 241