Faster CPU TG for GQA models#207
Merged
Merged
Conversation
This allows us to optimize TG performance for GQA models. E.g., for IQ4_XS L3-8B with 8k TG-64 goes from 8.6 to 10.26 t/s.
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.
This PR
iqkmatrix multiplication logic inggmlinto a newiqkfunctioniqk_mul_mat_4d. The change toggmlto incorporate theiqk-added functionality is now much less intusiveiqk_mul_mat_4dspecial handling of the TG case with GQA. In this case theKandVtensors have a shapeN x M x Lkv(Nis the head size,Lkvis the number of KV heads), and they multiply a tensor (QorK*Q) with shapeN x 1 x L(Lis the number of heads,L > Lkv). If we rearrangeQasN x L/Lkv x Lkv, we now have GEMM instead of GEMV, and this is significantly faster.This better approach only gives noticeable TG speedup for long context (large KV cache), as without that the fraction of time spent on the
K*QandV*softmax(K*Q)is small. So, here is a table comparing TG performance on main and with this PR for LLaMA-3.1-8B for different prompt lengths. Model is quantized withIQ4_XSand is running on a Ryzen-7950X (Zen4) or M2-Max CPUOn the M2-Max, which has a higher memory bandwidth (so better TG performance) but lower computing power than the Ryzen-7950X, the speedup is significantly higher.