Repository navigation
Commit 70e9727
Perf: single-pass remap_instance_id (unique + bincount + LUT gather) (#9009)
### Description
Replaces the per-instance loops in `remap_instance_id` (which scanned
the full volume once per instance id — twice with `by_size=True` — for
`O(instances * volume)` work) with a single
`torch.unique(return_inverse=True)` + `bincount` + LUT-gather pass,
computing the same relabeling in `O(volume)`. Same approach as #8978;
`by_size` tie-breaking (stable, ascending-id) and the int32 output dtype
are preserved exactly.
### Benchmark
Synthetic instance masks (random sparse ids, `by_size=True`), median of
repeated runs, `OMP_NUM_THREADS=8`.
**2D**
| Input (H×W, #inst) | CPU speedup | CUDA speedup |
| --- | --- | --- |
| 128×128, 40 | ~4.2× | ~8.2× |
| 256×256, 80 | ~10.8× | ~10.4× |
| 512×512, 150 | ~9.1× | ~17.9× |
| 512×512, 300 | ~18× | ~36× |
| 1024×1024, 150 | ~5.7× | ~16.6× |
| 1024×1024, 500 | ~18.7× | ~54.6× |
**3D**
| Input (D×H×W, #inst) | CPU speedup | CUDA speedup |
| --- | --- | --- |
| 64×64×64, 30 | ~2.4× | ~3.8× |
| 96×96×96, 50 | ~2.8× | ~5.8× |
| 128×128×128, 80 | ~3.6× | ~8.9× |
| 128×128×128, 200 | ~8.6× | ~21.6× |
| 160×160×160, 120 | ~5.8× | ~13× |
Numerics are bit-identical (`torch.equal`) at every size. The old
implementation was slowest on GPU (e.g. 2720 ms for 1024×1024 with 500
instances, vs 262 ms on CPU) because the per-instance masked scans
serialize; that inversion is gone (49.8 ms).
System: 12th Gen Intel Core i7-12800H (20 threads, `OMP_NUM_THREADS=8`);
NVIDIA RTX A1000 Laptop GPU (4 GB); Linux 6.8.0-124-generic x86_64;
Python 3.10.12; PyTorch 2.12.1+cu130.
### Types of changes
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [x] New tests added to cover the changes.
- [x] The metrics tests passed locally (490 passed, including new
parameterized cases for non-contiguous ids, size ties, no-background and
empty inputs, and randomized 2D/3D equivalence against the previous
implementation), along with `black`/`isort`/`ruff`/`mypy`.
---------
Signed-off-by: Soumya Snigdha Kundu <soumya_snigdha.kundu@kcl.ac.uk>
Signed-off-by: Soumya Snigdha Kundu <soumyawork15@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>1 parent b087a1b commit 70e9727
2 files changed
Lines changed: 107 additions & 15 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| |||
515 | 515 | | |
516 | 516 | | |
517 | 517 | | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
518 | 521 | | |
519 | | - | |
520 | | - | |
521 | | - | |
522 | | - | |
523 | | - | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
524 | 525 | | |
525 | 526 | | |
526 | | - | |
527 | | - | |
528 | | - | |
529 | | - | |
530 | | - | |
531 | | - | |
532 | | - | |
533 | | - | |
534 | | - | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
535 | 534 | | |
536 | 535 | | |
537 | 536 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
0 commit comments