Skip to content

Functionalize coreai-models export + new functions for authored models to define KV Cache updates and forward method - #337

Merged
guru-desh merged 7 commits into
apple:mainfrom
guru-desh:functionalize-model-export-code
Oct 10, 2026
Merged

guru-desh merged 7 commits into
apple:mainfrom
guru-desh:functionalize-model-export-code

Conversation

@guru-desh

Copy link
Copy Markdown
Contributor

This PR does the following:

  1. Edit ExportConfig to have a from_preset method
  2. Add resolve_model + build_model by moving components out of async_export_model
  3. Edit BaseForCausalLM to define new_eager_state and eager_forward_args which define how KV Cache is stored and how a forward pass works. This is overriden in BaseForCausalLMForiOS

@guru-desh guru-desh added the code health Improving the overall health of the codebase label Oct 10, 2026
@guru-desh
guru-desh requested a review from stikves October 10, 2026 00:14
…d build_model

_async_export_model's resolution, loading and compression move, unchanged, into two
functions: resolve_model reads the checkpoint's config and resolves it against the
registry, and build_model loads the weights and compresses them. _async_export_model
calls both.

The moved lines are the original lines, re-indented where they leave the `with` block.
The one exception is an assert that ruff re-wraps once it is less indented. Review with:

    git show --color-moved=dimmed-zebra --color-moved-ws=allow-indentation-change

On a tiny Qwen3, the model, config and externalized model handed to the exporter are
identical before and after, for macOS uncompressed, macOS 4-bit and iOS palettized.
The if/elif chain in _resolve_export_config moves, unchanged, into resolve_compression,
so another entry point can resolve a compression exactly as the export CLI does. The
only edit is `args.compression_config` becoming the `compression_config` parameter.
The "Resolve compression preset" block moves, unchanged, from build_model into
resolve_model, and ResolvedModel carries the two configs it resolves. A caller can then
see which compression a model will get before any weight is read.

The one behaviour change: a config naming both a quantization and a palettization now
fails before the weights load rather than after.
quantize_for_export already takes the backend coreai-opt finalizes quantized weights
for; build_model now passes one through, defaulting to Core AI as before. With another
backend nothing is exported, so graph-mode quantization skips marking composite ops for
externalization.
macOS still loads through from_hf_memory_efficient, with mmap_path=None, and the
quantizer skips its disk checkpoints. The export always passes a temp_dir on macOS, so
it is unaffected.
The config that exports a registry preset, with any value passed in winning over the
preset's, for callers that start from a registry preset rather than from CLI arguments.
The export CLI keeps its own resolution, unchanged.
new_eager_state, eager_forward_args and eager_step call forward on real inputs, one
sequence at a time. macOS takes positions over the whole history and the KV pair; iOS
takes only the query's positions, the write offset, and an additive mask over the
context, which causal_mask builds.

Export's palettization keeps its own trace inputs, so what it traces is unchanged.
@guru-desh
guru-desh force-pushed the functionalize-model-export-code branch from 4eb43b4 to ce7cec7 Compare October 10, 2026 00:35
@guru-desh
guru-desh merged commit 52dbd93 into apple:main Oct 10, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

code health Improving the overall health of the codebase

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants