0008-add-mllama-support.patch 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: jmorganca <jmorganca@gmail.com>
  3. Date: Thu, 17 Oct 2024 15:18:22 -0700
  4. Subject: [PATCH] add mllama support
  5. mllama adds cross-attention layers to the standard llama architecture
  6. it also requires a way to input a new tensor: cross_attention_state
  7. once per generation
  8. cross-attention layers don't change and so they are cached in the
  9. kv cache once per run
  10. remaining is to implement the cross attention mask
  11. ---
  12. examples/llava/llava.cpp | 5 +-
  13. ggml/src/ggml-backend-reg.cpp | 6 +-
  14. include/llama.h | 6 +
  15. src/llama-arch.cpp | 44 +++++
  16. src/llama-arch.h | 10 ++
  17. src/llama-batch.cpp | 3 +
  18. src/llama-context.cpp | 19 ++-
  19. src/llama-context.h | 2 +
  20. src/llama-cparams.h | 1 +
  21. src/llama-hparams.cpp | 8 +-
  22. src/llama-hparams.h | 4 +
  23. src/llama-kv-cache.cpp | 33 ++++
  24. src/llama-model-loader.cpp | 2 +
  25. src/llama-model.cpp | 59 ++-----
  26. src/llama-model.h | 51 ++++++
  27. src/llama-quant.cpp | 4 +-
  28. src/llama.cpp | 307 +++++++++++++++++++++++++++++++++-
  29. 17 files changed, 508 insertions(+), 56 deletions(-)
  30. diff --git a/examples/llava/llava.cpp b/examples/llava/llava.cpp
  31. index 16f30c56..0f0f3f62 100644
  32. --- a/examples/llava/llava.cpp
  33. +++ b/examples/llava/llava.cpp
  34. @@ -429,7 +429,7 @@ struct llava_embd_batch {
  35. std::vector<llama_seq_id *> seq_ids;
  36. std::vector<int8_t> logits;
  37. llama_batch batch;
  38. - llava_embd_batch(float * embd, int32_t n_tokens, llama_pos pos_0, llama_seq_id seq_id) {
  39. + llava_embd_batch(float * embd, int32_t n_embd, int32_t n_tokens, llama_pos pos_0, llama_seq_id seq_id) {
  40. pos .resize(n_tokens);
  41. n_seq_id.resize(n_tokens);
  42. seq_ids .resize(n_tokens + 1);
  43. @@ -441,6 +441,7 @@ struct llava_embd_batch {
  44. /*n_tokens =*/ n_tokens,
  45. /*tokens =*/ nullptr,
  46. /*embd =*/ embd,
  47. + /*n_embd =*/ n_embd,
  48. /*pos =*/ pos.data(),
  49. /*n_seq_id =*/ n_seq_id.data(),
  50. /*seq_id =*/ seq_ids.data(),
  51. @@ -464,7 +465,7 @@ bool llava_eval_image_embed(llama_context * ctx_llama, const struct llava_image_
  52. n_eval = n_batch;
  53. }
  54. float * embd = image_embed->embed+i*n_embd;
  55. - llava_embd_batch llava_batch = llava_embd_batch(embd, n_eval, *n_past, 0);
  56. + llava_embd_batch llava_batch = llava_embd_batch(embd, n_embd, n_eval, *n_past, 0);
  57. if (llama_decode(ctx_llama, llava_batch.batch)) {
  58. LOG_ERR("%s : failed to eval\n", __func__);
  59. return false;
  60. diff --git a/ggml/src/ggml-backend-reg.cpp b/ggml/src/ggml-backend-reg.cpp
  61. index 7ddd178b..899d16f2 100644
  62. --- a/ggml/src/ggml-backend-reg.cpp
  63. +++ b/ggml/src/ggml-backend-reg.cpp
  64. @@ -171,9 +171,9 @@ struct ggml_backend_registry {
  65. #ifdef GGML_USE_CANN
  66. register_backend(ggml_backend_cann_reg());
  67. #endif
  68. -#ifdef GGML_USE_BLAS
  69. - register_backend(ggml_backend_blas_reg());
  70. -#endif
  71. +// #ifdef GGML_USE_BLAS
  72. +// register_backend(ggml_backend_blas_reg());
  73. +// #endif
  74. #ifdef GGML_USE_RPC
  75. register_backend(ggml_backend_rpc_reg());
  76. #endif
  77. diff --git a/include/llama.h b/include/llama.h
  78. index a0d5ba5d..9f411960 100644
  79. --- a/include/llama.h
  80. +++ b/include/llama.h
  81. @@ -250,6 +250,7 @@ extern "C" {
  82. llama_token * token;
  83. float * embd;
  84. + int32_t n_embd;
  85. llama_pos * pos;
  86. int32_t * n_seq_id;
  87. llama_seq_id ** seq_id;
  88. @@ -347,6 +348,7 @@ extern "C" {
  89. bool offload_kqv; // whether to offload the KQV ops (including the KV cache) to GPU
  90. bool flash_attn; // whether to use flash attention [EXPERIMENTAL]
  91. bool no_perf; // whether to measure performance timings
  92. + bool cross_attn; // whether to use cross attention
  93. // Abort callback
  94. // if it returns true, execution of llama_decode() will be aborted
  95. @@ -426,6 +428,10 @@ extern "C" {
  96. struct llama_model * model,
  97. struct llama_context_params params);
  98. + // TODO (jmorganca): this should most likely be passed in as part of a batch
  99. + // and not set on the context for all batches.
  100. + LLAMA_API void llama_set_cross_attention(struct llama_context * ctx, bool cross_attn_state);
  101. +
  102. // Frees all allocated memory
  103. LLAMA_API void llama_free(struct llama_context * ctx);
  104. diff --git a/src/llama-arch.cpp b/src/llama-arch.cpp
  105. index 5b376c5e..b35aeb31 100644
  106. --- a/src/llama-arch.cpp
  107. +++ b/src/llama-arch.cpp
  108. @@ -6,6 +6,7 @@
  109. static const std::map<llm_arch, const char *> LLM_ARCH_NAMES = {
  110. { LLM_ARCH_LLAMA, "llama" },
  111. + { LLM_ARCH_MLLAMA, "mllama" },
  112. { LLM_ARCH_DECI, "deci" },
  113. { LLM_ARCH_FALCON, "falcon" },
  114. { LLM_ARCH_GROK, "grok" },
  115. @@ -124,6 +125,7 @@ static const std::map<llm_kv, const char *> LLM_KV_NAMES = {
  116. { LLM_KV_ATTENTION_SLIDING_WINDOW, "%s.attention.sliding_window" },
  117. { LLM_KV_ATTENTION_SCALE, "%s.attention.scale" },
  118. { LLM_KV_ATTENTION_BLOCK_SKIP_CONNECTION, "%s.attention.block_skip_connection" },
  119. + { LLM_KV_ATTENTION_CROSS_ATTENTION_LAYERS, "%s.attention.cross_attention_layers" },
  120. { LLM_KV_ROPE_DIMENSION_COUNT, "%s.rope.dimension_count" },
  121. { LLM_KV_ROPE_DIMENSION_SECTIONS, "%s.rope.dimension_sections" },
  122. @@ -220,6 +222,40 @@ static const std::map<llm_arch, std::map<llm_tensor, const char *>> LLM_TENSOR_N
  123. { LLM_TENSOR_FFN_UP_EXPS, "blk.%d.ffn_up_exps" },
  124. },
  125. },
  126. + {
  127. + LLM_ARCH_MLLAMA,
  128. + {
  129. + { LLM_TENSOR_TOKEN_EMBD, "token_embd" },
  130. + { LLM_TENSOR_OUTPUT_NORM, "output_norm" },
  131. + { LLM_TENSOR_OUTPUT, "output" },
  132. + { LLM_TENSOR_ROPE_FREQS, "rope_freqs" },
  133. + { LLM_TENSOR_ATTN_NORM, "blk.%d.attn_norm" },
  134. + { LLM_TENSOR_ATTN_Q, "blk.%d.attn_q" },
  135. + { LLM_TENSOR_ATTN_K, "blk.%d.attn_k" },
  136. + { LLM_TENSOR_ATTN_V, "blk.%d.attn_v" },
  137. + { LLM_TENSOR_ATTN_OUT, "blk.%d.attn_output" },
  138. + { LLM_TENSOR_ATTN_ROT_EMBD, "blk.%d.attn_rot_embd" },
  139. + { LLM_TENSOR_FFN_GATE_INP, "blk.%d.ffn_gate_inp" },
  140. + { LLM_TENSOR_FFN_NORM, "blk.%d.ffn_norm" },
  141. + { LLM_TENSOR_FFN_GATE, "blk.%d.ffn_gate" },
  142. + { LLM_TENSOR_FFN_DOWN, "blk.%d.ffn_down" },
  143. + { LLM_TENSOR_FFN_UP, "blk.%d.ffn_up" },
  144. + { LLM_TENSOR_FFN_GATE_EXP, "blk.%d.ffn_gate.%d" },
  145. + { LLM_TENSOR_FFN_DOWN_EXP, "blk.%d.ffn_down.%d" },
  146. + { LLM_TENSOR_FFN_UP_EXP, "blk.%d.ffn_up.%d" },
  147. + { LLM_TENSOR_FFN_GATE_EXPS, "blk.%d.ffn_gate_exps" },
  148. + { LLM_TENSOR_FFN_DOWN_EXPS, "blk.%d.ffn_down_exps" },
  149. + { LLM_TENSOR_FFN_UP_EXPS, "blk.%d.ffn_up_exps" },
  150. + { LLM_TENSOR_CROSS_ATTN_K_NORM, "blk.%d.cross_attn_k_norm" },
  151. + { LLM_TENSOR_CROSS_ATTN_K_PROJ, "blk.%d.cross_attn_k_proj" },
  152. + { LLM_TENSOR_CROSS_ATTN_O_PROJ, "blk.%d.cross_attn_o_proj" },
  153. + { LLM_TENSOR_CROSS_ATTN_Q_NORM, "blk.%d.cross_attn_q_norm" },
  154. + { LLM_TENSOR_CROSS_ATTN_Q_PROJ, "blk.%d.cross_attn_q_proj" },
  155. + { LLM_TENSOR_CROSS_ATTN_V_PROJ, "blk.%d.cross_attn_v_proj" },
  156. + { LLM_TENSOR_CROSS_ATTN_ATTN_GATE, "blk.%d.cross_attn_attn_gate" },
  157. + { LLM_TENSOR_CROSS_ATTN_MLP_GATE, "blk.%d.cross_attn_mlp_gate" },
  158. + },
  159. + },
  160. {
  161. LLM_ARCH_DECI,
  162. {
  163. @@ -1393,6 +1429,14 @@ static const std::map<llm_tensor, llm_tensor_info> LLM_TENSOR_INFOS = {
  164. // this tensor is loaded for T5, but never used
  165. {LLM_TENSOR_DEC_CROSS_ATTN_REL_B, {LLM_TENSOR_LAYER_REPEATING, GGML_OP_NONE}},
  166. {LLM_TENSOR_BSKCN_TV, {LLM_TENSOR_LAYER_REPEATING, GGML_OP_MUL}},
  167. + {LLM_TENSOR_CROSS_ATTN_K_NORM, {LLM_TENSOR_LAYER_REPEATING, GGML_OP_MUL}},
  168. + {LLM_TENSOR_CROSS_ATTN_K_PROJ, {LLM_TENSOR_LAYER_REPEATING, GGML_OP_MUL_MAT}},
  169. + {LLM_TENSOR_CROSS_ATTN_O_PROJ, {LLM_TENSOR_LAYER_REPEATING, GGML_OP_MUL_MAT}},
  170. + {LLM_TENSOR_CROSS_ATTN_Q_NORM, {LLM_TENSOR_LAYER_REPEATING, GGML_OP_MUL}},
  171. + {LLM_TENSOR_CROSS_ATTN_Q_PROJ, {LLM_TENSOR_LAYER_REPEATING, GGML_OP_MUL_MAT}},
  172. + {LLM_TENSOR_CROSS_ATTN_V_PROJ, {LLM_TENSOR_LAYER_REPEATING, GGML_OP_MUL_MAT}},
  173. + {LLM_TENSOR_CROSS_ATTN_ATTN_GATE, {LLM_TENSOR_LAYER_REPEATING, GGML_OP_MUL}},
  174. + {LLM_TENSOR_CROSS_ATTN_MLP_GATE, {LLM_TENSOR_LAYER_REPEATING, GGML_OP_MUL}},
  175. {LLM_TENSOR_CONV1D, {LLM_TENSOR_LAYER_INPUT, GGML_OP_IM2COL}},
  176. {LLM_TENSOR_POS_NET_NORM, {LLM_TENSOR_LAYER_REPEATING, GGML_OP_MUL}},
  177. {LLM_TENSOR_POS_NET_NORM1, {LLM_TENSOR_LAYER_REPEATING, GGML_OP_MUL}},
  178. diff --git a/src/llama-arch.h b/src/llama-arch.h
  179. index eac7055b..e8235ae0 100644
  180. --- a/src/llama-arch.h
  181. +++ b/src/llama-arch.h
  182. @@ -10,6 +10,7 @@
  183. enum llm_arch {
  184. LLM_ARCH_LLAMA,
  185. + LLM_ARCH_MLLAMA,
  186. LLM_ARCH_DECI,
  187. LLM_ARCH_FALCON,
  188. LLM_ARCH_BAICHUAN,
  189. @@ -128,6 +129,7 @@ enum llm_kv {
  190. LLM_KV_ATTENTION_SLIDING_WINDOW,
  191. LLM_KV_ATTENTION_SCALE,
  192. LLM_KV_ATTENTION_BLOCK_SKIP_CONNECTION,
  193. + LLM_KV_ATTENTION_CROSS_ATTENTION_LAYERS,
  194. LLM_KV_ROPE_DIMENSION_COUNT,
  195. LLM_KV_ROPE_DIMENSION_SECTIONS,
  196. @@ -308,6 +310,14 @@ enum llm_tensor {
  197. LLM_TENSOR_CLS,
  198. LLM_TENSOR_CLS_OUT,
  199. LLM_TENSOR_BSKCN_TV,
  200. + LLM_TENSOR_CROSS_ATTN_K_NORM,
  201. + LLM_TENSOR_CROSS_ATTN_K_PROJ,
  202. + LLM_TENSOR_CROSS_ATTN_O_PROJ,
  203. + LLM_TENSOR_CROSS_ATTN_Q_NORM,
  204. + LLM_TENSOR_CROSS_ATTN_Q_PROJ,
  205. + LLM_TENSOR_CROSS_ATTN_V_PROJ,
  206. + LLM_TENSOR_CROSS_ATTN_ATTN_GATE,
  207. + LLM_TENSOR_CROSS_ATTN_MLP_GATE,
  208. LLM_TENSOR_CONV1D,
  209. LLM_TENSOR_CONVNEXT_DW,
  210. LLM_TENSOR_CONVNEXT_NORM,
  211. diff --git a/src/llama-batch.cpp b/src/llama-batch.cpp
  212. index 01d5ca57..8682b0e6 100644
  213. --- a/src/llama-batch.cpp
  214. +++ b/src/llama-batch.cpp
  215. @@ -316,6 +316,7 @@ struct llama_batch llama_batch_get_one(
  216. /*n_tokens =*/ n_tokens,
  217. /*tokens =*/ tokens,
  218. /*embd =*/ nullptr,
  219. + /*n_embd =*/ 0,
  220. /*pos =*/ nullptr,
  221. /*n_seq_id =*/ nullptr,
  222. /*seq_id =*/ nullptr,
  223. @@ -328,6 +329,7 @@ struct llama_batch llama_batch_init(int32_t n_tokens_alloc, int32_t embd, int32_
  224. /*n_tokens =*/ 0,
  225. /*tokens =*/ nullptr,
  226. /*embd =*/ nullptr,
  227. + /*n_embd =*/ 0,
  228. /*pos =*/ nullptr,
  229. /*n_seq_id =*/ nullptr,
  230. /*seq_id =*/ nullptr,
  231. @@ -336,6 +338,7 @@ struct llama_batch llama_batch_init(int32_t n_tokens_alloc, int32_t embd, int32_
  232. if (embd) {
  233. batch.embd = (float *) malloc(sizeof(float) * n_tokens_alloc * embd);
  234. + batch.n_embd = embd;
  235. } else {
  236. batch.token = (llama_token *) malloc(sizeof(llama_token) * n_tokens_alloc);
  237. }
  238. diff --git a/src/llama-context.cpp b/src/llama-context.cpp
  239. index b9c4a5bf..9d0e7ca3 100644
  240. --- a/src/llama-context.cpp
  241. +++ b/src/llama-context.cpp
  242. @@ -71,10 +71,19 @@ void llama_set_inputs(llama_context & lctx, const llama_ubatch & ubatch) {
  243. }
  244. if (ubatch.embd) {
  245. - const int64_t n_embd = hparams.n_embd;
  246. - const int64_t n_tokens = ubatch.n_tokens;
  247. + if (lctx.inp_cross_attn_state && lctx.inp_cross_attn_state->buffer) {
  248. + ggml_backend_tensor_set(lctx.inp_cross_attn_state, ubatch.embd, 0, ggml_nbytes(lctx.inp_cross_attn_state));
  249. + // zero out inp_embd since it's not used
  250. + float * inp_embd_data = (float *)lctx.inp_embd->data;
  251. + for (int i = 0; i < ggml_nelements(lctx.inp_embd); ++i) {
  252. + inp_embd_data[i] = 0.0f;
  253. + }
  254. + } else {
  255. + const int64_t n_embd = hparams.n_embd;
  256. + const int64_t n_tokens = ubatch.n_tokens;
  257. - ggml_backend_tensor_set(lctx.inp_embd, ubatch.embd, 0, n_tokens*n_embd*ggml_element_size(lctx.inp_embd));
  258. + ggml_backend_tensor_set(lctx.inp_embd, ubatch.embd, 0, n_tokens*n_embd*ggml_element_size(lctx.inp_embd));
  259. + }
  260. }
  261. if (ubatch.pos && lctx.inp_pos) {
  262. @@ -653,6 +662,10 @@ void llama_set_causal_attn(struct llama_context * ctx, bool causal_attn) {
  263. ctx->cparams.causal_attn = causal_attn;
  264. }
  265. +void llama_set_cross_attention(struct llama_context * ctx, bool cross_attention) {
  266. + ctx->cparams.cross_attn = cross_attention;
  267. +}
  268. +
  269. void llama_synchronize(struct llama_context * ctx) {
  270. ggml_backend_sched_synchronize(ctx->sched.get());
  271. diff --git a/src/llama-context.h b/src/llama-context.h
  272. index 0d163c47..4980a60e 100644
  273. --- a/src/llama-context.h
  274. +++ b/src/llama-context.h
  275. @@ -107,6 +107,8 @@ struct llama_context {
  276. struct ggml_tensor * inp_pos_bucket; // I32 [n_batch|n_kv, n_batch]
  277. struct ggml_tensor * inp_embd_enc; // F32 [n_embd, n_outputs_enc]
  278. struct ggml_tensor * inp_KQ_mask_cross; // F32 [n_outputs_enc, n_batch]
  279. +
  280. + struct ggml_tensor * inp_cross_attn_state; // F32 [4, n_embd, 1061]
  281. };
  282. // TODO: make these methods of llama_context
  283. diff --git a/src/llama-cparams.h b/src/llama-cparams.h
  284. index 252012f3..9681e5a0 100644
  285. --- a/src/llama-cparams.h
  286. +++ b/src/llama-cparams.h
  287. @@ -29,6 +29,7 @@ struct llama_cparams {
  288. bool offload_kqv;
  289. bool flash_attn;
  290. bool no_perf;
  291. + bool cross_attn;
  292. enum llama_pooling_type pooling_type;
  293. diff --git a/src/llama-hparams.cpp b/src/llama-hparams.cpp
  294. index 450738da..42f8a58f 100644
  295. --- a/src/llama-hparams.cpp
  296. +++ b/src/llama-hparams.cpp
  297. @@ -2,6 +2,8 @@
  298. #include "ggml.h"
  299. +#include <algorithm>
  300. +
  301. uint32_t llama_hparams::n_head(uint32_t il) const {
  302. if (il < n_layer) {
  303. return n_head_arr[il];
  304. @@ -76,4 +78,8 @@ bool llama_hparams::n_bskcn(uint32_t n, uint32_t il) const {
  305. }
  306. GGML_ABORT("fatal error");
  307. -}
  308. \ No newline at end of file
  309. +}
  310. +
  311. +bool llama_hparams::cross_attention_layers(uint32_t il) const {
  312. + return std::find(cross_attn_layers.begin(), cross_attn_layers.end(), il) != cross_attn_layers.end();
  313. +}
  314. diff --git a/src/llama-hparams.h b/src/llama-hparams.h
  315. index fd898e27..f826cd9a 100644
  316. --- a/src/llama-hparams.h
  317. +++ b/src/llama-hparams.h
  318. @@ -53,6 +53,7 @@ struct llama_hparams {
  319. std::array<uint32_t, LLAMA_MAX_LAYERS> n_ff_arr;
  320. std::array<std::array<uint32_t, LLAMA_MAX_LAYERS>, 4> n_bskcn_arr = {};
  321. + std::array<uint32_t, LLAMA_MAX_LAYERS> cross_attn_layers;
  322. uint32_t n_layer_dense_lead = 0;
  323. uint32_t n_lora_q = 0;
  324. @@ -139,6 +140,9 @@ struct llama_hparams {
  325. // Block skip connection
  326. bool n_bskcn(uint32_t n, uint32_t il) const;
  327. +
  328. + // cross attention layers
  329. + bool cross_attention_layers(uint32_t il) const;
  330. };
  331. static_assert(std::is_trivially_copyable<llama_hparams>::value, "llama_hparams must be trivially copyable");
  332. diff --git a/src/llama-kv-cache.cpp b/src/llama-kv-cache.cpp
  333. index 53379253..cf814dbe 100644
  334. --- a/src/llama-kv-cache.cpp
  335. +++ b/src/llama-kv-cache.cpp
  336. @@ -72,6 +72,39 @@ bool llama_kv_cache_init(
  337. cache.v_l.reserve(n_layer);
  338. for (int i = 0; i < n_layer; i++) {
  339. + // for cross attention layers
  340. + if (model.arch == LLM_ARCH_MLLAMA && hparams.cross_attention_layers(i)) {
  341. + const uint32_t n_embd_k_gqa = hparams.n_embd_k_gqa(i) + hparams.n_embd_k_s();
  342. + const llama_model::buft_list_t * buft_list;
  343. + if (offload) {
  344. + buft_list = model.dev_layer.at(i).buft_list;
  345. + } else {
  346. + buft_list = &model.cpu_buft_list;
  347. + }
  348. + ggml_backend_buffer_type_t buft = select_buft(*buft_list,
  349. + [&](ggml_context * ctx) {
  350. + ggml_tensor * k = ggml_new_tensor_1d(ctx, type_k, n_embd_k_gqa*kv_size);
  351. + if (hparams.rope_type == LLAMA_ROPE_TYPE_NONE) {
  352. + return k;
  353. + }
  354. + ggml_tensor * p = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, 1);
  355. + return ggml_rope(ctx, k, p, hparams.n_rot, hparams.rope_type);
  356. + });
  357. + ggml_context * ctx = ctx_for_buft(buft);
  358. +
  359. + if (!ctx) {
  360. + LLAMA_LOG_ERROR("%s: failed to create ggml context for kv cache\n", __func__);
  361. + return false;
  362. + }
  363. + ggml_tensor * k = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, hparams.n_embd_head_k, 6404, hparams.n_head_kv(i));
  364. + ggml_tensor * v = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, hparams.n_embd_head_v, 6404, hparams.n_head_kv(i));
  365. + ggml_format_name(k, "cache_k_l%d", i);
  366. + ggml_format_name(v, "cache_v_l%d", i);
  367. + cache.k_l.push_back(k);
  368. + cache.v_l.push_back(v);
  369. + continue;
  370. + }
  371. +
  372. const uint32_t n_embd_k_gqa = hparams.n_embd_k_gqa(i) + hparams.n_embd_k_s();
  373. const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(i) + hparams.n_embd_v_s();
  374. diff --git a/src/llama-model-loader.cpp b/src/llama-model-loader.cpp
  375. index 422524a8..b12d6566 100644
  376. --- a/src/llama-model-loader.cpp
  377. +++ b/src/llama-model-loader.cpp
  378. @@ -240,6 +240,8 @@ namespace GGUFMeta {
  379. return true;
  380. }
  381. + template bool llama_model_loader::get_arr<std::array<unsigned int, 512>>(enum llm_kv kid, std::array<unsigned int, 512>& result, bool required);
  382. +
  383. template<typename T, size_t N_MAX>
  384. bool llama_model_loader::get_arr(const std::string & key, std::array<T, N_MAX> & result, bool required) {
  385. const int kid = gguf_find_key(meta.get(), key.c_str());
  386. diff --git a/src/llama-model.cpp b/src/llama-model.cpp
  387. index 306c557d..4f9bbf90 100644
  388. --- a/src/llama-model.cpp
  389. +++ b/src/llama-model.cpp
  390. @@ -146,46 +146,6 @@ std::string llama_model_ftype_name(const llama_model & model) {
  391. return llama_model_ftype_name(model.ftype);
  392. }
  393. -template<typename F>
  394. -static bool buft_supported(ggml_backend_buffer_type_t buft, ggml_backend_dev_t dev, F & fn) {
  395. - ggml_init_params params = {
  396. - /*.mem_size =*/ ggml_tensor_overhead()*8,
  397. - /*.mem_buffer =*/ NULL,
  398. - /*.no_alloc =*/ true,
  399. - };
  400. -
  401. - ggml_context_ptr ctx { ggml_init(params) };
  402. - if (!ctx) {
  403. - throw std::runtime_error(format("failed to create ggml context"));
  404. - }
  405. -
  406. - ggml_backend_buffer_ptr buf { ggml_backend_buft_alloc_buffer(buft, 0) };
  407. - ggml_tensor * op_tensor = fn(ctx.get());
  408. - for (int i = 0; i < GGML_MAX_SRC; i++) {
  409. - if (op_tensor->src[i] != nullptr) {
  410. - assert(op_tensor->src[i]->buffer == nullptr);
  411. - op_tensor->src[i]->buffer = buf.get();
  412. - }
  413. - }
  414. -
  415. - bool op_supported = ggml_backend_dev_supports_op(dev, op_tensor);
  416. -
  417. - return op_supported;
  418. -}
  419. -
  420. -template<typename F>
  421. -static ggml_backend_buffer_type_t select_buft(const llama_model::buft_list_t & buft_list, const F & fn) {
  422. - for (const auto & cur : buft_list) {
  423. - ggml_backend_dev_t cur_dev = cur.first;
  424. - ggml_backend_buffer_type_t cur_buft = cur.second;
  425. - if (buft_supported(cur_buft, cur_dev, fn)) {
  426. - return cur_buft;
  427. - }
  428. - }
  429. -
  430. - throw std::runtime_error(format("no suitable buffer type found"));
  431. -}
  432. -
  433. ggml_backend_buffer_type_t llama_model_select_buft(const llama_model & model, int il) {
  434. return select_buft(
  435. *model.dev_layer.at(il).buft_list,
  436. @@ -312,9 +272,11 @@ void llm_load_hparams(llama_model_loader & ml, llama_model & model) {
  437. std::fill(hparams.n_head_arr.begin(), hparams.n_head_arr.end(), 0);
  438. std::fill(hparams.n_head_kv_arr.begin(), hparams.n_head_kv_arr.end(), 0);
  439. std::fill(hparams.n_ff_arr.begin(), hparams.n_ff_arr.end(), 0);
  440. + std::fill(hparams.cross_attn_layers.begin(), hparams.cross_attn_layers.end(), -1);
  441. - ml.get_key_or_arr(LLM_KV_FEED_FORWARD_LENGTH, hparams.n_ff_arr, hparams.n_layer, false);
  442. - ml.get_key_or_arr(LLM_KV_ATTENTION_HEAD_COUNT, hparams.n_head_arr, hparams.n_layer, false);
  443. + ml.get_key_or_arr(LLM_KV_FEED_FORWARD_LENGTH, hparams.n_ff_arr, hparams.n_layer, false);
  444. + ml.get_key_or_arr(LLM_KV_ATTENTION_HEAD_COUNT, hparams.n_head_arr, hparams.n_layer, false);
  445. + ml.get_arr(LLM_KV_ATTENTION_CROSS_ATTENTION_LAYERS, hparams.cross_attn_layers, false);
  446. // n_head_kv is optional, default to n_head
  447. hparams.n_head_kv_arr = hparams.n_head_arr;
  448. @@ -363,7 +325,7 @@ void llm_load_hparams(llama_model_loader & ml, llama_model & model) {
  449. ml.get_key(LLM_KV_ROPE_DIMENSION_COUNT, hparams.n_rot, false);
  450. - if (model.arch == LLM_ARCH_LLAMA || model.arch == LLM_ARCH_DECI || model.arch == LLM_ARCH_FALCON) {
  451. + if (model.arch == LLM_ARCH_LLAMA || model.arch == LLM_ARCH_MLLAMA || model.arch == LLM_ARCH_DECI || model.arch == LLM_ARCH_FALCON) {
  452. if (hparams.n_rot != hparams.n_embd_head_k) {
  453. throw std::runtime_error(format("invalid n_rot: %u, expected %u", hparams.n_rot, hparams.n_embd_head_k));
  454. }
  455. @@ -405,6 +367,16 @@ void llm_load_hparams(llama_model_loader & ml, llama_model & model) {
  456. }
  457. }
  458. } break;
  459. + case LLM_ARCH_MLLAMA:
  460. + {
  461. + ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  462. +
  463. + switch (hparams.n_layer) {
  464. + case 40: model.type = e_model::MODEL_11B; break;
  465. + case 100: model.type = e_model::MODEL_90B; break;
  466. + default: model.type = e_model::MODEL_UNKNOWN;
  467. + }
  468. + } break;
  469. case LLM_ARCH_DECI:
  470. {
  471. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  472. @@ -2062,6 +2034,7 @@ enum llama_rope_type llama_rope_type(const struct llama_model * model) {
  473. // use what we call a normal RoPE, operating on pairs of consecutive head values
  474. case LLM_ARCH_LLAMA:
  475. + case LLM_ARCH_MLLAMA:
  476. case LLM_ARCH_DECI:
  477. case LLM_ARCH_BAICHUAN:
  478. case LLM_ARCH_STARCODER:
  479. diff --git a/src/llama-model.h b/src/llama-model.h
  480. index c1b9c0a1..5b23e2ba 100644
  481. --- a/src/llama-model.h
  482. +++ b/src/llama-model.h
  483. @@ -9,6 +9,7 @@
  484. #include "ggml-cpp.h"
  485. #include <vector>
  486. +#include <stdexcept>
  487. // available models
  488. // TODO: this enum does not follow the enum naming convention
  489. @@ -62,6 +63,7 @@ enum llm_type {
  490. MODEL_40B,
  491. MODEL_65B,
  492. MODEL_70B,
  493. + MODEL_90B,
  494. MODEL_236B,
  495. MODEL_314B,
  496. MODEL_671B,
  497. @@ -278,6 +280,16 @@ struct llama_layer {
  498. struct ggml_tensor * bskcn_tv = nullptr;
  499. + // cross attention
  500. + struct ggml_tensor * cross_attn_k_norm = nullptr;
  501. + struct ggml_tensor * cross_attn_k_proj = nullptr;
  502. + struct ggml_tensor * cross_attn_o_proj = nullptr;
  503. + struct ggml_tensor * cross_attn_q_norm = nullptr;
  504. + struct ggml_tensor * cross_attn_q_proj = nullptr;
  505. + struct ggml_tensor * cross_attn_v_proj = nullptr;
  506. + struct ggml_tensor * cross_attn_attn_gate = nullptr;
  507. + struct ggml_tensor * cross_attn_mlp_gate = nullptr;
  508. +
  509. struct llama_layer_posnet posnet;
  510. struct llama_layer_convnext convnext;
  511. @@ -376,6 +388,45 @@ std::string llama_model_arch_name (const llama_model & model);
  512. std::string llama_model_type_name (const llama_model & model);
  513. std::string llama_model_ftype_name(const llama_model & model);
  514. +template<typename F>
  515. +bool buft_supported(ggml_backend_buffer_type_t buft, ggml_backend_dev_t dev, F & fn) {
  516. + ggml_init_params params = {
  517. + /*.mem_size =*/ ggml_tensor_overhead()*8,
  518. + /*.mem_buffer =*/ NULL,
  519. + /*.no_alloc =*/ true,
  520. + };
  521. +
  522. + ggml_context_ptr ctx { ggml_init(params) };
  523. + if (!ctx) {
  524. + throw std::runtime_error("failed to create ggml context");
  525. + }
  526. +
  527. + ggml_backend_buffer_ptr buf { ggml_backend_buft_alloc_buffer(buft, 0) };
  528. + ggml_tensor * op_tensor = fn(ctx.get());
  529. + for (int i = 0; i < GGML_MAX_SRC; i++) {
  530. + if (op_tensor->src[i] != nullptr) {
  531. + op_tensor->src[i]->buffer = buf.get();
  532. + }
  533. + }
  534. +
  535. + bool op_supported = ggml_backend_dev_supports_op(dev, op_tensor);
  536. +
  537. + return op_supported;
  538. +}
  539. +
  540. +template<typename F>
  541. +ggml_backend_buffer_type_t select_buft(const llama_model::buft_list_t & buft_list, const F & fn) {
  542. + for (const auto & cur : buft_list) {
  543. + ggml_backend_dev_t cur_dev = cur.first;
  544. + ggml_backend_buffer_type_t cur_buft = cur.second;
  545. + if (buft_supported(cur_buft, cur_dev, fn)) {
  546. + return cur_buft;
  547. + }
  548. + }
  549. +
  550. + throw std::runtime_error("no suitable buffer type found");
  551. +}
  552. +
  553. // used by llama_adapter_cvec
  554. ggml_backend_buffer_type_t llama_model_select_buft(const llama_model & model, int il);
  555. diff --git a/src/llama-quant.cpp b/src/llama-quant.cpp
  556. index 42974f8f..27def6fd 100644
  557. --- a/src/llama-quant.cpp
  558. +++ b/src/llama-quant.cpp
  559. @@ -629,7 +629,9 @@ static void llama_model_quantize_internal(const std::string & fname_inp, const s
  560. if (llama_model_has_encoder(&model)) {
  561. n_attn_layer *= 3;
  562. }
  563. - GGML_ASSERT((qs.n_attention_wv == n_attn_layer) && "n_attention_wv is unexpected");
  564. + if (qs.n_attention_wv != n_attn_layer) {
  565. + LLAMA_LOG_WARN("%s: n_attention_wv is unexpected, expected: %d, found: %d\n", __func__, n_attn_layer, qs.n_attention_wv);
  566. + }
  567. }
  568. size_t total_size_org = 0;
  569. diff --git a/src/llama.cpp b/src/llama.cpp
  570. index 7dec50ae..bac66c24 100644
  571. --- a/src/llama.cpp
  572. +++ b/src/llama.cpp
  573. @@ -563,6 +563,52 @@ static bool llm_load_tensors(
  574. }
  575. }
  576. } break;
  577. + case LLM_ARCH_MLLAMA:
  578. + {
  579. + model.tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab+8}, 0);
  580. +
  581. + // output
  582. + {
  583. + model.output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  584. + model.output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, llama_model_loader::TENSOR_NOT_REQUIRED);
  585. +
  586. + // if output is NULL, init from the input tok embed
  587. + if (model.output == NULL) {
  588. + model.output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, llama_model_loader::TENSOR_DUPLICATED);
  589. + }
  590. + }
  591. +
  592. + for (int i = 0; i < n_layer; ++i) {
  593. + auto & layer = model.layers[i];
  594. +
  595. + if (hparams.cross_attention_layers(i)) {
  596. + layer.cross_attn_k_norm = create_tensor(tn(LLM_TENSOR_CROSS_ATTN_K_NORM, "weight", i), {128}, 0);
  597. + layer.cross_attn_k_proj = create_tensor(tn(LLM_TENSOR_CROSS_ATTN_K_PROJ, "weight", i), {n_embd, 1024}, 0);
  598. + layer.cross_attn_o_proj = create_tensor(tn(LLM_TENSOR_CROSS_ATTN_O_PROJ, "weight", i), {n_embd, n_embd}, 0);
  599. + layer.cross_attn_q_norm = create_tensor(tn(LLM_TENSOR_CROSS_ATTN_Q_NORM, "weight", i), {128}, 0);
  600. + layer.cross_attn_q_proj = create_tensor(tn(LLM_TENSOR_CROSS_ATTN_Q_PROJ, "weight", i), {n_embd, n_embd}, 0);
  601. + layer.cross_attn_v_proj = create_tensor(tn(LLM_TENSOR_CROSS_ATTN_V_PROJ, "weight", i), {n_embd, 1024}, 0);
  602. + layer.cross_attn_attn_gate = create_tensor(tn(LLM_TENSOR_CROSS_ATTN_ATTN_GATE, i), {1}, 0);
  603. + layer.cross_attn_mlp_gate = create_tensor(tn(LLM_TENSOR_CROSS_ATTN_MLP_GATE, i), {1}, 0);
  604. + layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  605. + layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  606. + layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  607. + layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  608. + layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  609. + } else {
  610. + layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  611. + layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  612. + layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  613. + layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  614. + layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  615. + layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  616. + layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, llama_model_loader::TENSOR_NOT_REQUIRED | (i != 0 ? llama_model_loader::TENSOR_DUPLICATED : 0));
  617. + layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  618. + layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  619. + layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  620. + }
  621. + }
  622. + } break;
  623. case LLM_ARCH_DECI:
  624. {
  625. model.tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  626. @@ -2514,7 +2560,7 @@ static int llama_model_load(const std::string & fname, llama_model & model, llam
  627. if (model.vocab.type != LLAMA_VOCAB_TYPE_NONE &&
  628. model.hparams.n_vocab != model.vocab.id_to_token.size()) {
  629. - throw std::runtime_error("vocab size mismatch");
  630. + LLAMA_LOG_WARN("%s: vocab mismatch %u !- %zu ...\n", __func__, model.hparams.n_vocab, model.vocab.id_to_token.size());
  631. }
  632. if (params.vocab_only) {
  633. @@ -2598,6 +2644,21 @@ static struct ggml_tensor * llm_build_inp_embd(
  634. return inpL;
  635. }
  636. +static struct ggml_tensor * llm_build_inp_cross_attn_state(
  637. + struct ggml_context * ctx,
  638. + struct llama_context & lctx,
  639. + const llama_hparams & hparams,
  640. + const llm_build_cb & cb) {
  641. + const int64_t n_embd = hparams.n_embd;
  642. +
  643. + struct ggml_tensor * inpCAS = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, n_embd, 1601, 4);
  644. + cb(inpCAS, "inp_cross_attn_state", -1);
  645. + ggml_set_input(inpCAS);
  646. + lctx.inp_cross_attn_state = inpCAS;
  647. +
  648. + return inpCAS;
  649. +}
  650. +
  651. static void llm_build_kv_store(
  652. struct ggml_context * ctx,
  653. const llama_hparams & hparams,
  654. @@ -3593,6 +3654,7 @@ struct llm_build_context {
  655. lctx.inp_pos_bucket = nullptr;
  656. lctx.inp_embd_enc = nullptr;
  657. lctx.inp_KQ_mask_cross = nullptr;
  658. + lctx.inp_cross_attn_state = nullptr;
  659. }
  660. void free() {
  661. @@ -4074,6 +4136,240 @@ struct llm_build_context {
  662. return gf;
  663. }
  664. + struct ggml_cgraph * build_mllama() {
  665. + struct ggml_cgraph * gf = ggml_new_graph_custom(ctx0, llama_model_max_nodes(model), false);
  666. +
  667. + // mutable variable, needed during the last layer of the computation to skip unused tokens
  668. + int32_t n_tokens = this->n_tokens;
  669. +
  670. + const int64_t n_embd_head = hparams.n_embd_head_v;
  671. + GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  672. + GGML_ASSERT(n_embd_head == hparams.n_rot);
  673. +
  674. + struct ggml_tensor * cur;
  675. + struct ggml_tensor * inpL;
  676. + struct ggml_tensor * inpCAS;
  677. +
  678. + inpL = llm_build_inp_embd(ctx0, lctx, hparams, ubatch, model.tok_embd, cb);
  679. + inpCAS = llm_build_inp_cross_attn_state(ctx0, lctx, hparams, cb);
  680. +
  681. + // inp_pos - contains the positions
  682. + struct ggml_tensor * inp_pos = build_inp_pos();
  683. +
  684. + // KQ_mask (mask for 1 head, it will be broadcasted to all heads)
  685. + struct ggml_tensor * KQ_mask = build_inp_KQ_mask();
  686. +
  687. + for (int il = 0; il < n_layer; ++il) {
  688. + struct ggml_tensor * inpSA = inpL;
  689. +
  690. + // norm
  691. + cur = llm_build_norm(ctx0, inpL, hparams,
  692. + model.layers[il].attn_norm, NULL,
  693. + LLM_NORM_RMS, cb, il);
  694. + cb(cur, "attn_norm", il);
  695. +
  696. + if (hparams.cross_attention_layers(il)) {
  697. + if (!ubatch.embd && !cparams.cross_attn) {
  698. + continue;
  699. + }
  700. +
  701. + // cross attention layer
  702. + struct ggml_tensor * Qcur = ggml_mul_mat(ctx0, model.layers[il].cross_attn_q_proj, cur);
  703. + cb(Qcur, "Qcur", il);
  704. +
  705. + Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  706. + cb(Qcur, "Qcur", il);
  707. +
  708. + Qcur = ggml_cont(ctx0, ggml_permute(ctx0, Qcur, 0, 2, 1, 3));
  709. + cb(Qcur, "Qcur", il);
  710. +
  711. + Qcur = llm_build_norm(ctx0, Qcur, hparams, model.layers[il].cross_attn_q_norm, NULL, LLM_NORM_RMS, cb, il);
  712. + cb(Qcur, "Qcur", il);
  713. +
  714. + struct ggml_tensor * Kcur, * Vcur;
  715. + if (ubatch.embd) {
  716. + Kcur = ggml_mul_mat(ctx0, model.layers[il].cross_attn_k_proj, inpCAS);
  717. + cb(Kcur, "Kcur", il);
  718. +
  719. + Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, 6404);
  720. + cb(Kcur, "Kcur", il);
  721. +
  722. + Kcur = ggml_cont(ctx0, ggml_permute(ctx0, Kcur, 0, 2, 1, 3));
  723. + cb(Kcur, "Kcur", il);
  724. +
  725. + Kcur = llm_build_norm(ctx0, Kcur, hparams, model.layers[il].cross_attn_k_norm, NULL, LLM_NORM_RMS, cb, il);
  726. + cb(Kcur, "Kcur", il);
  727. +
  728. + ggml_build_forward_expand(gf, ggml_cpy(ctx0, Kcur, kv_self.k_l[il]));
  729. +
  730. + Vcur = ggml_mul_mat(ctx0, model.layers[il].cross_attn_v_proj, inpCAS);
  731. + cb(Vcur, "Vcur", il);
  732. +
  733. + Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, 6404);
  734. + cb(Vcur, "Vcur", il);
  735. +
  736. + Vcur = ggml_permute(ctx0, Vcur, 0, 2, 1, 3);
  737. + cb(Vcur, "Vcur", il);
  738. +
  739. + ggml_build_forward_expand(gf, ggml_cpy(ctx0, Vcur, kv_self.v_l[il]));
  740. + } else {
  741. + Kcur = ggml_view_tensor(ctx0, kv_self.k_l[il]);
  742. + cb(Kcur, "Kcur (view)", il);
  743. +
  744. + Vcur = ggml_view_tensor(ctx0, kv_self.v_l[il]);
  745. + cb(Vcur, "Vcur (view)", il);
  746. + }
  747. +
  748. + struct ggml_tensor * kq = ggml_mul_mat(ctx0, Kcur, Qcur);
  749. + cb(kq, "kq", il);
  750. +
  751. + // TODO: apply causal masks
  752. + struct ggml_tensor * kq_soft_max = ggml_soft_max_ext(ctx0, kq, nullptr, 1.f/sqrtf(float(n_embd_head)), hparams.f_max_alibi_bias);
  753. + cb(kq_soft_max, "kq_soft_max", il);
  754. +
  755. + Vcur = ggml_cont(ctx0, ggml_transpose(ctx0, Vcur));
  756. + cb(Vcur, "Vcur", il);
  757. +
  758. + struct ggml_tensor * kqv = ggml_mul_mat(ctx0, Vcur, kq_soft_max);
  759. + cb(kqv, "kqv", il);
  760. +
  761. + struct ggml_tensor * kqv_merged = ggml_permute(ctx0, kqv, 0, 2, 1, 3);
  762. + cb(kqv_merged, "kqv_merged", il);
  763. +
  764. + cur = ggml_cont_2d(ctx0, kqv_merged, n_embd_head_v*n_head, n_tokens);
  765. + cb(cur, "kqv_merged_cont", il);
  766. +
  767. + cur = ggml_mul_mat(ctx0, model.layers[il].cross_attn_o_proj, cur);
  768. + cb(cur, "cur", il);
  769. +
  770. + // TODO: do this in place once?
  771. + cur = ggml_mul(ctx0, cur, ggml_tanh(ctx0, model.layers[il].cross_attn_attn_gate));
  772. +
  773. + struct ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  774. + cb(ffn_inp, "ffn_inp", il);
  775. +
  776. + // feed-forward network
  777. + cur = llm_build_norm(ctx0, ffn_inp, hparams,
  778. + model.layers[il].ffn_norm, NULL,
  779. + LLM_NORM_RMS, cb, il);
  780. + cb(cur, "ffn_norm", il);
  781. +
  782. + cur = llm_build_ffn(ctx0, lctx, cur,
  783. + model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  784. + model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  785. + model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  786. + NULL,
  787. + LLM_FFN_SILU, LLM_FFN_PAR, cb, il);
  788. + cb(cur, "ffn_out", il);
  789. +
  790. + // TODO: do this inplace once?
  791. + cur = ggml_add_inplace(ctx0, ggml_mul_inplace(ctx0, cur, ggml_tanh(ctx0, model.layers[il].cross_attn_mlp_gate)), ffn_inp);
  792. + cb(cur, "ffn_out", il);
  793. +
  794. + cur = lctx.cvec.apply_to(ctx0, cur, il);
  795. + cb(cur, "l_out", il);
  796. +
  797. + // input for next layer
  798. + inpL = cur;
  799. + } else {
  800. + // self attention layer
  801. +
  802. + // rope freq factors for llama3; may return nullptr for llama2 and other models
  803. + struct ggml_tensor * rope_factors = build_rope_factors(il);
  804. +
  805. + // compute Q and K and RoPE them
  806. + struct ggml_tensor * Qcur = llm_build_lora_mm(lctx, ctx0, model.layers[il].wq, cur);
  807. + cb(Qcur, "Qcur", il);
  808. + if (model.layers[il].bq) {
  809. + Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  810. + cb(Qcur, "Qcur", il);
  811. + }
  812. +
  813. + struct ggml_tensor * Kcur = llm_build_lora_mm(lctx, ctx0, model.layers[il].wk, cur);
  814. + cb(Kcur, "Kcur", il);
  815. + if (model.layers[il].bk) {
  816. + Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  817. + cb(Kcur, "Kcur", il);
  818. + }
  819. +
  820. + struct ggml_tensor * Vcur = llm_build_lora_mm(lctx, ctx0, model.layers[il].wv, cur);
  821. + cb(Vcur, "Vcur", il);
  822. + if (model.layers[il].bv) {
  823. + Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  824. + cb(Vcur, "Vcur", il);
  825. + }
  826. +
  827. + Qcur = ggml_rope_ext(
  828. + ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens), inp_pos, rope_factors,
  829. + n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  830. + ext_factor, attn_factor, beta_fast, beta_slow
  831. + );
  832. + cb(Qcur, "Qcur", il);
  833. +
  834. + Kcur = ggml_rope_ext(
  835. + ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens), inp_pos, rope_factors,
  836. + n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  837. + ext_factor, attn_factor, beta_fast, beta_slow
  838. + );
  839. + cb(Kcur, "Kcur", il);
  840. +
  841. + cur = llm_build_kv(ctx0, lctx, kv_self, gf,
  842. + model.layers[il].wo, model.layers[il].bo,
  843. + Kcur, Vcur, Qcur, KQ_mask, n_tokens, kv_head, n_kv, 1.0f/sqrtf(float(n_embd_head)), cb, il);
  844. +
  845. +
  846. + if (il == n_layer - 1) {
  847. + // skip computing output for unused tokens
  848. + struct ggml_tensor * inp_out_ids = build_inp_out_ids();
  849. + n_tokens = n_outputs;
  850. + cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  851. + inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  852. + }
  853. +
  854. + struct ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  855. + cb(ffn_inp, "ffn_inp", il);
  856. +
  857. + // feed-forward network
  858. + cur = llm_build_norm(ctx0, ffn_inp, hparams,
  859. + model.layers[il].ffn_norm, NULL,
  860. + LLM_NORM_RMS, cb, il);
  861. + cb(cur, "ffn_norm", il);
  862. +
  863. + cur = llm_build_ffn(ctx0, lctx, cur,
  864. + model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  865. + model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  866. + model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  867. + NULL,
  868. + LLM_FFN_SILU, LLM_FFN_PAR, cb, il);
  869. + cb(cur, "ffn_out", il);
  870. +
  871. + cur = ggml_add(ctx0, cur, ffn_inp);
  872. + cb(cur, "ffn_out", il);
  873. +
  874. + cur = lctx.cvec.apply_to(ctx0, cur, il);
  875. + cb(cur, "l_out", il);
  876. +
  877. + // input for next layer
  878. + inpL = cur;
  879. + }
  880. + }
  881. +
  882. + cur = inpL;
  883. +
  884. + cur = llm_build_norm(ctx0, cur, hparams,
  885. + model.output_norm, NULL,
  886. + LLM_NORM_RMS, cb, -1);
  887. + cb(cur, "result_norm", -1);
  888. +
  889. + // lm_head
  890. + cur = llm_build_lora_mm(lctx, ctx0, model.output, cur);
  891. + cb(cur, "result_output", -1);
  892. +
  893. + ggml_build_forward_expand(gf, cur);
  894. +
  895. + return gf;
  896. + }
  897. +
  898. struct ggml_cgraph * build_deci() {
  899. struct ggml_cgraph * gf = ggml_new_graph_custom(ctx0, llama_model_max_nodes(model), false);
  900. @@ -10646,6 +10942,10 @@ static struct ggml_cgraph * llama_build_graph(
  901. {
  902. result = llm.build_llama();
  903. } break;
  904. + case LLM_ARCH_MLLAMA:
  905. + {
  906. + result = llm.build_mllama();
  907. + } break;
  908. case LLM_ARCH_DECI:
  909. {
  910. result = llm.build_deci();
  911. @@ -10971,7 +11271,7 @@ static int llama_decode_internal(
  912. n_outputs = 1;
  913. }
  914. - lctx.sbatch.from_batch(batch, n_embd,
  915. + lctx.sbatch.from_batch(batch, batch.n_embd,
  916. /* simple_split */ !kv_self.recurrent,
  917. /* logits_all */ n_outputs == n_tokens_all);
  918. @@ -11282,7 +11582,7 @@ static int llama_encode_internal(
  919. const int64_t n_embd = hparams.n_embd;
  920. - lctx.sbatch.from_batch(batch, n_embd, /* simple_split */ true, /* logits_all */ true);
  921. + lctx.sbatch.from_batch(batch, batch.n_embd, /* simple_split */ true, /* logits_all */ true);
  922. const llama_ubatch ubatch = lctx.sbatch.split_simple(n_tokens);
  923. @@ -11775,6 +12075,7 @@ struct llama_context_params llama_context_default_params() {
  924. /*.offload_kqv =*/ true,
  925. /*.flash_attn =*/ false,
  926. /*.no_perf =*/ true,
  927. + /*.cross_attn =*/ false,
  928. /*.abort_callback =*/ nullptr,
  929. /*.abort_callback_data =*/ nullptr,
  930. };