Commit 69e42da
authored
fix(cli/scoring): sort speaker ids so diarization speaker mapping is deterministic (#923)
## Summary
Fixes #922 — same nondeterminism class as #911/#921, in the diarization
scorer.
`computeSpeakerMapping` built its speaker index orders with
`Array(predicted.keys)` / `Array(groundTruth.keys)`. Swift dictionary
iteration order is per-instance random (the hash seed incorporates the
storage allocation — re-confirmed empirically on Swift 6.2.3), and both
assignment paths (the subset DP and the greedy fallback) tie-break by
first-encountered index via strict `>` comparisons. The confusion matrix
rounds overlaps to integer milliseconds, so exact ties are realistic —
and whenever two pairings tie, the winning speaker mapping (and JER)
depended on random key enumeration order.
## Evidence
Standalone harness on the scenario in the new test (two predicted
speakers each overlapping one ground-truth speaker by exactly 4.75 s
after the 0.25 s collar): the pre-fix code produced **2 distinct speaker
mappings within a single 200-call process** (3/3 runs), and fails the
new test's predicate in **20/20 processes**. With the fix: 1 mapping,
always (`right → spk_a`, pinned in the test).
## Fixes (commit 2 adds the full sweep from adversarial review)
1. `computeSpeakerMapping`: sort both key arrays — ties resolve by the
solver's fixed index-order rule over lexicographically sorted speaker
ids; unambiguous assignments unchanged.
2. `offlineMetrics`: the `correctlyAssigned`, `jaccardScores`, and
unmapped-pred loops accumulated Doubles in dictionary order. Addition is
not associative, and both DER and JER pass through near-zero
cancellation (`max(0, overlapSpeech - correctlyAssigned)`, `1.0 -
averageJaccard`) where a ulp-level reorder survives the Float cast —
with 3+ speakers, DER/JER were still not bit-deterministic. All three
loops now iterate in sorted key order.
3. Streaming path (`DiarizationBenchmark.calculateStreamingMetrics`):
`overlapsByGtSpeaker.max(by: { $0.value < $1.value })` kept the
first-encountered element among tied overlaps in random dictionary order
— same class. Ties now break by smaller speaker id.
## Tests
`DiarizationSpeakerMappingTests`: tied-overlap stability across 50 calls
(each call regroups segments into fresh dictionaries, each with an
independent per-allocation key order — the unfixed code fails this 20/20
processes), the exact tied winner pinned so silent tie-break changes
fail loudly, plus an unambiguous-mapping correctness check.1 parent 74ad822 commit 69e42da
3 files changed
Lines changed: 138 additions & 7 deletions
File tree
- Sources/FluidAudioCLI
- Commands
- Utils
- Tests/FluidAudioTests/CLI
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
939 | 939 | | |
940 | 940 | | |
941 | 941 | | |
| 942 | + | |
| 943 | + | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
942 | 954 | | |
943 | 955 | | |
944 | 956 | | |
| |||
985 | 997 | | |
986 | 998 | | |
987 | 999 | | |
988 | | - | |
| 1000 | + | |
989 | 1001 | | |
990 | 1002 | | |
991 | 1003 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
186 | 186 | | |
187 | 187 | | |
188 | 188 | | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
189 | 192 | | |
190 | | - | |
| 193 | + | |
191 | 194 | | |
192 | 195 | | |
193 | 196 | | |
| |||
205 | 208 | | |
206 | 209 | | |
207 | 210 | | |
208 | | - | |
| 211 | + | |
209 | 212 | | |
210 | 213 | | |
211 | 214 | | |
| |||
215 | 218 | | |
216 | 219 | | |
217 | 220 | | |
218 | | - | |
| 221 | + | |
| 222 | + | |
219 | 223 | | |
220 | 224 | | |
221 | 225 | | |
| |||
230 | 234 | | |
231 | 235 | | |
232 | 236 | | |
233 | | - | |
| 237 | + | |
234 | 238 | | |
235 | 239 | | |
236 | 240 | | |
| |||
593 | 597 | | |
594 | 598 | | |
595 | 599 | | |
596 | | - | |
597 | | - | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
598 | 605 | | |
599 | 606 | | |
600 | 607 | | |
| |||
Lines changed: 112 additions & 0 deletions
| 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 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
0 commit comments