Repository navigation
Conversation
Although, to me it looks like one should simply fuse scale + diagnonal infinity + soft_max on the KQtensor.
It does work for PP, but somehow it fails for TG. Need to look more into it.
This time more carefully
|
Sorry for the delay - will look into this in more details soon.
Interesting observation and I guess I just realize something that could be important. 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 However, the With this change I get the following benchmarks:
build: 71ca2fa (1221)
build: 71ca2fa (1221) Here is LLaMA 7B on
build: 71ca2fa (1221) And this is with the same trick applied to the LLaMA graph:
build: 71ca2fa (1221) I think this might be worth looking into more. I'm not yet sure if the additional |
|
Sure, copying |
|
Given #3168, I'm closing this PR. |

TL;DR: For PP on metal, a significant fraction of the time is spent in the
K * Qmatrix 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 f32matrix 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 inf16 x f32matrix multiplications is due to theK * Qmatrix multiplication. On metal, the kernel utilized for most other matrix multiplications cannot be used because theKandQtensors 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: