Updates contact sensor API to use Newton new API that allows fast initialization.#4883
Conversation
Greptile SummaryThis PR delivers a significant performance optimization for multi-world contact sensor construction — reducing Allegro finger sensor setup from ~80 s to under 1 s — by extracting pattern resolution and replicated-kernel construction into a new Key concerns to address before merging:
Confidence Score: 3/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant Caller
participant NewtonManager
participant build_contact_sensor
participant _build_replicated
participant _ReplicatedContactSensor
participant NewtonContactSensor
Caller->>NewtonManager: add_contact_sensor(expr, prune_noncolliding)
NewtonManager->>build_contact_sensor: build_contact_sensor(model, expr, prune_noncolliding)
alt world_count == 1
build_contact_sensor->>NewtonContactSensor: NewtonContactSensor(model, indices, prune_noncolliding)
NewtonContactSensor-->>build_contact_sensor: sensor
build_contact_sensor-->>NewtonManager: NewtonContactSensor
else world_count > 1 (replicated)
build_contact_sensor->>_build_replicated: _build_replicated(model, ..., verbose)<br/>[prune_noncolliding NOT forwarded]
_build_replicated->>_build_replicated: resolve world-0 indices
_build_replicated->>NewtonContactSensor: NewtonContactSensor(world-0 indices,<br/>prune_noncolliding=False [hardcoded])
NewtonContactSensor-->>_build_replicated: template
_build_replicated->>_build_replicated: build sp_sorted_local (localise shape pairs)
_build_replicated->>_ReplicatedContactSensor: new(template, n_worlds, shape_world, ...)
_ReplicatedContactSensor-->>_build_replicated: sensor
_build_replicated-->>build_contact_sensor: _ReplicatedContactSensor
build_contact_sensor-->>NewtonManager: _ReplicatedContactSensor
end
NewtonManager->>NewtonManager: store sensor_key → sensor
NewtonManager-->>Caller: sensor_key
|
| logger.info(f"Finalizing model on device: {device}") | ||
| cls._builder.up_axis = Axis.from_string(cls._up_axis) | ||
| # Set smaller contact margin for manipulation examples (default 10cm is too large) |
There was a problem hiding this comment.
Silent removal of custom contact margin
The line cls._builder.default_shape_cfg.contact_margin = 0.01 was removed. The adjacent comment that remains in the diff context explicitly described why this setting existed: "Set smaller contact margin for manipulation examples (default 10cm is too large)". By removing the override, all simulations now revert to Newton's default 10 cm contact margin.
This is a physics-behavior-changing removal that is not mentioned in the CHANGELOG.rst entry for 0.5.3. Manipulation scenarios (especially the Allegro finger tests added in this same PR) may be affected. Please either:
- Restore the
contact_marginoverride (with a config option to override it), or - Add a
Changedentry to the changelog explaining this intentional revert and its implications.
| ) | ||
|
|
||
| sp_np = template._sp_sorted.numpy() | ||
| sw0 = int(shape_start[0]) | ||
| sp_local = np.empty_like(sp_np) | ||
| for i in range(sp_np.shape[0]): | ||
| a, b = int(sp_np[i][0]), int(sp_np[i][1]) | ||
| sp_local[i][0] = -1 if a == -1 else a - sw0 | ||
| sp_local[i][1] = -1 if b == -1 else b - sw0 | ||
| sp_sorted_local = wp.array(sp_local, dtype=wp.vec2i, device=model.device) | ||
|
|
||
| return _ReplicatedContactSensor( |
There was a problem hiding this comment.
Reliance on Newton private API
_ReplicatedContactSensor.__init__ reads five private attributes of the NewtonContactSensor template:
template._n_shape_pairstemplate._sp_readingtemplate._sp_ep_offsettemplate._sp_ep_count
And in _build_replicated at line 559:
template._sp_sorted
These names are implementation details of Newton's SensorContact class. Any upstream refactor (attribute rename, restructure) will silently produce wrong results or an AttributeError at runtime. Since the PR already notes that _bisect_shape_pairs had to be duplicated because Warp kernels can't call foreign-module functions at compile time, this coupling is unavoidable to some degree — but it should at minimum be documented clearly (e.g., a version pin comment or an assertion like assert hasattr(template, "_sp_sorted"), "Newton private API changed").
AntoineRichard
left a comment
There was a problem hiding this comment.
It looks reasonable to me! @camevor could you take a look?
There was a problem hiding this comment.
Do we also want to add the exact same tests as PhysX? I think it can be nice to validate that the behavior between PhysX and Newton is similar.
There was a problem hiding this comment.
Do we want to bring these to PhysX too?
There was a problem hiding this comment.
Same here, do we want to bring that to PhysX?
AntoineRichard
left a comment
There was a problem hiding this comment.
After talking with @camevor i don't think we should merge this in it's current state. Newton, will provide an improved sensor that supports "cloner style" logic: newton-physics/newton#1997
2cdac66 to
5c77631
Compare
ac7d0b6 to
6b6e858
Compare
need merge, concern already addressed
…tialization. (isaac-sim#4883) # Description - This PR add contact sensor test and test utilities to newton contacts sensors Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. ## Type of change <!-- As you go through the list, delete the ones that are not applicable. --> - Bug fix (non-breaking change which fixes an issue) ## Screenshots Please attach before and after screenshots of the change if applicable. <!-- Example: | Before | After | | ------ | ----- | | _gif/png before_ | _gif/png after_ | To upload images to a PR -- simply drag and drop an image while in edit mode and it should upload the image directly. You can then paste that source into the above before/after sections. --> ## Checklist - [x] I have read and understood the [contribution guidelines](https://isaac-sim.github.io/IsaacLab/main/source/refs/contributing.html) - [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 - [x] I have added tests that prove my fix is effective or that my feature works - [x] 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 <!-- As you go through the checklist above, you can mark something as done by putting an x character in it For example, - [x] I have done this task - [ ] I have not done this task -->
## Summary Fix incorrect attribute name `contact_margin` → `gap` on Newton `ShapeConfig` in `NewtonManager.create_builder()`. Newton PR #1732 renamed `contact_margin` to `gap` (broad-phase AABB expansion). The IsaacLab code was never updated, creating a dead attribute that Python silently accepted. The intended 1 cm default shape gap was never applied. ## Newton PR #1732 rename mapping | Old field | New field | Semantics | |-----------|-----------|-----------| | `thickness` | `margin` | Shape surface extension (affects contact forces) | | `contact_margin` | **`gap`** | Broad-phase AABB expansion (detection range only) | | `rigid_contact_margin` | `rigid_gap` | Builder-level default gap (already 0.1) | ## Timeline | Date | Newton | IsaacLab | `contact_margin` valid? | |------|--------|----------|------------------------| | Feb 19 | pin: `51ce35e` (has `contact_margin`) | #4646 adds `contact_margin = 0.01` | Yes | | Feb 24 | **PR #1732 renames `contact_margin` → `gap`** | — | — | | Mar 2 | pin updated to `v0.2.3` (after rename) | #4761 keeps `contact_margin` | **No — broken** | | Mar 9 | — | #4883 removes the line | Removed | | Apr 13 | — | #4822 re-adds `contact_margin` | **No — still broken** | ## Ablation: gap vs margin We conducted an ablation study to understand the impact. Note: `margin` (shape surface extension) is a different field from `gap` (broad-phase range). The original code intended to set `gap`, but setting `margin` also has a significant positive effect on training for rough terrain locomotion. | Setting | `gap` | `margin` | Go1 Reward (300 iter) | Effect | |---------|-------|----------|----------------------|--------| | Dead field (baseline) | 0.1 (default) | 0.0 | ~1.0 | — | | `gap=0.01` only | 0.01 | 0.0 | 0.66 | No training improvement | | `margin=0.01` only | 0.1 (default) | 0.01 | 18.77 | Major improvement | | `gap=0.01` + `margin=0.01` | 0.01 | 0.01 | 16.54 | Similar to margin-only | **This PR fixes the correct field migration** (`contact_margin` → `gap`). The `margin` setting for rough terrain contact quality will be addressed separately in the rough terrain env PR (#5248) via a new `default_shape_margin` config field on `NewtonCfg`. ## Test plan - [x] Verified `contact_margin` is not a field on `ShapeConfig` (Newton 1.1.0.dev0) - [x] Verified `gap` is the correct replacement field per Newton PR #1732 - [x] Confirmed by camevor (Newton developer) - [x] Ablation study: gap alone doesn't change training, so existing tests should pass Co-authored-by: Antoine RICHARD <antoiner@nvidia.com>
Description
Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context.
List any dependencies that are required for this change.
Type of change
Screenshots
Please attach before and after screenshots of the change if applicable.
Checklist
pre-commitchecks with./isaaclab.sh --formatconfig/extension.tomlfileCONTRIBUTORS.mdor my name already exists there