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

Please consider adding literals support #4230

Closed
igor-elovikov opened this issue Feb 8, 2022 · 4 comments
Closed

Please consider adding literals support #4230

igor-elovikov opened this issue Feb 8, 2022 · 4 comments
Labels
feature request Suggest an idea on this project welcome contribution

Comments

@igor-elovikov
Copy link

Concisely describe the proposed feature
Let's have a look on an implementation of popular pcg hash in taichi. Roughly it looks like this:

uint pcg_hash(uint input)
{
    uint state = input * 747796405u + 2891336453u;
    uint word = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u;
    return (word >> 22u) ^ word;
}

In taichi I should do something like this:

@ti.func
def pcg_hash(inp: ti.u32) -> ti.u32:
    n1: ti.u32 = 747796405
    n2: ti.u32 = 2891336453
    n3: ti.u32 = 277803737

    b28: ti.u32 = 28
    b22: ti.u32 = 22
    b4: ti.u32 = 4

    state: ti.u32 = inp * n1 + n2
    word: ti.u32 = ((state >> ((state >> b28) + b4)) ^ state) * n3

    return (word >> b22) ^ word

There are other issues rather than verbosity (mentioned separately here: #4210, apparently I can't even declare typed const expressions), but here I'd like to point to using literals for convenience.

Describe the solution you'd like (if any)

Not sure what is the best way to do that.
I can think of at least two methods.

  1. Making dtype callable at least in taichi context.
@ti.func
def pcg_hash(inp: ti.u32) -> ti.u32:
    state: ti.u32 = inp * ti.u32(747796405) + ti.u32(2891336453)
    word: ti.u32 = ((state >> ((state >> ti.u32(28)) + ti.u32(4))) ^ state) * ti.u32(277803737)

    return (word >> ti.u32(22)) ^ word

Much better and with some additional analysis on arguments can be used as an alternative to ti.cast

  1. Fancy indexing or exploiting ast.Call
@ti.func
def pcg_hash(inp: ti.u32) -> ti.u32:
    state: ti.u32 = inp * 747796405[ti.u32] + 2891336453[ti.u32]
    word: ti.u32 = ((state >> ((state >> 28[ti.u32]) + 4[ti.u32])) ^ state) * 277803737[ti.u32]

    return (word >> 22[ti.u32]) ^ word

Considering the fact that 25[ti.f32] {1.0, 2.0}[vec3f] (1.0, 2.0)[vec3i] 25(ti.f32) {1.0, 2.0}(vec3f) (1.0, 2.0)(vec3i) are valid python syntax (making set or tuple callable doesn't make sense in the interpreter but it's still valid)
Here we can use some sort of trailing type declaration. A bit isoteric, but who knows

Additional comments
It's probably a very specific issue, but I would be nice to have at least an optional support to be more explicit in the language.

Right now I've tested hash function and it works as expected, but I'd still like explicitly set unsigned here as at some backends signed bit shifting can be undefined.

@igor-elovikov igor-elovikov added the feature request Suggest an idea on this project label Feb 8, 2022
@k-ye
Copy link
Member

k-ye commented Feb 9, 2022

Thanks for proposing this! Personally I find 1 to be the (much) more intuitive approach

@ailzhang
Copy link
Contributor

ailzhang commented Feb 9, 2022

+1 on the first approach. :D

@igor-elovikov
Copy link
Author

Agreed, it's also close to how pycuda works when passing parameters to the kernel.

@strongoier
Copy link
Contributor

Closed via #4440.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Suggest an idea on this project welcome contribution
Projects
None yet
Development

No branches or pull requests

4 participants