Repository navigation
perf(paddleocr-vl): batch vision attention segments - #700
Merged
Merged
Conversation
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
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
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Performance evidence
Test plan
cargo test --lib vision::encoders::paddleocr_vl::tests::cargo test --test paddleocr_vl_paritycargo check --lib --testscargo clippy --lib --tests -- -D warningscargo 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 --forceCloses #552