Repository navigation
Add experimental DFlash2 training and checkpoint support #1006
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
50ac0cb
Add experimental DFlash2 training support
mgoin 1690bb5
Fix scheduler LR restoration on resume
mgoin 317ddb6
Add type and shape hints
fynnsu fa70faf
Fix quality
fynnsu 022a2f0
Refactor helper
fynnsu 8453e3e
Refactor to simplify selector loss calculation
fynnsu 1b3d845
Add sample_from_anchor support
fynnsu c9ced32
Update docs and examples
fynnsu ce713cf
Add smoke and regression tests
fynnsu c9f4040
Fix tests
fynnsu e7c8337
Handle review feedback
fynnsu f5adebf
Merge branch 'main' into mgoin/dflash2-prototype
shanjiaz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| # DFlash2 | ||
|
|
||
| DFlash2 extends [DFlash](dflash.md) with local dynamic convolutions and a candidate selector. The convolution lets neighboring positions exchange local information while the draft block remains parallel. The selector then adds a low-rank, predecessor-conditioned transition score to the draft model's unary token scores. | ||
|
|
||
| ## How It Works | ||
|
|
||
| ### Local Convolution | ||
|
|
||
| Each draft layer wraps both attention and the MLP with a grouped causal convolution. A projection of the current hidden state produces two dynamic kernels: one for the sublayer input and one for its output. A learned base kernel is shared across positions, while channels in each group share the dynamic component. | ||
|
|
||
| Convolution never crosses an anchor-block boundary. This matters during training because Speculators flattens multiple anchored blocks into one token dimension. | ||
|
|
||
| ### Candidate Selector | ||
|
|
||
| The unary draft logits first select `selector_top_k` candidates at each position. The selector scores each transition as | ||
|
|
||
| ```text | ||
| unary(candidate) | ||
| + dot(predecessor_codebook(previous) * hidden_projection(hidden), | ||
| successor_codebook(candidate)) | ||
| ``` | ||
|
|
||
| Inference starts with the anchor token and then conditions each position on the token selected at the preceding position. This preserves one parallel draft-model forward pass: only the small selector walks the candidate path. | ||
|
|
||
| ## Training Objective | ||
|
|
||
| The public DFlash2 implementation specifies inference but does not publish its training objective. The current Speculators implementation is therefore an experimental split objective: | ||
|
|
||
| 1. The configured DFlash-family compound loss trains the unary logits. | ||
| 2. A separate hard cross-entropy term trains the selector over the unary top-K candidates while teacher-forcing the previous sequence token. If the target is outside that set, training replaces the weakest unary candidate with the target so the K-way loss remains defined. Validation loss uses the same replacement; serving and strict validation candidate/path metrics do not. | ||
|
|
||
| `--selector-loss-alpha` controls the second term. Both terms use the configured fixed exponential or D-PACE position weighting. The selector is never trained against a full-vocabulary corrected distribution, matching the candidate set it can rerank at serving time. | ||
|
|
||
| Validation reports clearly separated unary candidate recall and target mass, teacher-forced selector accuracy, and an actual greedy self-conditioned path. The path begins at the verified anchor and feeds each selected token to the next edge score. Its per-position accuracy is conditioned on the earlier path being correct. The accepted-length metrics include the verified anchor and report both the realized selector path and the oracle unary-top-K path. | ||
|
|
||
| DFlash2 currently requires the full verifier vocabulary. Pruned draft vocabularies are rejected because current serving implementations select candidates before any draft-to-target vocabulary mapping. | ||
|
|
||
| This experimental implementation directly trains full-vocabulary predecessor and successor codebooks. That is an expanded free-codebook parameterization: the public checkpoint format contains materialized codebooks, but the public sources do not disclose whether those tensors were free parameters during training. In particular, this prototype should not be interpreted as reproducing the selector parameter count reported in the DFlash2 blog. | ||
|
|
||
| ## Key Parameters | ||
|
|
||
| | Parameter | Default | Description | | ||
| | ----------------------- | ------: | ---------------------------------------- | | ||
| | `--conv-kernel-size` | 2 | Number of causal convolution taps | | ||
| | `--conv-group-size` | 16 | Hidden channels sharing a dynamic kernel | | ||
| | `--selector-rank` | 256 | Rank of the transition factorization | | ||
| | `--selector-top-k` | 16 | Unary candidates reranked per position | | ||
| | `--selector-loss-alpha` | 1.0 | Weight of the selector K-way CE term | | ||
|
|
||
| All [DFlash](dflash.md) backbone parameters also apply. DFlash2 defaults to five draft layers, block size 8, `sample_from_anchor: False`, fixed exponential position weighting, and KL divergence loss. Set all shared knobs explicitly when comparing it with another algorithm. | ||
|
|
||
| To train your own, see `examples/train/dflash2_qwen3_8b_sharegpt_online_5k.sh`. | ||
|
|
||
| ## Serving | ||
|
|
||
| Checkpoints emitted here follow the public Z Lab weight contract but use a speculators config. They can be served in vLLM using `vllm serve ./checkpoint`. | ||
|
|
||
| ## Research and Implementation Reference | ||
|
|
||
| The architecture follows Z Lab's [DFlash2 implementation](https://github.com/z-lab/dflash/blob/07ebd93db9f472af339b644bb70221ad8428328a/dflash/model.py) and the [DFlash2 technical blog](https://inco.ai/blog/dflash2/). The pinned source revision defines the inference architecture and checkpoint contract; the experimental training objective above is specific to this implementation. | ||
|
shanjiaz marked this conversation as resolved.
Resolved
|
||
|
|
||
| ## See Also | ||
|
|
||
| - [DFlash](dflash.md) -- The block-parallel draft backbone | ||
| - [DSpark](dspark.md) -- Adds a Markov head and confidence scheduling | ||
| - [Train a Speculator](../tutorials/train.md) -- End-to-end training workflow | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.