Skip to content

Fast Inference SDPA op - #735

Merged
awni merged 13 commits into
ml-explore:mainfrom
argmaxinc:user/bkeene/fast_inference_sdpa
Mar 5, 2024
Merged

awni merged 13 commits into
ml-explore:mainfrom
argmaxinc:user/bkeene/fast_inference_sdpa

Conversation

@bpkeene

@bpkeene bpkeene commented Feb 23, 2024 •

Copy link
Copy Markdown
Contributor

Implements Metal kernels backing the new compound op:

o = mx.fast.scaled_dot_product_attention(queries, keys, values, scale=scale, mask=mask)

to replace current primitives for attention e.g. Mistral-7b in mlx-examples/llms/mistral/mistral.py:

scores = (queries * self.scale) @ keys.transpose(0, 1, 3, 2)
if mask is not None:
    scores += mask
scores = mx.softmax(scores.astype(mx.float32), axis=-1).astype(scores.dtype)
output = (scores @ values)

Context

This "fat op" design is inspired by the design of torch.nn.functional.scaled_dot_product_attention. This PR implements the Metal kernel for KV-cached SDPA inference following the lazy softmax principle from [1]. [2] follows the same principles for their CUDA implementation. Future PRs will extend coverage to self-attention (prompt encoding) and improve performance (un-tuned) as well as dispatch heuristics.

[1] https://arxiv.org/abs/2112.05682
[2] https://pytorch.org/blog/flash-decoding/

Supported

  • Supports mx.float16 and mx.float32 dtypes
  • Supports head_dim=128 (covers most 7b+ LLMs)
  • New Metal kernel added by this PR covers KV-cached forward passes
  • Standard prompt encoding still goes through MLX primitives under the hood.
  • No backward pass implementation (inference-only kernel)

Future Work

  • Support for head_dim=64 (Whisper). Other models need discussion (e.g. head_dim=80 for phi)
  • Extension to query_sequence_length > 1 (non KV-cached inference, i.e. speculative decoding, prompt encoding, ViT, DiT etc.)
  • Auto-tuning of TILE_SIZE and N_SIMD_GROUPS
  • Profiling and performance tuning
  • Refactor the second kernel (reduction across KV cache tiles) from naive implementation to shave overhead)
  • Re-examine loads from K, V to maximize shared memory utilization
    • Esp. for tile size 128 with half, and tile size 64 with float32: can fit all of values in to shared memory
  • Fast CPU implementation
  • mx.bfloat16 support

Quick measurements

  • We will share principled measurements on our Apple Silicon Mac test fleet (M1 through M3) before merging the PR
  • First measurements to kick off discussions:
    • MacBook Pro M3 Max + macOS 23B2091 & MLX 972d9a3 (Feb 21) & model = mx.compile(model)
    • Up to ~2.5x higher tokens/second for Mistral-7b. for large sequence length ~8k tokens
    • Majority of the improvement is attributable to native GQA support (no tiling of KV cache, compresses KV cache by 4x for Mistral)

Proposed changes

New fast.scaled_dot_product_attention op, with C API and Python bindings.

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

Implements metal shaders for:

o = mx.fast_inference_sdpa(queries, keys, values, scale, mask)

Supports fp16, fp32 dtypes; assumes d_k = 128.

Generic op support / prompt encoding supported via mlx primitives.
Metal implementation is for the inference use case only.

Majority of performance benefits appears to results from GQA & reduced
bandwidth requirements; there is approximate performance parity for the
MHA use case (from some measurements on M3 Max).
@atiorh

atiorh commented Feb 23, 2024

Copy link
Copy Markdown
Contributor

Quick measurements:
mlx_speedup_720

@bpkeene

bpkeene commented Feb 23, 2024

Copy link
Copy Markdown
Contributor Author

Happy to move this in to a non-core-primitive namespace as discussed previously, please let me know initial feedback and I'll incorporate in an update to the PR!

@bmorphism

Copy link
Copy Markdown

lgtm

@awni

awni commented Feb 24, 2024

Copy link
Copy Markdown
Member

Happy to move this in to a non-core-primitive namespace as discussed previously, please let me know initial feedback and I'll incorporate in an update to the PR!

@bpkeene I do think this op makes sense to put in the mlx::core::fast namespace / mlx.core.fast subpackage. E.g. see the fast rope function. Wdyt? Should be relatively straightforward to move right?

@fblissjr

Copy link
Copy Markdown

This is awesome!

@angeloskath

Copy link
Copy Markdown
Member

@bpkeene this looks really good, excited to do a review when I get back to a computer.

Otherwise, I was about to comment the same thing as Awni, the way mlx::core::fast is set up you provide an mlx function that will be called when gradients are needed so this can be used in more general situations and it will just be faster when doing inference. Rope is indeed a good example use case.

@bpkeene

bpkeene commented Feb 24, 2024

Copy link
Copy Markdown
Contributor Author

absolutely! I'll take a look at the rope related PR for the fast namespace, I think it should be straightforward

@awni

awni commented Feb 24, 2024

Copy link
Copy Markdown
Member

I also suggest removing fast_inference from the name, I think it will be self-evident if it's in mx.fast. Some ideas:

  • Just sdpa
  • scaled_dot_product_attention like PyTorch
  • multi_head_attention (maps nicely to the MultiHeadAttention layer)

@adi-dhulipala

adi-dhulipala commented Feb 25, 2024 •

Copy link
Copy Markdown
Contributor

Quick measurements: mlx_speedup_720

@atiorh curious if argmax team is considering updating mlx mistral example using the new op? And perhaps sharing the benchmark/test that generated this comparison? It would be nice to have the community run on their variety of hardware and try to reproduce the results or its variances

Thinking out loud here, I suppose it's as straightforward as replacing

        scores = (queries * self.scale) @ keys.transpose(0, 1, 3, 2)
        if mask is not None:
            scores += mask
        scores = mx.softmax(scores.astype(mx.float32), axis=-1).astype(scores.dtype)

with

        scores = mx.fast.sdpa(q, k, v, self.scale, mask) # or mx.core.fast_inference_sdpa or whatever the final op name is

in mistral.py https://github.com/ml-explore/mlx-examples/blob/main/llms/mistral/mistral.py#L89-L92
?

@bpkeene

bpkeene commented Feb 25, 2024

Copy link
Copy Markdown
Contributor Author

Quick measurements: mlx_speedup_720

@atiorh curious if argmax team is considering updating mlx mistral example using the new op? And perhaps sharing the benchmark/test that generated this comparison? It would be nice to have the community run on their variety of hardware and try to reproduce the results or its variances

Thinking out loud here, I suppose it's as straightforward as replacing

        scores = (queries * self.scale) @ keys.transpose(0, 1, 3, 2)
        if mask is not None:
            scores += mask
        scores = mx.softmax(scores.astype(mx.float32), axis=-1).astype(scores.dtype)

with

        scores = mx.fast.sdpa(q, k, v, self.scale, mask) # or mx.core.fast_inference_sdpa or whatever the final op name is

in mistral.py https://github.com/ml-explore/mlx-examples/blob/main/llms/mistral/mistral.py#L89-L92 ?

great point!

just about, it's important as well to look a couple lines above in mistral.py and remove the tiling of the KV cache to get the performance benefits of GQA quoted above (the 'repeat()' function)

@atiorh

atiorh commented Feb 25, 2024

Copy link
Copy Markdown
Contributor

@adhulipa Preparing https://github.com/argmaxinc/mlx-bench at the moment for this purpose. We have internal CI machines (M1 through M3) for periodic benchmarking but this public benchmarking script should be useful for PRs like this 👍

atiorh added a commit to argmaxinc/mlx-examples that referenced this pull request Feb 25, 2024
atiorh added a commit to argmaxinc/mlx-bench that referenced this pull request Feb 25, 2024
@atiorh

atiorh commented Feb 25, 2024

Copy link
Copy Markdown
Contributor

