Skip to content

[BUG FIX] Fix taichi debug mode errors and warnings#1560

Merged
duburcqa merged 2 commits intoGenesis-Embodied-AI:mainfrom
Libero0809:sap_debug_fix
Aug 13, 2025
Merged

[BUG FIX] Fix taichi debug mode errors and warnings#1560
duburcqa merged 2 commits intoGenesis-Embodied-AI:mainfrom
Libero0809:sap_debug_fix

Conversation

@Libero0809
Copy link
Contributor

Description

  • Add ti.i32 and ti.u32 explicit typing to avoid overflow.
  • Change expand_bits function to avoid taichi compiler warning in debug modes.
  • Fix bugs in indexing.

Related Issue

Resolves Genesis-Embodied-AI/Genesis#

Motivation and Context

Now the code can be run in taichi debug mode without outputting tons of warning messages.

How Has This Been / Can This Be Tested?

Tested with code

import taichi as ti
import time

@ti.func
def expand1(x: ti.u32) -> ti.u32:
    x = (x | (x << 16)) & ti.u32(0xFF0000FF)
    x = (x | (x <<  8)) & ti.u32(0x0F00F00F)
    x = (x | (x <<  4)) & ti.u32(0xC30C30C3)
    x = (x | (x <<  2)) & ti.u32(0x49249249)
    return x

@ti.func
def expand2(x: ti.u32) -> ti.u32:
    x = (x * ti.u32(0x00010001)) & ti.u32(0xFF0000FF)
    x = (x * ti.u32(0x00000101)) & ti.u32(0x0F00F00F)
    x = (x * ti.u32(0x00000011)) & ti.u32(0xC30C30C3)
    x = (x * ti.u32(0x00000005)) & ti.u32(0x49249249)
    return x

@ti.func
def expand3(x: ti.u32) -> ti.u32:
    x = (x * ti.u32(0x00010001)) & ti.u32(0xFF0000FF)
    x = (x | ((x & 0x00FFFFFF) <<  8)) & 0x0F00F00F
    x = (x * ti.u32(0x00000011)) & ti.u32(0xC30C30C3)
    x = (x * ti.u32(0x00000005)) & ti.u32(0x49249249)
    return x

@ti.kernel
def test1(n_test: ti.i32):
    for i, j in ti.ndrange(n_test, 1024):
        data1[i, j] = expand1(j)

@ti.kernel
def test2(n_test: ti.i32):
    for i, j in ti.ndrange(n_test, 1024):
        data2[i, j] = expand2(j)

@ti.kernel
def test3(n_test: ti.i32):
    for i, j in ti.ndrange(n_test, 1024):
        data3[i, j] = expand3(j)

if __name__ == "__main__":
    ti.init(arch=ti.gpu, debug=False)
    n_test = 100000
    n_rounds = 10
    data1 = ti.field(ti.u32, shape=(n_test, 1024))
    data2 = ti.field(ti.u32, shape=(n_test, 1024))
    data3 = ti.field(ti.u32, shape=(n_test, 1024))
    for _ in range(n_rounds):
        data1.fill(0)
        start = time.time()
        test1(n_test)
        print("expand1 time:", time.time() - start)
        data2.fill(0)
        start = time.time()
        test2(n_test)
        print("expand2 time:", time.time() - start)
        data3.fill(0)
        start = time.time()
        test3(n_test)
        print("expand3 time:", time.time() - start)
        assert (data1.to_numpy() == data2.to_numpy()).all()
        assert (data1.to_numpy() == data3.to_numpy()).all()

Negligible difference:
expand1 time: 0.01114034652709961
expand2 time: 0.01214289665222168
expand3 time: 0.012060403823852539
expand1 time: 3.123283386230469e-05
expand2 time: 1.7642974853515625e-05
expand3 time: 1.6927719116210938e-05
expand1 time: 1.3589859008789062e-05
expand2 time: 1.2636184692382812e-05
expand3 time: 1.3589859008789062e-05
expand1 time: 1.239776611328125e-05
expand2 time: 1.3113021850585938e-05
expand3 time: 1.1920928955078125e-05
expand1 time: 1.2874603271484375e-05
expand2 time: 6.103515625e-05
expand3 time: 1.3589859008789062e-05
expand1 time: 1.239776611328125e-05
expand2 time: 1.2159347534179688e-05
expand3 time: 1.1920928955078125e-05
expand1 time: 1.2159347534179688e-05
expand2 time: 1.2159347534179688e-05
expand3 time: 1.1920928955078125e-05
expand1 time: 1.1920928955078125e-05
expand2 time: 1.1682510375976562e-05
expand3 time: 1.1920928955078125e-05
expand1 time: 1.6450881958007812e-05
expand2 time: 1.239776611328125e-05
expand3 time: 1.3589859008789062e-05
expand1 time: 1.33514404296875e-05
expand2 time: 1.430511474609375e-05
expand3 time: 1.2636184692382812e-05

Screenshots (if appropriate):

Checklist:

  • I read the CONTRIBUTING document.
  • I followed the Submitting Code Changes section of CONTRIBUTING document.
  • I tagged the title correctly (including BUG FIX/FEATURE/MISC/BREAKING)
  • I updated the documentation accordingly or no change is needed.
  • I tested my changes and added instructions on how to test it for reviewers.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants