Skip to content

Commit

Permalink
[BugFix] Torch 1.13 compat (#1294)
Browse files Browse the repository at this point in the history
  • Loading branch information
vmoens authored Jun 17, 2023
1 parent 593961e commit 3b8e128
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
14 changes: 12 additions & 2 deletions torchrl/objectives/value/advantages.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@
vec_td_lambda_return_estimate,
)

try:
from torch import vmap
except ImportError as err:
try:
from functorch import vmap
except ImportError:
raise ImportError(
"vmap couldn't be found. Make sure you have torch>1.13 installed."
) from err


def _self_set_grad_enabled(fun):
@wraps(fun)
Expand Down Expand Up @@ -111,9 +121,9 @@ def _call_value_nets(
)
elif params is not None:
params_stack = torch.stack([params, next_params], 0)
data_out = torch.vmap(value_net, (0, 0))(data_in, params_stack)
data_out = vmap(value_net, (0, 0))(data_in, params_stack)
else:
data_out = torch.vmap(value_net, (0,))(data_in)
data_out = vmap(value_net, (0,))(data_in)
value_est = data_out.get(value_key)
value, value_ = value_est[0], value_est[1]
data.set(value_key, value)
Expand Down
4 changes: 2 additions & 2 deletions tutorials/sphinx-tutorials/torchrl_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,10 +474,10 @@

###############################################################################

import functorch
from torch import vmap

params_expand = params.expand(4)
tensordict_exp = functorch.vmap(sequence, (None, 0))(tensordict, params_expand)
tensordict_exp = vmap(sequence, (None, 0))(tensordict, params_expand)
print(tensordict_exp)

###############################################################################
Expand Down

1 comment on commit 3b8e128

@github-actions
Copy link

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'CPU Benchmark Results'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 3b8e128 Previous: 593961e Ratio
benchmarks/test_replaybuffer_benchmark.py::test_iterate_rb[TensorDictPrioritizedReplayBuffer-LazyMemmapStorage-None-10000] 14.669207694211515 iter/sec (stddev: 0.09292552904622331) 32.867065367879405 iter/sec (stddev: 0.0005792337951938823) 2.24

This comment was automatically generated by workflow using github-action-benchmark.

CC: @vmoens

Please sign in to comment.