Skip to content

feat: add DeepSeek V4.1 Flash with DSpark MTP and Engram SSD offload - #3574

Merged
jundot merged 5 commits into
mainfrom
feat/deepseek-v41
Sep 11, 2026
Merged

jundot merged 5 commits into
mainfrom
feat/deepseek-v41

Conversation

@jundot

@jundot jundot commented Sep 11, 2026 •

Copy link
Copy Markdown
Owner

Add DeepSeek V4.1 Flash to oMLX's model loading, serving, model settings, and Web UI quantization paths. Original FP4/FP8 checkpoints load with automatic packed repacking; converted checkpoints support vision, Engram RAM/SSD modes, and single-stream Lightning MTP.

Memory required to run (Engram SSD offload)

Model Resident weights oMLX memory budget with SSD offload
DeepSeek-V4.1-Flash-oQ3e-mtp 239.34 GiB ~251.34 GiB
DeepSeek-V4.1-Flash-oQ4e-mtp 287.92 GiB ~302.35 GiB

These budgets include the language backbone, vision components, and DSpark MTP weights, with Engram tables offloaded to SSD. They are calculated from checkpoint headers as resident weights × 1.05 + 32 MiB; they are loader estimates, not measured peaks or total system RAM requirements. The oQ4e runtime peaks are listed in the benchmarks below.

Benchmarks

Benchmarked on M3 Ultra (512 GiB) with oQ4e, code_python, temperature 1, top_p 1, and top_k 0. Prefix caching is off; each configuration uses a 4K warm-up, up to 2K prefill chunks, and 128 generated tokens.

Speeds are tok/s; peak memory is GiB. PP pairs show MTP OFF → ON; peak memory uses MTP ON. Each row is one sampled run, excluding model loading.

Engram RAM

Context PP OFF → ON TG (128) MTP OFF TG (128) MTP ON TG Delta (%) Peak MLX Peak RSS
4K 457.99 → 452.15 20.23 32.06 +58.5% 404.04 403.16
16K 459.12 → 454.77 20.00 34.72 +73.6% 404.52 403.19
32K 452.19 → 447.73 19.80 31.51 +59.1% 404.54 403.22
64K 439.39 → 435.56 19.67 39.66 +101.6% 404.56 403.25

Engram SSD offload

Context PP OFF → ON TG (128) MTP OFF TG (128) MTP ON TG Delta (%) Peak MLX Peak RSS
4K 458.62 → 452.48 19.96 34.45 +72.6% 289.60 295.05
16K 441.40 → 436.41 19.83 35.42 +78.6% 290.08 306.39
32K 443.11 → 442.52 19.75 34.14 +72.9% 290.09 315.37
64K 429.41 → 431.74 19.58 36.00 +83.9% 290.12 329.09

Changes

  • Add the model, vision encoder, image processor, official conversation encoder, and namespaced tool-call handling.
  • Keep window KV, compressed KV, and index keys packed. Add sparse attention, streaming index selection, grouped-expert execution, and affine 2/3/4/6/8-bit Metal kernels.
  • Add per-model Engram SSD offload with automatic enforcement when the model exceeds the memory ceiling. RAM mode keeps packed tables in Metal-managed buffers shared with CPU gathers, without expanding the tables to BF16. SSD mode prefetches selected pages and drains pending reads before closing mappings.
  • Integrate oQ3/oQ3e and oQ4/oQ4e into the existing quantization workflow. Budget Engram separately, export a unified safetensors index with 5 GB shard targets, preserve DSpark heads for -mtp checkpoints, and retain original vision tensor precision.
  • Add DSpark drafting and packed verification through Lightning MTP, with accepted-boundary restoration without target replay.
  • Support SSD prefix-cache persistence and boundary snapshots at 2,048-token boundaries, including partial-prefix restoration.

Validation

  • All 16 temperature-1 benchmark configurations above completed with 128 generated tokens and zero prefix-cache hits. Runtime Engram and MTP modes were checked, and all test servers were stopped after measurement.
  • Earlier greedy validation matched all eight RAM outputs to their corresponding SSD outputs. The new sampled runs do not require output equality across configurations.
  • Recorded official numerical outputs cover prefill/decode, DSpark, Engram and vision without vendoring the reference implementation. An independent MLX oracle checks attention against 64-key online maxima and BF16 probability rounding; it does not execute the official CUDA kernels.
  • Regression checks passed for packed storage, RAM/SSD equivalence, memory accounting, unload settlement, and wired-limit restoration on successful and failed loads. Full/partial SSD prefix-cache restoration and staggered requests were verified separately.
  • Metal extension builds, offload settings UI checks, and packaging checks passed. Focused attention and model regressions passed after the latest changes; the full suite has not been rerun after the RAM residency and attention changes.

