Skip to content

New custom op for KV cache update - #126

Merged
vineet-g merged 3 commits into
apple:mainfrom
vineet-g:new_kv_cache_custom_op
Jul 29, 2026
Merged

New custom op for KV cache update#126
vineet-g merged 3 commits into
apple:mainfrom
vineet-g:new_kv_cache_custom_op

Conversation

@vineet-g

Copy link
Copy Markdown
Contributor

Summary

  • Unify KV-cache update + fetch into a single fused custom op
  • Both iOS and macOS model definitions now use the new custom op

@carinapeng

Copy link
Copy Markdown
Contributor

Thank you! A high level question for the motivation of this fused op, do we have perf numbers? Or is this for correctness?

Am I right in the understanding that at the MLIR level the op is decomposed away into unsqueeze -> immutable_slice_update -> aten.slice -> aten.slice -> aten.squeeze?

Can we get the same primitives from slice_update + slice + squeeze primitives? Or is the intention to change dependency as we lower?

Comment thread python/src/coreai_models/export/mlir_ops.py Outdated
@TobyRoseman

Copy link
Copy Markdown

This looks interesting. Would it be possible to add unit tests for this new functionality?

Comment thread python/src/coreai_models/export/mlir_ops.py Outdated
@vineet-g

vineet-g commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

Thank you! A high level question for the motivation of this fused op, do we have perf numbers? Or is this for correctness?

This is to enable KV-cache quantization. No perf change is expected; the lowered MLIR has the same primitives as before.

Am I right in the understanding that at the MLIR level the op is decomposed away into unsqueeze -> immutable_slice_update -> aten.slice -> aten.slice -> aten.squeeze?

Yes

Can we get the same primitives from slice_update + slice + squeeze primitives? Or is the intention to change dependency as we lower?

Existing mutable_slice_update takes one layer's K/V in and returns the whole cache out which creates asymmetry while placing quantize/dequantize nodes around the custom op.

The new fused op takes one layer in and returns that same layer's populated cache as output which gives the quantizer a symmetric custom-op boundary.

@carinapeng carinapeng left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but let's make sure we are not introducing regression with the python tests, and +1 to @TobyRoseman 's point on adding unit test for this

@vineet-g
vineet-g force-pushed the new_kv_cache_custom_op branch from eda1d06 to 54d4dba Compare July 29, 2026 20:58
@vineet-g

Copy link
Copy Markdown
Contributor Author

Would it be possible to add unit tests for this new functionality?

python/tests/test_model_units/test_primitives/test_macos/test_cache.py already covers the unit tests for cache_update which now uses the new custom op

@vineet-g
vineet-g merged commit 367ad52 into apple:main Jul 29, 2026
3 checks passed
@vineet-g
vineet-g deleted the new_kv_cache_custom_op branch July 29, 2026 22:25
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.

3 participants