Skip to content

Reuse Qwen3-VL vision rotary tables without changing CUDA rounding - #3655

Merged
inureyes merged 4 commits into
lablup:mainfrom
Jaeyeong-CHOI:perf/qwen3-vl-vision-rope-gb10
Dec 2, 2026
Merged

inureyes merged 4 commits into
lablup:mainfrom
Jaeyeong-CHOI:perf/qwen3-vl-vision-rope-gb10

Conversation

@Jaeyeong-CHOI

@Jaeyeong-CHOI Jaeyeong-CHOI commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Closes #3634.

Share one expanded cosine/sine table pair across Qwen3-VL vision blocks. Compile the two f32 products but leave addition and casting outside the graph: a fully fused CUDA prototype changed greedy output. The dequantized diagnostic retains its eager reference. Other encoders are unchanged.

Spark2 / NVIDIA GB10

Baseline b67573543 versus this PR; NVIDIA driver 580.178.04, MLX 5a48f9cc. Model: mlx-community/Qwen3-VL-4B-Instruct-4bit, weights SHA-256 90eeb02604181dbcccd0a30a1f550a4a8928ca7dcbee4aee1449239306cfdfca. Deterministic, generated RGB inputs: (x, y, x XOR y), each channel modulo 256. Six paired runs per size, alternating baseline→PR and PR→baseline. Each binary invocation starts a fresh process. Each accepted pair held the shared GPU lock throughout; monitoring rejected overlap with other GPU compute PIDs, builds or transfers. On-disk kernel caches were primed once per binary/size and reused; first use is the first tower call, not a cache-empty compile or an end-to-end cold start. The clock excludes loading and preprocessing, and includes evaluation of the main output and all three DeepStack outputs.

Median [min, max] across the six process timings, ms. For warm, each timing is the median of ten forwards after three warmups. Delta is PR minus baseline within each pair; negative means faster.

Input Phase Baseline, ms PR, ms Paired Δ, ms
256x256 first 3146.183 [3125.206, 3179.712] 4373.284 [4363.844, 4375.641] 1222.256 [1192.513, 1250.435]
256x256 warm 12.608 [10.824, 13.046] 11.794 [11.138, 12.176] -0.654 [-1.736, 1.352]
640x384 first 3150.134 [3135.237, 3167.500] 4384.952 [4362.125, 4399.043] 1229.746 [1209.437, 1256.162]
640x384 warm 27.712 [26.534, 28.767] 27.327 [26.801, 27.613] -0.437 [-1.586, 0.897]

First use regressed in every pair: approximately 1.22–1.23 seconds more on the PR, despite primed disk caches. Warm paired medians favored the PR by 0.65 ms and 0.44 ms, respectively, but both ranges cross zero. These data do not establish a reliable warm speedup, and the first-use cost dominates for one-off calls.

Reproduction

Copy the PR’s examples/qwen3_vl_tower_timing.rs unchanged into the baseline checkout. In each checkout, build and save the executable under the corresponding name:

export CARGO_TARGET_DIR="$BENCH_DIR/target-$REV"  # separate target for each revision
cargo build --locked --features cuda --profile test-fast --example qwen3_vl_tower_timing
cp "${CARGO_TARGET_DIR:-target}/test-fast/examples/qwen3_vl_tower_timing" "$BENCH_DIR/$REV"  # REV=base or cand

Set MODEL to the local model directory. Run on an otherwise idle GB10 under the shared device lock, with a common persistent PTX cache:

cd "$BENCH_DIR"
export MLX_ENABLE_TF32=0 MLXCEL_SDPA_DETERMINISTIC=1 MLXCEL_CACHE_LIMIT=4G
export MLX_PTX_CACHE_DIR="$BENCH_DIR/ptx"
mkdir -p "$MLX_PTX_CACHE_DIR"
exec 9>"$HOME/.local/share/gpu-lock/gb10.lock"; flock 9
for size in 256x256 640x384; do
  for rev in base cand; do "./$rev" "$MODEL" "$size" >/dev/null; done
  for order in 'base cand' 'cand base' 'base cand' 'cand base' 'base cand' 'cand base'; do
    for rev in $order; do echo "$rev"; "./$rev" "$MODEL" "$size"; done
  done
done

