Skip to content

Commit 13e6738

Browse files
hexagon: flash attention rework (optimizations, accuracy improvements, etc) (ggml-org#25085)
* hex-mm: fold mm quant tasks into the main matmul threads * hex-mm: minor formatting fixes * hex-mm: cleanup is_quant checks in dma dispatch * hex-mm: fix dst-spad alignment * hex-mm: move fp kernels in the hvx-mm-kernels header * hex-mm: fuse with ADD * hex-fa: factor out ukernels into separate headers and unify the rest * hex-fa: move kernel-params compute into the host * hex-fa: refactor vtcm alloc for consistency * hex-fa: add support for FA_SELECT * hex-fa: update tracing insrumentation to cover all functions * hex-fa: update hvx fallback thresholds to recover t/g regressions * hex-fa: update tracing instrumentation * hex-fa: improved tracing with additional events * hex-fa: optimize mask processing (fastdiv, etc) * hex-fa: improve mask dma caching * hmx-fa: change loop order to maximize mask cache hits * hex-fa: remove over instrumentation * hex-fa: breakdown QKV prep trace events * hmx-fa: further mask proc optimizations * hex-fa: mask broadcast is the common case, optimize for that * hex-fa: use aligned loads where possible * hex-fa: update loops to use uint32_t indices * hmx-fa: fold vtcm init into q prep task * hex-fa: update rest of the hmx funcs to use uint32_t * hmx-fa: fold build_d into the main softmax loop * hmx-fa: start kv dmas earlier * hmx-fa: start mask dma a bit earlier * hex-fa: precompute rows per task to avoid divs * hmx-fa: specialize fa_o_store for f16 and f32 * hmx-fa: prelim support for Sinks * hmx-fa: keep softmax accumulators in fp32 * hex-fa: add tanh_f16 and exp2_f16 and use that in FA * hex-fa: use fp16 math in the hvx kernel * hex-fa: avoid expensive float -> __fp16 cast for slopes and softcap * hex-fa: replace most vec_exp_f32 with vec_exp2_f16 * hmx-fa: vectorize sinks update * hex-fa: minor formatting * hmx-fa: fold softcap loop into the tile load * hmx-fa: use vectoralias to populate sinks * hex-fa: remove redudant check * hex-fa: fix vtcm size compute to use fp32 for accumulators * hex-mm: fix trailing spaces * hmx-fa: dont use -inf to init mask to avoid conversion overflows * hex-fa: no need to explicitly guard -inf in the f16->f32 converter now * hmx-fa: cleanup fa sinks handling * hex-mm: fixed src2 stride handling when mm is fused with add * hex-fa: make lto happy
1 parent b820cc8 commit 13e6738

24 files changed

Lines changed: 3774 additions & 3020 deletions

ggml/src/ggml-hexagon/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ include(${HEXAGON_SDK_ROOT}/build/cmake/hexagon_fun.cmake)
2323
include(ExternalProject)
2424

2525
option(GGML_HEXAGON_HTP_DEBUG "ggml-hexagon: enable HTP debug output" OFF)
26-
option(GGML_HEXAGON_FA_EXP2_HF "ggml-hexagon: use FP16 exp2 polynomial in FA softmax instead of F32 exp round-trip" OFF)
2726
set(GGML_HEXAGON_HTP_CERT "$ENV{HEXAGON_HTP_CERT}" CACHE PATH "ggml-hexagon: enable HTP library signing using certificate")
2827

2928
add_library(htp_iface OBJECT

ggml/src/ggml-hexagon/ggml-hexagon.cpp

Lines changed: 221 additions & 26 deletions
Large diffs are not rendered by default.

ggml/src/ggml-hexagon/htp-opnode.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <stdio.h>
1212
#include "htp-ops.h"
1313
#include "htp/matmul-ops.h"
14+
#include "htp/flash-attn-ops.h"
1415

1516
struct htp_opnode {
1617
ggml_tensor * node = nullptr;
@@ -335,7 +336,8 @@ struct htp_opformat {
335336
}
336337
void format_kernel_params(char * str, size_t max_size, const htp_opnode & node) {
337338
if (node.opcode == HTP_OP_MUL_MAT || node.opcode == HTP_OP_MUL_MAT_ID ||
338-
node.opcode == HTP_OP_MUL_MAT_QKV || node.opcode == HTP_OP_MUL_MAT_FFN) {
339+
node.opcode == HTP_OP_MUL_MAT_QKV || node.opcode == HTP_OP_MUL_MAT_FFN ||
340+
node.opcode == HTP_OP_MUL_MAT_ADD) {
339341
const auto * kparams = (const struct htp_mm_kernel_params *) node.kernel_params;
340342
const char * path = "unknown";
341343
int32_t type = kparams->kernel_type;
@@ -350,6 +352,16 @@ struct htp_opformat {
350352
path = "hvx-flat";
351353
}
352354
snprintf(str, max_size, "%s vtcm %d", path, (int) kparams->vtcm_size);
355+
} else if (node.opcode == HTP_OP_FLASH_ATTN_EXT) {
356+
const auto * kparams = (const struct htp_fa_kernel_params *) node.kernel_params;
357+
const char * path = "unknown";
358+
int32_t type = kparams->kernel_type;
359+
if (type == HTP_FA_KERNEL_HMX) {
360+
path = kparams->u.hmx.pipeline ? "hmx-pipe" : "hmx-seq";
361+
} else if (type == HTP_FA_KERNEL_HVX) {
362+
path = "hvx";
363+
}
364+
snprintf(str, max_size, "%s vtcm %d", path, (int) kparams->vtcm_size);
353365
} else {
354366
snprintf(str, max_size, "----");
355367
}

ggml/src/ggml-hexagon/htp/CMakeLists.txt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ add_library(${HTP_LIB} SHARED
2020
worker-pool.c
2121
hex-dma.c
2222
hmx-queue.c
23-
flash-attn-ops.c
24-
hmx-flash-attn-ops.c
25-
matmul-ops.c
2623
binary-ops.c
2724
unary-ops.c
2825
sum-rows-ops.c
@@ -42,16 +39,14 @@ add_library(${HTP_LIB} SHARED
4239
solve-tri-ops.c
4340
gated-delta-net-ops.c
4441
pad-ops.c
42+
matmul-ops.c
43+
flash-attn-ops.c
4544
)
4645

4746
target_compile_definitions(${HTP_LIB} PRIVATE
4847
$<IF:$<BOOL:${HEXAGON_HTP_DEBUG}>,HTP_DEBUG=1,NDEBUG=1>
4948
$<IF:$<BOOL:${HEXAGON_HTP_DEBUG}>,FARF_HIGH=1,>)
5049

51-
if (GGML_HEXAGON_FA_EXP2_HF)
52-
message(STATUS "ggml-htp: HMX_FA_USE_EXP2_HF=1 (use FP16 exp2 polynomial in FA softmax)")
53-
target_compile_definitions(${HTP_LIB} PRIVATE HMX_FA_USE_EXP2_HF=1)
54-
endif()
5550

5651
build_idl(htp_iface.idl ${HTP_LIB})
5752

0 commit comments

Comments
 (0)