Repository navigation
[Warp] Part 1a/8: Resolve id and mask selections through shared helpers #8821
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
5676dd1
97e5662
92590d0
f044c92
7b48c38
3d3661f
43bb968
e390289
13ec11c
784b0a2
6a4f173
bc9a8bb
981e3a4
cff8f86
bbb5104
029ee47
551e508
86ce1c9
b37d231
dc2aba6
92a413e
efee1b9
b7d5295
c5299e5
3635089
ddcd317
12270ab
22b9aed
c94a9f3
6afa0a2
47b5499
b75b5a8
2a65a30
84470f4
11d840a
f522b47
e6e178c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| * Added :func:`~isaaclab.utils.warp.utils.resolve_ids`, which resolves an index selection given as ids or as a | ||
| boolean mask to ids on a device, as :func:`~isaaclab.utils.warp.utils.resolve_1d_mask` does to a mask. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| * Changed the env selection helpers of :class:`~isaaclab.utils.wrench_composer.WrenchComposer`, | ||
| :class:`~isaaclab.sensors.SensorBase` and :class:`~isaaclab.sensors.Camera` to resolve ``env_ids`` and ``env_mask`` | ||
| through :func:`~isaaclab.utils.warp.utils.resolve_ids` and :func:`~isaaclab.utils.warp.utils.resolve_1d_mask`. | ||
| Any integer sequence and slices with a negative step are accepted, ids on another device are moved to the owner's | ||
| device, and a :class:`~isaaclab.utils.warp.ProxyArray` is rejected with a :class:`TypeError`. | ||
| * Changed :class:`~isaaclab.utils.wrench_composer.WrenchComposer` to resolve ``body_ids`` through | ||
| :func:`~isaaclab.utils.warp.utils.resolve_ids` as well, so slices other than ``slice(None)`` are accepted. | ||
| * Changed :func:`~isaaclab.utils.warp.utils.resolve_1d_mask` to make ``scratch_mask`` optional for callers that never | ||
| pass ids. Ids without a ``scratch_mask`` raise a :class:`ValueError`. | ||
| * Changed :func:`~isaaclab.utils.warp.utils.resolve_1d_mask` to copy a Warp mask on another device to ``device``, and | ||
| to accept a boolean torch mask on ``device`` inside a CUDA graph capture. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| * Fixed the ``write_nodal_*_to_sim_mask`` methods of :class:`~isaaclab.assets.BaseDeformableObject`, which the | ||
| OVPhysX deformable object inherits, raising a :class:`RuntimeError` whenever ``env_mask`` was given. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,8 @@ | |
| import torch | ||
| import warp as wp | ||
|
|
||
| from .proxy_array import ProxyArray | ||
|
|
||
| ## | ||
| # Mask resolution - ids/mask to warp boolean mask. | ||
| ## | ||
|
|
@@ -30,7 +32,7 @@ def resolve_1d_mask( | |
| ids: Sequence[int] | slice | torch.Tensor | wp.array | None = None, | ||
| mask: wp.array | torch.Tensor | None = None, | ||
| all_mask: wp.array, | ||
| scratch_mask: wp.array, | ||
| scratch_mask: wp.array | None = None, | ||
| device: str, | ||
| ) -> wp.array: | ||
| """Resolve ids/mask into a warp boolean mask. | ||
|
|
@@ -41,30 +43,36 @@ def resolve_1d_mask( | |
| Args: | ||
| ids: Index ids. Accepts ``Sequence[int]``, ``slice``, ``torch.Tensor``, | ||
| ``wp.array(dtype=wp.int32)``, or ``None`` (all elements). | ||
| mask: Direct boolean mask. ``wp.array`` is returned as-is; | ||
| mask: Direct boolean mask. ``wp.array`` is returned as-is, or copied when on another device; | ||
| ``torch.Tensor`` is converted. | ||
| all_mask: Pre-allocated all-True mask returned when both *ids* and *mask* | ||
| are ``None``. | ||
| scratch_mask: Pre-allocated scratch buffer populated in-place when *ids* | ||
| are provided. Not re-entrant (shared buffer). | ||
| are provided. Not re-entrant (shared buffer). Defaults to None, for callers | ||
| that never pass *ids*. | ||
| device: Warp device string (e.g. ``"cuda:0"``). | ||
|
|
||
| Returns: | ||
| A ``wp.array(dtype=wp.bool)`` mask. | ||
|
|
||
| Raises: | ||
| ValueError: If *ids* select a subset of elements and no ``scratch_mask`` is given. | ||
| """ | ||
| # Normalize slice(None) to None so the capture guard treats it identically to ids=None. | ||
| if isinstance(ids, slice) and ids == slice(None): | ||
| ids = None | ||
|
|
||
| if wp.get_device().is_capturing: | ||
| if ids is not None or (mask is not None and not isinstance(mask, wp.array)): | ||
| if ids is not None or (mask is not None and _mask_needs_copy(mask, device)): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Experimental review tooling · medium · bug Now that the experimental API uses this helper, rejecting every non-None
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Main's |
||
| raise RuntimeError( | ||
| "resolve_1d_mask is only capturable when mask is a wp.array or both ids and mask are None." | ||
| "resolve_1d_mask is only capturable when ids are None and mask is None or a boolean mask on device." | ||
| ) | ||
|
|
||
| # --- Direct mask input --- | ||
| if mask is not None: | ||
| if isinstance(mask, wp.array): | ||
| if mask.device != wp.get_device(device): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Warning · Style Consistency — Stale allocation guarantee in docstring This cross-device clone allocates, but the docstring above still states the function never allocates because callers pass pre-allocated buffers. Update that sentence and note scratch_mask is now optional.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reworded in 13ec11c: nothing is allocated for |
||
| mask = wp.clone(mask, device=device) | ||
| return mask | ||
| if isinstance(mask, torch.Tensor): | ||
| if mask.dtype != torch.bool: | ||
|
|
@@ -78,6 +86,9 @@ def resolve_1d_mask( | |
| if ids is None: | ||
| return all_mask | ||
|
|
||
| if scratch_mask is None: | ||
| raise ValueError("resolve_1d_mask needs a scratch_mask to write ids into.") | ||
|
|
||
| if isinstance(ids, slice): | ||
| scratch_mask.zero_() | ||
| wp.to_torch(scratch_mask)[ids] = True | ||
|
|
@@ -115,6 +126,104 @@ def resolve_1d_mask( | |
| return scratch_mask | ||
|
|
||
|
|
||
| def _mask_needs_copy(mask: wp.array | torch.Tensor, device: str) -> bool: | ||
| """Whether :func:`resolve_1d_mask` casts or copies ``mask``, which a graph capture cannot record.""" | ||
| if isinstance(mask, wp.array): | ||
| return mask.device != wp.get_device(device) | ||
| if isinstance(mask, torch.Tensor): | ||
| return mask.dtype != torch.bool or str(mask.device) != device | ||
| return True | ||
|
|
||
|
|
||
| ## | ||
| # Id resolution - ids/mask to ids. | ||
| ## | ||
|
|
||
|
|
||
| def resolve_ids( | ||
| *, | ||
| ids: Sequence[int] | slice | torch.Tensor | wp.array | None = None, | ||
| mask: wp.array | torch.Tensor | None = None, | ||
| all_ids: wp.array, | ||
| device: str, | ||
| dtype: type | None = None, | ||
| ) -> wp.array | torch.Tensor: | ||
| """Resolve ids/mask into ids on ``device``. The mask takes precedence over the ids. | ||
|
|
||
| Args: | ||
| ids: Index ids. Accepts ``Sequence[int]``, ``slice``, ``torch.Tensor``, ``wp.array``, | ||
| or ``None`` (all elements). Tensors and warp arrays keep their integer dtype | ||
| unless ``dtype`` is given. Pass ids from ``find_*(as_proxy=True)`` as ``.warp`` or ``.torch``. | ||
| mask: Boolean mask. Its ids are found with :func:`torch.nonzero`, which waits for | ||
| the device. | ||
| all_ids: Pre-allocated ids of every element, returned as-is when *ids* and *mask* | ||
| select every element. | ||
| device: Warp device string (e.g. ``"cuda:0"``). | ||
| dtype: Warp integer dtype the ids must have, for callers whose kernels take one | ||
| index dtype. The ids are then a contiguous 1-D ``wp.array`` of this dtype. | ||
| Defaults to None: a mask resolves to an int64 ``torch.Tensor``, and ids keep | ||
| their type and dtype. | ||
|
|
||
| Returns: | ||
| ``all_ids``, a ``torch.Tensor`` or a ``wp.array`` of ids on ``device``. | ||
|
|
||
| Raises: | ||
| TypeError: If *ids* is a :class:`ProxyArray`, which is output-only, *mask* is | ||
| neither a ``wp.array`` nor a ``torch.Tensor``, or ``dtype`` is given and the | ||
| ids are not 32-bit or 64-bit integers. | ||
| ValueError: If ``dtype`` is given and the ids are not one-dimensional. | ||
| """ | ||
| if mask is not None: | ||
| if isinstance(mask, wp.array): | ||
| mask = wp.to_torch(mask) | ||
| elif not isinstance(mask, torch.Tensor): | ||
| raise TypeError(f"Unsupported mask type: {type(mask)}") | ||
| ids = torch.nonzero(mask.to(wp.device_to_torch(device))).squeeze(-1) | ||
| else: | ||
| ids = _ids_on_device(ids, all_ids, device) | ||
| return ids if dtype is None else _ids_as_dtype(ids, dtype) | ||
|
|
||
|
|
||
| def _ids_on_device( | ||
| ids: Sequence[int] | slice | torch.Tensor | wp.array | None, all_ids: wp.array, device: str | ||
| ) -> wp.array | torch.Tensor: | ||
| """Ids on ``device``, keeping the dtype of tensors and warp arrays; lists and slices become ``wp.int32``.""" | ||
| if isinstance(ids, torch.Tensor): | ||
| return ids if str(ids.device) == str(wp.get_device(device)) else ids.to(wp.device_to_torch(device)) | ||
| if ids is None: | ||
| return all_ids | ||
| if isinstance(ids, wp.array): | ||
| return ids if ids.device == wp.get_device(device) else ids.to(device) | ||
| if isinstance(ids, ProxyArray): | ||
| raise TypeError("ProxyArray is output-only; pass .warp or .torch explicitly.") | ||
| if isinstance(ids, slice): | ||
| if ids == slice(None): | ||
| return all_ids | ||
| start, stop, step = ids.indices(all_ids.shape[0]) | ||
| if step > 0: | ||
| # a strided view of the caller's all-ids, no copy | ||
| return wp.from_torch(wp.to_torch(all_ids)[start:stop:step]) | ||
| return wp.array(list(range(start, stop, step)), dtype=wp.int32, device=device) | ||
| return wp.array(list(ids), dtype=wp.int32, device=device) | ||
|
|
||
|
|
||
| def _ids_as_dtype(ids: wp.array | torch.Tensor, dtype: type) -> wp.array: | ||
| """``ids`` as a contiguous 1-D ``wp.array`` of ``dtype`` on their device.""" | ||
| if dtype not in (wp.int32, wp.int64): | ||
| raise TypeError(f"Unsupported id dtype: {dtype}. Expected wp.int32 or wp.int64.") | ||
| if ids.ndim != 1: | ||
| raise ValueError(f"Ids must be a one-dimensional array, got {ids.ndim} dimensions.") | ||
| if isinstance(ids, wp.array): | ||
| if ids.dtype == dtype and ids.is_contiguous: | ||
| return ids | ||
| if ids.dtype not in (wp.int32, wp.int64): | ||
| raise TypeError(f"Warp ids must be 32-bit or 64-bit signed integers, got {ids.dtype}.") | ||
| ids = wp.to_torch(ids) | ||
| elif ids.dtype.is_floating_point or ids.dtype.is_complex or ids.dtype == torch.bool: | ||
| raise TypeError(f"Ids must be integers, got {ids.dtype}.") | ||
| return wp.from_torch(ids.to(wp.dtype_to_torch(dtype)).contiguous(), dtype=dtype) | ||
|
|
||
|
|
||
| ## | ||
| # Capture safety — property guard. | ||
| ## | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Experimental review tooling · medium · bug
Cross-device Warp IDs are not moved by
resolve_1d_mask: its Warp branch passesidsdirectly towp.launchondevice. For example, resetting a CUDA sensor with a CPUwp.array(dtype=wp.int32)fails with a device-mismatch error, despite the intended cross-device selection support. Move Warp IDs to the sensor's device in the shared helper before launching, and add a cross-device IDs test.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Main's
memory.resolve_1d_mask(#8843) now moves ids to the sensor's device, so this left #8821.