Skip to content

Fix bf16 binary search in topk_mask#167

Draft
oliverdutton wants to merge 2 commits intolatency_hiding_binary_searchfrom
claude/fix-bf16-topk-mask-WA8uy
Draft

Fix bf16 binary search in topk_mask#167
oliverdutton wants to merge 2 commits intolatency_hiding_binary_searchfrom
claude/fix-bf16-topk-mask-WA8uy

Conversation

@oliverdutton
Copy link
Copy Markdown
Owner

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

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
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