Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

math_brute_force: fix fdim to use device's rounding when converting result back to half. #2223

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

cycheng
Copy link
Contributor

@cycheng cycheng commented Jan 10, 2025

In the half-precision fdim test, the original code used CL_HALF_RTE to convert the float result back to half, causing a mismatch in computation results when the hardware uses RTZ. Some of the examples:

  fdim(0x365f, 0xdc63) = fdim( 0.398193f,  -280.75f)     =   281.148193f (RTE=0x5c65, RTZ=0x5c64)
  fdim(0xa4a3, 0xf0e9) = fdim(-0.018112f, 10056.0f)      = 10055.981445f (RTE=0x70e9, RTZ=0x70e8)
  fdim(0x1904, 0x9ab7) = fdim( 0.002449f,    -0.003279f) =     0.005728f (RTE=0x1dde, RTZ=0x1ddd)

Fixed this by using the hardware's default rounding mode when converting the result back to half.

@@ -312,9 +317,9 @@ cl_int TestHalf(cl_uint job_id, cl_uint thread_id, void *data)
s2[j] = cl_half_to_float(p2[j]);
if (isNextafter)
r[j] = cl_half_from_float(reference_nextafterh(s[j], s2[j]),
Copy link
Member

Choose a reason for hiding this comment

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

The changes look reasonable to me.

However this affects all binary fp16 operations as far as I can tell, not only fdim? It would be good to make that clear in the commit message and description; right now it suggests the change would only affect fdim which is not the case.

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.

2 participants