0007-solar-pro.patch 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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.cpp | 269 +++++++++++++++++++++++++++++++++++++++++++++++---
  13. 1 file changed, 255 insertions(+), 14 deletions(-)
  14. diff --git a/src/llama.cpp b/src/llama.cpp
  15. index a639522d..83b80b59 100644
  16. --- a/src/llama.cpp
  17. +++ b/src/llama.cpp
  18. @@ -217,6 +217,7 @@ enum llm_arch {
  19. LLM_ARCH_GRANITE,
  20. LLM_ARCH_GRANITE_MOE,
  21. LLM_ARCH_CHAMELEON,
  22. + LLM_ARCH_SOLAR,
  23. LLM_ARCH_UNKNOWN,
  24. };
  25. @@ -270,6 +271,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_UNKNOWN, "(unknown)" },
  31. };
  32. @@ -327,6 +329,7 @@ enum llm_kv {
  33. LLM_KV_ATTENTION_RELATIVE_BUCKETS_COUNT,
  34. LLM_KV_ATTENTION_SLIDING_WINDOW,
  35. LLM_KV_ATTENTION_SCALE,
  36. + LLM_KV_ATTENTION_BLOCK_SKIP_CONNECTION,
  37. LLM_KV_ROPE_DIMENSION_COUNT,
  38. LLM_KV_ROPE_FREQ_BASE,
  39. @@ -421,20 +424,21 @@ static const std::map<llm_kv, const char *> LLM_KV_NAMES = {
  40. { LLM_KV_RESIDUAL_SCALE, "%s.residual_scale" },
  41. { LLM_KV_EMBEDDING_SCALE, "%s.embedding_scale" },
  42. - { LLM_KV_ATTENTION_HEAD_COUNT, "%s.attention.head_count" },
  43. - { LLM_KV_ATTENTION_HEAD_COUNT_KV, "%s.attention.head_count_kv" },
  44. - { LLM_KV_ATTENTION_MAX_ALIBI_BIAS, "%s.attention.max_alibi_bias" },
  45. - { LLM_KV_ATTENTION_CLAMP_KQV, "%s.attention.clamp_kqv" },
  46. - { LLM_KV_ATTENTION_KEY_LENGTH, "%s.attention.key_length" },
  47. - { LLM_KV_ATTENTION_VALUE_LENGTH, "%s.attention.value_length" },
  48. - { LLM_KV_ATTENTION_LAYERNORM_EPS, "%s.attention.layer_norm_epsilon" },
  49. - { LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, "%s.attention.layer_norm_rms_epsilon" },
  50. - { LLM_KV_ATTENTION_CAUSAL, "%s.attention.causal" },
  51. - { LLM_KV_ATTENTION_Q_LORA_RANK, "%s.attention.q_lora_rank" },
  52. - { LLM_KV_ATTENTION_KV_LORA_RANK, "%s.attention.kv_lora_rank" },
  53. - { LLM_KV_ATTENTION_RELATIVE_BUCKETS_COUNT, "%s.attention.relative_buckets_count" },
  54. - { LLM_KV_ATTENTION_SLIDING_WINDOW, "%s.attention.sliding_window" },
  55. - { LLM_KV_ATTENTION_SCALE, "%s.attention.scale" },
  56. + { LLM_KV_ATTENTION_HEAD_COUNT, "%s.attention.head_count" },
  57. + { LLM_KV_ATTENTION_HEAD_COUNT_KV, "%s.attention.head_count_kv" },
  58. + { LLM_KV_ATTENTION_MAX_ALIBI_BIAS, "%s.attention.max_alibi_bias" },
  59. + { LLM_KV_ATTENTION_CLAMP_KQV, "%s.attention.clamp_kqv" },
  60. + { LLM_KV_ATTENTION_KEY_LENGTH, "%s.attention.key_length" },
  61. + { LLM_KV_ATTENTION_VALUE_LENGTH, "%s.attention.value_length" },
  62. + { LLM_KV_ATTENTION_LAYERNORM_EPS, "%s.attention.layer_norm_epsilon" },
  63. + { LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, "%s.attention.layer_norm_rms_epsilon" },
  64. + { LLM_KV_ATTENTION_CAUSAL, "%s.attention.causal" },
  65. + { LLM_KV_ATTENTION_Q_LORA_RANK, "%s.attention.q_lora_rank" },
  66. + { LLM_KV_ATTENTION_KV_LORA_RANK, "%s.attention.kv_lora_rank" },
  67. + { LLM_KV_ATTENTION_RELATIVE_BUCKETS_COUNT, "%s.attention.relative_buckets_count" },
  68. + { LLM_KV_ATTENTION_SLIDING_WINDOW, "%s.attention.sliding_window" },
  69. + { LLM_KV_ATTENTION_SCALE, "%s.attention.scale" },
  70. + { LLM_KV_ATTENTION_BLOCK_SKIP_CONNECTION, "%s.attention.block_skip_connection.%d" },
  71. { LLM_KV_ROPE_DIMENSION_COUNT, "%s.rope.dimension_count" },
  72. { LLM_KV_ROPE_FREQ_BASE, "%s.rope.freq_base" },
  73. @@ -608,6 +612,7 @@ enum llm_tensor {
  74. LLM_TENSOR_ENC_OUTPUT_NORM,
  75. LLM_TENSOR_CLS,
  76. LLM_TENSOR_CLS_OUT,
  77. + LLM_TENSOR_BSKCN_TV,
  78. };
  79. static const std::map<llm_arch, std::map<llm_tensor, std::string>> LLM_TENSOR_NAMES = {
  80. @@ -1527,6 +1532,24 @@ static const std::map<llm_arch, std::map<llm_tensor, std::string>> LLM_TENSOR_NA
  81. { LLM_TENSOR_ATTN_K_NORM, "blk.%d.attn_k_norm" },
  82. },
  83. },
  84. + {
  85. + LLM_ARCH_SOLAR,
  86. + {
  87. + { LLM_TENSOR_TOKEN_EMBD, "token_embd" },
  88. + { LLM_TENSOR_OUTPUT_NORM, "output_norm" },
  89. + { LLM_TENSOR_OUTPUT, "output" },
  90. + { LLM_TENSOR_ATTN_NORM, "blk.%d.attn_norm" },
  91. + { LLM_TENSOR_ATTN_Q, "blk.%d.attn_q" },
  92. + { LLM_TENSOR_ATTN_K, "blk.%d.attn_k" },
  93. + { LLM_TENSOR_ATTN_V, "blk.%d.attn_v" },
  94. + { LLM_TENSOR_ATTN_OUT, "blk.%d.attn_output" },
  95. + { LLM_TENSOR_FFN_NORM, "blk.%d.ffn_norm" },
  96. + { LLM_TENSOR_FFN_GATE, "blk.%d.ffn_gate" },
  97. + { LLM_TENSOR_FFN_DOWN, "blk.%d.ffn_down" },
  98. + { LLM_TENSOR_FFN_UP, "blk.%d.ffn_up" },
  99. + { LLM_TENSOR_BSKCN_TV, "bskcn_tv" },
  100. + },
  101. + },
  102. {
  103. LLM_ARCH_UNKNOWN,
  104. {
  105. @@ -2360,6 +2383,7 @@ enum e_model {
  106. MODEL_15B,
  107. MODEL_16B,
  108. MODEL_20B,
  109. + MODEL_22B,
  110. MODEL_30B,
  111. MODEL_34B,
  112. MODEL_35B,
  113. @@ -2409,6 +2433,8 @@ struct llama_hparams {
  114. std::array<uint32_t, LLAMA_MAX_LAYERS> n_head_kv_arr;
  115. std::array<uint32_t, LLAMA_MAX_LAYERS> n_ff_arr;
  116. + std::array<std::array<uint32_t, LLAMA_MAX_LAYERS>, 4> n_bskcn_arr;
  117. +
  118. uint32_t n_layer_dense_lead = 0;
  119. uint32_t n_lora_q = 0;
  120. uint32_t n_lora_kv = 0;
  121. @@ -2479,6 +2505,7 @@ struct llama_hparams {
  122. if (this->n_head_arr != other.n_head_arr) return true;
  123. if (this->n_head_kv_arr != other.n_head_kv_arr) return true;
  124. if (this->n_ff_arr != other.n_ff_arr) return true;
  125. + if (this->n_bskcn_arr != other.n_bskcn_arr) return true;
  126. if (this->n_rel_attn_bkts != other.n_rel_attn_bkts) return true;
  127. if (this->n_layer_dense_lead != other.n_layer_dense_lead) return true;
  128. @@ -2588,6 +2615,14 @@ struct llama_hparams {
  129. return ssm_d_state * ssm_d_inner;
  130. }
  131. }
  132. +
  133. + bool n_bskcn(uint32_t n, uint32_t il = 0) const {
  134. + if (il < n_layer) {
  135. + return n_bskcn_arr[n][il] > 0;
  136. + }
  137. +
  138. + GGML_ABORT("fatal error");
  139. + }
  140. };
  141. static_assert(std::is_trivially_copyable<llama_hparams>::value, "llama_hparams must be trivially copyable");
  142. @@ -2769,6 +2804,8 @@ struct llama_layer {
  143. struct ggml_tensor * ffn_gate_scale;
  144. struct ggml_tensor * ffn_up_scale;
  145. struct ggml_tensor * ffn_down_scale;
  146. +
  147. + struct ggml_tensor * bskcn_tv;
  148. };
  149. // very similar to llama_batch,
  150. @@ -6134,6 +6171,21 @@ static void llm_load_hparams(
  151. default: model.type = e_model::MODEL_UNKNOWN;
  152. }
  153. } break;
  154. + case LLM_ARCH_SOLAR:
  155. + {
  156. + ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  157. +
  158. + for (int i = 0; i < hparams.n_bskcn_arr.max_size(); ++i) {
  159. + auto & bskcn = hparams.n_bskcn_arr.at(i);
  160. + bskcn.fill(0);
  161. + ml.get_key_or_arr(::format(LLM_KV_NAMES.at(LLM_KV_ATTENTION_BLOCK_SKIP_CONNECTION), LLM_ARCH_NAMES.at(ml.llm_kv.arch), i), bskcn, hparams.n_layer, false);
  162. + }
  163. +
  164. + switch (hparams.n_layer) {
  165. + case 64: model.type = e_model::MODEL_22B; break;
  166. + default: model.type = e_model::MODEL_UNKNOWN;
  167. + }
  168. + }
  169. default: (void)0;
  170. }
  171. @@ -8831,6 +8883,38 @@ static bool llm_load_tensors(
  172. layer.ffn_norm = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd});
  173. + layer.ffn_gate = ml.create_tensor(ctx_split, tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff});
  174. + layer.ffn_down = ml.create_tensor(ctx_split, tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd});
  175. + layer.ffn_up = ml.create_tensor(ctx_split, tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff});
  176. + }
  177. + } break;
  178. + case LLM_ARCH_SOLAR:
  179. + {
  180. + model.tok_embd = ml.create_tensor(ctx_input, tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab});
  181. +
  182. + // output
  183. + {
  184. + model.output_norm = ml.create_tensor(ctx_output, tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd});
  185. + model.output = ml.create_tensor(ctx_output_split, tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, llama_model_loader::TENSOR_NOT_REQUIRED);
  186. + }
  187. +
  188. + for (int i = 0; i < n_layer; ++i) {
  189. + ggml_context * ctx_layer = ctx_for_layer(i);
  190. + ggml_context * ctx_split = ctx_for_layer_split(i);
  191. +
  192. + auto & layer = model.layers[i];
  193. +
  194. + layer.attn_norm = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd});
  195. +
  196. + layer.wq = ml.create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head});
  197. + layer.wk = ml.create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa});
  198. + layer.wv = ml.create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa});
  199. + layer.wo = ml.create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd});
  200. +
  201. + layer.ffn_norm = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd});
  202. +
  203. + layer.bskcn_tv = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_BSKCN_TV, "weight"), {2}, llama_model_loader::TENSOR_NOT_REQUIRED | (i != 0 ? llama_model_loader::TENSOR_DUPLICATED : 0));
  204. +
  205. layer.ffn_gate = ml.create_tensor(ctx_split, tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff});
  206. layer.ffn_down = ml.create_tensor(ctx_split, tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd});
  207. layer.ffn_up = ml.create_tensor(ctx_split, tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff});
  208. @@ -16179,6 +16263,158 @@ struct llm_build_context {
  209. return gf;
  210. }
  211. +
  212. + ggml_cgraph * build_solar() {
  213. + struct ggml_cgraph * gf = ggml_new_graph_custom(ctx0, llama_model_max_nodes(model), false);
  214. +
  215. + // mutable variable, needed during the last layer of the computation to skip unused tokens
  216. + int32_t n_tokens = this->n_tokens;
  217. +
  218. + const int64_t n_embd_head = hparams.n_embd_head_v;
  219. + GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  220. + GGML_ASSERT(n_embd_head == hparams.n_rot);
  221. +
  222. + struct ggml_tensor * cur;
  223. + struct ggml_tensor * inpL;
  224. +
  225. + inpL = llm_build_inp_embd(ctx0, lctx, hparams, batch, model.tok_embd, cb);
  226. +
  227. + // inp_pos - contains the positions
  228. + struct ggml_tensor * inp_pos = build_inp_pos();
  229. +
  230. + // KQ_mask (mask for 1 head, it will be broadcasted to all heads)
  231. + struct ggml_tensor * KQ_mask = build_inp_KQ_mask();
  232. +
  233. + struct ggml_tensor * bskcn_1;
  234. + struct ggml_tensor * bskcn_2;
  235. +
  236. + for (int il = 0; il < n_layer; ++il) {
  237. + struct ggml_tensor * inpSA = inpL;
  238. +
  239. + if (hparams.n_bskcn(0, il)) {
  240. + bskcn_1 = inpSA;
  241. + }
  242. +
  243. + if (hparams.n_bskcn(1, il)) {
  244. + bskcn_2 = inpSA;
  245. + }
  246. +
  247. + if (hparams.n_bskcn(2, il)) {
  248. + inpSA = ggml_add(
  249. + ctx0,
  250. + ggml_mul(ctx0, bskcn_1, ggml_view_1d(ctx0, model.layers[il].bskcn_tv, 1, 0)),
  251. + ggml_mul(ctx0, inpSA, ggml_view_1d(ctx0, model.layers[il].bskcn_tv, 1, ggml_element_size(model.layers[il].bskcn_tv))));
  252. + }
  253. +
  254. + if (hparams.n_bskcn(3, il)) {
  255. + inpSA = ggml_add(
  256. + ctx0,
  257. + ggml_mul(ctx0, bskcn_2, ggml_view_1d(ctx0, model.layers[il].bskcn_tv, 1, 0)),
  258. + ggml_mul(ctx0, inpSA, ggml_view_1d(ctx0, model.layers[il].bskcn_tv, 1, ggml_element_size(model.layers[il].bskcn_tv))));
  259. + }
  260. +
  261. + // norm
  262. + cur = llm_build_norm(ctx0, inpL, hparams,
  263. + model.layers[il].attn_norm, NULL,
  264. + LLM_NORM_RMS, cb, il);
  265. + cb(cur, "attn_norm", il);
  266. +
  267. + // self-attention
  268. + {
  269. + // rope freq factors for llama3; may return nullptr for llama2 and other models
  270. + struct ggml_tensor * rope_factors = build_rope_factors(il);
  271. +
  272. + // compute Q and K and RoPE them
  273. + struct ggml_tensor * Qcur = llm_build_lora_mm(lctx, ctx0, model.layers[il].wq, cur);
  274. + cb(Qcur, "Qcur", il);
  275. + if (model.layers[il].bq) {
  276. + Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  277. + cb(Qcur, "Qcur", il);
  278. + }
  279. +
  280. + struct ggml_tensor * Kcur = llm_build_lora_mm(lctx, ctx0, model.layers[il].wk, cur);
  281. + cb(Kcur, "Kcur", il);
  282. + if (model.layers[il].bk) {
  283. + Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  284. + cb(Kcur, "Kcur", il);
  285. + }
  286. +
  287. + struct ggml_tensor * Vcur = llm_build_lora_mm(lctx, ctx0, model.layers[il].wv, cur);
  288. + cb(Vcur, "Vcur", il);
  289. + if (model.layers[il].bv) {
  290. + Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  291. + cb(Vcur, "Vcur", il);
  292. + }
  293. +
  294. + Qcur = ggml_rope_ext(
  295. + ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens), inp_pos, rope_factors,
  296. + n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  297. + ext_factor, attn_factor, beta_fast, beta_slow
  298. + );
  299. + cb(Qcur, "Qcur", il);
  300. +
  301. + Kcur = ggml_rope_ext(
  302. + ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens), inp_pos, rope_factors,
  303. + n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  304. + ext_factor, attn_factor, beta_fast, beta_slow
  305. + );
  306. + cb(Kcur, "Kcur", il);
  307. +
  308. + cur = llm_build_kv(ctx0, lctx, kv_self, gf,
  309. + model.layers[il].wo, model.layers[il].bo,
  310. + Kcur, Vcur, Qcur, KQ_mask, n_tokens, kv_head, n_kv, 1.0f/sqrtf(float(n_embd_head)), cb, il);
  311. + }
  312. +
  313. + if (il == n_layer - 1) {
  314. + // skip computing output for unused tokens
  315. + struct ggml_tensor * inp_out_ids = build_inp_out_ids();
  316. + n_tokens = n_outputs;
  317. + cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  318. + inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  319. + }
  320. +
  321. + struct ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  322. + cb(ffn_inp, "ffn_inp", il);
  323. +
  324. + // feed-forward network
  325. + cur = llm_build_norm(ctx0, ffn_inp, hparams,
  326. + model.layers[il].ffn_norm, NULL,
  327. + LLM_NORM_RMS, cb, il);
  328. + cb(cur, "ffn_norm", il);
  329. +
  330. + cur = llm_build_ffn(ctx0, lctx, cur,
  331. + model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  332. + model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  333. + model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  334. + NULL,
  335. + LLM_FFN_SILU, LLM_FFN_PAR, cb, il);
  336. + cb(cur, "ffn_out", il);
  337. +
  338. + cur = ggml_add(ctx0, cur, ffn_inp);
  339. + cb(cur, "ffn_out", il);
  340. +
  341. + cur = lctx.cvec.apply_to(ctx0, cur, il);
  342. + cb(cur, "l_out", il);
  343. +
  344. + // input for next layer
  345. + inpL = cur;
  346. + }
  347. +
  348. + cur = inpL;
  349. +
  350. + cur = llm_build_norm(ctx0, cur, hparams,
  351. + model.output_norm, NULL,
  352. + LLM_NORM_RMS, cb, -1);
  353. + cb(cur, "result_norm", -1);
  354. +
  355. + // lm_head
  356. + cur = llm_build_lora_mm(lctx, ctx0, model.output, cur);
  357. + cb(cur, "result_output", -1);
  358. +
  359. + ggml_build_forward_expand(gf, cur);
  360. +
  361. + return gf;
  362. + }
  363. };
  364. static struct ggml_cgraph * llama_build_graph_defrag(llama_context & lctx, const std::vector<uint32_t> & ids) {
  365. @@ -16443,6 +16679,10 @@ static struct ggml_cgraph * llama_build_graph(
  366. {
  367. result = llm.build_chameleon();
  368. } break;
  369. + case LLM_ARCH_SOLAR:
  370. + {
  371. + result = llm.build_solar();
  372. + } break;
  373. default:
  374. GGML_ABORT("fatal error");
  375. }
  376. @@ -19589,6 +19829,7 @@ enum llama_rope_type llama_rope_type(const struct llama_model * model) {
  377. case LLM_ARCH_GRANITE:
  378. case LLM_ARCH_GRANITE_MOE:
  379. case LLM_ARCH_CHAMELEON:
  380. + case LLM_ARCH_SOLAR:
  381. return LLAMA_ROPE_TYPE_NORM;
  382. // the pairs of head values are offset by n_rot/2