Skip to content

[Feature] Add QMix, VDN and IQL support to DQN trainer#3694

Merged
vmoens merged 9 commits into
pytorch:mainfrom
Xmaster6y:ma-dqn
May 1, 2026
Merged

[Feature] Add QMix, VDN and IQL support to DQN trainer#3694
vmoens merged 9 commits into
pytorch:mainfrom
Xmaster6y:ma-dqn

Conversation

@Xmaster6y
Copy link
Copy Markdown
Contributor

Description

  • Extend DQNTrainer to support custom key and potential reward aggregation
  • Add mixer networks and loss configs with custom builders
  • Expose customisation in QValueActor
  • Add new sota-implementations for QMix, VDN and IQL
  • Sort config tests (module and loss)

Motivation and Context

This change is required for better trainer support of multi-agent algorithms.

  • I have raised an issue to propose this change (required for new features and bug fixes)

Types of changes

What types of changes does your code introduce? Remove all that do not apply:

  • New feature (non-breaking change which adds core functionality)
  • Example (update in the folder of examples)

Checklist

Go over all the following points, and put an x in all the boxes that apply.
If you are unsure about any of these, don't hesitate to ask. We are here to help!

  • I have read the CONTRIBUTION guide (required)
  • My change requires a change to the documentation.
  • I have updated the tests accordingly (required for a bug fix or a new feature).
  • I have updated the documentation accordingly.

@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented Apr 29, 2026

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/3694

Note: Links to docs will display an error until the docs builds have been completed.

⚠️ 17 Awaiting Approval

As of commit d90d0de with merge base 4da311b (image):

AWAITING APPROVAL - The following workflows need approval before CI can run:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 29, 2026
self.log_rewards = log_rewards
self.log_observations = log_observations

if self.mixing_strategy in ("qmix", "vdn"):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if self.mixing_strategy in ("qmix", "vdn"): # "iql" missing
self.register_op("batch_process", self._aggregate_agent_rewards)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no mixing for iql

Comment thread torchrl/trainers/algorithms/dqn.py Outdated
source_key = ("next", "agents", key[-1] if isinstance(key, tuple) else key)
value = batch.get(source_key, None)
if value is not None:
batch.set(next_key, value.mean(-2))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

batch.set(next_key, value.mean(-2))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-2 is the agent dim

spec = spec.clone()
if "action" not in spec.keys():
spec["action"] = None
if action_key not in spec.keys(True, True):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spec logic is inverted and nested key wrapping is wrong.
The else branch fires when the key already exists and replaces the whole spec with a new Composite wrapping the old one, backwards. Also Composite({("agents", "action"): spec}) won't create proper nesting for tuple keys.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't get you sorry

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like if the value keys aren't passed explicitly, they default to flat "action_value" / "chosen_action_value" instead of ("agents", "action_value") / ("agents", "chosen_action_value"). This breaks QMixerLoss. The value key defaults should mirror action_key's namespace.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where do they default to flat?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tbh not really a big issue - was just thinking more in the situation where checking whether action_key is nested, so they always resolve to flat strings.

Xmaster6y and others added 5 commits April 30, 2026 09:27
… in test

QValueActor does not accept out_keys, but ModelConfig (the parent of
QValueModelConfig) defaults out_keys=None which Hydra forwards as a kwarg.
Drop it in _make_qvalue_model. Also add the missing depth=2 to MLPConfig
calls in test_qvalue_model_config and test_ppo_trainer_config: passing
num_cells as an int requires depth.
@vmoens vmoens merged commit 8038188 into pytorch:main May 1, 2026
59 of 67 checks passed
@Xmaster6y Xmaster6y deleted the ma-dqn branch May 1, 2026 14:43
vmoens added a commit to vmoens/rl that referenced this pull request May 2, 2026
Local ufmt run with the newer black 26.x kept some constructs unchanged
that CI's pinned `black==22.3.0` (per .pre-commit-config.yaml) wants to
re-shape: an MLPConfig call collapses to one line, and a nested ternary
splits across three. Re-formatted with the CI-pinned versions to keep
the lint hook clean across the merge with main (which picked up
additional unformatted code from pytorch#3694 since the previous push).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Feature New feature Integrations/torch_geometric Integrations Modules sota-implementations/ Trainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants