Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Fixed
^^^^^

* Fixed :func:`isaaclab.sim.build_simulation_context` silently ignoring the
``device`` kwarg when ``sim_cfg`` is also provided. Most test callers pass
both kwargs together; the helper now applies the explicit ``device`` over
``sim_cfg.device`` so the caller's choice wins. Without this, warp kernel
launches in :mod:`isaaclab_newton.assets.articulation` raised device
mismatch errors on non-default GPUs (``env_ids`` allocated on the test's
device while the articulation's resolved device came from the untouched
``sim_cfg`` default ``cuda:0``).
20 changes: 17 additions & 3 deletions source/isaaclab/isaaclab/sim/simulation_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ def _predicate(prim: Usd.Prim) -> bool:
def build_simulation_context(
create_new_stage: bool = True,
gravity_enabled: bool = True,
device: str = "cuda:0",
device: str | None = None,
dt: float = 0.01,
sim_cfg: SimulationCfg | None = None,
add_ground_plane: bool = False,
Expand All @@ -971,7 +971,11 @@ def build_simulation_context(
Args:
create_new_stage: Whether to create a new stage. Defaults to True.
gravity_enabled: Whether to enable gravity. Defaults to True.
device: Device to run the simulation on. Defaults to "cuda:0".
device: Device to run the simulation on. When given alongside ``sim_cfg``,
overrides ``sim_cfg.device`` so the caller's explicit choice wins
(most test callers pass both, expecting this behavior). Defaults to
``None``, meaning ``sim_cfg.device`` is left untouched and a freshly
built ``sim_cfg`` uses :class:`SimulationCfg`'s default device.
dt: Time step for the simulation. Defaults to 0.01.
sim_cfg: SimulationCfg to use. Defaults to None.
add_ground_plane: Whether to add a ground plane. Defaults to False.
Expand All @@ -993,7 +997,17 @@ def build_simulation_context(

if sim_cfg is None:
gravity = (0.0, 0.0, -9.81) if gravity_enabled else (0.0, 0.0, 0.0)
sim_cfg = SimulationCfg(device=device, dt=dt, gravity=gravity)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

maybe just SimulationCfg(dt=dt, gravity=gravity, device=device)?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

actually do

if device:
     sim_cfg.device=device

replace does an extra copy iirc

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

addressed

sim_cfg = SimulationCfg(dt=dt, gravity=gravity)
if device is not None:
# Honor the explicit device kwarg in both branches: when sim_cfg is
# freshly built, this picks the device; when sim_cfg is passed in,
# this overrides its (possibly default) device. Without the override,
# callers passing both ``sim_cfg=<built-with-default-device>`` and
# ``device=cuda:N`` silently got sim_cfg's device, causing warp
# kernel-launch mismatches when test fixtures allocated tensors on
# the requested device while assets resolved their device from the
# untouched sim_cfg.
sim_cfg.device = device

sim = SimulationContext(sim_cfg)

Expand Down
19 changes: 16 additions & 3 deletions source/isaaclab/test/sim/test_build_simulation_context_headless.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,14 @@ def test_build_simulation_context_auto_add_lighting(add_lighting, auto_add_light

@pytest.mark.isaacsim_ci
def test_build_simulation_context_cfg():
"""Test that the simulation context is built with the correct cfg and values don't get overridden."""
"""Test that the simulation context honors sim_cfg's values, with an explicit
device override winning when both ``sim_cfg`` and ``device`` are passed.

Most test callers pass both kwargs together expecting the device kwarg to
win; the override branch in :func:`build_simulation_context` exists for
that case. ``gravity`` and ``dt`` are not overridable by the helper's
kwargs (only sim_cfg's values are used).
"""
dt = 0.001
# Non-standard gravity
gravity = (0.0, 0.0, -1.81)
Expand All @@ -87,8 +94,14 @@ def test_build_simulation_context_cfg():
dt=dt,
)

with build_simulation_context(sim_cfg=cfg, gravity_enabled=False, dt=0.01, device="cpu") as sim:
# Values from sim_cfg should not be overridden by build_simulation_context args
# Pass only sim_cfg: gravity, device, dt all come from sim_cfg (kwargs ignored).
with build_simulation_context(sim_cfg=cfg, gravity_enabled=False, dt=0.01) as sim:
assert sim.cfg.gravity == gravity
assert sim.cfg.device == device
assert sim.cfg.dt == dt

# Pass sim_cfg and an explicit device override: device kwarg wins.
with build_simulation_context(sim_cfg=cfg, device="cpu") as sim:
assert sim.cfg.gravity == gravity
assert sim.cfg.device == "cpu"
assert sim.cfg.dt == dt
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,14 @@ def test_build_simulation_context_auto_add_lighting(add_lighting, auto_add_light


def test_build_simulation_context_cfg():
"""Test that the simulation context is built with the correct cfg and values don't get overridden."""

"""Test that the simulation context honors sim_cfg's values, with an explicit
device override winning when both ``sim_cfg`` and ``device`` are passed.

Most test callers pass both kwargs together expecting the device kwarg to
win; the override branch in :func:`build_simulation_context` exists for
that case. ``gravity`` and ``dt`` are not overridable by the helper's
kwargs (only sim_cfg's values are used).
"""
dt = 0.001
# Non-standard gravity
gravity = (0.0, 0.0, -1.81)
Expand All @@ -83,8 +89,14 @@ def test_build_simulation_context_cfg():
dt=dt,
)

with build_simulation_context(sim_cfg=cfg, gravity_enabled=False, dt=0.01, device="cpu") as sim:
# Values from sim_cfg should not be overridden by build_simulation_context args
# Pass only sim_cfg: gravity, device, dt all come from sim_cfg (kwargs ignored).
with build_simulation_context(sim_cfg=cfg, gravity_enabled=False, dt=0.01) as sim:
assert sim.cfg.gravity == gravity
assert sim.cfg.device == device
assert sim.cfg.dt == dt

# Pass sim_cfg and an explicit device override: device kwarg wins.
with build_simulation_context(sim_cfg=cfg, device="cpu") as sim:
assert sim.cfg.gravity == gravity
assert sim.cfg.device == "cpu"
assert sim.cfg.dt == dt
Loading