Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions ggml/src/ggml-cuda/ggml-cuda.cu
Original file line number Diff line number Diff line change
Expand Up @@ -4081,12 +4081,15 @@ static bool ggml_cuda_topk_moe_fusion(const struct ggml_cgraph * cgraph, int nod
}

if (nodes[node_idx]->op == GGML_OP_RESHAPE) {
//check RESHAPE->SUM_ROWS->CLAMP->DIV->RESHAPE
//check RESHAPE->SUM_ROWS->CLAMP->DIV->RESHAPE, or SCALE (sum + eps) in place of CLAMP
static const std::vector<ggml_op> norm_ops = { GGML_OP_RESHAPE, GGML_OP_SUM_ROWS, GGML_OP_CLAMP };

args.norm = true;
for (const ggml_op op : norm_ops) {
if (nodes[node_idx]->op == op && nodes[node_idx]->src[0] == nodes[node_idx - 1]) {
const ggml_tensor * n = nodes[node_idx];
const bool eps_add = op == GGML_OP_CLAMP && n->op == GGML_OP_SCALE && ggml_get_op_params_f32(n, 0) == 1.0f;
if ((n->op == op || eps_add) && n->src[0] == nodes[node_idx - 1]) {
args.norm_eps = eps_add;
node_idx++;
} else {
args.norm = false;
Expand Down Expand Up @@ -5174,7 +5177,7 @@ static int ggml_cuda_try_fuse(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph

if (args.norm) {
ops.insert(ops.end(),
{ GGML_OP_RESHAPE, GGML_OP_SUM_ROWS, GGML_OP_CLAMP, GGML_OP_DIV, GGML_OP_RESHAPE });
{ GGML_OP_RESHAPE, GGML_OP_SUM_ROWS, args.norm_eps ? GGML_OP_SCALE : GGML_OP_CLAMP, GGML_OP_DIV, GGML_OP_RESHAPE });
clamp = cgraph->nodes[i + ops.size() - 3];
}
if (args.scale) {
Expand Down Expand Up @@ -6725,7 +6728,7 @@ static void ggml_backend_cuda_graph_optimize(ggml_backend_t backend, ggml_cgraph

if (args.norm) {
ops.insert(ops.end(),
{ GGML_OP_RESHAPE, GGML_OP_SUM_ROWS, GGML_OP_CLAMP, GGML_OP_DIV, GGML_OP_RESHAPE });
{ GGML_OP_RESHAPE, GGML_OP_SUM_ROWS, args.norm_eps ? GGML_OP_SCALE : GGML_OP_CLAMP, GGML_OP_DIV, GGML_OP_RESHAPE });
}
if (args.scale) {
ops.insert(ops.end(), { GGML_OP_SCALE });
Expand Down
8 changes: 6 additions & 2 deletions ggml/src/ggml-cuda/topk-moe.cu
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ struct topk_moe_config {
bool use_sigmoid;
bool use_sqrt_softplus;
bool with_norm;
bool norm_add; // clamp_val is added to the sum instead of being its lower bound
bool delayed_softmax;
int n_groups;
int n_group_used;
Expand Down Expand Up @@ -334,7 +335,7 @@ __global__ void topk_moe_cuda(const float * logits,

if (config.with_norm) {
wt_sum = warp_reduce_sum<warp_size>(wt_sum);
wt_sum = max(wt_sum, clamp_val);
wt_sum = config.norm_add ? wt_sum + clamp_val : max(wt_sum, clamp_val);
const float inv_sum = 1.0f / wt_sum;

for (int i = 0; i < experts_per_thread; i++) {
Expand Down Expand Up @@ -462,15 +463,18 @@ void ggml_cuda_op_topk_moe(ggml_backend_cuda_context & ctx,

const bool with_norm = clamp != nullptr;

// CLAMP: the minimum of the sum; SCALE (with scale 1): the epsilon added to the sum
const bool norm_add = clamp && clamp->op == GGML_OP_SCALE;
float clamp_val = -INFINITY;
if (clamp) {
clamp_val = ggml_get_op_params_f32(clamp, 0);
clamp_val = ggml_get_op_params_f32(clamp, norm_add ? 1 : 0);
}

topk_moe_config config;
config.use_sigmoid = args.sigmoid;
config.use_sqrt_softplus = args.sqrt_softplus;
config.with_norm = with_norm;
config.norm_add = norm_add;
config.delayed_softmax = args.delayed_softmax;
config.n_groups = args.n_groups;
config.n_group_used = args.n_group_used;
Expand Down
1 change: 1 addition & 0 deletions ggml/src/ggml-cuda/topk-moe.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct ggml_cuda_topk_moe_args {
bool delayed_softmax{};
bool prob_bias{};
bool norm{};
bool norm_eps{}; // the norm adds an epsilon to the sum (SCALE) instead of clamping it (CLAMP)
bool scale{};
int n_groups{}; // > 0: pick experts only from the top n_group_used expert groups
int n_group_used{};
Expand Down
7 changes: 6 additions & 1 deletion src/models/inkling2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,12 @@ ggml_tensor * llama_model_inkling2::graph::build_layer(int il, int ip, ggml_tens

// project r on the bias table, the attention gathers it by distance: [extent + 1, n_head, n_tokens]
// the last bucket is a zero pad, for the cells that are farther than the extent
ggml_tensor * table = ggml_mul_mat(ctx0, ggml_pad(ctx0, layer.attn_rel_bias, 0, 1, 0, 0), Rcur);
// the looped cache slots share the weights of their physical layer, pad each once
ggml_tensor *& rel_bias = rel_bias_padded[layer.attn_rel_bias];
if (!rel_bias) {
rel_bias = ggml_pad(ctx0, layer.attn_rel_bias, 0, 1, 0, 0);
}
ggml_tensor * table = ggml_mul_mat(ctx0, rel_bias, Rcur);

// the sink logit t is per query: exp(s - t)/(1 + sum(exp(s - t))) = exp(s)/(exp(t) + sum(exp(s))), with a zero sink
table = ggml_sub(ctx0, table, ggml_reshape_3d(ctx0, sinks, 1, n_head, n_tokens));
Expand Down
1 change: 1 addition & 0 deletions src/models/models.h
Original file line number Diff line number Diff line change
Expand Up @@ -2570,6 +2570,7 @@ struct llama_model_inkling2 : public llama_model_base {
ggml_tensor * zero_sinks = nullptr;

std::map<std::vector<ggml_tensor *>, ggml_tensor *> stacks; // see stack()
std::map<const ggml_tensor *, ggml_tensor *> rel_bias_padded; // relative-position bias table + a zero bucket
};

std::unique_ptr<llm_graph_context> build_arch_graph(const llm_graph_params & params) const override;
Expand Down
6 changes: 6 additions & 0 deletions tests/test-backend-ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15148,6 +15148,12 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
}
}

// Inkling 2: sigmoid with a selection bias, 128 experts, 8 used, norm with an additive epsilon
for (float scale_w : {0.0f, 2.5f}) {
test_cases.emplace_back(new test_topk_moe({128, 1, 1, 1}, 8, true, true, GATING_FUNC_SIGMOID, scale_w, 1, true));
test_cases.emplace_back(new test_topk_moe({128, 17, 1, 1}, 8, true, true, GATING_FUNC_SIGMOID, scale_w, 1, true));
}

// sigmoid scoring always ranks a selection bias (GLM, DeepSeek-V2/V3.2)
for (float scale_w : {0.0f, 2.0f}) {
test_cases.emplace_back(new test_topk_moe({8, 22, 1, 1}, 4, true, true, GATING_FUNC_SIGMOID, scale_w, 1, true));
Expand Down
Loading