使用 ProxyArray#
Isaac Lab 数据类返回 ProxyArray ,这是以 Warp 为基础的轻量封装,可将仿真数据作为 warp.array 或 torch.Tensor 零拷贝访问。
备注
ProxyArray 的设计参考了 mujocolab/mjlab 中的 ProxyArray 类(BSD-3-Clause 许可),将同样的双访问器模式用于 Isaac Lab 基于 Warp 的数据管线。
快速开始#
资产和传感器数据类的每个属性,例如 robot.data.joint_pos 、 sensor.data.net_normal_forces_w ,都会返回 ProxyArray :
robot = env.scene["robot"]
# Explicit torch access (preferred)
joint_positions = robot.data.joint_pos.torch # torch.Tensor, cached zero-copy view
gravity_proj = robot.data.projected_gravity_b.torch # torch.Tensor
# Explicit warp access (for kernel interop)
wp_array = robot.data.joint_pos.warp # warp.array, the original buffer
# Pass directly to warp kernels — no unwrapping needed
wp.launch(my_kernel, inputs=[robot.data.joint_pos], ...) # works via __cuda_array_interface__
.torch 与 .warp 访问器#
.torch通过warp.to_torch(),返回底层 Warp 数组的缓存式零拷贝torch.Tensor视图。张量在首次访问时创建,后续调用复用。由于是零拷贝视图,修改张量也会反映在 Warp 数组中,反之亦然。.warp返回原始warp.array。需要显式向 Warp 核函数传入数据,或访问ptr、strides等 Warp 专用属性时,使用此访问器。
弃用过渡兼容机制#
为方便迁移, ProxyArray 提供弃用过渡兼容机制,让旧代码可以暂时将其当作 torch.Tensor 使用:
# These still work but emit a one-time DeprecationWarning:
result = torch.sum(robot.data.joint_pos) # via __torch_function__
value = robot.data.joint_pos[0, 3] # via __getitem__
result = robot.data.joint_pos + 1.0 # via __add__
legacy = wp.to_torch(robot.data.joint_pos) # temporary shim
# Preferred (no warning):
result = torch.sum(robot.data.joint_pos.torch)
value = robot.data.joint_pos.torch[0, 3]
result = robot.data.joint_pos.torch + 1.0
tensor = robot.data.joint_pos.torch
wp.to_torch() 兼容路径是临时适配层,用于在 ProxyArray 提供显式访问器之前已迁移的代码。它返回与 .torch 相同的零拷贝张量,并发出一次 DeprecationWarning 。此适配层和其他弃用兼容机制将在后续版本移除。请现在迁移到显式 .torch 访问。
从 Isaac Lab 2.x 迁移#
Isaac Lab 2.x 的数据属性直接返回 torch.Tensor ;3.0 则返回 ProxyArray 。追加 .torch 即可获取张量:
# BEFORE (Isaac Lab 2.x) — properties returned torch.Tensor directly
joint_pos = robot.data.joint_pos
first_contact = sensor.compute_first_contact(dt)
# AFTER (Isaac Lab 3.0) — properties return ProxyArray
joint_pos = robot.data.joint_pos.torch
first_contact = sensor.compute_first_contact(dt).torch
备注
临时兼容层允许向 wp.to_torch() 传入 ProxyArray ,返回与 .torch 相同的缓存式零拷贝张量。该路径会发出一次 DeprecationWarning ,并将在后续版本移除。新代码应使用 .torch 。
后端差异#
各后端的 ProxyArray 接口相同,但底层数据刷新方式不同:
- PhysX(访问时拉取刷新):
每个仿真步中首次访问属性时,会从 PhysX 张量 API 拉取最新数据并缓存结果。底层 GPU 缓冲区稳定且已预分配,因此
ProxyArray封装只创建一次,可安全地跨步复用。- Newton(自动刷新并替换封装):
仿真每步自动刷新 GPU 缓冲区。完整仿真重置时,可能重新创建缓冲区。Newton 后端会为新的 Warp 数组创建新的
ProxyArray封装,使此前缓存的 Torch 张量失效。
两种情况下, .torch 始终返回当前步中当前仿真状态的视图。
警告
不要跨仿真重置缓存 .torch 的结果。重置后(尤其是 Newton),此前获取的张量可能指向过时或已释放的 GPU 内存。重置后务必重新访问属性。
API 参考#
- class isaaclab.utils.warp.ProxyArray[源代码]
Warp-first array wrapper providing cached zero-copy
.torchand.warpaccessors.This class wraps a
warp.arrayand provides:A
.warpproperty that returns the original warp array (for kernel interop).A
.torchproperty that returns a cached, zero-copytorch.Tensorview (viawarp.to_torch()).Convenience properties (
shape,dtype,device) delegated to the warp array.A deprecation bridge for common torch functions, indexing, and arithmetic/comparison operators while emitting a one-time
DeprecationWarning. Tensor instance methods such asclone()are not forwarded; use explicit.torchaccess for those.
Example:
import warp as wp from isaaclab.utils.warp.proxy_array import ProxyArray arr = wp.zeros(100, dtype=wp.vec3f, device="cuda:0") ta = ProxyArray(arr) # Explicit access (preferred) ta.warp # -> wp.array, shape (100,), dtype vec3f ta.torch # -> torch.Tensor, shape (100, 3) # Deprecation bridge (warns once, then silent) result = ta + 1.0 # works, emits DeprecationWarning
Methods:
__init__(wp_array)Initialize the ProxyArray wrapper.
Attributes:
The underlying warp array.
A cached, zero-copy
torch.Tensorview of the warp array.Shape of the underlying warp array.
Warp dtype of the underlying array.
Device string of the underlying warp array.
- __init__(wp_array: warp.array) None[源代码]
Initialize the ProxyArray wrapper.
The instance is immutable after construction: the wrapped
wp.arraycannot be reassigned. If the underlying simulation memory is re-allocated, construct a newProxyArrayinstead of mutating an existing one.- 参数:
wp_array¶ -- The warp array to wrap.
- 抛出:
TypeError -- If
wp_arrayis not awarp.array.
- property warp: warp.array[源代码]
The underlying warp array.
- property torch: torch.Tensor[源代码]
A cached, zero-copy
torch.Tensorview of the warp array.The tensor is created on first access and cached for subsequent calls. Since this is a zero-copy view, modifications to the tensor are visible through the warp array and vice versa.
When the underlying warp array has dtype
wp.quatfand theWARN_ON_TORCH_QUATF_ACCESSenvironment variable is set to"1", each read emits aUserWarningpointing at the call site. This is a runtime aid for migrating Isaac Lab 2.x code (which used the(w, x, y, z)quaternion convention) to Isaac Lab 3.x's(x, y, z, w)convention.
- property dtype[源代码]
Warp dtype of the underlying array.