Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Category
Operator Mechanism
PR Types
Bug fixes
Description
to_tensor存在如下两个bug:
首先,原则上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。还有另一种组合:
在以上case中,我们需要先把list of Tensor转换为float32的numpy,再构造core.eager.Tensor,此时,numpy的值需要存2.0而非16384。
因此,bfloat16的bug本质是Paddle的bfloat16与numpy转换处理不恰当导致的。
float16的问题在于,由于_to_tensor_non_static传入的dtype为None,在代码中有一段逻辑会将None分配一个默认值float32,导致了后面构造了float32的Tensor。解决办法是:
devPR:#76000