Skip to content
Merged
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
33 changes: 33 additions & 0 deletions docs/architecture/inference.md
Original file line number Diff line number Diff line change
Expand Up @@ -6806,6 +6806,39 @@ and the collective payload quote covers the longer frame
the loopback ring with the control exchanged or folded and checks that both
retire the same tokens.

**Build-ahead (driver protocol; not yet on in the product).** With
`QwenTensorParallelBatchSettings.buildsAhead` (load agreement field
`qwen-tp-batch-build-ahead-v1`), the driver builds a plain decode step before
it reads the step before it (`QwenTensorParallelBatchDriver.serveAhead`), so
the GPU and the collectives run while the host reads, publishes and answers.
The window-1 profile measured about 1.9 ms of GPU idle at each token boundary.
- Every folded step's selection sum also carries two decision words, one per
rank, for the step two later. Continuations need no plan: both ranks build
that step on the unread step's selection rows (`QwenTensorParallelBatchExecutor.submitDecode`
and `readDecode`) only when neither rank asked for a barrier and the unread
step decoded the same lanes the decision covered.
- A rank asks for a barrier whenever anything needs a plan: an owner's stop or
the answer to a request's last token, a request waiting to join, an idle lane,
a cancellation or deadline, an ended lane, a lane that would pass its output
count, or more than four tokens held unpublished. At a barrier both ranks read
every submitted step, and the next plan is made and exchanged with nothing in
flight, exactly as without build-ahead.
- Owner answers that a decision reads and that need a plan are kept for that
plan, and their lane publishes nothing more meanwhile. Tokens selected before
their credit are held, one queue a row (`heldLogprobs`), and published in order,
one per "go on" answer. A client stop drops all held tokens. An EOS read after
the next step was built discards that one forward on both ranks (the
generation schedule accepts one declared discarded forward after an EOS). No
step is built past a request's output count.

Every input to a decision and to the build is agreed state (read sums, read
tokens, plans), so both ranks submit the same collectives in the same order.
Ranks on different protocols refuse each other at load (the field). The driver
is checked on two ranks with a fake executor (`QwenTensorParallelBatchDriverTests`:
steady decode, EOS, owner stops, late credits, joins, prompts, cancellation, and
ranks that disagree failing closed). The native executor does not submit decode
steps yet (`buildsAhead` false), so the product runs read-then-build until it does.

The batch path can restore mutually held original-grid prefix snapshots into
fresh request-owned state. TP speculative rounds remain a separate integration. Installed owners propagate a priced
saved width or explicit diagnostic width for every Qwen TP row; placement and automatic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,16 @@ struct QwenLayerStageGenerationSchedule {
}

