| 285 | core::TensorShape::from_dims({text_ids.shape.dims[0], text_ids.shape.dims[1], config.llm.hidden_size}); |
| 286 | auto audio_mask_expanded = modules::RepeatModule({broadcast_shape}).build(ctx, audio_mask); |
| 287 | auto text_mask_expanded = modules::RepeatModule({broadcast_shape}).build(ctx, text_mask); |
| 288 | auto text_part = modules::MulModule{}.build(ctx, text_embeds, text_mask_expanded); |
| 289 | auto audio_part = modules::MulModule{}.build(ctx, audio_embeds, audio_mask_expanded); |
| 290 | return modules::AddModule{}.build(ctx, text_part, audio_part); |
| 291 | } |
| 292 | |
| 293 | core::TensorValue decoder_layer( |
| 294 | core::ModuleBuildContext & ctx, |
| 295 | const core::TensorValue & input, |
| 296 | const core::TensorValue & positions, |
| 297 | const LayerWeights & weights, |
| 298 | const OmniVoiceLLMConfig & config, |
| 299 | const core::TensorValue & attention_mask, |
| 300 | OmniVoiceGeneratorPerfMode perf_mode = OmniVoiceGeneratorPerfMode::Standard) { |
| 301 | const int64_t dim = head_dim(config); |
| 302 | const int64_t kv_repeats = config.num_attention_heads / config.num_key_value_heads; |
| 303 | const modules::LinearModule q_proj( |
| 304 | binding::linear_config(config.hidden_size, config.num_attention_heads * dim, false)); |
| 305 | const modules::LinearModule k_proj( |
| 306 | binding::linear_config(config.hidden_size, config.num_key_value_heads * dim, false)); |
| 307 | const modules::LinearModule v_proj( |
| 308 | binding::linear_config(config.hidden_size, config.num_key_value_heads * dim, false)); |
| 309 | const modules::LinearModule o_proj( |
| 310 | binding::linear_config(config.num_attention_heads * dim, config.hidden_size, false)); |
| 311 | const modules::RMSNormModule hidden_norm({config.hidden_size, config.rms_norm_eps, true, false}); |
| 312 | const modules::RMSNormModule head_norm({dim, config.rms_norm_eps, true, false}); |
| 313 | auto x_norm = hidden_norm.build(ctx, input, binding::norm_data(ctx, weights.input_norm)); |
| 314 | auto q = q_proj.build(ctx, x_norm, binding::linear_data(ctx, weights.q_proj)); |
| 315 | auto k = k_proj.build(ctx, x_norm, binding::linear_data(ctx, weights.k_proj)); |
| 316 | auto v = v_proj.build(ctx, x_norm, binding::linear_data(ctx, weights.v_proj)); |
| 317 | q = head_norm.build(ctx, reshape_heads(ctx, q, config.num_attention_heads, dim), binding::norm_data(ctx, weights.q_norm)); |
| 318 | k = head_norm.build(ctx, reshape_heads(ctx, k, config.num_key_value_heads, dim), binding::norm_data(ctx, weights.k_norm)); |
| 319 | v = reshape_heads(ctx, v, config.num_key_value_heads, dim); |
| 320 | q = modules::RoPEModule({dim, GGML_ROPE_TYPE_NEOX, config.rope_theta}).build(ctx, q, positions); |
| 321 | k = modules::RoPEModule({dim, GGML_ROPE_TYPE_NEOX, config.rope_theta}).build(ctx, k, positions); |
| 322 | |
| 323 | auto q_heads = modules::TransposeModule({{0, 2, 1, 3}, q.shape.rank}).build(ctx, q); |
| 324 | auto k_heads = repeat_kv_heads(ctx, modules::TransposeModule({{0, 2, 1, 3}, k.shape.rank}).build(ctx, k), kv_repeats); |
| 325 | auto v_heads = repeat_kv_heads(ctx, modules::TransposeModule({{0, 2, 1, 3}, v.shape.rank}).build(ctx, v), kv_repeats); |
| 326 | auto context = attention_from_heads(ctx, q_heads, k_heads, v_heads, dim, attention_mask, perf_mode); |
| 327 | context = ensure_contiguous(ctx, context); |
| 328 | context = core::reshape_tensor( |
| 329 | ctx, |
| 330 | context, |
| 331 | core::TensorShape::from_dims({input.shape.dims[0], input.shape.dims[1], config.num_attention_heads * dim})); |
| 332 | auto x = modules::AddModule{}.build(ctx, input, o_proj.build(ctx, context, binding::linear_data(ctx, weights.o_proj))); |
| 333 | |
| 334 | auto ff_in = hidden_norm.build(ctx, x, binding::norm_data(ctx, weights.post_norm)); |
| 335 | auto gate = modules::LinearModule( |
| 336 | binding::linear_config(config.hidden_size, config.intermediate_size, false)) |
| 337 | .build(ctx, ff_in, binding::linear_data(ctx, weights.gate_proj)); |
| 338 | gate = modules::SiluModule{}.build(ctx, gate); |
| 339 | auto up = modules::LinearModule( |
| 340 | binding::linear_config(config.hidden_size, config.intermediate_size, false)) |
| 341 | .build(ctx, ff_in, binding::linear_data(ctx, weights.up_proj)); |
| 342 | auto ff = modules::LinearModule( |
| 343 | binding::linear_config(config.intermediate_size, config.hidden_size, false)) |
no test coverage detected