Repository navigation
[RLlib] Fix broken rllib.connectors docstring examples and make them run - #67759
Conversation
There was a problem hiding this comment.
Code Review
This pull request primarily updates docstrings, doctests, and examples across several RLlib connector files to improve correctness and formatting. Key changes include converting testcode blocks to doctest examples, fixing variable references and typos, and updating outdated configuration APIs. A review comment correctly points out that in the FrameSkippingAgentConnector example, using del on a defaultdict for a non-existent env_id will raise a KeyError, and suggests using .pop(env_id, None) instead to safely remove the key.
| ... def __init__(self, n): | ||
| ... self._n = n | ||
| ... self._frame_count = default_dict(str, default_dict(str, int)) | ||
| ... self._frame_count = defaultdict(lambda: defaultdict(int)) |
There was a problem hiding this comment.
In the FrameSkippingAgentConnector example, the reset method uses del self._frame_count[env_id]. Since self._frame_count is a defaultdict, attempting to delete a non-existent key (e.g., when resetting an environment that hasn't been stepped in yet) will raise a KeyError. Consider using .pop(env_id, None) instead to safely remove the key if it exists.
Convert the last `testcode` blocks in `rllib/connectors`, whose prints of random observations need expected output, and fix examples that were wrong: the `FrameSkippingAgentConnector` sketch, the missing NumPy import in the `ActionConnector` example, the stray backtick and deprecated `config.training(learner_connector=...)` call in `AddColumnsFromEpisodesToTrainBatch`, and a wrong sequence length in an `AddTimeDimToBatchAndZeroPad` comment. @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]>
1d00d04 to
8c81c1b
Compare
Description
This is a correctness change for RLlib review. It fixes
rllib.connectorsdocstring examples that were wrong, and it converts the last twotestcodeblocks inrllib/connectors. Those blocks needed an expected-output change, so they stayed out of the syntax-only conversion in #67758. After this PR, notestcodeortestoutputremains inrllib/connectors.Stacked on #67758. Review that PR first. This diff is against its branch.
testcodeblocks this PR convertsAddObservationsFromEpisodesToBatchandAddNextObservationsFromEpisodesToTrainBatch: each example prints observations drawn fromobs_space.sample()and shows no expected output. Each print now shows its output, with...in place of the random values, such as1st Episode's last obs is [...]. The next-observations example shows thatget_observations([1, 2])returns a list of float32 arrays. Both now run.Examples that were wrong
AgentConnector: theFrameSkippingAgentConnectorsketch calleddefault_dict, which doesn't exist. It also indexed the frame count withac_data.env_id, butac_datais the input list, not an item. It now usescollections.defaultdict, and it indexes withd.env_idandd.agent_id. With the fix, the class emits an item on everynth call, andreset()clears the count. The doctest defines the class but doesn't call it. This also fixes the "prosessed" typo in the prose that follows.ActionConnector: the example callednp.zeros_likewithout importing NumPy. It now imports it.AddColumnsFromEpisodesToTrainBatch: the config snippet ended with a stray backtick. It also calledconfig.training(learner_connector=...), which is deprecated in favor ofconfig.learners(learner_connector=...), so it now callslearners(). It stays a render-onlypythoncode block, because it sits mid-prose and uses the reader's ownconfig.AddTimeDimToBatchAndZeroPad: a comment said the learner connector uses a sequence length of 4. The dummy module setsmax_seq_lento 3, and the checked output is chunked by 3, so the comment now says 3.Review fixes
AgentConnector: theFrameSkippingAgentConnector.resetsketch useddel self._frame_count[env_id], which raisesKeyErrorfor an env that hasn't stepped yet. It now usespop(env_id, None). From a gemini-code-assist comment.Related issues
Stacked on #67758. 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: "5 docstring(s) run, 0 failed, 0 skipped, in 5 file(s)."doctestpass over the same docstrings withELLIPSIS, which fails on unexpected output the way the RLlibdoctest()target does: all 5 pass.ci/ray_ci/doc/check_docstring_only.py --base doc-1926-rllib-connectors-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