Commit 61750ac
perf: reuse sentence embeddings for chunk vectors
On an Oracle Free Tier 4-vCPU ARM box with no GPU, bge-m3 encode is the
single biggest cost of attachment upload. Measured in-container on a
synthetic 7KB, 150-sentence text: semantic-chunking + chunk-storage needed
two separate embed_texts passes (~8.5s each) = ~17s wall time total, which
dominated the user-visible extract latency.
Fold the per-chunk embed into the sentence-level pass:
- New chunk_and_embed() does the same sentence-boundary detection as
chunk_text_semantic, but returns the resulting (chunks, embeddings) pair.
Per-chunk embeddings are the L2-renormalized sum of their constituent
sentence vectors. Since bge-m3 sentence embeddings are already unit-norm,
this is equivalent to mean-pool + renormalize.
- process_attachment uses chunk_and_embed instead of chunk_text_semantic +
a second embed_texts(chunks) call.
- chunk_text_semantic stays as a thin text-only wrapper (kept for other
call sites and test compat).
- Fallback path: when the single sentence-embed pass fails, fall back to
_chunk_fixed and re-embed the fixed chunks once (same cost shape as the
old double-pass, acceptable for the error path only).
Also set TOKENIZERS_PARALLELISM=true in the backend compose env so the HF
tokenizer can fork during encode's tokenize step (tiny speedup, zero risk —
we don't fork post-import).
Expected staging wall-time: extract ~17s → ~9s, dominated by the now-single
sentence embed pass. Will verify on staging.
Tests
- Updated process_attachment tests to mock chunk_and_embed.
- Updated chunk_text_semantic fallback test (fallback path now re-embeds).
- Added TestChunkAndEmbed covering: aligned (chunks, embeddings) lengths,
unit-norm of pooled chunk vectors, re-embed fallback on failure,
empty-input returns ([], []).
250 pytest passed (was 246 + 4 new).
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>1 parent 4912ebc commit 61750ac
3 files changed
Lines changed: 200 additions & 60 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
| |||
217 | 218 | | |
218 | 219 | | |
219 | 220 | | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
220 | 288 | | |
221 | 289 | | |
222 | | - | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
223 | 295 | | |
224 | | - | |
225 | | - | |
226 | | - | |
227 | | - | |
228 | | - | |
229 | 296 | | |
230 | | - | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
231 | 311 | | |
232 | 312 | | |
233 | 313 | | |
234 | 314 | | |
235 | 315 | | |
236 | | - | |
| 316 | + | |
237 | 317 | | |
238 | | - | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
239 | 323 | | |
240 | 324 | | |
241 | | - | |
242 | | - | |
243 | | - | |
244 | | - | |
245 | | - | |
246 | | - | |
247 | | - | |
248 | | - | |
249 | | - | |
250 | | - | |
251 | | - | |
252 | | - | |
253 | | - | |
254 | | - | |
255 | | - | |
256 | | - | |
257 | | - | |
258 | | - | |
259 | | - | |
260 | | - | |
261 | | - | |
262 | | - | |
263 | | - | |
264 | | - | |
265 | | - | |
266 | | - | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
271 | | - | |
272 | | - | |
273 | | - | |
274 | | - | |
275 | | - | |
276 | | - | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
277 | 338 | | |
278 | 339 | | |
279 | | - | |
280 | | - | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
281 | 346 | | |
282 | 347 | | |
283 | 348 | | |
| |||
372 | 437 | | |
373 | 438 | | |
374 | 439 | | |
375 | | - | |
376 | | - | |
| 440 | + | |
| 441 | + | |
377 | 442 | | |
378 | 443 | | |
379 | 444 | | |
380 | | - | |
381 | | - | |
382 | | - | |
383 | 445 | | |
384 | 446 | | |
385 | 447 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
121 | | - | |
122 | | - | |
| 121 | + | |
| 122 | + | |
123 | 123 | | |
124 | | - | |
125 | 124 | | |
126 | 125 | | |
127 | | - | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
128 | 137 | | |
129 | 138 | | |
130 | 139 | | |
131 | 140 | | |
132 | 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 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
133 | 207 | | |
134 | 208 | | |
135 | 209 | | |
| |||
355 | 429 | | |
356 | 430 | | |
357 | 431 | | |
358 | | - | |
359 | | - | |
360 | | - | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
361 | 435 | | |
362 | 436 | | |
363 | 437 | | |
| |||
371 | 445 | | |
372 | 446 | | |
373 | 447 | | |
374 | | - | |
| 448 | + | |
| 449 | + | |
375 | 450 | | |
376 | 451 | | |
377 | 452 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
10 | 13 | | |
11 | 14 | | |
12 | 15 | | |
| |||
0 commit comments