Skip to content

metal: more PP speedup for Falcon - #3123

Closed
ikawrakow wants to merge 10 commits into
masterfrom
ik/metal_falcon_pp
Closed

ikawrakow wants to merge 10 commits into
masterfrom
ik/metal_falcon_pp

Conversation

@ikawrakow

Copy link
Copy Markdown
Contributor

TL;DR: For PP on metal, a significant fraction of the time is spent in the K * Q matrix multiplication. The PR is an attempt to speed up this matrix multiplication. The result is mostly useful for the Falcon model where we see a ~12% increase in performance.

If I turn the f16 x f32 matrix multiplication operation into a no-op, I see a 51% increase in PP performance for Falcon-7B and 17% gain for LLaMA-7B on metal. This explains most of the PP performance gap between between Falcon and LLaMA. Most of the time spent in f16 x f32 matrix multiplications is due to the K * Q matrix multiplication. On metal, the kernel utilized for most other matrix multiplications cannot be used because the K and Q tensors are not contiguous. Hence, a poor-men's matrix multiplication kernel is invoked, which becomes a bottleneck as the context/batch size increases. This PR adds a slightly better version, which I think is still very far from optimal. The effect is a ~12% speedup for Falcon-7B PP, along with some very minor gains for TG.

30-core M2 Max:

model backend test /s (Master) t/s (PR) Speedup
Falcon 7B mostly F16 Metal pp 512 380.18 ± 0.13 426.83 ± 0.26 1.123
LLaMA 7B mostly F16 Metal pp 512 539.46 ± 0.31 539.61 ± 0.37 1.000
LLaMA 7B mostly Q8_0 Metal pp 512 486.91 ± 0.18 486.83 ± 0.44 1.000
LLaMA 7B mostly Q4_0 Metal pp 512 495.91 ± 0.38 495.82 ± 0.17 1.000
LLaMA 7B mostly Q4_1 Metal pp 512 494.31 ± 0.26 494.52 ± 0.55 1.000
LLaMA 7B mostly Q6_K Metal pp 512 413.82 ± 0.39 414.16 ± 0.23 1.001
LLaMA 7B mostly Q5_K - Small Metal pp 512 413.35 ± 0.27 413.58 ± 0.12 1.000
LLaMA 7B mostly Q4_K - Small Metal pp 512 438.64 ± 0.23 438.39 ± 0.18 1.000
LLaMA 7B mostly Q3_K - Small Metal pp 512 417.91 ± 0.25 417.79 ± 0.25 1.000
Falcon 7B mostly F16 Metal tg 128 23.33 ± 0.04 23.51 ± 0.05 1.008
LLaMA 7B mostly F16 Metal tg 128 24.26 ± 0.04 24.24 ± 0.07 1.000
LLaMA 7B mostly Q8_0 Metal tg 128 40.16 ± 0.27 40.35 ± 0.25 1.005
LLaMA 7B mostly Q4_0 Metal tg 128 62.53 ± 0.06 63.34 ± 0.04 1.013
LLaMA 7B mostly Q4_1 Metal tg 128 58.88 ± 0.78 58.62 ± 0.02 0.996
LLaMA 7B mostly Q6_K Metal tg 128 42.93 ± 0.48 43.21 ± 0.46 1.006
LLaMA 7B mostly Q5_K - Small Metal tg 128 44.67 ± 0.03 45.79 ± 0.03 1.025
LLaMA 7B mostly Q4_K - Small Metal tg 128 57.31 ± 0.44 57.86 ± 0.04 1.010
LLaMA 7B mostly Q3_K - Small Metal tg 128 53.42 ± 0.24 53.77 ± 0.17 1.007

@ikawrakow
ikawrakow marked this pull request as ready for review September 11, 2023 14:25
@ggerganov

ggerganov commented Sep 13, 2023 •

Copy link
Copy Markdown
Member

Sorry for the delay - will look into this in more details soon.

On metal, the kernel utilized for most other matrix multiplications cannot be used because the K and Q tensors are not contiguous.

Interesting observation and I guess I just realize something that could be important.
I quickly tried the following change on master:

diff --git a/ggml-metal.m b/ggml-metal.m
index 4f3f14e..cc34a76 100644
--- a/ggml-metal.m
+++ b/ggml-metal.m
@@ -867,8 +867,7 @@ void ggml_metal_graph_compute(
 
                             // for now the matrix-matrix multiplication kernel only works on A14+/M1+ SoCs
                             // AMD GPU and older A-chips will reuse matrix-vector multiplication kernel
-                            if (ggml_is_contiguous(src0) &&
-                                ggml_is_contiguous(src1) &&
+                            if (ggml_is_contiguous(src1) &&
                                 src1t == GGML_TYPE_F32 &&
                                 [ctx->device supportsFamily:MTLGPUFamilyApple7] &&
                                 ne00%32 == 0 &&
diff --git a/llama.cpp b/llama.cpp
index cbaf8ed..a30b777 100644
--- a/llama.cpp
+++ b/llama.cpp
@@ -2760,7 +2760,7 @@ static struct ggml_cgraph * llm_build_falcon(
             offload_func_kq(K);
             ggml_set_name(K, "K");
 
-            struct ggml_tensor * KQ = ggml_mul_mat(ctx0, K, Q);
+            struct ggml_tensor * KQ = ggml_mul_mat(ctx0, K, ggml_cont(ctx0, Q));
             offload_func_kq(KQ);
             ggml_set_name(KQ, "KQ");

The idea is that Q tensor is indeed not contiguous so we add ggml_cont(ctx0, Q) to make a contiguous copy.

However, the K tensor is actually contiguous so we don't need to do anything, just relax the condition for the fast mul_mm_f16_f32 kernel. The tensor is contiguous across dimensions 0 and 1, but not across dimension 2 (the heads).
But this is enough for the kernel to work correctly.

With this change I get the following benchmarks:

  • diff
model size params backend ngl test t/s
Falcon 7B mostly F16 13.44 GiB 7.22 B Metal 99 pp 512 1363.51 ± 3.30
Falcon 7B mostly F16 13.44 GiB 7.22 B Metal 99 tg 256 39.12 ± 0.05

build: 71ca2fa (1221)

  • master
model size params backend ngl test t/s
Falcon 7B mostly F16 13.44 GiB 7.22 B Metal 99 pp 512 890.50 ± 0.92
Falcon 7B mostly F16 13.44 GiB 7.22 B Metal 99 tg 256 39.65 ± 0.02

build: 71ca2fa (1221)

Here is LLaMA 7B on master without the diff (i.e. no ggml_cont(Q)) for reference:

model size params backend ngl test t/s
LLaMA 7B mostly F16 12.55 GiB 6.74 B Metal 99 pp 512 1269.55 ± 0.57
LLaMA 7B mostly F16 12.55 GiB 6.74 B Metal 99 tg 256 40.25 ± 0.03

build: 71ca2fa (1221)

And this is with the same trick applied to the LLaMA graph:

model size params backend ngl test t/s
LLaMA 7B mostly F16 12.55 GiB 6.74 B Metal 99 pp 512 1481.27 ± 0.76
LLaMA 7B mostly F16 12.55 GiB 6.74 B Metal 99 tg 256 39.78 ± 0.07

build: 71ca2fa (1221)

I think this might be worth looking into more. I'm not yet sure if the additional ggml_cont would break something with the other formats or if it will introduce some extra regressions - will look into it. Probably it will hurt slightly TG, but there might be an easy way around it.

@ikawrakow

Copy link
Copy Markdown
Contributor Author

Sure, copying Q to contiguous memory is an obvious approach, but I wanted to avoid the copy to not have the TG performance degradation. For LLaMA-7B the Q tensor is 128 x Nb x 32, so the amount of data to be copied becomes non-trivial when generating a long sequence of tokens.

@ggerganov

Copy link
Copy Markdown
Member

During TG, Nb == 1, so the Q tensor is always the same size and it is small. Here is for example the graph for n_past == 45 and n_batch == 1:

image

@ikawrakow

Copy link
Copy Markdown
Contributor Author

Given #3168, I'm closing this PR.

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.

2 participants