Fix bf16 binary search in topk_mask#167
Draft
oliverdutton wants to merge 2 commits intolatency_hiding_binary_searchfrom
Draft
Fix bf16 binary search in topk_mask#167oliverdutton wants to merge 2 commits intolatency_hiding_binary_searchfrom
oliverdutton wants to merge 2 commits intolatency_hiding_binary_searchfrom
Conversation
The binary search used f32/u32-space midpoints with only 16 iterations for bf16 inputs. Since bf16 values cast to f32 are spaced ~0x10000 apart in u32 monotonic space, 16 iterations leave a window of 0x10000 — exactly one bf16 step — causing the threshold to land between adjacent bf16 values and producing off-by-one counts. Add interp_bf16_as_f32 which operates in 16-bit bf16 monotonic space so midpoints are always bf16-representable and 16 iterations exactly suffice. https://claude.ai/code/session_01KBsJLkK3MvJm5PPFrtztq5
Replace the separate bf16 monotonic space (interp_bf16_as_f32 and associated conversion functions) with a simpler approach: add an underlying_dtype parameter to interp() that, for bfloat16, zeroes the lower 16 f32 mantissa bits (& 0xFFFF0000) after the u32→f32 conversion. This snaps every midpoint to the bf16 grid since bf16 values occupy the upper 16 bits of f32 regardless of sign. https://claude.ai/code/session_01KBsJLkK3MvJm5PPFrtztq5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The binary search used f32/u32-space midpoints with only 16 iterations
for bf16 inputs. Since bf16 values cast to f32 are spaced ~0x10000 apart
in u32 monotonic space, 16 iterations leave a window of 0x10000 — exactly
one bf16 step — causing the threshold to land between adjacent bf16
values and producing off-by-one counts.
Add interp_bf16_as_f32 which operates in 16-bit bf16 monotonic space so
midpoints are always bf16-representable and 16 iterations exactly suffice.
https://claude.ai/code/session_01KBsJLkK3MvJm5PPFrtztq5