Skip to content

Conversation

@zhangbo9674
Copy link
Contributor

@zhangbo9674 zhangbo9674 commented Oct 27, 2025

PR Category

Operator Mechanism

PR Types

Bug fixes

Description

to_tensor存在如下两个bug:

import paddle
import numpy as np
a = [paddle.to_tensor(2,dtype=paddle.bfloat16)]
print(a)
b = paddle.to_tensor(a)
print(b)

a = [paddle.to_tensor(2,dtype=paddle.float16)]
print(a)
b = paddle.to_tensor(a)
print(b)
[Tensor(shape=[], dtype=bfloat16, place=Place(gpu:0), stop_gradient=True,
       2.)]
Tensor(shape=[1], dtype=bfloat16, place=Place(gpu:0), stop_gradient=True,
       [0.00000000]) #数值错误
[Tensor(shape=[], dtype=float16, place=Place(gpu:0), stop_gradient=True,
       2.)]
Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,  #dtype错误
       [2.])
  • bfloat16问题分析:
    首先,原则上bfloat16的Tensor是不应该转成numpy的,因为numpy没有bfloat16这个类型,因此早期开发者在转换成numpy时使用numpy.uint16替代。
    bfloat16的bug的原因是当to_tensor的data是list of Tensor,dtype=bfloat16,数据值为2.0时,data = np.array(data)会将现将Tensor转成numpy.uint16类型的numpy对象数值为2。a.numpy()则会将Tensor转换成numpy.uint16类型的numpy对象数值为16384。我们期望的是后者。为什么呢?因为16384存储的正是bfloat16 Tensor真正的“内存布局”,而2是Tensor的值而非”内存布局”。使用值为2的uint16 numpy构造core.eager.Tensor时,会得到Tensor的值为0.00000,而使用值为16384的uint16 numpy构造core.eager.Tensor时,会得到Tensor的值为2.0。
    还有另一种组合:
a = [paddle.to_tensor(2,dtype=paddle.float16), paddle.to_tensor(2,dtype=paddle.float32)]
print(a)
b = paddle.to_tensor(a)
print(b)

在以上case中,我们需要先把list of Tensor转换为float32的numpy,再构造core.eager.Tensor,此时,numpy的值需要存2.0而非16384。
因此,bfloat16的bug本质是Paddle的bfloat16与numpy转换处理不恰当导致的。

  • float16问题分析:
    float16的问题在于,由于_to_tensor_non_static传入的dtype为None,在代码中有一段逻辑会将None分配一个默认值float32,导致了后面构造了float32的Tensor。解决办法是:
            if not dtype:
                dtype = data.dtype

devPR:#76000

@paddle-bot
Copy link

paddle-bot bot commented Oct 27, 2025

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@swgu98 swgu98 merged commit 7320f14 into PaddlePaddle:fleety_12 Oct 30, 2025
48 of 53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants