norm()函数在gpu上和cpu上结果不一致

问题
使用norm()函数,在gpu和cpu上得到的结果不一致

代码

import taichi as ti
ti.init(arch=ti.gpu, debug=True, default_fp=ti.f64)

coord = ti.Vector.field(2, ti.f64, 2)
coord[0] = ti.Vector([0.0, 0.0])
coord[1] = ti.Vector([0.0, 0.3])

@ti.kernel
def cal():
    a = coord[0] - coord[1]
    b = a.norm()
    print(b)

cal()

output:

[Taichi] version 1.1.2, llvm 10.0.0, commit f25cf4a2, win, python 3.9.12
[Taichi] Starting on arch=cuda
0.299999952316

在gpu上得到的结果为:0.299999952316,而在cpu上可以得到:0.300000000000

谢谢!

看着有点像 GPU 算 sqrt 的精度问题 :joy: 可以帮忙看一看直接 print(ti.sqrt(0.09)) 结果是什么样的吗?

结果是0.3 :grinning:

1 个赞

Thanks for reporting & help with testing. I confirm this is a bug feature :joy: It will be tracked here: ti.sqrt has lower precision on CUDA than on CPU · Issue #6346 · taichi-dev/taichi · GitHub

谢谢!

你试试 ti.init 的时候设置 fast_math=False?

这回结果一致了,太强啦 :grin: