Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python confusing behavior due to implicit casting #98

Open
111116 opened this issue Nov 8, 2023 · 2 comments
Open

python confusing behavior due to implicit casting #98

111116 opened this issue Nov 8, 2023 · 2 comments
Labels

Comments

@111116
Copy link
Collaborator

111116 commented Nov 8, 2023

import luisa
luisa.init('cuda')

@luisa.func
def f():
    if dispatch_id().x == 999:
        a = 0
    else:
        a = 0.5
    print('a:', a)

f(dispatch_size=1)

Result:

a: 0

While the expected result is 0.5.


Change the condition to False and result will become a: 0.5


Above code crashes when using cpu backend instead:

thread '<unnamed>' panicked at 'index out of bounds: the len is 5 but the index is 5', luisa_compute_backend_impl/src/cpu/codegen/cpp.rs:1799:16
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
terminate called without an active exception
@111116 111116 added the Python label Nov 8, 2023
@Mike-Leo-Smith
Copy link
Contributor

The type of a variable is decided at its first occurrence so

if dispatch_id().x == 999:
        a = 0
    else:
        a = 0.5

leads to an int-typed a (as initialized by int(0)).

On the other hand, we perform some basic control flow simplification, so if you change the condition to a constant False, the above code folds to the unbranched version

    a = 0.5

where a now is initialized by a float(0.5).

You may solve such inconsistence by either changing 0 to 0.0 or explicitly type a as float aforehand:

import luisa
luisa.init('cuda')

@luisa.func
def f():
    a = float()
    if dispatch_id().x == 999:
        a = 0
    else:
        a = 0.5
    print('a:', a)

f(dispatch_size=1)

@Mike-Leo-Smith
Copy link
Contributor

Above code crashes when using cpu backend instead:

thread '<unnamed>' panicked at 'index out of bounds: the len is 5 but the index is 5', luisa_compute_backend_impl/src/cpu/codegen/cpp.rs:1799:16
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
terminate called without an active exception

This should have been fixed in new luisa-python releases. Please upgrade with the following command:

python -m pip install -U luisa-python

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants