Enable MetalConfig to load pre-quantized MLX models from HuggingFace Hub#44348
Open
n0kovo wants to merge 4 commits intohuggingface:mainfrom
Open
Enable MetalConfig to load pre-quantized MLX models from HuggingFace Hub#44348n0kovo wants to merge 4 commits intohuggingface:mainfrom
n0kovo wants to merge 4 commits intohuggingface:mainfrom
Conversation
Most quantized models for Apple Silicon on the Hub are in MLX format.
The MetalConfig quantization backend can quantize standard checkpoints
on-the-fly but cannot load pre-quantized MLX models. This commit fixes
the five issues blocking that:
1. auto.py: Detect MLX affine quantization configs (mode=affine + bits
+ group_size) in AutoQuantizationConfig.from_dict() and map them to
the Metal quantization method.
2. hub.py: Handle stale shard index files. MLX repos often copy
model.safetensors.index.json from the original model, referencing
non-existent shards. Add _rebuild_shard_index_from_repo() fallback
that discovers actual safetensors files via HfApi and rebuilds the
weight_map from their headers.
3. quantizer_metal.py: Add weight conversions for pre-quantized loading:
- Rename MLX "biases" keys to "qbiases" (MetalLinear convention)
- Dequantize embed_tokens back to float (nn.Embedding expects float)
- Skip auto-exclusion of lm_head for pre-quantized checkpoints since
MLX models typically quantize the output head too
4. conversion_mapping.py: Add Qwen3VL key prefix mappings for MLX
checkpoint format (language_model.model.* -> model.language_model.*,
language_model.lm_head.* -> lm_head.*, vision_tower.* -> model.visual.*)
5. metal_quantization.py:
- Make MetalDequantize use source_patterns from kwargs as dict keys
for flexibility with pattern-specific converters
- Add _load_from_state_dict fallback for biases->qbiases rename
- Add locally-compiled Metal shader fallback via torch.mps.compile_shader
when the Hub kernel is unavailable or targets an incompatible MSL version
The qwen3_vl entry in conversion_mapping.py broke CI tests because: 1. ruff format: formatting issues in quantizer_metal.py and hub.py 2. tests_torch: conversion_mapping entries must be bidirectional (for save/load round-trips), but the regex-anchored MLX key patterns aren't reversible Fix by moving the MLX-specific key renamings into MetalHfQuantizer's get_weight_conversions(), where they only apply during pre-quantized loads and don't interfere with standard checkpoint tests.
The pre-built metallib targets MSL 4.0 (macOS 26) which is rejected by the Metal runtime on macOS 15.x, printing "Failed to create Metal library from embedded header" to stderr before raising. Redirect fd 2 to /dev/null during the smoke test to avoid noisy output.
Contributor
|
[For maintainers] Suggested jobs to run (before merge) run-slow: metal |
redpanda1995
approved these changes
Feb 28, 2026
Member
|
cc @MekkCyber maybe? You might need to pull in someone else for MLX though |
Author
|
cc @SunMarc |
Author
Thanks! Feel free to ping me if I can contribute in any way! |
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
Most quantized models for Apple Silicon on the Hub are in MLX format. The
MetalConfigquantization backend supports on-the-fly quantization of standard checkpoints but cannot load pre-quantized MLX models. This PR fixes the five issues blocking that:quantizers/auto.py: Detect MLX affine quantization configs (mode=affine+bits+group_size) inAutoQuantizationConfig.from_dict()and map them to the Metal quantization methodutils/hub.py: Handle stale shard index files — MLX repos often copymodel.safetensors.index.jsonfrom the original model referencing non-existent shards. Adds_rebuild_shard_index_from_repo()fallback that discovers actual safetensors files viaHfApiand rebuilds theweight_mapfrom their headersquantizers/quantizer_metal.py: Weight conversions for pre-quantized loading (MLXbiases→qbiasesrename,embed_tokensdequantization, skip auto-exclusion of lm_head for pre-quantized checkpoints)conversion_mapping.py: Qwen3VL key prefix mappings for MLX checkpoint formatintegrations/metal_quantization.py: MakeMetalDequantizeflexible with source patterns, add_load_from_state_dictfallback for biases→qbiases rename, and add locally-compiled Metal shader fallback viatorch.mps.compile_shaderwhen the Hub kernel is unavailable or targets an incompatible MSL versionUsage
Test plan