PaddlePaddle / PaddleMIX

Paddle Multimodal Integration and eXploration, supporting mainstream multi-modal tasks, including end-to-end large-scale multi-modal pretrain models and diffusion model toolbox. Equipped with high performance and flexibility.
Apache License 2.0
354 stars 135 forks source link

Use tensor in `window_reverse` to avoid precision issue #617

Closed SigureMo closed 3 months ago

SigureMo commented 3 months ago

window_reverse 里将 window_size 转换为 Tensor,windows.shape[0]HW 在动转静下,因为是动态 shape,所以都是 Tensor,而 window_size 是 scalar,会触发 scale OP,x / n 会转为 scale(x, 1/n),这会导致精度问题,进而导致计算结果出错

一个简单的复现样例:

x = paddle.to_tensor(25)
y = paddle.to_tensor(35)
out = int(x / (y * y / 7 / 7))
# 0,但应该是 1

暂时避免触发 scale OP,将 window_size 转为 Tensor 以触发 elementwise_div,未来可以考虑加一个 scale_div OP,让 x / n 转为 scale_div(x, n) 以避免精度问题(等之后 @gouzil 有空试试?)

该问题一直存在,只是之前的类型提升是向左 cast 隐藏了这点而已,3.0-beta 改了类型提升机制暴露了这点

另外还修改了下 predict 脚本里因为类型提升机制修改挂掉的问题,加了手动 cast

paddle-bot[bot] commented 3 months ago

Thanks for your contribution!