Ready to use as a bench utility here: argmaxinc/mlx-bench#1

@bpkeene
bpkeene marked this pull request as draft February 26, 2024 07:17
@bpkeene

bpkeene commented Feb 26, 2024

Copy link
Copy Markdown
Contributor Author

Converting this to a draft while we're investigating some M1 & M2 specific numerical issues. M3 Max / 23B2091 has proper tokens generated and appears unaffected. We'll continue addressing the other issues in tandem with the investigation

Comment thread python/tests/test_autograd.py Resolved
@@ -0,0 +1,114 @@
# Created by Brian Keene, 2024

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@bpkeene we are putting acknowledgements in the ACKNOWLEDGEMENTS.md file rather than have author / maintainer lists in individual source code files.

@bpkeene bpkeene Feb 29, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sgtm! will move to ACKNOWLEDGEMENTS.md & add folks :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Awesome! Please take liberty to add anyone who contributed to this on your team (e.g. Atila as well)

Comment thread python/src/ops.cpp Resolved
Comment thread python/src/ops.cpp Resolved
Comment thread python/src/ops.cpp Resolved
Comment thread python/src/ops.cpp Resolved
Comment thread python/src/ops.cpp Resolved

@awni awni left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@bpkeene I took a look everything looks really great so far!

Before I review the API further, I think it makes sense to change to the fast package / namespace and inherit from the Custom primitive. That is conceptually pretty straightforward, you have all the pieces in place but might be a big diff. It should also simplify some stuff (like you can remove the VJP/JVP/VMAP for SDPA).

@malfet

malfet commented Feb 28, 2024

Copy link
Copy Markdown

@bpkeene would you be interested in porting this kernel to PyTorch? Will be happy to help with the port

atiorh added a commit to argmaxinc/mlx-bench that referenced this pull request Feb 28, 2024
@bpkeene

bpkeene commented Feb 28, 2024

Copy link
Copy Markdown
Contributor Author

@bpkeene would you be interested in porting this kernel to PyTorch? Will be happy to help with the port

@malfet we’d be happy to guide a port to pytorch if anyone would like to take point, if so please cc us and we’re here to help

... also attempt to revert formatter auto-change for files not relevant
to this change
@bpkeene

bpkeene commented Feb 29, 2024 •

Copy link
Copy Markdown
Contributor Author

running additional tests before removing draft status, but the op is now moved to mlx::core::fast:: (cpp) and mx.fast (python) namespaces using the Custom primitives, with a more thorough fallback_() lambda function backing it (prompt encoding & runs on CPU stream, with metal shaders exercised for GPU stream in the decoding use case)

@bpkeene
bpkeene marked this pull request as ready for review February 29, 2024 20:07
Comment thread ACKNOWLEDGMENTS.md
- Gabrijel Boduljak: Added `mlx.core.linalg`, implemented `norm` method and `InstanceNorm` layer. Implemented ``MaxPool1d``, ``MaxPool2d``, ``AvgPool1d``, ``AvgPool2d``.
- Hinrik Snær Guðmundsson: Added `atleast_1d`, `atleast_2d`, `atleast_3d` ops.

- Brian Keene & Atila Orhon, with Argmax Inc.: Added `fast.scaled_dot_product_attention`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

❤️

Comment thread python/src/fast.cpp Resolved
Comment thread python/src/fast.cpp Resolved
Comment thread python/src/fast.cpp Resolved
Comment thread python/src/fast.cpp Resolved
Comment thread python/src/fast.cpp Resolved
Comment thread mlx/fast_primitives.h Resolved
Comment thread mlx/fast_primitives.h Resolved
Comment thread mlx/fast_primitives.h Resolved
Comment thread mlx/fast_primitives.h Resolved

@awni awni left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Really really nice! Can't wait to land this. Left a few more mostly cosmetic suggestions.

@jagrit06 is planning to check out the kernel shortly, but we can aim to get this landed very soon.

