Skip to content

Commit deae5ee

Browse files
model : simplify MiniMax-01 graph (ggml-org#27790)
Co-authored-by: Stanisław Szymczyk <sszymczy@gmail.com>
1 parent f295512 commit deae5ee

1 file changed

Lines changed: 14 additions & 51 deletions

File tree

src/models/minimax-01.cpp

Lines changed: 14 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,9 @@ class llm_graph_input_la : public llm_graph_input_i {
174174
bool can_reuse(const llm_graph_params & params) override {
175175
bool res = true;
176176

177-
if (params.ubatch.n_seq_tokens > 1) {
178-
res &= ( inp_q_decay && inp_q_decay->ne[2] == params.ubatch.n_seq_tokens);
179-
res &= ( inp_k_decay && inp_k_decay->ne[2] == params.ubatch.n_seq_tokens);
180-
res &= (inp_diag_decay && inp_diag_decay->ne[1] == params.ubatch.n_seq_tokens);
181-
}
177+
res &= ( inp_q_decay && inp_q_decay->ne[2] == params.ubatch.n_seq_tokens);
178+
res &= ( inp_k_decay && inp_k_decay->ne[2] == params.ubatch.n_seq_tokens);
179+
res &= (inp_diag_decay && inp_diag_decay->ne[1] == params.ubatch.n_seq_tokens);
182180

183181
return res;
184182
}
@@ -223,19 +221,17 @@ llama_model_minimax_01::graph::graph(const llama_model & model, const llm_graph_
223221
ggml_set_input(inp->inp_slopes);
224222
cb(inp->inp_slopes, "slopes", -1);
225223

226-
if (n_seq_tokens != 1) {
227-
inp->inp_q_decay = ggml_new_tensor_4d(ctx0, GGML_TYPE_F32, 1, n_head, n_seq_tokens, n_seqs);
228-
ggml_set_input(inp->inp_q_decay);
229-
cb(inp->inp_q_decay, "q_decay_exp", -1);
224+
inp->inp_q_decay = ggml_new_tensor_4d(ctx0, GGML_TYPE_F32, 1, n_head, n_seq_tokens, n_seqs);
225+
ggml_set_input(inp->inp_q_decay);
226+
cb(inp->inp_q_decay, "q_decay_exp", -1);
230227

231-
inp->inp_k_decay = ggml_new_tensor_4d(ctx0, GGML_TYPE_F32, 1, n_head, n_seq_tokens, n_seqs);
232-
ggml_set_input(inp->inp_k_decay);
233-
cb(inp->inp_k_decay, "k_decay_exp", -1);
228+
inp->inp_k_decay = ggml_new_tensor_4d(ctx0, GGML_TYPE_F32, 1, n_head, n_seq_tokens, n_seqs);
229+
ggml_set_input(inp->inp_k_decay);
230+
cb(inp->inp_k_decay, "k_decay_exp", -1);
234231

235-
inp->inp_diag_decay = ggml_new_tensor_4d(ctx0, GGML_TYPE_F32, n_seq_tokens, n_seq_tokens, n_head, n_seqs);
236-
ggml_set_input(inp->inp_diag_decay);
237-
cb(inp->inp_diag_decay, "diag_decay_exp", -1);
238-
}
232+
inp->inp_diag_decay = ggml_new_tensor_4d(ctx0, GGML_TYPE_F32, n_seq_tokens, n_seq_tokens, n_head, n_seqs);
233+
ggml_set_input(inp->inp_diag_decay);
234+
cb(inp->inp_diag_decay, "diag_decay_exp", -1);
239235

240236
la = (llm_graph_input_la *) res->add_input(std::move(inp));
241237

@@ -319,41 +315,8 @@ llama_model_minimax_01::graph::graph(const llama_model & model, const llm_graph_
319315

320316
ggml_tensor * qkv = nullptr;
321317
ggml_tensor * kv_new = nullptr;
322-
323-
if (n_seq_tokens == 1) {
324-
// lightning attention - optimized single token case for TG
325-
326-
ggml_tensor * slopes_neg = ggml_scale(ctx0, slope_rate, -1.0);
327-
cb(slopes_neg, "slopes_neg", il);
328-
329-
ggml_tensor * ratio = ggml_exp(ctx0, slopes_neg);
330-
cb(ratio, "ratio", il);
331-
332-
ggml_tensor * ratio_3d = ggml_reshape_3d(ctx0, ratio, 1, 1, n_head);
333-
cb(ratio_3d, "ratio3d", il);
334-
335-
ggml_tensor * v_trans = ggml_cont(ctx0, ggml_permute(ctx0, Vcur, 1, 2, 0, 3));
336-
cb(v_trans, "v_trans", il);
337-
338-
ggml_tensor * k_trans = ggml_cont(ctx0, ggml_permute(ctx0, Kcur, 1, 2, 0, 3));
339-
cb(k_trans, "k_trans", il);
340-
341-
ggml_tensor * kv_cur = ggml_mul_mat(ctx0, k_trans, v_trans);
342-
cb(kv_cur, "kv_cur", il);
343-
344-
ggml_tensor * kv_old_s = ggml_mul(ctx0, kv_old, ratio_3d);
345-
cb(kv_old_s, "kv_old_s", il);
346-
347-
kv_new = ggml_add(ctx0, kv_old_s, kv_cur);
348-
cb(kv_new, "kv_new", il);
349-
350-
ggml_tensor * q_trans = ggml_permute(ctx0, Qcur, 0, 2, 1, 3);
351-
cb(q_trans, "q_trans", il);
352-
353-
qkv = ggml_mul_mat(ctx0, kv_new, q_trans);
354-
cb(qkv, "qkv", il);
355-
} else if(n_seq_tokens > 1) {
356-
// lightning attention - general multi token case for PP
318+
{
319+
// lightning attention
357320

358321
ggml_tensor * q_decay_exp = la->inp_q_decay;
359322
ggml_tensor * k_decay_exp = la->inp_k_decay;

0 commit comments

Comments
 (0)