Skip to content

Conversation

DongBaiYue
Copy link
Contributor

@DongBaiYue DongBaiYue commented Jul 15, 2025

PR Category

Operator Mechanism

PR Types

Bug fixes

Description

Problem Summary

When converting fp32 tensors to fp16, current implementation lacks IEEE 754-compliant rounding logic, leading to Precision Error.

Example

import numpy as np
import paddle
import torch

input_data = 1e-05
numpy_tensor = np.array(input_data).astype("float16")
torch_x = torch.tensor(numpy_tensor)
paddle_x = paddle.to_tensor(numpy_tensor)
print("numpy == 1e-5:", numpy_tensor == 1e-5 )              # True
print("torch == 1e-5:", (torch_x == 1e-5).cpu().numpy() )   # True
print("paddle == 1e-5:", (paddle_x == 1e-5 ) )              # False

Solution Approach

Implemented IEEE 754 round-to-nearest-even standard

// Rounding: round to nearest, ties to even
// https://en.wikipedia.org/wiki/Rounding#Rounding_half_to_even
const uint32_t lsb =
    (v.ui >> shift) & 0x1;               // Least significant retained bit
v.ui += (0xFFF + lsb) & -(v.ui < infN);  // Round with overflow protection

Validation

Input (fp32) Previous Output  New Output
1e-5f 0x00a7 0x00a8
0.333f 0x3553 0x3554
6.10056e-5f 0x03ff 0x0400
5e-8f 0x0000 0x0001

Others

These codes are very difficult to understand, so I have added some comments to clarify them.

Pcard-67164

Copy link

paddle-bot bot commented Jul 15, 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.

@paddle-bot paddle-bot bot added the contributor External developers label Jul 15, 2025
@DongBaiYue
Copy link
Contributor Author

/re-run coverage build

@DongBaiYue
Copy link
Contributor Author

/re-run Infer

@DongBaiYue
Copy link
Contributor Author

/re-run inference build

@DongBaiYue
Copy link
Contributor Author

/re-run all-failed

wanghuancoder
wanghuancoder previously approved these changes Jul 22, 2025
Copy link
Contributor

@wanghuancoder wanghuancoder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

lshpku
lshpku previously approved these changes Jul 22, 2025
@DongBaiYue DongBaiYue dismissed stale reviews from lshpku and wanghuancoder via f6030f4 July 24, 2025 06:23
@DongBaiYue
Copy link
Contributor Author

/re-run all-failed

3 similar comments
@DongBaiYue
Copy link
Contributor Author

/re-run all-failed

@DongBaiYue
Copy link
Contributor Author

/re-run all-failed

@DongBaiYue
Copy link
Contributor Author

/re-run all-failed

Copy link
Contributor

@wanghuancoder wanghuancoder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@lshpku lshpku merged commit a8bb594 into PaddlePaddle:develop Jul 31, 2025
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants