Commit 44fcfdc
committed
counting: vectorize the subsampling paths, bit-identically
`subsample="prob"`/`"dirty"` were the last two configurations still on the
Python loop, on the stated grounds that they were already the fastest ones and
vectorizing them "would optimize the cheap case". That was measured and it was
wrong in the usual way: they were fastest per surviving pair and the *slowest
per call*, which is the number a user waits on. At window=5 on 720k tokens they
were the two slowest rows in the benchmark.
The real obstacle was the one the equivalence gate named: the two draw streams
interleave. Within a sentence `iterate_tokens` draws one random() per
subsample-eligible token, and only then, knowing which survived, one
randint(1, window) per survivor -- so neither stream can be drawn in a single
pass over the chunk. `_subsample_draws` walks that interleaving sentence by
sentence in Python and leaves only the emission to numpy, which is the right
split: the draws were the cheap half all along.
subsample="prob" 1.19s -> 0.39s (3.0x)
subsample="dirty" 1.39s -> 0.42s (3.3x)
both knobs random 1.04s -> 0.50s (2.1x)
Because the streams are reproduced rather than replaced, `subsample="prob"` is
now bit-identical to the frozen f68cc74 reference. The equivalence grid grows
from 48 exact cells to 96 -- all four windows x all four dynamic_window modes x
three subsample modes x both seeds -- and the statistical comparison for "prob"
is removed: bit-identity implies equality of every moment, so keeping it would
cost ten runs per cell to prove less. "dirty" keeps its statistical check (the
frozen reference predates the mode) and gains an exact one against our own loop.
Fixes a latent bug found by extending that fallback test to the randomized
modes: a chunk above MAX_VECTORIZED_EVENTS had already drawn from the RNG before
discovering it was too large, so the loop resumed from an advanced generator and
a corpus counted differently depending only on how it was chunked. count_texts
now snapshots and restores the RNG state around the attempt.
Also drops `is_vectorizable`, which now has no false case, and corrects the
benchmark's note that the "prob" rows' checksums are expected to move -- nothing
vectorized the RNG in the end, so every row is bit-reproducible.1 parent 3bc7f2c commit 44fcfdc
5 files changed
Lines changed: 403 additions & 249 deletions
File tree
- bench
- hyperhyper
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
| 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 | + | |
51 | 88 | | |
52 | 89 | | |
53 | 90 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
185 | 185 | | |
186 | 186 | | |
187 | 187 | | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
193 | 205 | | |
194 | 206 | | |
195 | 207 | | |
| |||
199 | 211 | | |
200 | 212 | | |
201 | 213 | | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
206 | 217 | | |
207 | 218 | | |
208 | 219 | | |
| |||
213 | 224 | | |
214 | 225 | | |
215 | 226 | | |
216 | | - | |
217 | | - | |
218 | | - | |
219 | | - | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
220 | 234 | | |
221 | 235 | | |
222 | 236 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
60 | | - | |
61 | | - | |
62 | | - | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
63 | 66 | | |
64 | 67 | | |
65 | 68 | | |
| |||
106 | 109 | | |
107 | 110 | | |
108 | 111 | | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
109 | 117 | | |
110 | 118 | | |
111 | 119 | | |
| |||
261 | 269 | | |
262 | 270 | | |
263 | 271 | | |
264 | | - | |
265 | | - | |
| 272 | + | |
| 273 | + | |
266 | 274 | | |
267 | 275 | | |
268 | 276 | | |
| |||
0 commit comments