Skip to content

perf(dex-hand): run retargeting optimizer under torch.no_grad#478

Open
nvddr wants to merge 2 commits intomainfrom
perf/dex-hand-no-grad
Open

perf(dex-hand): run retargeting optimizer under torch.no_grad#478
nvddr wants to merge 2 commits intomainfrom
perf/dex-hand-no-grad

Conversation

@nvddr
Copy link
Copy Markdown
Contributor

@nvddr nvddr commented May 7, 2026

Summary

  • DexHandRetargeter._compute_hand was wrapping every call to self._dex_hand.retarget(...) in torch.enable_grad() + torch.inference_mode(False). The dex_retargeting QP solver does not consume those gradients, so each frame paid for autograd bookkeeping that was immediately discarded.
  • Switch to torch.no_grad() so forward tensor ops inside the optimizer skip grad tracking entirely. The optimizer is unchanged; only the surrounding context flips.

Why this matters

At 90 Hz with two DexHandRetargeter instances (bimanual), the per-frame cost adds up. Disabling grad tracking on the hot path is a free win unless dex_retargeting internally requires autograd (which it does not — it's a NLOpt-style optimization).

Risk

The previous explicit enable_grad() + inference_mode(False) reads as defensive escape from an outer no-grad context, but dex_retargeting's retarget() does not differentiate through any tensor operation, so running under no_grad is safe. If any downstream code actually relies on graph capture from retarget(...) (none in this repo), CI will flag it.

Test plan

  • ctest retargeting suite green
  • Optional: run an existing dex bimanual example; confirm output joints match the previous run on the same recorded input.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Optimized hand retargeting execution context for improved performance during retargeting operations.

DexHandRetargeter._compute_hand wrapped the per-frame call to
``self._dex_hand.retarget(...)`` in ``torch.enable_grad()`` and
``torch.inference_mode(False)`` — every step paid for autograd
bookkeeping the dex_retargeting QP solver does not consume. Switch to
``torch.no_grad()`` so forward ops in the optimizer skip grad tracking
entirely.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings May 7, 2026 20:39
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 7, 2026

Review Change Stack

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 22965c1e-9b87-41c2-bf38-06f40bbee7f4

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

The PR modifies the DexHandRetargeter._compute_hand method to simplify its PyTorch execution context. Previously, the retargeting call was wrapped in torch.enable_grad() followed by torch.inference_mode(False). The change replaces this with a single torch.no_grad() context, maintaining the same try/except error handling and return behavior while altering only the gradient computation mode during retargeting.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and concisely describes the main change: running the retargeting optimizer under torch.no_grad() for performance improvement.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch perf/dex-hand-no-grad

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes the dex-hand retargeting hot path by disabling PyTorch autograd tracking around the dex_retargeting solver call, aiming to reduce per-frame overhead in DexHandRetargeter.

Changes:

  • Replace torch.enable_grad() + torch.inference_mode(False) with torch.no_grad() around self._dex_hand.retarget(...).
  • Add an explanatory comment documenting the rationale for disabling grad tracking.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +415 to 423
# ``dex_retargeting`` solves a QP-style optimization that does not
# require autograd; running under ``torch.no_grad()`` avoids the
# per-step grad-tracking overhead the previous ``enable_grad`` /
# ``inference_mode(False)`` context was incurring on every frame.
try:
import torch # type: ignore

with torch.enable_grad(), torch.inference_mode(False):
with torch.no_grad():
return self._dex_hand.retarget(ref_value) # type: ignore
Copilot review on #478 flagged that switching the per-frame context
from ``torch.enable_grad(), torch.inference_mode(False)`` to
``torch.no_grad()`` alone drops the explicit opt-out from any outer
``torch.inference_mode()``. Some in-place / view ops in dex_retargeting
can error under inference mode, so a caller wrapping the session in
``torch.inference_mode()`` would now break.

Combine both: ``torch.inference_mode(False), torch.no_grad()`` keeps the
escape from inference mode (preserving prior behaviour) while still
skipping autograd bookkeeping (the actual perf win).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.

2 participants