char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
90e0f5cfc
char/aliceai-t5-moe
1#include "models.h" 2 3#include "llama-impl.h" 4#include "llama-memory-recurrent.h" 5 6// utility to get one slice from the third dimension 7// input dim: [x, y, c, b] 8// output dim: [x, y, 1, b] 9static ggml_tensor * get_slice_2d (ggml_context * ctx0 ,ggml_tensor * t ,int64_t c ) { 10return ggml_view_4d (ctx0 ,t ,t -> ne [0 ],t -> ne [1 ],1 ,t -> ne [3 ], 11t -> nb [1 ],t -> nb [2 ],t -> nb [3 ],t -> nb [2 ]* c ); 12} 13 14llm_build_delta_net_base::llm_build_delta_net_base (const llm_graph_params & params ) :llm_graph_context (params ) {} 15 16std::pair < ggml_tensor * ,ggml_tensor *> llm_build_delta_net_base::build_delta_net_chunking ( 17ggml_tensor * q , 18ggml_tensor * k , 19ggml_tensor * v , 20ggml_tensor * g , 21ggml_tensor * b , 22ggml_tensor * s , 23int il ) { 24const int64_t S_k = q -> ne [0 ]; 25const int64_t H_k = q -> ne [1 ]; 26const int64_t n_tokens = q -> ne [2 ]; 27const int64_t n_seqs = q -> ne [3 ]; 28 29const int64_t S_v = v -> ne [0 ]; 30const int64_t H_v = v -> ne [1 ]; 31const bool kda = (g -> ne [0 ]== S_k && g -> ne [1 ]== H_k ); 32 33GGML_ASSERT (S_k == S_v ); 34GGML_ASSERT (H_v %H_k == 0 ); 35 36GGML_ASSERT (q -> ne [0 ]== S_k && q -> ne [1 ]== H_k && q -> ne [2 ]== n_tokens && q -> ne [3 ]== n_seqs ); 37GGML_ASSERT (k -> ne [0 ]== S_k && k -> ne [1 ]== H_k && k -> ne [2 ]== n_tokens && k -> ne [3 ]== n_seqs ); 38GGML_ASSERT (v -> ne [0 ]== S_v && v -> ne [1 ]== H_v && v -> ne [2 ]== n_tokens && v -> ne [3 ]== n_seqs ); 39 40GGML_ASSERT (g -> ne [0 ]== 1 || g -> ne [0 ]== S_v ); 41GGML_ASSERT (g -> ne [1 ]== H_v && g -> ne [2 ]== n_tokens && g -> ne [3 ]== n_seqs ); 42GGML_ASSERT (b -> ne [0 ]== 1 && b -> ne [1 ]== H_v && b -> ne [2 ]== n_tokens && b -> ne [3 ]== n_seqs ); 43GGML_ASSERT (s -> ne [0 ]== S_v && s -> ne [1 ]== S_v && s -> ne [2 ]== H_v && s -> ne [3 ]== n_seqs ); 44 45const float scale = 1.0f /sqrtf (S_k ); 46 47q = ggml_scale (ctx0 ,q ,scale ); 48 49cb (q ,"q_in" ,il ); 50cb (k ,"k_in" ,il ); 51cb (v ,"v_in" ,il ); 52cb (b ,"b_in" ,il ); 53cb (g ,"g_in" ,il ); 54 55q = ggml_permute (ctx0 ,q ,0 ,2 ,1 ,3 );// [S_k, n_tokens, H_k, n_seqs] 56k = ggml_permute (ctx0 ,k ,0 ,2 ,1 ,3 );// [S_k, n_tokens, H_k, n_seqs] 57v = ggml_permute (ctx0 ,v ,0 ,2 ,1 ,3 );// [S_v, n_tokens, H_v, n_seqs] 58g = ggml_permute (ctx0 ,g ,0 ,2 ,1 ,3 );// [g_0, n_tokens, H_v, n_seqs] 59b = ggml_permute (ctx0 ,b ,0 ,2 ,1 ,3 );// [ 1, n_tokens, H_v, n_seqs] 60 61const int CS = kda ?16 :64 ;// chunk size 62 63const int pad = (CS - n_tokens %CS ) %CS ; 64const int n_chunks = (n_tokens + pad ) /CS ; 65 66q = ggml_pad (ctx0 ,q ,0 ,pad ,0 ,0 ); 67k = ggml_pad (ctx0 ,k ,0 ,pad ,0 ,0 ); 68v = ggml_pad (ctx0 ,v ,0 ,pad ,0 ,0 ); 69g = ggml_pad (ctx0 ,g ,0 ,pad ,0 ,0 ); 70b = ggml_pad (ctx0 ,b ,0 ,pad ,0 ,0 ); 71 72ggml_tensor * v_b = ggml_mul (ctx0 ,v ,b ); 73ggml_tensor * k_b = ggml_mul (ctx0 ,k ,b ); 74 75cb (v_b ,"v_b" ,il ); 76cb (k_b ,"k_b" ,il ); 77 78q = ggml_reshape_4d (ctx0 ,q ,S_k ,CS ,n_chunks ,H_k * n_seqs ); 79k = ggml_reshape_4d (ctx0 ,k ,S_k ,CS ,n_chunks ,H_k * n_seqs ); 80k_b = ggml_reshape_4d (ctx0 ,k_b ,S_k ,CS ,n_chunks ,H_v * n_seqs ); 81v = ggml_reshape_4d (ctx0 ,v ,S_v ,CS ,n_chunks ,H_v * n_seqs ); 82v_b = ggml_reshape_4d (ctx0 ,v_b ,S_v ,CS ,n_chunks ,H_v * n_seqs ); 83 84g = ggml_reshape_4d (ctx0 ,g ,g -> ne [0 ],CS ,n_chunks ,H_v * n_seqs ); 85b = ggml_reshape_4d (ctx0 ,b ,1 ,CS ,n_chunks ,H_v * n_seqs ); 86 87// [CS, g_0, n_chunks, H_v * n_seqs] 88// TODO: extend ggml_cumsum with axis parameter to avoid transpose 89ggml_tensor * g_cs = ggml_cumsum (ctx0 ,ggml_cont (ctx0 ,ggml_transpose (ctx0 ,g ))); 90cb (g_cs ,"g_cs" ,il ); 91 92ggml_tensor * kb = nullptr ; 93ggml_tensor * kq = nullptr ; 94if (kda ) { 95const int64_t CHB = n_chunks * H_k * n_seqs ; 96 97ggml_tensor * g_cs_i = ggml_reshape_4d (ctx0 ,g_cs ,CS ,1 ,S_k ,CHB );// [chunk_size, 1, S_k, CHB] 98ggml_tensor * g_cs_j = ggml_reshape_4d (ctx0 ,g_cs ,1 ,CS ,S_k ,CHB );// [1, chunk_size, S_k, CHB] 99 100g_cs_j = ggml_repeat_4d (ctx0 ,g_cs_j ,CS ,CS ,S_k ,CHB );// [1, chunk_size, S_k, CHB] -> [chunk_size, chunk_size, S_k, CHB] 101 102// decay_mask [chunk_size,chunk_size,S_k,CHB] 103ggml_tensor * decay_mask ; 104decay_mask = ggml_sub (ctx0 ,g_cs_j ,g_cs_i ); 105decay_mask = ggml_tri (ctx0 ,decay_mask ,GGML_TRI_TYPE_LOWER_DIAG ); 106decay_mask = ggml_exp (ctx0 ,decay_mask ); 107cb (decay_mask ,"decay_mask" ,il ); 108 109// decay_mask [S_k,BT_j,BT_i,CHB] *Note* second and third chunk_sizes are switched 110decay_mask = ggml_cont_4d (ctx0 ,ggml_permute (ctx0 ,decay_mask ,2 ,1 ,0 ,3 ),S_k ,CS ,CS ,CHB ); 111 112ggml_tensor * k_b_i = ggml_reshape_4d (ctx0 ,k_b ,S_k ,CS ,1 ,CHB ); 113ggml_tensor * k_j = ggml_reshape_4d (ctx0 ,k ,S_k ,1 ,CS ,CHB ); 114ggml_tensor * q_i = ggml_reshape_4d (ctx0 ,q ,S_k ,CS ,1 ,CHB ); 115 116ggml_tensor * decay_k_b_i = ggml_mul (ctx0 ,decay_mask ,k_b_i ); 117ggml_tensor * decay_q_i = ggml_mul (ctx0 ,decay_mask ,q_i ); 118 119// decay_k_b_i [S,BT,BT,CHB] @ k_j [S,1,BT,CHB] = Akk [BT,1,BT,CHB] 120kb = ggml_mul_mat (ctx0 ,decay_k_b_i ,k_j ); 121kq = ggml_mul_mat (ctx0 ,decay_q_i ,k_j ); 122 123kb = ggml_cont (ctx0 ,ggml_transpose (ctx0 ,ggml_reshape_4d (ctx0 ,kb ,CS ,CS ,n_chunks ,H_v * n_seqs ))); 124kq = ggml_cont (ctx0 ,ggml_transpose (ctx0 ,ggml_reshape_4d (ctx0 ,kq ,CS ,CS ,n_chunks ,H_v * n_seqs ))); 125 }else { 126ggml_tensor * g_cs_i = g_cs ; 127ggml_tensor * g_cs_j = ggml_reshape_4d (ctx0 ,g_cs ,1 ,CS ,n_chunks ,H_v * n_seqs ); 128 129g_cs_j = ggml_repeat_4d (ctx0 ,g_cs_j ,CS ,CS ,n_chunks ,H_v * n_seqs ); 130 131// [CS, CS, n_chunks, H_v * n_seqs] 132ggml_tensor * decay_mask ; 133decay_mask = ggml_sub (ctx0 ,g_cs_j ,g_cs_i ); 134decay_mask = ggml_tri (ctx0 ,decay_mask ,GGML_TRI_TYPE_LOWER_DIAG ); 135decay_mask = ggml_exp (ctx0 ,decay_mask ); 136cb (decay_mask ,"decay_mask" ,il ); 137 138// [CS, CS, n_chunks, H_k * n_seqs] 139kb = ggml_mul_mat (ctx0 ,k ,k_b ); 140kb = ggml_mul (ctx0 ,kb ,decay_mask ); 141 142// [CS, CS, n_chunks, H_k * n_seqs] 143kq = ggml_mul_mat (ctx0 ,k ,q ); 144kq = ggml_mul (ctx0 ,kq ,decay_mask ); 145 } 146 147kq = ggml_tri (ctx0 ,kq ,GGML_TRI_TYPE_LOWER_DIAG ); 148cb (kq ,"kq" ,il ); 149 150// [CS, CS, n_chunks, H_k * n_seqs] 151ggml_tensor * attn ; 152attn = ggml_tri (ctx0 ,kb ,GGML_TRI_TYPE_LOWER ); 153cb (attn ,"attn" ,il ); 154 155ggml_tensor * identity ; 156identity = ggml_view_1d (ctx0 ,attn ,CS ,0 ); 157identity = ggml_fill (ctx0 ,identity ,1.0f ); 158identity = ggml_diag (ctx0 ,identity ); 159 160ggml_tensor * lhs = ggml_add (ctx0 ,attn ,identity ); 161cb (lhs ,"dnet_add_ch_lhs" ,il ); 162 163attn = ggml_neg (ctx0 ,attn ); 164cb (attn ,"attn_pre_solve" ,il ); 165 166ggml_tensor * lin_solve = ggml_solve_tri (ctx0 ,lhs ,attn , true, true, false); 167attn = ggml_add (ctx0 ,lin_solve ,identity ); 168cb (attn ,"dnet_add_ch_attn_solved" ,il );// [CS, CS, n_chunks, H_k * n_seqs] 169 170// [S_v, CS, n_chunks, H_v * n_seqs] 171v = ggml_mul_mat (ctx0 ,ggml_cont (ctx0 ,ggml_transpose (ctx0 ,v_b )),attn ); 172 173// [CS, 1, n_chunks, H_v * n_seqs] KDA: [CS, S_k, n_chunks, H_v * n_seqs] 174ggml_tensor * g_exp = ggml_exp (ctx0 ,g_cs ); 175 176k_b = ggml_cont (ctx0 ,ggml_transpose (ctx0 ,k_b )); 177 178// [CS, S_k, n_chunks, H_k * n_seqs] 179ggml_tensor * kbg = ggml_mul (ctx0 ,k_b ,g_exp ); 180cb (kbg ,"k_beta_g_exp" ,il ); 181 182// [S_k, CS, n_chunks, H_k * n_seqs] 183ggml_tensor * k_cd = ggml_mul_mat (ctx0 ,kbg ,attn ); 184cb (k_cd ,"k_cumdecay" ,il ); 185 186// [1, CS, n_chunks, H_k * n_seqs] KDA: [S_k, CS, n_chunks, H_k * n_seqs] 187ggml_tensor * g_exp_t = ggml_cont (ctx0 ,ggml_transpose (ctx0 ,g_exp )); 188ggml_tensor * q_g_exp = ggml_mul (ctx0 ,q ,g_exp_t ); 189 190// vectorized calculation of key_gdiff 191// improved from the chunked version: 192// g_last = torch.clamp(g_cum[:, :, -1], max=50.0).exp().unsqueeze(-1).unsqueeze(-1) 193// g_diff = torch.clamp(g_cum[:, :, -1:] - g_cum, max=50.0).exp() 194// key_gdiff = key * g_diff.unsqueeze(-1) 195// kgdmulvnew = (key_gdiff).transpose(-1, -2) @ v_new 196// last_recurrent_state = last_recurrent_state * g_last + kgdmulvnew 197 198// get last element in g_cumsum along CS dimension (ne0) 199// example: [[x, y, z, ..., last], ...] -> [[last], ...] 200// [1, 1, n_chunks, H_v * n_seqs] KDA: [1, S_k, n_chunks, H_v * n_seqs] 201ggml_tensor * g_last = ggml_view_4d (ctx0 ,g_cs ,1 ,g_cs -> ne [1 ],g_cs -> ne [2 ],g_cs -> ne [3 ], 202g_cs -> nb [1 ], 203g_cs -> nb [2 ], 204g_cs -> nb [3 ], 205ggml_row_size (g_cs -> type ,g_cs -> ne [0 ]- 1 )); 206cb (g_last ,"g_last" ,il ); 207 208// TODO: remove this cont when CUDA supports non-cont unary ops 209g_last = ggml_cont (ctx0 ,g_last ); 210 211// [1, 1, n_chunks, H_v * n_seqs] KDA: [S_k, 1, n_chunks, H_v * n_seqs] 212ggml_tensor * g_last_exp_t = ggml_transpose (ctx0 ,ggml_exp (ctx0 ,g_last )); 213cb (g_last_exp_t ,"g_last_exp_t" ,il ); 214 215// [CS, 1, n_chunks, H_v * n_seqs] KDA: [CS, S_k, n_chunks, H_v * n_seqs] 216ggml_tensor * g_diff = ggml_neg (ctx0 ,ggml_sub (ctx0 ,g_cs ,g_last )); 217cb (g_diff ,"g_diff" ,il ); 218 219ggml_tensor * g_diff_exp_t = ggml_cont (ctx0 ,ggml_transpose (ctx0 ,ggml_exp (ctx0 ,g_diff ))); 220 221// [S_k, CS, n_chunks, H_v * n_seqs] 222ggml_tensor * kg = ggml_mul (ctx0 ,k ,g_diff_exp_t ); 223cb (kg ,"key_gdiff" ,il ); 224 225// [CS, S_k, n_chunks, H_v * n_seqs] 226ggml_tensor * kg_t = ggml_cont (ctx0 ,ggml_transpose (ctx0 ,kg )); 227cb (kg_t ,"key_gdiff_t" ,il ); 228 229s = ggml_reshape_4d (ctx0 ,s ,S_v ,S_v ,1 ,H_v * n_seqs ); 230cb (s ,"dnet_add_ch_state" ,il ); 231 232// [CS, S_v, n_chunks, H_v * n_seqs] 233ggml_tensor * v_t = ggml_cont (ctx0 ,ggml_transpose (ctx0 ,v )); 234 235for (int64_t chunk = 0 ;chunk < n_chunks ;chunk ++ ) { 236ggml_tensor * ch_k_cd = get_slice_2d (ctx0 ,k_cd ,chunk );// [S_k, CS, 1, H_k * n_seqs] 237ggml_tensor * ch_v_t = get_slice_2d (ctx0 ,v_t ,chunk );// [ CS, S_v, 1, H_v * n_seqs] 238ggml_tensor * ch_kq = get_slice_2d (ctx0 ,kq ,chunk );// [ CS, CS, 1, H_k * n_seqs] 239ggml_tensor * ch_q_g_exp = get_slice_2d (ctx0 ,q_g_exp ,chunk );// [S_k, CS, 1, H_k * n_seqs] 240ggml_tensor * ch_kg_t = get_slice_2d (ctx0 ,kg_t ,chunk );// [ CS, S_k, 1, H_v * n_seqs] 241 242// [CS, S_v, 1, H_v * n_seqs] 243ggml_tensor * v_t_p = ggml_mul_mat (ctx0 ,ch_k_cd ,s ); 244cb (v_t_p ,"v_prime" ,il ); 245 246// [CS, S_v, 1, H_v * n_seqs] 247ggml_tensor * v_t_new = ggml_sub (ctx0 ,ch_v_t ,v_t_p ); 248cb (v_t_new ,"v_t_new" ,il ); 249 250// [S_v, CS, 1, H_v * n_seqs] 251ggml_tensor * v_attn = ggml_mul_mat (ctx0 ,v_t_new ,ch_kq ); 252cb (v_attn ,"v_attn" ,il ); 253 254// [S_v, CS, 1, H_v * n_seqs] 255ggml_tensor * attn_inter = ggml_mul_mat (ctx0 ,s ,ch_q_g_exp ); 256cb (attn_inter ,"attn_inter" ,il ); 257 258// [S_v, CS, 1, H_v * n_seqs] 259ggml_tensor * o_ch = ggml_add (ctx0 ,attn_inter ,v_attn ); 260cb (o_ch ,"dnet_add_ch_attn_out" ,il ); 261 262v = ggml_set_inplace (ctx0 ,v ,o_ch ,v -> nb [1 ],v -> nb [2 ],v -> nb [3 ],chunk * v -> nb [2 ]); 263 264// kgdmulvnew = (key_gdiff).transpose(-1, -2) @ v_new 265// TODO: head broadcast might not work here - probably will need a transpose 266ggml_tensor * kgv = ggml_mul_mat (ctx0 ,ch_kg_t ,v_t_new );// [S_k, S_v, 1, H_k * n_seqs] 267 268// last_recurrent_state = last_recurrent_state * g_last + kgdmulvnew 269ggml_tensor * ch_g_last_exp_t = get_slice_2d (ctx0 ,g_last_exp_t ,chunk ); 270 271s = ggml_mul (ctx0 ,s ,ch_g_last_exp_t ); 272s = ggml_add (ctx0 ,s ,kgv ); 273cb (s ,"dnet_add_ch_state" ,il ); 274 } 275 276// truncate padded tokens 277ggml_tensor * o = ggml_view_4d (ctx0 ,v , 278S_v ,n_tokens ,H_v ,n_seqs , 279ggml_row_size (v -> type ,S_v ), 280ggml_row_size (v -> type ,S_v * CS * n_chunks ), 281ggml_row_size (v -> type ,S_v * CS * n_chunks * H_v ),0 ); 282o = ggml_permute (ctx0 ,o ,0 ,2 ,1 ,3 );// [S_v, H_v, n_tokens, n_seqs] 283s = ggml_reshape_4d (ctx0 ,s ,S_v ,S_v ,H_v ,n_seqs ); 284cb (s ,"output_state" ,il ); 285 286return {o ,s }; 287} 288 289std::pair < ggml_tensor * ,ggml_tensor *> llm_build_delta_net_base::build_delta_net_autoregressive ( 290ggml_tensor * q , 291ggml_tensor * k , 292ggml_tensor * v , 293ggml_tensor * g , 294ggml_tensor * b ,// beta 295ggml_tensor * s ,// state 296int il ) { 297const int64_t S_k = q -> ne [0 ]; 298const int64_t H_k = q -> ne [1 ]; 299const int64_t n_tokens = q -> ne [2 ]; 300const int64_t n_seqs = q -> ne [3 ]; 301 302const int64_t S_v = v -> ne [0 ]; 303const int64_t H_v = v -> ne [1 ]; 304 305GGML_ASSERT (n_tokens == 1 ); 306 307GGML_ASSERT (S_k == S_v ); 308GGML_ASSERT (H_v %H_k == 0 ); 309 310GGML_ASSERT (q -> ne [0 ]== S_k && q -> ne [1 ]== H_k && q -> ne [2 ]== n_tokens && q -> ne [3 ]== n_seqs ); 311GGML_ASSERT (k -> ne [0 ]== S_k && k -> ne [1 ]== H_k && k -> ne [2 ]== n_tokens && k -> ne [3 ]== n_seqs ); 312GGML_ASSERT (v -> ne [0 ]== S_v && v -> ne [1 ]== H_v && v -> ne [2 ]== n_tokens && v -> ne [3 ]== n_seqs ); 313 314GGML_ASSERT (g -> ne [0 ]== 1 || g -> ne [0 ]== S_v ); 315GGML_ASSERT (g -> ne [1 ]== H_v && g -> ne [2 ]== n_tokens && g -> ne [3 ]== n_seqs ); 316GGML_ASSERT (b -> ne [0 ]== 1 && b -> ne [1 ]== H_v && b -> ne [2 ]== n_tokens && b -> ne [3 ]== n_seqs ); 317GGML_ASSERT (s -> ne [0 ]== S_v && s -> ne [1 ]== S_v && s -> ne [2 ]== H_v && s -> ne [3 ]== n_seqs ); 318 319const float scale = 1.0f /sqrtf (S_k ); 320 321q = ggml_scale (ctx0 ,q ,scale ); 322 323q = ggml_permute (ctx0 ,q ,0 ,2 ,1 ,3 );// [S_k, n_tokens, H_k, n_seqs] 324k = ggml_permute (ctx0 ,k ,0 ,2 ,1 ,3 );// [S_k, n_tokens, H_k, n_seqs] 325v = ggml_permute (ctx0 ,v ,0 ,2 ,1 ,3 );// [S_v, n_tokens, H_v, n_seqs] 326 327cb (q ,"q_in" ,il ); 328cb (k ,"k_in" ,il ); 329cb (v ,"v_in" ,il ); 330cb (b ,"b_in" ,il ); 331cb (g ,"g_in" ,il ); 332 333// GDA: [1, 1, H_v, n_seqs] 334// KDA: [1, S_k, H_v, n_seqs] 335g = ggml_reshape_4d (ctx0 ,g ,1 ,g -> ne [0 ],H_v ,n_seqs ); 336b = ggml_reshape_4d (ctx0 ,b ,1 ,1 ,H_v ,n_seqs ); 337 338// [S_v, S_v, H_v, n_seqs] 339g = ggml_exp (ctx0 ,g ); 340s = ggml_mul (ctx0 ,s ,g ); 341 342// [1, S_v, H_v, n_seqs] 343ggml_tensor * sk ; 344sk = ggml_mul (ctx0 ,s ,k ); 345sk = ggml_sum_rows (ctx0 ,sk ); 346 347// [S_v, 1, H_v, n_seqs] 348ggml_tensor * d ; 349d = ggml_sub (ctx0 ,v ,ggml_transpose (ctx0 ,sk )); 350d = ggml_mul (ctx0 ,d ,b ); 351 352// [1, S_v, H_v, n_seqs] 353ggml_tensor * d_t ; 354d_t = ggml_transpose (ctx0 ,d ); 355 356// [S_v, S_v, H_v, n_seqs] 357ggml_tensor * kd ; 358k = ggml_repeat (ctx0 ,k ,s ); 359kd = ggml_mul (ctx0 ,k ,d_t ); 360 361s = ggml_add (ctx0 ,s ,kd ); 362 363cb (s ,"dnet_add_ar_state" ,il ); 364 365ggml_tensor * s_q = ggml_mul (ctx0 ,s ,q ); 366ggml_tensor * o = ggml_sum_rows (ctx0 ,s_q ); 367 368o = ggml_permute (ctx0 ,o ,2 ,0 ,1 ,3 );// [S_v, H_v, n_tokens, n_seqs] 369 370return {o ,s }; 371} 372 373std::pair < ggml_tensor * ,ggml_tensor *> llm_build_delta_net_base::build_delta_net_fused ( 374ggml_tensor * q , 375ggml_tensor * k , 376ggml_tensor * v , 377ggml_tensor * g , 378ggml_tensor * b , 379ggml_tensor * s , 380int il ) { 381const int64_t S_k = q -> ne [0 ]; 382const int64_t H_k = q -> ne [1 ]; 383const int64_t n_tokens = q -> ne [2 ]; 384const int64_t n_seqs = q -> ne [3 ]; 385 386const int64_t S_v = v -> ne [0 ]; 387const int64_t H_v = v -> ne [1 ]; 388 389GGML_ASSERT (S_k == S_v ); 390GGML_ASSERT (H_v %H_k == 0 ); 391 392GGML_ASSERT (q -> ne [0 ]== S_k && q -> ne [1 ]== H_k && q -> ne [2 ]== n_tokens && q -> ne [3 ]== n_seqs ); 393GGML_ASSERT (k -> ne [0 ]== S_k && k -> ne [1 ]== H_k && k -> ne [2 ]== n_tokens && k -> ne [3 ]== n_seqs ); 394GGML_ASSERT (v -> ne [0 ]== S_v && v -> ne [1 ]== H_v && v -> ne [2 ]== n_tokens && v -> ne [3 ]== n_seqs ); 395 396GGML_ASSERT (g -> ne [0 ]== 1 || g -> ne [0 ]== S_v ); 397GGML_ASSERT (g -> ne [1 ]== H_v && g -> ne [2 ]== n_tokens && g -> ne [3 ]== n_seqs ); 398GGML_ASSERT (b -> ne [0 ]== 1 && b -> ne [1 ]== H_v && b -> ne [2 ]== n_tokens && b -> ne [3 ]== n_seqs ); 399GGML_ASSERT (s -> ne [0 ]== S_v && s -> ne [1 ]== S_v && s -> ne [2 ]== H_v && s -> ne [3 ]== n_seqs ); 400 401// K=1: output carries the final state only. state s is 4D [S_v, S_v, H_v, n_seqs]. 402ggml_tensor * result = ggml_gated_delta_net (ctx0 ,q ,k ,v ,g ,b ,s ,/*K=*/ 1 ); 403if (n_tokens == 1 ) { 404res -> add_fused_node ({LLM_FUSED_OP_GDN_AR ,result ,il }); 405 }else { 406res -> add_fused_node ({LLM_FUSED_OP_GDN_CH ,result ,il }); 407 } 408 409ggml_tensor * output = ggml_view_4d (ctx0 ,result , 410S_v ,H_v ,n_tokens ,n_seqs , 411ggml_row_size (result -> type ,S_v ), 412ggml_row_size (result -> type ,S_v * H_v ), 413ggml_row_size (result -> type ,S_v * H_v * n_tokens ),0 ); 414 415ggml_tensor * new_state = ggml_view_4d (ctx0 ,result , 416S_v ,S_v ,H_v ,n_seqs , 417ggml_row_size (result -> type ,S_v ), 418ggml_row_size (result -> type ,S_v * S_v ), 419ggml_row_size (result -> type ,S_v * S_v * H_v ), 420ggml_row_size (result -> type ,S_v * H_v * n_tokens * n_seqs )); 421 422return {output ,new_state }; 423} 424 425std::pair < ggml_tensor * ,ggml_tensor *> llm_build_delta_net_base::build_delta_net ( 426ggml_tensor * q , 427ggml_tensor * k , 428ggml_tensor * v , 429ggml_tensor * g , 430ggml_tensor * b , 431ggml_tensor * s , 432int il ) { 433const int64_t n_seq_tokens = q -> ne [2 ]; 434 435if (n_seq_tokens == 1 ) { 436if (cparams .fused_gdn_ar ) { 437return build_delta_net_fused (q ,k ,v ,g ,b ,s ,il ); 438 } 439return build_delta_net_autoregressive (q ,k ,v ,g ,b ,s ,il ); 440 } 441 442if (cparams .fused_gdn_ch ) { 443return build_delta_net_fused (q ,k ,v ,g ,b ,s ,il ); 444 } 445 446return build_delta_net_chunking (q ,k ,v ,g ,b ,s ,il ); 447} 448 449ggml_tensor * llm_build_delta_net_base::build_conv_state ( 450llm_graph_input_rs * inp , 451ggml_tensor * conv_states_all , 452ggml_tensor * qkv_mixed , 453int64_t conv_kernel_size , 454int64_t conv_channels , 455int il ) { 456const auto * mctx_cur = inp -> mctx ; 457 458const auto kv_head = mctx_cur -> get_head (); 459const auto mem_size = mctx_cur -> get_size (); 460 461const int64_t n_seqs = ubatch .n_seqs ; 462 463ggml_tensor * conv_states = build_rs (inp ,conv_states_all ,hparams .n_embd_r (),n_seqs ); 464cb (conv_states ,"conv_states" ,il ); 465 466conv_states = ggml_reshape_3d (ctx0 ,conv_states ,conv_kernel_size - 1 ,conv_channels ,n_seqs ); 467cb (conv_states ,"conv_states_reshaped" ,il ); 468 469qkv_mixed = ggml_transpose (ctx0 ,qkv_mixed ); 470cb (qkv_mixed ,"qkv_mixed_transposed" ,il ); 471 472ggml_tensor * conv_input = ggml_concat (ctx0 ,conv_states ,qkv_mixed ,0 ); 473cb (conv_input ,"conv_input" ,il ); 474 475const int64_t row_count = (conv_kernel_size - 1 )* conv_channels ; 476 477const size_t row_size = ggml_row_size (conv_states_all -> type ,row_count ); 478 479if (cparams .n_rs_seq == 0 ) { 480const int64_t s_idx = conv_input -> ne [0 ]- conv_states -> ne [0 ]; 481const int64_t s_slot = 0 ; 482 483ggml_tensor * conv_state_last = 484ggml_view_3d (ctx0 ,conv_input , 485conv_kernel_size - 1 ,conv_channels ,n_seqs , 486conv_input -> nb [1 ],conv_input -> nb [2 ], 487ggml_row_size (conv_input -> type ,s_idx )); 488cb (conv_state_last ,"conv_state_last" ,il ); 489 490ggml_tensor * conv_state_update = 491ggml_view_2d (ctx0 ,conv_states_all , 492row_count ,n_seqs ,conv_states_all -> nb [1 ], 493 (s_slot * mem_size + kv_head )* row_size ); 494cb (conv_state_update ,"conv_state_update" ,il ); 495 496ggml_build_forward_expand (gf ,ggml_cpy (ctx0 ,conv_state_last ,conv_state_update )); 497 }else { 498// [TAG_RECURRENT_ROLLBACK_SPLITS] 499// this logic assumes that the last (n_rs_seq + 1) tokens of a sequence in a batch are inside 500// the same ubatch, which `split_equal()` guarantees via its n_keep_tail argument 501 502const int64_t K = (int64_t )cparams .n_rs_seq + 1 ; 503 504for (int64_t t = 1 ;t <=K ;++ t ) { 505const int64_t s_idx = std::max < int64_t > (0 ,conv_input -> ne [0 ]- conv_states -> ne [0 ]- K + t ); 506const int64_t s_slot = K - t ; 507 508ggml_tensor * conv_state_last = 509ggml_view_3d (ctx0 ,conv_input , 510conv_kernel_size - 1 ,conv_channels ,n_seqs , 511conv_input -> nb [1 ],conv_input -> nb [2 ], 512ggml_row_size (conv_input -> type ,s_idx )); 513 514ggml_tensor * conv_state_update = 515ggml_view_2d (ctx0 , 516conv_states_all ,row_count ,n_seqs , 517conv_states_all -> nb [1 ], 518 (s_slot * mem_size + kv_head )* row_size ); 519 520ggml_build_forward_expand (gf ,ggml_cpy (ctx0 ,conv_state_last ,conv_state_update )); 521 } 522 } 523 524return conv_input ; 525} 526 527ggml_tensor * llm_build_delta_net_base::build_recurrent_attn ( 528llm_graph_input_rs * inp , 529ggml_tensor * ssm_states_all , 530ggml_tensor * q , 531ggml_tensor * k , 532ggml_tensor * v , 533ggml_tensor * g , 534ggml_tensor * b , 535ggml_tensor * s , 536int il ) { 537const auto * mctx_cur = inp -> mctx ; 538const auto kv_head = mctx_cur -> get_head (); 539const uint32_t mem_size = mctx_cur -> get_size (); 540 541const int64_t S_v = s -> ne [0 ]; 542const int64_t H_v = s -> ne [2 ]; 543const int64_t n_seqs = s -> ne [3 ]; 544const int64_t n_seq_tokens = q -> ne [2 ]; 545 546const bool keep = cparams .n_rs_seq > 0 ; 547 548if (!keep ) { 549auto attn_out = build_delta_net (q ,k ,v ,g ,b ,s ,il ); 550ggml_tensor * output = attn_out .first ; 551ggml_tensor * new_state = attn_out .second ; 552cb (output ,"attn_output" ,il ); 553cb (new_state ,"new_state" ,il ); 554 555ggml_build_forward_expand (gf , 556ggml_cpy (ctx0 ,new_state , 557ggml_view_2d (ctx0 ,ssm_states_all ,hparams .n_embd_s (),n_seqs ,ssm_states_all -> nb [1 ], 558kv_head * hparams .n_embd_s ()* ggml_element_size (ssm_states_all )))); 559 560return output ; 561 } 562 563const int64_t D = S_v * S_v * H_v ; 564const int64_t K = cparams .n_rs_seq + 1 ; 565 566// state s is 4D [S_v, S_v, H_v, n_seqs]; K snapshot slots are written into the output. 567ggml_tensor * gdn_out = ggml_gated_delta_net (ctx0 ,q ,k ,v ,g ,b ,s ,K ); 568if (n_seq_tokens > 1 ) { 569res -> add_fused_node ({LLM_FUSED_OP_GDN_CH ,gdn_out ,il }); 570 }else { 571res -> add_fused_node ({LLM_FUSED_OP_GDN_AR ,gdn_out ,il }); 572 } 573 574const int64_t attn_score_elems = S_v * H_v * n_seq_tokens * n_seqs ; 575const int64_t state_size_per_snap = S_v * S_v * H_v * n_seqs ; 576 577ggml_tensor * output = ggml_view_4d (ctx0 ,gdn_out , 578S_v ,H_v ,n_seq_tokens ,n_seqs , 579ggml_row_size (gdn_out -> type ,S_v ), 580ggml_row_size (gdn_out -> type ,S_v * H_v ), 581ggml_row_size (gdn_out -> type ,S_v * H_v * n_seq_tokens ), 5820 ); 583cb (output ,"attn_output" ,il ); 584 585const size_t row_size = hparams .n_embd_s ()* ggml_element_size (ssm_states_all ); 586 587// op writes the last min(n_seq_tokens, K) snapshots; trailing slots are left unwritten 588const int64_t n_written = std::min < int64_t > (n_seq_tokens ,K ); 589 590// write the produced snapshots into the recurrent cache (snapshot slot i -> rollback group i) 591ggml_tensor * src = ggml_view_3d (ctx0 ,gdn_out , 592D ,n_seqs ,n_written , 593ggml_row_size (gdn_out -> type ,D ), 594ggml_row_size (gdn_out -> type ,state_size_per_snap ), 595ggml_row_size (gdn_out -> type ,attn_score_elems )); 596 597ggml_tensor * dst = ggml_view_3d (ctx0 ,ssm_states_all , 598D ,n_seqs ,n_written , 599ssm_states_all -> nb [1 ], 600 (size_t )mem_size * row_size , 601 (size_t )kv_head * row_size ); 602 603ggml_build_forward_expand (gf ,ggml_cpy (ctx0 ,src ,dst )); 604 605return output ; 606}