char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
7acb4e8cd
char/aliceai-t5-moe
1#include "models.h" 2 3void llama_model_bloom::load_arch_hparams (llama_model_loader & ml ) { 4ml .get_key (LLM_KV_ATTENTION_LAYERNORM_EPS ,hparams .f_norm_eps ); 5 6switch (hparams .n_layer ()) { 7case 24 :type = LLM_TYPE_1B ;break ; 8case 30 : 9switch (hparams .n_embd ) { 10case 2560 :type = LLM_TYPE_3B ;break ; 11case 4096 :type = LLM_TYPE_7B ;break ; 12default :type = LLM_TYPE_UNKNOWN ; 13 }break ; 14default :type = LLM_TYPE_UNKNOWN ; 15 } 16 17// TODO: become GGUF KV parameter 18hparams .f_max_alibi_bias = 8.0f ; 19} 20 21void llama_model_bloom::load_arch_tensors (llama_model_loader & ) { 22LLAMA_LOAD_LOCALS ; 23 24tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 25tok_norm = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD_NORM ,"weight" ,0 ), {n_embd },0 ); 26tok_norm_b = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD_NORM ,"bias" ,0 ), {n_embd },0 ); 27 28// output 29output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 30output_norm_b = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"bias" ), {n_embd },0 ); 31output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 32 33// if output is NULL, init from the input tok embed 34if (output == NULL ) { 35output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 36 } 37 38for (int i = 0 ;i < n_layer ;++ i ) { 39auto & layer = layers [i ]; 40 41layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 42layer .attn_norm_b = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"bias" ,i ), {n_embd },0 ); 43 44layer .wqkv = create_tensor (tn (LLM_TENSOR_ATTN_QKV ,"weight" ,i ), {n_embd ,n_embd + 2 * n_embd_gqa },0 ); 45layer .wqkv_b = create_tensor (tn (LLM_TENSOR_ATTN_QKV ,"bias" ,i ), {n_embd + 2 * n_embd_gqa },0 ); 46 47layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd ,n_embd },0 ); 48layer .wo_b = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"bias" ,i ), {n_embd },0 ); 49 50layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 51layer .ffn_norm_b = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"bias" ,i ), {n_embd },0 ); 52 53layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 54layer .ffn_down_b = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"bias" ,i ), {n_embd },0 ); 55 56layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 57layer .ffn_up_b = create_tensor (tn (LLM_TENSOR_FFN_UP ,"bias" ,i ), {n_ff },0 ); 58 } 59} 60 61std::unique_ptr < llm_graph_context > llama_model_bloom::build_arch_graph (const llm_graph_params & params )const { 62return std::make_unique < graph > (* this ,params ); 63} 64 65llama_model_bloom::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 66const int64_t n_embd_head = hparams .n_embd_head_v (); 67 68GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 69 70ggml_tensor * cur ; 71ggml_tensor * inpL ; 72 73inpL = build_inp_embd (model .tok_embd ); 74 75auto * inp_attn = build_attn_inp_kv (); 76 77inpL = build_norm (inpL , 78model .tok_norm , 79model .tok_norm_b , 80LLM_NORM ,0 ); 81cb (inpL ,"inp_norm" ,0 ); 82 83ggml_tensor * inp_out_ids = build_inp_out_ids (); 84 85for (int il = 0 ;il < n_layer ;++ il ) { 86cur = build_norm (inpL , 87model .layers [il ].attn_norm , 88model .layers [il ].attn_norm_b , 89LLM_NORM ,il ); 90cb (cur ,"attn_norm" ,il ); 91 92// self-attention 93 { 94auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 95n_embd_head ,n_head ,n_head_kv ,il ); 96 97cur = build_attn (inp_attn , 98model .layers [il ].wo ,model .layers [il ].wo_b ,model .layers [il ].wo_s , 99Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_embd_head )),il ); 100 } 101 102if (il == n_layer - 1 && inp_out_ids ) { 103cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 104inpL = ggml_get_rows (ctx0 ,inpL ,inp_out_ids ); 105 } 106 107// Add the input 108ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpL ); 109cb (ffn_inp ,"ffn_inp" ,il ); 110 111// FF 112 { 113cur = build_norm (ffn_inp , 114model .layers [il ].ffn_norm , 115model .layers [il ].ffn_norm_b , 116LLM_NORM ,il ); 117cb (cur ,"ffn_norm" ,il ); 118 119cur = build_ffn (cur , 120model .layers [il ].ffn_up ,model .layers [il ].ffn_up_b ,NULL , 121NULL ,NULL ,NULL , 122model .layers [il ].ffn_down ,model .layers [il ].ffn_down_b ,NULL , 123NULL , 124LLM_FFN_GELU ,LLM_FFN_SEQ ,il ); 125cb (cur ,"ffn_out" ,il ); 126 } 127 128cur = ggml_add (ctx0 ,cur ,ffn_inp ); 129 130cur = build_cvec (cur ,il ); 131cb (cur ,"l_out" ,il ); 132 133// input for next layer 134inpL = cur ; 135 } 136 137cur = build_norm (inpL , 138model .output_norm , 139model .output_norm_b , 140LLM_NORM ,-1 ); 141 142cb (cur ,"result_norm" ,-1 ); 143res -> t_embd = cur ; 144 145cur = build_lora_mm (model .output ,cur ,model .output_s ); 146 147cb (cur ,"result_output" ,-1 ); 148res -> t_logits = cur ; 149 150ggml_build_forward_expand (gf ,cur ); 151}