Skip to content

Commit 655cc9f

Browse files
Libero0809YilingQiao
authored andcommitted
[BUG FIX] Fix taichi debug mode errors and warnings (Genesis-Embodied-AI#1560)
1 parent 3d16cc0 commit 655cc9f

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

genesis/engine/bvh.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,16 @@ def compute_morton_codes(self):
235235
self.morton_codes[i_b, i_a] = ti.Vector([morton_code, i_a], dt=ti.u32)
236236

237237
@ti.func
238-
def expand_bits(self, v):
238+
def expand_bits(self, v: ti.u32) -> ti.u32:
239239
"""
240240
Expands a 10-bit integer into 30 bits by inserting 2 zeros before each bit.
241241
"""
242242
v = (v * ti.u32(0x00010001)) & ti.u32(0xFF0000FF)
243-
v = (v * ti.u32(0x00000101)) & ti.u32(0x0F00F00F)
243+
# This is to silence taichi debug warning of overflow
244+
# Has the same result as v = (v * ti.u32(0x00000101)) & ti.u32(0x0F00F00F)
245+
# Performance difference is negligible
246+
# See https://github.com/Genesis-Embodied-AI/Genesis/pull/1560 for details
247+
v = (v | ((v & 0x00FFFFFF) << 8)) & 0x0F00F00F
244248
v = (v * ti.u32(0x00000011)) & ti.u32(0xC30C30C3)
245249
v = (v * ti.u32(0x00000005)) & ti.u32(0x49249249)
246250
return v
@@ -351,21 +355,21 @@ def build_radix_tree(self):
351355

352356
delta_min = self.delta(i, i - d, i_b)
353357
l_max = ti.u32(2)
354-
while self.delta(i, i + l_max * d, i_b) > delta_min:
358+
while self.delta(i, i + ti.i32(l_max) * d, i_b) > delta_min:
355359
l_max *= 2
356360
l = ti.u32(0)
357361

358362
t = l_max // 2
359363
while t > 0:
360-
if self.delta(i, i + (l + t) * d, i_b) > delta_min:
364+
if self.delta(i, i + ti.i32(l + t) * d, i_b) > delta_min:
361365
l += t
362366
t //= 2
363-
j = i + l * d
367+
j = i + ti.i32(l) * d
364368
delta_node = self.delta(i, j, i_b)
365369
s = ti.u32(0)
366370
t = (l + 1) // 2
367371
while t > 0:
368-
if self.delta(i, i + (s + t) * d, i_b) > delta_node:
372+
if self.delta(i, i + ti.i32(s + t) * d, i_b) > delta_node:
369373
s += t
370374
t = ti.select(t > 1, (t + 1) // 2, 0)
371375

@@ -378,17 +382,17 @@ def build_radix_tree(self):
378382
self.nodes[i_b, ti.i32(right)].parent = i
379383

380384
@ti.func
381-
def delta(self, i, j, i_b):
385+
def delta(self, i: ti.i32, j: ti.i32, i_b: ti.i32):
382386
"""
383387
Compute the longest common prefix (LCP) of the morton codes of two AABBs.
384388
"""
385389
result = -1
386390
if j >= 0 and j < self.n_aabbs:
387391
result = 64
388392
for i_bit in range(2):
389-
x = self.morton_codes[i_b, ti.i32(i)][i_bit] ^ self.morton_codes[i_b, ti.i32(j)][i_bit]
393+
x = self.morton_codes[i_b, i][i_bit] ^ self.morton_codes[i_b, j][i_bit]
390394
for b in range(32):
391-
if x & (1 << (31 - b)):
395+
if x & (ti.u32(1) << (31 - b)):
392396
result = b + 32 * i_bit
393397
break
394398
if result != 64:

genesis/engine/couplers/sap_coupler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ def compute_inertia_elastic_gradient_alpha(self, i_step: ti.i32):
655655
for i_b, i_v in ti.ndrange(self._B, self.fem_solver.n_vertices):
656656
if not self.batch_linesearch_active[i_b]:
657657
continue
658-
self.linesearch_state.dell_dalpha[i_b] += dp[i_b, i_v].dot(v[i_b, i_v] - v_star[i_step + 1, i_b, i_v])
658+
self.linesearch_state.dell_dalpha[i_b] += dp[i_b, i_v].dot(v[i_b, i_v] - v_star[i_step + 1, i_v, i_b])
659659

660660
@ti.kernel
661661
def compute_inertia_elastic_hessian_alpha(self):
@@ -679,7 +679,7 @@ def compute_inertia_elastic_energy_alpha(self, i_step: ti.i32, energy: ti.templa
679679
for i_b, i_v in ti.ndrange(self._B, self.fem_solver.n_vertices):
680680
if not self.batch_linesearch_active[i_b]:
681681
continue
682-
energy[i_b] += alpha[i_b] * dp[i_b, i_v].dot(v[i_b, i_v] - v_star[i_step + 1, i_b, i_v])
682+
energy[i_b] += alpha[i_b] * dp[i_b, i_v].dot(v[i_b, i_v] - v_star[i_step + 1, i_v, i_b])
683683

684684
def prepare_search_direction_data(self):
685685
self.prepare_inertia_elastic_search_direction_data()

0 commit comments

Comments
 (0)