Repository navigation
[RLlib] Fix broken rllib.core docstring examples and make them run - #67766
dstrodtman wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
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.
b3e5697 to
b735b5a
Compare
b735b5a to
408213e
Compare
408213e to
150cb2e
Compare
150cb2e to
0e5a05b
Compare
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]>
0e5a05b to
9a33f92
Compare
…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]>
|
This pull request has been automatically marked as stale because it has not had 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. |
Description
This is a correctness change for RLlib review. It fixes
rllib.coredocstring examples that were wrong or couldn't run, and it turns theCNNTransposeHeadConfigcode blocks back into doctests. After this PR, notestcodeortestoutputremains inrllib/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
MLPHeadConfigandMLPEncoderConfig: one example in each built withframework="tf2".build()returns a model only for"torch", somodelwasNone. 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 ofoutput_dimsas outputs. So the stacks now readLinear(16, 4)with a(4,)free log-std, andLinear(100, 50)with a(50,)free log-std.+SKIP_EXAMPLEis gone.CNNTransposeHeadConfig: both examples were code blocks, becauseframework="torch)didn't parse. The first example also passedinitial_dense_weights_initializertwice and used TensorFlow's"HeNormal", which Torch rejects. Both examples passedcnn_use_bias, which isn't a field. They now usekaiming_normal_andcnn_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 thirdConv2Dthat 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, becauseterminatedwas stillTruefrom 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_EXAMPLEbecause 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 setslogits, so it now passesprobs=. The output wastensor([3, 4]), but 4 isn't a valid class. It now shows a real sample.TorchDiagGaussian: the "scale is None" example raisedTypeError, becausescaleis required, and passingscale=NoneraisesValueErrorintorch.distributions.Normal. This PR removes that example and thelocdescription's claim about aNonescale. The output now shows a real sample.InferenceOnlyAPI.get_non_inference_attributesstays apythoncode block, because it's pseudocode.Review fixes
These address gemini-code-assist comments on #67765:
Model: the example importedModelConfigtwice.RLModule: the training and inference examples importedPPOTorchRLModuleandRLModuleSpecwithout using them and importedPPOCatalogtwice. The sampling example also importedRLModuleSpecwithout 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, andtensorflow2.21:ci/ray_ci/doc/run_docstring_examples.pyon all 5 changed files: "11 docstring(s) run, 0 failed, 2 skipped, in 5 file(s)."doctestpass over the same docstrings withELLIPSIS. Unlike the runner above, it fails on unexpected output, the way the RLlibdoctest()target does. All 11 runnable docstrings pass, and the other 2 carry+SKIP_EXAMPLE.TorchCategoricalandTorchDiagGaussianexamples with+SKIP_EXAMPLEremoved: 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 --fileson all 5 files: all hooks pass.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