Skip to content

[RLlib] Convert rllib.core docstring examples to stock doctests - #67976

Draft
dstrodtman wants to merge 1 commit into
doc-1926-runner-rllib-stubsfrom
doc-1926-rllib-core-conversion
Draft

dstrodtman wants to merge 1 commit into
doc-1926-runner-rllib-stubsfrom
doc-1926-rllib-core-conversion

Conversation

@dstrodtman

Copy link
Copy Markdown
Contributor

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 the docstring-only runner failures this change hit on rllib.core examples 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 Catalog example builds from a 0-D gym.spaces.Box(0, 1), and the Distribution example's kl call mixes a 256-wide head with a one-class TorchCategorical. 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 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 ([core] Add a docstring-level +SKIP_EXAMPLE doctest flag for render-only examples #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

@dstrodtman dstrodtman added rllib RLlib related issues go add ONLY when ready to merge, run all tests docstring-only Skip heavy library tests for Ray API docstring edits. labels Nov 9, 2026
@dstrodtman
dstrodtman added this pull request to stack #67977 November 9, 2026 14:54
@dstrodtman
dstrodtman removed this pull request from stack #67977 November 9, 2026 16:53
@dstrodtman
dstrodtman force-pushed the doc-1926-rllib-core-conversion branch from 831bb5d to 901ad88 Compare November 9, 2026 16:54
@dstrodtman
dstrodtman added this pull request to stack #67980 November 9, 2026 16:54
> [!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
dstrodtman force-pushed the doc-1926-rllib-core-conversion branch from 901ad88 to 8753ace Compare November 9, 2026 21:17
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]>
@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had
any activity for 14 days. It will be closed in another 14 days if no further activity occurs.
Thank you for your contributions.

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.

@github-actions github-actions Bot added the stale The issue is stale. It will be closed within 7 days unless there are further conversation label Nov 24, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docstring-only Skip heavy library tests for Ray API docstring edits. go add ONLY when ready to merge, run all tests rllib RLlib related issues stale The issue is stale. It will be closed within 7 days unless there are further conversation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant