-
Notifications
You must be signed in to change notification settings - Fork 327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] multiagent data standardization: PPO advantages #2677
base: main
Are you sure you want to change the base?
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/2677
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: This comment was automatically generated by Dr. CI and updates every 15 minutes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can definitely see why this is a problem, thanks for pointing it out and coming up with a solution!
My first thought when looking at this PR is that it can easily be forgotten.
IMO the ideal API is, by decreasing order of desiderability
- The API doesn't break with edge cases (I'm using a overly generic term here, not saying MARL is edge case :) ) but takes care of them automatically. That can be achieved with a trainer class for instance.
- The API breaks when encoutering an edge case and the error message tells the user exactly why and what to do
- The run goes through but a warning is raised.
Finally, 4., the run goes through and things are silently wrong.
I think this is a (4). The ideal solution would be to do it automatically (1) but I'm not sure how we can achieve that with the current API. Any thoughts on how to make it a (2) or (3)?
(As a counter-example, if your data has some specific keys, the loss will simply not work until you call loss_module.set_keys(**tensor_keys)
. We're not doing it automatically but there is very little chance that this step is forgotten and we can capture the errors and give an informative error message to let people know what to do.)
Another way of saying this is: there are two levels of complexity that make a lib hard to use and I think both are present with the current solution:
- (weak) There is something that users need to know when they use the library but they may not be aware of that and the lib isn't telling them at runtime;
- (strong) The lib doesn't take care automaticlly of edge cases and it's up to the user to come up with a solution.
How can we make sure that users are not assuming that this is done automatically when they say "normalize_advantage=True`? Is there a quick check that we can do?
One thing I have in mind is to look at the reward shape and detecting if the last dim is a singleton (if not, the reward is probably something that isn't from regular single agent)? In principle, anything with shape that is not (*tensordict.shape, 1)
should have a dedicated reward normalization, no?
Regarding other parts of the lib where this could be needed:
- I think we also need to address this issue with the advantages where there's a lot of normalization of rewards etc.
rl/torchrl/objectives/value/advantages.py
Lines 1434 to 1438 in d009835
if self.average_gae: loc = adv.mean() scale = adv.std().clamp_min(1e-4) adv = adv - loc adv = adv / scale
rl/torchrl/objectives/value/advantages.py
Lines 1804 to 1808 in d009835
if self.average_adv: loc = adv.mean() scale = adv.std().clamp_min(1e-5) adv = adv - loc adv = adv / scale - VecNorm needs a big refactoring (it's clunky and inefficient atm), so we can put this in the TODO list.
- For ObservationNorm, you could pass a loc / scale with any broadcastable shape and it should work.
TheObservationNorm.init_stats
should also have the required keyword arguments to account for the example you gave.
torchrl/objectives/ppo.py
Outdated
@@ -87,6 +88,8 @@ class PPOLoss(LossModule): | |||
Can be one of "l1", "l2" or "smooth_l1". Defaults to ``"smooth_l1"``. | |||
normalize_advantage (bool, optional): if ``True``, the advantage will be normalized | |||
before being used. Defaults to ``False``. | |||
normalize_advantage_exclude_dims (Sequence[int], optional): dimensions to exclude from the advantage |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
normalize_advantage_exclude_dims (Sequence[int], optional): dimensions to exclude from the advantage | |
normalize_advantage_exclude_dims (Tuple[int], optional): dimensions to exclude from the advantage |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like an indirect argument to me and it could be hard for users to understand.
If you're using PPO with this kwarg, you need to think this way:
- I asked to normalize the advantage
- My reward has one or more agent dims
- I want to excldue these from the advantage normalization.
Another way to do this could be to have a kwarg
reward_agent_dim: int | Tuple[int] | None = None,
If this is passed, the reward_agent_dim
will be kept during calls to mean
/ std
.
Per se, it changes nothing but it's a tiny bit more clear from the name and signature that this indicates a dim that is not a feature dim.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my discussion point on this being useful for more uscases. We can come up with a better name together
torchrl/objectives/ppo.py
Outdated
@@ -87,6 +88,8 @@ class PPOLoss(LossModule): | |||
Can be one of "l1", "l2" or "smooth_l1". Defaults to ``"smooth_l1"``. | |||
normalize_advantage (bool, optional): if ``True``, the advantage will be normalized | |||
before being used. Defaults to ``False``. | |||
normalize_advantage_exclude_dims (Sequence[int], optional): dimensions to exclude from the advantage | |||
standardization, can be negative. Useful in multiagent settings to exlude the agent dimension. Default: (). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
standardization, can be negative. Useful in multiagent settings to exlude the agent dimension. Default: (). | |
standardization. Negative dimensions are valid. This is useful in multiagent settings where the agent dimension may be excluded from the reductions. | |
Defaults to ``()``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a question reguarding the "negative" dims.
I guess ultimately we would also like to do the same with this
rl/torchrl/objectives/value/advantages.py
Lines 696 to 701 in d009835
if self.average_rewards: | |
reward = reward - reward.mean() | |
reward = reward / reward.std().clamp_min(1e-5) | |
tensordict.set( | |
("next", self.tensor_keys.reward), reward | |
) # we must update the rewards if they are used later in the code |
But there we have an extra "time" dimension, so we must be careful that agent_dim=(-2,)
may actually mean (-3,)
in some cases (similarly, feature_dim=(4,)
may be feature_dim=(5,)
).
Is it an issue in this (ie, PPO) case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the user should pass the dimension index for the tensor that goes in the class.
If you give as input [batch,time,agents,n] then agents is -2
also agents is usually after time
Thanks so much for the in-depth reveiw! I agree! Let me trying t give some answers, I'll start from some general clarifications This is not a feature purely for multiagent uses. The need for this feature could be various. Imagine a multi-objective rl setting where you have an array of rewards. Now, even in that case you would not want to mix rewards during normalization. You could also have mutli-objective + multi-agent. So, the goal of this PR is more like saying that using I agree this should be automatic. The problem is that it is quite impossible to automatically know what dimensions the user wants to keep independent. What we could do to turn it into a (3) is what you suggest: if you are asking for normalization and your shape is more than (*tensordict.shape, 1), we can give you a warning saying "hey! you have all those extra dims that we are considering in our statistics, any of those independent?" |
I should have addressed all comments. Anorther thing I forgot to say is that the |
Hello!
Context
So, in this library we frequently apply standardizations over data (also called normalization) where we subtract the mean from the data and divide it by a standard deviation.
A few places where we do this that come to mind are in the transforms (where it can be used over observations and rewards) and in the advantages in ppo. Please let me know about other places if you can think of more.
Issue
The issue is that normally the mean and std statistics are computed over all the dimensions. But in multiagent settings, where for example a reward tensor could have an "agent" dimension, this is not the proper way to do it as different agents might have different reward distributions and applying standardization naively across them will create issues.
Example
For example consider the case where we have 2 agents each with 2 rewards (agent_1_r = [100,200], agent_2_r = [0,-1]), structured in a 2x2 tensor
r = torch.tensor([[100,200],[0,-1]])
If we standardize using
mean(r)
andstd(r)
, we getr = torch.tensor([[0.3,1.5],[-0.9,-.91]])
, which is badIf we standardize using
mean(r, dim=1)
andstd(r,dim=1)
, hence excluding the agent dim from the statistics, we getr = torch.tensor([[-1,1],[1,-1]])
, which is better as it does not cross pollute over agentsProposal
We should adapt all places in the library where standardization is used to take a list of dimensions to exlude from the statistics. This way, multiagent users can provide the agent dimension as input and use the standardization feature
What I did here
I did this for the advantages in PPO
To do it, I made a nice standardization util function that takes as input a list of dimensions to exlude from the standardization (a more flexible implementation of a functional layer norm)