Skip to content

Commit a69a47c

Browse files
YilingQiaoduburcqa
andauthored
[BUG FIX] Fix data races in getting contact and equality constraints (Genesis-Embodied-AI#1676)
Co-authored-by: Alexis Duburcq <alexis.duburcq@gmail.com>
1 parent 9258f22 commit a69a47c

File tree

4 files changed

+32
-38
lines changed

4 files changed

+32
-38
lines changed

genesis/engine/solvers/rigid/collider_decomp.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,9 @@ def collider_kernel_get_contacts(
596596
_B = collider_state.active_buffer.shape[1]
597597
n_contacts_max = gs.ti_int(0)
598598

599-
ti.loop_config(serialize=static_rigid_sim_config.para_level < gs.PARA_LEVEL.ALL)
599+
# this is a reduction operation (global max), we have to serialize it
600+
# TODO: a good unittest and a better implementation from gstaichi for this kind of reduction
601+
ti.loop_config(serialize=True)
600602
for i_b in range(_B):
601603
n_contacts = collider_state.n_contacts[i_b]
602604
if n_contacts > n_contacts_max:

genesis/engine/solvers/rigid/constraint_solver_decomp.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2221,7 +2221,9 @@ def kernel_get_equality_constraints(
22212221
_B = constraint_state.ti_n_equalities.shape[0]
22222222
n_eqs_max = gs.ti_int(0)
22232223

2224-
ti.loop_config(serialize=static_rigid_sim_config.para_level < gs.PARA_LEVEL.ALL)
2224+
# this is a reduction operation (global max), we have to serialize it
2225+
# TODO: a good unittest and a better implementation from gstaichi for this kind of reduction
2226+
ti.loop_config(serialize=True)
22252227
for i_b in range(_B):
22262228
n_eqs = constraint_state.ti_n_equalities[i_b]
22272229
if n_eqs > n_eqs_max:

tests/test_rigid_physics.py

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2913,53 +2913,43 @@ def test_mesh_primitive_COM(show_viewer, tol):
29132913

29142914

29152915
@pytest.mark.required
2916-
@pytest.mark.parametrize("backend", [gs.cpu])
2917-
def test_batched_aabb(show_viewer, tol):
2918-
scene = gs.Scene(
2919-
sim_options=gs.options.SimOptions(gravity=(0.0, 0.0, -10.0)),
2920-
show_viewer=show_viewer,
2921-
)
2922-
2916+
def test_batched_aabb(tol):
2917+
scene = gs.Scene()
29232918
plane = scene.add_entity(
2924-
gs.morphs.Plane(normal=(0, 0, 1), pos=(0, 0, 0)),
2925-
material=gs.materials.Rigid(),
2919+
gs.morphs.Plane(
2920+
normal=(0, 0, 1),
2921+
pos=(0, 0, 0),
2922+
),
29262923
)
29272924
box = scene.add_entity(
2928-
gs.morphs.Box(size=(0.1, 0.1, 0.1), pos=(0.5, 0, 0.05)),
2929-
material=gs.materials.Rigid(),
2925+
gs.morphs.Box(
2926+
size=(0.1, 0.1, 0.1),
2927+
pos=(0.5, 0, 0.05),
2928+
),
29302929
)
29312930
sphere = scene.add_entity(
2932-
gs.morphs.Sphere(radius=0.05, pos=(-0.5, 0, 0.05)),
2933-
material=gs.materials.Rigid(),
2931+
gs.morphs.Sphere(
2932+
radius=0.05,
2933+
pos=(-0.5, 0, 0.05),
2934+
),
29342935
)
2935-
29362936
scene.build()
29372937

29382938
all_aabbs = scene.sim.rigid_solver.get_aabb()
2939-
assert_allclose(torch.tensor(all_aabbs.shape), torch.tensor([3, 2, 3]), atol=tol)
2940-
29412939
plane_aabb = plane.get_aabb()
29422940
box_aabb = box.get_aabb()
29432941
sphere_aabb = sphere.get_aabb()
29442942

2945-
assert_allclose(torch.tensor(plane_aabb.shape[-1]), torch.tensor(3), atol=tol)
2946-
assert_allclose(torch.tensor(box_aabb.shape[-1]), torch.tensor(3), atol=tol)
2947-
assert_allclose(torch.tensor(sphere_aabb.shape[-1]), torch.tensor(3), atol=tol)
2948-
2949-
plane_aabb_squeezed = plane_aabb.squeeze() if plane_aabb.ndim == 3 else plane_aabb
2950-
box_aabb_squeezed = box_aabb.squeeze() if box_aabb.ndim == 3 else box_aabb
2951-
sphere_aabb_squeezed = sphere_aabb.squeeze() if sphere_aabb.ndim == 3 else sphere_aabb
2952-
2953-
assert_allclose(plane_aabb_squeezed, all_aabbs[0], atol=tol)
2954-
assert_allclose(box_aabb_squeezed, all_aabbs[1], atol=tol)
2955-
assert_allclose(sphere_aabb_squeezed, all_aabbs[2], atol=tol)
2943+
assert_allclose(all_aabbs.shape, (3, 2, 3), atol=0)
2944+
assert_allclose(plane_aabb.shape[-1], 3, atol=0)
2945+
assert_allclose(box_aabb.shape[-1], 3, atol=0)
2946+
assert_allclose(sphere_aabb.shape[-1], 3, atol=0)
2947+
assert_allclose((plane_aabb, box_aabb, sphere_aabb), all_aabbs, atol=tol)
29562948

2957-
expected_box_min = torch.tensor([0.45, -0.05, 0.0])
2958-
expected_box_max = torch.tensor([0.55, 0.05, 0.1])
2959-
assert_allclose(box_aabb_squeezed[0], expected_box_min, atol=tol)
2960-
assert_allclose(box_aabb_squeezed[1], expected_box_max, atol=tol)
2949+
box_aabb_min, box_aabb_max = box_aabb
2950+
assert_allclose(box_aabb_min, (0.45, -0.05, 0.0), atol=tol)
2951+
assert_allclose(box_aabb_max, (0.55, 0.05, 0.1), atol=tol)
29612952

2962-
expected_sphere_min = torch.tensor([-0.55, -0.05, 0.0])
2963-
expected_sphere_max = torch.tensor([-0.45, 0.05, 0.1])
2964-
assert_allclose(sphere_aabb_squeezed[0], expected_sphere_min, atol=tol)
2965-
assert_allclose(sphere_aabb_squeezed[1], expected_sphere_max, atol=tol)
2953+
sphere_aabb_min, sphere_aabb_max = sphere_aabb
2954+
assert_allclose(sphere_aabb_min, (-0.55, -0.05, 0.0), atol=tol)
2955+
assert_allclose(sphere_aabb_max, (-0.45, 0.05, 0.1), atol=tol)

tests/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def assert_allclose(actual, desired, *, atol=None, rtol=None, tol=None, err_msg=
266266
if all(e.size == 0 for e in args):
267267
return
268268

269-
np.testing.assert_allclose(*args, atol=atol, rtol=rtol, err_msg=err_msg)
269+
np.testing.assert_allclose(*map(np.squeeze, args), atol=atol, rtol=rtol, err_msg=err_msg)
270270

271271

272272
def assert_array_equal(actual, desired, *, err_msg=""):

0 commit comments

Comments
 (0)