Commit b259737
authored
feat: add batch size guardrails to prevent GPU memory exhaustion (#135)
* feat: add batch size guardrails to prevent GPU memory exhaustion
Implements two-stage batch size validation:
1. Hard maximum (512): Validates at CLI parse time to reject absurdly
large values immediately, preventing GPU memory exhaustion and timeouts.
2. Per-file adjustment: At runtime, automatically adjusts batch size down
to the estimated segment count for short audio files, preventing
unnecessary memory allocation and padding.
Changes:
- Add MAX_BATCH_SIZE constant (512) in src/constants.rs
- Update parse_batch_size() validator to enforce maximum
- Add per-file batch size adjustment in process_file()
- Adjustment happens before create_batch_context() to save GPU memory
- Log adjustments at DEBUG level to avoid spam with large file sets
- Add comprehensive tests for new validation behavior
Fixes issue where users could specify batch sizes like 2560 that
caused GPU hangs and process termination.
* fix: address code review findings
Fixes identified by code review (Claude + Gemini):
1. Critical: Prevent effective_batch_size from becoming 0
- Empty or corrupt files with duration_hint=0 could set batch_size to 0
- This would trigger process_batch for every chunk (batch size 1)
- Now keeps original batch_size when estimated_segments is 0
2. Low: Add trim() for whitespace handling in parse_batch_size
- Config files/env vars may include leading/trailing whitespace
- Now accepts inputs like " 32 " or " 64 "
- Added test case to verify whitespace handling
All 230 tests pass.
* fix: address Claude code review feedback
Fixed items 1-3 from Claude's review:
1. Channel capacity now uses effective_batch_size instead of batch_size
- Ensures memory optimization is consistent for short files
- Channel buffer size now matches adjusted batch allocation
2. Added comment clarifying cast_possible_truncation scope
- Documents the truncation happens in closure (u64 -> usize)
- Notes it's safe in practice (would need 408 years of audio)
3. Tests now use MAX_BATCH_SIZE constant in assertions
- If MAX_BATCH_SIZE changes, tests will catch mismatches
- More robust than hardcoded "512" strings
Items 4-6 deferred:
- Item 4: .trim() consistency across validators (needs architectural decision)
- Item 5: Extract adjustment logic (quality improvement, not blocking)
- Item 6: Pre-existing code, not introduced by this PR1 parent e9b1697 commit b259737
3 files changed
+115
-35
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
| 6 | + | |
5 | 7 | | |
6 | 8 | | |
7 | 9 | | |
| |||
49 | 51 | | |
50 | 52 | | |
51 | 53 | | |
52 | | - | |
| 54 | + | |
53 | 55 | | |
54 | 56 | | |
| 57 | + | |
55 | 58 | | |
56 | 59 | | |
57 | 60 | | |
58 | 61 | | |
59 | 62 | | |
60 | 63 | | |
61 | 64 | | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
62 | 73 | | |
63 | 74 | | |
64 | 75 | | |
| |||
124 | 135 | | |
125 | 136 | | |
126 | 137 | | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 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 | + | |
127 | 174 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
23 | 34 | | |
24 | 35 | | |
25 | 36 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
435 | 435 | | |
436 | 436 | | |
437 | 437 | | |
438 | | - | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
439 | 487 | | |
440 | | - | |
441 | | - | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
442 | 491 | | |
443 | 492 | | |
444 | 493 | | |
445 | | - | |
| 494 | + | |
446 | 495 | | |
447 | 496 | | |
448 | 497 | | |
| |||
460 | 509 | | |
461 | 510 | | |
462 | 511 | | |
463 | | - | |
464 | | - | |
465 | | - | |
466 | | - | |
467 | | - | |
468 | | - | |
469 | | - | |
470 | | - | |
471 | | - | |
472 | | - | |
473 | | - | |
474 | | - | |
475 | | - | |
476 | | - | |
477 | | - | |
478 | | - | |
479 | | - | |
480 | | - | |
481 | | - | |
482 | | - | |
483 | | - | |
484 | | - | |
485 | | - | |
486 | | - | |
487 | | - | |
488 | | - | |
489 | | - | |
490 | | - | |
491 | 512 | | |
492 | 513 | | |
493 | 514 | | |
| |||
522 | 543 | | |
523 | 544 | | |
524 | 545 | | |
525 | | - | |
| 546 | + | |
| 547 | + | |
526 | 548 | | |
527 | 549 | | |
528 | 550 | | |
| |||
546 | 568 | | |
547 | 569 | | |
548 | 570 | | |
549 | | - | |
| 571 | + | |
550 | 572 | | |
551 | 573 | | |
552 | 574 | | |
| |||
0 commit comments