okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b2de2641f
main
1#include "models.h" 2 3#include "llama-memory-recurrent.h" 4 5#include <algorithm> 6 7llm_build_mamba_base::llm_build_mamba_base (const llm_graph_params & params ) :llm_graph_context (params ) {} 8 9ggml_tensor * llm_build_mamba_base::build_mamba_layer (llm_graph_input_rs * inp , 10ggml_tensor * cur , 11const llama_model & model , 12const llama_ubatch & ubatch , 13int il ) { 14const auto * mctx_cur = inp -> mctx ; 15 16const auto kv_head = mctx_cur -> get_head (); 17 18const auto & layer = model .layers [il ]; 19 20const int64_t d_conv = hparams .ssm_d_conv ; 21const int64_t d_inner = hparams .ssm_d_inner ; 22const int64_t d_state = hparams .ssm_d_state ; 23const int64_t dt_rank = hparams .ssm_dt_rank ; 24const int64_t n_head = d_inner ; 25const int64_t head_dim = 1 ; 26const int64_t n_seqs = ubatch .n_seqs ; 27// Some variants of Mamba arch (e.g. FalconMamba do apply layer norm on B and Dt layers) 28const bool ssm_dt_b_c_rms = hparams .ssm_dt_b_c_rms ; 29 30const int64_t n_seq_tokens = ubatch .n_seq_tokens ; 31 32GGML_ASSERT (n_seqs != 0 ); 33GGML_ASSERT (ubatch .equal_seqs ()); 34GGML_ASSERT (ubatch .n_tokens == n_seq_tokens * n_seqs ); 35GGML_ASSERT (d_inner %n_head == 0 ); 36 37ggml_tensor * conv_states_all = mctx_cur -> get_r_l (il ); 38ggml_tensor * ssm_states_all = mctx_cur -> get_s_l (il ); 39 40ggml_tensor * conv = build_rs (inp ,conv_states_all ,hparams .n_embd_r (),n_seqs ); 41conv = ggml_reshape_3d (ctx0 ,conv ,d_conv - 1 ,d_inner ,n_seqs ); 42 43// {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs} 44cur = ggml_reshape_3d (ctx0 ,cur ,cur -> ne [0 ],n_seq_tokens ,n_seqs ); 45 46// {n_embd, 2*d_inner} @ {n_embd, n_seq_tokens, n_seqs} => {2*d_inner, n_seq_tokens, n_seqs} 47ggml_tensor * xz = build_lora_mm (layer .ssm_in ,cur ,layer .ssm_in_s ); 48// split the above in two 49// => {d_inner, n_seq_tokens, n_seqs} 50ggml_tensor * x = ggml_view_3d (ctx0 ,xz ,d_inner ,xz -> ne [1 ],xz -> ne [2 ],xz -> nb [1 ],xz -> nb [2 ],0 ); 51ggml_tensor * z = 52ggml_view_3d (ctx0 ,xz ,d_inner ,xz -> ne [1 ],xz -> ne [2 ],xz -> nb [1 ],xz -> nb [2 ],d_inner * ggml_element_size (xz )); 53 54// conv 55 { 56// => {d_conv - 1 + n_seq_tokens, d_inner, n_seqs} 57ggml_tensor * conv_x = ggml_concat (ctx0 ,conv ,ggml_transpose (ctx0 ,x ),0 ); 58 59// copy last (d_conv - 1) columns back into the state cache 60ggml_tensor * last_conv = ggml_view_3d (ctx0 ,conv_x ,d_conv - 1 ,d_inner ,n_seqs ,conv_x -> nb [1 ],conv_x -> nb [2 ], 61n_seq_tokens * (conv_x -> nb [0 ])); 62 63ggml_build_forward_expand ( 64gf ,ggml_cpy (ctx0 ,last_conv , 65ggml_view_1d (ctx0 ,conv_states_all , (d_conv - 1 )* (d_inner )* (n_seqs ), 66kv_head * (d_conv - 1 )* (d_inner )* ggml_element_size (conv_states_all )))); 67 68// 1D convolution 69// The equivalent is to make a self-overlapping view of conv_x 70// over d_conv columns at each stride in the 3rd dimension, 71// then element-wise multiply that with the conv1d weight, 72// then sum the elements of each row, 73// (the last two steps are a dot product over rows (also doable with mul_mat)) 74// then permute away the ne[0] dimension, 75// and then you're left with the resulting x tensor. 76// For simultaneous sequences, all sequences need to have the same length. 77x = ggml_ssm_conv (ctx0 ,conv_x ,layer .ssm_conv1d ); 78 79// bias 80x = ggml_add (ctx0 ,x ,layer .ssm_conv1d_b ); 81 82x = ggml_silu (ctx0 ,x ); 83 } 84 85// ssm 86 { 87// {d_inner, dt_rank + 2*d_state} @ {d_inner, n_seq_tokens, n_seqs} => {dt_rank + 2*d_state, n_seq_tokens, n_seqs} 88ggml_tensor * x_db = build_lora_mm (layer .ssm_x ,x ); 89// split 90ggml_tensor * dt = ggml_view_3d (ctx0 ,x_db ,dt_rank ,n_seq_tokens ,n_seqs ,x_db -> nb [1 ],x_db -> nb [2 ],0 ); 91ggml_tensor * B = 92ggml_view_4d (ctx0 ,x_db ,d_state ,/* n_group */ 1 ,n_seq_tokens ,n_seqs ,d_state * x_db -> nb [0 ],x_db -> nb [1 ], 93x_db -> nb [2 ],ggml_element_size (x_db )* dt_rank ); 94ggml_tensor * C = 95ggml_view_4d (ctx0 ,x_db ,d_state ,/* n_group */ 1 ,n_seq_tokens ,n_seqs ,d_state * x_db -> nb [0 ],x_db -> nb [1 ], 96x_db -> nb [2 ],ggml_element_size (x_db )* (dt_rank + d_state )); 97 98// Some Mamba variants (e.g. FalconMamba, Jamba) apply RMS norm in B, C & Dt layers 99if (ssm_dt_b_c_rms || (layer .ssm_dt_norm && layer .ssm_b_norm && layer .ssm_c_norm )) { 100dt = build_norm (dt ,layer .ssm_dt_norm ,NULL ,LLM_NORM_RMS ,il ); 101B = build_norm (B ,layer .ssm_b_norm ,NULL ,LLM_NORM_RMS ,il ); 102C = build_norm (C ,layer .ssm_c_norm ,NULL ,LLM_NORM_RMS ,il ); 103 }else { 104dt = ggml_cont (ctx0 ,dt ); 105 } 106 107// {dt_rank, d_inner} @ {dt_rank, n_seq_tokens, n_seqs} => {d_inner, n_seq_tokens, n_seqs} 108dt = build_lora_mm (layer .ssm_dt ,dt ); 109dt = ggml_add (ctx0 ,dt ,layer .ssm_dt_b ); 110 111cur = x ; 112x = ggml_reshape_4d (ctx0 ,x ,head_dim ,n_head ,n_seq_tokens ,n_seqs ); 113 114ggml_tensor * A = layer .ssm_a ; 115 116// use the states and the indices provided by build_recurrent_state 117// (this is necessary in order to properly use the states before they are overwritten, 118// while avoiding to make unnecessary copies of the states) 119auto get_ssm_rows = [& ](ggml_context * ctx ,ggml_tensor * states ,ggml_tensor * ids ) { 120ggml_tensor * ssm = ggml_reshape_4d (ctx ,states ,d_state ,head_dim ,n_head ,mctx_cur -> get_size ()); 121 122// Custom operator to optimize the parallel associative scan 123// as described in the Annex D of the Mamba paper. 124// => {d_inner, n_seq_tokens, n_seqs} and {d_state, d_inner, n_seqs} 125return ggml_ssm_scan (ctx ,ssm ,x ,dt ,A ,B ,C ,ids ,/*K=*/ 1 ); 126 }; 127 128ggml_tensor * y_ssm = build_rs (inp ,ssm_states_all ,hparams .n_embd_s (),ubatch .n_seqs ,get_ssm_rows ); 129 130// store last states 131ggml_build_forward_expand ( 132gf ,ggml_cpy (ctx0 ,ggml_view_1d (ctx0 ,y_ssm ,d_state * d_inner * n_seqs ,x -> nb [3 ]* x -> ne [3 ]), 133ggml_view_1d (ctx0 ,ssm_states_all ,d_state * d_inner * n_seqs , 134kv_head * d_state * d_inner * ggml_element_size (ssm_states_all )))); 135 136ggml_tensor * y = ggml_view_3d (ctx0 ,y_ssm ,d_inner ,n_seq_tokens ,n_seqs ,x -> nb [2 ],x -> nb [3 ],0 ); 137 138// TODO: skip computing output earlier for unused tokens 139 140y = ggml_add (ctx0 ,y ,ggml_mul (ctx0 ,cur ,layer .ssm_d )); 141y = ggml_swiglu_split (ctx0 ,ggml_cont (ctx0 ,z ),y ); 142 143// {d_inner, n_embd} @ {d_inner, n_seq_tokens, n_seqs} => {n_embd, n_seq_tokens, n_seqs} 144cur = build_lora_mm (layer .ssm_out ,y ,layer .ssm_out_s ); 145 } 146 147// {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens} 148cur = ggml_reshape_2d (ctx0 ,cur ,cur -> ne [0 ],n_seq_tokens * n_seqs ); 149 150return cur ; 151} 152 153ggml_tensor * llm_build_mamba_base::build_mamba2_layer (llm_graph_input_rs * inp , 154ggml_tensor * cur , 155const llama_model & model , 156const llama_ubatch & ubatch , 157int il )const { 158const auto * mctx_cur = inp -> mctx ; 159 160const auto kv_head = mctx_cur -> get_head (); 161const auto mem_size = mctx_cur -> get_size (); 162 163const int64_t d_conv = hparams .ssm_d_conv ; 164const int64_t d_inner = hparams .ssm_d_inner ; 165const int64_t d_state = hparams .ssm_d_state ; 166const int64_t n_head = hparams .ssm_dt_rank ; 167const int64_t head_dim = d_inner /n_head ; 168const int64_t n_group = hparams .ssm_n_group ; 169const int64_t n_seqs = ubatch .n_seqs ; 170 171const int64_t n_seq_tokens = ubatch .n_seq_tokens ; 172const int64_t K = cparams .n_rs_seq > 0 ? (int64_t )cparams .n_rs_seq + 1 :1 ; 173 174GGML_ASSERT (n_seqs != 0 ); 175GGML_ASSERT (ubatch .equal_seqs ()); 176GGML_ASSERT (ubatch .n_tokens == n_seq_tokens * n_seqs ); 177GGML_ASSERT (d_inner %n_head == 0 ); 178GGML_ASSERT (d_inner %n_group == 0 ); 179 180ggml_tensor * conv_states_all = mctx_cur -> get_r_l (il ); 181ggml_tensor * ssm_states_all = mctx_cur -> get_s_l (il ); 182const int64_t state_slots = ssm_states_all -> ne [1 ]; 183 184ggml_tensor * conv = build_rs (inp ,conv_states_all ,hparams .n_embd_r (),n_seqs ); 185conv = ggml_reshape_3d (ctx0 ,conv ,d_conv - 1 ,d_inner + 2 * n_group * d_state ,n_seqs ); 186 187// d_in_proj = 2 * self.d_inner + 2 * self.ngroups * self.d_state + self.nheads 188 189// Keep the projection 2D: with a {n_embd, 1, n_seqs} batch the CUDA backend 190// dispatches a column-batched GEMV for what is a large dense GEMM. 191// {n_embd, d_in_proj} @ {n_embd, n_tokens} => {d_in_proj, n_tokens} 192ggml_tensor * zxBCdt = build_lora_mm (model .layers [il ].ssm_in ,cur ,model .layers [il ].ssm_in_s ); 193// {d_in_proj, n_tokens} => {d_in_proj, n_seq_tokens, n_seqs} 194zxBCdt = ggml_reshape_3d (ctx0 ,zxBCdt ,zxBCdt -> ne [0 ],n_seq_tokens ,n_seqs ); 195 196// split the above in three 197ggml_tensor * z = ggml_view_4d (ctx0 ,zxBCdt ,head_dim ,n_head ,n_seq_tokens ,n_seqs ,head_dim * zxBCdt -> nb [0 ], 198zxBCdt -> nb [1 ],zxBCdt -> nb [2 ],0 ); 199ggml_tensor * xBC = ggml_view_3d (ctx0 ,zxBCdt ,d_inner + 2 * n_group * d_state ,n_seq_tokens ,n_seqs ,zxBCdt -> nb [1 ], 200zxBCdt -> nb [2 ],d_inner * ggml_element_size (zxBCdt )); 201ggml_tensor * dt = ggml_view_3d (ctx0 ,zxBCdt ,n_head ,n_seq_tokens ,n_seqs ,zxBCdt -> nb [1 ],zxBCdt -> nb [2 ], 202 (2 * d_inner + 2 * n_group * d_state )* ggml_element_size (zxBCdt )); 203 204// conv 205 { 206// => {d_conv - 1 + n_seq_tokens, d_inner + 2*n_group*d_state, n_seqs} 207ggml_tensor * conv_x = ggml_concat (ctx0 ,conv ,ggml_transpose (ctx0 ,xBC ),0 ); 208 209const int64_t row_count = (d_conv - 1 )* (d_inner + 2 * n_group * d_state ); 210const size_t row_size = ggml_row_size (conv_states_all -> type ,row_count ); 211const int64_t n_written = std::min < int64_t > (n_seq_tokens ,K ); 212 213for (int64_t slot = 0 ;slot < n_written ;++ slot ) { 214ggml_tensor * last_conv = ggml_view_3d (ctx0 ,conv_x ,d_conv - 1 ,d_inner + 2 * n_group * d_state ,n_seqs , 215conv_x -> nb [1 ],conv_x -> nb [2 ], (n_seq_tokens - slot )* conv_x -> nb [0 ]); 216 217ggml_build_forward_expand (gf ,ggml_cpy (ctx0 ,last_conv , 218ggml_view_2d (ctx0 ,conv_states_all ,row_count ,n_seqs , 219conv_states_all -> nb [1 ], 220 ((size_t )slot * mem_size + kv_head )* row_size ))); 221 } 222 223// 1D convolution 224// The equivalent is to make a self-overlapping view of conv_x 225// over d_conv columns at each stride in the 3rd dimension, 226// then element-wise multiply that with the conv1d weight, 227// then sum the elements of each row, 228// (the last two steps are a dot product over rows (also doable with mul_mat)) 229// then permute away the ne[0] dimension, 230// and then you're left with the resulting x tensor. 231// For simultaneous sequences, all sequences need to have the same length. 232xBC = ggml_ssm_conv (ctx0 ,conv_x ,model .layers [il ].ssm_conv1d ); 233 234// bias 235xBC = ggml_add (ctx0 ,xBC ,model .layers [il ].ssm_conv1d_b ); 236 237xBC = ggml_silu (ctx0 ,xBC ); 238 } 239 240// ssm 241 { 242// These correspond to V K Q in SSM/attention duality 243ggml_tensor * x = ggml_view_4d (ctx0 ,xBC ,head_dim ,n_head ,n_seq_tokens ,n_seqs ,head_dim * xBC -> nb [0 ], 244xBC -> nb [1 ],xBC -> nb [2 ],0 ); 245ggml_tensor * B = ggml_view_4d (ctx0 ,xBC ,d_state ,n_group ,n_seq_tokens ,n_seqs ,d_state * xBC -> nb [0 ], 246xBC -> nb [1 ],xBC -> nb [2 ],d_inner * ggml_element_size (xBC )); 247ggml_tensor * C = ggml_view_4d (ctx0 ,xBC ,d_state ,n_group ,n_seq_tokens ,n_seqs ,d_state * xBC -> nb [0 ], 248xBC -> nb [1 ],xBC -> nb [2 ], (d_inner + n_group * d_state )* ggml_element_size (xBC )); 249 250// {n_head, n_seq_tokens, n_seqs} 251dt = ggml_add (ctx0 ,ggml_cont (ctx0 ,dt ),model .layers [il ].ssm_dt_b ); 252 253ggml_tensor * A = model .layers [il ].ssm_a ; 254 255// use the states and the indices provided by build_recurrent_state 256// (this is necessary in order to properly use the states before they are overwritten, 257// while avoiding to make unnecessary copies of the states) 258auto get_ssm_rows = [& ](ggml_context * ctx ,ggml_tensor * states ,ggml_tensor * ids ) { 259ggml_tensor * ssm = ggml_reshape_4d (ctx ,states ,d_state ,head_dim ,n_head ,state_slots ); 260 261// TODO: use semistructured matrices to implement state-space duality 262// => {d_inner, n_seq_tokens, n_seqs} and {d_state, d_inner, n_seqs} 263// K > 1 asks the backend to return rollback snapshots in addition to the final state. 264return ggml_ssm_scan (ctx ,ssm ,x ,dt ,A ,B ,C ,ids ,K ); 265 }; 266 267ggml_tensor * y_ssm = build_rs (inp ,ssm_states_all ,hparams .n_embd_s (),ubatch .n_seqs ,get_ssm_rows ); 268const int64_t D = d_state * d_inner ; 269const int64_t n_written = std::min < int64_t > (n_seq_tokens ,K ); 270const size_t row_size = ggml_row_size (ssm_states_all -> type ,D ); 271const size_t y_row_size = ggml_row_size (y_ssm -> type ,D ); 272const size_t state_offset = ggml_nelements (x )* ggml_element_size (x ); 273 274ggml_build_forward_expand ( 275gf ,ggml_cpy (ctx0 , 276ggml_view_3d (ctx0 ,y_ssm ,D ,n_seqs ,n_written , 277y_row_size ,y_row_size * n_seqs ,state_offset ), 278ggml_view_3d (ctx0 ,ssm_states_all ,D ,n_seqs ,n_written , 279ssm_states_all -> nb [1 ], (size_t )mem_size * row_size ,kv_head * row_size ))); 280 281ggml_tensor * y = ggml_view_4d (ctx0 ,y_ssm ,head_dim ,n_head ,n_seq_tokens ,n_seqs ,x -> nb [1 ],n_head * x -> nb [1 ], 282n_seq_tokens * n_head * x -> nb [1 ],0 ); 283 284// TODO: skip computing output earlier for unused tokens 285 286y = ggml_add (ctx0 ,y ,ggml_mul (ctx0 ,x ,model .layers [il ].ssm_d )); 287cb (y ,"mamba2_y_add_d" ,il ); 288y = ggml_swiglu_split (ctx0 ,ggml_cont (ctx0 ,z ),y ); 289 290// grouped RMS norm 291if (model .layers [il ].ssm_norm ) { 292y = ggml_reshape_4d (ctx0 ,y ,d_inner /n_group ,n_group ,n_seq_tokens ,n_seqs ); 293y = build_norm (y ,model .layers [il ].ssm_norm ,NULL ,LLM_NORM_RMS ,il ); 294 } 295 296y = ggml_reshape_2d (ctx0 ,y ,d_inner ,n_seq_tokens * n_seqs ); 297 298// {d_inner, n_embd} @ {d_inner, n_tokens} => {n_embd, n_tokens} 299cur = build_lora_mm (model .layers [il ].ssm_out ,y ,model .layers [il ].ssm_out_s ); 300 } 301 302cb (cur ,"mamba_out" ,il ); 303return cur ; 304}