Repository navigation
Mistral3 medium - #1986
Merged
Merged
Mistral3 medium#1986
Conversation
zcbenz
approved these changes
Oct 22, 2026
9 tasks
This was referenced Dec 4, 2026
inureyes
added a commit
to lablup/mlxcel
that referenced
this pull request
Dec 5, 2026
* feat(models): load per-tensor FP8 checkpoints as native mxfp8 Mistral's own FP8 releases (`mistralai/Mistral-Medium-3.5-128B`) store each converted projection as E4M3 bytes plus one BF16 `weight_scale_inv` scalar (`weight_block_size: null`). mlxcel had no per-tensor FP8 route, so the bytes arrived as `uint8` with nothing applying the scale: every projection would be wrong by its own per-tensor factor. `src/models/fp8_tensor.rs` adds the layout and a byte-preserving rewrite. The E4M3 bytes already are `w / scale`, so the plane is viewed as `uint32`, every E8M0 group scale byte becomes `127 + floor(log2(scale))`, and the residual `scale / 2^floor(log2(scale))` in `[1, 2)` is applied as the existing per-linear `global_scale` output multiplier. The power-of-two part is an exact exponent shift and a per-tensor scalar commutes with the matmul, so no weight byte is re-encoded. Dequantizing was rejected on memory (about 256 GB), and upstream's unit-scale convention (ml-explore/mlx-lm#1986) leaves the matmul output `1 / scale` (7400x here) too large, which bf16 absorbs and the f16 activations mlxcel runs on Apple Silicon do not. Wiring: - dtype policy: `.weight_scale_inv` / `.activation_scale` are exempt from the bf16-to-f16 conversion in both funnels, so the scale is read from its BF16 value and never through an f16 subnormal. - config: the `mxfp8` block is inserted from the `quantization_config` declaration before the text args are parsed; an explicit `quantization` still wins. - Mistral 3 VLM route: the transformers-5 key layout (`model.language_model.`, `model.vision_tower.`, `model.multi_modal_projector.`) is remapped only when the checkpoint uses it, then the weights are rewritten. - text-only route: `Ministral3Model::load` and `::load_from_text_config` do the same merge and rewrite. Not included: the issue's optional q/k/v and gate/up fusion (a measure-then- keep gate needs the 134 GB checkpoint on a 256 GB host), so the split planes stay. `FusedQKVLinear` already keeps planes split when any carries a `global_scale` sidecar, which is the outcome that gate would otherwise have to choose. The end-to-end run and the logit trace are likewise unverified here. Closes #3239 (items 1-5 and 7; item 6 is the measure-then-keep fusion). * fix(models): harden the per-tensor FP8 rewrite and its Mistral 3 routes - Read the FP8 declaration from the wrapper in Ministral3Model::load_from_text_config. The checkpoint declares quantization_config on the wrapper, so checking text_config alone never inserted the mxfp8 block and the text-only route read the rewritten planes without it. New merge_per_tensor_fp8_quantization_into covers this. - Run rewrite_per_tensor_fp8 only for a declared checkpoint on all three Mistral 3 routes, so a block FP8 export that also carries .weight_scale_inv is left to its own path instead of being refused. - Split the scale from its f32 bits instead of log2, so every power of two gets residual 1.0 and f32 subnormals are normalized exactly. - Refuse a weight that is not a 2-D [out, in] plane or has an empty axis, compute the scale-plane size in checked i64, and refuse a raw one-byte .weight left without .scales (a missing weight_scale_inv), which otherwise loaded as a dense uint8 matrix. - Restore the four vlm_pixtral tests the branch had dropped, pin the YaRN check to the 128B checkpoint's literal rope_parameters block, cover the ministral3 and mistral3 plan architectures in the TP test, and add per_tensor_fp8_scale_skips_bf16_to_f16 to sanitize_tests.rs plus E8M0 range and malformed-plane tests. Verified on mistralai/Ministral-3-3B-Instruct-2512 (per-tensor FP8, weight_block_size null) on GB10 CUDA: greedy text and image generation are coherent. --------- Co-authored-by: Jeongkyu Shin <inureyes@gmail.com>
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.
This PR added correct mistral implementation.
Before this pull requests, weights were dequantized in sanitize to bf16, which is wasteful: peak memory is 200+ GB instead of 150GB and decode is slower because we fetch bf16 weights.
The problem is that all the layers are implemented as fp8 matmuls with fp32 scale, that we don't support natively.
Since 1) only this mistral model has this type of layers 2) we have mxfp8 mode, no fp8 3) mxfp8 does not have fp32 scale I propose to add custom layers to this model to express this type of computation.