mlx-dspark
==========

This project is an independent MLX/Apple-Silicon port of the *inference path* of
DSpark, a speculative-decoding drafter open-sourced by DeepSeek as part of the
DeepSpec codebase:

  - DeepSpec: https://github.com/deepseek-ai/DeepSpec  (MIT License)

It loads the published DSpark drafter checkpoints (also released by DeepSeek under
their respective licenses):

  - deepseek-ai/dspark_gemma4_12b_block7
  - deepseek-ai/dspark_qwen3_4b_block7

The DSpark drafter architecture, training, and checkpoints are the work of DeepSeek
and the DeepSpec authors. This repository reimplements only the DSpark forward/
verification path for MLX and contains no DeepSpec source code.

---

DFlash (z-lab)
--------------

mlx-dspark also runs z-lab's original DFlash drafter (block diffusion for speculative
decoding):

  - DFlash: https://github.com/z-lab/dflash  (MIT License)
  - Paper:  Chen et al., "DFlash: Block Diffusion for Flash Speculative Decoding",
            arXiv:2602.06036

The file `src/mlx_dspark/dflash_model.py` contains the DFlash drafter *model* classes
(DFlashConfig, DFlashAttention, DFlashDecoderLayer, DFlashDraftModel) vendored verbatim
from z-lab/dflash (`dflash/model_mlx.py`) under the MIT License; copyright (c) z-lab.
The MIT permission notice is reproduced in that file's header. The generation/
verification loop around it (`generate.dflash_generate`) is mlx-dspark's own. DFlash
checkpoints (e.g. `z-lab/gemma4-12B-it-DFlash`) are downloaded at runtime from z-lab.

The same file's **DFlash 2** components (DFlashGroupedConv, CandidateSelector and the
selector drafting path) are an MLX port of the DFlash 2 reference implementation
contributed to SGLang (sgl-project/sglang, `srt/models/dflash.py` and
`srt/speculative/dflash_worker_v2.py`, Apache License 2.0). DFlash 2 is by Inco AI
(https://inco.ai/blog/dflash2/); its checkpoints (e.g. `incoai/Qwen3.8-27B-DFlash2`,
Apache-2.0) are downloaded at runtime from their publisher.

The same file's **DSpine** components (DSpineCodeBridge, the last-write refinement table and
the block-causal draft path) are an MLX port of Tencent's DSpine reference implementation
(https://github.com/Tencent/DSpine — `sglang_dspine/models/dspine.py`, `kernels/dspine.py`
and the authors' `dspine_mlx` package; Apache License 2.0, Copyright (C) 2026 Tencent).
Paper: Zhang et al., "Draft in Parallel, Condition Through Depth: Adjacent Causal Injection
for Speculative Decoding", arXiv:2609.36173. No DSpine source code is included; the math is
reimplemented against our own generate / verify loop. DSpine checkpoints (e.g.
`tencent/Qwen3.8-27B-DSpine-b16`, Apache-2.0) are downloaded at runtime from Tencent.

---

Small-M quantized-matmul kernel (credits; no third-party code included)
-----------------------------------------------------------------------

`src/mlx_dspark/skinny_qmm.py` is mlx-dspark's own implementation. Its design builds on
ideas from two MIT-licensed projects (their code is not included):

  - avlp12's mlx-lm fork (`mlx_lm/fast_qmm.py`, the `qmm_mma4` kernel; shipped here
    v0.12.0-v0.19.0): a simdgroup-matrix kernel for the few-row qmm dead zone
    (https://github.com/avlp12/mlx-lm, context: https://github.com/ml-explore/mlx/issues/4265)
  - TensorFold (`simd_qmm`, https://github.com/ashhart/TensorFold): weights as the
    matrix operand for skinny batches, with quantized fields masked in place against
    pre-scaled inputs (a technique also long used by llama.cpp's Metal kernels)

---

Nanbeige model module (Apple / mlx-lm)
--------------------------------------

The file `src/mlx_dspark/nanbeige_lm.py` contains the Nanbeige4.2 model classes
(ModelArgs, Attention, MLP, TransformerBlock, NanbeigeModel, Model) vendored from the
mlx-lm main branch (`mlx_lm/models/nanbeige.py`, MIT License; Copyright © 2026 Apple
Inc.), which no mlx-lm release ships yet:

  - https://github.com/ml-explore/mlx-lm  (MIT License)

The runtime registration shim around it is mlx-dspark's own and self-retires once an
installed mlx-lm provides the module itself.

---

Inkling 2 (Thinking Machines) support
-------------------------------------

`src/mlx_dspark/inkling.py` (the DSpark head Inkling 2 ships in its own repository) follows
the math of vLLM's Inkling 2 DSpark implementation (Apache License 2.0, Copyright
contributors to the vLLM project) as ported to MLX by mlx-vlm
(`mlx_vlm/speculative/drafters/inkling2_dspark`, https://github.com/Blaizzy/mlx-vlm). No
source file is included; the module is written against mlx-dspark's own drafter interface.

`src/mlx_dspark/inkling_kernels.py` adapts the FP4 / FP8 quantized types and the wide
matrix-vector layout of MLX (`mlx/backend/metal/kernels/fp_quantized.h`, MIT License,
Copyright © 2023 Apple Inc.), via mlx-vlm's `models/masked_quantized.py` (MIT License).

`src/mlx_dspark/inkling_fused.py` contains three forward bodies adapted from mlx-vlm's
Inkling 2 model (`mlx_vlm/models/inkling2/language.py`: `TextModel.__call__`,
`DecoderLayer.__call__` and `Attention.__call__`; Apache License 2.0, Copyright 2026 The
HuggingFace Team, itself adapted for MLX from Transformers' Inkling2), with runs of small
operations replaced by mlx-dspark's own Metal kernels; the short-convolution expression of
mlx-vlm's `models/inkling2/kernels.py` and the relative-position mask expression of its
`models/inkling/language.py` (MIT License). The kernels' reductions follow the layouts of
MLX's own `rms_norm.metal` and `reduction/reduce_row.h`, and the packed verify attention is
derived from MLX's two-pass `sdpa_vector.h` (MIT License, Copyright © 2024 Apple Inc.).
`src/mlx_dspark/inkling_batch.py` (batched decoding) follows the same three forward bodies
with the sequence state held per request.

Inkling 2 checkpoints (e.g. `thinkingmachines/Inkling-2-Mini`, Apache-2.0) are downloaded
at runtime from their publisher.

---

Target models (Gemma-4, Qwen3) are downloaded at runtime from their respective
publishers and are subject to their own licenses (e.g. the Gemma Terms of Use and the
Qwen license).

No model weights are bundled with this package.