A real-image smoke test passed and original vision weights are preserved; broader vision and 1M-context validation remain pending, with single-stream Lightning MTP and sequential per-row execution.

Acknowledgements

Thanks to DeepSeek and the mlx-vlm contributors for the code adapted in this implementation:

Original copyright and license notices are retained with adapted code.

@jundot
jundot merged commit f79b785 into main Sep 11, 2026
3 checks passed
monroewilliams added a commit to monroewilliams/omlx that referenced this pull request Sep 11, 2026
Both V4.1 export writers wrote safetensors without __metadata__, so
model_discovery._is_hf_cache_mlx_compatible() had no signal to accept the
checkpoint and silently filtered every oQ V4.1 release out of the model
list. The repo name carries 'mtp' rather than a standalone 'mlx' token and
model_type is deepseek_v41, so neither the name fallback nor the
speculative-decoding helper path rescues it either.

- ShardWriter._flush(): pass metadata={"format": "mlx"}. finish() only
  renames the .model-shard-NNNNN temporaries, so the published
  model-NNNNN-of-NNNNN names inherited the omission (61 of 65 shards).
- quantize_engram(): the hand-rolled header now emits __metadata__ after
  the tensor entries (the 4 engram shards). Padding and data_start derive
  from the encoded length, so the longer header needs no offset changes.

Every mx.save_safetensors site in omlx/oq.py already declared the format;
deepseek_v41/sharding.py is the only writer added by jundot#3574 that did not,
and deepseek_v4 has no export code at all -- which is why the earlier V4
release enumerated fine while V4.1 stayed invisible.

Bit width is orthogonal: quantize() accepts oq_level 3 and 4 through one
unconditional ShardWriter and one quantize_engram(bits=oq_level), so both
published quantizations are affected. The test is parametrized over
bits=[3, 4] to pin that down, and asserts DiskEngramEmbedding still
resolves its tensor entries from the enlarged header (TensorFile reads by
explicit key only; nothing iterates header keys).

Verified to fail against the pre-fix writers at both the projection shard
and the engram shard, so neither assertion is vacuous.
kaushikvira pushed a commit to kaushikvira/omlx that referenced this pull request Sep 12, 2026
jundot pushed a commit that referenced this pull request Sep 14, 2026
Both V4.1 export writers wrote safetensors without __metadata__, so
model_discovery._is_hf_cache_mlx_compatible() had no signal to accept the
checkpoint and silently filtered every oQ V4.1 release out of the model
list. The repo name carries 'mtp' rather than a standalone 'mlx' token and
model_type is deepseek_v41, so neither the name fallback nor the
speculative-decoding helper path rescues it either.

- ShardWriter._flush(): pass metadata={"format": "mlx"}. finish() only
  renames the .model-shard-NNNNN temporaries, so the published
  model-NNNNN-of-NNNNN names inherited the omission (61 of 65 shards).
- quantize_engram(): the hand-rolled header now emits __metadata__ after
  the tensor entries (the 4 engram shards). Padding and data_start derive
  from the encoded length, so the longer header needs no offset changes.

Every mx.save_safetensors site in omlx/oq.py already declared the format;
deepseek_v41/sharding.py is the only writer added by #3574 that did not,
and deepseek_v4 has no export code at all -- which is why the earlier V4
release enumerated fine while V4.1 stayed invisible.

Bit width is orthogonal: quantize() accepts oq_level 3 and 4 through one
unconditional ShardWriter and one quantize_engram(bits=oq_level), so both
published quantizations are affected. The test is parametrized over
bits=[3, 4] to pin that down, and asserts DiskEngramEmbedding still
resolves its tensor entries from the enlarged header (TensorFile reads by
explicit key only; nothing iterates header keys).

Verified to fail against the pre-fix writers at both the projection shard
and the engram shard, so neither assertion is vacuous.
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.

1 participant