Commit bc4e1cd
authored
[Cleanup] Combine Batched and Regular KMeans Impl (#2015)
## Combine batched and regular k-means implementations
- Unified the batched (host-data) and regular (device-data) k-means `fit` into a single `kmeans_fit` template that works with both host and device mdspans via `batch_load_iterator`
- Unified the device and host initialization paths in `init_centroids`
- Removed the `inertia_check` parameter — inertia-based convergence checking now always runs. Zero clustering cost (perfect fit) logs a warning instead of asserting. This is needed because spectral clustering can cause all points to converge on the cluster centroids itself.
- Added `init_size` parameter to control how many samples are drawn for KMeansPlusPlus initialization. Defaults to `n_samples` for device data, `(3 * n_clusters)` for host data
- Replaced per-iteration centroid `raft::copy` with `std::swap` of buffer pointers
- For streaming fit, precompute data norms once and cache them: host norms cached to a host buffer on the first iteration and copied back for subsequent iterations. `process_batch` no longer computes norms internally
- Replaced raw `cudaPointerGetAttributes` call with `raft::memory_type_from_pointer`
- Replaced `cub::DeviceReduce::Sum` calls with `raft::linalg::mapThenSumReduce`
- Guarded weight normalization against overflow: apply `(w / wt_sum) * n_samples` via a composed op instead of precomputing a scale, so very small `wt_sum` values don't produce inf
- Renamed `checkWeight` to `weightSum` and made it mdspan-based with an `Accessor` template: device reduce for device weights, host loop for host weights. Callers apply the scaling themselves
- Eliminated `batch_sums` / `batch_counts` scratch buffers by accumulating directly into `centroid_sums` / `weight_per_cluster` via `reset_sums=false` in `reduce_rows_by_key` / `reduce_cols_by_key`, removing two per-batch `raft::linalg::add` kernels
- Removed dead `update_centroids` helpers (both the `detail` and public template) — no remaining callers after the `fit_main` consolidation
- Perf: remove multiple `raft::sync_stream` calls and add a CUDA Event to record if the convergence criteria is met. Convergence check is now done on device. Average per-iteration time with mandatory inertia check now matches previous benchmarks even when previously inertia check was disabled.
## C Tests
This PR adds C tests for KMeans. These were missing. Here we test both -- the old version and the new (i.e. breaking change).
## Benchmarks:
With mandatory early stopping. Batch size is such that we fill up 90% of available GPU memory (95830MiB)
HW:
GPU:
`NVIDIA H100 NVL (CUDA 13.0)`
CPU:
```
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Address sizes: 52 bits physical, 57 bits virtual
Byte Order: Little Endian
CPU(s): 256
On-line CPU(s) list: 0-255
Vendor ID: AuthenticAMD
Model name: AMD EPYC 9554 64-Core Processor
```
```
================================================================================
SUMMARY
================================================================================
n_clusters batch_size fit_time(s) inertia n_iter
----------------------------------------------------------------
10,000 29,120,352 1584.72 2.8677e+08 30
20,000 29,120,352 2907.34 2.7368e+08 31
30,000 29,101,305 4254.43 2.6617e+08 31
40,000 29,092,704 5836.12 2.6086e+08 32
50,000 29,083,488 7107.04 2.5680e+08 31
```
## Breaking Change
This PR is a breaking change of the C++ API because the `inertia_check` param is removed. The breaking changes to the C ABI will be applied in 26.08
Authors:
- Tarang Jain (https://github.com/tarang-jain)
Approvers:
- Victor Lafargue (https://github.com/viclafargue)
- Dante Gama Dessavre (https://github.com/dantegd)
- Micka (https://github.com/lowener)
URL: #20151 parent e3af4cd commit bc4e1cd
17 files changed
Lines changed: 1517 additions & 1092 deletions
File tree
- cpp
- include/cuvs/cluster
- src
- cluster
- detail
- neighbors/ivf_pq
- tests/cluster
- c
- include/cuvs/cluster
- src/cluster
- tests
- cluster
- python/cuvs/cuvs/cluster/kmeans
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
| 42 | + | |
| 43 | + | |
42 | 44 | | |
43 | 45 | | |
44 | 46 | | |
| |||
91 | 93 | | |
92 | 94 | | |
93 | 95 | | |
94 | | - | |
| 96 | + | |
95 | 97 | | |
96 | 98 | | |
97 | 99 | | |
| |||
108 | 110 | | |
109 | 111 | | |
110 | 112 | | |
111 | | - | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
112 | 200 | | |
113 | 201 | | |
114 | 202 | | |
| 203 | + | |
115 | 204 | | |
116 | 205 | | |
117 | 206 | | |
118 | 207 | | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
119 | 211 | | |
120 | 212 | | |
121 | 213 | | |
| |||
124 | 216 | | |
125 | 217 | | |
126 | 218 | | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
127 | 222 | | |
128 | 223 | | |
129 | 224 | | |
130 | 225 | | |
131 | 226 | | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
132 | 246 | | |
133 | 247 | | |
134 | 248 | | |
| |||
154 | 268 | | |
155 | 269 | | |
156 | 270 | | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
157 | 274 | | |
158 | 275 | | |
159 | 276 | | |
| |||
181 | 298 | | |
182 | 299 | | |
183 | 300 | | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
184 | 334 | | |
185 | 335 | | |
186 | 336 | | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
187 | 340 | | |
188 | 341 | | |
189 | 342 | | |
| |||
209 | 362 | | |
210 | 363 | | |
211 | 364 | | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
212 | 396 | | |
213 | 397 | | |
214 | 398 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
20 | 22 | | |
21 | 23 | | |
22 | 24 | | |
| |||
28 | 30 | | |
29 | 31 | | |
30 | 32 | | |
31 | | - | |
| 33 | + | |
32 | 34 | | |
33 | 35 | | |
34 | 36 | | |
35 | 37 | | |
36 | | - | |
| 38 | + | |
| 39 | + | |
37 | 40 | | |
38 | 41 | | |
39 | 42 | | |
40 | 43 | | |
41 | 44 | | |
42 | 45 | | |
43 | 46 | | |
44 | | - | |
| 47 | + | |
45 | 48 | | |
46 | | - | |
| 49 | + | |
47 | 50 | | |
48 | 51 | | |
49 | 52 | | |
| |||
140 | 143 | | |
141 | 144 | | |
142 | 145 | | |
143 | | - | |
| 146 | + | |
144 | 147 | | |
145 | | - | |
| 148 | + | |
146 | 149 | | |
147 | 150 | | |
148 | 151 | | |
| |||
237 | 240 | | |
238 | 241 | | |
239 | 242 | | |
240 | | - | |
| 243 | + | |
241 | 244 | | |
242 | 245 | | |
243 | | - | |
| 246 | + | |
| 247 | + | |
244 | 248 | | |
245 | 249 | | |
246 | 250 | | |
| |||
294 | 298 | | |
295 | 299 | | |
296 | 300 | | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
297 | 374 | | |
298 | 375 | | |
299 | 376 | | |
| |||
0 commit comments