Correctness: CUDA Qwen3-VL library tests (64 passed, 1 ignored) include exact eager/compiled f32, f16 and bf16 parity and a deterministic f16 rounding regression. In the earlier two-image validation, the main and three DeepStack outputs matched byte-for-byte; a 16-token greedy continuation matched on the smaller image. Workspace formatting passed. Metal, server latency and other models were not measured.

@inureyes inureyes added status:review Under review type:performance Performance improvements priority:low Low priority area:models Model architectures, weights, loading, metadata modeltype:vlm Vision-language model labels Dec 1, 2026
Jaeyeong-CHOI and others added 4 commits December 2, 2026 08:56
…measurements

Drop `mlx::core::compile` (`compiled_vision_rotary`) from the C++ bridge and keep `VisionRotaryTables::apply` eager. The compiled path regressed NVIDIA GB10 CUDA first-use by +1,222..1,230 ms with warm paired deltas crossing zero, and 4 paired runs on Apple M1 Ultra Metal (`models/qwen3-vl-4b-instruct-4bit`) showed `256x256` warm regressed +0.27 ms [+0.06, +0.86] in 4/4 pairs (first +2.26 ms) while `640x384` warm was -0.34 ms [-0.94, +2.30] (noise, first +6.17 ms).

Keep the hoisted `VisionRotaryTables` (eliminating redundant per-block `cos`/`sin`/`tile` recomputations with bit-identical output), remove the one-off `examples/qwen3_vl_tower_timing.rs` probe, restore `/// Used by:` annotations in `src/vision/encoders/qwen2_vl.rs` (dropping the incorrect `Youtu-VL` entry), and add a multi-image/video grid unit test.

Validated with `cargo test --profile test-fast --lib qwen3_vl` (64 passed), `cargo fmt --check`, and a real Metal `mlxcel generate --image` check on `models/qwen3-vl-4b-instruct-4bit`.

Refs lablup#3634, lablup#3655
@inureyes
inureyes force-pushed the perf/qwen3-vl-vision-rope-gb10 branch from d0f9242 to a8d0da0 Compare December 2, 2026 00:11
@inureyes

inureyes commented Dec 2, 2026

Copy link
Copy Markdown
Member

Implementation Hardening Summary

Audited PR #3655 against issue #3634 and AGENTS.md performance issue completion criteria.

Findings & Fixes

  • Performance (CRITICAL): compiled_vision_rotary (mlx::core::compile) regressed NVIDIA GB10 CUDA first-use by +1,222..1,230 ms in 6/6 pairs with warm deltas crossing zero. Paired Apple M1 Ultra Metal measurements (4 alternating pairs on models/qwen3-vl-4b-instruct-4bit, hoisted compiled vs hoisted eager) confirmed no win on Metal either: 256x256 first +2.26 ms [-2.44, +4.17], warm +0.27 ms [+0.06, +0.86] (compiled slower in 4/4 pairs); 640x384 first +6.17 ms [-6.03, +12.68], warm -0.34 ms [-0.94, +2.30] (crosses zero). Dropped compiled_vision_rotary from src/lib/mlxcel-core/ and kept pure hoisted VisionRotaryTables with eager application, documenting both backend measurements on VisionRotaryTables::apply.
  • Conventions (HIGH): Restored /// Used by: annotations on apply_rotary_pos_emb_vision and rotate_half in src/vision/encoders/qwen2_vl.rs and removed the incorrect Youtu-VL entry (Youtu-VL uses its own youtu_vl_rope.rs).
  • Scope & Tests (MEDIUM): Removed the one-off examples/qwen3_vl_tower_timing.rs probe and added vision_rotary_tables_match_eager_on_multi_image_video_grid in src/vision/encoders/qwen3_vl_tests.rs.
  • Security & Reliability: Zero C++ FFI / JIT compile surface added; bit-identical output on f16, bf16, and f32 verified.

Validation

  • cargo test --profile test-fast --lib qwen3_vl: 64 passed, 0 failed (1 ignored).
  • cargo fmt --check: passed.
  • Real-model Metal check (models/qwen3-vl-4b-instruct-4bit with tests/fixtures/got_ocr_page.png, Apple M1 Ultra): coherent image description generated.

@inureyes
inureyes merged commit cdfd254 into lablup:main Dec 2, 2026
36 checks passed
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 modeltype:vlm Vision-language model priority:low Low priority status:review Under review type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: Compile the Qwen3-VL vision rotary and hoist cos/sin

2 participants