Unverified Commit 649ad88b authored by Louis LE LAY's avatar Louis LE LAY Committed by GitHub

Fixes tensor construction warning in `events.py` (#3251)

# Description

This PR removes a `UserWarning` from PyTorch about using
`torch.tensor()` on an existing tensor in `events.py`. It replaces
`torch.tensor(actuator.joint_indices, device=asset.device)` with
`.to(device)` to avoid unnecessary copies.

Warning mentionned:

```bash
/home/spring/IsaacLab/source/isaaclab/isaaclab/envs/mdp/events.py:542: 
UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor). actuator_joint_indices = torch.tensor(actuator.joint_indices, device=asset.device)
```

## Type of change

- Bug fix (non-breaking change which fixes an issue)

## Checklist

- [x] I have run the [`pre-commit` checks](https://pre-commit.com/) with
`./isaaclab.sh --format`
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] I have updated the changelog and the corresponding version in the
extension's `config/extension.toml` file
- [x] I have added my name to the `CONTRIBUTORS.md` or my name already
exists there

---------
Signed-off-by: 's avatarLouis LE LAY <le.lay.louis@gmail.com>
Co-authored-by: 's avatarooctipus <zhengyuz@nvidia.com>
Co-authored-by: 's avatarMayank Mittal <12863862+Mayankm96@users.noreply.github.com>
parent b7a46b52
...@@ -596,14 +596,16 @@ class randomize_actuator_gains(ManagerTermBase): ...@@ -596,14 +596,16 @@ class randomize_actuator_gains(ManagerTermBase):
actuator_indices = slice(None) actuator_indices = slice(None)
if isinstance(actuator.joint_indices, slice): if isinstance(actuator.joint_indices, slice):
global_indices = slice(None) global_indices = slice(None)
elif isinstance(actuator.joint_indices, torch.Tensor):
global_indices = actuator.joint_indices.to(self.asset.device)
else: else:
global_indices = torch.tensor(actuator.joint_indices, device=self.asset.device) raise TypeError("Actuator joint indices must be a slice or a torch.Tensor.")
elif isinstance(actuator.joint_indices, slice): elif isinstance(actuator.joint_indices, slice):
# we take the joints defined in the asset config # we take the joints defined in the asset config
global_indices = actuator_indices = torch.tensor(self.asset_cfg.joint_ids, device=self.asset.device) global_indices = torch.tensor(self.asset_cfg.joint_ids, device=self.asset.device)
else: else:
# we take the intersection of the actuator joints and the asset config joints # we take the intersection of the actuator joints and the asset config joints
actuator_joint_indices = torch.tensor(actuator.joint_indices, device=self.asset.device) actuator_joint_indices = actuator.joint_indices
asset_joint_ids = torch.tensor(self.asset_cfg.joint_ids, device=self.asset.device) asset_joint_ids = torch.tensor(self.asset_cfg.joint_ids, device=self.asset.device)
# the indices of the joints in the actuator that have to be randomized # the indices of the joints in the actuator that have to be randomized
actuator_indices = torch.nonzero(torch.isin(actuator_joint_indices, asset_joint_ids)).view(-1) actuator_indices = torch.nonzero(torch.isin(actuator_joint_indices, asset_joint_ids)).view(-1)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment