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

Code optimization leads to unexpected values when using quantized types #7981

Closed
lucifer1004 opened this issue May 11, 2023 · 0 comments
Closed
Assignees

Comments

@lucifer1004
Copy link
Contributor

Describe the bug

Code optimization leads to unexpected values when using quantized types.

To Reproduce

import taichi as ti

ti.init(arch=ti.cpu, print_ir=True, offline_cache=False)

i4 = ti.types.quant.int(4)
f = ti.field(dtype=i4)
v = ti.BitpackedFields(max_num_bits=32)
v.place(f)
ti.root.dense(ti.i, (4,)).place(v)

@ti.kernel
def fn():
    for i in f:
        f[i] = 100 * i
        print(f[i])

fn()

Log/Screenshots

[Taichi] version 1.5.0, llvm 15.0.4, commit 7b885c28, linux, python 3.10.6
[Taichi] Starting on arch=x64
...
kernel {
$0 = offloaded range_for(0, 4) grid_dim=0 block_dim=4 
body {
  <i32> $1 = loop $0 index 0
  <i32> $2 = const 100
  <i32> $3 = mul $1 $2
  <*gen> $4 = get root [S0root][root]
  <i32> $5 = const 0
  <*gen> $6 = [S0root][root]::lookup($4, $5) activate = false
  <*gen> $7 = get child [S0root->S1dense] $6
  <*gen> $8 = [S1dense][dense]::lookup($7, $1) activate = false
  <*bs(0: qi4@0)> $9 = get child [S1dense->S2bit_struct<bs(0: qi4@0)>] $8
  <*gen> $10 = [S2bit_struct<bs(0: qi4@0)>][bit_struct]::lookup($9, $5) activate = false
  $11 : atomic bit_struct_store $10, ch_ids=[0], values=[$3]
  print $3, "\n"
}
}
0
100
200
300

Additional comments

As the log shows, print(f[i]) is optimized to print(100 * i); however, f[i] is a 4-bit integer here, so the actual value of f[i] is wrapped and does not equal to 100 * i.

I think this can lead to some bugs that cannot be easily discovered in practice, and there should be a way to handle this.

@github-project-automation github-project-automation bot moved this to Untriaged in Taichi Lang May 11, 2023
@feisuzhu feisuzhu moved this from Untriaged to Todo in Taichi Lang May 12, 2023
dream189free added a commit that referenced this issue May 24, 2023
… bit struct store fusion when unfeasible (#8023)

Issue: #7981

### Brief Summary

<!--
copilot:summary
-->
### <samp>🤖 Generated by Copilot at cef23dc</samp>

Exclude `qi4` store pointers from forwarding optimization in live
variable analysis. This prevents precision loss when quantizing integers
in the Taichi compiler.

### Walkthrough

<!--
copilot:walkthrough
-->
### <samp>🤖 Generated by Copilot at cef23dc</samp>

* Exclude quantized integer store pointers from forwarding optimization
to avoid precision loss
([link](https://github.com/taichi-dev/taichi/pull/8023/files?diff=unified&w=0#diff-837b90142d1730f6a3ab20c91f1f35c95335ef82a021c74fd4dbdb05ff0e164fL128-R139))
@github-project-automation github-project-automation bot moved this from Todo to Done in Taichi Lang May 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

No branches or pull requests

2 participants