07-gemma.diff 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. From 5cadb45f39d001ffbad95b690d6cf0abcb4a6d96 Mon Sep 17 00:00:00 2001
  2. From: Ollama maintainers <hello@ollama.com>
  3. Date: Wed, 26 Jun 2024 16:18:09 -0700
  4. Subject: [PATCH] Architecture support
  5. ---
  6. llama.cpp | 194 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
  7. 1 file changed, 193 insertions(+), 1 deletion(-)
  8. diff --git a/llama.cpp b/llama.cpp
  9. index 61948751..3b4196f5 100644
  10. --- a/llama.cpp
  11. +++ b/llama.cpp
  12. @@ -217,6 +217,7 @@ enum llm_arch {
  13. LLM_ARCH_INTERNLM2,
  14. LLM_ARCH_MINICPM,
  15. LLM_ARCH_GEMMA,
  16. + LLM_ARCH_GEMMA2,
  17. LLM_ARCH_STARCODER2,
  18. LLM_ARCH_MAMBA,
  19. LLM_ARCH_XVERSE,
  20. @@ -255,6 +256,7 @@ static const std::map<llm_arch, const char *> LLM_ARCH_NAMES = {
  21. { LLM_ARCH_INTERNLM2, "internlm2" },
  22. { LLM_ARCH_MINICPM, "minicpm" },
  23. { LLM_ARCH_GEMMA, "gemma" },
  24. + { LLM_ARCH_GEMMA2, "gemma2" },
  25. { LLM_ARCH_STARCODER2, "starcoder2" },
  26. { LLM_ARCH_MAMBA, "mamba" },
  27. { LLM_ARCH_XVERSE, "xverse" },
  28. @@ -464,10 +466,12 @@ enum llm_tensor {
  29. LLM_TENSOR_ATTN_NORM,
  30. LLM_TENSOR_ATTN_NORM_2,
  31. LLM_TENSOR_ATTN_OUT_NORM,
  32. + LLM_TENSOR_ATTN_POST_NORM,
  33. LLM_TENSOR_ATTN_ROT_EMBD,
  34. LLM_TENSOR_FFN_GATE_INP,
  35. LLM_TENSOR_FFN_GATE_INP_SHEXP,
  36. LLM_TENSOR_FFN_NORM,
  37. + LLM_TENSOR_FFN_POST_NORM,
  38. LLM_TENSOR_FFN_GATE,
  39. LLM_TENSOR_FFN_DOWN,
  40. LLM_TENSOR_FFN_UP,
  41. @@ -960,6 +964,24 @@ static const std::map<llm_arch, std::map<llm_tensor, std::string>> LLM_TENSOR_NA
  42. { LLM_TENSOR_FFN_UP, "blk.%d.ffn_up" },
  43. },
  44. },
  45. + {
  46. + LLM_ARCH_GEMMA2,
  47. + {
  48. + { LLM_TENSOR_TOKEN_EMBD, "token_embd" },
  49. + { LLM_TENSOR_OUTPUT_NORM, "output_norm" },
  50. + { LLM_TENSOR_ATTN_NORM, "blk.%d.attn_norm" },
  51. + { LLM_TENSOR_ATTN_Q, "blk.%d.attn_q" },
  52. + { LLM_TENSOR_ATTN_K, "blk.%d.attn_k" },
  53. + { LLM_TENSOR_ATTN_V, "blk.%d.attn_v" },
  54. + { LLM_TENSOR_ATTN_OUT, "blk.%d.attn_output" },
  55. + { LLM_TENSOR_ATTN_POST_NORM, "blk.%d.post_attention_norm" },
  56. + { LLM_TENSOR_FFN_NORM, "blk.%d.ffn_norm" },
  57. + { LLM_TENSOR_FFN_GATE, "blk.%d.ffn_gate" },
  58. + { LLM_TENSOR_FFN_DOWN, "blk.%d.ffn_down" },
  59. + { LLM_TENSOR_FFN_UP, "blk.%d.ffn_up" },
  60. + { LLM_TENSOR_FFN_POST_NORM, "blk.%d.post_ffw_norm" },
  61. + },
  62. + },
  63. {
  64. LLM_ARCH_STARCODER2,
  65. {
  66. @@ -1941,6 +1963,8 @@ enum e_model {
  67. MODEL_8x22B,
  68. MODEL_16x12B,
  69. MODEL_10B_128x3_66B,
  70. + MODEL_9B,
  71. + MODEL_27B,
  72. };
  73. static const size_t kiB = 1024;
  74. @@ -2114,6 +2138,7 @@ struct llama_layer {
  75. struct ggml_tensor * attn_out_norm_b;
  76. struct ggml_tensor * attn_q_a_norm;
  77. struct ggml_tensor * attn_kv_a_norm;
  78. + struct ggml_tensor * attn_post_norm;
  79. // attention
  80. struct ggml_tensor * wq;
  81. @@ -2136,6 +2161,7 @@ struct llama_layer {
  82. // normalization
  83. struct ggml_tensor * ffn_norm;
  84. struct ggml_tensor * ffn_norm_b;
  85. + struct ggml_tensor * ffn_post_norm;
  86. struct ggml_tensor * layer_out_norm;
  87. struct ggml_tensor * layer_out_norm_b;
  88. struct ggml_tensor * ffn_norm_exps;
  89. @@ -4529,6 +4555,16 @@ static void llm_load_hparams(
  90. }
  91. } break;
  92. case LLM_ARCH_GEMMA:
  93. + {
  94. + ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  95. +
  96. + switch (hparams.n_layer) {
  97. + case 18: model.type = e_model::MODEL_9B; break;
  98. + case 28: model.type = e_model::MODEL_27B; break;
  99. + default: model.type = e_model::MODEL_UNKNOWN;
  100. + }
  101. + } break;
  102. + case LLM_ARCH_GEMMA2:
  103. {
  104. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  105. @@ -6305,6 +6341,40 @@ static bool llm_load_tensors(
  106. layer.ffn_down = ml.create_tensor(ctx_split, tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd});
  107. }
  108. } break;
  109. + case LLM_ARCH_GEMMA2:
  110. + {
  111. + model.tok_embd = ml.create_tensor(ctx_input, tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab});
  112. +
  113. + // output
  114. + model.output_norm = ml.create_tensor(ctx_output, tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd});
  115. + model.output = ml.create_tensor(ctx_output, tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, llama_model_loader::TENSOR_DUPLICATED); // same as tok_embd, duplicated to allow offloading
  116. +
  117. + const int64_t n_ff = hparams.n_ff;
  118. + const int64_t n_embd_head_k = hparams.n_embd_head_k;
  119. + const int64_t n_embd_k_gqa = hparams.n_embd_k_gqa();
  120. + const int64_t n_embd_v_gqa = hparams.n_embd_v_gqa();
  121. +
  122. + for (uint32_t i = 0; i < n_layer; ++i) {
  123. + ggml_context * ctx_layer = ctx_for_layer(i);
  124. + ggml_context * ctx_split = ctx_for_layer_split(i);
  125. +
  126. + auto & layer = model.layers[i];
  127. +
  128. + layer.attn_norm = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd});
  129. +
  130. + layer.wq = ml.create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * hparams.n_head});
  131. + layer.wk = ml.create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa});
  132. + layer.wv = ml.create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa});
  133. + layer.wo = ml.create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * hparams.n_head, n_embd});
  134. + layer.attn_post_norm = ml.create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd});
  135. +
  136. + layer.ffn_norm = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd});
  137. + layer.ffn_gate = ml.create_tensor(ctx_split, tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff});
  138. + layer.ffn_up = ml.create_tensor(ctx_split, tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff});
  139. + layer.ffn_down = ml.create_tensor(ctx_split, tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd});
  140. + layer.ffn_post_norm = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd});
  141. + }
  142. + } break;
  143. case LLM_ARCH_STARCODER2:
  144. {
  145. model.tok_embd = ml.create_tensor(ctx_input, tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab});
  146. @@ -10614,6 +10684,123 @@ struct llm_build_context {
  147. return gf;
  148. }
  149. + struct ggml_cgraph * build_gemma2() {
  150. + struct ggml_cgraph * gf = ggml_new_graph_custom(ctx0, LLAMA_MAX_NODES, false);
  151. +
  152. + const int64_t n_embd_head_k = hparams.n_embd_head_k;
  153. +
  154. + struct ggml_tensor * cur;
  155. + struct ggml_tensor * inpL;
  156. +
  157. + inpL = llm_build_inp_embd(ctx0, lctx, hparams, batch, model.tok_embd, cb);
  158. +
  159. + inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));
  160. + cb(inpL, "inp_scaled", -1);
  161. +
  162. + // inp_pos - contains the positions
  163. + struct ggml_tensor * inp_pos = build_inp_pos();
  164. +
  165. + // KQ_mask (mask for 1 head, it will be broadcasted to all heads)
  166. + struct ggml_tensor * KQ_mask = build_inp_KQ_mask();
  167. +
  168. + for (int il = 0; il < n_layer; ++il) {
  169. + // norm
  170. + cur = llm_build_norm(ctx0, inpL, hparams,
  171. + model.layers[il].attn_norm, NULL,
  172. + LLM_NORM_RMS, cb, il);
  173. + cb(cur, "attn_norm", il);
  174. +
  175. + // self-attention
  176. + {
  177. + // compute Q and K and RoPE them
  178. + struct ggml_tensor * Qcur = ggml_mul_mat(ctx0, model.layers[il].wq, cur);
  179. + cb(Qcur, "Qcur", il);
  180. +
  181. + struct ggml_tensor * Kcur = ggml_mul_mat(ctx0, model.layers[il].wk, cur);
  182. + cb(Kcur, "Kcur", il);
  183. +
  184. + struct ggml_tensor * Vcur = ggml_mul_mat(ctx0, model.layers[il].wv, cur);
  185. + cb(Vcur, "Vcur", il);
  186. +
  187. + Qcur = ggml_rope_ext(
  188. + ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head_k, n_head, n_tokens), inp_pos, nullptr,
  189. + n_embd_head_k, rope_type, n_ctx_orig, freq_base, freq_scale,
  190. + ext_factor, attn_factor, beta_fast, beta_slow);
  191. + cb(Qcur, "Qcur", il);
  192. +
  193. + Qcur = ggml_scale(ctx0, Qcur, 1.0f / sqrtf(float(n_embd_head_k)));
  194. + cb(Qcur, "Qcur_scaled", il);
  195. +
  196. + Kcur = ggml_rope_ext(
  197. + ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head_k, n_head_kv, n_tokens), inp_pos, nullptr,
  198. + n_embd_head_k, rope_type, n_ctx_orig, freq_base, freq_scale,
  199. + ext_factor, attn_factor, beta_fast, beta_slow);
  200. + cb(Kcur, "Kcur", il);
  201. +
  202. + cur = llm_build_kv(ctx0, model, hparams, cparams, kv_self, gf,
  203. + model.layers[il].wo, NULL,
  204. + Kcur, Vcur, Qcur, KQ_mask, n_tokens, kv_head, n_kv, 1.0f, cb, il);
  205. + }
  206. +
  207. + if (il == n_layer - 1) {
  208. + // skip computing output for unused tokens
  209. + struct ggml_tensor * inp_out_ids = build_inp_out_ids();
  210. + cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  211. + inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  212. + }
  213. +
  214. + cur = llm_build_norm(ctx0, cur, hparams,
  215. + model.layers[il].attn_post_norm, NULL,
  216. + LLM_NORM_RMS, cb, il);
  217. + cb(cur, "attn_post_norm", il);
  218. +
  219. + struct ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL);
  220. + cb(sa_out, "sa_out", il);
  221. +
  222. + cur = llm_build_norm(ctx0, sa_out, hparams,
  223. + model.layers[il].ffn_norm, NULL,
  224. + LLM_NORM_RMS, cb, il);
  225. + cb(cur, "ffn_norm", il);
  226. +
  227. + // feed-forward network
  228. + {
  229. + cur = llm_build_ffn(ctx0, cur,
  230. + model.layers[il].ffn_up, NULL,
  231. + model.layers[il].ffn_gate, NULL,
  232. + model.layers[il].ffn_down, NULL,
  233. + NULL,
  234. + LLM_FFN_GELU, LLM_FFN_PAR, cb, il);
  235. + cb(cur, "ffn_out", il);
  236. + }
  237. +
  238. + cur = llm_build_norm(ctx0, cur, hparams,
  239. + model.layers[il].ffn_post_norm, NULL,
  240. + LLM_NORM_RMS, cb, -1);
  241. + cb(cur, "ffn_post_norm", -1);
  242. +
  243. + cur = ggml_add(ctx0, cur, sa_out);
  244. + cb(cur, "l_out", il);
  245. +
  246. + // input for next layer
  247. + inpL = cur;
  248. + }
  249. +
  250. + cur = inpL;
  251. +
  252. + cur = llm_build_norm(ctx0, cur, hparams,
  253. + model.output_norm, NULL,
  254. + LLM_NORM_RMS, cb, -1);
  255. + cb(cur, "result_norm", -1);
  256. +
  257. + // lm_head
  258. + cur = ggml_mul_mat(ctx0, model.output, cur);
  259. + cb(cur, "result_output", -1);
  260. +
  261. + ggml_build_forward_expand(gf, cur);
  262. +
  263. + return gf;
  264. + }
  265. +
  266. struct ggml_cgraph * build_starcoder2() {
  267. struct ggml_cgraph * gf = ggml_new_graph_custom(ctx0, LLAMA_MAX_NODES, false);
  268. @@ -11847,6 +12034,10 @@ static struct ggml_cgraph * llama_build_graph(
  269. {
  270. result = llm.build_gemma();
  271. } break;
  272. + case LLM_ARCH_GEMMA2:
  273. + {
  274. + result = llm.build_gemma2();
  275. + } break;
  276. case LLM_ARCH_STARCODER2:
  277. {
  278. result = llm.build_starcoder2();
  279. @@ -16671,6 +16862,7 @@ enum llama_rope_type llama_rope_type(const struct llama_model * model) {
  280. case LLM_ARCH_PHI2:
  281. case LLM_ARCH_PHI3:
  282. case LLM_ARCH_GEMMA:
  283. + case LLM_ARCH_GEMMA2:
  284. case LLM_ARCH_STARCODER2:
  285. case LLM_ARCH_GPTNEOX:
  286. return LLAMA_ROPE_TYPE_NEOX;
  287. @@ -18551,7 +18743,7 @@ static int32_t llama_chat_apply_template_internal(
  288. if (add_ass) {
  289. ss << "<s>assistant\n";
  290. }
  291. - } else if (tmpl == "gemma" || tmpl.find("<start_of_turn>") != std::string::npos) {
  292. + } else if (tmpl == "gemma" || tmpl == "gemma2" || tmpl.find("<start_of_turn>") != std::string::npos) {
  293. // google/gemma-7b-it
  294. std::string system_prompt = "";
  295. for (auto message : chat) {
  296. --
  297. 2.45.2