Skip to content

Consolidate VLM sequential engine: shared iterator helpers + tidy (B3+B4) - #3

Draft
stikves wants to merge 2 commits into
sukru/vlm-consolidation-b1b2from
sukru/vlm-consolidation-b3b4
Draft

stikves wants to merge 2 commits into
sukru/vlm-consolidation-b1b2from
sukru/vlm-consolidation-b3b4

Conversation

@stikves

@stikves stikves commented Sep 13, 2026

Copy link
Copy Markdown
Owner

Summary

Second half of the VLM sequential-engine consolidation (steps B3 and B4). Stacked on apple#249.

Targets sukru/vlm-consolidation-b1b2 so the diff is limited to the B3/B4 changes. Will be
retargeted to main once apple#249 merges.

Changes

B3: Extract the identical pure-value generation logic (clampMaxTokens, nextToken) into a
SequentialIterator namespace called by both engines' next() loops. Each engine keeps its own
control flow: the text engine's batched forced-continuation PPL path and prefix-cache bookkeeping,
and the VLM engine's image prefill. Clamp arithmetic and copy-on-write sampling semantics are
unchanged.

This is deliberately narrower than a full shared SequentialGenerationIterator, which would require
folding the text engine's batched-PPL control flow into shared code for little additional dedup.

B4: Remove the addressed "TODO: Refactor to re-use common components" marker, fold the repeated
processedTokenCount/kvCache.reset()/additionalStates zeroing into a private
clearGenerationState(), and update the VLM engine class doc. The separate pipelined-variant TODO
is left in place.

Validation

Bit-exact against the pre-refactor baseline at this tip: text greedy 128/128, T2 PPL exact, VLM
greedy 115/115 and 400/400.

xcodebuild build and xcodebuild test pass; the B1/B2 unit suites remain green.

Scope

The EngineFactory bundle entry point and CLI fork removal (C1/C2) are a separate PR.

@stikves
stikves force-pushed the sukru/vlm-consolidation-b1b2 branch from 3250fb0 to 0eadf06 Compare September 14, 2026 20:34
@stikves
stikves force-pushed the sukru/vlm-consolidation-b3b4 branch from b065d86 to 9e90ffc Compare September 14, 2026 20:34
@stikves
stikves force-pushed the sukru/vlm-consolidation-b1b2 branch from 8d9bfc2 to f31b292 Compare September 14, 2026 21:09
@stikves
stikves force-pushed the sukru/vlm-consolidation-b3b4 branch from 9e90ffc to 51d4c22 Compare September 14, 2026 21:09
@stikves
stikves force-pushed the sukru/vlm-consolidation-b1b2 branch from 0f8dd87 to f56e84b Compare September 14, 2026 21:26
@stikves
stikves force-pushed the sukru/vlm-consolidation-b3b4 branch 2 times, most recently from 9f083c2 to 8d200cb Compare September 14, 2026 21:30
…d prefill (B1+B2) (apple#249)

B1: Replace the VLM engine's hand-rolled KV cache (keyCache/valueCache/currentKVCapacity
plus ensureKVCapacity/copyCache/zeroFill) with the shared StateHandlerFactory and
runWithStates path used by the text engine. Growth semantics unchanged (initial 256, 2x).
Restore the per-realloc "KV cache grew" log in GrowingNDArrayState. Read back
hasNonTruncatableStates and guard reset(to:) against partial reset for recurrent-state
models, matching the text engine.

B2: Add GenerationTokenBox for the active-token busy/cancel/install bookkeeping both engines
duplicated, and runChunkedPrefill() to replace both processChunkedPrompt implementations.
The text engine passes a non-zero heldBack and routes non-held-back chunks through the
prefill graph; the VLM engine passes heldBack = 0.

Adds GenerationTokenBox and chunked-prefill unit tests.
@stikves
stikves force-pushed the sukru/vlm-consolidation-b3b4 branch from 8d200cb to 3d6e3f3 Compare September 15, 2026 01:25
Extract the identical scalar iterator logic (max-token clamp, next-token
selection) shared by CoreAISequentialEngine and CoreAISequentialVLMEngine into
a SequentialIterator namespace both call, and tidy the VLM engine: drop the
addressed re-use TODO, dedup the post-warmup/reset state-clear into
clearGenerationState(), and refresh the class docs to describe the now-shared
decode machinery.

Behavior-preserving; each engine keeps its own generation control flow.
@stikves
stikves force-pushed the sukru/vlm-consolidation-b3b4 branch from 3d6e3f3 to b50e30a Compare September 15, 2026 01:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant