Skip to content

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

Merged
pseudo-rnd-thoughts merged 1 commit into
doc-1926-runner-rllib-stubsfrom
doc-1926-rllib-core-doctests
Nov 4, 2026
Merged

pseudo-rnd-thoughts merged 1 commit into
doc-1926-runner-rllib-stubsfrom
doc-1926-rllib-core-doctests

Conversation

@dstrodtman

@dstrodtman dstrodtman commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

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 ([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 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 20:59
@dstrodtman
dstrodtman requested a review from a team as a code owner November 3, 2026 20:59
@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 converts Sphinx-style testcode blocks into standard Python doctest-style examples across multiple RLlib files. The review feedback highlights several issues within the updated examples, including duplicate imports in base.py, a duplicate keyword argument in configs.py that would trigger a syntax error, unterminated string literals in configs.py, and unused or duplicate imports in rl_module.py.

Comment on lines +34 to +35
>>> from ray.rllib.core.models.configs import ModelConfig
>>> from ray.rllib.core.models.configs import ModelConfig

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.

medium

The import from ray.rllib.core.models.configs import ModelConfig is duplicated here. We should remove the duplicate import to keep the example clean.

Suggested change
>>> from ray.rllib.core.models.configs import ModelConfig
>>> from ray.rllib.core.models.configs import ModelConfig
>>> from ray.rllib.core.models.configs import ModelConfig

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks. This PR leaves example code unchanged, so the fix is in #67766: b3e5697.

Comment on lines +488 to +489
initial_dense_weights_initializer="HeNormal",
initial_dense_weights_initializer={"seed": 334},

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.

medium

The keyword argument initial_dense_weights_initializer is duplicated in the constructor call, which will raise a SyntaxError in Python. The second argument should be initial_dense_weights_initializer_config instead.

Suggested change
initial_dense_weights_initializer="HeNormal",
initial_dense_weights_initializer={"seed": 334},
initial_dense_weights_initializer="HeNormal",
initial_dense_weights_initializer_config={"seed": 334},

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, the duplicate keyword is a bug. This PR leaves example code unchanged, and #67766, stacked on this PR, fixes it differently than suggested: Torch rejects "HeNormal", with "Not an in-place initializer". So #67766 uses "kaiming_normal_", Torch's in-place He-normal initializer, and drops the seed config.

cnn_transpose_use_layernorm=False,
cnn_use_bias=True,
)
model = config.build(framework="torch)

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.

medium

There is an unterminated string literal here: framework="torch). It should be framework="torch".

Suggested change
model = config.build(framework="torch)
model = config.build(framework="torch")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, that's right. This PR is syntax-only on purpose, so it leaves example code unchanged. #67766, stacked on this PR, fixes this.

cnn_transpose_kernel_initializer="xavier_normal_",
cnn_transpose_kernel_initializer_config={"gain": 0.8},
)
model = config.build(framework="torch)

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.

medium

There is an unterminated string literal here: framework="torch). It should be framework="torch".

Suggested change
model = config.build(framework="torch)
model = config.build(framework="torch")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, that's right. This PR is syntax-only on purpose, so it leaves example code unchanged. #67766, stacked on this PR, fixes this.

Comment on lines +334 to +345
>>> from ray.rllib.algorithms.ppo.torch.ppo_torch_rl_module import (
... PPOTorchRLModule
... )
>>> from ray.rllib.algorithms.ppo.ppo_catalog import PPOCatalog
>>> from ray.rllib.core.rl_module.rl_module import RLModuleSpec
>>> from ray.rllib.core.rl_module.default_model_config import DefaultModelConfig
>>> import gymnasium as gym
>>> import torch
>>> from ray.rllib.algorithms.ppo.torch.default_ppo_torch_rl_module import (
... DefaultPPOTorchRLModule
... )
>>> from ray.rllib.algorithms.ppo.ppo_catalog import PPOCatalog

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.

medium

In this training example, several imports are unused (PPOTorchRLModule, RLModuleSpec) or duplicated (PPOCatalog). We can clean this up by only importing what is actually used.

Suggested change
>>> from ray.rllib.algorithms.ppo.torch.ppo_torch_rl_module import (
... PPOTorchRLModule
... )
>>> from ray.rllib.algorithms.ppo.ppo_catalog import PPOCatalog
>>> from ray.rllib.core.rl_module.rl_module import RLModuleSpec
>>> from ray.rllib.core.rl_module.default_model_config import DefaultModelConfig
>>> import gymnasium as gym
>>> import torch
>>> from ray.rllib.algorithms.ppo.torch.default_ppo_torch_rl_module import (
... DefaultPPOTorchRLModule
... )
>>> from ray.rllib.algorithms.ppo.ppo_catalog import PPOCatalog
>>> from ray.rllib.algorithms.ppo.torch.default_ppo_torch_rl_module import (
... DefaultPPOTorchRLModule
... )
>>> from ray.rllib.algorithms.ppo.ppo_catalog import PPOCatalog
>>> from ray.rllib.core.rl_module.default_model_config import DefaultModelConfig
>>> import gymnasium as gym
>>> import torch

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks. This PR leaves example code unchanged, so the fix is in #67766: b3e5697. It also drops the same unused RLModuleSpec import from the sampling example.

Comment on lines +361 to +372
>>> from ray.rllib.algorithms.ppo.torch.ppo_torch_rl_module import (
... PPOTorchRLModule
... )
>>> from ray.rllib.algorithms.ppo.ppo_catalog import PPOCatalog
>>> from ray.rllib.core.rl_module.rl_module import RLModuleSpec
>>> from ray.rllib.core.rl_module.default_model_config import DefaultModelConfig
>>> import gymnasium as gym
>>> import torch
>>> from ray.rllib.algorithms.ppo.torch.default_ppo_torch_rl_module import (
... DefaultPPOTorchRLModule
... )
>>> from ray.rllib.algorithms.ppo.ppo_catalog import PPOCatalog

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.

medium

In this inference example, several imports are unused (PPOTorchRLModule, RLModuleSpec) or duplicated (PPOCatalog). We can clean this up by only importing what is actually used.

Suggested change
>>> from ray.rllib.algorithms.ppo.torch.ppo_torch_rl_module import (
... PPOTorchRLModule
... )
>>> from ray.rllib.algorithms.ppo.ppo_catalog import PPOCatalog
>>> from ray.rllib.core.rl_module.rl_module import RLModuleSpec
>>> from ray.rllib.core.rl_module.default_model_config import DefaultModelConfig
>>> import gymnasium as gym
>>> import torch
>>> from ray.rllib.algorithms.ppo.torch.default_ppo_torch_rl_module import (
... DefaultPPOTorchRLModule
... )
>>> from ray.rllib.algorithms.ppo.ppo_catalog import PPOCatalog
>>> from ray.rllib.algorithms.ppo.torch.default_ppo_torch_rl_module import (
... DefaultPPOTorchRLModule
... )
>>> from ray.rllib.algorithms.ppo.ppo_catalog import PPOCatalog
>>> from ray.rllib.core.rl_module.default_model_config import DefaultModelConfig
>>> import gymnasium as gym
>>> import torch

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks. This PR leaves example code unchanged, so the fix is in #67766: b3e5697.

Convert the `.. testcode::` examples in `python/ray/rllib/core` docstrings to `>>>` examples under `Examples:` headers, so they render in both Sphinx and griffe/MkDocs. Runnable examples now run under the RLlib doctest target, `:skipif: True` examples carry `+SKIP_EXAMPLE`, and examples that don't parse become code blocks. The example code and docstring claims are unchanged apart from format-forced edits.

@rtd-build

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Douglas Strodtman <[email protected]>
@dstrodtman
dstrodtman changed the base branch from master to doc-1926-runner-rllib-stubs November 4, 2026 16:55
@dstrodtman
dstrodtman force-pushed the doc-1926-rllib-core-doctests branch from 1a8f430 to b76aeb0 Compare November 4, 2026 16:55
@pseudo-rnd-thoughts
pseudo-rnd-thoughts merged commit 5a1dfb3 into doc-1926-runner-rllib-stubs Nov 4, 2026
5 checks passed
@pseudo-rnd-thoughts
pseudo-rnd-thoughts deleted the doc-1926-rllib-core-doctests branch November 4, 2026 22:49
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants