0005-solar-pro.patch 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Michael Yang <mxyng@pm.me>
  3. Date: Mon, 16 Sep 2024 15:53:16 -0700
  4. Subject: [PATCH] solar-pro
  5. solar-pro introduces block skip connections where blocks are connected
  6. to other, non-sequential blocks with a scale multiple
  7. this change adds 4 new keys to store the skip connections and one new
  8. tensor to store the scalar. the scalar is implemented a 1-dimensional
  9. tensor with 2 elements dervied from the model's bskcn_tv configuration.
  10. in general, the values are (bskcn_tv, 1 - bskcn_tv)
  11. ---
  12. src/llama-arch.cpp | 53 +++++++----
  13. src/llama-arch.h | 3 +
  14. src/llama-hparams.cpp | 8 ++
  15. src/llama-hparams.h | 5 +
  16. src/llama-model-loader.cpp | 1 +
  17. src/llama-model.cpp | 16 ++++
  18. src/llama-model.h | 3 +
  19. src/llama.cpp | 185 +++++++++++++++++++++++++++++++++++++
  20. 8 files changed, 258 insertions(+), 16 deletions(-)
  21. diff --git a/src/llama-arch.cpp b/src/llama-arch.cpp
  22. index 007d79f8..5b376c5e 100644
  23. --- a/src/llama-arch.cpp
  24. +++ b/src/llama-arch.cpp
  25. @@ -59,6 +59,7 @@ static const std::map<llm_arch, const char *> LLM_ARCH_NAMES = {
  26. { LLM_ARCH_GRANITE, "granite" },
  27. { LLM_ARCH_GRANITE_MOE, "granitemoe" },
  28. { LLM_ARCH_CHAMELEON, "chameleon" },
  29. + { LLM_ARCH_SOLAR, "solar" },
  30. { LLM_ARCH_WAVTOKENIZER_DEC, "wavtokenizer-dec" },
  31. { LLM_ARCH_UNKNOWN, "(unknown)" },
  32. };
  33. @@ -106,22 +107,23 @@ static const std::map<llm_kv, const char *> LLM_KV_NAMES = {
  34. { LLM_KV_RESIDUAL_SCALE, "%s.residual_scale" },
  35. { LLM_KV_EMBEDDING_SCALE, "%s.embedding_scale" },
  36. - { LLM_KV_ATTENTION_HEAD_COUNT, "%s.attention.head_count" },
  37. - { LLM_KV_ATTENTION_HEAD_COUNT_KV, "%s.attention.head_count_kv" },
  38. - { LLM_KV_ATTENTION_MAX_ALIBI_BIAS, "%s.attention.max_alibi_bias" },
  39. - { LLM_KV_ATTENTION_CLAMP_KQV, "%s.attention.clamp_kqv" },
  40. - { LLM_KV_ATTENTION_KEY_LENGTH, "%s.attention.key_length" },
  41. - { LLM_KV_ATTENTION_VALUE_LENGTH, "%s.attention.value_length" },
  42. - { LLM_KV_ATTENTION_LAYERNORM_EPS, "%s.attention.layer_norm_epsilon" },
  43. - { LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, "%s.attention.layer_norm_rms_epsilon" },
  44. - { LLM_KV_ATTENTION_GROUPNORM_EPS, "%s.attention.group_norm_epsilon" },
  45. - { LLM_KV_ATTENTION_GROUPNORM_GROUPS, "%s.attention.group_norm_groups" },
  46. - { LLM_KV_ATTENTION_CAUSAL, "%s.attention.causal" },
  47. - { LLM_KV_ATTENTION_Q_LORA_RANK, "%s.attention.q_lora_rank" },
  48. - { LLM_KV_ATTENTION_KV_LORA_RANK, "%s.attention.kv_lora_rank" },
  49. - { LLM_KV_ATTENTION_RELATIVE_BUCKETS_COUNT, "%s.attention.relative_buckets_count" },
  50. - { LLM_KV_ATTENTION_SLIDING_WINDOW, "%s.attention.sliding_window" },
  51. - { LLM_KV_ATTENTION_SCALE, "%s.attention.scale" },
  52. + { LLM_KV_ATTENTION_HEAD_COUNT, "%s.attention.head_count" },
  53. + { LLM_KV_ATTENTION_HEAD_COUNT_KV, "%s.attention.head_count_kv" },
  54. + { LLM_KV_ATTENTION_MAX_ALIBI_BIAS, "%s.attention.max_alibi_bias" },
  55. + { LLM_KV_ATTENTION_CLAMP_KQV, "%s.attention.clamp_kqv" },
  56. + { LLM_KV_ATTENTION_KEY_LENGTH, "%s.attention.key_length" },
  57. + { LLM_KV_ATTENTION_VALUE_LENGTH, "%s.attention.value_length" },
  58. + { LLM_KV_ATTENTION_LAYERNORM_EPS, "%s.attention.layer_norm_epsilon" },
  59. + { LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, "%s.attention.layer_norm_rms_epsilon" },
  60. + { LLM_KV_ATTENTION_GROUPNORM_EPS, "%s.attention.group_norm_epsilon" },
  61. + { LLM_KV_ATTENTION_GROUPNORM_GROUPS, "%s.attention.group_norm_groups" },
  62. + { LLM_KV_ATTENTION_CAUSAL, "%s.attention.causal" },
  63. + { LLM_KV_ATTENTION_Q_LORA_RANK, "%s.attention.q_lora_rank" },
  64. + { LLM_KV_ATTENTION_KV_LORA_RANK, "%s.attention.kv_lora_rank" },
  65. + { LLM_KV_ATTENTION_RELATIVE_BUCKETS_COUNT, "%s.attention.relative_buckets_count" },
  66. + { LLM_KV_ATTENTION_SLIDING_WINDOW, "%s.attention.sliding_window" },
  67. + { LLM_KV_ATTENTION_SCALE, "%s.attention.scale" },
  68. + { LLM_KV_ATTENTION_BLOCK_SKIP_CONNECTION, "%s.attention.block_skip_connection" },
  69. { LLM_KV_ROPE_DIMENSION_COUNT, "%s.rope.dimension_count" },
  70. { LLM_KV_ROPE_DIMENSION_SECTIONS, "%s.rope.dimension_sections" },
  71. @@ -1240,6 +1242,24 @@ static const std::map<llm_arch, std::map<llm_tensor, const char *>> LLM_TENSOR_N
  72. { LLM_TENSOR_POS_NET_ATTN_OUT, "posnet.%d.attn_output" },
  73. },
  74. },
  75. + {
  76. + LLM_ARCH_SOLAR,
  77. + {
  78. + { LLM_TENSOR_TOKEN_EMBD, "token_embd" },
  79. + { LLM_TENSOR_OUTPUT_NORM, "output_norm" },
  80. + { LLM_TENSOR_OUTPUT, "output" },
  81. + { LLM_TENSOR_ATTN_NORM, "blk.%d.attn_norm" },
  82. + { LLM_TENSOR_ATTN_Q, "blk.%d.attn_q" },
  83. + { LLM_TENSOR_ATTN_K, "blk.%d.attn_k" },
  84. + { LLM_TENSOR_ATTN_V, "blk.%d.attn_v" },
  85. + { LLM_TENSOR_ATTN_OUT, "blk.%d.attn_output" },
  86. + { LLM_TENSOR_FFN_NORM, "blk.%d.ffn_norm" },
  87. + { LLM_TENSOR_FFN_GATE, "blk.%d.ffn_gate" },
  88. + { LLM_TENSOR_FFN_DOWN, "blk.%d.ffn_down" },
  89. + { LLM_TENSOR_FFN_UP, "blk.%d.ffn_up" },
  90. + { LLM_TENSOR_BSKCN_TV, "bskcn_tv" },
  91. + },
  92. + },
  93. {
  94. LLM_ARCH_UNKNOWN,
  95. {
  96. @@ -1372,6 +1392,7 @@ static const std::map<llm_tensor, llm_tensor_info> LLM_TENSOR_INFOS = {
  97. {LLM_TENSOR_FFN_EXP_PROBS_B, {LLM_TENSOR_LAYER_REPEATING, GGML_OP_ADD}},
  98. // this tensor is loaded for T5, but never used
  99. {LLM_TENSOR_DEC_CROSS_ATTN_REL_B, {LLM_TENSOR_LAYER_REPEATING, GGML_OP_NONE}},
  100. + {LLM_TENSOR_BSKCN_TV, {LLM_TENSOR_LAYER_REPEATING, GGML_OP_MUL}},
  101. {LLM_TENSOR_CONV1D, {LLM_TENSOR_LAYER_INPUT, GGML_OP_IM2COL}},
  102. {LLM_TENSOR_POS_NET_NORM, {LLM_TENSOR_LAYER_REPEATING, GGML_OP_MUL}},
  103. {LLM_TENSOR_POS_NET_NORM1, {LLM_TENSOR_LAYER_REPEATING, GGML_OP_MUL}},
  104. diff --git a/src/llama-arch.h b/src/llama-arch.h
  105. index 45e458bb..eac7055b 100644
  106. --- a/src/llama-arch.h
  107. +++ b/src/llama-arch.h
  108. @@ -63,6 +63,7 @@ enum llm_arch {
  109. LLM_ARCH_GRANITE,
  110. LLM_ARCH_GRANITE_MOE,
  111. LLM_ARCH_CHAMELEON,
  112. + LLM_ARCH_SOLAR,
  113. LLM_ARCH_WAVTOKENIZER_DEC,
  114. LLM_ARCH_UNKNOWN,
  115. };
  116. @@ -126,6 +127,7 @@ enum llm_kv {
  117. LLM_KV_ATTENTION_RELATIVE_BUCKETS_COUNT,
  118. LLM_KV_ATTENTION_SLIDING_WINDOW,
  119. LLM_KV_ATTENTION_SCALE,
  120. + LLM_KV_ATTENTION_BLOCK_SKIP_CONNECTION,
  121. LLM_KV_ROPE_DIMENSION_COUNT,
  122. LLM_KV_ROPE_DIMENSION_SECTIONS,
  123. @@ -305,6 +307,7 @@ enum llm_tensor {
  124. LLM_TENSOR_ENC_OUTPUT_NORM,
  125. LLM_TENSOR_CLS,
  126. LLM_TENSOR_CLS_OUT,
  127. + LLM_TENSOR_BSKCN_TV,
  128. LLM_TENSOR_CONV1D,
  129. LLM_TENSOR_CONVNEXT_DW,
  130. LLM_TENSOR_CONVNEXT_NORM,
  131. diff --git a/src/llama-hparams.cpp b/src/llama-hparams.cpp
  132. index c4053469..450738da 100644
  133. --- a/src/llama-hparams.cpp
  134. +++ b/src/llama-hparams.cpp
  135. @@ -69,3 +69,11 @@ uint32_t llama_hparams::n_embd_v_s() const {
  136. // corresponds to Mamba's ssm_states size
  137. return ssm_d_state * ssm_d_inner;
  138. }
  139. +
  140. +bool llama_hparams::n_bskcn(uint32_t n, uint32_t il) const {
  141. + if (il < n_layer) {
  142. + return n_bskcn_arr[n][il] > 0;
  143. + }
  144. +
  145. + GGML_ABORT("fatal error");
  146. +}
  147. \ No newline at end of file
  148. diff --git a/src/llama-hparams.h b/src/llama-hparams.h
  149. index a29f20ec..fd898e27 100644
  150. --- a/src/llama-hparams.h
  151. +++ b/src/llama-hparams.h
  152. @@ -52,6 +52,8 @@ struct llama_hparams {
  153. std::array<uint32_t, LLAMA_MAX_LAYERS> n_head_kv_arr;
  154. std::array<uint32_t, LLAMA_MAX_LAYERS> n_ff_arr;
  155. + std::array<std::array<uint32_t, LLAMA_MAX_LAYERS>, 4> n_bskcn_arr = {};
  156. +
  157. uint32_t n_layer_dense_lead = 0;
  158. uint32_t n_lora_q = 0;
  159. uint32_t n_lora_kv = 0;
  160. @@ -134,6 +136,9 @@ struct llama_hparams {
  161. // dimension of the recurrent state embeddings
  162. uint32_t n_embd_v_s() const;
  163. +
  164. + // Block skip connection
  165. + bool n_bskcn(uint32_t n, uint32_t il) const;
  166. };
  167. static_assert(std::is_trivially_copyable<llama_hparams>::value, "llama_hparams must be trivially copyable");
  168. diff --git a/src/llama-model-loader.cpp b/src/llama-model-loader.cpp
  169. index 7743b465..422524a8 100644
  170. --- a/src/llama-model-loader.cpp
  171. +++ b/src/llama-model-loader.cpp
  172. @@ -364,6 +364,7 @@ namespace GGUFMeta {
  173. // TODO: this is not very clever - figure out something better
  174. template bool llama_model_loader::get_key_or_arr<std::array<int, 4>>(enum llm_kv kid, std::array<int, 4> & result, uint32_t n, bool required);
  175. template bool llama_model_loader::get_key_or_arr<std::array<uint32_t, 512>>(enum llm_kv kid, std::array<uint32_t, 512> & result, uint32_t n, bool required);
  176. + template bool llama_model_loader::get_key_or_arr<uint32_t>(const std::string & key, std::array<uint32_t, 512> & result, uint32_t n, bool required);
  177. llama_model_loader::llama_model_loader(const std::string & fname, bool use_mmap, bool check_tensors, const struct llama_model_kv_override * param_overrides_p) {
  178. int trace = 0;
  179. diff --git a/src/llama-model.cpp b/src/llama-model.cpp
  180. index 00b80c52..306c557d 100644
  181. --- a/src/llama-model.cpp
  182. +++ b/src/llama-model.cpp
  183. @@ -1091,6 +1091,21 @@ void llm_load_hparams(llama_model_loader & ml, llama_model & model) {
  184. default: model.type = e_model::MODEL_UNKNOWN;
  185. }
  186. } break;
  187. + case LLM_ARCH_SOLAR:
  188. + {
  189. + ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  190. + for (size_t i = 0; i < hparams.n_bskcn_arr.max_size(); ++i) {
  191. + auto & bskcn = hparams.n_bskcn_arr[i];
  192. + bskcn.fill(0);
  193. + auto kv = LLM_KV(model.arch);
  194. + ml.get_key_or_arr(format((kv(LLM_KV_ATTENTION_BLOCK_SKIP_CONNECTION) + ".%d").c_str(), i), bskcn, hparams.n_layer, false);
  195. + }
  196. +
  197. + switch (hparams.n_layer) {
  198. + case 64: model.type = e_model::MODEL_22B; break;
  199. + default: model.type = e_model::MODEL_UNKNOWN;
  200. + }
  201. + } break;
  202. case LLM_ARCH_WAVTOKENIZER_DEC:
  203. {
  204. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  205. @@ -2065,6 +2080,7 @@ enum llama_rope_type llama_rope_type(const struct llama_model * model) {
  206. case LLM_ARCH_GRANITE:
  207. case LLM_ARCH_GRANITE_MOE:
  208. case LLM_ARCH_CHAMELEON:
  209. + case LLM_ARCH_SOLAR:
  210. return LLAMA_ROPE_TYPE_NORM;
  211. // the pairs of head values are offset by n_rot/2
  212. diff --git a/src/llama-model.h b/src/llama-model.h
  213. index ce038932..c1b9c0a1 100644
  214. --- a/src/llama-model.h
  215. +++ b/src/llama-model.h
  216. @@ -54,6 +54,7 @@ enum llm_type {
  217. MODEL_15B,
  218. MODEL_16B,
  219. MODEL_20B,
  220. + MODEL_22B,
  221. MODEL_30B,
  222. MODEL_32B,
  223. MODEL_34B,
  224. @@ -275,6 +276,8 @@ struct llama_layer {
  225. struct ggml_tensor * ffn_up_scale = nullptr;
  226. struct ggml_tensor * ffn_down_scale = nullptr;
  227. + struct ggml_tensor * bskcn_tv = nullptr;
  228. +
  229. struct llama_layer_posnet posnet;
  230. struct llama_layer_convnext convnext;
  231. diff --git a/src/llama.cpp b/src/llama.cpp
  232. index 4eb3f6b9..7dec50ae 100644
  233. --- a/src/llama.cpp
  234. +++ b/src/llama.cpp
  235. @@ -2206,6 +2206,35 @@ static bool llm_load_tensors(
  236. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  237. + layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  238. + layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  239. + layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  240. + }
  241. + } break;
  242. + case LLM_ARCH_SOLAR:
  243. + {
  244. + model.tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  245. +
  246. + // output
  247. + {
  248. + model.output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  249. + model.output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, llama_model_loader::TENSOR_NOT_REQUIRED);
  250. + }
  251. +
  252. + for (int i = 0; i < n_layer; ++i) {
  253. + auto & layer = model.layers[i];
  254. +
  255. + layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  256. +
  257. + layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  258. + layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  259. + layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  260. + layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  261. +
  262. + layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  263. +
  264. + layer.bskcn_tv = create_tensor(tn(LLM_TENSOR_BSKCN_TV, "weight", i), {2}, llama_model_loader::TENSOR_NOT_REQUIRED | (i != 0 ? llama_model_loader::TENSOR_DUPLICATED : 0));
  265. +
  266. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  267. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  268. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  269. @@ -10226,6 +10255,158 @@ struct llm_build_context {
  270. return gf;
  271. }
  272. + ggml_cgraph * build_solar() {
  273. + struct ggml_cgraph * gf = ggml_new_graph_custom(ctx0, llama_model_max_nodes(model), false);
  274. +
  275. + // mutable variable, needed during the last layer of the computation to skip unused tokens
  276. + int32_t n_tokens = this->n_tokens;
  277. +
  278. + const int64_t n_embd_head = hparams.n_embd_head_v;
  279. + GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  280. + GGML_ASSERT(n_embd_head == hparams.n_rot);
  281. +
  282. + struct ggml_tensor * cur;
  283. + struct ggml_tensor * inpL;
  284. +
  285. + inpL = llm_build_inp_embd(ctx0, lctx, hparams, ubatch, model.tok_embd, cb);
  286. +
  287. + // inp_pos - contains the positions
  288. + struct ggml_tensor * inp_pos = build_inp_pos();
  289. +
  290. + // KQ_mask (mask for 1 head, it will be broadcasted to all heads)
  291. + struct ggml_tensor * KQ_mask = build_inp_KQ_mask();
  292. +
  293. + struct ggml_tensor * bskcn_1;
  294. + struct ggml_tensor * bskcn_2;
  295. +
  296. + for (int il = 0; il < n_layer; ++il) {
  297. + struct ggml_tensor * inpSA = inpL;
  298. +
  299. + if (hparams.n_bskcn(0, il)) {
  300. + bskcn_1 = inpSA;
  301. + }
  302. +
  303. + if (hparams.n_bskcn(1, il)) {
  304. + bskcn_2 = inpSA;
  305. + }
  306. +
  307. + if (hparams.n_bskcn(2, il)) {
  308. + inpSA = ggml_add(
  309. + ctx0,
  310. + ggml_mul(ctx0, bskcn_1, ggml_view_1d(ctx0, model.layers[il].bskcn_tv, 1, 0)),
  311. + ggml_mul(ctx0, inpSA, ggml_view_1d(ctx0, model.layers[il].bskcn_tv, 1, ggml_element_size(model.layers[il].bskcn_tv))));
  312. + }
  313. +
  314. + if (hparams.n_bskcn(3, il)) {
  315. + inpSA = ggml_add(
  316. + ctx0,
  317. + ggml_mul(ctx0, bskcn_2, ggml_view_1d(ctx0, model.layers[il].bskcn_tv, 1, 0)),
  318. + ggml_mul(ctx0, inpSA, ggml_view_1d(ctx0, model.layers[il].bskcn_tv, 1, ggml_element_size(model.layers[il].bskcn_tv))));
  319. + }
  320. +
  321. + // norm
  322. + cur = llm_build_norm(ctx0, inpL, hparams,
  323. + model.layers[il].attn_norm, NULL,
  324. + LLM_NORM_RMS, cb, il);
  325. + cb(cur, "attn_norm", il);
  326. +
  327. + // self-attention
  328. + {
  329. + // rope freq factors for llama3; may return nullptr for llama2 and other models
  330. + struct ggml_tensor * rope_factors = build_rope_factors(il);
  331. +
  332. + // compute Q and K and RoPE them
  333. + struct ggml_tensor * Qcur = llm_build_lora_mm(lctx, ctx0, model.layers[il].wq, cur);
  334. + cb(Qcur, "Qcur", il);
  335. + if (model.layers[il].bq) {
  336. + Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  337. + cb(Qcur, "Qcur", il);
  338. + }
  339. +
  340. + struct ggml_tensor * Kcur = llm_build_lora_mm(lctx, ctx0, model.layers[il].wk, cur);
  341. + cb(Kcur, "Kcur", il);
  342. + if (model.layers[il].bk) {
  343. + Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  344. + cb(Kcur, "Kcur", il);
  345. + }
  346. +
  347. + struct ggml_tensor * Vcur = llm_build_lora_mm(lctx, ctx0, model.layers[il].wv, cur);
  348. + cb(Vcur, "Vcur", il);
  349. + if (model.layers[il].bv) {
  350. + Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  351. + cb(Vcur, "Vcur", il);
  352. + }
  353. +
  354. + Qcur = ggml_rope_ext(
  355. + ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens), inp_pos, rope_factors,
  356. + n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  357. + ext_factor, attn_factor, beta_fast, beta_slow
  358. + );
  359. + cb(Qcur, "Qcur", il);
  360. +
  361. + Kcur = ggml_rope_ext(
  362. + ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens), inp_pos, rope_factors,
  363. + n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  364. + ext_factor, attn_factor, beta_fast, beta_slow
  365. + );
  366. + cb(Kcur, "Kcur", il);
  367. +
  368. + cur = llm_build_kv(ctx0, lctx, kv_self, gf,
  369. + model.layers[il].wo, model.layers[il].bo,
  370. + Kcur, Vcur, Qcur, KQ_mask, n_tokens, kv_head, n_kv, 1.0f/sqrtf(float(n_embd_head)), cb, il);
  371. + }
  372. +
  373. + if (il == n_layer - 1) {
  374. + // skip computing output for unused tokens
  375. + struct ggml_tensor * inp_out_ids = build_inp_out_ids();
  376. + n_tokens = n_outputs;
  377. + cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  378. + inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  379. + }
  380. +
  381. + struct ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  382. + cb(ffn_inp, "ffn_inp", il);
  383. +
  384. + // feed-forward network
  385. + cur = llm_build_norm(ctx0, ffn_inp, hparams,
  386. + model.layers[il].ffn_norm, NULL,
  387. + LLM_NORM_RMS, cb, il);
  388. + cb(cur, "ffn_norm", il);
  389. +
  390. + cur = llm_build_ffn(ctx0, lctx, cur,
  391. + model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  392. + model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  393. + model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  394. + NULL,
  395. + LLM_FFN_SILU, LLM_FFN_PAR, cb, il);
  396. + cb(cur, "ffn_out", il);
  397. +
  398. + cur = ggml_add(ctx0, cur, ffn_inp);
  399. + cb(cur, "ffn_out", il);
  400. +
  401. + cur = lctx.cvec.apply_to(ctx0, cur, il);
  402. + cb(cur, "l_out", il);
  403. +
  404. + // input for next layer
  405. + inpL = cur;
  406. + }
  407. +
  408. + cur = inpL;
  409. +
  410. + cur = llm_build_norm(ctx0, cur, hparams,
  411. + model.output_norm, NULL,
  412. + LLM_NORM_RMS, cb, -1);
  413. + cb(cur, "result_norm", -1);
  414. +
  415. + // lm_head
  416. + cur = llm_build_lora_mm(lctx, ctx0, model.output, cur);
  417. + cb(cur, "result_output", -1);
  418. +
  419. + ggml_build_forward_expand(gf, cur);
  420. +
  421. + return gf;
  422. + }
  423. +
  424. struct ggml_cgraph * build_wavtokenizer_dec() {
  425. struct ggml_cgraph * gf = ggml_new_graph_custom(ctx0, llama_model_max_nodes(model), false);
  426. @@ -10660,6 +10841,10 @@ static struct ggml_cgraph * llama_build_graph(
  427. {
  428. result = llm.build_chameleon();
  429. } break;
  430. + case LLM_ARCH_SOLAR:
  431. + {
  432. + result = llm.build_solar();
  433. + } break;
  434. case LLM_ARCH_WAVTOKENIZER_DEC:
  435. {
  436. result = llm.build_wavtokenizer_dec();