Repository navigation
Conversation
ahuber21
requested review from
ethanglaser,
homksei,
mihaic,
rfsaliev and
yuejiaointel
as code owners
December 1, 2026 07:35
ahuber21
force-pushed
the
dev/ahuber/emeans
branch
from
December 1, 2026 10:09
8ff724e to
d2e409f
Compare
mihaic
reviewed
Dec 1, 2026
mihaic
left a comment
Member
There was a problem hiding this comment.
The attribution and citation look good to me.
ahuber21
force-pushed
the
dev/ahuber/emeans
branch
from
December 2, 2026 10:25
d2e409f to
72b82aa
Compare
svs::kmeans::emeans_train in svs/core/kmeans_emeans.h implements Algorithm 1 of Bessa et al., arXiv:2611.09501: - EMA per-cluster counts and sums (centroid = S / N), with a 1-based batch counter as in Algorithm 1. - A cosine-annealed step from alpha0 down to 0.1 * alpha0. - Batch size and alpha0 from the paper's Table 2 scaling laws when unset, including the /8 batch law above n = 2M. - Dead-cluster revival by splitting every 5 batches at tau = 0.1. - A final pass that sets each centroid to the mean of its last-epoch points. It reuses centroid_assignment, generate_norms and init_centroids from kmeans_training.h. Assignment runs in chunks of at most assignment_chunk rows, so the score buffer is assignment_chunk x k floats rather than B x k. The state (k * d double sums, k counts) and all scratch are allocated once. Revival costs O(k + dead * log k) per round. Deviations and readings where the paper is silent are commented in the code: - the tail batch is trained; - power-of-two rounding is done in log2 space; - the donor pool excludes this round's dead clusters; - delta = 2^-7; - epochs defaults to 2, an SVS choice. The scaling laws were fitted on DEEP (L2, 256 <= k <= 65536). Outside that range they are applied as published. The returned objective is the MSE of the final centroids. Tests cover: determinism; revival of a forced-dead cluster; the scaling laws against hand-computed values; a tail batch; the k > n error and the other parameter checks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The e-means paper's authors publish a reference implementation in Faiss (contrib/e_means), so kmeans_emeans.h may contain portions derived from it. Add the file to the Faiss entry in THIRD-PARTY-PROGRAMS and point to it from the header; the paper citation stays. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ahuber21
force-pushed
the
dev/ahuber/emeans
branch
from
December 2, 2026 10:26
72b82aa to
13d784a
Compare
Contributor
There was a problem hiding this comment.
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Copilot review overview
6 open issues
resolve_emeans_parametershas undefined behavior whenn == 0:std::clamp(v, 1, n)violates… · New The default scaling-lawbatch_sizecan be very large for bign, and this implementation… · New Rounding to the nearest power of two vialog2/round/powcan become numerically fragile for… · New Bothemeans_trainoverloads takeData& data, but the docstrings say the training set is not… · New Bothemeans_trainoverloads takeData& data, but the docstrings say the training set is not… · New Given the new input-validation needs (notablyn == 0andk/num_clusters == 0), add unit tests… · New
What changed in this PR
Adds an e-means (mini-batch k-means with EMA centroid updates + dead-cluster revival) training API and accompanying unit tests, leveraging existing assignment/init utilities from kmeans_training.h.
Changes:
- Introduces
svs::kmeans::emeans_train(two overloads) plus parameter resolution utilities in a new public header. - Adds Catch2 unit tests covering determinism, objective skipping, dead-cluster revival, scaling-law resolution, tail batches, and error cases.
- Updates third-party attribution to include the new header as potentially derived from Faiss.
| File | Description |
|---|---|
| tests/svs/core/kmeans_training.cpp | Adds unit tests validating the new e-means training behavior and parameter resolution. |
| include/svs/core/kmeans_emeans.h | Implements the e-means training API, scaling-law parameter resolution, revival logic, and objective computation. |
| THIRD-PARTY-PROGRAMS | Extends Faiss attribution to cover the new header. |
Review effort: Lite
Comment on lines
+458
to
+468
| resolve_emeans_parameters(size_t n, size_t k, const EMeansParameters& parameters) { | ||
| size_t batch_size = std::clamp( | ||
| parameters.batch_size.value_or(detail::resolve_batch_size(n, k, parameters.epochs)), | ||
| size_t{1}, | ||
| n | ||
| ); | ||
| double alpha0 = parameters.alpha0.value_or( | ||
| detail::resolve_alpha0(n, k, batch_size, parameters.epochs) | ||
| ); | ||
| return ResolvedEMeansParameters{batch_size, alpha0}; | ||
| } |
Comment on lines
+559
to
+567
| // Scratch buffers sized once and reused for every batch; chunk_matmul_results is | ||
| // capped at chunk_size rows, not batch_size (B from the scaling law can be huge). | ||
| auto centroids_fp32 = data::SimpleData<float>(k, dims); | ||
| auto gathered = data::SimpleData<float>(batch_size, dims); | ||
| auto chunk_matmul_results = data::SimpleData<float>(chunk_size, k); | ||
| std::vector<float> chunk_norm_scratch(chunk_size); | ||
| std::vector<size_t> chunk_assignments(chunk_size); | ||
| std::vector<float> centroids_norm(k); | ||
| std::vector<size_t> local_assignments(batch_size); |
Comment on lines
+110
to
+119
| inline size_t resolve_batch_size(size_t n, size_t k, size_t epochs) { | ||
| double b_c = n > EMEANS_BATCH_LARGE_N_THRESHOLD ? EMEANS_BATCH_COEFF_LARGE_N | ||
| : EMEANS_BATCH_COEFF_SMALL_N; | ||
| double raw = b_c * std::pow(static_cast<double>(k), EMEANS_BATCH_EXP_K) * | ||
| std::pow(static_cast<double>(n), EMEANS_BATCH_EXP_N) * | ||
| std::pow(static_cast<double>(epochs), EMEANS_BATCH_EXP_T); | ||
| double pow2 = std::pow(2.0, std::round(std::log2(std::max(raw, 1.0)))); | ||
| size_t b = static_cast<size_t>(std::min(pow2, static_cast<double>(n))); | ||
| return std::clamp(b, size_t{1}, n); | ||
| } |
Comment on lines
+487
to
+494
| EMeansResult emeans_train( | ||
| const EMeansParameters& parameters, | ||
| Data& data, | ||
| data::SimpleData<float> initial_centroids, | ||
| std::mt19937& rng, | ||
| Pool& threadpool, | ||
| lib::Timer& timer | ||
| ) { |
Comment on lines
+758
to
+765
| EMeansResult emeans_train( | ||
| const EMeansParameters& parameters, | ||
| Data& data, | ||
| size_t num_clusters, | ||
| std::mt19937& rng, | ||
| Pool& threadpool, | ||
| lib::Timer& timer | ||
| ) { |
Comment on lines
+405
to
+419
| CATCH_SECTION("k > n throws") { | ||
| auto data = svs::data::SimpleData<float>(3, 2); | ||
| data.set_datum(0, std::vector<float>{0.0F, 0.0F}); | ||
| data.set_datum(1, std::vector<float>{1.0F, 0.0F}); | ||
| data.set_datum(2, std::vector<float>{0.0F, 1.0F}); | ||
|
|
||
| auto parameters = small_emeans_parameters(); | ||
| auto rng = std::mt19937(0xc0ffee); | ||
| auto threadpool = svs::threads::as_threadpool(2); | ||
| auto timer = svs::lib::Timer(); | ||
|
|
||
| CATCH_REQUIRE_THROWS( | ||
| svs::kmeans::emeans_train(parameters, data, size_t{5}, rng, threadpool, timer) | ||
| ); | ||
| } |
mihaic
reviewed
Dec 3, 2026
mihaic
left a comment
Member
There was a problem hiding this comment.
Please have a look at the Copilot suggestions which look reasonable to me.
Copilot stopped reviewing on behalf of
mihaic due to an error
December 3, 2026 20:14
This branch has not been deployed
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.


Adds e-means (
svs::kmeans::emeans_train,svs/core/kmeans_emeans.h): mini-batch k-means with EMA centroid updates,a cosine-annealed step, dead-cluster revival and the batch-size/step scaling laws of Bessa et al., arXiv:2611.09501
(Algorithm 1). It reuses the assignment and init code that the previous PR moved into
svs/core/kmeans_training.h.Things a reviewer should know:
kmeans_training.h, which thePQ PR introduces.
emeans_train; it is covered by unit tests only. PQ training stilluses the existing k-means.
log2 space, delta = 2^-7).
epochsdefaults to 2, an SVS choice, not the paper's. The scaling laws were fitted on DEEP(L2, 256 <= k <= 65536) and are applied as published outside that range.