Repository navigation
[RLlib] Convert rllib.core docstring examples to stock doctests - #67976
Draft
dstrodtman wants to merge 1 commit into
Draft
dstrodtman wants to merge 1 commit into
dstrodtman wants to merge 1 commit into
Conversation
dstrodtman
force-pushed
the
doc-1926-rllib-core-conversion
branch
from
November 9, 2026 16:54
831bb5d to
901ad88
Compare
> [!NOTE] > Stacked on #67822, which fixes the `docstring-only` runner failures this PR hit on `rllib.core` examples that need torch. Merge #67822 first. GitHub then retargets this PR to `master`. ## Description Converts the `.. testcode::` examples in `python/ray/rllib/core` docstrings to `>>>` examples under a plural `Examples:` header. Sphinx renders `.. testcode::`, but griffe and MkDocs don't, and stock `>>>` examples render in both. This follows #66749 and the `rllib.utils` conversion in #67753. This PR covers all 29 `testcode` blocks in `rllib/core`, in 10 files: - **20 runnable blocks become runnable doctests, in 13 docstrings.** They're in `distribution.py` (2), `models/base.py` (2), `models/catalog.py`, `models/torch/base.py`, `models/configs.py` (7), `default_model_config.py` (2), `rl_module.py` (3), and `learner.py` (2). None of these ran before, because the RLlib `doctest()` target doesn't execute `testcode`. All of them pass now. - **6 never-run blocks stay never-run.** These were `:skipif: True`. Each now sits in a docstring that carries the `# doctest: +SKIP_EXAMPLE` flag (#66753). They're in `TorchCategorical`, `TorchDiagGaussian` (2), `TorchDeterministic`, and `FreeLogStdMLPHeadConfig` (2). - **3 blocks that don't parse as Python become `.. code-block:: python`.** The two `CNNTransposeHeadConfig` examples have an unterminated string, `framework="torch)`. The `InferenceOnlyAPI.get_non_inference_attributes` example is pseudocode (`[some NN component]`). This is a syntax-only change. The example code and the claims in each docstring are unchanged, apart from these format-forced edits: - **Two calls discard their result.** In `Learner`, `learner.add_module(...)` and `learner.remove_module(...)` return a `MultiRLModuleSpec`, which a doctest would echo. They're now assigned to `_`. - **Section headers change.** The `Example:` headers in `models/configs.py` merge into one `Examples:` section per docstring. In `Catalog`, `Usage example:` becomes `Examples:`, and the stray line `# Define a custom catalog` becomes a code comment in the example. As plain docstring text, that line would render as a Markdown heading. - **Intro prose moves into the `Examples:` section.** Lines such as "Example for training:" in `RLModule`, the "Note: We use PPO..." paragraph in `Learner`, and the second paragraph of `DefaultModelConfig` are unchanged, but they're now indented under `Examples:`. - **`Model` gains an `Args` entry.** An `Examples:` section makes pydoclint check the class docstring against `__init__`, and the RLlib baseline is empty. The entry says `config` is the model config, which the model stores as `self.config`. That's what `Model.__init__` does. ### Left for a separate correctness PR Some converted examples run but are wrong, and some never-run examples fail when run. They'll go to RLlib review as a correctness change. For example, `MLPHeadConfig` and `MLPEncoderConfig` build with `framework="tf2"`, which returns `None`. The `RLModule` inference loop never runs. The `Learner` example's `model` settings are ignored. `TorchDiagGaussian` is called without its required `scale` argument. ## Related issues Related to #66749, #66753, and #67753. ## Additional information **Not a duplicate.** I searched open PRs for RLlib `testcode` and doctest conversions, and none touches these docstrings. **Tests run** - `ci/ray_ci/doc/run_docstring_examples.py` on all 10 files, in a Python 3.12 virtualenv with `ray[rllib]==2.59.0`, `torch`, and `tensorflow` 2.21: "13 docstring(s) run, 0 failed, 4 skipped, in 10 file(s)." All four skips are `+SKIP_EXAMPLE`. - A strict stdlib `doctest` pass over the same docstrings with `ELLIPSIS`. Unlike the runner above, it fails on unexpected output, the way the RLlib `doctest()` target does. All 13 runnable docstrings pass. - `ci/ray_ci/doc/check_docstring_only.py --base upstream/master`: "10 file(s), docstrings only." - `pre-commit run --files` on all 10 files: all hooks pass, including pydoclint. The PR carries the `docstring-only` label. Its commit carries an `@rtd-build` line for the Read the Docs preview. AI assistance (Claude Code) was used for this PR. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Signed-off-by: Douglas Strodtman <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
dstrodtman
force-pushed
the
doc-1926-rllib-core-conversion
branch
from
November 9, 2026 21:17
901ad88 to
8753ace
Compare
pseudo-rnd-thoughts
pushed a commit
that referenced
this pull request
Nov 17, 2026
…the rllib doctest target (#68242) ## Why are these changes needed? The scheduled postmerge step `:brain: rllib: docs example tests` has failed on every run since #67774 turned the `rllib.algorithms` docstring examples into doctests. The failing target is `//python/ray/rllib:doctest`. #67834 fixed the `APPOConfig` and `IMPALAConfig` failures, but `BCConfig` still fails, and the target runs out of time partway through `PPOConfig`. See, for example, [postmerge 21027](https://buildkite.com/ray-project/postmerge/builds/21027). The second `BCConfig` example deadlocks on CPUs. It runs a `tune.Tuner` with a `grid_search` over two learning rates, so Tune starts both trials at once. On the doctest cluster the two trials take every CPU. Each trial's offline data pipeline, a `MapBatches(OfflinePreLearner)` actor pool, then never gets a CPU to start. After 1800 seconds, Ray Data's no-progress timeout fails both trials with `ExecutionTimeoutError`. That lost half hour is also why the target runs out of time in `PPOConfig`. This PR passes `tune_config=tune.TuneConfig(max_concurrent_trials=1)` to the example's `Tuner`. The `grid_search` still runs both learning rates, one trial after the other, so the example teaches the same thing. ## Testing Run in a `python:3.12-slim` container (linux/aarch64) capped at 4 CPUs with `docker run --cpus 4`. The container installs the master wheel built at 456e286, the base of this branch, plus CPU torch, pytest, Pillow, and tensorboardX. The command matches what the Bazel target runs, with its env (`RAY_LOG_TO_DRIVER=0`, `TF_USE_LEGACY_KERAS=1`) and the default doctest plugin: ```bash python -m pytest --noconftest --doctest-modules -p default_doctest_pytest_plugin \ ray/rllib/algorithms/bc/bc.py ``` - Before this change: `1 failed in 1811.24s`. Both trials raise `ray.data.exceptions.ExecutionTimeoutError: Dataset execution made no progress for at least 1800s`. While it hung, `ray status` showed `4.0/4.0 CPU (2.0 used of 2.0 reserved in placement groups)` with `{'CPU': 1.0}: 4+ pending tasks/actors`. - After this change: `1 passed in 16.52s`. - `ray/rllib/algorithms/ppo/ppo.py` on its own: `1 passed in 12.84s`. `PPOConfig` doesn't hang. On CI it only ran out of time after the 30-minute `BCConfig` stall. Caveats: the 4-CPU cap is an estimate of what one doctest container gets on the `medium` instance with `--parallelism-per-worker 2`. I haven't confirmed CI's exact per-container CPU count. I also haven't run the full `//python/ray/rllib:doctest` target locally. A scheduled postmerge run will confirm the fix. I also tested adding `algo.stop()` after the first example, because that `Algorithm` stays alive until the module's `ray.shutdown()`. It didn't help, and the Tuner example deadlocked the same way, so this PR leaves it out. ## Duplicate check No open PR or issue touches `rllib/algorithms/bc/bc.py` or this target failure. Searched open PRs for `BCConfig`, `bc.py`, and `rllib doctest`. The open `rllib.core` doctest PRs (#67976, #67766) add examples to the same target but don't change `BCConfig`. ## AI assistance AI assistance (Claude Code) helped with diagnosis, the local reproduction, and drafting. I reviewed the change and the test results. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: Douglas Strodtman <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
|
This pull request has been automatically marked as stale because it has not had You can always ask for help on our discussion forum or Ray's public slack channel. If you'd like to keep this open, just leave any comment, and the stale label will be removed. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note
This is #67765's change, split back out of #67822. #67765 targeted #67822's branch and merged into it, which put a ~900-line RLlib docstring rewrite inside a
[ci]PR. This PR carries the same commit, unchanged, so #67822 stays CI-only and reverting either one doesn't revert the other. #67765 was approved there. It's stacked on #67822, which fixes thedocstring-onlyrunner failures this change hit onrllib.coreexamples that need torch. Merge #67822 first. #67766 is stacked on this PR.Cursor Bugbot flagged two of these examples on #67822 and they're still open: the
Catalogexample builds from a 0-Dgym.spaces.Box(0, 1), and theDistributionexample'sklcall mixes a 256-wide head with a one-classTorchCategorical. Bugbot reports both fail wherever torch is installed, which conflicts with the test run below, so they need a recheck before this merges.Description
Converts the
.. testcode::examples inpython/ray/rllib/coredocstrings to>>>examples under a pluralExamples:header. Sphinx renders.. testcode::, but griffe and MkDocs don't, and stock>>>examples render in both. This follows #66749 and therllib.utilsconversion in #67753.This PR covers all 29
testcodeblocks inrllib/core, in 10 files:distribution.py(2),models/base.py(2),models/catalog.py,models/torch/base.py,models/configs.py(7),default_model_config.py(2),rl_module.py(3), andlearner.py(2). None of these ran before, because the RLlibdoctest()target doesn't executetestcode. All of them pass now.:skipif: True. Each now sits in a docstring that carries the# doctest: +SKIP_EXAMPLEflag ([core] Add a docstring-level +SKIP_EXAMPLE doctest flag for render-only examples #66753). They're inTorchCategorical,TorchDiagGaussian(2),TorchDeterministic, andFreeLogStdMLPHeadConfig(2)... code-block:: python. The twoCNNTransposeHeadConfigexamples have an unterminated string,framework="torch). TheInferenceOnlyAPI.get_non_inference_attributesexample is pseudocode ([some NN component]).This is a syntax-only change. The example code and the claims in each docstring are unchanged, apart from these format-forced edits:
Learner,learner.add_module(...)andlearner.remove_module(...)return aMultiRLModuleSpec, which a doctest would echo. They're now assigned to_.Example:headers inmodels/configs.pymerge into oneExamples:section per docstring. InCatalog,Usage example:becomesExamples:, and the stray line# Define a custom catalogbecomes a code comment in the example. As plain docstring text, that line would render as a Markdown heading.Examples:section. Lines such as "Example for training:" inRLModule, the "Note: We use PPO..." paragraph inLearner, and the second paragraph ofDefaultModelConfigare unchanged, but they're now indented underExamples:.Modelgains anArgsentry. AnExamples:section makes pydoclint check the class docstring against__init__, and the RLlib baseline is empty. The entry saysconfigis the model config, which the model stores asself.config. That's whatModel.__init__does.Left for a separate correctness PR
Some converted examples run but are wrong, and some never-run examples fail when run. They'll go to RLlib review as a correctness change. For example,
MLPHeadConfigandMLPEncoderConfigbuild withframework="tf2", which returnsNone. TheRLModuleinference loop never runs. TheLearnerexample'smodelsettings are ignored.TorchDiagGaussianis called without its requiredscaleargument.Related issues
Related to #66749, #66753, and #67753.
Additional information
Not a duplicate. I searched open PRs for RLlib
testcodeand doctest conversions, and none touches these docstrings.Tests run
ci/ray_ci/doc/run_docstring_examples.pyon all 10 files, in a Python 3.12 virtualenv withray[rllib]==2.59.0,torch, andtensorflow2.21: "13 docstring(s) run, 0 failed, 4 skipped, in 10 file(s)." All four skips are+SKIP_EXAMPLE.doctestpass over the same docstrings withELLIPSIS. Unlike the runner above, it fails on unexpected output, the way the RLlibdoctest()target does. All 13 runnable docstrings pass.ci/ray_ci/doc/check_docstring_only.py --base upstream/master: "10 file(s), docstrings only."pre-commit run --fileson all 10 files: all hooks pass, including pydoclint.The PR carries the
docstring-onlylabel. Its commit carries an@rtd-buildline for the Read the Docs preview.AI assistance (Claude Code) was used for this PR.
🤖 Generated with Claude Code