llama: create new fused op for QSA mask (#30640)
* llama: create new fused op for QSA scoring * enroll glm5.3-next too * tests : initialize the remaining tensors in test_kpool_mask The harness sentinels were left uninitialized, so leftover memory could read as NaN and fail the NaN check. * ggml: generalize the k-pool mask op into ggml_top_k_mask The k-pool mask op only covered the pooled QSA selection of qwen4exp and glm5-next, while the DSA models and the DeepSeek V4 CSA build the same top-k mask by hand with fill, set_rows and add. ggml_top_k_mask(mask, top_k, score, blocks, extra) keeps the mask cells of the live picks and the extra cells, and sets the rest to -inf. Every row tensor is 4D in the layout of top_k, and the mask is broadcast to those rows, so a selection can also be made per head. score, blocks and extra are optional: without score every pick is live, without blocks a pick is a cell, and extra holds the cells a row keeps without a pick. llm_graph_context::build_fused_top_k_mask views the row tensors in the layout of kq_mask and registers the fused node. The k-pool models, the DSA attention of build_attn and the DeepSeek V4 CSA mask use it when the probe enables the op, and keep their current path on backends without it. --------- Co-authored-by: Pascal <admin@serveurperso.com>
This commit is contained in:
1 parent
0756e83fc1
commit
d454a204ef
18 files changed
+592
-31
No files matched your search
@@ -9128,6 +9128,148 @@ struct test_lightning_indexer : public test_case {
|
||||
}
|
||||
};
|
||||
|
||||
// GGML_OP_TOP_K_MASK
|
||||
struct test_top_k_mask : public test_case {
|
||||
const ggml_type type;
|
||||
const int64_t n_kv;
|
||||
const int64_t n_blk; // 0: no blocks, a pick is a cell
|
||||
const int64_t n_pick; // number of blocks, ignored without blocks
|
||||
const int64_t n_top;
|
||||
const int64_t n_extra; // 0: no extra cells
|
||||
const bool score; // false: every pick is live
|
||||
const int64_t nb; // rows per stream
|
||||
const int64_t nh; // rows of top_k per mask row, broadcast over ne2
|
||||
const int64_t ns; // streams
|
||||
const bool real; // blocks of consecutive cells, live picks, as in a real cache
|
||||
|
||||
std::string vars() override {
|
||||
return VARS_TO_STR11(type, n_kv, n_blk, n_pick, n_top, n_extra, score, nb, nh, ns, real);
|
||||
}
|
||||
|
||||
// the result is a copy of the mask or -inf, the harness checks the -inf pattern, the finite values must match exactly
|
||||
double max_err() override {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
double err(const float * a, const float * b, size_t n) override {
|
||||
double result = 0.0;
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
if (std::isfinite(a[i]) && std::isfinite(b[i])) {
|
||||
result = std::max(result, (double) fabsf(a[i] - b[i]));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
size_t op_size(ggml_tensor * t) override {
|
||||
const size_t ts = ggml_type_size(type);
|
||||
const size_t nc = std::max<int64_t>(n_blk, 1);
|
||||
return ggml_nbytes(t) + nb*nh*ns*(n_top*(sizeof(int32_t) + (score ? sizeof(float) : 0) + nc*(sizeof(int32_t) + 2*ts)) + n_extra*(sizeof(int32_t) + 2*ts));
|
||||
}
|
||||
|
||||
test_top_k_mask(ggml_type type = GGML_TYPE_F16, int64_t n_kv = 256, int64_t n_blk = 4, int64_t n_pick = 64,
|
||||
int64_t n_top = 16, int64_t n_extra = 3, bool score = true, int64_t nb = 32, int64_t nh = 1, int64_t ns = 1, bool real = false)
|
||||
: type(type), n_kv(n_kv), n_blk(n_blk), n_pick(n_blk ? n_pick : n_kv), n_top(n_top), n_extra(n_extra), score(score),
|
||||
nb(nb), nh(nh), ns(ns), real(real) {}
|
||||
|
||||
ggml_tensor * build_graph(ggml_context * ctx) override {
|
||||
ggml_tensor * m = ggml_new_tensor_4d(ctx, type, n_kv, nb, 1, ns);
|
||||
ggml_set_name(m, "m");
|
||||
|
||||
ggml_tensor * top_k = ggml_new_tensor_4d(ctx, GGML_TYPE_I32, n_top, nb, nh, ns);
|
||||
ggml_set_name(top_k, "top_k");
|
||||
|
||||
ggml_tensor * s = nullptr;
|
||||
if (score) {
|
||||
s = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, n_pick, nb, nh, ns);
|
||||
ggml_set_name(s, "score");
|
||||
}
|
||||
|
||||
ggml_tensor * blocks = nullptr;
|
||||
if (n_blk > 0) {
|
||||
blocks = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, n_blk, n_pick);
|
||||
ggml_set_name(blocks, "blocks");
|
||||
}
|
||||
|
||||
ggml_tensor * extra = nullptr;
|
||||
if (n_extra > 0) {
|
||||
extra = ggml_new_tensor_4d(ctx, GGML_TYPE_I32, n_extra, nb, nh, ns);
|
||||
ggml_set_name(extra, "extra");
|
||||
}
|
||||
|
||||
ggml_tensor * out = ggml_top_k_mask(ctx, m, top_k, s, blocks, extra);
|
||||
ggml_set_name(out, "out");
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
void initialize_tensors(ggml_context * ctx) override {
|
||||
std::mt19937 gen(0x4B50);
|
||||
|
||||
// cells and picks sometimes fall out of range, the op skips them
|
||||
auto init_cells = [&](ggml_tensor * t) {
|
||||
std::vector<int32_t> data(ggml_nelements(t));
|
||||
if (real) {
|
||||
// block p holds cells p*n_blk..p*n_blk + n_blk - 1, the extra cells are the last cells
|
||||
const bool is_blocks = strcmp(t->name, "blocks") == 0;
|
||||
for (int64_t i = 0; i < (int64_t) data.size(); ++i) {
|
||||
const int64_t c = is_blocks ? i : n_kv - 1 - i % t->ne[0];
|
||||
data[i] = c < n_kv ? (int32_t) c : (int32_t) n_kv;
|
||||
}
|
||||
} else {
|
||||
std::uniform_int_distribution<int32_t> cell(0, n_kv - 1);
|
||||
for (auto & c : data) {
|
||||
const uint32_t r = gen() % 16;
|
||||
c = r == 0 ? (int32_t) n_kv : r == 1 ? -1 : cell(gen);
|
||||
}
|
||||
}
|
||||
ggml_backend_tensor_set(t, data.data(), 0, data.size()*sizeof(int32_t));
|
||||
};
|
||||
|
||||
for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
|
||||
if (strcmp(t->name, "m") == 0) {
|
||||
if (type == GGML_TYPE_F16) {
|
||||
init_tensor_kq_mask(t);
|
||||
} else {
|
||||
std::vector<float> data(ggml_nelements(t));
|
||||
std::uniform_real_distribution<float> dis(-1.0f, 1.0f);
|
||||
for (auto & v : data) {
|
||||
v = gen() % 4 == 0 ? -INFINITY : dis(gen);
|
||||
}
|
||||
ggml_backend_tensor_set(t, data.data(), 0, data.size()*sizeof(float));
|
||||
}
|
||||
} else if (strcmp(t->name, "score") == 0) {
|
||||
// -inf marks the picks a row cannot see
|
||||
std::vector<float> data(ggml_nelements(t));
|
||||
std::uniform_real_distribution<float> dis(-1.0f, 1.0f);
|
||||
for (auto & v : data) {
|
||||
v = !real && gen() % 4 == 0 ? -INFINITY : dis(gen);
|
||||
}
|
||||
ggml_backend_tensor_set(t, data.data(), 0, data.size()*sizeof(float));
|
||||
} else if (strcmp(t->name, "top_k") == 0) {
|
||||
std::vector<int32_t> data(ggml_nelements(t));
|
||||
std::vector<int32_t> order(n_pick);
|
||||
for (int64_t r = 0; r < ggml_nrows(t); ++r) {
|
||||
for (int64_t i = 0; i < n_pick; ++i) {
|
||||
order[i] = i;
|
||||
}
|
||||
std::shuffle(order.begin(), order.end(), gen);
|
||||
for (int64_t j = 0; j < n_top; ++j) {
|
||||
const bool out = !real && j == 0 && r % 5 == 0;
|
||||
data[r*n_top + j] = out ? (r % 2 ? -1 : (int32_t) n_pick) : order[j % n_pick];
|
||||
}
|
||||
}
|
||||
ggml_backend_tensor_set(t, data.data(), 0, data.size()*sizeof(int32_t));
|
||||
} else if (strcmp(t->name, "blocks") == 0 || strcmp(t->name, "extra") == 0) {
|
||||
init_cells(t);
|
||||
} else {
|
||||
// the harness sentinels and the output, the callback checks the sentinels for NaN
|
||||
init_tensor_uniform(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Deserializable generic test case
|
||||
struct input_tensor {
|
||||
ggml_type type;
|
||||
@@ -9912,6 +10054,25 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
|
||||
test_cases.emplace_back(new test_dsv4_hc_post(2560, 21, true, true));
|
||||
test_cases.emplace_back(new test_dsv4_hc_post(31, 17, false, true));
|
||||
|
||||
for (ggml_type type : {GGML_TYPE_F16, GGML_TYPE_F32}) {
|
||||
// blocks with extra cells and scores (k-pool)
|
||||
test_cases.emplace_back(new test_top_k_mask(type));
|
||||
test_cases.emplace_back(new test_top_k_mask(type, 1001, 4, 70, 16, 3, true, 7));
|
||||
test_cases.emplace_back(new test_top_k_mask(type, 256, 4, 64, 64, 0, true, 33));
|
||||
test_cases.emplace_back(new test_top_k_mask(type, 512, 8, 16, 32, 7, true, 5, 1, 4));
|
||||
test_cases.emplace_back(new test_top_k_mask(type, 4096, 4, 1024, 512, 3, true, 1));
|
||||
test_cases.emplace_back(new test_top_k_mask(type, 4096, 4, 1024, 512, 3, true, 64, 1, 2));
|
||||
// cells without scores (DSA)
|
||||
test_cases.emplace_back(new test_top_k_mask(type, 256, 0, 0, 16, 0, false, 32));
|
||||
test_cases.emplace_back(new test_top_k_mask(type, 1001, 0, 0, 64, 0, false, 7, 1, 3));
|
||||
test_cases.emplace_back(new test_top_k_mask(type, 4096, 0, 0, 2048, 0, false, 5));
|
||||
// cells with scores and extra cells
|
||||
test_cases.emplace_back(new test_top_k_mask(type, 512, 0, 0, 32, 5, true, 9));
|
||||
// blocks picked per head, the mask broadcast over the heads
|
||||
test_cases.emplace_back(new test_top_k_mask(type, 512, 16, 32, 4, 0, true, 6, 4, 1));
|
||||
test_cases.emplace_back(new test_top_k_mask(type, 1024, 8, 128, 16, 8, false, 3, 2, 2));
|
||||
}
|
||||
|
||||
// glu ops
|
||||
for (ggml_type type : {GGML_TYPE_F16, GGML_TYPE_F32, GGML_TYPE_BF16}) {
|
||||
for (int v : {0, 1}) {
|
||||
@@ -12745,6 +12906,14 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_perf() {
|
||||
}
|
||||
}
|
||||
|
||||
// top_k_mask, Flash-Next QSA shapes: 512 pools of 4 + a 3-cell tail, and DSA shapes: 2048 cells
|
||||
for (int64_t n_kv : { 18432, 131072 }) {
|
||||
for (int64_t nb : { 1, 2048 }) {
|
||||
test_cases.emplace_back(new test_top_k_mask(GGML_TYPE_F16, n_kv, 4, n_kv/4, 512, 3, true, nb, 1, 1, true));
|
||||
test_cases.emplace_back(new test_top_k_mask(GGML_TYPE_F16, n_kv, 0, 0, 2048, 0, false, nb, 1, 1, true));
|
||||
}
|
||||
}
|
||||
|
||||
// launch-overhead isolation: single L2_NORM launch vs batched siblings at the GDN
|
||||
// production shape (strided qkv views) -- perf-mode only, the eval list has its own
|
||||
// 2/4-wide coverage
|
||||
|
||||
Reference in new issue
Block a user