0007-add-mllama-support.patch 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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 | 28 ++--
  19. src/llama-context.h | 2 +
  20. src/llama-cparams.h | 1 +
  21. src/llama-hparams.cpp | 6 +
  22. src/llama-hparams.h | 5 +
  23. src/llama-kv-cache.cpp | 13 +-
  24. src/llama-model-loader.cpp | 2 +
  25. src/llama-model.cpp | 65 ++++++++-
  26. src/llama-model.h | 12 ++
  27. src/llama-quant.cpp | 4 +-
  28. src/llama.cpp | 262 +++++++++++++++++++++++++++++++++-
  29. 17 files changed, 452 insertions(+), 22 deletions(-)
  30. diff --git a/examples/llava/llava.cpp b/examples/llava/llava.cpp
  31. index 518aad3f..f0e484a1 100644
  32. --- a/examples/llava/llava.cpp
  33. +++ b/examples/llava/llava.cpp
  34. @@ -445,7 +445,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. @@ -457,6 +457,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. @@ -480,7 +481,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 955ed505..95036ef8 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 47919602..cc948005 100644
  79. --- a/include/llama.h
  80. +++ b/include/llama.h
  81. @@ -249,6 +249,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. @@ -343,6 +344,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. @@ -443,6 +445,10 @@ extern "C" {
  96. struct llama_context_params params),
  97. "use llama_init_from_model instead");
  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 a1e0ebcc..b6f20286 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. @@ -127,6 +128,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. @@ -225,6 +227,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. @@ -1450,6 +1486,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 77919578..ec742224 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. @@ -131,6 +132,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. @@ -314,6 +316,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 47e79ed4..7b22fe13 100644
  240. --- a/src/llama-context.cpp
  241. +++ b/src/llama-context.cpp
  242. @@ -74,10 +74,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. @@ -470,12 +479,11 @@ void llama_set_inputs(llama_context & lctx, const llama_ubatch & ubatch) {
  263. size_t llama_output_reserve(struct llama_context & lctx, size_t n_outputs) {
  264. const auto & cparams = lctx.cparams;
  265. const auto & hparams = lctx.model.hparams;
  266. - const auto & vocab = lctx.model.vocab;
  267. const size_t n_outputs_max = std::max(n_outputs, (size_t) cparams.n_seq_max);
  268. const auto n_batch = cparams.n_batch;
  269. - const auto n_vocab = vocab.n_tokens();
  270. + const auto n_vocab = hparams.n_vocab;
  271. const auto n_embd = hparams.n_embd;
  272. // TODO: use a per-batch flag for logits presence instead
  273. @@ -542,7 +550,7 @@ size_t llama_output_reserve(struct llama_context & lctx, size_t n_outputs) {
  274. void llama_output_reorder(struct llama_context & ctx) {
  275. std::vector<size_t> & out_ids = ctx.sbatch.out_ids;
  276. if (!out_ids.empty()) {
  277. - const uint32_t n_vocab = ctx.model.vocab.n_tokens();
  278. + const uint32_t n_vocab = ctx.model.hparams.n_vocab;
  279. const uint32_t n_embd = ctx.model.hparams.n_embd;
  280. const int32_t n_outputs = ctx.n_outputs;
  281. @@ -657,6 +665,10 @@ void llama_set_causal_attn(struct llama_context * ctx, bool causal_attn) {
  282. ctx->cparams.causal_attn = causal_attn;
  283. }
  284. +void llama_set_cross_attention(struct llama_context * ctx, bool cross_attention) {
  285. + ctx->cparams.cross_attn = cross_attention;
  286. +}
  287. +
  288. void llama_synchronize(struct llama_context * ctx) {
  289. ggml_backend_sched_synchronize(ctx->sched.get());
  290. @@ -726,7 +738,7 @@ float * llama_get_logits_ith(struct llama_context * ctx, int32_t i) {
  291. throw std::runtime_error(format("corrupt output buffer (j=%d, n_outputs=%d)", j, ctx->n_outputs));
  292. }
  293. - return ctx->logits + j*ctx->model.vocab.n_tokens();
  294. + return ctx->logits + j*ctx->model.hparams.n_vocab;
  295. } catch (const std::exception & err) {
  296. LLAMA_LOG_ERROR("%s: invalid logits id %d, reason: %s\n", __func__, i, err.what());
  297. #ifndef NDEBUG
  298. @@ -886,7 +898,7 @@ struct llama_data_write {
  299. }
  300. void write_logits(const struct llama_context * ctx) {
  301. - const uint64_t logits_size = std::min((uint64_t) ctx->logits_size, (uint64_t) ctx->n_outputs * ctx->model.vocab.n_tokens());
  302. + const uint64_t logits_size = std::min((uint64_t) ctx->logits_size, (uint64_t) ctx->n_outputs * ctx->model.hparams.n_vocab);
  303. write(&logits_size, sizeof(logits_size));
  304. diff --git a/src/llama-context.h b/src/llama-context.h
  305. index a9268b29..cf12c9d7 100644
  306. --- a/src/llama-context.h
  307. +++ b/src/llama-context.h
  308. @@ -107,6 +107,8 @@ struct llama_context {
  309. struct ggml_tensor * inp_pos_bucket; // I32 [n_batch|n_kv, n_batch]
  310. struct ggml_tensor * inp_embd_enc; // F32 [n_embd, n_outputs_enc]
  311. struct ggml_tensor * inp_KQ_mask_cross; // F32 [n_outputs_enc, n_batch]
  312. +
  313. + struct ggml_tensor * inp_cross_attn_state; // F32 [4, n_embd, 1061]
  314. };
  315. // TODO: make these methods of llama_context
  316. diff --git a/src/llama-cparams.h b/src/llama-cparams.h
  317. index 252012f3..9681e5a0 100644
  318. --- a/src/llama-cparams.h
  319. +++ b/src/llama-cparams.h
  320. @@ -29,6 +29,7 @@ struct llama_cparams {
  321. bool offload_kqv;
  322. bool flash_attn;
  323. bool no_perf;
  324. + bool cross_attn;
  325. enum llama_pooling_type pooling_type;
  326. diff --git a/src/llama-hparams.cpp b/src/llama-hparams.cpp
  327. index f3955de9..0b841028 100644
  328. --- a/src/llama-hparams.cpp
  329. +++ b/src/llama-hparams.cpp
  330. @@ -2,6 +2,8 @@
  331. #include "ggml.h"
  332. +#include <algorithm>
  333. +
  334. uint32_t llama_hparams::n_head(uint32_t il) const {
  335. if (il < n_layer) {
  336. return n_head_arr[il];
  337. @@ -76,4 +78,8 @@ bool llama_hparams::n_bskcn(uint32_t n, uint32_t il) const {
  338. }
  339. GGML_ABORT("fatal error");
  340. +}
  341. +
  342. +bool llama_hparams::cross_attention_layers(uint32_t il) const {
  343. + return std::find(cross_attn_layers.begin(), cross_attn_layers.end(), il) != cross_attn_layers.end();
  344. }
  345. \ No newline at end of file
  346. diff --git a/src/llama-hparams.h b/src/llama-hparams.h
  347. index 1bdcdfd5..05383046 100644
  348. --- a/src/llama-hparams.h
  349. +++ b/src/llama-hparams.h
  350. @@ -41,6 +41,7 @@ struct llama_hparams {
  351. uint32_t n_expert = 0;
  352. uint32_t n_expert_used = 0;
  353. uint32_t n_rel_attn_bkts = 0;
  354. + uint32_t n_vocab = 0;
  355. // for WavTokenizer
  356. struct llama_hparams_posnet posnet;
  357. @@ -51,6 +52,7 @@ struct llama_hparams {
  358. std::array<uint32_t, LLAMA_MAX_LAYERS> n_ff_arr;
  359. std::array<std::array<uint32_t, LLAMA_MAX_LAYERS>, 4> n_bskcn_arr = {};
  360. + std::array<uint32_t, LLAMA_MAX_LAYERS> cross_attn_layers;
  361. uint32_t n_layer_dense_lead = 0;
  362. uint32_t n_lora_q = 0;
  363. @@ -138,6 +140,9 @@ struct llama_hparams {
  364. // Block skip connection
  365. bool n_bskcn(uint32_t n, uint32_t il) const;
  366. +
  367. + // cross attention layers
  368. + bool cross_attention_layers(uint32_t il) const;
  369. };
  370. static_assert(std::is_trivially_copyable<llama_hparams>::value, "llama_hparams must be trivially copyable");
  371. diff --git a/src/llama-kv-cache.cpp b/src/llama-kv-cache.cpp
  372. index feffdf0d..b541c5a3 100644
  373. --- a/src/llama-kv-cache.cpp
  374. +++ b/src/llama-kv-cache.cpp
  375. @@ -91,8 +91,17 @@ bool llama_kv_cache_init(
  376. return false;
  377. }
  378. - ggml_tensor * k = ggml_new_tensor_1d(ctx, type_k, n_embd_k_gqa*kv_size);
  379. - ggml_tensor * v = ggml_new_tensor_1d(ctx, type_v, n_embd_v_gqa*kv_size);
  380. + ggml_tensor * k, *v;
  381. +
  382. + // for cross attention layers
  383. + if (model.arch == LLM_ARCH_MLLAMA && hparams.cross_attention_layers(i)) {
  384. + k = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, hparams.n_embd_head_k, 6404, hparams.n_head_kv(i));
  385. + v = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, hparams.n_embd_head_v, 6404, hparams.n_head_kv(i));
  386. + } else {
  387. + k = ggml_new_tensor_1d(ctx, type_k, n_embd_k_gqa*kv_size);
  388. + v = ggml_new_tensor_1d(ctx, type_v, n_embd_v_gqa*kv_size);
  389. + }
  390. +
  391. ggml_format_name(k, "cache_k_l%d", i);
  392. ggml_format_name(v, "cache_v_l%d", i);
  393. cache.k_l.push_back(k);
  394. diff --git a/src/llama-model-loader.cpp b/src/llama-model-loader.cpp
  395. index 1252aca1..45d08721 100644
  396. --- a/src/llama-model-loader.cpp
  397. +++ b/src/llama-model-loader.cpp
  398. @@ -315,6 +315,8 @@ namespace GGUFMeta {
  399. return true;
  400. }
  401. + template bool llama_model_loader::get_arr<std::array<unsigned int, 512>>(enum llm_kv kid, std::array<unsigned int, 512>& result, bool required);
  402. +
  403. template<typename T, size_t N_MAX>
  404. bool llama_model_loader::get_arr(const std::string & key, std::array<T, N_MAX> & result, bool required) {
  405. const int kid = gguf_find_key(meta.get(), key.c_str());
  406. diff --git a/src/llama-model.cpp b/src/llama-model.cpp
  407. index ad1315c6..21819080 100644
  408. --- a/src/llama-model.cpp
  409. +++ b/src/llama-model.cpp
  410. @@ -401,6 +401,7 @@ void llama_model::load_hparams(llama_model_loader & ml) {
  411. // get general kv
  412. ml.get_key(LLM_KV_GENERAL_NAME, name, false);
  413. + ml.get_key(LLM_KV_VOCAB_SIZE, hparams.n_vocab, false) || ml.get_arr_n(LLM_KV_TOKENIZER_LIST, hparams.n_vocab, false);
  414. // everything past this point is not vocab-related
  415. if (hparams.vocab_only) {
  416. @@ -412,6 +413,7 @@ void llama_model::load_hparams(llama_model_loader & ml) {
  417. ml.get_key(LLM_KV_BLOCK_COUNT, hparams.n_layer);
  418. ml.get_key(LLM_KV_EXPERT_COUNT, hparams.n_expert, false);
  419. ml.get_key(LLM_KV_EXPERT_USED_COUNT, hparams.n_expert_used, false);
  420. + ml.get_key(LLM_KV_VOCAB_SIZE, hparams.n_vocab, false);
  421. if (arch == LLM_ARCH_WAVTOKENIZER_DEC) {
  422. ml.get_key(LLM_KV_FEATURES_LENGTH, hparams.n_embd_features);
  423. @@ -435,9 +437,11 @@ void llama_model::load_hparams(llama_model_loader & ml) {
  424. std::fill(hparams.n_head_arr.begin(), hparams.n_head_arr.end(), 0);
  425. std::fill(hparams.n_head_kv_arr.begin(), hparams.n_head_kv_arr.end(), 0);
  426. std::fill(hparams.n_ff_arr.begin(), hparams.n_ff_arr.end(), 0);
  427. + std::fill(hparams.cross_attn_layers.begin(), hparams.cross_attn_layers.end(), -1);
  428. ml.get_key_or_arr(LLM_KV_FEED_FORWARD_LENGTH, hparams.n_ff_arr, hparams.n_layer, false);
  429. ml.get_key_or_arr(LLM_KV_ATTENTION_HEAD_COUNT, hparams.n_head_arr, hparams.n_layer, false);
  430. + ml.get_arr(LLM_KV_ATTENTION_CROSS_ATTENTION_LAYERS, hparams.cross_attn_layers, false);
  431. // n_head_kv is optional, default to n_head
  432. hparams.n_head_kv_arr = hparams.n_head_arr;
  433. @@ -486,7 +490,7 @@ void llama_model::load_hparams(llama_model_loader & ml) {
  434. ml.get_key(LLM_KV_ROPE_DIMENSION_COUNT, hparams.n_rot, false);
  435. - if (arch == LLM_ARCH_LLAMA || arch == LLM_ARCH_DECI || arch == LLM_ARCH_FALCON) {
  436. + if (arch == LLM_ARCH_LLAMA || arch == LLM_ARCH_MLLAMA || arch == LLM_ARCH_DECI || arch == LLM_ARCH_FALCON) {
  437. if (hparams.n_rot != hparams.n_embd_head_k) {
  438. throw std::runtime_error(format("invalid n_rot: %u, expected %u", hparams.n_rot, hparams.n_embd_head_k));
  439. }
  440. @@ -530,6 +534,16 @@ void llama_model::load_hparams(llama_model_loader & ml) {
  441. }
  442. }
  443. } break;
  444. + case LLM_ARCH_MLLAMA:
  445. + {
  446. + ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  447. +
  448. + switch (hparams.n_layer) {
  449. + case 40: type = LLM_TYPE_11B; break;
  450. + case 100: type = LLM_TYPE_90B; break;
  451. + default: type = LLM_TYPE_UNKNOWN;
  452. + }
  453. + } break;
  454. case LLM_ARCH_DECI:
  455. {
  456. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  457. @@ -1398,7 +1412,7 @@ bool llama_model::load_tensors(llama_model_loader & ml) {
  458. const int64_t n_embd_head_v = hparams.n_embd_head_v;
  459. const int64_t n_ff = hparams.n_ff();
  460. const int64_t n_embd_gqa = n_embd_v_gqa;
  461. - const int64_t n_vocab = vocab.n_tokens();
  462. + const int64_t n_vocab = hparams.n_vocab;
  463. const int64_t n_token_types = vocab.n_token_types();
  464. const int64_t n_rot = hparams.n_rot;
  465. const int64_t n_expert = hparams.n_expert;
  466. @@ -1581,6 +1595,52 @@ bool llama_model::load_tensors(llama_model_loader & ml) {
  467. }
  468. }
  469. } break;
  470. + case LLM_ARCH_MLLAMA:
  471. + {
  472. + tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab+8}, 0);
  473. +
  474. + // output
  475. + {
  476. + output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  477. + output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, llama_model_loader::TENSOR_NOT_REQUIRED);
  478. +
  479. + // if output is NULL, init from the input tok embed
  480. + if (output == NULL) {
  481. + output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, llama_model_loader::TENSOR_DUPLICATED);
  482. + }
  483. + }
  484. +
  485. + for (int i = 0; i < n_layer; ++i) {
  486. + auto & layer = layers[i];
  487. +
  488. + if (hparams.cross_attention_layers(i)) {
  489. + layer.cross_attn_k_norm = create_tensor(tn(LLM_TENSOR_CROSS_ATTN_K_NORM, "weight", i), {128}, 0);
  490. + layer.cross_attn_k_proj = create_tensor(tn(LLM_TENSOR_CROSS_ATTN_K_PROJ, "weight", i), {n_embd, 1024}, 0);
  491. + layer.cross_attn_o_proj = create_tensor(tn(LLM_TENSOR_CROSS_ATTN_O_PROJ, "weight", i), {n_embd, n_embd}, 0);
  492. + layer.cross_attn_q_norm = create_tensor(tn(LLM_TENSOR_CROSS_ATTN_Q_NORM, "weight", i), {128}, 0);
  493. + layer.cross_attn_q_proj = create_tensor(tn(LLM_TENSOR_CROSS_ATTN_Q_PROJ, "weight", i), {n_embd, n_embd}, 0);
  494. + layer.cross_attn_v_proj = create_tensor(tn(LLM_TENSOR_CROSS_ATTN_V_PROJ, "weight", i), {n_embd, 1024}, 0);
  495. + layer.cross_attn_attn_gate = create_tensor(tn(LLM_TENSOR_CROSS_ATTN_ATTN_GATE, i), {1}, 0);
  496. + layer.cross_attn_mlp_gate = create_tensor(tn(LLM_TENSOR_CROSS_ATTN_MLP_GATE, i), {1}, 0);
  497. + layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  498. + layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  499. + layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  500. + layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  501. + layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  502. + } else {
  503. + layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  504. + layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  505. + layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  506. + layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  507. + layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  508. + layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  509. + 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));
  510. + layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  511. + layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  512. + layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  513. + }
  514. + }
  515. + } break;
  516. case LLM_ARCH_DECI:
  517. {
  518. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  519. @@ -3925,6 +3985,7 @@ enum llama_rope_type llama_model_rope_type(const struct llama_model * model) {
  520. // use what we call a normal RoPE, operating on pairs of consecutive head values
  521. case LLM_ARCH_LLAMA:
  522. + case LLM_ARCH_MLLAMA:
  523. case LLM_ARCH_DECI:
  524. case LLM_ARCH_BAICHUAN:
  525. case LLM_ARCH_STARCODER:
  526. diff --git a/src/llama-model.h b/src/llama-model.h
  527. index 1afb0024..7cf57587 100644
  528. --- a/src/llama-model.h
  529. +++ b/src/llama-model.h
  530. @@ -9,6 +9,7 @@
  531. #include <string>
  532. #include <unordered_map>
  533. #include <vector>
  534. +#include <stdexcept>
  535. struct llama_model_loader;
  536. @@ -63,6 +64,7 @@ enum llm_type {
  537. LLM_TYPE_40B,
  538. LLM_TYPE_65B,
  539. LLM_TYPE_70B,
  540. + LLM_TYPE_90B,
  541. LLM_TYPE_236B,
  542. LLM_TYPE_314B,
  543. LLM_TYPE_671B,
  544. @@ -284,6 +286,16 @@ struct llama_layer {
  545. struct ggml_tensor * bskcn_tv = nullptr;
  546. + // cross attention
  547. + struct ggml_tensor * cross_attn_k_norm = nullptr;
  548. + struct ggml_tensor * cross_attn_k_proj = nullptr;
  549. + struct ggml_tensor * cross_attn_o_proj = nullptr;
  550. + struct ggml_tensor * cross_attn_q_norm = nullptr;
  551. + struct ggml_tensor * cross_attn_q_proj = nullptr;
  552. + struct ggml_tensor * cross_attn_v_proj = nullptr;
  553. + struct ggml_tensor * cross_attn_attn_gate = nullptr;
  554. + struct ggml_tensor * cross_attn_mlp_gate = nullptr;
  555. +
  556. struct llama_layer_posnet posnet;
  557. struct llama_layer_convnext convnext;
  558. diff --git a/src/llama-quant.cpp b/src/llama-quant.cpp
  559. index fb798265..6eb1da08 100644
  560. --- a/src/llama-quant.cpp
  561. +++ b/src/llama-quant.cpp
  562. @@ -632,7 +632,9 @@ static void llama_model_quantize_impl(const std::string & fname_inp, const std::
  563. if (llama_model_has_encoder(&model)) {
  564. n_attn_layer *= 3;
  565. }
  566. - GGML_ASSERT((qs.n_attention_wv == n_attn_layer) && "n_attention_wv is unexpected");
  567. + if (qs.n_attention_wv != n_attn_layer) {
  568. + LLAMA_LOG_WARN("%s: n_attention_wv is unexpected, expected: %d, found: %d\n", __func__, n_attn_layer, qs.n_attention_wv);
  569. + }
  570. }
  571. size_t total_size_org = 0;
  572. diff --git a/src/llama.cpp b/src/llama.cpp
  573. index 6d320ea4..8f7902df 100644
  574. --- a/src/llama.cpp
  575. +++ b/src/llama.cpp
  576. @@ -154,6 +154,21 @@ static struct ggml_tensor * llm_build_inp_embd(
  577. return inpL;
  578. }
  579. +static struct ggml_tensor * llm_build_inp_cross_attn_state(
  580. + struct ggml_context * ctx,
  581. + struct llama_context & lctx,
  582. + const llama_hparams & hparams,
  583. + const llm_build_cb & cb) {
  584. + const int64_t n_embd = hparams.n_embd;
  585. +
  586. + struct ggml_tensor * inpCAS = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, n_embd, 1601, 4);
  587. + cb(inpCAS, "inp_cross_attn_state", -1);
  588. + ggml_set_input(inpCAS);
  589. + lctx.inp_cross_attn_state = inpCAS;
  590. +
  591. + return inpCAS;
  592. +}
  593. +
  594. static void llm_build_kv_store(
  595. struct ggml_context * ctx,
  596. const llama_hparams & hparams,
  597. @@ -1157,6 +1172,7 @@ struct llm_build_context {
  598. lctx.inp_pos_bucket = nullptr;
  599. lctx.inp_embd_enc = nullptr;
  600. lctx.inp_KQ_mask_cross = nullptr;
  601. + lctx.inp_cross_attn_state = nullptr;
  602. }
  603. void free() {
  604. @@ -1639,6 +1655,240 @@ struct llm_build_context {
  605. return gf;
  606. }
  607. + struct ggml_cgraph * build_mllama() {
  608. + struct ggml_cgraph * gf = ggml_new_graph_custom(ctx0, model.max_nodes(), false);
  609. +
  610. + // mutable variable, needed during the last layer of the computation to skip unused tokens
  611. + int32_t n_tokens = this->n_tokens;
  612. +
  613. + const int64_t n_embd_head = hparams.n_embd_head_v;
  614. + GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  615. + GGML_ASSERT(n_embd_head == hparams.n_rot);
  616. +
  617. + struct ggml_tensor * cur;
  618. + struct ggml_tensor * inpL;
  619. + struct ggml_tensor * inpCAS;
  620. +
  621. + inpL = llm_build_inp_embd(ctx0, lctx, hparams, ubatch, model.tok_embd, cb);
  622. + inpCAS = llm_build_inp_cross_attn_state(ctx0, lctx, hparams, cb);
  623. +
  624. + // inp_pos - contains the positions
  625. + struct ggml_tensor * inp_pos = build_inp_pos();
  626. +
  627. + // KQ_mask (mask for 1 head, it will be broadcasted to all heads)
  628. + struct ggml_tensor * KQ_mask = build_inp_KQ_mask();
  629. +
  630. + for (int il = 0; il < n_layer; ++il) {
  631. + struct ggml_tensor * inpSA = inpL;
  632. +
  633. + // norm
  634. + cur = llm_build_norm(ctx0, inpL, hparams,
  635. + model.layers[il].attn_norm, NULL,
  636. + LLM_NORM_RMS, cb, il);
  637. + cb(cur, "attn_norm", il);
  638. +
  639. + if (hparams.cross_attention_layers(il)) {
  640. + if (!ubatch.embd && !cparams.cross_attn) {
  641. + continue;
  642. + }
  643. +
  644. + // cross attention layer
  645. + struct ggml_tensor * Qcur = ggml_mul_mat(ctx0, model.layers[il].cross_attn_q_proj, cur);
  646. + cb(Qcur, "Qcur", il);
  647. +
  648. + Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  649. + cb(Qcur, "Qcur", il);
  650. +
  651. + Qcur = ggml_cont(ctx0, ggml_permute(ctx0, Qcur, 0, 2, 1, 3));
  652. + cb(Qcur, "Qcur", il);
  653. +
  654. + Qcur = llm_build_norm(ctx0, Qcur, hparams, model.layers[il].cross_attn_q_norm, NULL, LLM_NORM_RMS, cb, il);
  655. + cb(Qcur, "Qcur", il);
  656. +
  657. + struct ggml_tensor * Kcur, * Vcur;
  658. + if (ubatch.embd) {
  659. + Kcur = ggml_mul_mat(ctx0, model.layers[il].cross_attn_k_proj, inpCAS);
  660. + cb(Kcur, "Kcur", il);
  661. +
  662. + Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, 6404);
  663. + cb(Kcur, "Kcur", il);
  664. +
  665. + Kcur = ggml_cont(ctx0, ggml_permute(ctx0, Kcur, 0, 2, 1, 3));
  666. + cb(Kcur, "Kcur", il);
  667. +
  668. + Kcur = llm_build_norm(ctx0, Kcur, hparams, model.layers[il].cross_attn_k_norm, NULL, LLM_NORM_RMS, cb, il);
  669. + cb(Kcur, "Kcur", il);
  670. +
  671. + ggml_build_forward_expand(gf, ggml_cpy(ctx0, Kcur, kv_self.k_l[il]));
  672. +
  673. + Vcur = ggml_mul_mat(ctx0, model.layers[il].cross_attn_v_proj, inpCAS);
  674. + cb(Vcur, "Vcur", il);
  675. +
  676. + Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, 6404);
  677. + cb(Vcur, "Vcur", il);
  678. +
  679. + Vcur = ggml_permute(ctx0, Vcur, 0, 2, 1, 3);
  680. + cb(Vcur, "Vcur", il);
  681. +
  682. + ggml_build_forward_expand(gf, ggml_cpy(ctx0, Vcur, kv_self.v_l[il]));
  683. + } else {
  684. + Kcur = ggml_view_tensor(ctx0, kv_self.k_l[il]);
  685. + cb(Kcur, "Kcur (view)", il);
  686. +
  687. + Vcur = ggml_view_tensor(ctx0, kv_self.v_l[il]);
  688. + cb(Vcur, "Vcur (view)", il);
  689. + }
  690. +
  691. + struct ggml_tensor * kq = ggml_mul_mat(ctx0, Kcur, Qcur);
  692. + cb(kq, "kq", il);
  693. +
  694. + // TODO: apply causal masks
  695. + 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);
  696. + cb(kq_soft_max, "kq_soft_max", il);
  697. +
  698. + Vcur = ggml_cont(ctx0, ggml_transpose(ctx0, Vcur));
  699. + cb(Vcur, "Vcur", il);
  700. +
  701. + struct ggml_tensor * kqv = ggml_mul_mat(ctx0, Vcur, kq_soft_max);
  702. + cb(kqv, "kqv", il);
  703. +
  704. + struct ggml_tensor * kqv_merged = ggml_permute(ctx0, kqv, 0, 2, 1, 3);
  705. + cb(kqv_merged, "kqv_merged", il);
  706. +
  707. + cur = ggml_cont_2d(ctx0, kqv_merged, n_embd_head_v*n_head, n_tokens);
  708. + cb(cur, "kqv_merged_cont", il);
  709. +
  710. + cur = ggml_mul_mat(ctx0, model.layers[il].cross_attn_o_proj, cur);
  711. + cb(cur, "cur", il);
  712. +
  713. + // TODO: do this in place once?
  714. + cur = ggml_mul(ctx0, cur, ggml_tanh(ctx0, model.layers[il].cross_attn_attn_gate));
  715. +
  716. + struct ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  717. + cb(ffn_inp, "ffn_inp", il);
  718. +
  719. + // feed-forward network
  720. + cur = llm_build_norm(ctx0, ffn_inp, hparams,
  721. + model.layers[il].ffn_norm, NULL,
  722. + LLM_NORM_RMS, cb, il);
  723. + cb(cur, "ffn_norm", il);
  724. +
  725. + cur = llm_build_ffn(ctx0, lctx, cur,
  726. + model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  727. + model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  728. + model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  729. + NULL,
  730. + LLM_FFN_SILU, LLM_FFN_PAR, cb, il);
  731. + cb(cur, "ffn_out", il);
  732. +
  733. + // TODO: do this inplace once?
  734. + cur = ggml_add_inplace(ctx0, ggml_mul_inplace(ctx0, cur, ggml_tanh(ctx0, model.layers[il].cross_attn_mlp_gate)), ffn_inp);
  735. + cb(cur, "ffn_out", il);
  736. +
  737. + cur = lctx.cvec.apply_to(ctx0, cur, il);
  738. + cb(cur, "l_out", il);
  739. +
  740. + // input for next layer
  741. + inpL = cur;
  742. + } else {
  743. + // self attention layer
  744. +
  745. + // rope freq factors for llama3; may return nullptr for llama2 and other models
  746. + struct ggml_tensor * rope_factors = build_rope_factors(il);
  747. +
  748. + // compute Q and K and RoPE them
  749. + struct ggml_tensor * Qcur = llm_build_lora_mm(lctx, ctx0, model.layers[il].wq, cur);
  750. + cb(Qcur, "Qcur", il);
  751. + if (model.layers[il].bq) {
  752. + Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  753. + cb(Qcur, "Qcur", il);
  754. + }
  755. +
  756. + struct ggml_tensor * Kcur = llm_build_lora_mm(lctx, ctx0, model.layers[il].wk, cur);
  757. + cb(Kcur, "Kcur", il);
  758. + if (model.layers[il].bk) {
  759. + Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  760. + cb(Kcur, "Kcur", il);
  761. + }
  762. +
  763. + struct ggml_tensor * Vcur = llm_build_lora_mm(lctx, ctx0, model.layers[il].wv, cur);
  764. + cb(Vcur, "Vcur", il);
  765. + if (model.layers[il].bv) {
  766. + Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  767. + cb(Vcur, "Vcur", il);
  768. + }
  769. +
  770. + Qcur = ggml_rope_ext(
  771. + ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens), inp_pos, rope_factors,
  772. + n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  773. + ext_factor, attn_factor, beta_fast, beta_slow
  774. + );
  775. + cb(Qcur, "Qcur", il);
  776. +
  777. + Kcur = ggml_rope_ext(
  778. + ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens), inp_pos, rope_factors,
  779. + n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  780. + ext_factor, attn_factor, beta_fast, beta_slow
  781. + );
  782. + cb(Kcur, "Kcur", il);
  783. +
  784. + cur = llm_build_kv(ctx0, lctx, kv_self, gf,
  785. + model.layers[il].wo, model.layers[il].bo,
  786. + Kcur, Vcur, Qcur, KQ_mask, n_tokens, kv_head, n_kv, 1.0f/sqrtf(float(n_embd_head)), cb, il);
  787. +
  788. +
  789. + if (il == n_layer - 1) {
  790. + // skip computing output for unused tokens
  791. + struct ggml_tensor * inp_out_ids = build_inp_out_ids();
  792. + n_tokens = n_outputs;
  793. + cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  794. + inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  795. + }
  796. +
  797. + struct ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  798. + cb(ffn_inp, "ffn_inp", il);
  799. +
  800. + // feed-forward network
  801. + cur = llm_build_norm(ctx0, ffn_inp, hparams,
  802. + model.layers[il].ffn_norm, NULL,
  803. + LLM_NORM_RMS, cb, il);
  804. + cb(cur, "ffn_norm", il);
  805. +
  806. + cur = llm_build_ffn(ctx0, lctx, cur,
  807. + model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  808. + model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  809. + model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  810. + NULL,
  811. + LLM_FFN_SILU, LLM_FFN_PAR, cb, il);
  812. + cb(cur, "ffn_out", il);
  813. +
  814. + cur = ggml_add(ctx0, cur, ffn_inp);
  815. + cb(cur, "ffn_out", il);
  816. +
  817. + cur = lctx.cvec.apply_to(ctx0, cur, il);
  818. + cb(cur, "l_out", il);
  819. +
  820. + // input for next layer
  821. + inpL = cur;
  822. + }
  823. + }
  824. +
  825. + cur = inpL;
  826. +
  827. + cur = llm_build_norm(ctx0, cur, hparams,
  828. + model.output_norm, NULL,
  829. + LLM_NORM_RMS, cb, -1);
  830. + cb(cur, "result_norm", -1);
  831. +
  832. + // lm_head
  833. + cur = llm_build_lora_mm(lctx, ctx0, model.output, cur);
  834. + cb(cur, "result_output", -1);
  835. +
  836. + ggml_build_forward_expand(gf, cur);
  837. +
  838. + return gf;
  839. + }
  840. +
  841. struct ggml_cgraph * build_deci() {
  842. struct ggml_cgraph * gf = ggml_new_graph_custom(ctx0, model.max_nodes(), false);
  843. @@ -8344,6 +8594,10 @@ static struct ggml_cgraph * llama_build_graph(
  844. {
  845. result = llm.build_llama();
  846. } break;
  847. + case LLM_ARCH_MLLAMA:
  848. + {
  849. + result = llm.build_mllama();
  850. + } break;
  851. case LLM_ARCH_DECI:
  852. {
  853. result = llm.build_deci();
  854. @@ -8634,7 +8888,7 @@ static int llama_prepare_sbatch(
  855. n_outputs = 1;
  856. }
  857. - lctx.sbatch.from_batch(batch, n_embd,
  858. + lctx.sbatch.from_batch(batch, batch.n_embd,
  859. /* simple_split */ !lctx.kv_self.recurrent,
  860. /* logits_all */ n_outputs == n_tokens_all);
  861. @@ -8749,7 +9003,6 @@ static int llama_decode_impl(
  862. const llama_batch & batch = batch_allocr.batch;
  863. const auto & model = lctx.model;
  864. - const auto & vocab = model.vocab;
  865. const auto & hparams = model.hparams;
  866. const auto & cparams = lctx.cparams;
  867. @@ -8760,7 +9013,7 @@ static int llama_decode_impl(
  868. llama_kv_slot_restorer kv_slot_restorer(kv_self);
  869. const int64_t n_embd = hparams.n_embd;
  870. - const int64_t n_vocab = vocab.n_tokens();
  871. + const int64_t n_vocab = hparams.n_vocab;
  872. uint32_t n_outputs = 0;
  873. uint32_t n_outputs_prev = 0;
  874. @@ -9025,7 +9278,7 @@ static int llama_encode_impl(
  875. const int64_t n_embd = hparams.n_embd;
  876. - lctx.sbatch.from_batch(batch, n_embd, /* simple_split */ true, /* logits_all */ true);
  877. + lctx.sbatch.from_batch(batch, batch.n_embd, /* simple_split */ true, /* logits_all */ true);
  878. const llama_ubatch ubatch = lctx.sbatch.split_simple(n_tokens);
  879. @@ -9511,6 +9764,7 @@ struct llama_context_params llama_context_default_params() {
  880. /*.offload_kqv =*/ true,
  881. /*.flash_attn =*/ false,
  882. /*.no_perf =*/ true,
  883. + /*.cross_attn =*/ false,
  884. /*.abort_callback =*/ nullptr,
  885. /*.abort_callback_data =*/ nullptr,
  886. };