Repository navigation
Description
Summary
Four small vision-side fixes that mlx-vlm landed for families mlxcel already ships: RF-DETR projector LayerNorm eps and mask resize, Muse Glimmer's position-embedding axis order on non-square grids, and Mage-VL-Cowork (a released Mage-VL checkpoint mlxcel cannot load at all, plus FP32 LayerNorm/GELU in the Mage ViT). They share one test surface (vision encoder unit tests plus one real-checkpoint smoke each) and none blocks another, so they are grouped. Implement all four in a single PR, run the verification pass once at the end, and keep pushes to a minimum. Priority follows the worst item, Mage-VL-Cowork failing to load.
1. RF-DETR projector LayerNorm eps and bilinear mask resize
Upstream. Neywa-Labs/mlx-vlm#2709 (2bb12923): the projector's ConvX.bn and the final norm after C2f use eps 1e-6, rfdetr's channel LayerNorm default, instead of nn.LayerNorm's 1e-5 (max |logits| vs PyTorch on the base variant went from 3.46 to 5.4e-3). Neywa-Labs/mlx-vlm#2713 (791231d5, merge 93df458d): _resize_masks upsamples mask logits bilinearly with half-pixel centers, as rfdetr's PostProcess (F.interpolate(mode="bilinear", align_corners=False) then > 0), not cubic (differing mask pixels went from 0.67% to 0). Files: https://github.com/Blaizzy/mlx-vlm/blob/main/mlx_vlm/models/rfdetr/vision.py and https://github.com/Blaizzy/mlx-vlm/blob/main/mlx_vlm/models/rfdetr/generate.py#L200.
Current mlxcel. src/vision/detection/rfdetr/layers.rs:87 (ConvNorm::from_weights, used for every cv1 / cv2 in projector.rs:20-21,41,45) and src/vision/detection/rfdetr/projector.rs:46 (projector.stages.0.1) load the norm at 1.0e-5. src/vision/detection/rfdetr/predictor.rs:221 resizes the selected mask logits with resize_bicubic_nhwc. The other RF-DETR norms already match upstream (backbone 1e-6, decoder and enc_output_norm 1e-5, segmentation 1e-6) and must not change.
Change. Use 1.0e-6 at layers.rs:87 and projector.rs:46. In predictor.rs:221 call crate::vision::dense::interpolate::resize_bilinear_nhwc(&x, (height, width), false, false) (align_corners false, no antialias) and keep the f32 cast and the > 0 threshold.
Acceptance.
-
src/vision/detection/rfdetr/tests.rs:projector_norms_use_eps_1e6buildsConvNormandProjectorfrom a synthetic weight map and asserts every projectorLayerNorm.eps == 1e-6(fails today with 1e-5). - Same file:
mask_resize_is_bilinear_half_pixelresizes a 2x2 logit plane[[-1, 1], [1, -1]]to 4x4 through the predictor's mask path and asserts the result equalsresize_bilinear_nhwc(.., false, false)thresholded at 0 (cubic overshoot produces a different binary plane at the 4x4 borders; confirm the chosen input fails on current code before keeping it).
2. Muse Glimmer position-embedding H/W order
Upstream. Neywa-Labs/mlx-vlm#2746 (0d3ed9ee, merge b249dab2): the reference samples the learned table with meshgrid(pos_h, pos_w, indexing="xy"), so PatchEmbedder._position_embeddings swaps roles: temporal, height, width = t, w, h before building the sample grids. The flattened order changes with it, so this is not a transpose of the current output. https://github.com/Blaizzy/mlx-vlm/blob/main/mlx_vlm/models/muse_glimmer/vision.py#L107.
Current mlxcel. src/vision/encoders/muse_glimmer_pos.rs:19 bilinear_indices_and_weights loops for y in 0..h / for x in 0..w with axis_bilinear(y, h, table_h) and axis_bilinear(x, w, table_w) (:35-50), the pre-fix order. Square grids are unaffected; every non-square image is.
Change. Inside the per-grid loop, bind let (h, w) = (w, h); before the loops (outer loop runs over the original width count, sampled against table_h; inner over the original height count, against table_w). Token count stays t * h * w. muse_2d_rope is unchanged (upstream did not touch it).
Acceptance.
-
src/vision/encoders/muse_glimmer_pos.rs(inlinemod tests,:169):position_table_uses_xy_meshgrid_ordercallsbilinear_indices_and_weights(&[(1, 2, 4)], 4, 4)and asserts corner 0 indices[0, 2, 4, 6, 8, 10, 12, 14], corner 1[1, 3, 5, 7, 9, 11, 13, 15], corner 2[4, 6, 8, 10, 12, 14, 12, 14], corner 3[5, 7, 9, 11, 13, 15, 13, 15], weights 0.5 for corners 0 and 1 and 0.0 for corners 2 and 3. These are the outputs of the upstream numpy code on that grid. Current code returns corner 0[0, 1, 2, 3, 8, 9, 10, 11]with corner 0 and 2 weights 0.5. - Same module: a square grid
(1, 3, 3)returns the same arrays before and after the change.
3. Mage-VL-Cowork loading
Upstream. Neywa-Labs/mlx-vlm#2636 (merge 4693cd0e, 99bee88d): microsoft/Mage-VL-Cowork (4B computer-use model, same Mage-ViT and Qwen3 decoder) declares model_type: "llava_onevision2" (and vision_config.model_type: "llava_onevision2") and stores the tower under bare visual.*. Upstream maps llava_onevision2 -> mage_vl in MODEL_REMAPPING (mlx_vlm/utils.py), registers the Mage processor for it, and adds visual.* -> vision_tower.* to Model.sanitize (https://github.com/Blaizzy/mlx-vlm/blob/main/mlx_vlm/models/mage_vl/mage_vl.py).
Current mlxcel. src/models/detection.rs:1161 matches only "mage_vl", and nothing in src/ mentions llava_onevision2, so detection fails before any weight is read. remap_mage_vl_weight_key (src/loading/vlm_mage_vl.rs:49) rewrites model.visual. but not visual.. The checkpoint's other keys (model.layers.*, model.embed_tokens, model.norm, lm_head.weight) already pass through unchanged, and its vision config (temporal_patch_size: 1, layer_norm_type: "layer_norm", hidden 1024, 16 heads) satisfies src/vision/mage_vl_config.rs validation.
Change. Add "llava_onevision2" to the mage_vl arm in detection.rs and to ModelType::MageVLM's aliases in src/models/registry.rs:1260. Add a visual. arm to remap_mage_vl_weight_key after the model.visual. arm. Confirm the processor path picks up Cowork's preprocessor_config.json the same way as Mage-VL.
Acceptance.
-
src/loading/vlm_mage_vl_tests.rs: extendhub_and_converted_keys_remap_to_one_layoutwith("visual.merger.mlp.0.weight", "vision_tower.merger.mlp.0.weight")and("visual.layernorm_pre.bias", "vision_tower.layernorm_pre.bias"). -
src/models/detection_tests.rs:llava_onevision2_detects_as_mage_vl(config withmodel_type: "llava_onevision2"and a vision config) returnsModelType::MageVLM; fails today.
4. Mage-VL ViT FP32 LayerNorm and GELU
Upstream. Same PR, ae763da4: every Mage ViT LayerNorm runs fast.layer_norm on float32 input, weight and bias and casts back once; GELU (exact erf) runs in float32 and casts back once. Applies to layer_norm1, layer_norm2, layernorm_pre, layernorm_post (when present) and the merger ln_q, and to the SigLIP MLP and merger GELUs. https://github.com/Blaizzy/mlx-vlm/blob/main/mlx_vlm/models/mage_vl/vision.py.
Current mlxcel. src/vision/encoders/mage_vl.rs:146-152 (MageVlLayer::forward) and :163-168 (MageVlPatchMerger::forward) call LayerNorm::forward and mlxcel_core::gelu in the input dtype (f16 after the load-time bf16 conversion, or bf16 when kept). The norms load at :211, :219, :227, :235; layernorm_pre runs at :306.
Change. Add a private fn layer_norm_f32(ln: &LayerNorm, x: &MlxArray) -> UniquePtr<MlxArray> and fn gelu_f32(x: &MlxArray) -> UniquePtr<MlxArray> in mage_vl.rs (cast x, ln.weight and ln.bias to float32, run the fast layer norm, cast to x's dtype; for GELU cast to float32, mlxcel_core::gelu, cast back) and use them at all five norm sites and both GELU sites. Keep weights in their loaded dtype; only the arithmetic widens. Do not change mlxcel_core::layers::LayerNorm (a shared hotspot).
Acceptance.
-
src/vision/encoders/mage_vl_tests.rs:mage_layer_norm_rounds_once_from_f32runslayer_norm_f32on a seeded f16 input of width 1024 whose rows have |mean| much larger than their spread, with f16 weight and bias, and asserts the output equals the float32 reference cast to f16 element for element; the same input throughLayerNorm::forward(today's path) must differ in at least one element, which the test also asserts so it proves discrimination. - Same file:
mage_gelu_rounds_once_from_f32does the same forgelu_f32against f32 erf GELU cast to f16.
Validation
Checkpoints: mlx-community/rfdetr-base-fp32 and mlx-community/rfdetr-seg-small-fp32 (0.1 GB each), mlx-community/Muse-Glimmer-30B-4bit (19.4 GB), microsoft/Mage-VL-Cowork (9.5 GB, BF16) and mlx-community/Mage-VL-8bit as the no-regression Mage check.
make verify-test
cargo clippy --workspace --all-targets -- -D warnings
cargo fmt --check
./target/release/mlxcel detect -m models/rfdetr-seg-small-fp32 -i street.jpg --format json --masks-dir out/
./target/release/mlxcel generate -m models/Mage-VL-Cowork --image screenshot.png -p "Click the Save button." -n 256
./target/release/mlxcel generate -m models/Muse-Glimmer-30B-4bit --image wide_1280x720.png -p "Describe the image." -n 128The RF-DETR, Glimmer and Mage ViT items change numerics, so per CLAUDE.md "Changes that move the numbers" report evidence with the reverted arm: for RF-DETR, the real_tests.rs parity numbers (logits max abs, mask IoU) before and after; for Glimmer and Mage, the image trace (MLXCEL_TRACE_IMAGE with scripts/mlx_vlm_image_logit_trace.py --float32 --mlxcel-prompt and scripts/compare_logit_traces.py --decided 2) on a non-square image, with and without the change. For Cowork, a pass is a load plus a well-formed <tool_call> reply to a screenshot prompt.
Related
#2431 (RF-DETR port), #1100 (Muse Glimmer port), #1365 (Mage-VL port). Upstream: Neywa-Labs/mlx-vlm#2709, Neywa-Labs/mlx-vlm#2713, Neywa-Labs/mlx-vlm#2746, Neywa-Labs/mlx-vlm#2636.
Activity
fix(vision): RF-DETR, Muse Glimmer and Mage-VL-Cowork parity fixes
docs: point the V54 GELU row at the moved CLIP call site
fix(vision): RF-DETR, Muse Glimmer and Mage-VL-Cowork parity fixes (l…