emeans-rocm 0.2.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. emeans_rocm-0.2.4/.gitignore +16 -0
  2. emeans_rocm-0.2.4/LICENSE +21 -0
  3. emeans_rocm-0.2.4/PKG-INFO +463 -0
  4. emeans_rocm-0.2.4/README.md +442 -0
  5. emeans_rocm-0.2.4/contrib/e_means/README.md +632 -0
  6. emeans_rocm-0.2.4/contrib/e_means/__init__.py +52 -0
  7. emeans_rocm-0.2.4/contrib/e_means/base.py +457 -0
  8. emeans_rocm-0.2.4/contrib/e_means/capabilities.py +198 -0
  9. emeans_rocm-0.2.4/contrib/e_means/cute.py +605 -0
  10. emeans_rocm-0.2.4/contrib/e_means/cute_argmin.py +609 -0
  11. emeans_rocm-0.2.4/contrib/e_means/cute_grouped.py +364 -0
  12. emeans_rocm-0.2.4/contrib/e_means/cute_kernel.py +672 -0
  13. emeans_rocm-0.2.4/contrib/e_means/cute_row_owned.py +778 -0
  14. emeans_rocm-0.2.4/contrib/e_means/device_profile.py +226 -0
  15. emeans_rocm-0.2.4/contrib/e_means/e_means.py +659 -0
  16. emeans_rocm-0.2.4/contrib/e_means/e_means_distributed.py +268 -0
  17. emeans_rocm-0.2.4/contrib/e_means/e_means_grouping.py +198 -0
  18. emeans_rocm-0.2.4/contrib/e_means/e_means_quantized.py +697 -0
  19. emeans_rocm-0.2.4/contrib/e_means/e_means_spherical.py +43 -0
  20. emeans_rocm-0.2.4/contrib/e_means/e_means_torch.py +1159 -0
  21. emeans_rocm-0.2.4/contrib/e_means/figure/emeans-wordmark-framed.png +0 -0
  22. emeans_rocm-0.2.4/contrib/e_means/figure/readme_figure_v6.png +0 -0
  23. emeans_rocm-0.2.4/contrib/e_means/ivf_flat.md +133 -0
  24. emeans_rocm-0.2.4/contrib/e_means/ivf_flat.py +564 -0
  25. emeans_rocm-0.2.4/contrib/e_means/quantizer.py +181 -0
  26. emeans_rocm-0.2.4/contrib/e_means/quantizer_fp8.py +257 -0
  27. emeans_rocm-0.2.4/contrib/e_means/quantizer_int8.py +223 -0
  28. emeans_rocm-0.2.4/contrib/e_means/sharded_centroids.py +682 -0
  29. emeans_rocm-0.2.4/contrib/e_means/sharded_centroids_ivf.py +156 -0
  30. emeans_rocm-0.2.4/contrib/e_means/torch_backend.py +565 -0
  31. emeans_rocm-0.2.4/contrib/e_means/triton_assignment.py +1733 -0
  32. emeans_rocm-0.2.4/pyproject.toml +48 -0
