char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
6e62ba538
char/aliceai-t5-moe
1#include "models.h" 2 3#include <string> 4 5// on-device sampling: top-k, top-p, then a random draw 6ggml_tensor * clip_graph_qwen3tts_gen::code_gen::do_sampling (ggml_tensor * logits ,ggml_tensor * inp_rand )const { 7logits = ggml_reshape_1d (ctx0 ,logits ,ggml_nelements (logits )); 8const int64_t n_vocab = logits -> ne [0 ]; 9 10// sort a's rows by idx 11auto sort_by = [this ](ggml_tensor * a ,ggml_tensor * idx ) { 12ggml_tensor * a2d = ggml_reshape_2d (ctx0 ,a ,1 ,a -> ne [0 ]); 13return ggml_reshape_1d (ctx0 ,ggml_get_rows (ctx0 ,a2d ,idx ),idx -> ne [0 ]); 14 }; 15 16ggml_tensor * cur = logits ; 17ggml_tensor * candidates = nullptr ;// maps row index back to vocab id 18 19if (top_k > 0 && top_k < n_vocab ) { 20ggml_tensor * idx = ggml_top_k (ctx0 ,cur ,top_k ); 21candidates = idx ; 22cur = sort_by (cur ,idx ); 23cb (cur ,"sample_top_k_logits" ,-1 ); 24 } 25 26if (top_p < 1.0f ) { 27ggml_tensor * sorted_idx = ggml_argsort (ctx0 ,cur ,GGML_SORT_ORDER_DESC ); 28ggml_tensor * sorted_logits = sort_by (cur ,sorted_idx ); 29candidates = candidates ?sort_by (candidates ,sorted_idx ) :sorted_idx ; 30 31ggml_tensor * probs = ggml_soft_max (ctx0 ,sorted_logits ); 32ggml_tensor * cdf = ggml_cumsum (ctx0 ,probs ); 33 34// keep_mask[i] = 1 once cdf[i] crosses top_p 35ggml_tensor * cdf_scaled = ggml_scale_bias (ctx0 ,cdf ,-1.0f ,top_p ); 36ggml_tensor * keep_mask = ggml_step (ctx0 ,cdf_scaled ); 37ggml_tensor * idxf = ggml_sum (ctx0 ,keep_mask ); 38idxf = ggml_clamp (ctx0 ,idxf ,0.0f , (float )keep_mask -> ne [0 ]- 1 ); 39ggml_tensor * ones = ggml_scale_bias (ctx0 ,idxf ,0.0f ,1.0f ); 40 41// top-p must include the crossing element, so force it to 1 42ggml_tensor * keep_mask_2d = ggml_reshape_2d (ctx0 ,keep_mask ,1 ,keep_mask -> ne [0 ]); 43keep_mask_2d = ggml_set_rows (ctx0 ,keep_mask_2d ,ones ,ggml_cast (ctx0 ,idxf ,GGML_TYPE_I32 )); 44keep_mask = ggml_reshape_1d (ctx0 ,keep_mask_2d ,keep_mask -> ne [0 ]); 45 46// log(1) = 0 (keep), log(0) = -inf (drop) 47ggml_tensor * bias = ggml_log (ctx0 ,keep_mask ); 48cur = ggml_add (ctx0 ,sorted_logits ,bias ); 49cb (cur ,"sample_top_p_logits" ,-1 ); 50 } 51 52// draw one token: find where the cdf crosses inp_rand 53ggml_tensor * probs = ggml_soft_max (ctx0 ,cur ); 54ggml_tensor * cumsum = ggml_cumsum (ctx0 ,probs ); 55 56ggml_tensor * diff = ggml_sub (ctx0 ,cumsum ,inp_rand ); 57ggml_tensor * cross_mask = ggml_step (ctx0 ,diff ); 58ggml_tensor * idxf = ggml_sum (ctx0 ,cross_mask ); 59ggml_tensor * idx = ggml_cast (ctx0 ,ggml_scale_bias (ctx0 ,idxf ,-1.0f , (float )cross_mask -> ne [0 ]),GGML_TYPE_I32 ); 60 61if (candidates ) { 62ggml_tensor * cand_2d = ggml_reshape_2d (ctx0 ,candidates ,1 ,candidates -> ne [0 ]); 63idx = ggml_get_rows (ctx0 ,cand_2d ,idx ); 64 } 65cb (idx ,"sample_token_id" ,-1 ); 66 67return idx ; 68} 69 70// returns a new cache with row row_idx set to value 71ggml_tensor * clip_graph_qwen3tts_gen::code_gen::cache_set (ggml_tensor * cache ,int row_idx ,ggml_tensor * value )const { 72const int64_t n_embd = cache -> ne [0 ]; 73const int64_t n_cache = cache -> ne [1 ]; 74GGML_ASSERT (row_idx >=0 && row_idx < n_cache ); 75 76// append value as the last row, then gather it back into place 77ggml_tensor * value_2d = ggml_reshape_2d (ctx0 ,value ,n_embd ,1 ); 78ggml_tensor * cache_ext = ggml_concat (ctx0 ,cache ,value_2d ,1 );// [n_embd, n_cache + 1] 79 80// gather indices [0..row_idx-1, n_cache, row_idx+1..n_cache-1] 81// built via concat, since ggml_set_rows needs F32/F16 values, not an I32 index array 82ggml_tensor * idx = const_i32 (cache , (float )n_cache ); 83if (row_idx > 0 ) { 84ggml_tensor * prefix = ggml_cast (ctx0 ,ggml_arange (ctx0 ,0.0f , (float )row_idx ,1.0f ),GGML_TYPE_I32 ); 85idx = ggml_concat (ctx0 ,prefix ,idx ,0 ); 86 } 87if (row_idx < n_cache - 1 ) { 88ggml_tensor * suffix = ggml_cast (ctx0 ,ggml_arange (ctx0 , (float ) (row_idx + 1 ), (float )n_cache ,1.0f ),GGML_TYPE_I32 ); 89idx = ggml_concat (ctx0 ,idx ,suffix ,0 ); 90 } 91 92ggml_tensor * result = ggml_get_rows (ctx0 ,cache_ext ,idx ); 93cb (result ,"cache_set_out" ,-1 ); 94return result ; 95} 96 97// builds a const i32 with no host upload: view a tensor, zero it via scale, add value, cast to i32 98ggml_tensor * clip_graph_qwen3tts_gen::code_gen::const_i32 (ggml_tensor * anchor ,float value )const { 99ggml_tensor * v = ggml_view_1d (ctx0 ,anchor ,1 ,0 ); 100if (v -> type != GGML_TYPE_F32 ) { 101v = ggml_cast (ctx0 ,v ,GGML_TYPE_F32 ); 102 } 103return ggml_cast (ctx0 ,ggml_scale_bias (ctx0 ,v ,0.0f ,value ),GGML_TYPE_I32 ); 104} 105 106// causal keep-mask row for a query at position pos, window size n_kv_pad 107ggml_tensor * clip_graph_qwen3tts_gen::code_gen::causal_mask_row (int64_t n_kv_pad ,int pos )const { 108ggml_tensor * ones = ggml_fill (ctx0 ,ggml_new_tensor_2d (ctx0 ,GGML_TYPE_F32 ,n_kv_pad ,n_kv_pad ),1.0f ); 109ggml_tensor * keep = ggml_tri (ctx0 ,ones ,GGML_TRI_TYPE_LOWER_DIAG ); 110ggml_tensor * row = ggml_view_1d (ctx0 ,keep ,n_kv_pad , (size_t )pos * keep -> nb [1 ]); 111ggml_tensor * mask = ggml_log (ctx0 ,row );// 0 = keep, -inf = masked 112return ggml_reshape_4d (ctx0 ,mask ,n_kv_pad ,1 ,1 ,1 ); 113} 114 115// talker hidden size -> predictor hidden size (small_to_mtp_projection) 116ggml_tensor * clip_graph_qwen3tts_gen::code_gen::project_in (ggml_tensor * cur )const { 117if (!model .gen_code_proj_in_w ) { 118return cur ; 119 } 120cur = ggml_mul_mat (ctx0 ,model .gen_code_proj_in_w ,cur ); 121if (model .gen_code_proj_in_b ) { 122cur = ggml_add (ctx0 ,cur ,model .gen_code_proj_in_b ); 123 } 124return cur ; 125} 126 127// one transformer layer at position pos; writes k/v into k_cache_layer/v_cache_layer at row pos 128ggml_tensor * clip_graph_qwen3tts_gen::code_gen::layer_forward ( 129ggml_tensor * cur , 130const clip_layer & layer , 131ggml_tensor * inp_pos , 132ggml_tensor * kq_mask , 133ggml_tensor *& k_cache_layer , 134ggml_tensor *& v_cache_layer , 135int64_t n_kv_pad , 136int pos , 137int il )const { 138const int n_head = hparams .n_head ; 139const int n_head_kv = hparams .n_head_kv ; 140const int64_t d_head = layer .q_w -> ne [1 ] /n_head ;// real head_dim, not n_embd / n_head 141const float kq_scale = 1.0f /sqrtf ((float )d_head ); 142 143ggml_tensor * residual = cur ; 144 145ggml_tensor * h = ggml_rms_norm (ctx0 ,cur ,hparams .eps ); 146h = ggml_mul (ctx0 ,h ,layer .ln_1_w ); 147 148ggml_tensor * q = ggml_mul_mat (ctx0 ,layer .q_w ,h ); 149ggml_tensor * k = ggml_mul_mat (ctx0 ,layer .k_w ,h ); 150ggml_tensor * v = ggml_mul_mat (ctx0 ,layer .v_w ,h ); 151 152q = ggml_reshape_3d (ctx0 ,q ,d_head ,n_head ,1 ); 153k = ggml_reshape_3d (ctx0 ,k ,d_head ,n_head_kv ,1 ); 154 155q = ggml_rms_norm (ctx0 ,q ,hparams .eps ); 156q = ggml_mul (ctx0 ,q ,layer .q_norm ); 157k = ggml_rms_norm (ctx0 ,k ,hparams .eps ); 158k = ggml_mul (ctx0 ,k ,layer .k_norm ); 159 160q = ggml_rope_ext (ctx0 ,q ,inp_pos ,nullptr , (int )d_head ,GGML_ROPE_TYPE_NEOX ,0 , 161hparams .rope_theta ,1.0f ,0.0f ,1.0f ,0.0f ,0.0f ); 162k = ggml_rope_ext (ctx0 ,k ,inp_pos ,nullptr , (int )d_head ,GGML_ROPE_TYPE_NEOX ,0 , 163hparams .rope_theta ,1.0f ,0.0f ,1.0f ,0.0f ,0.0f ); 164 165// write k/v into the cache at row pos, flat layout 166ggml_tensor * k_flat = ggml_reshape_1d (ctx0 ,k ,d_head * n_head_kv ); 167k_cache_layer = cache_set (k_cache_layer ,pos ,k_flat ); 168v_cache_layer = cache_set (v_cache_layer ,pos ,v ); 169 170ggml_tensor * q_cur = ggml_reshape_4d (ctx0 ,q ,d_head ,n_head ,1 ,1 ); 171ggml_tensor * k_cur = ggml_reshape_4d (ctx0 ,k_cache_layer ,d_head ,n_head_kv ,n_kv_pad ,1 ); 172ggml_tensor * v_cur = ggml_reshape_4d (ctx0 ,v_cache_layer ,d_head ,n_head_kv ,n_kv_pad ,1 ); 173 174ggml_tensor * attn_out = build_attn (layer .o_w ,layer .o_b ,q_cur ,k_cur ,v_cur ,kq_mask ,kq_scale ,il ); 175 176cur = ggml_add (ctx0 ,residual ,attn_out ); 177 178ggml_tensor * h2 = ggml_rms_norm (ctx0 ,cur ,hparams .eps ); 179h2 = ggml_mul (ctx0 ,h2 ,layer .ln_2_w ); 180 181ggml_tensor * gate = ggml_mul_mat (ctx0 ,layer .ff_gate_w ,h2 ); 182ggml_tensor * up = ggml_mul_mat (ctx0 ,layer .ff_up_w ,h2 ); 183ggml_tensor * gu = ggml_swiglu_split (ctx0 ,gate ,up ); 184ggml_tensor * down = ggml_mul_mat (ctx0 ,layer .ff_down_w ,gu ); 185 186return ggml_add (ctx0 ,cur ,down ); 187} 188 189// position 0: hidden bridge, seeds the k/v cache, no sampling 190// position 1: embed(code0), sample with lm_head[0], write out_code_cache[1] 191void clip_graph_qwen3tts_gen::code_gen::prefill ( 192 std::vector < ggml_tensor *> & k_cache , 193 std::vector < ggml_tensor *> & v_cache , 194ggml_tensor *& out_code_cache , 195ggml_tensor * h_state , 196ggml_tensor * code0_embd , 197ggml_tensor * inp_rand )const { 198const int64_t n_kv_pad = k_cache [0 ]-> ne [1 ]; 199 200 { 201ggml_tensor * cur = project_in (h_state ); 202ggml_tensor * kq_mask = causal_mask_row (n_kv_pad ,0 ); 203ggml_tensor * inp_pos = const_i32 (k_cache [0 ],0.0f ); 204for (size_t il = 0 ;il < model .layers .size ();il ++ ) { 205cur = layer_forward (cur ,model .layers [il ],inp_pos ,kq_mask ,k_cache [il ],v_cache [il ],n_kv_pad ,0 , (int )il ); 206 } 207// position 0's output is unused, it only seeded the cache 208 } 209 210 { 211ggml_tensor * cur = project_in (code0_embd ); 212ggml_tensor * kq_mask = causal_mask_row (n_kv_pad ,1 ); 213ggml_tensor * inp_pos = const_i32 (k_cache [0 ],1.0f ); 214for (size_t il = 0 ;il < model .layers .size ();il ++ ) { 215cur = layer_forward (cur ,model .layers [il ],inp_pos ,kq_mask ,k_cache [il ],v_cache [il ],n_kv_pad ,1 , (int )il ); 216 } 217 218cur = ggml_rms_norm (ctx0 ,cur ,hparams .eps ); 219cur = ggml_mul (ctx0 ,cur ,model .gen_code_norm_w ); 220 221ggml_tensor * head_w = model .gen_code_head_w ; 222ggml_tensor * head_g = ggml_view_2d (ctx0 ,head_w ,head_w -> ne [0 ],head_w -> ne [1 ],head_w -> nb [1 ],0 );// lm_head[0] 223ggml_tensor * logits = ggml_mul_mat (ctx0 ,head_g ,cur ); 224 225ggml_tensor * sampled = do_sampling (logits ,inp_rand ); 226out_code_cache = cache_set (out_code_cache ,1 ,sampled ); 227 } 228} 229 230// one decode step of code_predictor 231// at step_idx g: 232// - read code from out_code_cache[g], then embed it with codebook table g-1 233// - write new kv at cache row g+1, sample with lm_head[g] 234// - write result to out_code_cache[g+1] 235// step_idx must be in [1, n_acoustic - 1] 236ggml_tensor * clip_graph_qwen3tts_gen::code_gen::step ( 237 std::vector < ggml_tensor *> & k_cache , 238 std::vector < ggml_tensor *> & v_cache , 239ggml_tensor * out_code_cache , 240ggml_tensor * inp_rand , 241int step_idx )const { 242const int64_t n_acoustic = model .gen_code_head_w -> ne [2 ]; 243GGML_ASSERT (step_idx >=1 && step_idx < n_acoustic ); 244GGML_ASSERT (k_cache .size ()== model .layers .size ()); 245GGML_ASSERT (v_cache .size ()== model .layers .size ()); 246 247const int64_t n_kv_pad = k_cache [0 ]-> ne [1 ]; 248const int pos = step_idx + 1 ;// new cache row and RoPE position 249 250// embed the previous code via this step's codebook table (rows are already scalars) 251ggml_tensor * code_in = ggml_view_1d (ctx0 ,out_code_cache ,1 , (size_t )step_idx * out_code_cache -> nb [1 ]); 252 253ggml_tensor * embd_w = model .gen_code_embd_w ;// [n_embd_talker, vocab, n_acoustic] 254ggml_tensor * embd_g = ggml_view_2d (ctx0 ,embd_w ,embd_w -> ne [0 ],embd_w -> ne [1 ],embd_w -> nb [1 ], 255 (size_t ) (step_idx - 1 )* embd_w -> nb [2 ]); 256ggml_tensor * cur = ggml_get_rows (ctx0 ,embd_g ,code_in ); 257cur = ggml_reshape_1d (ctx0 ,cur ,cur -> ne [0 ]); 258cb (cur ,"step_embd_in" ,step_idx ); 259 260cur = project_in (cur ); 261cb (cur ,"step_proj_in" ,step_idx ); 262 263ggml_tensor * kq_mask = causal_mask_row (n_kv_pad ,pos ); 264ggml_tensor * inp_pos = const_i32 (k_cache [0 ], (float )pos ); 265 266for (size_t il = 0 ;il < model .layers .size ();il ++ ) { 267cur = layer_forward (cur ,model .layers [il ],inp_pos ,kq_mask ,k_cache [il ],v_cache [il ],n_kv_pad ,pos , (int )il ); 268cb (cur ,"step_layer_out" , (int )il ); 269 } 270 271// final norm, this step's lm_head, sample, write the result 272cur = ggml_rms_norm (ctx0 ,cur ,hparams .eps ); 273cur = ggml_mul (ctx0 ,cur ,model .gen_code_norm_w ); 274 275ggml_tensor * head_w = model .gen_code_head_w ;// [n_embd_pred, vocab, n_acoustic] 276ggml_tensor * head_g = ggml_view_2d (ctx0 ,head_w ,head_w -> ne [0 ],head_w -> ne [1 ],head_w -> nb [1 ], 277 (size_t )step_idx * head_w -> nb [2 ]); 278ggml_tensor * logits = ggml_mul_mat (ctx0 ,head_g ,cur ); 279cb (logits ,"step_logits" ,step_idx ); 280 281ggml_tensor * sampled = do_sampling (logits ,inp_rand ); 282cb (sampled ,"step_sampled" ,step_idx ); 283 284return cache_set (out_code_cache ,pos ,sampled ); 285} 286 287// causal conv1d, stride 1: prepend persisted left-context instead of zero-padding, then a plain conv 288// x: [T, IC] (T-first). w: [K, IC, OC]. state_name empty means K == 1 (no left-context). returns [T, OC] 289ggml_tensor * clip_graph_qwen3tts_gen::code2wav::causal_conv1d (ggml_tensor * x ,ggml_tensor * w ,ggml_tensor * b ,int dilation ,const std::string & state_name )const { 290const int K = (int )w -> ne [0 ]; 291const int pad = (K - 1 )* dilation ; 292 293ggml_tensor * x_full = x ; 294if (pad > 0 ) { 295ggml_tensor * left = state_in .at (state_name );// [pad, IC] 296x_full = ggml_concat (ctx0 ,left ,x ,0 ); 297 } 298ggml_tensor * y = ggml_conv_1d (ctx0 ,w ,x_full ,1 ,0 ,dilation );// [T, OC, 1] 299y = ggml_reshape_2d (ctx0 ,y ,y -> ne [0 ],y -> ne [1 ]); 300if (b ) { 301y = ggml_add (ctx0 ,y ,ggml_reshape_2d (ctx0 ,b ,1 ,b -> ne [0 ])); 302 } 303if (pad > 0 ) { 304ggml_tensor * new_left = ggml_cont (ctx0 ,ggml_view_2d (ctx0 ,x_full ,pad ,x_full -> ne [1 ],x_full -> nb [1 ], 305 (size_t ) (x_full -> ne [0 ]- pad )* x_full -> nb [0 ])); 306state_out .push_back ({state_name ,new_left }); 307 } 308return y ; 309} 310 311// causal depthwise conv1d, stride 1, dilation 1, kernel from w's shape. 312// x: [T, C]. w: [K, 1, C]. returns [T, C]. see causal_conv1d for the state contract. 313ggml_tensor * clip_graph_qwen3tts_gen::code2wav::causal_conv1d_dw (ggml_tensor * x ,ggml_tensor * w ,ggml_tensor * b ,const std::string & state_name )const { 314const int K = (int )w -> ne [0 ]; 315const int pad = K - 1 ; 316 317ggml_tensor * x_full = x ; 318if (pad > 0 ) { 319ggml_tensor * left = state_in .at (state_name );// [pad, C] 320x_full = ggml_concat (ctx0 ,left ,x ,0 ); 321 } 322ggml_tensor * y = ggml_conv_1d_dw (ctx0 ,w ,x_full ,1 ,0 ,1 );// [T, C, 1] 323y = ggml_reshape_2d (ctx0 ,y ,y -> ne [0 ],y -> ne [1 ]); 324if (b ) { 325y = ggml_add (ctx0 ,y ,ggml_reshape_2d (ctx0 ,b ,1 ,b -> ne [0 ])); 326 } 327if (pad > 0 ) { 328ggml_tensor * new_left = ggml_cont (ctx0 ,ggml_view_2d (ctx0 ,x_full ,pad ,x_full -> ne [1 ],x_full -> nb [1 ], 329 (size_t ) (x_full -> ne [0 ]- pad )* x_full -> nb [0 ])); 330state_out .push_back ({state_name ,new_left }); 331 } 332return y ; 333} 334 335// causal ConvTranspose1d, the (kernel - stride) overlap tail is kept as state for the next call 336// x: [T, IC], w: [K, OC, IC]. state_name empty means K == stride (no overlap). returns [T * stride, OC] 337ggml_tensor * clip_graph_qwen3tts_gen::code2wav::causal_conv_transpose1d (ggml_tensor * x ,ggml_tensor * w ,ggml_tensor * b ,int stride ,const std::string & state_name )const { 338const int K = (int )w -> ne [0 ]; 339const int OC = (int )w -> ne [1 ]; 340const int trim = K - stride ; 341const int64_t emit_len = x -> ne [0 ]* stride ; 342 343// transposed conv as GEMM + col2im scatter-add, y: [emit_len + trim, OC] 344ggml_tensor * w2 = ggml_reshape_2d (ctx0 ,w , (int64_t )K * OC ,w -> ne [2 ]); 345w2 = ggml_cont (ctx0 ,ggml_transpose (ctx0 ,w2 )); 346ggml_tensor * xt = ggml_cont (ctx0 ,ggml_transpose (ctx0 ,x )); 347ggml_tensor * col = ggml_mul_mat (ctx0 ,w2 ,xt ); 348ggml_tensor * y = ggml_col2im_1d (ctx0 ,col ,stride ,OC ,0 ); 349 350ggml_tensor * out = y ; 351if (trim > 0 ) { 352ggml_tensor * tail = state_in .at (state_name );// [trim, OC] 353ggml_tensor * head = ggml_add (ctx0 ,ggml_view_2d (ctx0 ,y ,trim ,y -> ne [1 ],y -> nb [1 ],0 ),tail ); 354if (emit_len > trim ) { 355ggml_tensor * middle = ggml_view_2d (ctx0 ,y ,emit_len - trim ,y -> ne [1 ],y -> nb [1 ], (size_t )trim * y -> nb [0 ]); 356out = ggml_concat (ctx0 ,head ,middle ,0 ); 357 }else { 358out = head ; 359 } 360ggml_tensor * new_tail = ggml_cont (ctx0 ,ggml_view_2d (ctx0 ,y ,trim ,y -> ne [1 ],y -> nb [1 ], (size_t )emit_len * y -> nb [0 ])); 361state_out .push_back ({state_name ,new_tail }); 362 } 363if (b ) { 364out = ggml_add (ctx0 ,out ,ggml_reshape_2d (ctx0 ,b ,1 ,b -> ne [0 ])); 365 } 366return out ; 367} 368 369// SnakeBeta activation: y = x + sin(alpha*x)^2 * inv_beta (alpha/inv_beta folded via exp/reciprocal at conversion time) 370// x: [T, C]. alpha/beta: [C], broadcasts over T 371ggml_tensor * clip_graph_qwen3tts_gen::code2wav::snake (ggml_tensor * x ,ggml_tensor * alpha ,ggml_tensor * beta )const { 372ggml_tensor * a = ggml_reshape_2d (ctx0 ,alpha ,1 ,alpha -> ne [0 ]); 373ggml_tensor * b = ggml_reshape_2d (ctx0 ,beta ,1 ,beta -> ne [0 ]); 374 375// expand reshapes first so mul/sin/sqr/mul/add lands as consecutive nodes, letting backends fuse them 376ggml_build_forward_expand (gf ,a ); 377ggml_build_forward_expand (gf ,b ); 378 379ggml_tensor * s = ggml_sin (ctx0 ,ggml_mul (ctx0 ,x ,a )); 380s = ggml_sqr (ctx0 ,s ); 381s = ggml_mul (ctx0 ,s ,b ); 382return ggml_add (ctx0 ,x ,s ); 383} 384 385// RVQ codebook decode: T frames of 16 codes -> 512-dim hidden (C-first, [512, T]) 386// codebook 0 (semantic) and 1..15 (acoustic) sum within their group, project separately, then add 387ggml_tensor * clip_graph_qwen3tts_gen::code2wav::quant_decode (ggml_tensor * inp_codes )const { 388const auto & c2w = model .c2w ; 389const int64_t T = inp_codes -> ne [0 ]; 390 391// ids for codebook group g over all T frames, [T] I32 392auto group_ids = [& ](int g ) { 393return ggml_view_1d (ctx0 ,inp_codes ,T , (size_t )g * inp_codes -> nb [1 ]); 394 }; 395 396ggml_tensor * sem = ggml_get_rows (ctx0 ,c2w .quant_first_cb_w ,group_ids (0 ));// [256, T] 397ggml_tensor * sem_out = ggml_mul_mat (ctx0 ,c2w .quant_first_out_w ,sem );// [512, T] 398 399ggml_tensor * acc = nullptr ; 400const int64_t n_acoustic = c2w .quant_rest_cb_w -> ne [2 ]; 401for (int g = 1 ;g <=n_acoustic ;g ++ ) { 402ggml_tensor * cb_g = ggml_view_2d (ctx0 ,c2w .quant_rest_cb_w ,c2w .quant_rest_cb_w -> ne [0 ],c2w .quant_rest_cb_w -> ne [1 ], 403c2w .quant_rest_cb_w -> nb [1 ], (size_t ) (g - 1 )* c2w .quant_rest_cb_w -> nb [2 ]); 404ggml_tensor * embd = ggml_get_rows (ctx0 ,cb_g ,group_ids (g ));// [256, T] 405acc = acc ?ggml_add (ctx0 ,acc ,embd ) :embd ; 406 } 407ggml_tensor * ac_out = ggml_mul_mat (ctx0 ,c2w .quant_rest_out_w ,acc );// [512, T] 408 409ggml_tensor * hidden = ggml_add (ctx0 ,sem_out ,ac_out ); 410cb (hidden ,"wav_quant_hidden" ,-1 ); 411return hidden ; 412} 413 414// one pre_transformer layer over a batch of N = sliding_window new frames 415// attention runs over [(W-1)-frame prefix from the last batch] + [N new frames] 416// RoPE positions come from a persisted counter, so phases line up across batches 417ggml_tensor * clip_graph_qwen3tts_gen::code2wav::tfm_layer_forward (ggml_tensor * cur ,const clip_layer & layer ,int il )const { 418const int n_head = hparams .wav_tfm_n_head ; 419const int n_head_kv = hparams .wav_tfm_n_head_kv ; 420const int64_t d_head = layer .q_w -> ne [1 ] /n_head ; 421const float kq_scale = 1.0f /sqrtf ((float )d_head ); 422const int64_t W = hparams .wav_tfm_swa ;// == N, frames per batch 423const int64_t N = cur -> ne [1 ]; 424const int64_t prefix = W - 1 ; 425const int64_t total_kv = prefix + N ; 426 427ggml_tensor * residual = cur ; 428ggml_tensor * h = ggml_rms_norm (ctx0 ,cur ,hparams .wav_tfm_eps ); 429h = ggml_mul (ctx0 ,h ,layer .ln_1_w ); 430 431ggml_tensor * q = ggml_mul_mat (ctx0 ,layer .q_w ,h );// [n_head*d_head, N] 432ggml_tensor * k = ggml_mul_mat (ctx0 ,layer .k_w ,h );// [n_head_kv*d_head, N] 433ggml_tensor * v = ggml_mul_mat (ctx0 ,layer .v_w ,h );// [n_head_kv*d_head, N] 434 435q = ggml_reshape_3d (ctx0 ,q ,d_head ,n_head ,N ); 436k = ggml_reshape_3d (ctx0 ,k ,d_head ,n_head_kv ,N ); 437 438// real, ever-increasing positions: base (persisted) .. base+N-1 439ggml_tensor * base = ggml_reshape_1d (ctx0 ,state_in .at ("tfm_pos" ),1 ); 440ggml_tensor * offset = ggml_arange (ctx0 ,0.0f , (float )N ,1.0f ); 441ggml_tensor * pos = ggml_cast (ctx0 ,ggml_add (ctx0 ,offset ,base ),GGML_TYPE_I32 ); 442 443q = ggml_rope_ext (ctx0 ,q ,pos ,nullptr , (int )d_head ,GGML_ROPE_TYPE_NEOX ,0 , 444hparams .wav_tfm_rope_theta ,1.0f ,0.0f ,1.0f ,0.0f ,0.0f ); 445k = ggml_rope_ext (ctx0 ,k ,pos ,nullptr , (int )d_head ,GGML_ROPE_TYPE_NEOX ,0 , 446hparams .wav_tfm_rope_theta ,1.0f ,0.0f ,1.0f ,0.0f ,0.0f ); 447 448// the position counter is the same for all layers, push it once from layer 0 449if (il == 0 ) { 450state_out .push_back ({"tfm_pos" ,ggml_scale_bias (ctx0 ,state_in .at ("tfm_pos" ),1.0f , (float )N )}); 451 } 452 453ggml_tensor * k_new = ggml_reshape_2d (ctx0 ,k ,d_head * n_head_kv ,N ); 454ggml_tensor * v_new = ggml_reshape_2d (ctx0 ,v ,d_head * n_head_kv ,N ); 455 456ggml_tensor * old_k = state_in .at ("tfm_k_" + std::to_string (il ));// [d_head*n_head_kv, W-1] 457ggml_tensor * old_v = state_in .at ("tfm_v_" + std::to_string (il )); 458 459ggml_tensor * k_full = ggml_concat (ctx0 ,old_k ,k_new ,1 );// [.., prefix+N] 460ggml_tensor * v_full = ggml_concat (ctx0 ,old_v ,v_new ,1 ); 461 462// next batch's prefix: the last (W-1) frames of this batch 463state_out .push_back ({"tfm_k_" + std::to_string (il ), 464ggml_cont (ctx0 ,ggml_view_2d (ctx0 ,k_full ,k_full -> ne [0 ],prefix ,k_full -> nb [1 ], (size_t )N * k_full -> nb [1 ]))}); 465state_out .push_back ({"tfm_v_" + std::to_string (il ), 466ggml_cont (ctx0 ,ggml_view_2d (ctx0 ,v_full ,v_full -> ne [0 ],prefix ,v_full -> nb [1 ], (size_t )N * v_full -> nb [1 ]))}); 467 468// banded causal mask: key j is visible to query i iff 0 <= (prefix+i) - j < W 469ggml_tensor * pos_k = ggml_reshape_2d (ctx0 ,ggml_arange (ctx0 ,0.0f , (float )total_kv ,1.0f ),total_kv ,1 ); 470ggml_tensor * pos_q = ggml_reshape_2d (ctx0 ,ggml_arange (ctx0 , (float )prefix , (float ) (prefix + N ),1.0f ),1 ,N ); 471ggml_tensor * pos_q_grid = ggml_repeat_4d (ctx0 ,pos_q ,total_kv ,N ,1 ,1 ); 472ggml_tensor * diff = ggml_sub (ctx0 ,pos_q_grid ,pos_k );// [total_kv, N] 473 474ggml_tensor * causal_keep = ggml_step (ctx0 ,ggml_scale_bias (ctx0 ,diff ,1.0f ,0.5f ));// diff >= 0 475ggml_tensor * in_window = ggml_step (ctx0 ,ggml_scale_bias (ctx0 ,diff ,-1.0f , (float )W - 0.5f ));// diff < W 476ggml_tensor * keep = ggml_mul (ctx0 ,causal_keep ,in_window ); 477 478// on a cold start, key j is real state only when j >= prefix - tfm_pos, mask out the rest 479ggml_tensor * warm = ggml_step (ctx0 ,ggml_scale_bias (ctx0 ,ggml_add (ctx0 ,pos_k ,base ), 4801.0f ,0.5f - (float )prefix ));// j + pos > prefix - 0.5 481keep = ggml_mul (ctx0 ,keep ,warm ); 482 483ggml_tensor * mask = ggml_reshape_4d (ctx0 ,ggml_log (ctx0 ,keep ),total_kv ,N ,1 ,1 );// 0 = keep, -inf = masked 484 485ggml_tensor * q_cur = ggml_reshape_4d (ctx0 ,q ,d_head ,n_head ,N ,1 ); 486ggml_tensor * k_cur = ggml_reshape_4d (ctx0 ,k_full ,d_head ,n_head_kv ,total_kv ,1 ); 487ggml_tensor * v_cur = ggml_reshape_4d (ctx0 ,v_full ,d_head ,n_head_kv ,total_kv ,1 ); 488 489ggml_tensor * attn_out = build_attn (layer .o_w ,layer .o_b ,q_cur ,k_cur ,v_cur ,mask ,kq_scale ,il ); 490if (layer .ls_1_w ) { 491attn_out = ggml_mul (ctx0 ,attn_out ,layer .ls_1_w ); 492 } 493cur = ggml_add (ctx0 ,residual ,attn_out ); 494 495ggml_tensor * residual2 = cur ; 496ggml_tensor * h2 = ggml_rms_norm (ctx0 ,cur ,hparams .wav_tfm_eps ); 497h2 = ggml_mul (ctx0 ,h2 ,layer .ln_2_w ); 498 499ggml_tensor * gate = ggml_mul_mat (ctx0 ,layer .ff_gate_w ,h2 ); 500ggml_tensor * up = ggml_mul_mat (ctx0 ,layer .ff_up_w ,h2 ); 501ggml_tensor * gu = ggml_swiglu_split (ctx0 ,gate ,up ); 502ggml_tensor * down = ggml_mul_mat (ctx0 ,layer .ff_down_w ,gu ); 503if (layer .ls_2_w ) { 504down = ggml_mul (ctx0 ,down ,layer .ls_2_w ); 505 } 506return ggml_add (ctx0 ,residual2 ,down ); 507} 508 509// dwconv -> LayerNorm -> pwconv1 -> GELU -> pwconv2 -> layer scale -> residual 510// x: [T, C] T-first; LayerNorm/pwconv need C on ne0, so this transposes in and back out 511ggml_tensor * clip_graph_qwen3tts_gen::code2wav::convnext_block (ggml_tensor * x ,const clip_code2wav::upsample_block & blk ,const std::string & state_prefix )const { 512ggml_tensor * residual = x ; 513 514ggml_tensor * h = causal_conv1d_dw (x ,blk .dwconv_w ,blk .dwconv_b ,state_prefix + "_dwconv" );// [T, C] 515ggml_tensor * hc = ggml_cont (ctx0 ,ggml_transpose (ctx0 ,h ));// [C, T] 516 517hc = ggml_norm (ctx0 ,hc ,1e-6f ); 518hc = ggml_mul (ctx0 ,hc ,blk .norm_w ); 519hc = ggml_add (ctx0 ,hc ,blk .norm_b ); 520 521ggml_tensor * g = ggml_mul_mat (ctx0 ,blk .pw1_w ,hc ); 522g = ggml_add (ctx0 ,g ,blk .pw1_b ); 523g = ggml_gelu (ctx0 ,g ); 524g = ggml_mul_mat (ctx0 ,blk .pw2_w ,g ); 525g = ggml_add (ctx0 ,g ,blk .pw2_b ); 526g = ggml_mul (ctx0 ,g ,blk .gamma ); 527 528ggml_tensor * g_t = ggml_cont (ctx0 ,ggml_transpose (ctx0 ,g ));// back to [T, C] 529return ggml_add (ctx0 ,residual ,g_t ); 530} 531 532// SnakeBeta -> dilated causal conv (k=7) -> SnakeBeta -> pointwise causal conv (k=1) -> residual. 533// x: [T, C]. returns [T, C]. 534ggml_tensor * clip_graph_qwen3tts_gen::code2wav::dac_res_unit (ggml_tensor * x ,const clip_code2wav::dac_res & res ,int dilation ,const std::string & state_name )const { 535ggml_tensor * residual = x ; 536ggml_tensor * h = snake (x ,res .act1_alpha ,res .act1_beta ); 537h = causal_conv1d (h ,res .conv1_w ,res .conv1_b ,dilation ,state_name ); 538h = snake (h ,res .act2_alpha ,res .act2_beta ); 539h = causal_conv1d (h ,res .conv2_w ,res .conv2_b ,1 ,"" );// k=1, no left-context needed 540return ggml_add (ctx0 ,residual ,h ); 541} 542 543// RVQ codes -> raw PCM for a batch of N = sliding_window frames 544ggml_tensor * clip_graph_qwen3tts_gen::code2wav::decode (ggml_tensor * inp_codes )const { 545const auto & c2w = model .c2w ; 546 547// 1. quantizer decode: N frames of 16 codes -> [512, N] (C-first) 548ggml_tensor * hidden = quant_decode (inp_codes ); 549 550// 2. pre_conv: [512, N] -> T-first [N, 512] -> causal conv k=3 -> [N, 1024] 551ggml_tensor * x = ggml_cont (ctx0 ,ggml_transpose (ctx0 ,hidden ));// [N, 512] 552x = causal_conv1d (x ,c2w .pre_conv_w ,c2w .pre_conv_b ,1 ,"pre_conv" );// [N, 1024] 553cb (x ,"wav_pre_conv_out" ,-1 ); 554 555// 3. pre_transformer: back to C-first [1024, N], project down, run the layers, project back up 556ggml_tensor * cur = ggml_cont (ctx0 ,ggml_transpose (ctx0 ,x ));// [1024, N] 557cur = ggml_mul_mat (ctx0 ,c2w .tfm_in_proj_w ,cur ); 558cur = ggml_add (ctx0 ,cur ,c2w .tfm_in_proj_b );// [512 (tfm hidden), N] 559 560for (int il = 0 ;il < hparams .wav_tfm_n_layer ;il ++ ) { 561cur = tfm_layer_forward (cur ,c2w .tfm_layers [il ],il ); 562 } 563 564cur = ggml_rms_norm (ctx0 ,cur ,hparams .wav_tfm_eps ); 565cur = ggml_mul (ctx0 ,cur ,c2w .tfm_output_norm_w ); 566cur = ggml_mul_mat (ctx0 ,c2w .tfm_out_proj_w ,cur ); 567cur = ggml_add (ctx0 ,cur ,c2w .tfm_out_proj_b );// [1024, N] 568cb (cur ,"wav_tfm_out" ,-1 ); 569 570// 4. upsample: 2x (causal ConvTranspose1d, stride 2 + ConvNeXt block), back to T-first 571// kernel == stride here, so there is no overlap tail to persist 572x = ggml_cont (ctx0 ,ggml_transpose (ctx0 ,cur ));// [N, 1024] 573for (size_t il = 0 ;il < c2w .upsample .size ();il ++ ) { 574const auto & up = c2w .upsample [il ]; 575x = causal_conv_transpose1d (x ,up .conv_w ,up .conv_b ,2 ,"" ); 576x = convnext_block (x ,up ,"up" + std::to_string (il )); 577cb (x ,"wav_upsample_out" , (int )il ); 578 } 579 580// 5. DAC decoder: conv_pre -> n blocks (SnakeBeta -> ConvTranspose1d -> 3 res units) -> conv_post 581static constexpr int DAC_DILATIONS [3 ]= {1 ,3 ,9 }; 582 583x = causal_conv1d (x ,c2w .dac_entry_w ,c2w .dac_entry_b ,1 ,"dac_entry" ); 584cb (x ,"wav_dac_entry_out" ,-1 ); 585 586for (size_t il = 0 ;il < c2w .dac .size ();il ++ ) { 587const auto & blk = c2w .dac [il ]; 588const int stride = (int ) (blk .conv_w -> ne [0 ] /2 );// kernel == 2*stride for all 4 blocks 589const std::string blk_name = "dac" + std::to_string (il ); 590x = snake (x ,blk .snake_alpha ,blk .snake_beta ); 591x = causal_conv_transpose1d (x ,blk .conv_w ,blk .conv_b ,stride ,blk_name + "_tail" ); 592for (size_t ir = 0 ;ir < blk .res .size ();ir ++ ) { 593x = dac_res_unit (x ,blk .res [ir ],DAC_DILATIONS [ir ],blk_name + "_res" + std::to_string (ir )); 594 } 595cb (x ,"wav_dac_block_out" , (int )il ); 596 } 597 598x = snake (x ,c2w .dac_post_snake_alpha ,c2w .dac_post_snake_beta ); 599x = causal_conv1d (x ,c2w .dac_post_conv_w ,c2w .dac_post_conv_b ,1 ,"dac_post_conv" );// [n_samples, 1] 600 601x = ggml_clamp (ctx0 ,x ,-1.0f ,1.0f ); 602x = ggml_reshape_1d (ctx0 ,x ,x -> ne [0 ]); 603cb (x ,"wav_audio_out" ,-1 ); 604return x ; 605} 606 607// code2wav's persisted state buffers: RoPE position counter, K/V per pre_transformer layer, 608// left-context/tail per stateful conv. shape lookup only, no graph needed 609std::vector < c2w_state_slot > list_c2w_state_slots (const clip_hparams & hparams ,const clip_model & model ) { 610const auto & c2w = model .c2w ; 611 std::vector < c2w_state_slot > slots ; 612 613if (c2w .pre_conv_w == nullptr ) { 614return slots ;// not a code2wav model, it keeps no state between calls 615 } 616 617slots .push_back ({"tfm_pos" ,1 ,1 }); 618 619// prefix is (W-1) frames, the batch itself gives the other N=W frames (see tfm_layer_forward) 620const int64_t d_head = c2w .tfm_layers [0 ].q_w -> ne [1 ] /hparams .wav_tfm_n_head ; 621const int64_t kv_ch = d_head * hparams .wav_tfm_n_head_kv ; 622const int64_t prefix = hparams .wav_tfm_swa - 1 ; 623for (int il = 0 ;il < hparams .wav_tfm_n_layer ;il ++ ) { 624slots .push_back ({"tfm_k_" + std::to_string (il ),kv_ch ,prefix }); 625slots .push_back ({"tfm_v_" + std::to_string (il ),kv_ch ,prefix }); 626 } 627 628slots .push_back ({"pre_conv" ,c2w .pre_conv_w -> ne [0 ]- 1 ,c2w .pre_conv_w -> ne [1 ]}); 629 630for (size_t il = 0 ;il < c2w .upsample .size ();il ++ ) { 631const auto & up = c2w .upsample [il ]; 632slots .push_back ({"up" + std::to_string (il )+ "_dwconv" ,up .dwconv_w -> ne [0 ]- 1 ,up .dwconv_w -> ne [2 ]}); 633 } 634 635slots .push_back ({"dac_entry" ,c2w .dac_entry_w -> ne [0 ]- 1 ,c2w .dac_entry_w -> ne [1 ]}); 636 637static constexpr int DAC_DILATIONS [3 ]= {1 ,3 ,9 }; 638for (size_t il = 0 ;il < c2w .dac .size ();il ++ ) { 639const auto & blk = c2w .dac [il ]; 640const int64_t stride = blk .conv_w -> ne [0 ] /2 ;// kernel == 2*stride for all 4 blocks 641const std::string blk_name = "dac" + std::to_string (il ); 642slots .push_back ({blk_name + "_tail" ,stride ,blk .conv_w -> ne [1 ]}); 643for (size_t ir = 0 ;ir < blk .res .size ();ir ++ ) { 644const auto & res = blk .res [ir ]; 645slots .push_back ({blk_name + "_res" + std::to_string (ir ), 646 (res .conv1_w -> ne [0 ]- 1 )* DAC_DILATIONS [ir ],res .conv1_w -> ne [1 ]}); 647 } 648 } 649 650slots .push_back ({"dac_post_conv" ,c2w .dac_post_conv_w -> ne [0 ]- 1 ,c2w .dac_post_conv_w -> ne [1 ]}); 651 652return slots ; 653} 654 655// both sub-graphs are always built, so the topology stays constant 656// ggml_build_forward_select() then picks the one that actually runs 657ggml_cgraph * clip_graph_qwen3tts_gen::build () { 658GGML_ASSERT (n_batch == 1 );// this module only ever processes one frame at a time 659 660int idx ; 661switch (gen_process ) { 662case CLIP_GEN_PROCESS_GEN_CODE :idx = 0 ;break ; 663case CLIP_GEN_PROCESS_GEN_WAV :idx = 1 ;break ; 664default :GGML_ABORT ("unknown gen_process" ); 665 } 666 667// ---- CLIP_GEN_PROCESS_GEN_CODE: backbone hidden state -> 16 RVQ codes + next-step embd ---- 668// not build_inp_raw(), a GEN_WAV call's `img` has no hidden-state data 669ggml_tensor * h_state = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_F32 ,n_mmproj_embd ); 670ggml_set_name (h_state ,"inp_raw" );// must keep this exact name, clip_encode() sets it by name 671ggml_set_input (h_state ); 672 673ggml_tensor * code0 = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,1 ); 674ggml_set_name (code0 ,"inp_code0" ); 675ggml_set_input (code0 ); 676 677ggml_tensor * code0_embd = ggml_get_rows (ctx0 ,model .gen_code_out_embd_w ,code0 ); 678code0_embd = ggml_reshape_1d (ctx0 ,code0_embd ,code0_embd -> ne [0 ]); 679cb (code0_embd ,"code0_embd" ,-1 ); 680 681const int64_t n_acoustic = model .gen_code_head_w -> ne [2 ];// 15 682const int n_codes = (int )n_acoustic + 1 ;// 16 683const int64_t n_kv_pad = n_codes ; 684const int n_layer = (int )model .layers .size (); 685const int n_head = hparams .n_head ; 686const int n_head_kv = hparams .n_head_kv ; 687const int64_t d_head = model .layers [0 ].q_w -> ne [1 ] /n_head ; 688 689// zero-filled per layer k/v caches, so masked-out rows can't hold garbage 690 std::vector < ggml_tensor *> k_cache (n_layer ),v_cache (n_layer ); 691for (int il = 0 ;il < n_layer ;il ++ ) { 692k_cache [il ]= ggml_fill (ctx0 ,ggml_new_tensor_2d (ctx0 ,GGML_TYPE_F32 ,d_head * n_head_kv ,n_kv_pad ),0.0f ); 693v_cache [il ]= ggml_fill (ctx0 ,ggml_new_tensor_2d (ctx0 ,GGML_TYPE_F32 ,d_head * n_head_kv ,n_kv_pad ),0.0f ); 694 } 695 696code_gen cg (* this ,top_k ,top_p ); 697 698ggml_tensor * out_code_cache = ggml_new_tensor_2d (ctx0 ,GGML_TYPE_I32 ,1 ,n_codes ); 699out_code_cache = cg .cache_set (out_code_cache ,0 ,code0 ); 700 701ggml_tensor * inp_rand0 = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_F32 ,1 ); 702ggml_set_name (inp_rand0 ,"inp_rand_0" ); 703ggml_set_input (inp_rand0 ); 704 705cg .prefill (k_cache ,v_cache ,out_code_cache ,h_state ,code0_embd ,inp_rand0 ); 706 707for (int g = 1 ;g < n_acoustic ;g ++ ) { 708ggml_tensor * inp_rand = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_F32 ,1 ); 709ggml_set_name (inp_rand , ("inp_rand_" + std::to_string (g )).c_str ()); 710ggml_set_input (inp_rand ); 711out_code_cache = cg .step (k_cache ,v_cache ,out_code_cache ,inp_rand ,g ); 712 } 713 714// output 1: this frame's 16 sampled codes, for the caller's code2wav window 715ggml_tensor * out_codes = ggml_cont (ctx0 ,out_code_cache ); 716ggml_set_name (out_codes ,"out_codes" ); 717ggml_set_output (out_codes ); 718 719// output 2: sum of all 16 codebook embeddings, fed back to the talker for the next frame 720ggml_tensor * out_embd = code0_embd ; 721for (int g = 1 ;g <=n_acoustic ;g ++ ) { 722ggml_tensor * code_g = ggml_view_1d (ctx0 ,out_code_cache ,1 , (size_t )g * out_code_cache -> nb [1 ]); 723 724ggml_tensor * embd_g = ggml_view_2d (ctx0 ,model .gen_code_embd_w ,model .gen_code_embd_w -> ne [0 ],model .gen_code_embd_w -> ne [1 ], 725model .gen_code_embd_w -> nb [1 ], (size_t ) (g - 1 )* model .gen_code_embd_w -> nb [2 ]); 726ggml_tensor * e = ggml_get_rows (ctx0 ,embd_g ,code_g ); 727e = ggml_reshape_1d (ctx0 ,e ,e -> ne [0 ]); 728 729out_embd = ggml_add (ctx0 ,out_embd ,e ); 730 } 731out_embd = ggml_reshape_2d (ctx0 ,out_embd ,out_embd -> ne [0 ],1 ); 732cb (out_embd ,"gen_audio_out" ,-1 ); 733 734// ---- CLIP_GEN_PROCESS_GEN_WAV: 16 RVQ codes -> raw PCM ---- 735const int n_frames = hparams .wav_tfm_swa ;// frames per batch, == the attention window 736 737ggml_tensor * inp_codes = ggml_new_tensor_2d (ctx0 ,GGML_TYPE_I32 ,n_frames ,n_codes ); 738ggml_set_name (inp_codes ,"inp_codes" ); 739ggml_set_input (inp_codes ); 740 741code2wav c2w (* this ); 742for (const auto & slot :list_c2w_state_slots (hparams ,model )) { 743ggml_tensor * t = ggml_new_tensor_2d (ctx0 ,GGML_TYPE_F32 ,slot .ne0 ,slot .ne1 ); 744ggml_set_name (t , ("state_in_" + slot .name ).c_str ()); 745ggml_set_input (t ); 746c2w .state_in [slot .name ]= t ; 747 } 748 749ggml_tensor * out_audio = c2w .decode (inp_codes ); 750ggml_set_name (out_audio ,"out_audio" ); 751ggml_set_output (out_audio ); 752 753for (auto & slot :c2w .state_out ) { 754ggml_set_name (slot .second , ("state_out_" + slot .first ).c_str ()); 755ggml_set_output (slot .second ); 756 } 757 758// out_embd goes last, clip_encode() reads it back via ggml_graph_node(gf, -1) 759ggml_tensor * outs [2 ]; 760outs [0 ]= out_codes ;outs [1 ]= out_audio ; 761ggml_build_forward_select (gf ,outs ,2 ,idx ); 762for (auto & slot :c2w .state_out ) { 763outs [0 ]= out_codes ;outs [1 ]= slot .second ; 764ggml_build_forward_select (gf ,outs ,2 ,idx ); 765 } 766outs [0 ]= out_embd ;outs [1 ]= out_audio ; 767ggml_build_forward_select (gf ,outs ,2 ,idx ); 768 769return gf ; 770}