Repository navigation
gpl: Fix overflow, sampling bias, and edge cases in MBFF KMeans, GetS… - #11645
Open
debayanbandyopadhyay wants to merge 1 commit into
Open
debayanbandyopadhyay wants to merge 1 commit into
debayanbandyopadhyay wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces several robustness improvements to the multi-bit flip-flop (MBFF) clustering algorithms, including guarding against division-by-zero and NaN cases in silhouette calculations, preventing infinite loops and integer overflows in K-Means++ when handling large designs or co-located flops, and using double-precision floats for distance accumulation. The reviewer suggested a valuable performance optimization in the K-Means center selection phase to update distances using only the latest center, which reduces the complexity from O(K^2 * N) to O(K * N).
debayanbandyopadhyay
force-pushed
the
fix-mbff-kmeans-overflow-and-silh
branch
4 times, most recently
from
October 7, 2026 12:09
b153863 to
f5b946a
Compare
1. Guard empty inputs and small flop subsets: - Return early in `MBFF::KMeans` when `num_flops == 0`. - Cap `knn = std::min(knn, num_flops)` in `MBFF::KMeans` and `MBFF::KMeansDecomp` when `max_sz < num_flops < 8`, avoiding empty clusters (`cur_sz == 0`) and division-by-zero (`0 / 0 = NaN` in `GetKSilh` and `% 0` in `KMeans`). - Guard single-element (`cur_sz <= 1`), co-located (`max_ab == 0`), and single-center (`b_j == float::max()`) clusters in `MBFF::GetKSilh`. 2. Prevent integer overflow and sampling bias in `KMeans++` initialization: - Accumulate `d[i]` in `double` (`tot_sum`) instead of `float` and normalize probabilities to `kLargeDesignProbResolution = 1,000,000` using two combined random draws (`(rand_hi << 15) ^ rand_lo`, ensuring >=30 bits of entropy even when `RAND_MAX == 32767`) only when `raw_scaled_sum > std::numeric_limits<int>::max()`, preserving exact legacy behavior on small/medium designs. - Guard co-located flops (`raw_scaled_sum < 1.0`) to prevent modulo-by-zero (`% 0` UB). - Mask the sign bit (`val & 0x7FFFFFFF`) in `next_rand()` so negative entries in `rand_nums` (including `INT_MIN`) cannot trigger `std::abs(INT_MIN)` UB or produce negative modulo results. - Update minimum distances incrementally against `centers.back().pt` in `O(K * N)` instead of recomputing all center distances in `O(K^2 * N)`. 3. Unit tests: - Added unit tests in `src/gpl/test/mbff_test.cpp` covering empty input, small flop counts (`1 < num_flops < 8`), single-cluster `GetKSilh`, large-coordinate overflow, and negative `rand_nums` (including `INT_MIN`). Signed-off-by: Debayan Bandyopadhyay <dbandyopadhyay@google.com>
debayanbandyopadhyay
force-pushed
the
fix-mbff-kmeans-overflow-and-silh
branch
from
October 7, 2026 12:13
f5b946a to
699ab68
Compare
debayanbandyopadhyay
marked this pull request as ready for review
October 7, 2026 12:30
LucasYuki
approved these changes
Oct 7, 2026
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.
Summary
MBFF::KMeans:tot_sum,cum_sum) andprobindoublerather than 32-bitfloatto prevent mantissa absorption (> 1.67e7) on large clock domains (100k+flops) and eliminate float-to-integer cast overflow UB even for extreme coordinates (tot_sum * 100.0 > INT64_MAX, or near theINT_MAXboundary wherestatic_cast<float>(INT_MAX)rounds up to2147483648.0f).kLegacyProbScale = 100.0,kLargeDesignProbResolution = 1000000): preserve exact RNG modulo scaling for small designs (1.0 <= raw_scaled_sum <= INT_MAX), and combine two 15+ bit draws fromnext_rand()((rand_hi << 15) ^ rand_lo) for uniform 6-digit fractional scaling (tot_sum * fraction) whenraw_scaled_sum > INT_MAXsoprobis not hard-capped atRAND_MAX / 100.0(even whenRAND_MAX == 32767).next_rand()helper that wrapsrand_numsmodulorand_nums.size(), preserves the lower 31 bits of entropy (val & 0x7FFFFFFF) for negative 32-bit integers, guards against emptyrand_nums, and keeps the RNG stream aligned whenraw_scaled_sum < 1.0.d[i]incrementally againstcenters.back().ptduring K-Means++ center selection, reducing center initialization complexity fromO(K^2 * N)toO(K * N).knn > num_flops:actual_knn = std::min(knn, num_flops)inMBFF::KMeans(and return early ifnum_flops == 0 || knn <= 0).max_k = std::min(8, num_flops)andbest_k = std::min(4, max_k)inMBFF::KMeansDecomp(and return early ifnum_flops < 2).NaNandfloat::max()overflow:0.0f / 0.0f(NaN) andstd::numeric_limits<float>::max()overflow inMBFF::GetSilhandMBFF::GetKSilhwhen flops or tray slots are co-located or when only a single tray/center exists.Point,Tray, andFlopinsrc/gpl/src/mbff.hand add unit tests insrc/gpl/test/mbff_test.cppcovering large/extreme distances, negative 32-bitrand_nums, co-located flops/slots,knn > num_flops, short/emptyrand_nums,GetSilh, andGetKSilh.Type of Change
Impact
1.0 <= tot_sum * 100.0 <= INT_MAX.tot_sum * 100.0 > INT_MAX) and reduces K-Means++ initialization fromO(K^2 * N)toO(K * N).MBFF::KMeansorMBFF::KMeansDecompis invoked on small flop partitions (num_flops < knnornum_flops < 8).NaNorfloat::max()silhouette scores inMBFF::GetSilhandMBFF::GetKSilhon co-located flops/slots or single-tray/single-center cases.Verification
./etc/Build.sh/bazelisk).bazelisk test //src/gpl/test/...— 109 passed).git clang-format upstream/master).Related Issues
Follow-up to #11426