[Feature] Add COMA multi-agent objective - #4151
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/4151
Note: Links to docs will display an error until the docs builds have been completed. ❌ 4 新建 Failures, 1 Unrelated FailureAs of commit d167d23 with merge base b0eab87 ( NEW FAILURES - The following jobs have failed:
BROKEN TRUNK - The following job 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 @Iliamsou! 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! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
vmoens
left a comment
There was a problem hiding this comment.
Thanks for adding a dedicated COMA objective. I found several issues that need to be addressed before this can merge:
-
[P1] Shift the actual time dimension. In
compute_value_target,next_value[:, :-1] = value_target[:, 1:]only shifts time for exactly[B, T, ...]inputs. A standard collector over an unbatched environment returns[T, n_agents, 1], in which case this shifts the agent dimension. Multiple environment batch dimensions also shift the wrong axis. Please resolve the named/final TensorDict time dimension and add a[T]regression case. -
[P1] Bootstrap rollout boundaries and truncations correctly. Zero-filling shifted values makes the last
n_steptransitions of every non-terminal fixed-length rollout use incomplete returns. Masking withdonealso suppresses a valid bootstrap at truncation; TorchRL return semantics distinguish this withterminated. The target critic needs to evaluate the transition next observation at boundaries rather than treating the end of the sampled batch as terminal. -
[P1] Honor collector validity masks. Direct
.mean()andF.mse_loss(...)reductions bypassLossModule._reduce_loss, so("collector", "mask")andshifted_validdo not exclude padded transitions. COMA commonly consumes episodic sequences, so this can train both actor and critic on padding. Please keep losses elementwise and reduce through_reduce_loss(..., tensordict=tensordict). -
[P2] Preserve flat
NestedKeyvalues.("next",) + tuple(self.tensor_keys.reward)works for tuple keys but turnsreward="reward"into("next", "r", "e", "w", "a", "r", "d"). Please use the repository nested-key normalization pattern for both reward and termination keys.
The test volume is proportionate, but the current target tests only use synthetic [1, T] batches, which hides the time-axis failure. Please cover [T], a truncated boundary, automatic loss masking, and both flat and nested set_keys() values.
The public API integration also needs the repository-required documentation: a complete Sphinx-style class docstring with runnable example and paper reference, an entry in docs/source/reference/objectives_multiagent.rst, and the applicable tutorial/SOTA recipe. Since this is a new test file, please also add the executable if __name__ == "__main__": pytest.main(...) block.
Addresses review feedback on the COMA objective:
- Resolve the rollout time dimension dynamically instead of assuming dim 1,
fixing compute_value_target for [T], [B, T], and multi-batch-dim inputs.
- Bootstrap on terminated rather than done, and mark rollout-boundary
transitions with no reliable bootstrap as invalid via a shifted_valid mask
instead of silently biasing their target low.
- Compute elementwise actor/critic/entropy losses and reduce them through
LossModule._reduce_loss so collector masks and shifted_valid are honored
automatically.
- Stop shredding flat NestedKey values when building the ("next", ...) reward
and terminated keys.
- Add tests for a [T]-only rollout, a truncated boundary, automatic loss
masking, and flat/nested set_keys() values.
- Add a full Sphinx docstring with a doctest-verified example and paper
reference, an objectives_multiagent.rst entry, a Hydra SOTA recipe under
sota-implementations/multiagent, and a tutorial.
| next_value = _shift_time(value_target, time_dim, fill_value=0.0) | ||
| next_valid = _shift_time(valid, time_dim, fill_value=False) | ||
| value_target = reward + self.gamma * not_terminated * next_value | ||
| valid = terminated.to(torch.bool) | next_valid |
There was a problem hiding this comment.
done=True, terminated=False still shifts a value across the reset. changing the next episode's first action moved the previous target from 0.5 to 2.0. can we invalidate this bootstrap at every done boundary?
| if self.normalize_advantage: | ||
| # MAPPO-style per-batch standardisation: same counterfactual | ||
| # advantage, rescaled so the actor step size is batch-invariant. | ||
| advantage = (advantage - advantage.mean()) / advantage.std().clamp_min(1e-6) |
There was a problem hiding this comment.
one valid actor value returns nan here. masked padding also changes the scale. can we use the effective loss mask with correction=0?
What
Adds a dedicated
COMALossobjective for multi-agent reinforcement learning. The loss implements the COMA counterfactual baseline with a decentralised actor and a centralised Q-value network, including n-step Q-value targets and optional advantage normalisation.Contents
torchrl/objectives/multiagent/coma.py:COMALossimplementation and helpers to construct the joint observation, joint action without the current agent, and masked joint action inputs used by the centralised critic.torchrl/objectives/multiagent/__init__.pyandtorchrl/objectives/__init__.py: exposeCOMALoss.test/objectives/test_coma.py: tests for the counterfactual baseline, Q-value targets, n-step returns, input construction helpers, advantage normalisation, and diagnostics.Tests
Ran:
python -m pytest test/objectives/test_coma.py8 passed.