/// `discardedDecodeForwards` counts decode frames this stage ran alone
/// whose tokens the request owner never published. Only a client stop can
/// leave any: the stage decodes a batch before it learns the decision.
/// whose tokens the request owner never published. A client stop can
/// leave any (the stage decodes a batch before it learns the decision),
/// and an EOS under tensor-parallel build-ahead leaves exactly one (the
/// next step is built before the EOS is read; `QwenTensorParallelBatchDriver`). A
/// length finish never does: no step is built past the output count.
mutating func finish(_ reason: QwenLayerStageGenerationFinishReason,
selectedTokenCount: Int, lastTokenID: Int,
discardedDecodeForwards: Int = 0) throws {
guard !complete, committedPromptTokens == request.promptCount,
discardedDecodeForwards >= 0, discardedDecodeForwards == 0 || reason == .clientStop,
discardedDecodeForwards >= 0, discardedDecodeForwards == 0 || reason == .clientStop || (reason == .eos && discardedDecodeForwards == 1),
selectedTokenCount == decodeForwardCount + 1 - discardedDecodeForwards,
(1...request.outputCount).contains(selectedTokenCount),
(0..<request.profile.vocabularySize).contains(lastTokenID) else {
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ struct QwenTensorParallelBatchSettings {
/// A routed-expert row's cohort packs every row's expert and shared
/// partials into one collective a layer (`Qwen35SparseMoeBlock`).
var routedExperts = false
/// The driver builds decode steps ahead (`QwenTensorParallelBatchDriver`'s
/// build-ahead), on an executor that submits them
/// (`QwenTensorParallelBatchExecutor.buildsAhead`). Off until the native
/// executor submits decode steps; bound into the load agreement.
var buildsAhead = false
static func resolve(model: QwenRegisteredDenseModel, keyed: Bool,
environment: [String: String]) throws -> Self {
let definition = try QwenResidentModelDefinition(model: model)
Expand All @@ -25,5 +30,6 @@ struct QwenTensorParallelBatchSettings {
[QwenTensorParallelBatchWire.agreement, "lanes=\(lanes)", "kv=\(backend.rawValue)",
"fp32-row-m1-v1", "request-local-original-rope-v1", "batch-inventory-budget-v3-rank-heads", QwenTensorParallelBatchPrefixBudget.policy]
+ (routedExperts ? ["moe-rows-one-collective-v1"] : [])
+ (buildsAhead ? [QwenTensorParallelBatchWire.buildAheadAgreement] : [])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import Foundation
/// pays one sealed sum and one host read; every other step keeps its own exchange.
enum QwenTensorParallelBatchWire {
static let agreement = "qwen-tp-independent-history-batch-v2-folded-control"
/// Build-ahead (`QwenTensorParallelBatchDriver.serveAhead`): every folded
/// step's selection sum also carries both ranks' decision words for the
/// step two later, and decided continuations are built before the step
/// before them is read. A load-agreement field, so ranks of the two
/// protocols refuse each other before either loads a weight.
static let buildAheadAgreement = "qwen-tp-batch-build-ahead-v1"
static let words = 4 + 2 * (QwenBatchWire.headerWords + QwenBatchWire.replyWords)
private static let magic: Float = 0x515442
/// A folded plan's decode input: the token its row selects in the step
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ extension QwenTensorParallelResidentRuntime {
log("darkbloom-tp-mtp-profile-v1 rank=\(collective.rank) scope=\(records.requestID) capacity=\(batchLanes) " + profile.fields)
})
let driver = try QwenTensorParallelBatchDriver(executor: executor, book: batchBook,
capacity: batchLanes, vocabulary: admission.profile.vocabularySize)
capacity: batchLanes, vocabulary: admission.profile.vocabularySize, buildsAhead: batchBuildsAhead)
do {
try lifecycle.withRequest(identity: .init(requestID: records.requestID, epoch: records.requestID,
recordedRequestFingerprint: sha256(Data("qwen-tp-batch|\(loadAgreement)|\(batchOrdinal)".utf8)))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public final class QwenTensorParallelResidentRuntime {
/// What this rank measured of the link at load (`QwenTensorParallelLinkProbe`).
public let linkProbe: ClusterWorkerLinkProbe?
public let batchLanes: Int
/// The driver builds decode steps ahead: the load agreement's own value
/// (`QwenTensorParallelBatchSettings.buildsAhead`), so the gate and the
/// behaviour are one value.
let batchBuildsAhead: Bool
var batchHost: CBv2StageKVHost?
let batchBudget: QwenTensorParallelBatchBudget?
let batchMTPBudget: QwenTensorParallelBatchMTPBudget?
Expand Down Expand Up @@ -391,7 +395,7 @@ public final class QwenTensorParallelResidentRuntime {
profile: profile, shares: shares,
allocationBaseline: allocationBaseline, shareProbe: shareProbe,
loadAgreement: loadAgreement, capacity: capacity, receipt: receipt, linkProbe: probe,
batchLanes: batch.lanes, batchHost: batchHost, batchBudget: batchBudget, batchMTPBudget: batchMTPBudget,
batchLanes: batch.lanes, batchBuildsAhead: batch.buildsAhead, batchHost: batchHost, batchBudget: batchBudget, batchMTPBudget: batchMTPBudget,
requestCharge: requestCharge, servingContextTokens: serving, batchPrefixEnabled: prefixEnabled,
batchPrefixBudgetBytes: prefixBudget, prefixDisk: prefixDisk,
mtpTerms: speculativeTerms, mtpHead: loadedMTPHead, assistantReady: assistantReady)
Expand Down Expand Up @@ -427,7 +431,7 @@ public final class QwenTensorParallelResidentRuntime {
vision: QwenStageVision?, visionInventory: QwenStageVisionInventory?, profile: QwenRegisteredDenseModelProfile, shares: [Int],
allocationBaseline: Int, shareProbe: ClusterWorkerShareProbe?, loadAgreement: String, capacity: Int,
receipt: QwenTensorParallelShardReceipt, linkProbe: ClusterWorkerLinkProbe?,
batchLanes: Int, batchHost: CBv2StageKVHost?,
batchLanes: Int, batchBuildsAhead: Bool, batchHost: CBv2StageKVHost?,
batchBudget: QwenTensorParallelBatchBudget?, batchMTPBudget: QwenTensorParallelBatchMTPBudget?,
requestCharge: QwenTensorParallelRequestCharge, servingContextTokens: Int?,
batchPrefixEnabled: Bool, batchPrefixBudgetBytes: Int, prefixDisk: QwenTensorParallelPrefixDisk?,
Expand All @@ -438,7 +442,8 @@ public final class QwenTensorParallelResidentRuntime {
self.vision = vision; self.visionInventory = visionInventory
self.loadAgreement = loadAgreement; self.linkProbe = linkProbe
self.batchAllocationBaseline = allocationBaseline
self.batchLanes = batchLanes; self.batchHost = batchHost; self.batchBudget = batchBudget
self.batchLanes = batchLanes; self.batchBuildsAhead = batchBuildsAhead
self.batchHost = batchHost; self.batchBudget = batchBudget
self.batchMTPBudget = batchMTPBudget
self.requestCharge = requestCharge; self.servingContextTokens = servingContextTokens
self.mtpTerms = mtpTerms; self.mtpHead = mtpHead
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ final class HistoryBatchTestSession: IndependentHistoryBatchSession {
var isClosed = false, isFailed = false, failCleanup = false
/// Qwen TP driver tests: false stands for an assisted row, which never folds control.
var plainSelection = true
/// Forwards a client stop may discard: one, or more under build-ahead (held tokens and steps built after the stop).
var aheadDiscardLimit = 1
var nextFrame: QwenLayerStageFrame? { try? request.frame(sequence: frames.count) }
init(_ request: QwenLayerStageGenerationRequest) { self.request = request }
static func next(_ history: [Int]) -> Int { (history.reduce(0, +) + history.count * 7) % 64 }
Expand Down Expand Up @@ -134,7 +136,10 @@ final class HistoryBatchTestSession: IndependentHistoryBatchSession {
func finishGeneration(_ reason: QwenLayerStageGenerationFinishReason, selectedTokenCount: Int, lastTokenID: Int,
discarded: Int) throws {
let decoded = frames.count - request.prefillFrameCount
guard discarded == 0 || (reason == .clientStop && discarded == 1),
// A client stop drops the selection after the owner's answer; build-ahead may
// also have run forwards past it, or one past an EOS (`QwenTensorParallelBatchDriver`).
guard discarded == 0 || (reason == .clientStop && discarded <= aheadDiscardLimit)
|| (reason == .eos && discarded == 1),
lastTokenID == (discarded == 0 ? Self.next(history) : history[history.count - discarded]),
selectedTokenCount == decoded + 1 - discarded else {
throw ProbeError("Test finish differs from committed history")
Expand Down
Loading
Loading