| 332 | .build(ctx, input, weights.input_norm); |
| 333 | auto q = modules::LinearModule({config.hidden_size, config.num_attention_heads * dim, weights.q_proj.bias.has_value()}) |
| 334 | .build(ctx, x_norm, weights.q_proj); |
| 335 | auto k = modules::LinearModule({config.hidden_size, config.num_key_value_heads * dim, weights.k_proj.bias.has_value()}) |
| 336 | .build(ctx, x_norm, weights.k_proj); |
| 337 | auto v = modules::LinearModule({config.hidden_size, config.num_key_value_heads * dim, weights.v_proj.bias.has_value()}) |
| 338 | .build(ctx, x_norm, weights.v_proj); |
| 339 | q = modules::RoPEModule({dim, GGML_ROPE_TYPE_NEOX, config.rope_theta}).build(ctx, reshape_heads(ctx, q, config.num_attention_heads, dim), positions); |
| 340 | k = modules::RoPEModule({dim, GGML_ROPE_TYPE_NEOX, config.rope_theta}).build(ctx, reshape_heads(ctx, k, config.num_key_value_heads, dim), positions); |
| 341 | v = reshape_heads(ctx, v, config.num_key_value_heads, dim); |
| 342 | |
| 343 | auto key_tail = cache_view(ctx, cache_key, scratch_slot, 1, config.num_key_value_heads, dim); |
| 344 | auto value_tail = cache_view(ctx, cache_value, scratch_slot, 1, config.num_key_value_heads, dim); |
| 345 | ggml_build_forward_expand(graph, ggml_cpy(ctx.ggml, k.tensor, key_tail.tensor)); |
| 346 | ggml_build_forward_expand(graph, ggml_cpy(ctx.ggml, v.tensor, value_tail.tensor)); |
| 347 | |
| 348 | auto q_heads = modules::TransposeModule({{0, 2, 1, 3}, q.shape.rank}).build(ctx, q); |
| 349 | q_heads = core::wrap_tensor(ggml_cont(ctx.ggml, q_heads.tensor), q_heads.shape, q_heads.type); |
| 350 | auto k_heads = modules::TransposeModule({{0, 2, 1, 3}, cache_key.shape.rank}).build(ctx, cache_key); |
| 351 | auto v_heads = modules::TransposeModule({{0, 2, 1, 3}, cache_value.shape.rank}).build(ctx, cache_value); |
| 352 | k_heads = core::wrap_tensor(ggml_cont(ctx.ggml, k_heads.tensor), k_heads.shape, k_heads.type); |
| 353 | v_heads = core::wrap_tensor(ggml_cont(ctx.ggml, v_heads.tensor), v_heads.shape, v_heads.type); |
| 354 | auto context = modules::GroupedQueryAttentionModule({ |
| 355 | dim, |
| 356 | modules::GroupedQueryAttentionLowering::FlashGrouped, |
| 357 | GGML_PREC_F32, |
| 358 | }).build(ctx, q_heads, k_heads, v_heads, attention_mask); |
| 359 | context = core::ensure_backend_addressable_layout(ctx, context); |
| 360 | context = core::reshape_tensor(ctx, context, core::TensorShape::from_dims({1, 1, config.num_attention_heads * dim})); |
| 361 | auto x = modules::AddModule{}.build( |
| 362 | ctx, |
| 363 | input, |
| 364 | modules::LinearModule({config.num_attention_heads * dim, config.hidden_size, weights.o_proj.bias.has_value()}) |
| 365 | .build(ctx, context, weights.o_proj)); |
| 366 | auto ff_in = modules::RMSNormModule({config.hidden_size, config.rms_norm_eps, true, false}) |
| 367 | .build(ctx, x, weights.post_norm); |
| 368 | return {modules::AddModule{}.build(ctx, x, mlp(ctx, ff_in, weights, config)), k, v}; |
| 369 | } |
| 370 | |
| 371 | bool is_eos(int32_t token, int32_t tokenizer_eos) { |
| 372 | return token == tokenizer_eos; |
| 373 | } |
| 374 | |
| 375 | void apply_repetition_penalty( |
no test coverage detected