dspark-mlx 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dspark_mlx-0.1.0/LICENSE +182 -0
- dspark_mlx-0.1.0/PKG-INFO +108 -0
- dspark_mlx-0.1.0/README.md +78 -0
- dspark_mlx-0.1.0/dspark_mlx/__init__.py +45 -0
- dspark_mlx-0.1.0/dspark_mlx/adapter.py +78 -0
- dspark_mlx-0.1.0/dspark_mlx/arch/__init__.py +6 -0
- dspark_mlx-0.1.0/dspark_mlx/arch/backbone.py +57 -0
- dspark_mlx-0.1.0/dspark_mlx/arch/deepseek_v4.py +35 -0
- dspark_mlx-0.1.0/dspark_mlx/arch/gemma4.py +327 -0
- dspark_mlx-0.1.0/dspark_mlx/arch/qwen3.py +337 -0
- dspark_mlx-0.1.0/dspark_mlx/cli.py +164 -0
- dspark_mlx-0.1.0/dspark_mlx/events.py +26 -0
- dspark_mlx-0.1.0/dspark_mlx/generate.py +88 -0
- dspark_mlx-0.1.0/dspark_mlx/hosts/__init__.py +14 -0
- dspark_mlx-0.1.0/dspark_mlx/hosts/gemma4_unified.py +64 -0
- dspark_mlx-0.1.0/dspark_mlx/hosts/mlx_lm.py +122 -0
- dspark_mlx-0.1.0/dspark_mlx/kernels.py +85 -0
- dspark_mlx-0.1.0/dspark_mlx/loader.py +92 -0
- dspark_mlx-0.1.0/dspark_mlx/loading.py +96 -0
- dspark_mlx-0.1.0/dspark_mlx/loop.py +95 -0
- dspark_mlx-0.1.0/dspark_mlx/model/__init__.py +6 -0
- dspark_mlx-0.1.0/dspark_mlx/model/attention.py +142 -0
- dspark_mlx-0.1.0/dspark_mlx/model/block.py +116 -0
- dspark_mlx-0.1.0/dspark_mlx/model/config.py +78 -0
- dspark_mlx-0.1.0/dspark_mlx/model/drafter.py +53 -0
- dspark_mlx-0.1.0/dspark_mlx/model/heads.py +49 -0
- dspark_mlx-0.1.0/dspark_mlx/model/hyper.py +67 -0
- dspark_mlx-0.1.0/dspark_mlx/model/moe.py +113 -0
- dspark_mlx-0.1.0/dspark_mlx/model/norm_rope.py +77 -0
- dspark_mlx-0.1.0/dspark_mlx/quant.py +39 -0
- dspark_mlx-0.1.0/dspark_mlx/recipe.py +63 -0
- dspark_mlx-0.1.0/dspark_mlx/registry.py +37 -0
- dspark_mlx-0.1.0/dspark_mlx/verify.py +118 -0
- dspark_mlx-0.1.0/dspark_mlx.egg-info/PKG-INFO +108 -0
- dspark_mlx-0.1.0/dspark_mlx.egg-info/SOURCES.txt +53 -0
- dspark_mlx-0.1.0/dspark_mlx.egg-info/dependency_links.txt +1 -0
- dspark_mlx-0.1.0/dspark_mlx.egg-info/entry_points.txt +2 -0
- dspark_mlx-0.1.0/dspark_mlx.egg-info/requires.txt +13 -0
- dspark_mlx-0.1.0/dspark_mlx.egg-info/top_level.txt +1 -0
- dspark_mlx-0.1.0/pyproject.toml +46 -0
- dspark_mlx-0.1.0/setup.cfg +4 -0
- dspark_mlx-0.1.0/tests/test_accept_lossless.py +114 -0
- dspark_mlx-0.1.0/tests/test_block_parity.py +204 -0
- dspark_mlx-0.1.0/tests/test_drafter_parity.py +151 -0
- dspark_mlx-0.1.0/tests/test_gemma4_e2e.py +172 -0
- dspark_mlx-0.1.0/tests/test_generate_lossless.py +106 -0
- dspark_mlx-0.1.0/tests/test_heads_parity.py +80 -0
- dspark_mlx-0.1.0/tests/test_host_mlx_lm.py +105 -0
- dspark_mlx-0.1.0/tests/test_kernels_parity.py +94 -0
- dspark_mlx-0.1.0/tests/test_layers_parity.py +178 -0
- dspark_mlx-0.1.0/tests/test_loading.py +95 -0
- dspark_mlx-0.1.0/tests/test_loop_eager.py +79 -0
- dspark_mlx-0.1.0/tests/test_qwen3_arch.py +60 -0
- dspark_mlx-0.1.0/tests/test_qwen3_e2e.py +109 -0
- dspark_mlx-0.1.0/tests/test_registry.py +43 -0
dspark_mlx-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction, and
|
|
10
|
+
distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright
|
|
13
|
+
owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities
|
|
16
|
+
that control, are controlled by, or are under common control with that entity.
|
|
17
|
+
For the purposes of this definition, "control" means (i) the power, direct or
|
|
18
|
+
indirect, to cause the direction or management of such entity, whether by
|
|
19
|
+
contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
20
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
21
|
+
|
|
22
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising
|
|
23
|
+
permissions granted by this License.
|
|
24
|
+
|
|
25
|
+
"Source" form shall mean the preferred form for making modifications, including
|
|
26
|
+
but not limited to software source code, documentation source, and configuration
|
|
27
|
+
files.
|
|
28
|
+
|
|
29
|
+
"Object" form shall mean any form resulting from mechanical transformation or
|
|
30
|
+
translation of a Source form, including but not limited to compiled object code,
|
|
31
|
+
generated documentation, and conversions to other media types.
|
|
32
|
+
|
|
33
|
+
"Work" shall mean the work of authorship, whether in Source or Object form,
|
|
34
|
+
made available under the License, as indicated by a copyright notice that is
|
|
35
|
+
included in or attached to the work (an example is provided in the Appendix
|
|
36
|
+
below).
|
|
37
|
+
|
|
38
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that
|
|
39
|
+
is based on (or derived from) the Work and for which the editorial revisions,
|
|
40
|
+
annotations, elaborations, or other modifications represent, as a whole, an
|
|
41
|
+
original work of authorship. For the purposes of this License, Derivative Works
|
|
42
|
+
shall not include works that remain separable from, or merely link (or bind by
|
|
43
|
+
name) to the interfaces of, the Work and Derivative Works thereof.
|
|
44
|
+
|
|
45
|
+
"Contribution" shall mean any work of authorship, including the original version
|
|
46
|
+
of the Work and any modifications or additions to that Work or Derivative Works
|
|
47
|
+
thereof, that is intentionally submitted to Licensor for inclusion in the Work by
|
|
48
|
+
the copyright owner or by an individual or Legal Entity authorized to submit on
|
|
49
|
+
behalf of the copyright owner. For the purposes of this definition, "submitted"
|
|
50
|
+
means any form of electronic, verbal, or written communication sent to the
|
|
51
|
+
Licensor or its representatives, including but not limited to communication on
|
|
52
|
+
electronic mailing lists, source code control systems, and issue tracking
|
|
53
|
+
systems that are managed by, or on behalf of, the Licensor for the purpose of
|
|
54
|
+
discussing and improving the Work, but excluding communication that is
|
|
55
|
+
conspicuously marked or otherwise designated in writing by the copyright owner
|
|
56
|
+
as "Not a Contribution."
|
|
57
|
+
|
|
58
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf
|
|
59
|
+
of whom a Contribution has been received by Licensor and subsequently
|
|
60
|
+
incorporated within the Work.
|
|
61
|
+
|
|
62
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this
|
|
63
|
+
License, each Contributor hereby grants to You a perpetual, worldwide,
|
|
64
|
+
non-exclusive, no-charge, royalty-free, irrevocable copyright license to
|
|
65
|
+
reproduce, prepare Derivative Works of, publicly display, publicly perform,
|
|
66
|
+
sublicense, and distribute the Work and such Derivative Works in Source or
|
|
67
|
+
Object form.
|
|
68
|
+
|
|
69
|
+
3. Grant of Patent License. Subject to the terms and conditions of this License,
|
|
70
|
+
each Contributor hereby grants to You a perpetual, worldwide, non-exclusive,
|
|
71
|
+
no-charge, royalty-free, irrevocable (except as stated in this section) patent
|
|
72
|
+
license to make, have made, use, offer to sell, sell, import, and otherwise
|
|
73
|
+
transfer the Work, where such license applies only to those patent claims
|
|
74
|
+
licensable by such Contributor that are necessarily infringed by their
|
|
75
|
+
Contribution(s) alone or by combination of their Contribution(s) with the Work to
|
|
76
|
+
which such Contribution(s) was submitted. If You institute patent litigation
|
|
77
|
+
against any entity (including a cross-claim or counterclaim in a lawsuit)
|
|
78
|
+
alleging that the Work or a Contribution incorporated within the Work
|
|
79
|
+
constitutes direct or contributory patent infringement, then any patent licenses
|
|
80
|
+
granted to You under this License for that Work shall terminate as of the date
|
|
81
|
+
such litigation is filed.
|
|
82
|
+
|
|
83
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or
|
|
84
|
+
Derivative Works thereof in any medium, with or without modifications, and in
|
|
85
|
+
Source or Object form, provided that You meet the following conditions:
|
|
86
|
+
|
|
87
|
+
(a) You must give any other recipients of the Work or Derivative Works a copy of
|
|
88
|
+
this License; and
|
|
89
|
+
|
|
90
|
+
(b) You must cause any modified files to carry prominent notices stating that You
|
|
91
|
+
changed the files; and
|
|
92
|
+
|
|
93
|
+
(c) You must retain, in the Source form of any Derivative Works that You
|
|
94
|
+
distribute, all copyright, patent, trademark, and attribution notices from the
|
|
95
|
+
Source form of the Work, excluding those notices that do not pertain to any part
|
|
96
|
+
of the Derivative Works; and
|
|
97
|
+
|
|
98
|
+
(d) If the Work includes a "NOTICE" text file as part of its distribution, then
|
|
99
|
+
any Derivative Works that You distribute must include a readable copy of the
|
|
100
|
+
attribution notices contained within such NOTICE file, excluding those notices
|
|
101
|
+
that do not pertain to any part of the Derivative Works, in at least one of the
|
|
102
|
+
following places: within a NOTICE text file distributed as part of the Derivative
|
|
103
|
+
Works; within the Source form or documentation, if provided along with the
|
|
104
|
+
Derivative Works; or, within a display generated by the Derivative Works, if and
|
|
105
|
+
wherever such third-party notices normally appear. The contents of the NOTICE
|
|
106
|
+
file are for informational purposes only and do not modify the License. You may
|
|
107
|
+
add Your own attribution notices within Derivative Works that You distribute,
|
|
108
|
+
alongside or as an addendum to the NOTICE text from the Work, provided that such
|
|
109
|
+
additional attribution notices cannot be construed as modifying the License.
|
|
110
|
+
|
|
111
|
+
You may add Your own copyright statement to Your modifications and may provide
|
|
112
|
+
additional or different license terms and conditions for use, reproduction, or
|
|
113
|
+
distribution of Your modifications, or for any such Derivative Works as a whole,
|
|
114
|
+
provided Your use, reproduction, and distribution of the Work otherwise complies
|
|
115
|
+
with the conditions stated in this License.
|
|
116
|
+
|
|
117
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any
|
|
118
|
+
Contribution intentionally submitted for inclusion in the Work by You to the
|
|
119
|
+
Licensor shall be under the terms and conditions of this License, without any
|
|
120
|
+
additional terms or conditions. Notwithstanding the above, nothing herein shall
|
|
121
|
+
supersede or modify the terms of any separate license agreement you may have
|
|
122
|
+
executed with Licensor regarding such Contributions.
|
|
123
|
+
|
|
124
|
+
6. Trademarks. This License does not grant permission to use the trade names,
|
|
125
|
+
trademarks, service marks, or product names of the Licensor, except as required
|
|
126
|
+
for reasonable and customary use in describing the origin of the Work and
|
|
127
|
+
reproducing the content of the NOTICE file.
|
|
128
|
+
|
|
129
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in
|
|
130
|
+
writing, Licensor provides the Work (and each Contributor provides its
|
|
131
|
+
Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
132
|
+
KIND, either express or implied, including, without limitation, any warranties or
|
|
133
|
+
conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
134
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
135
|
+
appropriateness of using or redistributing the Work and assume any risks
|
|
136
|
+
associated with Your exercise of permissions under this License.
|
|
137
|
+
|
|
138
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in
|
|
139
|
+
tort (including negligence), contract, or otherwise, unless required by
|
|
140
|
+
applicable law (such as deliberate and grossly negligent acts) or agreed to in
|
|
141
|
+
writing, shall any Contributor be liable to You for damages, including any
|
|
142
|
+
direct, indirect, special, incidental, or consequential damages of any character
|
|
143
|
+
arising as a result of this License or out of the use or inability to use the
|
|
144
|
+
Work (including but not limited to damages for loss of goodwill, work stoppage,
|
|
145
|
+
computer failure or malfunction, or any and all other commercial damages or
|
|
146
|
+
losses), even if such Contributor has been advised of the possibility of such
|
|
147
|
+
damages.
|
|
148
|
+
|
|
149
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work or
|
|
150
|
+
Derivative Works thereof, You may choose to offer, and charge a fee for,
|
|
151
|
+
acceptance of support, warranty, indemnity, or other liability obligations
|
|
152
|
+
and/or rights consistent with this License. However, in accepting such
|
|
153
|
+
obligations, You may act only on Your own behalf and on Your sole
|
|
154
|
+
responsibility, not on behalf of any other Contributor, and only if You agree to
|
|
155
|
+
indemnify, defend, and hold each Contributor harmless for any liability incurred
|
|
156
|
+
by, or claims asserted against, such Contributor by reason of your accepting any
|
|
157
|
+
such warranty or additional liability.
|
|
158
|
+
|
|
159
|
+
END OF TERMS AND CONDITIONS
|
|
160
|
+
|
|
161
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
162
|
+
|
|
163
|
+
To apply the Apache License to your work, attach the following boilerplate
|
|
164
|
+
notice, with the fields enclosed by brackets "[]" replaced with your own
|
|
165
|
+
identifying information. (Don't include the brackets!) The text should be
|
|
166
|
+
enclosed in the appropriate comment syntax for the file format. We also
|
|
167
|
+
recommend that a file or class name and description of purpose be included on
|
|
168
|
+
the same "printed page" as the copyright notice for easier identification within
|
|
169
|
+
third-party archives.
|
|
170
|
+
|
|
171
|
+
Copyright 2026 bstnxbt
|
|
172
|
+
|
|
173
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
174
|
+
this file except in compliance with the License. You may obtain a copy of the
|
|
175
|
+
License at
|
|
176
|
+
|
|
177
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
178
|
+
|
|
179
|
+
Unless required by applicable law or agreed to in writing, software distributed
|
|
180
|
+
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
181
|
+
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
182
|
+
specific language governing permissions and limitations under the License.
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: dspark-mlx
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: DSpark self-speculative decoding for Apple Silicon (MLX) — lossless, target-agnostic
|
|
5
|
+
Author-email: popfido <wanghailin317@gmail.com>
|
|
6
|
+
Maintainer-email: popfido <wanghailin317@gmail.com>
|
|
7
|
+
License: Apache-2.0
|
|
8
|
+
Project-URL: Homepage, https://github.com/popfido/dspark-mlx
|
|
9
|
+
Project-URL: Issues, https://github.com/popfido/dspark-mlx/issues
|
|
10
|
+
Keywords: mlx,apple-silicon,speculative-decoding,dspark,llm-inference,deepseek,qwen3,gemma
|
|
11
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
14
|
+
Classifier: Operating System :: MacOS
|
|
15
|
+
Requires-Python: >=3.10
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Requires-Dist: mlx>=0.31.0
|
|
19
|
+
Requires-Dist: mlx-lm>=0.31.0
|
|
20
|
+
Requires-Dist: mlx-vlm>=0.6.0
|
|
21
|
+
Requires-Dist: numpy
|
|
22
|
+
Provides-Extra: bench
|
|
23
|
+
Requires-Dist: datasets; extra == "bench"
|
|
24
|
+
Provides-Extra: dev
|
|
25
|
+
Requires-Dist: pytest; extra == "dev"
|
|
26
|
+
Requires-Dist: torch; extra == "dev"
|
|
27
|
+
Requires-Dist: numpy; extra == "dev"
|
|
28
|
+
Requires-Dist: datasets; extra == "dev"
|
|
29
|
+
Dynamic: license-file
|
|
30
|
+
|
|
31
|
+
# dspark-mlx
|
|
32
|
+
|
|
33
|
+
Target-agnostic MLX implementation of DeepSeek **DSpark** self-speculative decoding.
|
|
34
|
+
|
|
35
|
+
DSpark drafts a block of tokens from a small EAGLE-style draft model (projected target
|
|
36
|
+
hidden states + a low-rank Markov logit bias + a per-token confidence head), which the
|
|
37
|
+
host base model then verifies **losslessly**. This package owns the DSpark draft stack and
|
|
38
|
+
the verify/accept policy; the base model is supplied by the host through a small adapter
|
|
39
|
+
(`dspark_mlx/adapter.py`). The emitted stream is identical to greedy decoding from the base
|
|
40
|
+
model alone.
|
|
41
|
+
|
|
42
|
+
## Install
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
pip install dspark-mlx # Qwen3 (mlx-lm) and Gemma (mlx-vlm) both supported out of the box
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
dspark generate --model qwen3-4b "Explain speculative decoding." --quant-draft 8 --no-think
|
|
50
|
+
dspark bench --model qwen3-4b --quant-draft 8 --no-think
|
|
51
|
+
dspark eval --model qwen3-4b --dataset gsm8k --n 20
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
The package itself is tiny; `dspark` downloads the DSpark draft + the deployed **instruct** base
|
|
55
|
+
on first use (some bases — e.g. `google/gemma-4-12b-it` — are gated and large, ~24 GB).
|
|
56
|
+
|
|
57
|
+
## Architectures
|
|
58
|
+
|
|
59
|
+
One DSpark recipe, three base-model backbones — selected by `model_type` via the registry
|
|
60
|
+
(`dspark_mlx/registry.py`), mirroring `dflash-mlx`'s `TARGET_BACKENDS`:
|
|
61
|
+
|
|
62
|
+
| Backbone | Checkpoint | Draft layer body |
|
|
63
|
+
|---|---|---|
|
|
64
|
+
| `deepseek_v4` | `DeepSeek-V4-Flash-DSpark` (bundled fp8/fp4, `mtp.*`) | MLA + hash-MoE + Hyper-Connections + windowed sparse attn |
|
|
65
|
+
| `qwen3` | `dspark_qwen3_{4b,8b,14b}_block7` (standalone bf16, `layers.*`) | Qwen3 GQA + QK-norm + SwiGLU |
|
|
66
|
+
| `gemma4` | `dspark_gemma4_12b_block7` (standalone bf16, `layers.*`) | Gemma4 GQA (K=V) + sandwich norms + GeGLU + partial RoPE + softcap |
|
|
67
|
+
|
|
68
|
+
Add an architecture: implement a `DraftArch` (build + key_map) in `dspark_mlx/arch/<name>.py`
|
|
69
|
+
and append it to `ARCH_REGISTRY` — `generate`/`verify`/`adapter` are unchanged.
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
from dspark_mlx import resolve_arch, load_drafter, generate
|
|
73
|
+
|
|
74
|
+
arch = resolve_arch(config) # by config["model_type"]
|
|
75
|
+
drafter = arch.build(config, max_seq_len=...)
|
|
76
|
+
load_drafter(drafter, weights, key_map=arch.key_map)
|
|
77
|
+
for event in generate(adapter, drafter, prompt_tokens, max_new_tokens):
|
|
78
|
+
...
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Benchmarks
|
|
82
|
+
|
|
83
|
+
Lossless speedup on Apple Silicon (eager loop, greedy) and average **accepted length**
|
|
84
|
+
(τ = tokens per verify step) vs the DSpark paper — the hardware-independent metric the paper
|
|
85
|
+
reports. Both backbones land in the paper's acceptance band. Full methodology, all precisions,
|
|
86
|
+
and findings in **[BENCHMARK.md](https://github.com/popfido/dspark-mlx/blob/main/BENCHMARK.md)**.
|
|
87
|
+
|
|
88
|
+
| model | accepted length (GSM8K) | acceptance rate | speedup |
|
|
89
|
+
|---|---|---|---|
|
|
90
|
+
| Qwen3-4B (bf16) | 3.86 / 6.27† | 41% / 75%† | 1.17× |
|
|
91
|
+
| Qwen3-14B (bf16) | 3.79 / 6.29† | 40% / 76%† | **1.98×**† |
|
|
92
|
+
| Gemma4-12B-it (bf16) | **5.84** | **69%** | **2.06×** |
|
|
93
|
+
|
|
94
|
+
†thinking off (`--no-think`). Two knobs: **acceptance length** is set by the draft + task
|
|
95
|
+
(≈ size-independent — 4B ≈ 14B), while **speedup** is set by how expensive the base is (a
|
|
96
|
+
costlier base amortizes the draft better, so Qwen3-4B 1.17× → 14B 1.98× → Gemma-12B 2.06×).
|
|
97
|
+
**Quantize the draft** (`--quant-draft 8`, acceptance-lossless, ½ the draft size) for a free
|
|
98
|
+
~10–20% on top of any base — Qwen3-4B bf16 1.26× → **1.67×**, 8-bit base 1.34× → **1.62×**,
|
|
99
|
+
Qwen3-14B 2.07× → **2.36×** (see BENCHMARK.md for the base×draft-precision table). The draft must
|
|
100
|
+
target the deployed **instruct** model (the pretrained Gemma base gives ~3× lower acceptance);
|
|
101
|
+
Qwen3's `<think>` traces roughly halve acceptance vs `--no-think`.
|
|
102
|
+
|
|
103
|
+
Based on `deepseek-ai/DeepSeek-V4-Flash-DSpark` and the DeepSpec codebase (`dspark/*`).
|
|
104
|
+
Repo structure mirrors `dflash-mlx`.
|
|
105
|
+
|
|
106
|
+
Status: the full draft→verify→accept pipeline is verified against the reference for all
|
|
107
|
+
three backbones (parity on tiny weights; lossless end-to-end against toy bases; real Qwen3
|
|
108
|
+
/ Gemma4 checkpoints load and run). See the test suite.
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# dspark-mlx
|
|
2
|
+
|
|
3
|
+
Target-agnostic MLX implementation of DeepSeek **DSpark** self-speculative decoding.
|
|
4
|
+
|
|
5
|
+
DSpark drafts a block of tokens from a small EAGLE-style draft model (projected target
|
|
6
|
+
hidden states + a low-rank Markov logit bias + a per-token confidence head), which the
|
|
7
|
+
host base model then verifies **losslessly**. This package owns the DSpark draft stack and
|
|
8
|
+
the verify/accept policy; the base model is supplied by the host through a small adapter
|
|
9
|
+
(`dspark_mlx/adapter.py`). The emitted stream is identical to greedy decoding from the base
|
|
10
|
+
model alone.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pip install dspark-mlx # Qwen3 (mlx-lm) and Gemma (mlx-vlm) both supported out of the box
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
dspark generate --model qwen3-4b "Explain speculative decoding." --quant-draft 8 --no-think
|
|
20
|
+
dspark bench --model qwen3-4b --quant-draft 8 --no-think
|
|
21
|
+
dspark eval --model qwen3-4b --dataset gsm8k --n 20
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
The package itself is tiny; `dspark` downloads the DSpark draft + the deployed **instruct** base
|
|
25
|
+
on first use (some bases — e.g. `google/gemma-4-12b-it` — are gated and large, ~24 GB).
|
|
26
|
+
|
|
27
|
+
## Architectures
|
|
28
|
+
|
|
29
|
+
One DSpark recipe, three base-model backbones — selected by `model_type` via the registry
|
|
30
|
+
(`dspark_mlx/registry.py`), mirroring `dflash-mlx`'s `TARGET_BACKENDS`:
|
|
31
|
+
|
|
32
|
+
| Backbone | Checkpoint | Draft layer body |
|
|
33
|
+
|---|---|---|
|
|
34
|
+
| `deepseek_v4` | `DeepSeek-V4-Flash-DSpark` (bundled fp8/fp4, `mtp.*`) | MLA + hash-MoE + Hyper-Connections + windowed sparse attn |
|
|
35
|
+
| `qwen3` | `dspark_qwen3_{4b,8b,14b}_block7` (standalone bf16, `layers.*`) | Qwen3 GQA + QK-norm + SwiGLU |
|
|
36
|
+
| `gemma4` | `dspark_gemma4_12b_block7` (standalone bf16, `layers.*`) | Gemma4 GQA (K=V) + sandwich norms + GeGLU + partial RoPE + softcap |
|
|
37
|
+
|
|
38
|
+
Add an architecture: implement a `DraftArch` (build + key_map) in `dspark_mlx/arch/<name>.py`
|
|
39
|
+
and append it to `ARCH_REGISTRY` — `generate`/`verify`/`adapter` are unchanged.
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
from dspark_mlx import resolve_arch, load_drafter, generate
|
|
43
|
+
|
|
44
|
+
arch = resolve_arch(config) # by config["model_type"]
|
|
45
|
+
drafter = arch.build(config, max_seq_len=...)
|
|
46
|
+
load_drafter(drafter, weights, key_map=arch.key_map)
|
|
47
|
+
for event in generate(adapter, drafter, prompt_tokens, max_new_tokens):
|
|
48
|
+
...
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Benchmarks
|
|
52
|
+
|
|
53
|
+
Lossless speedup on Apple Silicon (eager loop, greedy) and average **accepted length**
|
|
54
|
+
(τ = tokens per verify step) vs the DSpark paper — the hardware-independent metric the paper
|
|
55
|
+
reports. Both backbones land in the paper's acceptance band. Full methodology, all precisions,
|
|
56
|
+
and findings in **[BENCHMARK.md](https://github.com/popfido/dspark-mlx/blob/main/BENCHMARK.md)**.
|
|
57
|
+
|
|
58
|
+
| model | accepted length (GSM8K) | acceptance rate | speedup |
|
|
59
|
+
|---|---|---|---|
|
|
60
|
+
| Qwen3-4B (bf16) | 3.86 / 6.27† | 41% / 75%† | 1.17× |
|
|
61
|
+
| Qwen3-14B (bf16) | 3.79 / 6.29† | 40% / 76%† | **1.98×**† |
|
|
62
|
+
| Gemma4-12B-it (bf16) | **5.84** | **69%** | **2.06×** |
|
|
63
|
+
|
|
64
|
+
†thinking off (`--no-think`). Two knobs: **acceptance length** is set by the draft + task
|
|
65
|
+
(≈ size-independent — 4B ≈ 14B), while **speedup** is set by how expensive the base is (a
|
|
66
|
+
costlier base amortizes the draft better, so Qwen3-4B 1.17× → 14B 1.98× → Gemma-12B 2.06×).
|
|
67
|
+
**Quantize the draft** (`--quant-draft 8`, acceptance-lossless, ½ the draft size) for a free
|
|
68
|
+
~10–20% on top of any base — Qwen3-4B bf16 1.26× → **1.67×**, 8-bit base 1.34× → **1.62×**,
|
|
69
|
+
Qwen3-14B 2.07× → **2.36×** (see BENCHMARK.md for the base×draft-precision table). The draft must
|
|
70
|
+
target the deployed **instruct** model (the pretrained Gemma base gives ~3× lower acceptance);
|
|
71
|
+
Qwen3's `<think>` traces roughly halve acceptance vs `--no-think`.
|
|
72
|
+
|
|
73
|
+
Based on `deepseek-ai/DeepSeek-V4-Flash-DSpark` and the DeepSpec codebase (`dspark/*`).
|
|
74
|
+
Repo structure mirrors `dflash-mlx`.
|
|
75
|
+
|
|
76
|
+
Status: the full draft→verify→accept pipeline is verified against the reference for all
|
|
77
|
+
three backbones (parity on tiny weights; lossless end-to-end against toy bases; real Qwen3
|
|
78
|
+
/ Gemma4 checkpoints load and run). See the test suite.
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# Copyright 2026 popfido
|
|
2
|
+
# Licensed under the Apache License, Version 2.0 - see LICENSE file
|
|
3
|
+
# Based on DeepSeek DSpark (DeepSeek-V4-Flash-DSpark, deepseek-ai/DeepSpec)
|
|
4
|
+
|
|
5
|
+
__version__ = "0.1.0"
|
|
6
|
+
|
|
7
|
+
from .adapter import BaseModelAdapter, BlockOut, StepOut
|
|
8
|
+
from .arch.backbone import DraftArch, DraftBackbone
|
|
9
|
+
from .events import SummaryEvent, TokenEvent
|
|
10
|
+
from .generate import generate
|
|
11
|
+
from .loader import KNOWN_MODELS, load_draft, load_host, resolve_model
|
|
12
|
+
from .loading import is_dspark_checkpoint, load_drafter, map_checkpoint_key
|
|
13
|
+
from .loop import generate_eager
|
|
14
|
+
from .model.config import DSparkArgs
|
|
15
|
+
from .model.drafter import DSparkDrafter
|
|
16
|
+
from .quant import quantize_drafter
|
|
17
|
+
from .registry import ARCH_REGISTRY, resolve_arch
|
|
18
|
+
from .verify import AcceptResult, greedy_accept, speculative_sample_accept
|
|
19
|
+
|
|
20
|
+
__all__ = [
|
|
21
|
+
"BaseModelAdapter",
|
|
22
|
+
"BlockOut",
|
|
23
|
+
"StepOut",
|
|
24
|
+
"DSparkArgs",
|
|
25
|
+
"DSparkDrafter",
|
|
26
|
+
"DraftArch",
|
|
27
|
+
"DraftBackbone",
|
|
28
|
+
"resolve_arch",
|
|
29
|
+
"ARCH_REGISTRY",
|
|
30
|
+
"generate",
|
|
31
|
+
"generate_eager",
|
|
32
|
+
"load_draft",
|
|
33
|
+
"load_host",
|
|
34
|
+
"resolve_model",
|
|
35
|
+
"KNOWN_MODELS",
|
|
36
|
+
"greedy_accept",
|
|
37
|
+
"speculative_sample_accept",
|
|
38
|
+
"AcceptResult",
|
|
39
|
+
"TokenEvent",
|
|
40
|
+
"SummaryEvent",
|
|
41
|
+
"load_drafter",
|
|
42
|
+
"map_checkpoint_key",
|
|
43
|
+
"is_dspark_checkpoint",
|
|
44
|
+
"quantize_drafter",
|
|
45
|
+
]
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# Copyright 2026 popfido
|
|
2
|
+
# Licensed under the Apache License, Version 2.0 - see LICENSE file
|
|
3
|
+
# Based on DeepSeek DSpark (DeepSeek-V4-Flash-DSpark, deepseek-ai/DeepSpec)
|
|
4
|
+
|
|
5
|
+
"""The seam between dspark-mlx (drafter + verify/accept loop) and a host base model.
|
|
6
|
+
|
|
7
|
+
dspark-mlx is target-agnostic: it owns the DSpark draft stack and the lossless
|
|
8
|
+
accept policy, but never the base model. The host (e.g. omlx over its
|
|
9
|
+
``patches/deepseek_v4`` model) implements :class:`BaseModelAdapter` so the drafter
|
|
10
|
+
can (a) read the ``main_hidden`` it conditions on, (b) get the base distribution for
|
|
11
|
+
each candidate token during verify, and (c) snapshot/roll back base KV when a block is
|
|
12
|
+
only partially accepted.
|
|
13
|
+
|
|
14
|
+
Logit conventions (one decode cycle):
|
|
15
|
+
- ``prefill`` / ``decode_step`` return ``StepOut.logits`` = ``p_1``, the base
|
|
16
|
+
distribution for the *first* drafted token. It is free — already computed by the
|
|
17
|
+
step that produced the anchor — so the verify forward never recomputes it.
|
|
18
|
+
- ``verify_forward`` runs ONE base forward over the K drafted tokens and returns the
|
|
19
|
+
K base distributions ``p_2 .. p_{K+1}`` (``p_{K+1}`` is the bonus position).
|
|
20
|
+
- The generate loop concatenates ``[p_1] + [p_2..p_{K+1}]`` into the ``[K+1, V]`` block
|
|
21
|
+
the accept policy consumes (see :mod:`dspark_mlx.verify`).
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
from __future__ import annotations
|
|
25
|
+
|
|
26
|
+
from dataclasses import dataclass
|
|
27
|
+
from typing import Any, Protocol, Tuple, runtime_checkable
|
|
28
|
+
|
|
29
|
+
import mlx.core as mx
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@dataclass
|
|
33
|
+
class StepOut:
|
|
34
|
+
"""Output of a single base forward at the anchor position."""
|
|
35
|
+
|
|
36
|
+
logits: mx.array # [b, V] base distribution for the next (first drafted) token
|
|
37
|
+
main_hidden: mx.array # [b, dim * len(target_layer_ids)] concat of target-layer hiddens
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@dataclass
|
|
41
|
+
class BlockOut:
|
|
42
|
+
"""Output of the base verify forward over a K-token draft block."""
|
|
43
|
+
|
|
44
|
+
per_pos_logits: mx.array # [b, K, V] base distributions p_2 .. p_{K+1}
|
|
45
|
+
per_pos_main_hidden: mx.array # [b, K, D] main hidden at each verified position
|
|
46
|
+
main_hidden_last: mx.array # [b, D] convenience alias for the last verified position
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@runtime_checkable
|
|
50
|
+
class BaseModelAdapter(Protocol):
|
|
51
|
+
"""Host contract. Implementations own the base model and its KV cache."""
|
|
52
|
+
|
|
53
|
+
#: Main-model layer indices whose hidden states are concatenated into ``main_hidden``.
|
|
54
|
+
target_layer_ids: Tuple[int, ...]
|
|
55
|
+
|
|
56
|
+
def prefill(self, tokens: mx.array) -> StepOut:
|
|
57
|
+
"""Process the prompt; return logits for the first generated token + main_hidden."""
|
|
58
|
+
...
|
|
59
|
+
|
|
60
|
+
def decode_step(self, token: mx.array) -> StepOut:
|
|
61
|
+
"""Advance one token; return its next-token logits + main_hidden."""
|
|
62
|
+
...
|
|
63
|
+
|
|
64
|
+
def verify_forward(self, block_tokens: mx.array) -> BlockOut:
|
|
65
|
+
"""Run one base forward over K draft tokens; return p_2..p_{K+1} + main_hidden_last.
|
|
66
|
+
|
|
67
|
+
Appends K entries to the base KV cache speculatively; the caller rolls back the
|
|
68
|
+
rejected tail via :meth:`kv_rollback`.
|
|
69
|
+
"""
|
|
70
|
+
...
|
|
71
|
+
|
|
72
|
+
def kv_snapshot(self) -> Any:
|
|
73
|
+
"""Opaque handle capturing base KV state before a speculative block."""
|
|
74
|
+
...
|
|
75
|
+
|
|
76
|
+
def kv_rollback(self, n_keep: int) -> None:
|
|
77
|
+
"""Drop speculatively-appended KV beyond ``n_keep`` accepted tokens."""
|
|
78
|
+
...
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# Copyright 2026 popfido
|
|
2
|
+
# Licensed under the Apache License, Version 2.0 - see LICENSE file
|
|
3
|
+
# Based on DeepSeek DSpark (DeepSeek-V4-Flash-DSpark, deepseek-ai/DeepSpec)
|
|
4
|
+
|
|
5
|
+
"""The per-architecture seam for DSpark drafters.
|
|
6
|
+
|
|
7
|
+
DSpark ships one recipe (EAGLE-style context projection + Markov bias + confidence head +
|
|
8
|
+
block drafting) realized over different base-model decoder layers — DeepSeek-V4 (windowed
|
|
9
|
+
MLA + MoE + Hyper-Connections, bundled fp8/fp4 ``mtp.*`` checkpoint), Qwen3 and Gemma4
|
|
10
|
+
(standalone bf16 ``layers.*`` checkpoints, full-context GQA). ``generate()`` drives any of
|
|
11
|
+
them through the ``DraftBackbone`` interface; a ``DraftArch`` descriptor registers how to
|
|
12
|
+
build and load each one (see :mod:`dspark_mlx.registry`).
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
from dataclasses import dataclass
|
|
18
|
+
from typing import Any, Callable, Optional, Protocol, Tuple, runtime_checkable
|
|
19
|
+
|
|
20
|
+
import mlx.core as mx
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@runtime_checkable
|
|
24
|
+
class DraftBackbone(Protocol):
|
|
25
|
+
"""A loaded DSpark drafter for one base architecture (what ``generate`` consumes)."""
|
|
26
|
+
|
|
27
|
+
block_size: int
|
|
28
|
+
|
|
29
|
+
def forward_spec(
|
|
30
|
+
self, input_ids: mx.array, main_hidden: mx.array, start_pos: int = 0
|
|
31
|
+
) -> Optional[Tuple[mx.array, mx.array, mx.array]]:
|
|
32
|
+
"""Prefill (start_pos==0) seeds context; decode drafts (ids, logits, confidence)."""
|
|
33
|
+
...
|
|
34
|
+
|
|
35
|
+
def advance(self, main_hidden: mx.array, position: int) -> None:
|
|
36
|
+
"""Slide the drafter's context over one committed token."""
|
|
37
|
+
...
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@dataclass(frozen=True)
|
|
41
|
+
class DraftArch:
|
|
42
|
+
"""Registry entry: how to build + load a DSpark drafter for a base architecture."""
|
|
43
|
+
|
|
44
|
+
name: str
|
|
45
|
+
model_types: Tuple[str, ...]
|
|
46
|
+
build: Callable[..., DraftBackbone] # (config: dict, *, max_seq_len) -> DraftBackbone
|
|
47
|
+
key_map: Callable[[str], Optional[str]] # checkpoint key -> drafter param path (or None)
|
|
48
|
+
|
|
49
|
+
def supports(self, model_type: Optional[str]) -> bool:
|
|
50
|
+
return model_type in self.model_types
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def config_model_type(config: Any) -> Optional[str]:
|
|
54
|
+
"""Read ``model_type`` from a dict-like or attribute-like config."""
|
|
55
|
+
if isinstance(config, dict):
|
|
56
|
+
return config.get("model_type")
|
|
57
|
+
return getattr(config, "model_type", None)
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# Copyright 2026 popfido
|
|
2
|
+
# Licensed under the Apache License, Version 2.0 - see LICENSE file
|
|
3
|
+
# Based on DeepSeek DSpark (DeepSeek-V4-Flash-DSpark, deepseek-ai/DeepSpec)
|
|
4
|
+
|
|
5
|
+
"""DeepSeek-V4-Flash-DSpark backbone descriptor.
|
|
6
|
+
|
|
7
|
+
The windowed MLA + hash-MoE + Hyper-Connections realization, drafting from the ``mtp.*``
|
|
8
|
+
namespace of the bundled fp8/fp4 checkpoint. The model code lives under ``dspark_mlx.model``
|
|
9
|
+
(its parity tests pin it); this module just registers it as a DraftArch.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
from typing import Optional
|
|
15
|
+
|
|
16
|
+
from ..loading import map_checkpoint_key
|
|
17
|
+
from ..model.config import DSparkArgs
|
|
18
|
+
from ..model.drafter import DSparkDrafter
|
|
19
|
+
from .backbone import DraftArch, DraftBackbone
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def build(config: dict, *, max_seq_len: int = 8192) -> DraftBackbone:
|
|
23
|
+
return DSparkDrafter(DSparkArgs.from_dict(config), max_seq_len=max_seq_len)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def key_map(key: str) -> Optional[str]:
|
|
27
|
+
return map_checkpoint_key(key) # mtp.N.* -> blocks.N.*, embed/head pass through
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
DEEPSEEK_V4 = DraftArch(
|
|
31
|
+
name="deepseek_v4",
|
|
32
|
+
model_types=("deepseek_v4",),
|
|
33
|
+
build=build,
|
|
34
|
+
key_map=key_map,
|
|
35
|
+
)
|