Skip to content

Commit 1ccd623

Browse files
authored
[MISC] Fix differentiable unit tests not running on the CI. (#1963)
* Fix differentiable unit tests not running on the CI. * Fix AD support.
1 parent 6bc3e68 commit 1ccd623

File tree

4 files changed

+24
-21
lines changed

4 files changed

+24
-21
lines changed

.github/workflows/production.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ jobs:
3939
matrix:
4040
GS_ENABLE_NDARRAY: ["0", "1"]
4141

42+
env:
43+
GS_ENABLE_NDARRAY: ${{ matrix.GS_ENABLE_NDARRAY }}
44+
4245
steps:
4346
- name: Checkout code
4447
uses: actions/checkout@v4
@@ -75,7 +78,7 @@ jobs:
7578
pip install --no-input --extra-index-url https://pypi.nvidia.com/ omniverse-kit
7679
pip install --no-input ".[dev,render,usd]"
7780
78-
pytest -v --backend gpu --dev --forked ./tests
81+
pytest -v -rs --backend gpu --dev --forked ./tests
7982
EOF
8083
- name: Kill srun job systematically
8184
if: always()

genesis/engine/entities/emitter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def emit_omni(self, source_radius=0.1, pos=(0.5, 0.5, 1.0), speed=1.0, particle_
230230

231231
n_particles = len(positions)
232232
if n_particles > self._entity.n_particles:
233-
gs.logger.warning(
233+
gs.raise_exception(
234234
f"Number of particles to emit ({n_particles}) at the current step is larger than the maximum number "
235235
f"of particles ({self._entity.n_particles})."
236236
)

genesis/engine/solvers/mpm_solver.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,9 @@ def g2p(
398398
geoms_info: array_class.GeomsInfo,
399399
links_state: array_class.LinksState,
400400
rigid_global_info: array_class.RigidGlobalInfo,
401-
) -> ti.i32:
402-
is_success = True
401+
):
403402
for i_p, i_b in ti.ndrange(self._n_particles, self._B):
404-
if is_success and self.particles_ng[f, i_p, i_b].active:
403+
if self.particles_ng[f, i_p, i_b].active:
405404
base = ti.floor(self.particles[f, i_p, i_b].pos * self._inv_dx - 0.5).cast(gs.ti_int)
406405
fx = self.particles[f, i_p, i_b].pos * self._inv_dx - base.cast(gs.ti_float)
407406
w = [0.5 * (1.5 - fx) ** 2, 0.75 - (fx - 1.0) ** 2, 0.5 * (fx - 0.5) ** 2]
@@ -433,19 +432,16 @@ def g2p(
433432
new_vel += weight * grid_vel
434433
new_C += 4 * self._inv_dx * weight * grid_vel.outer_product(dpos)
435434

436-
if not ti.math.isnan(new_vel).any():
437-
# compute actual new_pos with new_vel
438-
new_pos = self.particles[f, i_p, i_b].pos + self.substep_dt * new_vel
435+
# compute actual new_pos with new_vel
436+
new_pos = self.particles[f, i_p, i_b].pos + self.substep_dt * new_vel
439437

440-
# impose boundary for safety, in case simulation explodes and tries to access illegal cell address
441-
new_pos, new_vel = self.boundary.impose_pos_vel(new_pos, new_vel)
438+
# impose boundary for safety, in case simulation explodes and tries to access illegal cell address
439+
new_pos, new_vel = self.boundary.impose_pos_vel(new_pos, new_vel)
442440

443-
# advect to next frame
444-
self.particles[f + 1, i_p, i_b].vel = new_vel
445-
self.particles[f + 1, i_p, i_b].C = new_C
446-
self.particles[f + 1, i_p, i_b].pos = new_pos
447-
else:
448-
is_success = False
441+
# advect to next frame
442+
self.particles[f + 1, i_p, i_b].vel = new_vel
443+
self.particles[f + 1, i_p, i_b].C = new_C
444+
self.particles[f + 1, i_p, i_b].pos = new_pos
449445
else:
450446
self.particles[f + 1, i_p, i_b].vel = self.particles[f, i_p, i_b].vel
451447
self.particles[f + 1, i_p, i_b].pos = self.particles[f, i_p, i_b].pos
@@ -455,6 +451,12 @@ def g2p(
455451

456452
self.particles_ng[f + 1, i_p, i_b].active = self.particles_ng[f, i_p, i_b].active
457453

454+
@ti.kernel
455+
def _is_state_valid(self, f: ti.i32) -> ti.i32:
456+
is_success = True
457+
for i_p, i_b in ti.ndrange(self._n_particles, self._B):
458+
if ti.math.isnan(self.particles[f, i_p, i_b].pos).any():
459+
is_success = False
458460
return is_success
459461

460462
# ------------------------------------------------------------------------------------
@@ -497,13 +499,13 @@ def substep_pre_coupling_grad(self, f):
497499
self.compute_F_tmp.grad(f)
498500

499501
def substep_post_coupling(self, f):
500-
is_success = self.g2p(
502+
self.g2p(
501503
f,
502504
self.sim.coupler.rigid_solver.geoms_info,
503505
self.sim.coupler.rigid_solver.links_state,
504506
self.sim.coupler.rigid_solver._rigid_global_info,
505507
)
506-
if not is_success:
508+
if not self._is_state_valid(f):
507509
gs.raise_exception(
508510
"NaN detected in MPM states. Try reducing the time step size or adjusting simulation parameters."
509511
)

tests/test_grad.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,11 +326,9 @@ def compute_loss(input_mass, input_jac, input_aref, input_efc_D, input_force):
326326
assert_allclose(dL_error, 0.0, atol=RTOL)
327327

328328

329+
@pytest.mark.required
329330
@pytest.mark.parametrize("backend", [gs.cpu, gs.gpu])
330331
def test_differentiable_push(precision, show_viewer):
331-
# FIXME: Wait for fix to be merged in GsTaichi: https://github.com/Genesis-Embodied-AI/gstaichi/pull/225
332-
if sys.platform == "darwin" and gs.backend != gs.cpu:
333-
pytest.skip(reason="GsTaichi does not support AutoDiff on non-CPU backend on Mac OS for now.")
334332
if sys.platform == "linux" and gs.backend == gs.cpu and precision == "64":
335333
pytest.skip(reason="GsTaichi segfault when using AutoDiff on CPU backend on Linux for now.")
336334

0 commit comments

Comments
 (0)