Skip to content

Fix selection slice shapes#792

Merged
nvlukasz merged 2 commits into
newton-physics:mainfrom
nvlukasz:fix-selection-slice-shapes
Sep 19, 2025
Merged

Fix selection slice shapes#792
nvlukasz merged 2 commits into
newton-physics:mainfrom
nvlukasz:fix-selection-slice-shapes

Conversation

@nvlukasz

@nvlukasz nvlukasz commented Sep 19, 2025

Copy link
Copy Markdown
Member

Description

  • Fix selection shape with fixed and floating base articulations.
  • Fix selection shape with empty slices.

Newton Migration Guide

Please ensure the migration guide for warp.sim users is up-to-date with the changes made in this PR.

  • The migration guide in docs/migration.rst is up-to date

Before your PR is "Ready for review"

  • Necessary tests have been added and new examples are tested (see newton/tests/test_examples.py)
  • Documentation is up-to-date
  • Code passes formatting and linting checks with pre-commit run -a

Summary by CodeRabbit

  • New Features

    • Direct integer-based slicing now supported, returning a collapsed axis.
  • Bug Fixes

    • Empty-slice selections preserve trailing dimensions and yield consistent zero-length shapes.
  • Performance

    • Reduced unnecessary memory copies for empty slices, improving slicing efficiency.
  • Tests

    • Updated test targeting to match model naming conventions.

@coderabbitai

coderabbitai Bot commented Sep 19, 2025

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Updates selection utility to handle integer indices directly, adjust empty-slice construction without pointer offset, preserve trailing dimensions for empty slices, and avoid copies. Test updated to use the anymal base path. No public signatures changed.

Changes

Cohort / File(s) Summary
Selection slicing logic
newton/_src/utils/selection.py
- Return attrib[:, _slice] immediately when _slice is an int.
- For empty slices (start == stop), construct a zero-width wp.array with shape (attrib.shape[0], 0, *attrib.shape[2:]), using the same base pointer (ptr=attrib.ptr), preserving strides, device, and pinned, and setting copy=False.
- Remove pointer-offset computation for empty-slice path; preserve trailing dimensions.
Test path update
newton/tests/test_anymal_reset.py
- Change ArticulationView base path in TestAnymalReset._setup_simulation from "*/Robot/base" to "*/anymal/base" to match model naming.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Caller
  participant Sel as selection._get_attribute_array
  participant WP as wp.array

  Caller->>Sel: _get_attribute_array(attrib, _slice)
  alt _slice is int
    Sel-->>Caller: return attrib[:, _slice]
  else _slice is slice
    alt empty slice (start==stop)
      Sel->>WP: construct array(shape=(attrib.shape[0], 0, *attrib.shape[2:]), ptr=attrib.ptr, strides=attrib.strides, device, pinned, copy=False)
      WP-->>Sel: zero-width view
      Sel-->>Caller: return view
    else non-empty slice
      Sel-->>Caller: process as existing slice path
    end
  end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested reviewers

  • AntoineRichard
  • eric-heiden

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title "Fix selection slice shapes" concisely and accurately summarizes the primary change in the changeset — fixes to slice handling (integer slices and empty-slice dimensionality) in the selection implementation — and aligns with the PR objectives and file diffs, making it clear for a teammate scanning history.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5ad6501 and 85845a1.

