Skip to content

Commit fdc54be

Browse files
committed
Refactor mhc helpers, graph base
1 parent 7152e9b commit fdc54be

5 files changed

Lines changed: 226 additions & 229 deletions

File tree

src/llama-graph.cpp

Lines changed: 0 additions & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -3410,181 +3410,6 @@ llm_graph_input_dsv4 * llm_graph_context::build_inp_dsv4() const {
34103410
return (llm_graph_input_dsv4 *) res->add_input(std::move(inp));
34113411
}
34123412

3413-
// manifold-constrained hyper-connections (mHC), deepseek4 and glm5-next
3414-
3415-
static ggml_tensor * hc_view_1d(ggml_context * ctx, ggml_tensor * t, int64_t ne0, int64_t i0) {
3416-
return ggml_view_1d(ctx, t, ne0, ggml_row_size(t->type, i0));
3417-
}
3418-
3419-
static ggml_tensor * hc_view_2d(ggml_context * ctx, ggml_tensor * t, int64_t ne0, int64_t ne1, int64_t i0) {
3420-
return ggml_view_2d(ctx, t, ne0, ne1, t->nb[1], ggml_row_size(t->type, i0));
3421-
}
3422-
3423-
static ggml_tensor * hc_affine(ggml_context * ctx, ggml_tensor * x, ggml_tensor * scale, ggml_tensor * base) {
3424-
x = ggml_mul(ctx, x, scale);
3425-
x = ggml_add(ctx, x, base);
3426-
return x;
3427-
}
3428-
3429-
ggml_tensor * llm_graph_context::build_hc_pre(
3430-
ggml_tensor * x,
3431-
ggml_tensor * weights,
3432-
int il) const {
3433-
GGML_ASSERT(x->ne[0] == n_embd);
3434-
GGML_ASSERT(x->ne[1] == hparams.dsv4_hc_mult);
3435-
3436-
const int64_t hc = hparams.dsv4_hc_mult;
3437-
const int64_t nt = x->ne[2];
3438-
3439-
if (cparams.fused_dsv4_hc_pre && il >= 0) {
3440-
ggml_tensor * result = ggml_dsv4_hc_pre(ctx0, x, weights);
3441-
res->add_fused_node({LLM_FUSED_OP_DSV4_HC_PRE, result, il});
3442-
return result;
3443-
}
3444-
3445-
ggml_tensor * result = nullptr;
3446-
for (int64_t ih = 0; ih < hc; ++ih) {
3447-
ggml_tensor * xh = ggml_view_2d(ctx0, x, n_embd, nt, x->nb[2], ih*x->nb[1]);
3448-
ggml_tensor * wh = ggml_view_2d(ctx0, weights, 1, nt, weights->nb[1], ih*weights->nb[0]);
3449-
ggml_tensor * cur = ggml_mul(ctx0, xh, wh);
3450-
result = result ? ggml_add(ctx0, result, cur) : cur;
3451-
}
3452-
3453-
return result;
3454-
}
3455-
3456-
ggml_tensor * llm_graph_context::build_hc_sinkhorn(
3457-
ggml_tensor * comb,
3458-
int il) const {
3459-
GGML_UNUSED(il);
3460-
3461-
// comb is [dst_hc, src_hc, n_tokens]. Sinkhorn follows the reference:
3462-
// row softmax over dst, one column normalization, then repeated row/column normalization.
3463-
comb = ggml_soft_max(ctx0, comb);
3464-
3465-
ggml_tensor * eps = ggml_new_tensor_1d(ctx0, GGML_TYPE_F32, 1);
3466-
eps = ggml_fill(ctx0, eps, hparams.dsv4_hc_eps);
3467-
3468-
comb = ggml_add(ctx0, comb, eps);
3469-
3470-
auto norm_cols = [&]() {
3471-
ggml_tensor * comb_src_dst = ggml_cont(ctx0, ggml_permute(ctx0, comb, 1, 0, 2, 3));
3472-
ggml_tensor * col_sum = ggml_sum_rows(ctx0, comb_src_dst);
3473-
col_sum = ggml_add(ctx0, col_sum, eps);
3474-
col_sum = ggml_permute(ctx0, col_sum, 1, 0, 2, 3);
3475-
comb = ggml_div(ctx0, comb, col_sum);
3476-
};
3477-
3478-
auto norm_rows = [&]() {
3479-
ggml_tensor * row_sum = ggml_sum_rows(ctx0, comb);
3480-
row_sum = ggml_add(ctx0, row_sum, eps);
3481-
comb = ggml_div(ctx0, comb, row_sum);
3482-
};
3483-
3484-
norm_cols();
3485-
for (uint32_t i = 1; i < hparams.dsv4_hc_sinkhorn_iters; ++i) {
3486-
norm_rows();
3487-
norm_cols();
3488-
}
3489-
3490-
return comb;
3491-
}
3492-
3493-
ggml_tensor * llm_graph_context::build_hc_pre(
3494-
ggml_tensor * x,
3495-
ggml_tensor * hc_fn,
3496-
ggml_tensor * hc_scale,
3497-
ggml_tensor * hc_base,
3498-
ggml_tensor ** post,
3499-
ggml_tensor ** comb,
3500-
int il) const {
3501-
const int64_t hc = hparams.dsv4_hc_mult;
3502-
const int64_t hc_dim = hc*n_embd;
3503-
const int64_t hc_mix_dim = (2 + hc)*hc;
3504-
const int64_t nt = x->ne[2];
3505-
3506-
GGML_ASSERT(hc == 4);
3507-
GGML_ASSERT(hc_fn->ne[1] == hc_mix_dim);
3508-
3509-
ggml_tensor * flat = ggml_reshape_2d(ctx0, x, hc_dim, nt);
3510-
ggml_tensor * flat_norm = ggml_rms_norm(ctx0, flat, norm_rms_eps);
3511-
ggml_tensor * mixes = ggml_mul_mat(ctx0, hc_fn, flat_norm);
3512-
cb(mixes, "hc_mixes", il);
3513-
3514-
ggml_tensor * scale_pre = hc_view_1d(ctx0, hc_scale, 1, 0);
3515-
ggml_tensor * scale_post = hc_view_1d(ctx0, hc_scale, 1, 1);
3516-
3517-
ggml_tensor * base_pre = hc_view_1d(ctx0, hc_base, hc, 0);
3518-
ggml_tensor * base_post = hc_view_1d(ctx0, hc_base, hc, hc);
3519-
3520-
ggml_tensor * pre = hc_view_2d(ctx0, mixes, hc, nt, 0);
3521-
pre = hc_affine(ctx0, pre, scale_pre, base_pre);
3522-
pre = ggml_sigmoid(ctx0, pre);
3523-
pre = ggml_scale_bias(ctx0, pre, 1.0f, hparams.dsv4_hc_eps);
3524-
cb(pre, "hc_pre", il);
3525-
3526-
*post = hc_view_2d(ctx0, mixes, hc, nt, hc);
3527-
*post = hc_affine(ctx0, *post, scale_post, base_post);
3528-
*post = ggml_sigmoid(ctx0, *post);
3529-
*post = ggml_scale(ctx0, *post, 2.0f);
3530-
cb(*post, "hc_post", il);
3531-
3532-
if (cparams.fused_dsv4_hc_comb) {
3533-
*comb = ggml_dsv4_hc_comb(ctx0, mixes, hc_scale, hc_base, hparams.dsv4_hc_eps,
3534-
(int32_t) hparams.dsv4_hc_sinkhorn_iters);
3535-
res->add_fused_node({LLM_FUSED_OP_DSV4_HC_COMB, *comb, il});
3536-
} else {
3537-
ggml_tensor * scale_comb = hc_view_1d(ctx0, hc_scale, 1, 2);
3538-
ggml_tensor * base_comb = hc_view_1d(ctx0, hc_base, hc*hc, 2*hc);
3539-
3540-
*comb = hc_view_2d(ctx0, mixes, hc*hc, nt, 2*hc);
3541-
*comb = hc_affine(ctx0, *comb, scale_comb, base_comb);
3542-
*comb = ggml_reshape_3d(ctx0, *comb, hc, hc, nt);
3543-
*comb = build_hc_sinkhorn(*comb, il);
3544-
}
3545-
cb(*comb, "hc_comb", il);
3546-
3547-
ggml_tensor * result = build_hc_pre(x, pre, il);
3548-
return result;
3549-
}
3550-
3551-
ggml_tensor * llm_graph_context::build_hc_post(
3552-
ggml_tensor * x,
3553-
ggml_tensor * residual,
3554-
ggml_tensor * post,
3555-
ggml_tensor * comb,
3556-
int il) const {
3557-
GGML_ASSERT(x->ne[0] == n_embd);
3558-
GGML_ASSERT(residual->ne[1] == hparams.dsv4_hc_mult);
3559-
3560-
if (cparams.fused_dsv4_hc_post) {
3561-
ggml_tensor * result = ggml_dsv4_hc_post(ctx0, x, residual, post, comb);
3562-
res->add_fused_node({LLM_FUSED_OP_DSV4_HC_POST, result, il});
3563-
return result;
3564-
}
3565-
3566-
const int64_t hc = hparams.dsv4_hc_mult;
3567-
const int64_t nt = x->ne[1];
3568-
3569-
ggml_tensor * out = nullptr;
3570-
for (int64_t dst = 0; dst < hc; ++dst) {
3571-
ggml_tensor * post_dst = ggml_view_2d(ctx0, post, 1, nt, post->nb[1], dst*post->nb[0]);
3572-
ggml_tensor * cur = ggml_mul(ctx0, x, post_dst);
3573-
3574-
for (int64_t src = 0; src < hc; ++src) {
3575-
ggml_tensor * res_src = ggml_view_2d(ctx0, residual, n_embd, nt, residual->nb[2], src*residual->nb[1]);
3576-
ggml_tensor * comb_src_dst = ggml_view_2d(ctx0, comb, 1, nt, comb->nb[2],
3577-
dst*comb->nb[0] + src*comb->nb[1]);
3578-
cur = ggml_add(ctx0, cur, ggml_mul(ctx0, res_src, comb_src_dst));
3579-
}
3580-
3581-
cur = ggml_reshape_3d(ctx0, cur, n_embd, 1, nt);
3582-
out = out ? ggml_concat(ctx0, out, cur, 1) : cur;
3583-
}
3584-
3585-
return out;
3586-
}
3587-
35883413
ggml_tensor * llm_graph_context::build_rs(
35893414
ggml_tensor * s,
35903415
ggml_tensor * state_copy_main,

src/llama-graph.h

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,36 +1339,6 @@ struct llm_graph_context {
13391339
// hybrid
13401340
//
13411341

1342-
// hyper-connections (mHC)
1343-
1344-
1345-
// collapse the hc streams with per-stream weights
1346-
ggml_tensor * build_hc_pre(
1347-
ggml_tensor * x,
1348-
ggml_tensor * weights,
1349-
int il) const;
1350-
1351-
// returns the collapsed input and fills the post / comb weights
1352-
ggml_tensor * build_hc_pre(
1353-
ggml_tensor * x,
1354-
ggml_tensor * hc_fn,
1355-
ggml_tensor * hc_scale,
1356-
ggml_tensor * hc_base,
1357-
ggml_tensor ** post,
1358-
ggml_tensor ** comb,
1359-
int il) const;
1360-
1361-
ggml_tensor * build_hc_post(
1362-
ggml_tensor * x,
1363-
ggml_tensor * residual,
1364-
ggml_tensor * post,
1365-
ggml_tensor * comb,
1366-
int il) const;
1367-
1368-
ggml_tensor * build_hc_sinkhorn(
1369-
ggml_tensor * comb,
1370-
int il) const;
1371-
13721342
llm_graph_input_mem_hybrid * build_inp_mem_hybrid() const;
13731343
llm_graph_input_mem_hybrid_k * build_inp_mem_hybrid_k() const;
13741344

0 commit comments

Comments
 (0)