Skip to content

Mistral3 medium - #1986

Merged
zcbenz merged 4 commits into
mainfrom
mistral3-medium
Oct 22, 2026
Merged

zcbenz merged 4 commits into
mainfrom
mistral3-medium

Conversation

@nastya236

Copy link
Copy Markdown
Collaborator

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.

@zcbenz
zcbenz merged commit 0d4d3d0 into main Oct 22, 2026
2 checks passed
@zcbenz
zcbenz deleted the mistral3-medium branch October 22, 2026 09:48
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants