char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
465e49b9c
char/aliceai-t5-moe
1#include "models.h" 2 3void llama_model_llada::load_arch_hparams (llama_model_loader & ml ) { 4ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 5 6// LLaDA-8B has 32 layers, similar to LLaMA but for diffusion 7switch (hparams .n_layer ()) { 8case 32 : 9type = LLM_TYPE_8B ; 10break ; 11default : 12type = LLM_TYPE_UNKNOWN ; 13 } 14 15// Set non-causal attention for diffusion models 16hparams .causal_attn = false; 17} 18 19void llama_model_llada::load_arch_tensors (llama_model_loader & ) { 20LLAMA_LOAD_LOCALS ; 21 22tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 23 24// output 25output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 26output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 27 28// if output is NULL, init from the input tok embed 29if (output == NULL ) { 30output = 31create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 32 } 33 34for (int i = 0 ;i < n_layer ;++ i ) { 35auto & layer = layers [i ]; 36 37layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 38 39create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head ,n_embd_k_gqa ,n_embd_v_gqa ,0 ); 40layer .wo = 41create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_k * n_head ,n_embd },0 ); 42layer .wo_b = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"bias" ,i ), {n_embd },TENSOR_NOT_REQUIRED ); 43 44layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 45 46layer .rope_freqs = create_tensor (tn (LLM_TENSOR_ROPE_FREQS ,"weight" ,i ), {n_rot /2 }, 47TENSOR_NOT_REQUIRED | (i != 0 ?TENSOR_DUPLICATED :0 )); 48 49layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 50layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 51layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 52 53// optional MLP bias 54layer .ffn_gate_b = 55create_tensor (tn (LLM_TENSOR_FFN_GATE ,"bias" ,i ), {n_ff },TENSOR_NOT_REQUIRED ); 56layer .ffn_down_b = 57create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"bias" ,i ), {n_embd },TENSOR_NOT_REQUIRED ); 58layer .ffn_up_b = create_tensor (tn (LLM_TENSOR_FFN_UP ,"bias" ,i ), {n_ff },TENSOR_NOT_REQUIRED ); 59 } 60} 61 62std::unique_ptr < llm_graph_context > llama_model_llada::build_arch_graph (const llm_graph_params & params )const { 63return std::make_unique < graph > (* this ,params ); 64} 65 66llama_model_llada::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 67// LLaDA is similar to LLaMA but uses non-causal attention for diffusion 68const int64_t n_embd_head = hparams .n_embd_head_v (); 69 70GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 71GGML_ASSERT (n_embd_head == n_rot ); 72 73ggml_tensor * cur ; 74ggml_tensor * inpL ; 75 76inpL = build_inp_embd (model .tok_embd ); 77 78// inp_pos - contains the positions 79ggml_tensor * inp_pos = build_inp_pos (); 80 81// Non-causal attention for diffusion 82auto * inp_attn = build_attn_inp_no_cache (); 83 84ggml_tensor * inp_out_ids = build_inp_out_ids (); 85 86for (int il = 0 ;il < n_layer ;++ il ) { 87ggml_tensor * inpSA = inpL ; 88 89// norm 90cur = build_norm (inpL ,model .layers [il ].attn_norm ,NULL ,LLM_NORM_RMS ,il ); 91cb (cur ,"attn_norm" ,il ); 92 93// self-attention 94 { 95// compute separate Q, K, V projections without bias, matching LLaDALlamaBlock 96auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 97n_embd_head ,n_head ,n_head_kv ,il ); 98 99Qcur = ggml_rope_ext (ctx0 ,Qcur ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 100ext_factor ,attn_factor ,beta_fast ,beta_slow ); 101 102Kcur = ggml_rope_ext (ctx0 ,Kcur ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 103ext_factor ,attn_factor ,beta_fast ,beta_slow ); 104 105cb (Qcur ,"Qcur" ,il ); 106cb (Kcur ,"Kcur" ,il ); 107cb (Vcur ,"Vcur" ,il ); 108 109cur = build_attn (inp_attn , 110model .layers [il ].wo ,NULL ,model .layers [il ].wo_s , 111Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_embd_head )),il ); 112 } 113if (il == n_layer - 1 && inp_out_ids ) { 114cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 115inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 116 } 117ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 118cb (ffn_inp ,"ffn_inp" ,il ); 119 120// feed-forward network 121cur = build_norm (ffn_inp ,model .layers [il ].ffn_norm ,NULL ,LLM_NORM_RMS ,il ); 122cb (cur ,"ffn_norm" ,il ); 123 124cur = build_ffn (cur , 125model .layers [il ].ffn_up ,NULL ,NULL , 126model .layers [il ].ffn_gate ,NULL ,NULL , 127model .layers [il ].ffn_down ,NULL ,NULL , 128NULL ,LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 129cb (cur ,"ffn_out" ,il ); 130 131cur = ggml_add (ctx0 ,cur ,ffn_inp ); 132 133cur = build_cvec (cur ,il ); 134cb (cur ,"l_out" ,il ); 135 136// input for next layer 137inpL = cur ; 138 } 139cur = inpL ; 140 141cur = build_norm (cur ,model .output_norm ,NULL ,LLM_NORM_RMS ,-1 ); 142 143cb (cur ,"result_norm" ,-1 ); 144res -> t_embd = cur ; 145 146// lm_head 147cur = build_lora_mm (model .output ,cur ,model .output_s ); 148 149cb (cur ,"result_output" ,-1 ); 150res -> t_logits = cur ; 151 152ggml_build_forward_expand (gf ,cur ); 153}