Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Libraries/MLXLLM/DFlash2Registration.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright © 2026 Apple Inc.

import Foundation
import MLXLMCommon

/// Registers the DFlash2 drafter model type.
///
/// Call once before loading a drafter through `DFlash2DrafterModelFactory`.
/// DFlash2 checkpoints report `model_type: qwen3`; the `dflash_config`
/// object tells them apart from a Qwen3 language model.
public enum DFlash2Registration {
public static func register() async {
await DFlash2DrafterTypeRegistry.shared.registerModelType(
"qwen3",
matches: isDFlash2Configuration,
creator: { data in
let config = try JSONDecoder.json5().decode(DFlash2Configuration.self, from: data)
return DFlash2DraftModel(config)
}
)
}
}

private func isDFlash2Configuration(_ data: Data) -> Bool {
guard let root = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else {
return false
}
return root["dflash_config"] is [String: Any]
}
31 changes: 31 additions & 0 deletions Libraries/MLXLLM/Documentation.docc/using-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,37 @@ DSpark currently requires greedy decoding (`temperature: 0`) so verification
remains exact. Pair each draft with the target named in its model card; loading
or running incompatible architecture dimensions fails closed.

### DFlash2 speculative decoding

A DFlash2 drafter proposes a block of tokens from a Qwen 3.5 or Qwen 3.8 target's hidden
states, and the target checks the block in one pass. Register the drafter type, load the
target and a drafter trained for it, then use the DFlash2 generation overload:

```swift
await DFlash2Registration.register()

let target = try await LLMModelFactory.shared.load(
from: targetDirectory,
using: tokenizerLoader
)
let drafter = try await DFlash2DrafterModelFactory.shared.load(
from: drafterDirectory,
using: tokenizerLoader
)
let input = try await target.processor.prepare(input: UserInput(prompt: prompt))

let stream = try generate(
input: input,
parameters: GenerateParameters(maxTokens: 256, temperature: 0),
context: target,
dflash2Drafter: drafter.model
)
```

`DFlash2DrafterRegistry.qwen38_27B_dflash2` names the Qwen3.8-27B drafter. Greedy and
sampled decoding both work. Quantized or bounded KV caches are not supported, and a drafter
trained for a target of another shape fails before prefill.

## Evaluation Flow

- Load the Model
Expand Down
Loading