Skip to content

perf(paddleocr-vl): batch vision attention segments - #700

Merged
inureyes merged 2 commits into
mainfrom
feature/issue-552-paddleocr-fast-paths
Jul 8, 2026
Merged

inureyes merged 2 commits into
mainfrom
feature/issue-552-paddleocr-fast-paths

Conversation

@inureyes

@inureyes inureyes commented Jul 8, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Add PaddleOCR-VL vision fast paths for packed dynamic-resolution segments: uniform segment batches reshape into one SDPA call and repeated variable-length batches are grouped by segment length while unique variable-length batches stay on the per-segment fallback to avoid dense block-mask memory regressions.
  • Cache repeated learned position-embedding interpolation grids within a vision pass so same-sized OCR pages do not rebuild identical interpolation graphs.
  • Extend PaddleOCR-VL coverage with deterministic allclose comparisons between the uniform/bucketed fast paths and the sequential packed-segment reference, plus shape coverage for repeated and unique variable-resolution batches.

Performance evidence

  • Local synthetic attention microbench on Apple GPU, debug test harness, 8 heads × 64 head dim, 16 packed segments alternating 128/256 tokens, 5 iterations: sequential per-segment path 16.864 ms, length-bucketed path 13.253 ms, 1.27x faster. This isolates the launch-reduction path without model load/tokenization noise; full PaddleOCR-VL smoke runs below validate the real checkpoint still executes.

Test plan

  • cargo test --lib vision::encoders::paddleocr_vl::tests::
  • cargo test --test paddleocr_vl_parity
  • cargo check --lib --tests
  • cargo clippy --lib --tests -- -D warnings
  • cargo test --lib vision::encoders::paddleocr_vl::tests::bench_bucketed_attention_fast_path -- --ignored --nocapture (temporary local benchmark harness; printed the 1.27x result above)
  • ./target/debug/mlxcel generate -m models/paddleocr-vl-bfloat16 --image /tmp/mlxcel-paddleocr-small.png --image /tmp/mlxcel-paddleocr-smoke.png -p "Read the text in both images." -n 1 --force
  • ./target/debug/mlxcel generate -m models/paddleocr-vl-bfloat16 --image /tmp/mlxcel-paddleocr-small.png --image /tmp/mlxcel-paddleocr-tall.png -p "Read the text in both images." -n 1 --force

Closes #552

Add PaddleOCR-VL vision fast paths for dynamic-resolution OCR batches: same-length segments reshape into one batched SDPA call, repeated variable-length segments are length-bucketed, and small unique variable-resolution batches use a bounded block-diagonal mask so they avoid per-segment SDPA loops without allocating giant masks for full-page cases.

Cache repeated learned-position interpolation grids across images in the same vision pass and document the new fast-path behavior in the supported-models table.

Validation: cargo test --lib vision::encoders::paddleocr_vl::tests::; cargo test --test paddleocr_vl_parity; cargo check --lib --tests; cargo clippy --lib --tests -- -D warnings; real-model smoke with mlx-community/paddleocr-vl-bfloat16 for one image and a two-image variable-resolution prompt.

Refs #552
@inureyes inureyes added type:enhancement New features, capabilities, or significant additions priority:low Low priority area:models Model architectures, weights, loading, metadata status:review Under review labels Jul 8, 2026
Tighten the PaddleOCR-VL fast-path validation after review by comparing uniform and length-bucketed attention outputs against the sequential packed-segment reference. Keep unique variable-resolution batches on the existing sequential fallback instead of using a dense block-mask path whose score work and memory can exceed the per-segment route.

Validation: cargo test --lib vision::encoders::paddleocr_vl::tests::; cargo test --test paddleocr_vl_parity; cargo check --lib --tests; cargo clippy --lib --tests -- -D warnings; real-model smoke with mlx-community/paddleocr-vl-bfloat16 for repeated-grid and unique-grid two-image prompts.

Refs #552
@inureyes inureyes added status:done Completed and removed status:review Under review labels Jul 8, 2026
@inureyes
inureyes merged commit 9be5913 into main Jul 8, 2026
4 checks passed
@inureyes
inureyes deleted the feature/issue-552-paddleocr-fast-paths branch July 8, 2026 23:38
@inureyes inureyes self-assigned this Aug 31, 2026
be-free-snowboy pushed a commit to freestylejs/snowbrain-mlxcel that referenced this pull request Oct 27, 2026
## Summary
- Add PaddleOCR-VL vision fast paths for packed dynamic-resolution segments: uniform segment batches reshape into one SDPA call and repeated variable-length batches are grouped by segment length while unique variable-length batches stay on the per-segment fallback to avoid dense block-mask memory regressions.
- Cache repeated learned position-embedding interpolation grids within a vision pass so same-sized OCR pages do not rebuild identical interpolation graphs.
- Extend PaddleOCR-VL coverage with deterministic allclose comparisons between the uniform/bucketed fast paths and the sequential packed-segment reference, plus shape coverage for repeated and unique variable-resolution batches.

## Performance evidence
- Local synthetic attention microbench on Apple GPU, debug test harness, 8 heads × 64 head dim, 16 packed segments alternating 128/256 tokens, 5 iterations: sequential per-segment path 16.864 ms, length-bucketed path 13.253 ms, 1.27x faster. This isolates the launch-reduction path without model load/tokenization noise; full PaddleOCR-VL smoke runs below validate the real checkpoint still executes.

## Test plan
- `cargo test --lib vision::encoders::paddleocr_vl::tests::`
- `cargo test --test paddleocr_vl_parity`
- `cargo check --lib --tests`
- `cargo clippy --lib --tests -- -D warnings`
- `cargo test --lib vision::encoders::paddleocr_vl::tests::bench_bucketed_attention_fast_path -- --ignored --nocapture` (temporary local benchmark harness; printed the 1.27x result above)
- `./target/debug/mlxcel generate -m models/paddleocr-vl-bfloat16 --image /tmp/mlxcel-paddleocr-small.png --image /tmp/mlxcel-paddleocr-smoke.png -p "Read the text in both images." -n 1 --force`
- `./target/debug/mlxcel generate -m models/paddleocr-vl-bfloat16 --image /tmp/mlxcel-paddleocr-small.png --image /tmp/mlxcel-paddleocr-tall.png -p "Read the text in both images." -n 1 --force`

Closes lablup#552
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:models Model architectures, weights, loading, metadata priority:low Low priority status:done Completed type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(paddleocr-vl): vision-encoder fast paths

1 participant