Skip to content

[RLlib] Fix broken rllib.core docstring examples and make them run - #67766

Open
dstrodtman wants to merge 2 commits into
doc-1926-rllib-core-conversionfrom
doc-1926-rllib-core-example-fixes
Open

dstrodtman wants to merge 2 commits into
doc-1926-rllib-core-conversionfrom
doc-1926-rllib-core-example-fixes

Conversation

@dstrodtman

@dstrodtman dstrodtman commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Description

This is a correctness change for RLlib review. It fixes rllib.core docstring examples that were wrong or couldn't run, and it turns the CNNTransposeHeadConfig code blocks back into doctests. After this PR, no testcode or testoutput remains in rllib/core.

Stacked on #67976, which carries #67765's change after it was split back out of #67822. Review that PR first. This diff is against its branch.

Examples that were wrong and now run

  • MLPHeadConfig and MLPEncoderConfig: one example in each built with framework="tf2". build() returns a model only for "torch", so model was None. Both now build with "torch", and the built stacks match the pseudocode comments.
  • FreeLogStdMLPHeadConfig: the first example had the same "tf2" problem. Both pseudocode stacks were wrong. The head always adds an output layer with half of output_dims as outputs. So the stacks now read Linear(16, 4) with a (4,) free log-std, and Linear(100, 50) with a (50,) free log-std. +SKIP_EXAMPLE is gone.
  • CNNTransposeHeadConfig: both examples were code blocks, because framework="torch) didn't parse. The first example also passed initial_dense_weights_initializer twice and used TensorFlow's "HeNormal", which Torch rejects. Both examples passed cnn_use_bias, which isn't a field. They now use kaiming_normal_ and cnn_transpose_use_bias, and they run. The pseudocode now matches the built model: Linear(10, 4*4*96), a layer norm over the channels axis, and a last layer that always uses a bias.
  • CNNEncoderConfig: the pseudocode listed a third Conv2D that the built model doesn't have, so it's gone.
  • TorchDeterministic: the output now matches the real repr, so the example runs.
  • Learner: .training(model={"fcnet_hiddens": [128, 128]}) has no effect on the new API stack, so the learner built the default 256x256 encoder. The example now uses .rl_module(model_config=DefaultModelConfig(fcnet_hiddens=[128, 128])).
  • RLModule: the inference example's loop never ran, because terminated was still True from the sampling example and its new env was never reset. It now resets the env and gets the action distribution class from its own module. The training example also resets its env.

Fixed but still render-only

These keep +SKIP_EXAMPLE because they print random samples. With the flag removed, every call succeeds, and only the sample values differ.

  • TorchCategorical: the example passed the probabilities positionally, which sets logits, so it now passes probs=. The output was tensor([3, 4]), but 4 isn't a valid class. It now shows a real sample.
  • TorchDiagGaussian: the "scale is None" example raised TypeError, because scale is required, and passing scale=None raises ValueError in torch.distributions.Normal. This PR removes that example and the loc description's claim about a None scale. The output now shows a real sample.

InferenceOnlyAPI.get_non_inference_attributes stays a python code block, because it's pseudocode.

Review fixes

These address gemini-code-assist comments on #67765:

  • Model: the example imported ModelConfig twice.
  • RLModule: the training and inference examples imported PPOTorchRLModule and RLModuleSpec without using them and imported PPOCatalog twice. The sampling example also imported RLModuleSpec without using it. The unused and duplicate imports are gone.

Related issues

Stacked on #67976 (#67765's change). Related to #66753.

Additional information

Not a duplicate. I searched open PRs for RLlib testcode, doctest, and docstring-example changes, and none touches these docstrings.

Tests run

These ran in a Python 3.12 virtualenv with ray[rllib]==2.59.0, torch, and tensorflow 2.21:

  • ci/ray_ci/doc/run_docstring_examples.py on all 5 changed files: "11 docstring(s) run, 0 failed, 2 skipped, in 5 file(s)."
  • 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 11 runnable docstrings pass, and the other 2 carry +SKIP_EXAMPLE.
  • The TorchCategorical and TorchDiagGaussian examples with +SKIP_EXAMPLE removed: every call succeeds, and only the random sample values differ.
  • ci/ray_ci/doc/check_docstring_only.py --base doc-1926-rllib-core-doctests: "5 file(s), docstrings only."
  • pre-commit run --files on all 5 files: all hooks pass.

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 docstring-only Skip heavy library tests for Ray API docstring edits. labels Nov 3, 2026
@dstrodtman
dstrodtman marked this pull request as ready for review November 3, 2026 21:00
@dstrodtman
dstrodtman requested a review from a team as a code owner November 3, 2026 21:00
@dstrodtman dstrodtman added the go add ONLY when ready to merge, run all tests label Nov 3, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates and corrects various docstring examples and doctests across RLlib's core modules, including torch_distribution.py, learner.py, configs.py, and rl_module.py. The changes focus on switching example frameworks from TensorFlow to PyTorch, correcting pseudocode layer dimensions, and adding missing setup code (such as environment resets) to ensure the doctests are valid and executable. There are no review comments to address, and the changes look correct.

@dstrodtman
dstrodtman force-pushed the doc-1926-rllib-core-example-fixes branch from b3e5697 to b735b5a Compare November 4, 2026 16:55
Base automatically changed from doc-1926-rllib-core-doctests to doc-1926-runner-rllib-stubs November 4, 2026 22:49
@dstrodtman
dstrodtman force-pushed the doc-1926-rllib-core-example-fixes branch from b735b5a to 408213e Compare November 5, 2026 13:28
@dstrodtman
dstrodtman requested review from a team as code owners November 9, 2026 14:53
@dstrodtman
dstrodtman removed this pull request from stack #67870 November 9, 2026 14:54
@dstrodtman
dstrodtman changed the base branch from doc-1926-runner-rllib-stubs to doc-1926-rllib-core-conversion November 9, 2026 14:54
@dstrodtman
dstrodtman force-pushed the doc-1926-rllib-core-example-fixes branch from 408213e to 150cb2e Compare November 9, 2026 14:54
@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-example-fixes branch from 150cb2e to 0e5a05b Compare November 9, 2026 16:54
@dstrodtman
dstrodtman added this pull request to stack #67980 November 9, 2026 16:54
dstrodtman and others added 2 commits November 9, 2026 16:16
Fix `rllib.core` docstring examples that were wrong or couldn't run: TF builds that silently returned `None`, a `CNNTransposeHeadConfig` example that didn't parse, a `TorchDiagGaussian` example without the required `scale`, a learner config whose model settings were ignored, and an `RLModule` inference loop that never ran. Fix pseudocode comments that didn't match the built models. Convert the last code blocks to runnable doctests, so no `testcode` or `testoutput` remains in `rllib/core`.

@rtd-build

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Douglas Strodtman <[email protected]>
Fixes issues gemini-code-assist found in the converted examples: unused or
duplicated imports and variables, example code that mutates shared state or
drops data, and a typo.

@rtd-build

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Douglas Strodtman <[email protected]>
@dstrodtman
dstrodtman force-pushed the doc-1926-rllib-core-example-fixes branch from 0e5a05b to 9a33f92 Compare November 9, 2026 21:16
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.

2 participants