{ "cells": [ { "attachments": {}, "cell_type": "markdown", "id": "8c7dab40", "metadata": {}, "source": [ "(docs-contribute)=\n", "\n", "# Contributing to the Ray Documentation\n", "\n", "There are many ways to contribute to the Ray documentation, and we're always looking for new contributors.\n", "Even if you just want to fix a typo or expand on a section, please feel free to do so!\n", "\n", "This document walks you through everything you need to do to get started.\n", "\n", "\n", "## Editorial style\n", "\n", "We follow the [Google developer documentation style guide](https://developers.google.com/style).\n", "Here are some highlights:\n", "\n", "* [Use present tense](https://developers.google.com/style/tense)\n", "* [Use second person](https://developers.google.com/style/person)\n", "* [Use contractions](https://developers.google.com/style/contractions)\n", "* [Use active voice](https://developers.google.com/style/voice)\n", "* [Use sentence case](https://developers.google.com/style/capitalization)\n", "\n", "The editorial style is enforced in CI by Vale. For more information, see \n", "[How to use Vale](vale).\n", "\n", "## Building the Ray documentation\n", "\n", "If you want to contribute to the Ray documentation, you need a way to build it.\n", "Don't install Ray in the environment you plan to use to build documentation. The requirements for the docs build system are generally not compatible with those you need to run Ray itself.\n", "\n", "Follow the these instructions to build the documentation:\n", "\n", "### Fork Ray\n", "1. [Fork the Ray repository](https://docs.ray.io/en/master/ray-contribute/development.html#fork-the-ray-repository)\n", "2. [Clone the forked repository](https://docs.ray.io/en/master/ray-contribute/development.html#fork-the-ray-repository) to your local machine\n", "\n", "Next, change into the `ray/doc` directory:\n", "\n", "```shell\n", "cd ray/doc\n", "```\n", "\n", "### Install dependencies\n", "\n", "If you haven't done so already, create a Python environment, preferably using the latest version of Python. For example, if you're using `conda`:\n", "```shell\n", "conda create -n docs python=3.12\n", "```\n", "Next, activate the Python environment you are using (e.g., venv, conda, etc.). With `conda` this would be:\n", "```shell\n", "conda activate docs\n", "```\n", "Install the documentation dependencies with the following command:\n", "\n", "```shell\n", "pip install -r requirements-doc.txt\n", "```\n", "\n", "Don't use `-U` in this step. You don't want to upgrade dependencies because `requirements-doc.txt` pins exact versions you need to build the docs.\n", "\n", "### Build documentation\n", "Build the documentation by running the following command:\n", "\n", "```shell\n", "make develop\n", "```\n", "\n", "Find the documentation build in the `_build` directory.\n", "After the build finishes, you can simply open the `_build/html/index.html` file in your browser.\n", "It's considered good practice to check the output of your build to make sure everything is working as expected.\n", "\n", "Before committing any changes, make sure you run the \n", "[linter](https://docs.ray.io/en/latest/ray-contribute/getting-involved.html#lint-and-formatting)\n", "with `../scripts/format.sh` from the `doc` folder,\n", "to make sure your changes are formatted correctly.\n", "\n", "## The basics of our build system\n", "\n", "The Ray documentation is built using the [`sphinx`](https://www.sphinx-doc.org/) build system.\n", "We're using the [PyData Sphinx Theme](https://pydata-sphinx-theme.readthedocs.io/en/stable/) for the documentation.\n", "\n", "We use [`myst-parser`](https://myst-parser.readthedocs.io/en/latest/) to allow you to write Ray documentation in either Sphinx's native \n", "[reStructuredText (rST)](https://www.sphinx-doc.org/en/master/usage/restructuredtext/index.html) or in\n", "[Markedly Structured Text (MyST)](https://myst-parser.readthedocs.io/en/latest/).\n", "The two formats can be converted to each other, so the choice is up to you.\n", "Having said that, it's important to know that MyST is\n", "[common markdown compliant](https://myst-parser.readthedocs.io/en/latest/syntax/reference.html#commonmark-block-tokens).\n", "If you intend to add a new document, we recommend starting from an `.md` file.\n", "\n", "The Ray documentation also fully supports executable formats like [Jupyter Notebooks](https://jupyter.org/).\n", "Many of our examples are notebooks with [MyST markdown cells](https://myst-nb.readthedocs.io/en/latest/index.html).\n", "In fact, this very document you're reading _is_ a notebook.\n", "You can check this for yourself by either downloading the `.ipynb` file,\n", "or directly launching this notebook into either Binder or Google Colab in the top navigation bar.\n", "\n", "## What to contribute?\n", "\n", "If you take Ray Tune as an example, you can see that our documentation is made up of several types of documentation,\n", "all of which you can contribute to:\n", "\n", "- [a project landing page](https://docs.ray.io/en/latest/tune/index.html),\n", "- [a getting started guide](https://docs.ray.io/en/latest/tune/getting-started.html),\n", "- [a key concepts page](https://docs.ray.io/en/latest/tune/key-concepts.html),\n", "- [user guides for key features](https://docs.ray.io/en/latest/tune/tutorials/overview.html),\n", "- [practical examples](https://docs.ray.io/en/latest/tune/examples/index.html),\n", "- [a detailed FAQ](https://docs.ray.io/en/latest/tune/faq.html),\n", "- [and API references](https://docs.ray.io/en/latest/tune/api/api.html).\n", "\n", "This structure is reflected in the\n", "[Ray documentation source code](https://github.com/ray-project/ray/tree/master/doc/source/tune) as well, so you\n", "should have no problem finding what you're looking for.\n", "All other Ray projects share a similar structure, but depending on the project there might be minor differences.\n", "\n", "Each type of documentation listed above has its own purpose, but at the end our documentation\n", "comes down to _two types_ of documents:\n", "\n", "- Markup documents, written in MyST or rST. If you don't have a lot of (executable) code to contribute or\n", " use more complex features such as\n", " [tabbed content blocks](https://docs.ray.io/en/latest/ray-core/walkthrough.html#starting-ray), this is the right\n", " choice. Most of the documents in Ray Tune are written in this way, for instance the\n", " [key concepts](https://github.com/ray-project/ray/blob/master/doc/source/tune/key-concepts.rst) or\n", " [API documentation](https://github.com/ray-project/ray/blob/master/doc/source/tune/api/api.rst).\n", "- Notebooks, written in `.ipynb` format. All Tune examples are written as notebooks. These notebooks render in\n", " the browser like `.md` or `.rst` files, but have the added benefit of adding launch buttons to the top of the\n", " document, so that users can run the code themselves in either Binder or Google Colab.\n", "\n", "## Fixing typos and improving explanations\n", "\n", "If you spot a typo in any document, or think that an explanation is not clear enough, please consider\n", "opening a pull request.\n", "In this scenario, just run the linter as described above and submit your pull request.\n", "\n", "## Adding API references\n", "\n", "We use [Sphinx's autodoc extension](https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html) to generate\n", "our API documentation from our source code.\n", "In case we're missing a reference to a function or class, please consider adding it to the respective document in question.\n", "\n", "For example, here's how you can add a function or class reference using `autofunction` and `autoclass`:\n", "\n", "```markdown\n", ".. autofunction:: ray.tune.integration.docker.DockerSyncer\n", "\n", ".. autoclass:: ray.tune.integration.keras.TuneReportCallback\n", "```\n", "\n", "The above snippet was taken from the\n", "[Tune API documentation](https://github.com/ray-project/ray/blob/master/doc/source/tune/api/integration.rst),\n", "which you can look at for reference.\n", "\n", "If you want to change the content of the API documentation, you will have to edit the respective function or class\n", "signatures directly in the source code.\n", "For example, in the above `autofunction` call, to change the API reference for `ray.tune.integration.docker.DockerSyncer`,\n", "you would have to [change the following source file](https://github.com/ray-project/ray/blob/7f1bacc7dc9caf6d0ec042e39499bbf1d9a7d065/python/ray/tune/integration/docker.py#L15-L38).\n", "\n", "To show the usage of APIs, it is important to have small usage examples embedded in the API documentation. These should be self-contained and run out of the box, so a user can copy and paste them into a Python interpreter and play around with them (e.g., if applicable, they should point to example data). Users often rely on these examples to build their applications. To learn more about writing examples, read [How to write code snippets](writing-code-snippets).\n", "\n", "## Adding code to an `.rST` or `.md` file\n", "\n", "Modifying text in an existing documentation file is easy, but you need to be careful when it comes to adding code.\n", "The reason is that we want to ensure every code snippet on our documentation is tested.\n", "This requires us to have a process for including and testing code snippets in documents. To learn how to write testable code \n", "snippets, read [How to write code snippets](writing-code-snippets).\n" ] }, { "cell_type": "code", "execution_count": null, "id": "ba88d95f", "metadata": {}, "outputs": [], "source": [ "# __function_api_start__\n", "from ray import train\n", "\n", "\n", "def objective(x, a, b): # Define an objective function.\n", " return a * (x ** 0.5) + b\n", "\n", "\n", "def trainable(config): # Pass a \"config\" dictionary into your trainable.\n", "\n", " for x in range(20): # \"Train\" for 20 iterations and compute intermediate scores.\n", " score = objective(x, config[\"a\"], config[\"b\"])\n", "\n", " train.report({\"score\": score}) # Send the score to Tune.\n", "\n", "\n", "# __function_api_end__" ] }, { "attachments": {}, "cell_type": "markdown", "id": "6971eeb0", "metadata": {}, "source": [ "This code is imported by `literalinclude` from a file called `doc_code/key_concepts.py`.\n", "Every Python file in the `doc_code` directory will automatically get tested by our CI system,\n", "but make sure to run scripts that you change (or new scripts) locally first.\n", "You do not need to run the testing framework locally.\n", "\n", "In rare situations, when you're adding _obvious_ pseudo-code to demonstrate a concept, it is ok to add it\n", "literally into your `.rST` or `.md` file, e.g. using a `.. code-cell:: python` directive.\n", "But if your code is supposed to run, it needs to be tested.\n", "\n", "## Creating a new document from scratch\n", "\n", "Sometimes you might want to add a completely new document to the Ray documentation, like adding a new\n", "user guide or a new example.\n", "\n", "For this to work, you need to make sure to add the new document explicitly to a parent document's toctree, \n", "which determines the structure of the Ray documentation. See \n", "[the sphinx documentation](https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-toctree) \n", "for more information.\n", "\n", "Depending on the type of document you're adding, you might also have to make changes to an existing overview\n", "page that curates the list of documents in question.\n", "For instance, for Ray Tune each user guide is added to the\n", "[user guide overview page](https://docs.ray.io/en/latest/tune/tutorials/overview.html) as a panel, and the same\n", "goes for [all Tune examples](https://docs.ray.io/en/latest/tune/examples/index.html).\n", "Always check the structure of the Ray sub-project whose documentation you're working on to see how to integrate\n", "it within the existing structure.\n", "In some cases you may be required to choose an image for the panel. Images are located in\n", "`doc/source/images`. \n", "\n", "## Creating a notebook example\n", "\n", "To add a new executable example to the Ray documentation, you can start from our\n", "[MyST notebook template](https://github.com/ray-project/ray/tree/master/doc/source/_templates/template.md) or\n", "[Jupyter notebook template](https://github.com/ray-project/ray/tree/master/doc/source/_templates/template.ipynb).\n", "You could also simply download the document you're reading right now (click on the respective download button at the\n", "top of this page to get the `.ipynb` file) and start modifying it.\n", "All the example notebooks in Ray Tune get automatically tested by our CI system, provided you place them in the\n", "[`examples` folder](https://github.com/ray-project/ray/tree/master/doc/source/tune/examples).\n", "If you have questions about how to test your notebook when contributing to other Ray sub-projects, please make\n", "sure to ask a question in [the Ray community Slack](https://forms.gle/9TSdDYUgxYs8SA9e8) or directly on GitHub,\n", "when opening your pull request.\n", "\n", "To work off of an existing example, you could also have a look at the\n", "[Ray Tune Hyperopt example (`.ipynb`)](https://github.com/ray-project/ray/blob/master/doc/source/tune/examples/hyperopt_example.ipynb)\n", "or the [Ray Serve guide for text classification (`.md`)](https://github.com/ray-project/ray/blob/master/doc/source/serve/tutorials/text-classification.md).\n", "We recommend that you start with an `.md` file and convert your file to an `.ipynb` notebook at the end of the process.\n", "We'll walk you through this process below.\n", "\n", "What makes these notebooks different from other documents is that they combine code and text in one document,\n", "and can be launched in the browser.\n", "We also make sure they are tested by our CI system, before we add them to our documentation.\n", "To make this work, notebooks need to define a _kernel specification_ to tell a notebook server how to interpret\n", "and run the code.\n", "For instance, here's the kernel specification of a Python notebook:\n", "\n", "```markdown\n", "---\n", "jupytext:\n", " text_representation:\n", " extension: .md\n", " format_name: myst\n", "kernelspec:\n", " display_name: Python 3\n", " language: python\n", " name: python3\n", "---\n", "```\n", "\n", "If you write a notebook in `.md` format, you need this YAML front matter at the top of the file.\n", "To add code to your notebook, you can use the `code-cell` directive.\n", "Here's an example:\n", "\n", "````markdown\n", "```python\n", "\n", "import ray\n", "import ray.rllib.agents.ppo as ppo\n", "from ray import serve\n", "\n", "def train_ppo_model():\n", " trainer = ppo.PPOTrainer(\n", " config={\"framework\": \"torch\", \"num_workers\": 0},\n", " env=\"CartPole-v0\",\n", " )\n", " # Train for one iteration\n", " trainer.train()\n", " trainer.save(\"/tmp/rllib_checkpoint\")\n", " return \"/tmp/rllib_checkpoint/checkpoint_000001/checkpoint-1\"\n", "\n", "\n", "checkpoint_path = train_ppo_model()\n", "```\n", "````\n", "\n", "Putting this markdown block into your document will render as follows in the browser:" ] }, { "cell_type": "code", "execution_count": null, "id": "78cac353", "metadata": {}, "outputs": [], "source": [ "import ray\n", "import ray.rllib.agents.ppo as ppo\n", "from ray import serve\n", "\n", "def train_ppo_model():\n", " trainer = ppo.PPOTrainer(\n", " config={\"framework\": \"torch\", \"num_workers\": 0},\n", " env=\"CartPole-v0\",\n", " )\n", " # Train for one iteration\n", " trainer.train()\n", " trainer.save(\"/tmp/rllib_checkpoint\")\n", " return \"/tmp/rllib_checkpoint/checkpoint_000001/checkpoint-1\"\n", "\n", "\n", "checkpoint_path = train_ppo_model()" ] }, { "attachments": {}, "cell_type": "markdown", "id": "d716d0bd", "metadata": {}, "source": [ "### Tags for your notebook\n", "\n", "What makes this work is the `:tags: [hide-cell]` directive in the `code-cell`.\n", "The reason we suggest starting with `.md` files is that it's much easier to add tags to them, as you've just seen.\n", "You can also add tags to `.ipynb` files, but you'll need to start a notebook server for that first, which may\n", "not want to do to contribute a piece of documentation.\n", "\n", "Apart from `hide-cell`, you also have `hide-input` and `hide-output` tags that hide the input and output of a cell.\n", "Also, if you need code that gets executed in the notebook, but you don't want to show it in the documentation,\n", "you can use the `remove-cell`, `remove-input`, and `remove-output` tags in the same way.\n", "\n", "### Testing notebooks\n", "\n", "Removing cells can be particularly interesting for compute-intensive notebooks.\n", "We want you to contribute notebooks that use _realistic_ values, not just toy examples.\n", "At the same time we want our notebooks to be tested by our CI system, and running them should not take too long.\n", "What you can do to address this is to have notebook cells with the parameters you want the users to see first:\n", "\n", "````markdown\n", "```{code-cell} python3\n", "num_workers = 8\n", "num_gpus = 2\n", "```\n", "````\n", "\n", "which will render as follows in the browser:" ] }, { "cell_type": "code", "execution_count": null, "id": "8412103e", "metadata": {}, "outputs": [], "source": [ "num_workers = 8\n", "num_gpus = 2" ] }, { "attachments": {}, "cell_type": "markdown", "id": "1d8cc54e", "metadata": {}, "source": [ "But then in your notebook you follow that up with a _removed_ cell that won't get rendered, but has much smaller\n", "values and make the notebook run faster:\n", "\n", "````markdown\n", "```{code-cell} python3\n", ":tags: [remove-cell]\n", "num_workers = 0\n", "num_gpus = 0\n", "```\n", "````\n", "\n", "### Converting markdown notebooks to ipynb\n", "\n", "Once you're finished writing your example, you can convert it to an `.ipynb` notebook using `jupytext`:\n", "\n", "```shell\n", "jupytext your-example.md --to ipynb\n", "```\n", "\n", "In the same way, you can convert `.ipynb` notebooks to `.md` notebooks with `--to myst`.\n", "And if you want to convert your notebook to a Python file, e.g. to test if your whole script runs without errors,\n", "you can use `--to py` instead.\n", "\n", "(vale)=\n", "\n", "## How to use Vale\n", "### What is Vale?\n", "\n", "[Vale](https://vale.sh/) checks if your writing adheres to the \n", "[Google developer documentation style guide](https://developers.google.com/style).\n", "It's only enforced on the Ray Data documentation.\n", "\n", "Vale catches typos and grammatical errors. It also enforces stylistic rules like \n", "“use contractions” and “use second person.” For the full list of rules, see the \n", "[configuration in the Ray repository](https://github.com/ray-project/ray/tree/master/.vale/styles/Google).\n", "\n", "### How do you run Vale?\n", "\n", "#### How to use the VSCode extension\n", "\n", "1. Install Vale. If you use macOS, use Homebrew.\n", " \n", " ```bash\n", " brew install vale \n", " ```\n", " \n", " Otherwise, use PyPI. \n", " \n", " ```bash\n", " pip install vale\n", " ```\n", "\n", " For more information on installation, see the [Vale documentation](https://vale.sh/docs/vale-cli/installation/).\n", "\n", "2. Install the Vale VSCode extension by following these \n", "[installation instructions](https://marketplace.visualstudio.com/items?itemName=ChrisChinchilla.vale-vscode).\n", "\n", "3. VSCode should show warnings in your code editor and in the “Problems” panel.\n", " \n", " ![Vale](../images/vale.png)\n", " \n", "#### How to run Vale on the command-line\n", "\n", "1. Install Vale. If you use macOS, use Homebrew.\n", " \n", " ```bash\n", " brew install vale \n", " ```\n", " \n", " Otherwise, use PyPI. \n", " \n", " ```bash\n", " pip install vale\n", " ```\n", "\n", " For more information on installation, see the [Vale documentation](https://vale.sh/docs/vale-cli/installation/).\n", "\n", "2. Run Vale in your terminal\n", " \n", " ```bash\n", " vale doc/source/data/overview.rst\n", " ```\n", " \n", "3. Vale should show warnings in your terminal.\n", " \n", " ```\n", " ❯ vale doc/source/data/overview.rst \n", " \n", " doc/source/data/overview.rst\n", " 18:1 warning Try to avoid using Google.We \n", " first-person plural like 'We'. \n", " 18:46 error Did you really mean Vale.Spelling \n", " 'distrbuted'? \n", " 24:10 suggestion In general, use active voice Google.Passive \n", " instead of passive voice ('is \n", " built'). \n", " 28:14 warning Use 'doesn't' instead of 'does Google.Contractions \n", " not'. \n", " \n", " ✖ 1 error, 2 warnings and 1 suggestion in 1 file.\n", " ```\n", " \n", "\n", "### How to handle false Vale.Spelling errors\n", "\n", "To add custom terminology, complete the following steps:\n", "\n", "1. If it doesn’t already exist, create a directory for your team in \n", "`.vale/styles/Vocab`. For example, `.vale/styles/Vocab/Data`.\n", "2. If it doesn’t already exist, create a text file named `accept.txt`. For example, \n", "`.vale/styles/Vocab/Data/accept.txt`.\n", "3. Add your term to `accept.txt`. Vale accepts Regex.\n", "\n", "For more information, see [Vocabularies](https://vale.sh/docs/topics/vocab/) in the Vale \n", "documentation.\n", "\n", "### How to handle false Google.WordList errors\n", "\n", "Vale errors if you use a word that isn't on [Google's word list](https://developers.google.com/style/word-list).\n", "\n", "```\n", " 304:52 error Use 'select' instead of Google.WordList\n", " 'check'.\n", "```\n", "\n", "If you want to use the word anyway, modify the appropriate field in the [WordList configuration](https://github.com/ray-project/ray/blob/81c169bde2414fe4237f3d2f05fc76fccfd52dee/.vale/styles/Google/WordList.yml#L41).\n", "\n", "## Troubleshooting\n", "\n", "If you run into a problem building the docs, following these steps can help isolate or eliminate most issues:\n", "\n", "1. **Clean out build artifacts.** Use `git clean -fdx ./` to clean out docs build artifacts in the working directory. This command does a more complete job than `make clean`. Sphinx uses caching to avoid doing work, and this sometimes causes problems. This is particularly true if you build the docs, then `git pull origin master` to pull in recent changes, and then try to build docs again. Note that this will delete any newly added files that haven't been committed yet, so use with caution.\n", "2. **Check your environment.** Use `pip list` to check the installed dependencies. Compare them to `docs/requirements-doc.txt`. The documentation build system doesn't have the same dependency requirements as Ray. You don't need to run ML models or execute code on distributed systems in order to build the docs. In fact, it's best to use a completely separate docs build environment from the environment you use to run Ray to avoid dependency conflicts. When installing requirements, do `pip install -r docs/requirements-doc.txt`. Don't use `-U` because you don't want to upgrade any dependencies during the installation.\n", "3. **Ensure a modern version of Python.** The docs build system doesn't keep the same dependency and Python version requirements as Ray. Use a modern version of Python when building docs. Newer versions of Python can be substantially faster than preceding versions. Consult for the latest version support information.\n", "4. **Enable breakpoints in Sphinx**. Add -P to the `SPHINXOPTS` in `docs/Makefile` to tell `sphinx` to stop when it encounters a breakpoint, and remove `-j auto` to disable parallel builds. Now you can put breakpoints in the modules you're trying to import, or in `sphinx` code itself, which can help isolate build stubborn build issues.\n", "\n", "## Where to go from here?\n", "\n", "There are many other ways to contribute to Ray other than documentation.\n", "See {ref}`our contributor guide ` for more information." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.6" } }, "nbformat": 4, "nbformat_minor": 5 }