Compare commits

...
4 Commits
Author SHA1 Message Date
anujj 2a8bf0f593 cuda : fix NVFP4 MMQ y_scale over-read in the stream-k fixup (#33136) 2026-11-27 15:18:10 +05:30
Aman Gupta e7ec6361dc moe-cache : support experts selected on another backend (#33133)
When the experts are selected on another backend than the cache, e.g. the loop passes of inkling2_loop with a layer split, the lookup reads a copy of the ids made by the scheduler, which has no source to read the shape from. Take the experts per token from build_lookup instead.

Fixes #33101
2026-11-27 11:32:07 +02:00
Sigbjørn Skjæret 69812f63c2 llama : remove unused llm_tensor enums (#33179) 2026-11-27 10:18:09 +01:00
Georgi Gerganov f351f415d6 metal : check all fused nodes for concurrency (#33175)
* metal : check all fused nodes for concurrency

The concurrency check for a fused group only examined the last node, so
intermediate outputs (e.g. the TOPK_MOE ids/weights) and the group's
sources (e.g. the router logits) were ignored. This allowed the fused
top-k kernel to run concurrently with the router matmul that writes its
input, producing nondeterministic expert routing.

Check every node of the fused group instead.

Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)

* tests : update the Metal fusion baseline for bailingmoe3

PR #33081 enables the SSM_CONV + SILU fusion in bailingmoe3. Record the
new SSM_CONV+UNARY count in the Metal baseline.

Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
2026-11-27 11:10:50 +02:00
6 changed files with 21 additions and 9 deletions
+2 -2
View File
@@ -272,7 +272,7 @@ void ggml_cuda_mul_mat_q(
ggml_cuda_pool_alloc<char> src1_q8_1(ctx.pool(), nbytes_src1_q8_1);
ggml_cuda_pool_alloc<float> src1_scale(ctx.pool());
if (src0->type == GGML_TYPE_NVFP4 && use_native_fp4) {
src1_scale.alloc(ne13*ne12*ne11);
src1_scale.alloc(ne13*ne12*ne11 + J_best-1); // Needs to be padded for unconditional memory access.
}
{
@@ -342,7 +342,7 @@ void ggml_cuda_mul_mat_q(
ggml_cuda_pool_alloc<char> src1_q8_1(ctx.pool(), nbytes_src1_q8_1);
ggml_cuda_pool_alloc<float> src1_scale(ctx.pool());
if (src0->type == GGML_TYPE_NVFP4 && use_native_fp4) {
src1_scale.alloc(ne12*n_expert_used);
src1_scale.alloc(ne12*n_expert_used + J_best-1); // Needs to be padded for unconditional memory access.
}
const int64_t ne11_flat = ne12*n_expert_used;
+7 -2
View File
@@ -236,8 +236,13 @@ static int ggml_metal_op_encode_impl(ggml_metal_op_t ctx, int idx) {
int n_fuse = 1;
const ggml_metal_fusion * fusion = ctx->can_fuse(idx, GGML_METAL_FUSION_FULL, &n_fuse);
if (fusion) {
// fused kernels write to the last node of the group, not necessarily to the first node's dst
is_concurrent = ggml_mem_ranges_check(ctx->mem_ranges, ctx->node(idx + n_fuse - 1));
// every node of the group contributes sources and destinations, check all of them
for (int j = 0; j < n_fuse; ++j) {
if (!ggml_mem_ranges_check(ctx->mem_ranges, ctx->node(idx + j))) {
is_concurrent = false;
break;
}
}
}
}
-2
View File
@@ -674,8 +674,6 @@ enum llm_tensor {
LLM_TENSOR_LAYER_OUT_NORM,
LLM_TENSOR_LAYER_OUT_SCALE,
LLM_TENSOR_ATTN_VRES_LAMBDA,
LLM_TENSOR_POST_ATTN_NORM,
LLM_TENSOR_POST_MLP_NORM,
LLM_TENSOR_PER_LAYER_TOKEN_EMBD, // gemma3n
LLM_TENSOR_PER_LAYER_MODEL_PROJ, // gemma3n
LLM_TENSOR_PER_LAYER_INP_GATE, // gemma3n
+9 -3
View File
@@ -350,6 +350,7 @@ struct llama_moe_cache::impl {
ggml_tensor * cpu_map = nullptr; // I32 [1, n_expert] in host memory, if cpu_share > 0
std::vector<int32_t> cpu_experts; // experts set in cpu_map
uint32_t cpu_acc = 0; // fraction of a miss carried over to the next plan, in 1/65536
int32_t n_used = 0; // experts selected per token by the graph, set by build_lookup
std::vector<ggml_tensor *> experts; // host expert tensors, in the order of the banks
};
@@ -789,6 +790,7 @@ struct llama_moe_cache::impl {
// the selected experts must be computed in an earlier split
// the scheduler starts a new split at the lookup because it reads a host weight, unless the lookup is the first node of its split
// if they are selected on another backend (e.g. the loop passes of inkling2_loop), sel is the copy made by the scheduler before this callback
const ggml_tensor * sel = lookup->src[1];
for (int i = 0; i < n_nodes; ++i) {
const ggml_tensor * node = ggml_graph_node(graph, i);
@@ -797,9 +799,11 @@ struct llama_moe_cache::impl {
}
}
GGML_ASSERT(ggml_is_contiguous(sel));
GGML_ASSERT(sel->op == GGML_OP_RESHAPE); // of the [n_expert_used, n_tokens] ids, see build_lookup
const int32_t n_used = (int32_t) sel->src[0]->ne[0];
// a copy of the 1D ids has no source to read the shape from
const int32_t n_used = l.n_used;
GGML_ASSERT(n_used > 0 && ggml_nelements(sel) % n_used == 0);
const int64_t n_tokens = ggml_nelements(sel)/n_used;
ids.resize(ggml_nelements(sel));
ggml_backend_tensor_get_async(backend, sel, ids.data(), 0, ggml_nbytes(sel));
@@ -817,7 +821,7 @@ struct llama_moe_cache::impl {
return (size_t) (acc >> 16);
};
size_t n_hit = 0;
if (!g.lru.plan(il, ids.data(), ids.size(), sel->src[0]->ne[1], n_used, n_zero, n_cpu, fills, cpu, n_hit)) {
if (!g.lru.plan(il, ids.data(), ids.size(), n_tokens, n_used, n_zero, n_cpu, fills, cpu, n_hit)) {
GGML_ABORT("the MoE cache is too small for the experts selected in layer %d", il);
}
if (has_cpu) {
@@ -933,6 +937,8 @@ llama_moe_cache_lookup llama_moe_cache::build_lookup(const llm_graph_context & g
ggml_backend_sched_set_tensor_backend(g.sched, slots, pimpl->backend(il));
g.cb(slots, "ffn_moe_slots", il);
pimpl->layers[il].n_used = (int32_t) selected_experts->ne[0];
// add the lookup now: the transform of a Hadamard-folded expert reads host weights and would start its split first
ggml_build_forward_expand(g.gf, slots);
+1
View File
@@ -22,6 +22,7 @@ bailingmoe3 ,1 ,any ,GATED_DELTA_NET+CPY , 1
bailingmoe3 ,1 ,any ,MUL+ADD , 2
bailingmoe3 ,1 ,any ,RMS_NORM+MUL , 8
bailingmoe3 ,1 ,any ,RMS_NORM+SCALE , 2
bailingmoe3 ,1 ,any ,SSM_CONV+UNARY , 3
bailingmoe3 ,1 ,any ,UNARY+ADD+ARGSORT+GET_ROWS , 1
beam ,1 ,any ,MUL+ADD , 6
beam ,1 ,any ,RMS_NORM+MUL , 25
1 # test-fusion baseline for device MTL
22 bailingmoe3 ,1 ,any ,MUL+ADD , 2
23 bailingmoe3 ,1 ,any ,RMS_NORM+MUL , 8
24 bailingmoe3 ,1 ,any ,RMS_NORM+SCALE , 2
25 bailingmoe3 ,1 ,any ,SSM_CONV+UNARY , 3
26 bailingmoe3 ,1 ,any ,UNARY+ADD+ARGSORT+GET_ROWS , 1
27 beam ,1 ,any ,MUL+ADD , 6
28 beam ,1 ,any ,RMS_NORM+MUL , 25
+2
View File
@@ -13451,6 +13451,8 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
test_cases.emplace_back(new test_mul_mat_id_w4a8(GGML_TYPE_NVFP4, GGML_TYPE_F32, 4, 2, true, 64, 16, 256));
test_cases.emplace_back(new test_mul_mat_id_w4a4(GGML_TYPE_NVFP4, GGML_TYPE_F32, 8, 2, false, 32, 32, 256));
test_cases.emplace_back(new test_mul_mat_id_w4a4(GGML_TYPE_NVFP4, GGML_TYPE_F32, 4, 2, true, 64, 16, 256));
// stream-k fixup with a partial column tile
test_cases.emplace_back(new test_mul_mat_id_w4a4(GGML_TYPE_NVFP4, GGML_TYPE_F32, 4, 4, false, 32, 17, 1024));
test_cases.emplace_back(new test_mul_mat_w4a8(GGML_TYPE_MXFP4, GGML_TYPE_F32, 32, 32, 256));
test_cases.emplace_back(new test_mul_mat_w4a8(GGML_TYPE_MXFP4, GGML_TYPE_F32, 64, 16, 512));
test_cases.emplace_back(new test_mul_mat_w4a4(GGML_TYPE_MXFP4, GGML_TYPE_F32, 32, 32, 256));