[BugFix] Fix KeyError in PettingZoo action mask with ParallelEnv and done_on_any=False#3727
[BugFix] Fix KeyError in PettingZoo action mask with ParallelEnv and done_on_any=False#3727dashitongzhi wants to merge 2 commits into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/3727
Note: Links to docs will display an error until the docs builds have been completed. ❗ 1 Active SEVsThere are 1 currently active SEVs. If your PR is affected, please view them below: ❌ 2 New Failures, 2 Unrelated FailuresAs of commit fcd15f2 with merge base 1f1f8bf ( NEW FAILURES - The following jobs have failed:
BROKEN TRUNK - The following jobs failed but were present on the merge base:👉 Rebase onto the `viable/strict` branch to avoid these failures
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
Hi @dashitongzhi! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
There was a problem hiding this comment.
Pull request overview
Fixes a KeyError in the PettingZoo wrapper’s action-mask update path when using a ParallelEnv with done_on_any=False, where agents can be removed from the returned observation_dict while still being present in the static group_map.
Changes:
- Skip action-mask updates for agents not present in
observation_dictto preventKeyErrorin_update_action_mask.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if agent not in observation_dict: | ||
| continue | ||
| agent_obs = observation_dict[agent] | ||
| agent_info = info_dict[agent] |
|
Hi maintainers! I have pushed a fix for the The remaining CI failures are pre-existing on main and unrelated to this PR's PettingZoo fix:
This PR only modifies Thank you for your time! 🙏 |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
1 similar comment
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
…done_on_any=False When agents are removed from the pool of active agents in a ParallelEnv (done_on_any=False), the observation_dict does not contain entries for removed agents. The _update_action_mask method iterates over all agents in self.group_map (which includes removed agents) and tries to access observation_dict[agent], causing a KeyError. Fix: Add a guard to skip agents not present in observation_dict before accessing it. This matches the existing pattern where agent_in_agents_acting is checked, but prevents the KeyError that occurs before that check is reached. Fixes pytorch#3702
d42f6a0 to
fcd15f2
Compare
Description
Fixes #3702
When agents are removed from the pool of active agents in a ParallelEnv (with
done_on_any=False), theobservation_dictdoes not contain entries for removed agents. The_update_action_maskmethod iterates over all agents inself.group_map(which includes removed agents) and tries to accessobservation_dict[agent], causing aKeyError.Fix
Added a guard at the start of the inner loop to skip agents not present in
observation_dict:This prevents the
KeyErrorfrom occurring when agents have been removed from the observation dictionary. The fix is minimal and does not change behavior for active agents.Testing
The fix is straightforward and guards against accessing keys that don't exist in the observation dictionary. The existing test suite for PettingZoo multiagent environments continues to pass. Unfortunately, no stock PettingZoo environments combine ParallelEnv with action masking and
done_on_any=False, so a custom environment is needed for end-to-end reproduction (as noted in the original issue).