Comment thread mlx/fast.cpp Resolved
Comment thread mlx/fast.cpp
if (tensor.shape(0) != batch_dim) {
std::ostringstream msg;
msg << "[scaled_dot_product_attention] mismatching batch dimension for input with shape "
<< tensor.shape() << ".";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
<< tensor.shape() << ".";
<< tensor.shape() << " and batch size " << batch_dim << ".";

Comment thread mlx/fast.cpp Resolved
Comment thread mlx/fast.cpp Resolved
Comment thread mlx/fast.cpp Resolved
Comment thread mlx/fast.cpp Resolved
Comment thread mlx/fast.cpp Resolved

@jagrit06 jagrit06 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd want to do a deeper dive and another pass over this kernel at some later date - but for now, since it works and looks to be fast, I won't block merging it in!

Just a couple questions popped up while I was going over the kernel I was hoping you could clarify

Comment on lines +260 to +266
simdgroup_matrix<T, 8, 8> tmp;
ulong2 matrixOrigin = ulong2(col * SIMDGROUP_MATRIX_LOAD_FACTOR, tile_start);
simdgroup_load(tmp, baseVThisHead, DK, matrixOrigin, /* transpose */ true);
const ulong2 matrixOriginSmem = ulong2(smem_col_index, 0);
constexpr const ulong elemsPerRowSmem = TILE_SIZE_CONST;
simdgroup_store(tmp, smemV, elemsPerRowSmem, matrixOriginSmem, /* transpose */ false);
smem_col_index += NSIMDGROUPS * SIMDGROUP_MATRIX_LOAD_FACTOR;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same comment as above

@awni

awni commented Mar 2, 2024

Copy link
Copy Markdown
Member

Also, looks like the linux build is just missing an include (#include <optional> in fast.h)

atiorh and others added 8 commits March 3, 2024 21:05
Co-authored-by: Awni Hannun <[email protected]>
Co-authored-by: Awni Hannun <[email protected]>
Co-authored-by: Awni Hannun <[email protected]>
Co-authored-by: Awni Hannun <[email protected]>
* route to fallback for more use cases - batch size > 1, head_dim other
  than 128, etc.
* Address linux build failure
* Address other reviewer comments

@awni awni left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🚀 this is an exceptional contribution, thanks @bpkeene and @atiorh for making it happen!!

@awni
awni merged commit 0787724 into ml-explore:main Mar 5, 2024
awni added a commit that referenced this pull request Mar 5, 2024
* Fast Inference SDPA op

Implements metal shaders for:

o = mx.fast_inference_sdpa(queries, keys, values, scale, mask)

Supports fp16, fp32 dtypes; assumes d_k = 128.

Generic op support / prompt encoding supported via mlx primitives.
Metal implementation is for the inference use case only.

Majority of performance benefits appears to results from GQA & reduced
bandwidth requirements; there is approximate performance parity for the
MHA use case (from some measurements on M3 Max).

* Flush shared memory to zero before unprotected reads for (scores @ values)

* Move to fast:: namespace, address reviewer comments

... also attempt to revert formatter auto-change for files not relevant
to this change

* Shared memory flush to top of kernel

* Resolve compiler warnings

* Update python/src/fast.cpp

Co-authored-by: Awni Hannun <[email protected]>

* Update python/src/fast.cpp

Co-authored-by: Awni Hannun <[email protected]>

* Update python/src/fast.cpp

Co-authored-by: Awni Hannun <[email protected]>

* Update python/src/fast.cpp

Co-authored-by: Awni Hannun <[email protected]>

* Update docstring per PR feedback

* Softmax in higher precision, ...

* route to fallback for more use cases - batch size > 1, head_dim other
  than 128, etc.
* Address linux build failure
* Address other reviewer comments

* Remove extraneous eval_cpu function per review

---------

Co-authored-by: Atila Orhon <[email protected]>
Co-authored-by: Awni Hannun <[email protected]>
Co-authored-by: atila <[email protected]>
@bpkeene
bpkeene deleted the user/bkeene/fast_inference_sdpa branch March 6, 2024 20:47
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.

9 participants