Repository navigation
[RLlib] Convert rllib.core docstring examples to stock doctests - #67765
pseudo-rnd-thoughts merged 1 commit into
Conversation
There was a problem hiding this comment.
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.
| >>> from ray.rllib.core.models.configs import ModelConfig | ||
| >>> from ray.rllib.core.models.configs import ModelConfig |
There was a problem hiding this comment.
The import from ray.rllib.core.models.configs import ModelConfig is duplicated here. We should remove the duplicate import to keep the example clean.
| >>> from ray.rllib.core.models.configs import ModelConfig | |
| >>> from ray.rllib.core.models.configs import ModelConfig | |
| >>> from ray.rllib.core.models.configs import ModelConfig |
| initial_dense_weights_initializer="HeNormal", | ||
| initial_dense_weights_initializer={"seed": 334}, |
There was a problem hiding this comment.
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.
| initial_dense_weights_initializer="HeNormal", | |
| initial_dense_weights_initializer={"seed": 334}, | |
| initial_dense_weights_initializer="HeNormal", | |
| initial_dense_weights_initializer_config={"seed": 334}, |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Thanks, that's right. This PR is syntax-only on purpose, so it leaves example code unchanged. #67766, stacked on this PR, fixes this.
| >>> 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 |
There was a problem hiding this comment.
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.
| >>> 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 |
| >>> 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 |
There was a problem hiding this comment.
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.
| >>> 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 |
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]>
1a8f430 to
b76aeb0
Compare
5a1dfb3
into
doc-1926-runner-rllib-stubs
Note
Stacked on #67822, which fixes the
docstring-onlyrunner failures this PR hit onrllib.coreexamples that need torch. Merge #67822 first. GitHub then retargets this PR tomaster.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