Repository navigation
[RLlib] Convert rllib.models, execution, offline, and callbacks docstring examples to stock doctests - #67761
Conversation
…ib.callbacks docstring examples to stock doctests Convert the `.. testcode::` examples in these docstrings to `>>>` examples under an `Examples:` header, so they render in griffe and MkDocs as well as Sphinx. Runnable blocks become runnable doctests, `:skipif: True` blocks carry `+SKIP_EXAMPLE`, and blocks that don't parse become `.. code-block::`. The example code and the docstring claims are unchanged. @rtd-build Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Douglas Strodtman <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request refactors documentation across multiple RLlib files, converting obsolete .. testcode:: and .. testoutput:: blocks into standard Examples: sections with doctest formatting or .. code-block:: directives. The review feedback identifies a few issues in the updated examples: a NameError in on_offline_eval_runners_recreated due to referencing an undefined is_evaluation variable, and syntax errors (extra closing parentheses) in the example print statements for train_one_step and multi_gpu_train_one_step.
| ... if is_evaluation: | ||
| ... offline_eval_runner._custom_property_for_evaluation = 1 | ||
| ... else: | ||
| ... offline_eval_runner._custom_property_for_training = 1 |
There was a problem hiding this comment.
The method on_offline_eval_runners_recreated does not have an is_evaluation parameter in its signature (unlike on_env_runners_recreated). Referencing is_evaluation here will raise a NameError at runtime. Since these are offline evaluation runners, they are always used for evaluation, so you can set the property directly without checking is_evaluation.
| ... if is_evaluation: | |
| ... offline_eval_runner._custom_property_for_evaluation = 1 | |
| ... else: | |
| ... offline_eval_runner._custom_property_for_training = 1 | |
| ... offline_eval_runner._custom_property_for_evaluation = 1 |
There was a problem hiding this comment.
Thanks, that's right. This PR is syntax-only on purpose, so it leaves example code unchanged. #67762, stacked on this PR, fixes this.
| algo = [...] | ||
| train_batch = synchronous_parallel_sample(algo.env_runner_group) | ||
| # This trains the policy on one batch. | ||
| print(train_one_step(algo, train_batch))) |
There was a problem hiding this comment.
Thanks, that's right. This PR is syntax-only on purpose, so it leaves example code unchanged. #67762, stacked on this PR, fixes this.
| algo = [...] | ||
| train_batch = synchronous_parallel_sample(algo.env_runner_group) | ||
| # This trains the policy on one batch. | ||
| print(multi_gpu_train_one_step(algo, train_batch))) |
There was a problem hiding this comment.
Thanks, that's right. This PR is syntax-only on purpose, so it leaves example code unchanged. #67762, stacked on this PR, fixes this.
…cstring examples (#67762) ## Description This is a correctness change for RLlib review. It fixes docstring examples in `rllib/models`, `execution`, `offline`, and `callbacks` that were wrong or couldn't run, and it converts the last `testcode` block in those subpackages. Where a fix is verified, the example now runs in the doctest target. After this PR, no `testcode` or `testoutput` remains in these four subpackages. Stacked on #67761. Review that PR first. This diff is against its branch. ### Examples that now run - `models/catalog.py`, `ModelCatalog`: the example used undefined placeholders, such as `env` and `raw_observation`. It also read `model.outputs`, which `ModelV2` doesn't have. It's now a working CartPole example with `framework="torch"`. It gets logits by calling the model and passes them to the distribution class. - `models/preprocessors.py`, `OneHotPreprocessor`: the example called `self.transform` on a random sample, and it wrote the output as `np.array(...)`. It now builds the preprocessor and transforms fixed values. The output is the real `array(..., dtype=float32)`, with the same one-hot values as before. - `models/repeated_values.py`, `unbatch_all` and `unbatch_repeat_dim`: these were `<Tensor shape=...>` pseudocode, and the second one called `batch.unbatch()`, which doesn't exist. Both now use a small torch tensor and print lengths and shapes. Both docstrings gain a `Returns` entry, which pydoclint requires once a docstring has an `Examples:` section. Each entry describes what the method body returns. - `execution/buffers/mixin_replay_buffer.py`, `MixInMultiAgentReplayBuffer`: the hidden `...` output matched anything. The full output varies, because `replay()` appends randomly replayed samples after the newly added batches. The example now prints the first three entries, `[1 2 2]`, which are always the newly added batches. - `offline/input_reader.py`, `InputReader.tf_input_ops`: the example called `self._build_layers_v2`, which doesn't exist in RLlib. It also read `input_ops["action"]`, but the column is `"actions"`. It now calls `self.forward(...)`, as `examples/_old_api_stack/models/custom_loss_model.py` does. The prose pointed to `examples/custom_loss.py`, which doesn't exist, and now points to that file. - `models/modelv2.py`, `get_initial_state` and `forward`: these class-definition sketches pass unchanged, so they drop `+SKIP_EXAMPLE`. - `callbacks/callbacks.py`, `on_offline_eval_runners_recreated`: the example read `is_evaluation`, which isn't a parameter of this hook. It also passed `remote_runner_ids=` to `foreach_runner`, which takes `remote_worker_ids`. Both are fixed. - `callbacks/callbacks.py`, `on_env_runners_recreated`: a comment named `local_worker=False`, but the code passes `local_env_runner=False`. ### Examples that stay render-only - `execution/train_ops.py`, `train_one_step` and `multi_gpu_train_one_step`: these need a built old-API-stack `Algorithm`. This PR removes the stray parenthesis and passes `worker_set=` to `synchronous_parallel_sample`, whose arguments are keyword-only. It also prints the output as a Python dict. They stay code blocks, because an `Examples:` section makes pydoclint require type hints in the function signatures, and that's a code change. - `offline/mixed_input.py`, `MixedInput`: this needs an `IOContext`, local JSON files, and an S3 path. It keeps `+SKIP_EXAMPLE`. ### Found, not fixed here This is a code issue, not a docstring one, so it's out of scope. `Algorithm._run_one_offline_evaluation` fires the `on_offline_eval_runners_recreated` callback with `env_runner_group=` and `env_runner_indices=`. The hook's signature in `callbacks/callbacks.py` declares `offline_eval_runner_group` and `offline_eval_runner_indices` as keyword-only arguments with no defaults. So an override that follows the documented signature would raise `TypeError` when the callback fires. ### Review fixes - `MixInMultiAgentReplayBuffer`: the example defined a batch `C` that it never used, so it's gone. From a gemini-code-assist comment. ## Related issues Stacked on #67761. Related to #66753. ## Additional information **Not a duplicate.** I searched open PRs for fixes to these docstring examples and found none. **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 the 8 changed files: "10 docstring(s) run, 0 failed, 0 skipped, in 8 file(s)." - A strict stdlib `doctest` pass over the same docstrings with `ELLIPSIS`, which fails on unexpected output the way the RLlib `doctest()` target does: all 10 pass. - `ci/ray_ci/doc/check_docstring_only.py --base doc-1926-rllib-models-execution-doctests`: "8 file(s), docstrings only." - `pre-commit run --files` on all 8 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]>
Description
Converts the
.. testcode::examples in thepython/ray/rllib/models,execution,offline, andcallbacksdocstrings to>>>examples under a pluralExamples:header. Sphinx renders.. testcode::, but griffe and MkDocs don't, and stock>>>examples render in both. This follows therllib.utilsconversion in #67753. The four subpackages are small, so they share one PR.This PR covers 17 of the 18
testcodeblocks in these subpackages, in 9 files:synchronous_parallel_sampleinexecution/rollout_ops.pyand the twoRLlibCallbackexamples incallbacks/callbacks.py. None of these ran before, because the RLlibdoctest()target doesn't executetestcode. All three 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 inmodels/catalog.py,models/modelv2.py(2),models/preprocessors.py(2, in one docstring),offline/input_reader.py, andoffline/mixed_input.py... code-block:: python, and their outputs become.. code-block:: text. The fiveRepeatedValuesblocks use<Tensor shape=...>placeholders. The twotrain_ops.pyexamples have an extra closing parenthesis.This is a syntax-only change. The example code and the claims in each docstring are unchanged. No format-forced changes were needed, and pydoclint passes without new
ArgsorReturnsentries.Left for a separate correctness PR
In
execution/buffers/mixin_replay_buffer.py, theMixInMultiAgentReplayBufferexample'stestoutputis:hide:with a body of only..., which matches any output. Converting it would change what the example checks.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 9 files, in a Python 3.12 virtualenv withray[rllib]==2.59.0,torch, andtensorflow2.21: "3 docstring(s) run, 0 failed, 6 skipped, in 9 file(s)." All six 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 3 runnable docstrings pass.ci/ray_ci/doc/check_docstring_only.py --base upstream/master: "9 file(s), docstrings only."pre-commit run --fileson all 9 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