dflash-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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 bstnxbt
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,149 @@
1
+ Metadata-Version: 2.4
2
+ Name: dflash-mlx
3
+ Version: 0.1.0
4
+ Summary: DFlash speculative decoding for Apple Silicon (MLX)
5
+ Author: bstnxbt
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/bstnxbt/dflash-mlx
8
+ Requires-Python: >=3.10
9
+ Description-Content-Type: text/markdown
10
+ License-File: LICENSE
11
+ Requires-Dist: mlx>=0.25.0
12
+ Requires-Dist: mlx-lm>=0.22.0
13
+ Dynamic: license-file
14
+
15
+ <p align="center">
16
+ <h1 align="center">dflash-mlx</h1>
17
+ <p align="center">DFlash speculative decoding for Apple Silicon</p>
18
+ </p>
19
+
20
+ <p align="center">
21
+ <img src="https://img.shields.io/badge/platform-Apple%20Silicon-black?logo=apple" alt="Apple Silicon">
22
+ <img src="https://img.shields.io/badge/python-3.10%2B-blue?logo=python" alt="Python 3.10+">
23
+ <img src="https://img.shields.io/badge/license-MIT-green" alt="License">
24
+ <img src="https://img.shields.io/badge/MLX-stock-red" alt="Stock MLX">
25
+ </p>
26
+
27
+ Paper: [DFlash: Block Diffusion for Flash Speculative Decoding](https://arxiv.org/abs/2602.06036) (Chen et al., 2026)
28
+
29
+ Block-diffusion draft generates 16 tokens in one pass. Target verifies in one pass. Output is lossless — every emitted token is verified against the target model before it is committed.
30
+
31
+ ## How it works
32
+
33
+ - A small draft model (~1B params) generates 16 tokens in parallel with block diffusion.
34
+ - The target model verifies those 16 tokens in a single forward pass.
35
+ - Greedy acceptance keeps the correct prefix and rejects the rest.
36
+ - Lossless: every emitted token is the target model's greedy argmax at verification time. Output can still differ from pure AR because of MLX dispatch divergence, but no unverified token is ever emitted.
37
+ - Uses stock MLX plus a small number of targeted kernels where rollback and long-context verify need tighter numerical control.
38
+
39
+ ## Technical details
40
+
41
+ - **Tape-replay rollback**: instead of snapshotting and restoring the full GatedDeltaNet state, dflash-mlx records an innovation tape during verify and replays only the accepted steps through a custom Metal kernel. Keeps rollback cost low and preserves acceptance over long generations.
42
+ - **JIT SDPA 2-pass**: long-context verify (`N >= 1024`) uses a custom Metal attention kernel that stays numerically aligned with stock MLX attention.
43
+ - **Numerical coherence**: bf16-sensitive paths, including recurrent state replay and small projections, are stabilized across speculative cycles so accepted tokens stay consistent.
44
+
45
+ ## Benchmarks
46
+
47
+ | Model | Tokens | Baseline | DFlash | Speedup | Acceptance |
48
+ |-------|--------|----------|--------|---------|------------|
49
+ | Qwen3.5-4B | 1024 | 53.48 tok/s | 197.49 tok/s | 3.69x | 88.67% |
50
+ | Qwen3.5-4B | 2048 | 53.74 tok/s | 219.83 tok/s | 4.10x | 89.26% |
51
+ | Qwen3.5-4B | 4096 | 53.58 tok/s | 155.19 tok/s | 2.83x | 87.55% |
52
+ | Qwen3.5-9B | 1024 | 31.09 tok/s | 127.47 tok/s | 4.10x | 88.96% |
53
+ | Qwen3.5-9B | 2048 | 30.96 tok/s | 127.07 tok/s | 4.13x | 89.36% |
54
+ | Qwen3.5-9B | 4096 | 31.58 tok/s | 103.90 tok/s | 3.29x | 88.57% |
55
+ | Qwen3.5-27B-4bit | 1024 | 33.24 tok/s | 65.80 tok/s | 1.98x | 89.45% |
56
+ | Qwen3.5-27B-4bit | 2048 | 32.35 tok/s | 62.78 tok/s | 1.90x | 89.11% |
57
+ | Qwen3.5-27B-4bit | 4096 | 29.38 tok/s | 48.89 tok/s | 1.66x | 87.99% |
58
+ | Qwen3.5-35B-A3B-4bit | 1024 | 139.97 tok/s | 242.92 tok/s | 1.74x | 89.26% |
59
+ | Qwen3.5-35B-A3B-4bit | 2048 | 142.12 tok/s | 240.21 tok/s | 1.69x | 88.67% |
60
+ | Qwen3.5-35B-A3B-4bit | 4096 | 140.73 tok/s | 189.62 tok/s | 1.35x | 86.96% |
61
+
62
+ ### Methodology
63
+
64
+ Hardware: Apple M5 Max, 64GB unified memory. MLX 0.31.1 from the stock pip install.
65
+
66
+ Protocol: stock `mlx_lm.stream_generate` on a pristine target model vs stock MLX plus the local DFlash runtime, measured sequentially. `3` repeats, median reported, `10s` cooldown between measured runs.
67
+
68
+ Generation: prompt `"The function $f$ satisfies the functional equation \[ f(x) + f(y) = f(x + y) - xy - 1 \] for all real numbers $x$ and $y$. If $f(1) = 1$, then find all integers $n$ such that $f(n) = n$. Enter all such integers, separated by commas. Please reason step by step, and put your final answer within \boxed{}."` with chat templates enabled by default, `--no-eos`, and post-prefill tok/s as the primary metric.
69
+
70
+ Full per-run JSON reports are available in [`benchmark/results/`](benchmark/results/).
71
+
72
+ ## Install
73
+
74
+ ```bash
75
+ pip install git+https://github.com/bstnxbt/dflash-mlx
76
+ ```
77
+
78
+ ## Quick start
79
+
80
+ ```bash
81
+ PROMPT='The function $f$ satisfies the functional equation \[ f(x) + f(y) = f(x + y) - xy - 1 \] for all real numbers $x$ and $y$. If $f(1) = 1$, then find all integers $n$ such that $f(n) = n$. Enter all such integers, separated by commas. Please reason step by step, and put your final answer within \boxed{}.'
82
+
83
+ # Generate — draft auto-resolved
84
+ dflash --model Qwen/Qwen3.5-9B --prompt "$PROMPT"
85
+
86
+ # Explicit draft model
87
+ dflash --model Qwen/Qwen3.5-9B --draft z-lab/Qwen3.5-9B-DFlash --prompt "$PROMPT"
88
+
89
+ # Server
90
+ dflash-serve --model Qwen/Qwen3.5-9B --port 8000
91
+
92
+ # Benchmark
93
+ dflash-benchmark --model Qwen/Qwen3.5-9B --draft z-lab/Qwen3.5-9B-DFlash \
94
+ --prompt "$PROMPT" --max-tokens 1024 --repeat 3 --no-eos
95
+
96
+ # Live demo — baseline vs DFlash side-by-side
97
+ PYTHONPATH=. python3 -m examples.demo --mode dflash \
98
+ --target-model Qwen/Qwen3.5-9B --draft-model z-lab/Qwen3.5-9B-DFlash \
99
+ --prompt "$PROMPT" --max-tokens 2048 --no-eos
100
+ ```
101
+
102
+ - Compatible with Open WebUI, Continue, and other OpenAI-compatible clients
103
+ - Streaming SSE support
104
+
105
+ ## Tested models
106
+
107
+ Any model with a DFlash draft on HuggingFace should work.
108
+
109
+ Optimized for Qwen3.5 models (hybrid GatedDeltaNet + attention architecture). Qwen3 (pure attention) models work, but without the precision benefits of tape-replay rollback.
110
+
111
+ | Target | Draft |
112
+ |--------|-------|
113
+ | [Qwen/Qwen3.5-4B](https://huggingface.co/Qwen/Qwen3.5-4B) | [z-lab/Qwen3.5-4B-DFlash](https://huggingface.co/z-lab/Qwen3.5-4B-DFlash) |
114
+ | [Qwen/Qwen3.5-9B](https://huggingface.co/Qwen/Qwen3.5-9B) | [z-lab/Qwen3.5-9B-DFlash](https://huggingface.co/z-lab/Qwen3.5-9B-DFlash) |
115
+ | [mlx-community/Qwen3.5-27B-4bit](https://huggingface.co/mlx-community/Qwen3.5-27B-4bit) | [z-lab/Qwen3.5-27B-DFlash](https://huggingface.co/z-lab/Qwen3.5-27B-DFlash) |
116
+ | [mlx-community/Qwen3.5-35B-A3B-4bit](https://huggingface.co/mlx-community/Qwen3.5-35B-A3B-4bit) | [z-lab/Qwen3.5-35B-A3B-DFlash](https://huggingface.co/z-lab/Qwen3.5-35B-A3B-DFlash) |
117
+
118
+ Models without a draft in the registry automatically fall back to standard autoregressive generation.
119
+
120
+ ## Features
121
+
122
+ - **Auto draft resolution** — no manual `--draft` flag needed
123
+ - **Streaming** — token-by-token output in the CLI and server
124
+ - **Chat templates** — enabled by default
125
+ - **Recurrent rollback** — `RecurrentRollbackCache` keeps GatedDeltaNet state coherent across speculative verify and rollback
126
+
127
+ ## Roadmap
128
+
129
+ - Sustained acceptance at 4096+ tokens — draft KV cache window scaling and long-context verify optimization
130
+ - Full-attention model support (Qwen3, LLaMA) with optimized verify path
131
+ - Draft model distillation and compression for faster draft forward
132
+
133
+ ## Citation
134
+
135
+ ```bibtex
136
+ @misc{chen2026dflash,
137
+ title={DFlash: Block Diffusion for Flash Speculative Decoding},
138
+ author={Jian Chen and Yesheng Liang and Zhijian Liu},
139
+ year={2026},
140
+ eprint={2602.06036},
141
+ archivePrefix={arXiv},
142
+ primaryClass={cs.CL},
143
+ url={https://arxiv.org/abs/2602.06036}
144
+ }
145
+ ```
146
+
147
+ ## License
148
+
149
+ MIT
@@ -0,0 +1,135 @@
1
+ <p align="center">
2
+ <h1 align="center">dflash-mlx</h1>
3
+ <p align="center">DFlash speculative decoding for Apple Silicon</p>
4
+ </p>
5
+
6
+ <p align="center">
7
+ <img src="https://img.shields.io/badge/platform-Apple%20Silicon-black?logo=apple" alt="Apple Silicon">
8
+ <img src="https://img.shields.io/badge/python-3.10%2B-blue?logo=python" alt="Python 3.10+">
9
+ <img src="https://img.shields.io/badge/license-MIT-green" alt="License">
10
+ <img src="https://img.shields.io/badge/MLX-stock-red" alt="Stock MLX">
11
+ </p>
12
+
13
+ Paper: [DFlash: Block Diffusion for Flash Speculative Decoding](https://arxiv.org/abs/2602.06036) (Chen et al., 2026)
14
+
15
+ Block-diffusion draft generates 16 tokens in one pass. Target verifies in one pass. Output is lossless — every emitted token is verified against the target model before it is committed.
16
+
17
+ ## How it works
18
+
19
+ - A small draft model (~1B params) generates 16 tokens in parallel with block diffusion.
20
+ - The target model verifies those 16 tokens in a single forward pass.
21
+ - Greedy acceptance keeps the correct prefix and rejects the rest.
22
+ - Lossless: every emitted token is the target model's greedy argmax at verification time. Output can still differ from pure AR because of MLX dispatch divergence, but no unverified token is ever emitted.
23
+ - Uses stock MLX plus a small number of targeted kernels where rollback and long-context verify need tighter numerical control.
24
+
25
+ ## Technical details
26
+
27
+ - **Tape-replay rollback**: instead of snapshotting and restoring the full GatedDeltaNet state, dflash-mlx records an innovation tape during verify and replays only the accepted steps through a custom Metal kernel. Keeps rollback cost low and preserves acceptance over long generations.
28
+ - **JIT SDPA 2-pass**: long-context verify (`N >= 1024`) uses a custom Metal attention kernel that stays numerically aligned with stock MLX attention.
29
+ - **Numerical coherence**: bf16-sensitive paths, including recurrent state replay and small projections, are stabilized across speculative cycles so accepted tokens stay consistent.
30
+
31
+ ## Benchmarks
32
+
33
+ | Model | Tokens | Baseline | DFlash | Speedup | Acceptance |
34
+ |-------|--------|----------|--------|---------|------------|
35
+ | Qwen3.5-4B | 1024 | 53.48 tok/s | 197.49 tok/s | 3.69x | 88.67% |
36
+ | Qwen3.5-4B | 2048 | 53.74 tok/s | 219.83 tok/s | 4.10x | 89.26% |
37
+ | Qwen3.5-4B | 4096 | 53.58 tok/s | 155.19 tok/s | 2.83x | 87.55% |
38
+ | Qwen3.5-9B | 1024 | 31.09 tok/s | 127.47 tok/s | 4.10x | 88.96% |
39
+ | Qwen3.5-9B | 2048 | 30.96 tok/s | 127.07 tok/s | 4.13x | 89.36% |
40
+ | Qwen3.5-9B | 4096 | 31.58 tok/s | 103.90 tok/s | 3.29x | 88.57% |
41
+ | Qwen3.5-27B-4bit | 1024 | 33.24 tok/s | 65.80 tok/s | 1.98x | 89.45% |
42
+ | Qwen3.5-27B-4bit | 2048 | 32.35 tok/s | 62.78 tok/s | 1.90x | 89.11% |
43
+ | Qwen3.5-27B-4bit | 4096 | 29.38 tok/s | 48.89 tok/s | 1.66x | 87.99% |
44
+ | Qwen3.5-35B-A3B-4bit | 1024 | 139.97 tok/s | 242.92 tok/s | 1.74x | 89.26% |
45
+ | Qwen3.5-35B-A3B-4bit | 2048 | 142.12 tok/s | 240.21 tok/s | 1.69x | 88.67% |
46
+ | Qwen3.5-35B-A3B-4bit | 4096 | 140.73 tok/s | 189.62 tok/s | 1.35x | 86.96% |
47
+
48
+ ### Methodology
49
+
50
+ Hardware: Apple M5 Max, 64GB unified memory. MLX 0.31.1 from the stock pip install.
51
+
52
+ Protocol: stock `mlx_lm.stream_generate` on a pristine target model vs stock MLX plus the local DFlash runtime, measured sequentially. `3` repeats, median reported, `10s` cooldown between measured runs.
53
+
54
+ Generation: prompt `"The function $f$ satisfies the functional equation \[ f(x) + f(y) = f(x + y) - xy - 1 \] for all real numbers $x$ and $y$. If $f(1) = 1$, then find all integers $n$ such that $f(n) = n$. Enter all such integers, separated by commas. Please reason step by step, and put your final answer within \boxed{}."` with chat templates enabled by default, `--no-eos`, and post-prefill tok/s as the primary metric.
55
+
56
+ Full per-run JSON reports are available in [`benchmark/results/`](benchmark/results/).
57
+
58
+ ## Install
59
+
60
+ ```bash
61
+ pip install git+https://github.com/bstnxbt/dflash-mlx
62
+ ```
63
+
64
+ ## Quick start
65
+
66
+ ```bash
67
+ PROMPT='The function $f$ satisfies the functional equation \[ f(x) + f(y) = f(x + y) - xy - 1 \] for all real numbers $x$ and $y$. If $f(1) = 1$, then find all integers $n$ such that $f(n) = n$. Enter all such integers, separated by commas. Please reason step by step, and put your final answer within \boxed{}.'
68
+
69
+ # Generate — draft auto-resolved
70
+ dflash --model Qwen/Qwen3.5-9B --prompt "$PROMPT"
71
+
72
+ # Explicit draft model
73
+ dflash --model Qwen/Qwen3.5-9B --draft z-lab/Qwen3.5-9B-DFlash --prompt "$PROMPT"
74
+
75
+ # Server
76
+ dflash-serve --model Qwen/Qwen3.5-9B --port 8000
77
+
78
+ # Benchmark
79
+ dflash-benchmark --model Qwen/Qwen3.5-9B --draft z-lab/Qwen3.5-9B-DFlash \
80
+ --prompt "$PROMPT" --max-tokens 1024 --repeat 3 --no-eos
81
+
82
+ # Live demo — baseline vs DFlash side-by-side
83
+ PYTHONPATH=. python3 -m examples.demo --mode dflash \
84
+ --target-model Qwen/Qwen3.5-9B --draft-model z-lab/Qwen3.5-9B-DFlash \
85
+ --prompt "$PROMPT" --max-tokens 2048 --no-eos
86
+ ```
87
+
88
+ - Compatible with Open WebUI, Continue, and other OpenAI-compatible clients
89
+ - Streaming SSE support
90
+
91
+ ## Tested models
92
+
93
+ Any model with a DFlash draft on HuggingFace should work.
94
+
95
+ Optimized for Qwen3.5 models (hybrid GatedDeltaNet + attention architecture). Qwen3 (pure attention) models work, but without the precision benefits of tape-replay rollback.
96
+
97
+ | Target | Draft |
98
+ |--------|-------|
99
+ | [Qwen/Qwen3.5-4B](https://huggingface.co/Qwen/Qwen3.5-4B) | [z-lab/Qwen3.5-4B-DFlash](https://huggingface.co/z-lab/Qwen3.5-4B-DFlash) |
100
+ | [Qwen/Qwen3.5-9B](https://huggingface.co/Qwen/Qwen3.5-9B) | [z-lab/Qwen3.5-9B-DFlash](https://huggingface.co/z-lab/Qwen3.5-9B-DFlash) |
101
+ | [mlx-community/Qwen3.5-27B-4bit](https://huggingface.co/mlx-community/Qwen3.5-27B-4bit) | [z-lab/Qwen3.5-27B-DFlash](https://huggingface.co/z-lab/Qwen3.5-27B-DFlash) |
102
+ | [mlx-community/Qwen3.5-35B-A3B-4bit](https://huggingface.co/mlx-community/Qwen3.5-35B-A3B-4bit) | [z-lab/Qwen3.5-35B-A3B-DFlash](https://huggingface.co/z-lab/Qwen3.5-35B-A3B-DFlash) |
103
+
104
+ Models without a draft in the registry automatically fall back to standard autoregressive generation.
105
+
106
+ ## Features
107
+
108
+ - **Auto draft resolution** — no manual `--draft` flag needed
109
+ - **Streaming** — token-by-token output in the CLI and server
110
+ - **Chat templates** — enabled by default
111
+ - **Recurrent rollback** — `RecurrentRollbackCache` keeps GatedDeltaNet state coherent across speculative verify and rollback
112
+
113
+ ## Roadmap
114
+
115
+ - Sustained acceptance at 4096+ tokens — draft KV cache window scaling and long-context verify optimization
116
+ - Full-attention model support (Qwen3, LLaMA) with optimized verify path
117
+ - Draft model distillation and compression for faster draft forward
118
+
119
+ ## Citation
120
+
121
+ ```bibtex
122
+ @misc{chen2026dflash,
123
+ title={DFlash: Block Diffusion for Flash Speculative Decoding},
124
+ author={Jian Chen and Yesheng Liang and Zhijian Liu},
125
+ year={2026},
126
+ eprint={2602.06036},
127
+ archivePrefix={arXiv},
128
+ primaryClass={cs.CL},
129
+ url={https://arxiv.org/abs/2602.06036}
130
+ }
131
+ ```
132
+
133
+ ## License
134
+
135
+ MIT
File without changes