Skip to content

Add SyncInputHandler protocol for engine extensibility - #147

Merged
stikves merged 1 commit into
apple:mainfrom
stikves:sukru/input-handler-protocol
Aug 6, 2026
Merged

Add SyncInputHandler protocol for engine extensibility#147
stikves merged 1 commit into
apple:mainfrom
stikves:sukru/input-handler-protocol

Conversation

@stikves

@stikves stikves commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Summary

Composable input preparation layer for inference engines, enabling model-specific inputs (RoPE, PLE, sliding window masks) without engine modifications.

  • SyncInputHandler protocol with prepare(_ context:) -> [String: NDArray]
  • InputContext with .dynamic() and .static() builders
  • TokenInputHandler — standard token/position ID preparation with batch caching
  • CompositeInputHandler — wraps base handler with extra model-specific inputs
  • PipelinedTokenInputHandler — MTLBuffer-based for pipelined engine
  • InputCoverage.verify() — fail-fast at engine init if inputs are uncovered

Also hardens NDArray+Helpers:

  • fillNDArray(count:using:) and readNDArray now check contiguity and fall back to stride-aware indexing for tensors with GPU alignment padding

Test plan

  • InputHandler Tests: 4 tests pass (context builders, type conformance, coverage API)
  • CoreAIShared Tests: 24 tests pass (no regressions)
  • Full package builds cleanly

@stikves stikves self-assigned this Aug 5, 2026
@stikves stikves added the enhancement New feature or request label Aug 5, 2026
@stikves
stikves force-pushed the sukru/input-handler-protocol branch 2 times, most recently from 0168d45 to 438e5d1 Compare August 5, 2026 16:16
@stikves
stikves marked this pull request as ready for review August 5, 2026 16:16
@stikves
stikves force-pushed the sukru/input-handler-protocol branch 2 times, most recently from 438e5d1 to f786d80 Compare August 5, 2026 18:01
@stikves
stikves requested review from carinapeng and tjia1818 August 5, 2026 18:03
Comment thread Package.resolved
@stikves
stikves force-pushed the sukru/input-handler-protocol branch 3 times, most recently from a2dc47c to 7b614e8 Compare August 5, 2026 19:09
Comment thread swift/Tests/LanguageModelsTests/ProfileSpanTests.swift Outdated
@stikves
stikves force-pushed the sukru/input-handler-protocol branch from e6a33c2 to f3ce8a3 Compare August 6, 2026 05:47
Introduces a composable input preparation layer for inference engines:
- SyncInputHandler protocol: prepares named NDArray inputs each step
- InputContext: carries tokens, position, batch size, sliding window
- InputContext.dynamic() for sequential/pipelined engines
- InputContext.static() for static-shape (ANE) engines
- TokenInputHandler: standard token ID input with batch size caching
- CompositeInputHandler: wraps a base handler with extra inputs (RoPE, PLE)
- InputCoverage.verify(): fail-fast check at engine init
- PipelinedTokenInputHandler: MTLBuffer-based for pipelined engine

Also hardens NDArray helpers with stride-aware fill/read:
- fillNDArray(count:using:) now checks contiguity and falls back to
  stride-aware indexing for 4D+ tensors with GPU alignment padding
- readNDArray checks contiguity similarly
- TokenInputHandler.prepare() validates non-empty batch
@stikves
stikves force-pushed the sukru/input-handler-protocol branch from f3ce8a3 to 24765f1 Compare August 6, 2026 05:50
@stikves
stikves merged commit 4236629 into apple:main Aug 6, 2026
3 checks passed
@stikves
stikves deleted the sukru/input-handler-protocol branch August 6, 2026 06:31
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants