Skip to content

Commit cecfbfa

Browse files
fluffy314cursoragent
authored andcommitted
Merge main into Mac K2.A reviewer branch
Resolve the dlm_restored_verifier dtype-cast conflict by preserving the K2.A round-trip cast while keeping the DFlash/mainline updates. Co-authored-by: Cursor <cursoragent@cursor.com>
2 parents bec29cb + 4201a60 commit cecfbfa

60 files changed

Lines changed: 12870 additions & 22 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
models/dflash-kakeya-baseline/*.safetensors filter=lfs diff=lfs merge=lfs -text

inference_engine/v04/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343
slice_position_embeddings,
4444
)
4545
from inference_engine.v04.dlm_restored_verifier import DLMRestoredVerifier
46+
from inference_engine.v04.dflash_drafter import (
47+
AuxHiddenProvider,
48+
DFlashConfig,
49+
DFlashDrafter,
50+
DFlashProposer,
51+
)
4652
from inference_engine.v04.kv_compressor import (
4753
IdentityCompressor,
4854
KakeyaLatticeCompressor,
@@ -110,4 +116,10 @@
110116
"KakeyaLatticeUnavailable",
111117
"KVCompressor",
112118
"make_default_compressor",
119+
# K3 — native DFlash drafter (Stage 1: module + proposer; see
120+
# docs/design/k3-cross-model-dlmrestored-verifier-contract.md)
121+
"AuxHiddenProvider",
122+
"DFlashConfig",
123+
"DFlashDrafter",
124+
"DFlashProposer",
113125
]

inference_engine/v04/dflash_drafter.py

Lines changed: 594 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# DFlash baseline drafter for `google/gemma-4-26B-A4B-it` (Kakeya-aligned)
2+
3+
Baseline **DFlash block-diffusion drafter** for the Gemma-4 26B-A4B verifier,
4+
for use in Kakeya inference speculative-decoding development and scenario
5+
testing. Loadable directly by the native engine:
6+
7+
```python
8+
import torch
9+
from inference_engine.v04.dflash_drafter import DFlashDrafter
10+
drafter = DFlashDrafter.from_pretrained("models/dflash-kakeya-baseline", dtype=torch.bfloat16)
11+
```
12+
13+
## What this is
14+
15+
- **Architecture**: the native `DFlashDrafter` (5-layer Qwen3 backbone + `fc`
16+
aux projection + `hidden_norm` + `norm`), faithful to vLLM PR #41703
17+
`qwen3_dflash.py`. Shares the verifier's embeddings (`×sqrt(hidden)`) and
18+
lm_head (`final_logit_softcapping=30`). Aux layers `(2,7,12,18,23,28)`.
19+
- **Weights**: the upstream `z-lab/gemma-4-26B-A4B-it-DFlash` checkpoint,
20+
**alignment-trained** to the Kakeya engine's inference path (see below).
21+
0.43 B params, bf16, `model.safetensors` (stored via Git LFS).
22+
23+
## Why alignment
24+
25+
The upstream DFlash forward is defined inside vLLM (custom KV-cache writes,
26+
fused kernels). The native engine reconstructs the math, but the exact
27+
aux-hidden-tap semantics live in vLLM internals. Rather than reverse-engineer
28+
them, we treat the gap as an `f_θ` alignment task (ADR 0008 §11,
29+
`docs/design/k3-f-theta-training-pipeline.md`): freeze the verifier, train the
30+
drafter so its drafts match the verifier's greedy tokens.
31+
32+
## Provenance / reproduce
33+
34+
- Base: `z-lab/gemma-4-26B-A4B-it-DFlash`
35+
- Verifier: `google/gemma-4-26B-A4B-it`
36+
- Trainer: `scripts/research/k3_dflash_alignment_train.py`
37+
```
38+
python scripts/research/k3_dflash_alignment_train.py \
39+
--steps 6000 --lr 5e-5 --block-size 16 --n-prompts 64 --gen-len 192 \
40+
--train-scope full --save dflash_aligned_corpus.pt
41+
```
42+
(64 diverse prompts, 58 usable; `train_match=0.71`)
43+
44+
## Acceptance (vs the real Gemma-4 verifier, block 16)
45+
46+
| eval | acceptance_rate | acceptance_length |
47+
|---|---|---|
48+
| held-out (8 disjoint prompts) | 0.107 | 2.45 |
49+
| in-domain (small set) | 0.561 | 8.62 |
50+
| reference (HumanEval, vLLM) | 0.447 | 7.70 |
51+
52+
The in-domain run reaching ≥ the reference proves the integration is correct;
53+
the held-out number is limited by the small (64-prompt) alignment corpus and
54+
climbs with more data (10→64 prompts: 1.94→2.45 length). This is a
55+
**baseline** — scaling the alignment corpus is expected to close the held-out
56+
gap toward 7.70.
57+
58+
## Status
59+
60+
Research baseline (not GA). Lossless vs greedy AR is preserved by the
61+
spec-decode accept loop regardless of draft quality; this drafter only affects
62+
*speedup*, not correctness.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"architectures": [
3+
"DFlashDraftModel"
4+
],
5+
"attention_bias": false,
6+
"attention_dropout": 0.0,
7+
"block_size": 16,
8+
"bos_token_id": 2,
9+
"dflash_config": {
10+
"mask_token_id": 4,
11+
"target_layer_ids": [
12+
1,
13+
6,
14+
11,
15+
17,
16+
22,
17+
27
18+
]
19+
},
20+
"dtype": "bfloat16",
21+
"eos_token_id": 1,
22+
"final_logit_softcapping": 30.0,
23+
"head_dim": 128,
24+
"hidden_act": "silu",
25+
"hidden_size": 2816,
26+
"initializer_range": 0.02,
27+
"intermediate_size": 5632,
28+
"layer_types": [
29+
"sliding_attention",
30+
"sliding_attention",
31+
"sliding_attention",
32+
"sliding_attention",
33+
"full_attention"
34+
],
35+
"max_position_embeddings": 262144,
36+
"max_window_layers": 5,
37+
"model_type": "qwen3",
38+
"num_attention_heads": 32,
39+
"num_hidden_layers": 5,
40+
"num_key_value_heads": 8,
41+
"num_target_layers": 30,
42+
"pad_token_id": 0,
43+
"rms_norm_eps": 1e-06,
44+
"sliding_window": 2048,
45+
"tie_word_embeddings": false,
46+
"transformers_version": "5.6.0",
47+
"use_cache": true,
48+
"use_sliding_window": true,
49+
"vocab_size": 262144,
50+
"rope_theta": 1000000,
51+
"rope_scaling": null
52+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "dflash-kakeya-baseline",
3+
"kind": "dflash_drafter",
4+
"role": "baseline speculative-decoding drafter for kakeya inference",
5+
"base_model": "z-lab/gemma-4-26B-A4B-it-DFlash",
6+
"verifier_model": "google/gemma-4-26B-A4B-it",
7+
"loader": "inference_engine.v04.dflash_drafter.DFlashDrafter.from_pretrained",
8+
"dtype": "bfloat16",
9+
"params": 429689088,
10+
"block_size": 16,
11+
"aux_layer_ids": [2, 7, 12, 18, 23, 28],
12+
"alignment": {
13+
"trainer": "scripts/research/k3_dflash_alignment_train.py",
14+
"scope": "full",
15+
"steps": 6000,
16+
"lr": 5e-05,
17+
"prompts": 64,
18+
"usable_sequences": 58,
19+
"train_match": 0.71
20+
},
21+
"acceptance": {
22+
"held_out": {"rate": 0.107, "length": 2.45, "n_prompts": 8, "block_size": 16},
23+
"in_domain": {"rate": 0.561, "length": 8.62},
24+
"reference_humaneval": {"rate": 0.447, "length": 7.7}
25+
},
26+
"evidence": [
27+
"results/research/k3_dflash_specdecode_corpus_heldout.json",
28+
"results/research/dflash_aligned_corpus_report.json"
29+
],
30+
"notes": "Research baseline; held-out acceptance is corpus-size-limited and improves with more alignment data. Lossless vs greedy AR is guaranteed by the verifier accept loop."
31+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:c0e9a83089f4cee77eb8231646bb5abbce942cac3eb3e6f206ac30c513b187aa
3+
size 859384296

requirements.txt

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
# NOTE on transformers version pin:
2-
# The proposer checkpoint dllm-hub/Qwen3-0.6B-diffusion-mdlm-v0.1 ships a
3-
# custom modeling file (`modeling_qwen3.py`) that depends on transformers
4-
# 4.x APIs (specifically `decoder_layer.attention_type`). transformers 5.x
5-
# removed those APIs, so we pin to 4.x project-wide.
2+
# Pin lifted 2026-06-09 — was `<5.0` to keep the legacy Qwen3 dLM
3+
# proposer (`dllm-hub/Qwen3-0.6B-diffusion-mdlm-v0.1`) running, since
4+
# its custom `modeling_qwen3.py` depends on the 4.x
5+
# `decoder_layer.attention_type` API that transformers 5.x removed.
66
#
7-
# On macOS, this means using a dedicated venv (e.g. `python3 -m venv
8-
# .venv`) — do NOT install into a system Python where transformers 5.x is
9-
# already present. `scripts/setup_mac.sh` handles this correctly.
7+
# K3 critical path needs transformers 5.0+:
8+
# * Gemma 4 26B-A4B verifier (per ADR 0008 §11.7.0)
9+
# * scripts/research/k3_dflash_specdecode_eval.py and
10+
# k3_dflash_alignment_train.py (load Gemma 4 via transformers)
11+
#
12+
# So the upper bound was dropped.
13+
#
14+
# KNOWN ISSUE (K2.B Qwen backport path, NOT K3 critical path): under
15+
# transformers 5.x, attempts to load
16+
# `dllm-hub/Qwen3-0.6B-diffusion-mdlm-v0.1` will likely raise an
17+
# AttributeError on `attention_type`. This affects:
18+
# * `training/repr_align/proposer_surgery.py`
19+
# * `kv_cache_proposer/proposer.py`
20+
# * `inference_engine/proposer/sparse_logits.py`
21+
# * `inference_engine/backends/mlx/proposer.py`
22+
# * `tests/system/test_http_*_real_engine.py` (if they download)
23+
# None of these are on the K3 critical path. K2.B Qwen backport is
24+
# the natural place to author a compat patch when that path resumes
25+
# after K3 ships.
26+
#
27+
# For now, if you need the legacy Qwen3 dLM path, install transformers
28+
# 4.x in a dedicated venv: pip install 'transformers>=4.45,<5.0'
1029
torch>=2.4,<3.0
11-
transformers>=4.45,<5.0
30+
transformers>=4.45
1231
accelerate>=0.34
1332
safetensors>=0.4
1433
huggingface_hub>=0.24
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"kind": "k3_dflash_alignment_train",
3+
"config": {
4+
"verifier_id": "google/gemma-4-26B-A4B-it",
5+
"drafter_id": "z-lab/gemma-4-26B-A4B-it-DFlash",
6+
"steps": 6000,
7+
"lr": 5e-05,
8+
"block_size": 16,
9+
"n_prompts": 64,
10+
"gen_len": 192,
11+
"prompt_min_ctx": 8,
12+
"train_scope": "full",
13+
"seed": 0,
14+
"save": "results/research/dflash_aligned_corpus.pt",
15+
"log_every": 25
16+
},
17+
"trainable_params": 429689088,
18+
"n_windows": 7924,
19+
"final_loss": 1.039782760348171,
20+
"final_train_match": 0.71,
21+
"elapsed_s": 213.54307655605953
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"kind": "k3_dflash_alignment_train",
3+
"config": {
4+
"verifier_id": "google/gemma-4-26B-A4B-it",
5+
"drafter_id": "z-lab/gemma-4-26B-A4B-it-DFlash",
6+
"steps": 600,
7+
"lr": 0.0001,
8+
"block_size": 16,
9+
"n_prompts": 8,
10+
"gen_len": 160,
11+
"prompt_min_ctx": 8,
12+
"train_scope": "fc_norms",
13+
"seed": 0,
14+
"save": "results/research/dflash_aligned_fcnorms.pt",
15+
"log_every": 25
16+
},
17+
"trainable_params": 47584768,
18+
"n_windows": 1041,
19+
"final_loss": 3.496910614967346,
20+
"final_train_match": 0.1725,
21+
"elapsed_s": 12.202172419056296
22+
}

0 commit comments

Comments
 (0)