Skip to content

ArticulationView get_link_velocities detaches gradients #500

@pschoc

Description

@pschoc

I have set up a global model:
self.model = self.builder.finalize(requires_grad=False)

then I have created an ArticulationView object to conveniently reference different joints of this robot:
self.robot = ArticulationView(self.model, "fr3", verbose=True, exclude_joint_types=[newton.JOINT_FREE])

Now say I have a state reference with grads enabled:
temp_state_for_jacobian = self.model.state(requires_grad=True)

(checking via debugger that gradients are really attached: temp_state_for_jacobian.body_qd.requires_grad = True)

however, when trying to access body_qd via the ArticulationView interface, my gradients get detached:
self.robot.get_attribute("body_qd", temp_state_for_jacobian)[0].requires_grad = False
or
self.robot.get_link_velocities(temp_state_for_jacobian).requires_grad = False

this is not the case for joint positions and velocities, therefore I checked the getter inside selection.py and saw that the staging_array access is preferred if available

original code:

def _get_attribute_values(self, name: str, source: Model | State | Control, _slice: slice | None = None):
attrib = self._get_attribute_array(name, source, _slice=_slice)
if hasattr(attrib, "_staging_array"):
wp.copy(attrib._staging_array, attrib)
return attrib._staging_array
else:
return attrib

proposing to check if the original tensor requires grad:
def _get_attribute_values(self, name: str, source: Model | State | Control, _slice: slice | None = None):
attrib = self._get_attribute_array(name, source, _slice=_slice)
if hasattr(attrib, "data") and attrib.data.requires_grad:
return attrib.data
else:
if hasattr(attrib, "_staging_array"):
wp.copy(attrib._staging_array, attrib)
return attrib._staging_array
else:
return attrib

I dont fully understand what the correct fix would be but this worked for my case

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

Status

In Progress

Relationships

None yet

Development

No branches or pull requests

Issue actions