Add bind(into:) for zero-copy state binding - #156
Merged
Conversation
stikves
force-pushed
the
sukru/state-handler-binder
branch
3 times, most recently
from
August 6, 2026 21:16
b12a824 to
ff3538f
Compare
stikves
marked this pull request as ready for review
August 7, 2026 01:46
stikves
force-pushed
the
sukru/state-handler-binder
branch
4 times, most recently
from
August 7, 2026 18:13
0237172 to
cb23ae4
Compare
State handlers are now classes (AnyObject) that own their NDArrays at refcount 1. bind(into:) inserts all states into MutableViews in a loop using stdlib _overrideLifetime to express disjoint element access. No COW, no switch on state count, no write-back needed. FixedMTLBufferState also gains bind(into:) for AsyncMutableViews, eliminating the 3x duplicated switch blocks in the pipelined engine. Engine call sites: runWithStates (sequential) and encodeWithStates (pipelined) are now thin wrappers around bind + function.run/encode.
stikves
force-pushed
the
sukru/state-handler-binder
branch
from
August 7, 2026 18:19
cb23ae4 to
e2daf95
Compare
stikves
requested review from
alejandro-isaza,
blevine1,
carinapeng,
kevchengcodes and
tjia1818
August 7, 2026 18:59
carinapeng
reviewed
Aug 7, 2026
carinapeng
reviewed
Aug 7, 2026
carinapeng
reviewed
Aug 7, 2026
tjia1818
approved these changes
Aug 8, 2026
carinapeng
pushed a commit
to carinapeng/coreai-models
that referenced
this pull request
Aug 10, 2026
Static-shape LLM inference engine (StaticShapeEngine) built on the bind(into:) state binding from apple#156. States are discovered by name and the KV cache is right-sized per context bucket (each bucket graph is compiled with its own per-ctx strides, so a max-ctx buffer sliced down corrupts KV); ctx is parsed from the function name. Input preparation is pluggable per model family via StaticInputProvider + StaticModelProfile; StaticInputContext composes the shared InputContext and the provider mirrors SyncInputHandler. Testing: LanguageModelsTests (317) pass; correct output on gemma4-E2B, qwen2.5-1.5B, qwen3-0.6B static assets. Known limitation: cross-bucket cache re-layout needs a channel-interleave-aware copy; within-bucket generation is correct. Depends on apple#156.
carinapeng
pushed a commit
to carinapeng/coreai-models
that referenced
this pull request
Aug 10, 2026
Static-shape LLM inference engine (StaticShapeEngine) built on the bind(into:) state binding from apple#156. States are discovered by name and the KV cache is right-sized per context bucket (each bucket graph is compiled with its own per-ctx strides, so a max-ctx buffer sliced down corrupts KV); ctx is parsed from the function name. Input preparation is pluggable per model family via StaticInputProvider + StaticModelProfile; StaticInputContext composes the shared InputContext and the provider mirrors SyncInputHandler. Testing: LanguageModelsTests (317) pass; correct output on gemma4-E2B, qwen2.5-1.5B, qwen3-0.6B static assets. Known limitation: cross-bucket cache re-layout needs a channel-interleave-aware copy; within-bucket generation is correct. Depends on apple#156.
carinapeng
pushed a commit
to carinapeng/coreai-models
that referenced
this pull request
Aug 10, 2026
Static-shape LLM inference engine (StaticShapeEngine). States are discovered by name and the KV cache is right-sized per context bucket (each bucket graph is compiled with its own per-ctx strides, so a max-ctx buffer sliced down corrupts KV); ctx is parsed from the function name. Input preparation extends Sukru's shared SyncInputHandler / InputContext (apple#147): concrete handlers (position ids, causal mask, step, RoPE, PLE, sliding) conform to SyncInputHandler; InputContext gains a per-graph descriptors map so handlers size their own buffers. No Static input protocol/context — one input family for dynamic and static engines. State binding uses bind(into:) (apple#156). Testing: LanguageModelsTests pass incl. new unit tests (ctx-bucket parsing, causal mask fill); correct output on gemma4-E2B, qwen2.5-1.5B, qwen3-0.6B static. Depends on apple#156, apple#147.
msnabiel
added a commit
to msnabiel/coreai-models
that referenced
this pull request
Aug 11, 2026
Resolves conflicts: - Removed ModelShapeConfig.swift (deleted upstream in apple#148) - Updated NDArray+Helpers.swift documentation for stride-aware indexing Pulls in upstream features: - Parakeet speech recognition export and runtime (apple#136) - SyncInputHandler protocol and InputContext (apple#147) - Zero-copy state binding with bind(into:) (apple#156) - Additional speech tests and config improvements - Bug fixes for forced_decoder_ids parsing, topK normalization, logits token count Preserves local iOS optimizations: - int8 KV cache quantization (2× memory reduction) - 16k default context with unreachable shape pruning - CoreAI simulator availability guards 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
State handlers are now classes that own their NDArrays at refcount 1.
bind(into:)inserts all states intoMutableViewsin a loop using stdlib_overrideLifetimeto express disjointelement access. Same pattern for
AsyncMutableViewson the pipelined engine'sFixedMTLBufferState.Changes
SyncStateHandlerprotocol: addbind(into:), constrain toAnyObjectFixedNDArrayState,GrowingNDArrayState: struct → class, dictionary-backedFixedMTLBufferState: struct → class, addbind(into:)forAsyncMutableViewsrunWithStates(): sequential engine helperencodeWithStates(): pipelined engine helperrunWithStates()encodeWithStates()Requires
Lifetimesexperimental Swift feature.Tested with 2 state and 4 state hybrid models.