📒 Files selected for processing (1)
  • newton/tests/test_anymal_reset.py (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
newton/tests/test_anymal_reset.py (1)
newton/_src/sim/joints.py (1)
  • JointType (20-44)
⏰ Context from checks skipped due to timeout of 900000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: Run GPU Benchmarks (Pull Request)
  • GitHub Check: Run GPU Unit Tests on AWS EC2 (Pull Request)
  • GitHub Check: run-newton-tests / newton-unittests (ubuntu-latest)
  • GitHub Check: run-newton-tests / newton-unittests (windows-latest)
🔇 Additional comments (1)
newton/tests/test_anymal_reset.py (1)

118-119: ArticulationView path update to "*/anymal/base" — repo check OK; USD asset must be confirmed.

  • Located ArticulationView(self.model, "*/anymal/base", ...) at newton/tests/test_anymal_reset.py:118; ripgrep shows no remaining "/Robot/base" references repo‑wide.
  • The anymal USD is an external/downloaded asset (anybotics_anymal_d); cannot verify prim contents from the repo — confirm anymal_d.usda contains prim "anymal/base".
  • Ensure selection tests cover integer indexing and empty slices for both fixed and floating bases.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@nvlukasz nvlukasz enabled auto-merge (squash) September 19, 2025 14:04

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
newton/_src/utils/selection.py (2)

548-559: Type hints drift: accept Slice | int | None in public helpers.

Both helpers now legitimately pass int through to _get_attribute_array. Update the annotations to match and reduce static‑analysis noise.

-    def _get_attribute_values(self, name: str, source: Model | State | Control, _slice: slice | None = None):
+    def _get_attribute_values(self, name: str, source: Model | State | Control, _slice: Slice | int | None = None):

-    def _set_attribute_values(
-        self, name: str, target: Model | State | Control, values, mask=None, _slice: slice | None = None
-    ):
+    def _set_attribute_values(
+        self, name: str, target: Model | State | Control, values, mask=None, _slice: Slice | int | None = None
+    ):

168-171: CI failure: make the “no matches” error actionable (and double‑check test patterns).

GPU tests failed with KeyError: 'No matching articulations'. Improve the message to include the pattern to speed up triage, and verify the test uses a valid pattern on CI fixtures.

-            raise KeyError("No matching articulations")
+            raise KeyError(f"No matching articulations for pattern '{pattern}'")
📜 Review details

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 08ff1bf and 5ad6501.

📒 Files selected for processing (1)
  • newton/_src/utils/selection.py (1 hunks)
🧰 Additional context used
🪛 Ruff (0.12.2)
newton/_src/utils/selection.py

521-521: Avoid specifying long messages outside the exception class

(TRY003)


526-526: Shebang should contain python, pytest, or uv run

(EXE003)


526-526: Shebang is present but file is not executable

(EXE001)


526-526: Shebang should be at the beginning of the file

(EXE005)

🪛 GitHub Actions: GPU Unit Tests on AWS EC2
newton/_src/utils/selection.py

[error] 169-169: KeyError: 'No matching articulations' raised during ArticulationView initialization.

⏰ Context from checks skipped due to timeout of 900000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Run GPU Benchmarks (Pull Request)
  • GitHub Check: run-newton-tests / newton-unittests (windows-latest)
  • GitHub Check: run-newton-tests / newton-unittests (ubuntu-latest)
🔇 Additional comments (2)
newton/_src/utils/selection.py (2)

519-521: Int indexing path: shape collapse is intentional—please confirm call‑site expectations and add tests.

Returning attrib[:, _slice] for int collapses the frequency axis. This looks correct for fixed/floating‑base roots, but please:

  • Verify negative indices behave as expected across CPU/GPU.
  • Add shape assertions/tests (e.g., transform/spatial_vector/float dtypes).

526-536: Empty‑slice construction LGTM; fix linter false positive and confirm stride safety.

  • Good: preserving trailing dims and using copy=False with base ptr avoids copies and keeps shapes consistent.
  • Lint: #! is parsed as a shebang mid‑file. Replace with plain # to silence EXE00x.

Apply this diff:

-                #! workaround for empty slice until this is fixed: https://github.com/NVIDIA/warp/issues/958
+                # workaround for empty slice until this is fixed: https://github.com/NVIDIA/warp/issues/958

Also, please run a quick GPU/CPU test to ensure a zero-length, strided view is accepted by Warp kernels for float/transform/spatial_vector.

AntoineRichard
AntoineRichard previously approved these changes Sep 19, 2025

@AntoineRichard AntoineRichard left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM thanks Lukasz!

@preist-nvidia preist-nvidia left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the UT fix!

@nvlukasz nvlukasz merged commit 9714e98 into newton-physics:main Sep 19, 2025
12 checks passed
eric-heiden pushed a commit to eric-heiden/newton that referenced this pull request Jan 28, 2026
mmacklin pushed a commit to mmacklin/newton that referenced this pull request Apr 7, 2026
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.

3 participants