@@ -0,0 +1,16 @@
1
+ __pycache__/
2
+ *.pyc
3
+ /.venv/
4
+ /dist/
5
+ /build/
6
+
7
+ # local planning docs (not tracked)
8
+ /docs/
9
+
10
+ # e-means ROCm probe outputs
11
+ tools/rocm_probe/ported_kernels.py
12
+ tools/rocm_probe/assign_kernels.py
13
+ tools/rocm_probe/plain_*.s
14
+ tools/rocm_probe/plain_*.ll
15
+ w0_smoke_results.jsonl
16
+ results/
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Facebook, Inc. and its affiliates.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,463 @@
1
+ Metadata-Version: 2.5
2
+ Name: emeans-rocm
3
+ Version: 0.2.4
4
+ Summary: E-means mini-batch k-means from faiss contrib, with AMD ROCm GPU support through Triton
5
+ Project-URL: Source, https://github.com/ogaude/e-means-rocm
6
+ Project-URL: Upstream, https://github.com/facebookresearch/faiss/tree/main/contrib/e_means
7
+ Author: Meta Platforms, Inc. and affiliates
8
+ Maintainer: ogaude
9
+ License-Expression: MIT
10
+ License-File: LICENSE
11
+ Keywords: amd,clustering,faiss,gpu,k-means,rocm,triton
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Intended Audience :: Science/Research
14
+ Classifier: Operating System :: POSIX :: Linux
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
17
+ Requires-Python: >=3.10
18
+ Requires-Dist: faiss-cpu
19
+ Requires-Dist: numpy
20
+ Description-Content-Type: text/markdown
21
+
22
+ # e-means-rocm
23
+
24
+ AMD GPU (ROCm) support for [e-means](https://github.com/ogaude/e-means-rocm/blob/main/contrib/e_means/README.md), the mini-batch k-means in
25
+ faiss `contrib/e_means`. The goal is that `Emeans(d, k).train(x)` runs on AMD GPUs through the
26
+ fast Triton kernels, as it does on NVIDIA, with NVIDIA results unchanged.
27
+
28
+ > **Status: v0.2.4 released (tag `e-means-rocm-v0.2.4`) for RX 9000 (gfx1200), installable with
29
+ > `pip install emeans-rocm`. MI300X follows in v0.3.**
30
+ > On an RX 9060 XT (gfx1200, ROCm 10.1) the default `Emeans(d, k).train(x)` uses Triton, every
31
+ > data format trains (FP32, FP16, BF16; INT8/UINT8 and FP8 through `QuantizedEmeans`), the whole e-means test suite passes,
32
+ > and the rewritten kernels match PyTorch bit for bit in the numerics tests. Triton is 2.1× to 9.3× faster than
33
+ > PyTorch for BF16, and tuned INT8 and FP8 train 1.02× to 1.37× faster than BF16. MI300X
34
+ > (gfx942) is planned for v0.3; MI350 (gfx950) and RX 7000 (gfx11xx) are compile-checked only.
35
+ > **NVIDIA has not been re-run on hardware** (see Known limitations).
36
+
37
+ ## Quick start
38
+
39
+ Install a ROCm build of PyTorch and Triton in a fresh virtual environment, then the package
40
+ `emeans-rocm` from PyPI. For an RX 9000 card (gfx1200) on ROCm 10.1:
41
+
42
+ ```bash
43
+ python3 -m venv ~/venvs/emeans-rocm && source ~/venvs/emeans-rocm/bin/activate
44
+ python -m pip install --index-url https://stable.repo.amd.com/rocm/whl-next/ \
45
+ "torch[device-gfx1200]==2.14.0+rocm10.1.0" # brings triton 3.8.0+rocm10.1.0
46
+ python -m pip install emeans-rocm
47
+ ```
48
+
49
+ The wheel extra is named after the GPU target (`device-gfx1200` here; `rocminfo` shows yours).
50
+ Install torch first: the package does not list it as a dependency, because pip would then
51
+ install the CUDA build from PyPI. The package pulls in `faiss-cpu` and `numpy` (in a conda
52
+ environment with `faiss-gpu`, install it with `--no-deps` to keep pip's faiss-cpu out). For the code on
53
+ `main` instead of the last release, use `python -m pip install "emeans-rocm @
54
+ git+https://github.com/ogaude/e-means-rocm"`, or `python -m pip install .` from a clone.
55
+
56
+ The package's import name is `emeans_rocm`. It holds the same code as `contrib/e_means` and
57
+ sits next to the installed faiss, whose own `faiss.contrib.e_means` (upstream's, older copy)
58
+ it leaves alone:
59
+
60
+ ```python
61
+ import torch
62
+ from emeans_rocm import Emeans
63
+
64
+ x = torch.randn(200_000, 64, device="cuda") # "cuda" also means AMD on ROCm
65
+ em = Emeans(64, 1024)
66
+ centroids, labels = em.train(x)
67
+ print(em.recipe["assignment_backend"]) # expected: "triton" on a supported AMD GPU
68
+ ```
69
+
70
+ INT8 and FP8 data go through `QuantizedEmeans` with an `Int8Quantizer` or `Fp8Quantizer`; see
71
+ the [e-means README](https://github.com/ogaude/e-means-rocm/blob/main/contrib/e_means/README.md#kernels), whose examples import
72
+ `faiss.contrib.e_means` (read `emeans_rocm` with the package).
73
+
74
+ ### Development setup
75
+
76
+ The tests, benchmarks and tools import `faiss.contrib.e_means`, as upstream's do. To work on
77
+ them, clone the repository (`git clone https://github.com/ogaude/e-means-rocm && cd
78
+ e-means-rocm`), install torch as above and `python -m pip install faiss-cpu numpy`; inside a
79
+ clone, `tools/gpu_session/setup.sh` does the install and the next step for the detected target.
80
+
81
+ The `faiss-cpu` wheel ships its own, older copy of `faiss/contrib/e_means`, which would shadow
82
+ this repository. From the repository root, replace it with a symlink to the working tree (the
83
+ wheel's copy is kept aside):
84
+
85
+ ```bash
86
+ C=$(python -c "import faiss.contrib, os; print(os.path.dirname(faiss.contrib.__file__))")
87
+ mv "$C/e_means" "$C/e_means.pypi-orig"
88
+ ln -s "$PWD/contrib/e_means" "$C/e_means"
89
+ python -c "import os, faiss.contrib.e_means as m; print(os.path.realpath(m.__file__))" # this repo
90
+ ```
91
+
92
+ The example above then runs with `from faiss.contrib.e_means import Emeans`. Remove the
93
+ symlink before reinstalling or uninstalling `faiss-cpu`: pip follows it and can delete or
94
+ overwrite files in the clone.
95
+
96
+ Do not use `python -m pip install -e .` for development: the package maps `contrib/e_means`
97
+ onto `emeans_rocm` at build time, and an editable install then gets a plain copy (pip still
98
+ reports it as editable), so later edits in the clone do not reach it. Reinstall with
99
+ `python -m pip install .` after changes, or work through the symlink.
100
+
101
+ ## Target GPUs
102
+
103
+ | Family | Architecture | Example GPUs | FP32 / FP16 / BF16 | INT8 | FP8 (E4M3) |
104
+ |---|---|---|---|---|---|
105
+ | CDNA 3 | gfx942 | MI300X, MI325X | compile-checked; run and tuning in v0.3 (cloud) | compile-checked | refused (no native E4M3) |
106
+ | CDNA 4 | gfx950 | MI350 series | compile-checked | compile-checked | compile-checked |
107
+ | RDNA 3 | gfx11xx | RX 7000 series, RDNA 3.5 APUs | compile-checked | compile-checked | refused (no native E4M3) |
108
+ | RDNA 4 | gfx120x | RX 9000 series | **runs, tuned** (v0.1) | **runs, tuned** (v0.2) | **runs, tuned** (v0.2) |
109
+
110
+ "Compile-checked" means that every kernel configuration the launcher can pick for that target
111
+ compiles within its shared-memory (LDS) budget, checked by GPU-free tests
112
+ (`torch_test_e_means_launch_config.py`, `torch_test_e_means_int8_compile.py`), and is selected
113
+ automatically, but has not been run. They use the NVIDIA-fitted tile settings, except that INT8
114
+ gets a pipeline depth that fits the family's LDS, and gfx950 a 64-row tile and one split-K stage
115
+ (see Known limitations). Sibling chips
116
+ (gfx1101, gfx1201, ...) are matched by family. Other AMD GPUs fall back to native FP32 through
117
+ PyTorch, which upstream already supports.
118
+
119
+ ## Results on RX 9000 (gfx1200)
120
+
121
+ Measured on an RX 9060 XT (16 GB), ROCm 10.1, torch 2.14.0+rocm10.1.0, triton 3.8.0, all in one
122
+ run (2026-11-20). Times are warm `train()` runs of 3 epochs on random FP32 data, converted to
123
+ each format: BF16 and FP16 by the default path during training, INT8 and FP8 by a calibrated
124
+ quantizer before timing (encoding not timed, as when data is stored encoded). "PyTorch" is
125
+ `assignment_backend="native"`. `tools/bench_train.py` reproduces these runs; speed-ups below are
126
+ computed from the unrounded times.
127
+
128
+ | Data format | 200k × 64, 1,024 centroids | 1M × 128, 4,096 centroids | 4M × 128, 16,384 centroids |
129
+ |---|---|---|---|
130
+ | BF16, Triton (the default for FP32 input) | 0.028 s | 0.232 s | 1.491 s |
131
+ | BF16, PyTorch | 0.058 s | 1.008 s | 13.864 s |
132
+ | FP16, Triton | 0.029 s | 0.227 s | 1.503 s |
133
+ | FP16, PyTorch | 0.058 s | 0.980 s | 13.410 s |
134
+ | Native FP32, Triton | 0.039 s | 0.638 s | 8.601 s |
135
+ | Native FP32, PyTorch | 0.063 s | 1.015 s | 14.132 s |
136
+ | INT8, Triton | 0.028 s | 0.193 s | 1.090 s |
137
+ | FP8, Triton | **0.025 s** | **0.169 s** | 1.143 s |
138
+
139
+ | Speed-up | 200k × 64 | 1M × 128 | 4M × 128 |
140
+ |---|---|---|---|
141
+ | Triton over PyTorch, BF16 | 2.1× | 4.3× | 9.3× |
142
+ | Triton over PyTorch, FP16 | 2.0× | 4.3× | 8.9× |
143
+ | Triton over PyTorch, FP32 | 1.6× | 1.6× | 1.6× |
144
+ | INT8 over BF16 (Triton) | 1.02× | 1.20× | 1.37× |
145
+ | FP8 over BF16 (Triton) | 1.13× | 1.37× | 1.30× |
146
+
147
+ Final losses: FP16 and FP32 within 0.02% of BF16, INT8 within 0.06%, FP8 within 0.08%.
148
+
149
+ ### What the RDNA4 tuning gains
150
+
151
+ Assignment kernel, tuned tables against the NVIDIA-fitted settings (`tools/tune_assign.py
152
+ --compare`, d = 32 to 768 (FP8: 64 to 768), k = 1,024 to 16,384, re-measured 2026-11-20). INT8
153
+ results are identical; for the other formats the tool accepts a configuration when the labels
154
+ are the same or, where tie order differs, the distances agree within rtol 1e-5 / atol 1e-4.
155
+
156
+ | Format | 65,536-row batches | 2,048-row batches |
157
+ |---|---|---|
158
+ | BF16 / FP16 | 1.06× to 2.80× | 0.95× to 3.45× |
159
+ | Native FP32 | 1.16× to 1.40× | 1.04× to 1.34× |
160
+ | INT8 | 2.99× to 8.51× | 2.57× to 10.4× |
161
+ | FP8 | 1.22× to 2.74× | 0.99× to 2.75× |
162
+
163
+ Training, each format with its tables on against off (same run, 3 warm epochs;
164
+ `tools/bench_train.py --no-tables` gives the "off" side):
165
+
166
+ | Format | 200k × 64 | 1M × 128 | 4M × 128 |
167
+ |---|---|---|---|
168
+ | BF16 | 1.00× | 1.26× | 1.80× |
169
+ | INT8 | 1.20× | 1.35× | 1.94× |
170
+ | FP8 | 1.08× | 1.31× | 1.66× |
171
+
172
+ The split-K choice (below) does not affect training, whose batches are large.
173
+
174
+ ### Findings
175
+
176
+ - **Keep BF16 on RX cards.** RDNA has no FP32 matrix instruction, so native FP32 costs 1.4× to
177
+ 5.8× the time of BF16 here, more as the problem grows. Triton is still the faster FP32 path,
178
+ so it stays the automatic choice.
179
+ - **The NVIDIA-fitted tile settings are slow on RDNA4** (wave32): mostly from 128-row tiles at
180
+ d > 128, and the stage count and tile shape on the full-row path (64 < d ≤ 128).
181
+ - **INT8 and FP8 need their own settings even more.** With the NVIDIA-fitted settings for both,
182
+ INT8 trained 0.83× to 1.27× as fast as BF16; with the tuned ones for both, 1.00× to 1.37×
183
+ (same run as the tuning-gain table). The largest INT8 kernel gains
184
+ (5× to 10×) are at d ≤ 64, where upstream pads every row to 128. One pipeline stage won on
185
+ every INT8 and FP8 shape swept.
186
+ - **Settings tuned at one batch size can lose at another.** The v0.1 BF16 settings for d ≤ 64,
187
+ tuned at 65,536 rows, ranged from 0.55× to 1.72× of upstream's speed between 2,048 and
188
+ 262,144 rows (128-row tiles at d ≤ 32 leave work-groups idle on small batches). One
189
+ configuration chosen over that whole range now stays at 0.96× to 1.69×.
190
+ - **Split-K pays only on very small row counts on RDNA4.** At 256 rows it is 1.1× to 3.9× faster
191
+ in all but one case (d = 64 with 4,096 centroids: 0.71×, still chosen). At 1,024 and 4,096 rows
192
+ it is 1.6× to 3.8× slower, yet the NVIDIA-fitted rule chose it up to 4,032 rows at d = 64 and
193
+ 8,128 rows at d ≥ 128, so RDNA4 now uses it up to 512 rows only (`tools/tune_split_k.py`). The
194
+ cap passes up a 1.02× to 1.14× gain at 1,024 rows with d = 128.
195
+ - **FP8 encodes exactly like PyTorch.** Every FP32 value up to 448 in magnitude (2.28 billion)
196
+ gives the same E4M3 code through Triton's conversion as through
197
+ `tensor.to(torch.float8_e4m3fn)` (an opt-in test, `EMEANS_EXHAUSTIVE_TESTS=1`; the default run
198
+ samples), and so do the scaled and affine encodes through a quantizer.
199
+ - **Graph replay brings no speed-up on this card** (0.94× and 1.00×), so training runs eagerly
200
+ by default on ROCm; replay is verified and available with `EMEANS_ROCM_GRAPH_REPLAY=1`.
201
+ - **Pitfalls for AMD code in general:** on ROCm `torch.cuda.get_device_capability()` returns gfx
202
+ numbers ((12, 0) here), so a bare `>= (8, 0)` check silently matches AMD;
203
+ `multi_processor_count` is 16 here because RDNA counts work-group processors (two CUs each);
204
+ and `HIP_VISIBLE_DEVICES=-1` aborts ROCm 10.1 (use `ROCR_VISIBLE_DEVICES=` to hide the GPU).
205
+
206
+ ## What this fork changes
207
+
208
+ Upstream e-means does not use its GPU kernels on AMD. Automatic selection treats ROCm as having
209
+ no fast backend, so by default only native FP32 through plain PyTorch works, and the default
210
+ FP32 → BF16 conversion makes `Emeans(d, k).train(x)` raise an error; native FP32 needs
211
+ `QuantizedEmeans(d, k, quantizer=None)`. An explicit `assignment_backend="triton"` is not
212
+ refused, but upstream's NVIDIA inline assembly does not run correctly on AMD. Most GPU tests
213
+ (Triton, FP16/BF16, INT8, FP8) are skipped on ROCm.
214
+
215
+ | Area | Change |
216
+ |---|---|
217
+ | Kernels | The 10 PTX inline-assembly snippets in the EMA-update, split and FP8-cast kernels are replaced by plain Triton arithmetic. With fusion off, on NVIDIA (sm90) every float multiply, add and subtract compiles to an explicitly rounded `.rn` instruction as the assembly did, with an FMA only in the affine FP8 cast, where the assembly had one; halving now multiplies by 0.5 instead of dividing by 2 (the same value for every input). On the four AMD families it compiles without FMA contraction. |
218
+ | Device detection | New `device_profile.py`: tells NVIDIA from AMD (ROCm reports both as `"cuda"`) and maps AMD chips to a family with its wavefront size, LDS budget and matrix-instruction support. |
219
+ | Backend selection | `assignment_backend="auto"` picks Triton for FP32, FP16 and BF16 on the four families, so the default BF16 path works. Without Triton, auto falls back to PyTorch there (FP16/BF16 through `torch.addmm(out_dtype=)`, as explicit `native` already could upstream). The default FP32 → BF16 conversion is limited to the four families; other AMD GPUs keep FP32. CuTe and TMA descriptors are never selected on AMD. |
220
+ | INT8 (v0.2) | INT8/UINT8 assignment on the four families' integer matrix instructions, with a per-family pipeline depth that fits in 64 KiB of LDS (upstream's three stages need up to 112 KiB outside gfx942). |
221
+ | FP8 (v0.2) | FP8 (E4M3) assignment on the families with a native E4M3 matrix instruction, gfx950 and gfx120x. The scaled split's NVIDIA-only `libdevice.ldexp` is replaced on AMD by an exact power-of-two multiply (bit-identical to `torch.ldexp`). gfx942 and gfx11xx refuse FP8 with an error. |
222
+ | Tuning | Per-family settings in `triton_assignment.py`, tuned on RDNA4: `_AMD_DIRECT_CONFIGS` (FP32, FP16/BF16, FP8), `_AMD_INT8_CONFIGS` with `_AMD_INT8_SMALL_CONFIGS` (batches up to 4,096 rows; 2,048 at d = 256), and `_AMD_SPLIT_K_MAX_ROW_BLOCKS`. gfx950 gets a cap to fit 64 KiB of LDS (`_AMD_DIRECT_CONFIGS["cdna4"]`, `_AMD_SPLIT_K_STAGES`). Other families keep the NVIDIA-fitted settings; `_direct_config` is the one place the launcher's choice is made. Gains are under Results. |
223
+ | Pre-flight checks (v0.2.2) | New `capabilities.py`: `supported(device, dtype, ...)` names the backend training would use, or `None`; `memory_estimate(model, n, dtype, device)` bounds the GPU memory of `train`. New `check_finite=True` on `Emeans`/`QuantizedEmeans` raises on NaN or infinite input. Upstream has none of these. |
224
+ | Graph replay | Training uses the eager loop on ROCm (same results, only speed differs); `EMEANS_ROCM_GRAPH_REPLAY=1` opts in to replay. |
225
+ | Tests | Vendor-aware skips replace the ROCm skips; INT8 and FP8 tests run on AMD. New GPU-free tests: no inline assembly and `.rn` arithmetic (four AMD targets plus sm90), NVIDIA-only features fenced off, LDS fit of every launch configuration on four AMD targets, pinned launch configurations (NVIDIA unchanged), rule boundaries. New GPU tests: bit-level checks against PyTorch, the FP8 encodes included. One upstream IVF test made robust to OpenBLAS builds. |
226
+
227
+ Supported data per GPU and the converters are in the kernel table of the
228
+ [e-means README](https://github.com/ogaude/e-means-rocm/blob/main/contrib/e_means/README.md#kernels).
229
+
230
+ ## Known limitations
231
+
232
+ - **Only RX 9000 (gfx120x) has been run.** MI300 (gfx942), MI350 (gfx950) and RX 7000 (gfx11xx)
233
+ compile and are selected automatically, but are not validated and not tuned. MI300X is
234
+ planned for v0.3.
235
+ - **gfx950 (MI350) is capped to fit 64 KiB of LDS, to be tested on hardware.** Its 128-row
236
+ BF16/FP16 tile at d > 128 and two-stage split-K compile to 96 KiB on gfx950, more than the
237
+ 64 KiB the device profile assumes until MI350's limit is measured. So gfx950 uses 64-row tiles
238
+ and one split-K stage. **To test on an MI350:** read its LDS limit (`tools/device_facts.py`),
239
+ and if it is larger, raise `lds_bytes` for cdna4, drop the cap and time both.
240
+ - **FP8 runs only where E4M3 is native** (gfx120x, gfx950). MI300 and RX 7000 refuse it;
241
+ emulated FP8 there is a possible v0.4 item.
242
+ - **NVIDIA has not been re-run on hardware.** "NVIDIA results unchanged" rests on mocked-device
243
+ tests (backend selection, launch configurations) and on sm90 compiles of the rewritten
244
+ kernels; the bit-level numerics tests have only run on AMD.
245
+ - **Several GPUs** (`DistributedEmeans`, sharded centroids) are expected to work on AMD but are
246
+ untested until v0.5; their tests stay skipped on ROCm.
247
+ - **Training is not bit-reproducible by default**, on AMD as upstream on NVIDIA: batch
248
+ statistics are summed with atomic additions, so two identical FP32 runs can end in different
249
+ clusterings. `torch.use_deterministic_algorithms(True)` makes runs identical, eager and with
250
+ graph replay.
251
+
252
+ ## Development
253
+
254
+ Recorded development stack: ROCm 10.1.0, Python 3.14, torch 2.14.0+rocm10.1.0,
255
+ triton 3.8.0+git669b31ac.rocm10.1.0, faiss-cpu 1.16.0, on an RX 9060 XT (gfx1200, 32 CUs, wave32, 64 KiB
256
+ LDS per workgroup, 16 GB).
257
+
258
+ ### Running the tests
259
+
260
+ The tests need `python -m pip install pytest`.
261
+
262
+ ```bash
263
+ cd tests
264
+ # Everything except the GPU tests, without touching the GPU:
265
+ ROCR_VISIBLE_DEVICES= HIP_VISIBLE_DEVICES= python -m pytest -q $(ls *e_means*.py | grep -v -e utils -e conditions)
266
+ # Full run on a GPU (add EMEANS_ROCM_GRAPH_REPLAY=1 to include the graph-replay tests):
267
+ python -m pytest -q $(ls *e_means*.py | grep -v -e utils -e conditions)
268
+ # The same tests against the installed emeans_rocm package (no symlink needed):
269
+ PYTHONPATH=../tools python -m pytest -q -p pkg_alias $(ls *e_means*.py | grep -v -e utils -e conditions)
270
+ ```
271
+
272
+ The first two commands need the development setup (see Quick start). The third uses the
273
+ `tools/pkg_alias.py` plugin, which maps `faiss.contrib.e_means` imports onto `emeans_rocm`; it
274
+ skips the three tests that check the faiss layout itself.
275
+
276
+ - Hide the GPU with `ROCR_VISIBLE_DEVICES=` (empty). `HIP_VISIBLE_DEVICES=-1` makes ROCm 10.1
277
+ abort at the first `torch.cuda.is_available()`.
278
+ - GPU-free checks worth knowing: `torch_test_e_means_ptx_free.py` (no inline assembly, no FMA
279
+ contraction on four AMD targets plus sm90), `torch_test_e_means_amd_fences.py` (NVIDIA-only
280
+ features stay off on AMD; RDNA4 table shape and LDS fit), `torch_test_e_means_int8_compile.py`
281
+ (INT8 LDS fit on four AMD targets), `torch_test_e_means_launch_config.py` (LDS fit of every
282
+ launch configuration on four AMD targets, pinned configurations, rule boundaries),
283
+ `torch_test_e_means_backend_selection.py` and `torch_test_e_means_device_profile.py` (mocked
284
+ devices).
285
+ - On the GPU, `torch_test_e_means_triton_numerics.py` checks the rewritten kernels bit for bit
286
+ against PyTorch and correctly rounded references, and `torch_test_e_means_fp8_amd.py` checks
287
+ the FP8 encodes (plain, scaled, affine) and the `ldexp` replacement against PyTorch;
288
+ `EMEANS_EXHAUSTIVE_TESTS=1` adds the encode of every FP32 value up to 448.
289
+ - `torch_test_e_means_ivf.py::TestIVFFlat::test_all_probes_split_tiles_empty_lists_and_ties`
290
+ failed by one rounding step on this torch build, also on the unmodified upstream snapshot:
291
+ the ROCm wheels use OpenBLAS, whose FP32 CPU matmul results depend on the matrix shape, so the
292
+ index's batched products and the test's single product could differ in the last bit. The
293
+ test now requires identical ids and scores within FP32 rounding of the represented operands;
294
+ on the test's data, unrepresented (FP32) operands miss that bound by about 100 times.
295
+
296
+ ### Tools
297
+
298
+ ```bash
299
+ # Smoke matrix: every precision x backend, each case in its own process with a timeout
300
+ python tools/w0_smoke.py --rows 20000 --k 256 # small, safe on a shared GPU
301
+ python tools/w0_smoke.py # 200k x 64, k = 1024
302
+ EMEANS_ROCM_GRAPH_REPLAY=1 python tools/w0_smoke.py # same with graph replay
303
+
304
+ # Training benchmark (the Results tables); add --no-tables for the NVIDIA-fitted settings
305
+ python tools/bench_train.py --shapes 1000000x128x4096 --precisions bf16 fp16 fp32 int8 fp8 --replay 0
306
+
307
+ # Assignment-kernel tuning: sweep tiles, warps and stages, each candidate checked against the
308
+ # NVIDIA-fitted configuration's results; --compare times the current table against it
309
+ python tools/tune_assign.py --quick --k 1024 16384 # BF16/FP16/FP32 sweep, about two hours
310
+ python tools/tune_assign.py --dtypes int8 --quick # INT8 sweep, about 45 minutes
311
+ python tools/tune_assign.py --dtypes fp8 --quick --d 64 128 256 768 # FP8 (d % 64 == 0)
312
+ python tools/tune_assign.py --compare # add --rows 2048 for small batches
313
+ python tools/tune_split_k.py # direct vs split-K on small row counts
314
+ ```
315
+
316
+ Sweep times are for gfx1200. `tools/rocm_probe/` holds the offline compile probes (no GPU
317
+ needed) used to verify the kernel rewrite; `tools/gpu_session/` runs the whole validation and
318
+ tuning sequence on a fresh cloud GPU; see their READMEs.
319
+
320
+ ## Roadmap
321
+
322
+ The work runs on two tracks. The **GPU track** adds and tunes hardware; each of its versions
323
+ needs the hardware it names. The **integration track** ships what callers of the library need,
324
+ as patch releases of the current version, and needs no new hardware. Since v0.2.1 only the
325
+ integration track has moved: v0.3 is waiting for MI300X access.
326
+
327
+ **GPU track**
328
+
329
+ | Version | Scope | Hardware | Status |
330
+ |---|---|---|---|
331
+ | **v0.1** | Triton as the default AMD path for FP32, FP16 and BF16; validated and tuned on RDNA4 | RX 9060 XT | **released** 2026-11-19 |
332
+ | **v0.2** | INT8 on all four families; native FP8 on gfx120x and gfx950; RDNA4 tuning for both, small-batch settings and the split-K choice; the cloud session tooling | RX 9060 XT | **released** 2026-11-20; v0.2.1 (review fixes) the same day |
333
+ | v0.3 | MI300X (gfx942) validation and tuning; measure whether a hand-written AMD kernel (the CuTe counterpart) is worth building | MI300X (cloud) | planned; scripts ready; **waiting for cloud access** |
334
+ | v0.4 | Optionally emulated FP8 on gfx942 and gfx11xx; the hand-written kernel if v0.3's measurement calls for it | RX 9060 XT and MI300X | planned |
335
+ | v0.5 | Several GPUs (`DistributedEmeans`, sharded centroids) | 8× MI300X (cloud) | planned |
336
+
337
+ The MI300X work was first planned for v0.2; it moved to v0.3 (2026-11-20) so the RX 9000 work
338
+ could be released without waiting for cloud access, and the later versions moved up by one.
339
+
340
+ **Integration track**
341
+
342
+ These came from a downstream integration of e-means, which had to work around missing checks
343
+ and the way the fork was installed.
344
+
345
+ | Release | Scope | Status |
346
+ |---|---|---|
347
+ | **v0.2.2** | Pre-flight checks: `supported()`, `memory_estimate()`, `check_finite=True` | **released** 2026-11-22 |
348
+ | **v0.2.3** | `Emeans` refuses INT8/UINT8 rows on the GPU with an error naming `QuantizedEmeans` | **released** 2026-11-22 |
349
+ | **v0.2.4** | Installable package `emeans-rocm` (import name `emeans_rocm`) on PyPI; users no longer need the symlink into faiss-cpu (development still uses it) | **released** 2026-11-23 |
350
+
351
+ Not scheduled on either track:
352
+
353
+ - **Upstream.** Whether faiss would take the ROCm changes into `contrib/e_means` has not been
354
+ asked yet. The fork keeps faiss's layout so its changes still apply there
355
+ ([Relation to upstream faiss](https://github.com/ogaude/e-means-rocm#relation-to-upstream-faiss)); if they were merged, the package
356
+ could shrink or be retired.
357
+ - **NVIDIA hardware run.** NVIDIA paths rest on mocked selection tests and sm90 compiles (see
358
+ Known limitations); a run needs access to an NVIDIA GPU.
359
+
360
+ **v0.3 plan (needs an MI300X)**
361
+
362
+ | Item | Plan |
363
+ |---|---|
364
+ | gfx942 validation | smoke matrix, test suite (eager and replay), INT8 there |
365
+ | gfx942 tuning | `cdna3` entries for the BF16, INT8 and split-K tables; graph-replay default decided on measured speed |
366
+ | Hand-written backend gate | Triton BF16 assignment against rocBLAS/hipBLASLt GEMM (`tools/gemm_gate.py`); build a FlyDSL backend (v0.4) only if Triton reaches less than about 80% of the GEMM's speed |
367
+ | MI350 (gfx950), if the cloud offers one | measure its LDS limit and lift or keep the 64 KiB cap (see Known limitations) |
368
+
369
+ ### Release history
370
+
371
+ **v0.2.4 (2026-11-23, tag `e-means-rocm-v0.2.4`)**: the fork installs as a package,
372
+ `pip install emeans-rocm`, imported as `emeans_rocm`. Before, using it meant replacing the
373
+ `faiss/contrib/e_means` folder inside an installed faiss-cpu, and a reinstall of faiss-cpu
374
+ through that symlink could delete files in the clone; the development setup still works that way. `pyproject.toml` builds the wheel straight
375
+ from `contrib/e_means`, so the source keeps faiss's layout; `tools/pkg_alias.py` runs the
376
+ unchanged tests against the installed package. No change to the e-means code. 285 tests passed
377
+ eager, 289 with graph replay, none failing; against the package (eager), 282 passed and the 3
378
+ that check the faiss layout were skipped, besides the usual hardware skips.
379
+
380
+ **v0.2.3 (2026-11-22, tag `e-means-rocm-v0.2.3`)**: `Emeans` refuses INT8/UINT8 rows on the GPU at
381
+ the first `train`, `accumulate` or `assign` call, naming `QuantizedEmeans`, instead of failing
382
+ inside the Triton launcher with a dtype-mismatch error. `QuantizedEmeans` and the CPU path are
383
+ unchanged. 285 tests passed eager, 289 with graph replay, none failing.
384
+
385
+ **v0.2.2 (2026-11-22, tag `e-means-rocm-v0.2.2`)**: checks a caller can run before training,
386
+ asked for by an integration that had to probe with a tiny training run. `supported(device,
387
+ dtype, ...)` names the backend training would use, or `None` when it would be refused, from
388
+ device facts and installed software (it matched real training in all 96 format × mode ×
389
+ backend cases on gfx1200 and the CPU). `memory_estimate(model, n, dtype, device)` bounds the GPU
390
+ memory of `train` (1.07× to 1.33× the measured peak on gfx1200, never below). `check_finite=True`
391
+ on `Emeans`/`QuantizedEmeans` raises on NaN or infinite input. New test that deterministic mode
392
+ gives bit-identical training runs. 284 tests passed eager, 288 with graph replay, none failing.
393
+
394
+ **v0.2.1 (2026-11-20, tag `e-means-rocm-v0.2.1`)**: fixes from an independent review. gfx950
395
+ (MI350) capped to fit 64 KiB of LDS (its BF16/FP16 128-row tile and two-stage split-K needed
396
+ 96 KiB; to be tested on hardware); a GPU-free test of every launch configuration on all four
397
+ AMD families; sm90 compiles of the split and FP8-cast kernels; FP8 quantized-encode parity and
398
+ an opt-in exhaustive encode test; a wrong test skip condition fixed; `bench_train.py
399
+ --no-tables`; documentation matched to code and tests. 269 tests passed eager, 273 with graph
400
+ replay, none failing.
401
+
402
+ **v0.2 (2026-11-20, tag `e-means-rocm-v0.2`)**, all on RX 9000 (gfx1200):
403
+
404
+ | Item | Result |
405
+ |---|---|
406
+ | INT8/UINT8 on the four AMD families | runs on gfx1200, distances exact against a float64 reference; LDS fit compile-checked on the other three |
407
+ | FP8 (E4M3) on gfx120x and gfx950 | runs on gfx1200; encode identical to PyTorch for all FP32 values up to 448; `libdevice.ldexp` replaced by an exact power-of-two multiply |
408
+ | RDNA4 tuning | INT8 and FP8 tables; BF16/FP16 d ≤ 64 re-chosen for small batches; split-K capped at 512 rows |
409
+ | Tests | 253 passed eager, 257 with graph replay, none failing |
410
+ | Cloud session tooling | `tools/gpu_session/`, `tools/gemm_gate.py`, `tools/bench_train.py`, `tools/tune_split_k.py`, `tools/device_facts.py`, INT8/FP8 modes in `tools/tune_assign.py` |
411
+
412
+ **v0.1 (2026-11-19, tag `e-means-rocm-v0.1`)**: PTX inline assembly replaced by plain Triton,
413
+ device profile and backend selection, NVIDIA-only Triton features fenced off on AMD, eager
414
+ training by default on ROCm, native FP16/BF16 PyTorch fallback, vendor-aware tests, RDNA4
415
+ tuning table. Acceptance on gfx1200, as measured then:
416
+
417
+ | Criterion | Result |
418
+ |---|---|
419
+ | Default `Emeans(d, k).train(x)` uses Triton | met (`recipe["assignment_backend"] == "triton"`, BF16) |
420
+ | Final loss within noise of the PyTorch backend | met (BF16 objectives agree to about 1e-10 relative) |
421
+ | EMA update and split bit-equal to PyTorch | met (`torch_test_e_means_triton_numerics.py`) |
422
+ | Triton faster than PyTorch for BF16 | met (2.1× to 4.8× at the sizes measured then; 9.3× at 4M × 128 in v0.2) |
423
+ | NVIDIA behaviour unchanged | **partly**: selection checked with mocked devices, kernels compiled for sm90 and checked; no run on NVIDIA hardware |
424
+
425
+ ## Repository layout
426
+
427
+ ```
428
+ pyproject.toml builds contrib/e_means as the package emeans-rocm (import name emeans_rocm)
429
+ contrib/e_means/ e-means source (upstream + ROCm changes); its README is the user guide
430
+ benchs/e_means/ upstream benchmarks and demos
431
+ tests/ e-means tests (upstream + new ROCm tests)
432
+ tools/rocm_probe/ offline Triton compile probes for AMD targets
433
+ tools/w0_smoke.py precision x backend smoke matrix
434
+ tools/bench_train.py warm train() benchmark: formats, Triton vs PyTorch, eager vs graph replay
435
+ tools/tune_assign.py assignment-kernel tuning sweep and upstream-vs-table comparison
436
+ tools/tune_split_k.py direct vs split-K assignment on small row grids
437
+ tools/gemm_gate.py Triton BF16 assignment vs a bare BLAS GEMM (hand-written backend gate)
438
+ tools/device_facts.py GPU and stack facts as JSON
439
+ tools/pkg_alias.py pytest plugin: run tests/ against the installed emeans_rocm package
440
+ tools/gpu_session/ setup + one-shot session runner for a cloud GPU VM (see its README)
441
+ ```
442
+
443
+ ## Relation to upstream faiss
444
+
445
+ This repository starts from [facebookresearch/faiss](https://github.com/facebookresearch/faiss)
446
+ `main` at commit `14392c952788ca1d6980361cf01d1c8736afc4db` (2026-11-18). The first commit is an
447
+ unmodified snapshot of `contrib/e_means`, `benchs/e_means` and the e-means tests, so
448
+ `git diff d6e4215 -- contrib benchs tests` shows exactly what this fork changes in faiss's
449
+ files. Those paths mirror faiss, so changes to them apply to a faiss checkout
450
+ (`git format-patch d6e4215 -- contrib benchs tests`, then `git am`); `README.md`, `pyproject.toml`,
451
+ `tools/` and `.gitignore` belong to this fork only. To see what upstream changed since the snapshot:
452
+
453
+ ```bash
454
+ git clone --depth 1 --filter=blob:none --sparse https://github.com/facebookresearch/faiss.git /tmp/faiss
455
+ git -C /tmp/faiss sparse-checkout set contrib/e_means benchs/e_means tests
456
+ mkdir -p /tmp/snapshot && git archive d6e4215 contrib/e_means | tar -x -C /tmp/snapshot
457
+ diff -r /tmp/snapshot/contrib/e_means /tmp/faiss/contrib/e_means
458
+ ```
459
+
460
+ ## License
461
+
462
+ MIT, as faiss; see [LICENSE](https://github.com/ogaude/e-means-rocm/blob/main/LICENSE). The e-means code is by Meta Platforms, Inc. and
463
+ affiliates; the ROCm changes in this repository are under the same license.