Skip to content

fix: add try-except block for Isaac Lab compatibility (#9)#10

Open
zbbright233 wants to merge 2 commits into
isaac-sim:mainfrom
zbbright233:fix-isaaclab-compatibility
Open

fix: add try-except block for Isaac Lab compatibility (#9)#10
zbbright233 wants to merge 2 commits into
isaac-sim:mainfrom
zbbright233:fix-isaaclab-compatibility

Conversation

@zbbright233

@zbbright233 zbbright233 commented Jun 23, 2026

Copy link
Copy Markdown

What does this PR do?

Fixes an AttributeError: 'Tensor' object has no attribute 'torch' when running training scripts with recent Isaac Lab updates (Extension version 0.54.4+), where physics buffers now natively return standard PyTorch Tensors.

How is it resolved?

To maintain backward compatibility for users still on older versions of Isaac Lab, I wrapped the data fetching in a try-except block. This ensures the code runs flawlessly on both old and new versions of Isaac Lab.

try:
    # Works for older Isaac Lab versions
    self.forwards = math_utils.quat_apply(self.robot.data.root_link_quat_w.torch, self.robot.data.FORWARD_VEC_B.torch)
except AttributeError:
    # Works for recent Isaac Lab (0.54.4+)
    self.forwards = math_utils.quat_apply(self.robot.data.root_link_quat_w, self.robot.data.FORWARD_VEC_B)

Fixes #9

@greptile-apps

greptile-apps Bot commented Jun 23, 2026

Copy link
Copy Markdown

Greptile Summary

This PR fixes a compatibility issue in _get_observations where root_link_quat_w and FORWARD_VEC_B used to return wrapped buffers requiring a .torch accessor, but now return plain PyTorch Tensors on Isaac Lab 0.54.4+. The implementation (which differs from the try-except shown in the PR description) correctly uses hasattr to detect the API shape.

Confidence Score: 5/5

Safe to merge — the change is a narrow, well-scoped fix that correctly handles both Isaac Lab API shapes without breaking existing behavior.

The hasattr-based guard correctly replaces the original hard-coded .torch calls and is functionally correct for both old and new Isaac Lab versions. No correctness or data-loss risk is introduced.

No files require special attention.

Important Files Changed

Filename Overview
source/isaac_lab_tutorial/isaac_lab_tutorial/tasks/direct/isaac_lab_tutorial/isaac_lab_tutorial_env.py Replaces the original .torch accessor calls with a hasattr-guarded block, correctly supporting both old and new Isaac Lab API shapes; minor inefficiency of re-evaluating hasattr every step.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["_get_observations() called\n(every env step)"] --> B["Fetch root_link_quat_w\nand FORWARD_VEC_B"]
    B --> C{"hasattr(quat, 'torch')?"}
    C -- Yes\nOld Isaac Lab --> D["quat = quat.torch\nfwd_vec = fwd_vec.torch"]
    C -- No\nIsaac Lab 0.54.4+ --> E["Use tensors directly"]
    D --> F["math_utils.quat_apply(quat, fwd_vec)"]
    E --> F
    F --> G["Compute observations\n(dot, cross, forward_speed)"]
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
flowchart TD
    A["_get_observations() called\n(every env step)"] --> B["Fetch root_link_quat_w\nand FORWARD_VEC_B"]
    B --> C{"hasattr(quat, 'torch')?"}
    C -- Yes\nOld Isaac Lab --> D["quat = quat.torch\nfwd_vec = fwd_vec.torch"]
    C -- No\nIsaac Lab 0.54.4+ --> E["Use tensors directly"]
    D --> F["math_utils.quat_apply(quat, fwd_vec)"]
    E --> F
    F --> G["Compute observations\n(dot, cross, forward_speed)"]
Loading

Reviews (2): Last reviewed commit: "refactor: optimize compatibility check u..." | Re-trigger Greptile

Comment on lines +107 to +112
try:
# Works for older Isaac Lab versions where buffers are wrapped
self.forwards = math_utils.quat_apply(self.robot.data.root_link_quat_w.torch, self.robot.data.FORWARD_VEC_B.torch)
except AttributeError:
# Works for recent Isaac Lab (0.54.4+) where buffers natively return standard PyTorch Tensors
self.forwards = math_utils.quat_apply(self.robot.data.root_link_quat_w, self.robot.data.FORWARD_VEC_B)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Exception thrown on every training step on new Isaac Lab

On Isaac Lab 0.54.4+, the try branch always raises AttributeError (because .torch no longer exists), which is then caught by except. Since _get_observations is called every environment step during training — potentially millions of times — this results in millions of exceptions being raised and swallowed silently. Python exception handling has non-trivial overhead compared to a conditional check, and the pattern also risks masking unrelated AttributeErrors raised inside quat_apply itself.

Prefer detecting the API shape once (e.g., with hasattr) rather than relying on exception control flow in a hot loop.

Suggested change
try:
# Works for older Isaac Lab versions where buffers are wrapped
self.forwards = math_utils.quat_apply(self.robot.data.root_link_quat_w.torch, self.robot.data.FORWARD_VEC_B.torch)
except AttributeError:
# Works for recent Isaac Lab (0.54.4+) where buffers natively return standard PyTorch Tensors
self.forwards = math_utils.quat_apply(self.robot.data.root_link_quat_w, self.robot.data.FORWARD_VEC_B)
# Support both older Isaac Lab (wrapped buffers with .torch accessor) and
# newer Isaac Lab 0.54.4+ (native PyTorch Tensors returned directly).
quat = self.robot.data.root_link_quat_w
fwd_vec = self.robot.data.FORWARD_VEC_B
if hasattr(quat, "torch"):
quat = quat.torch
fwd_vec = fwd_vec.torch
self.forwards = math_utils.quat_apply(quat, fwd_vec)

@zbbright233

Copy link
Copy Markdown
Author

Thanks for the great feedback regarding the performance overhead in the hot loop! I have refactored the compatibility logic using hasattr as suggested to keep the execution fast and clean.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] AttributeError: 'Tensor' object has no attribute 'torch' in direct task template with recent Isaac Lab (0.54.4)

1 participant