Skip to content

Fix 2.8 issue per sample grad #3460

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

Draft
wants to merge 7 commits into
base: RC-TEST-2.8
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions advanced_source/pendulum.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
from tensordict.nn import TensorDictModule
from torch import nn

from torchrl.data import BoundedTensorSpec, CompositeSpec, UnboundedContinuousTensorSpec
from torchrl.data import Bounded, Composite, Unbounded
from torchrl.envs import (
CatTensors,
EnvBase,
Expand Down Expand Up @@ -403,14 +403,14 @@ def _reset(self, tensordict):

def _make_spec(self, td_params):
# Under the hood, this will populate self.output_spec["observation"]
self.observation_spec = CompositeSpec(
th=BoundedTensorSpec(
self.observation_spec = Composite(
th=Bounded(
low=-torch.pi,
high=torch.pi,
shape=(),
dtype=torch.float32,
),
thdot=BoundedTensorSpec(
thdot=Bounded(
low=-td_params["params", "max_speed"],
high=td_params["params", "max_speed"],
shape=(),
Expand All @@ -426,24 +426,26 @@ def _make_spec(self, td_params):
self.state_spec = self.observation_spec.clone()
# action-spec will be automatically wrapped in input_spec when
# `self.action_spec = spec` will be called supported
self.action_spec = BoundedTensorSpec(
self.action_spec = Bounded(
low=-td_params["params", "max_torque"],
high=td_params["params", "max_torque"],
shape=(1,),
dtype=torch.float32,
)
self.reward_spec = UnboundedContinuousTensorSpec(shape=(*td_params.shape, 1))
self.reward_spec = Unbounded(shape=(*td_params.shape, 1))


def make_composite_from_td(td):
# custom function to convert a ``tensordict`` in a similar spec structure
# of unbounded values.
composite = CompositeSpec(
composite = Composite(
{
key: make_composite_from_td(tensor)
if isinstance(tensor, TensorDictBase)
else UnboundedContinuousTensorSpec(
dtype=tensor.dtype, device=tensor.device, shape=tensor.shape
key: (
make_composite_from_td(tensor)
if isinstance(tensor, TensorDictBase)
else Unbounded(
dtype=tensor.dtype, device=tensor.device, shape=tensor.shape
)
)
for key, tensor in td.items()
},
Expand Down Expand Up @@ -687,7 +689,7 @@ def _reset(
# is of type ``Composite``
@_apply_to_composite
def transform_observation_spec(self, observation_spec):
return BoundedTensorSpec(
return Bounded(
low=-1,
high=1,
shape=observation_spec.shape,
Expand All @@ -711,7 +713,7 @@ def _reset(
# is of type ``Composite``
@_apply_to_composite
def transform_observation_spec(self, observation_spec):
return BoundedTensorSpec(
return Bounded(
low=-1,
high=1,
shape=observation_spec.shape,
Expand Down
19 changes: 17 additions & 2 deletions intermediate_source/per_sample_grads.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,23 @@ def compute_loss(params, buffers, sample, target):
# we can double check that the results using ``grad`` and ``vmap`` match the
# results of hand processing each one individually:

for per_sample_grad, ft_per_sample_grad in zip(per_sample_grads, ft_per_sample_grads.values()):
assert torch.allclose(per_sample_grad, ft_per_sample_grad, atol=3e-3, rtol=1e-5)
# Get the parameter names in the same order as per_sample_grads

for name, ft_per_sample_grad in ft_per_sample_grads.items():
# Find the corresponding manually computed gradient
idx = list(model.named_parameters()).index((name, model.get_parameter(name)))
per_sample_grad = per_sample_grads[idx]

# Check if shapes match and reshape if needed
if per_sample_grad.shape != ft_per_sample_grad.shape and per_sample_grad.numel() == ft_per_sample_grad.numel():
ft_per_sample_grad = ft_per_sample_grad.view(per_sample_grad.shape)
Comment on lines +175 to +180
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a bit confused by this part

Is the issue that torch.allclose now fails due to the ordering of per_sample_grad and ft_per_sample_grad being different during zip?


# Print differences instead of asserting
max_diff = (per_sample_grad - ft_per_sample_grad).abs().max().item()
print(f"Parameter {name}: max difference = {max_diff}")
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we printing this? On a side note, could you share what this prints with the 2.8 RC?


# Optional: still assert for very large differences that might indicate real problems
assert max_diff < 0.5, f"Extremely large difference in {name}: {max_diff}"
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did we change this to not use allclose anymore?


######################################################################
# A quick note: there are limitations around what types of functions can be
Expand Down
Loading