MCPcopy Create free account
hub / github.com/0xShug0/audio.cpp / build

Method build

src/framework/modules/attention/longformer_attention_gpu.cpp:392–510  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

390}
391
392core::TensorValue LongformerRelativeSelfAttentionGpuModule::build(
393 core::ModuleBuildContext & ctx,
394 const core::TensorValue & input,
395 const core::TensorValue & pos_emb,
396 const RelativeAttentionWeights & weights,
397 const core::TensorValue & keep_mask,
398 int64_t layer_index,
399 LongformerAttentionExecutionState * exec_state,
400 LongformerAttentionDebugTensors * debug,
401 const std::optional<core::TensorValue> & projected_pos_emb) const {
402 GGML_UNUSED(layer_index);
403 GGML_UNUSED(exec_state);
404 if (debug != nullptr) {
405 debug->input = input;
406 }
407
408 adetail::validate_sequence_input(input, config_.hidden_size, "longform_attn_gpu.input");
409 core::validate_rank_between(pos_emb, 3, 3, "longform_attn_gpu.pos_emb");
410 core::validate_rank_between(keep_mask, 2, 2, "longform_attn_gpu.keep_mask");
411
412 const int64_t batch = input.shape.dims[0];
413 const int64_t time = input.shape.dims[1];
414 const int64_t head_dim = config_.hidden_size / config_.num_heads;
415 const int64_t w = std::max(config_.left_context, config_.right_context);
416 const int64_t padded_time = time + ((2 * w - (time % (2 * w))) % (2 * w));
417 const int64_t start_pos = w - config_.left_context;
418 const int64_t end_pos = w + config_.right_context;
419 const int64_t cols = 2 * w + 1;
420 const float scale = 1.0f / std::sqrt(static_cast<float>(head_dim));
421
422 auto keep_mask_f32 = core::wrap_tensor(ggml_cast(ctx.ggml, keep_mask.tensor, GGML_TYPE_F32), keep_mask.shape, GGML_TYPE_F32);
423 auto keep_mask_padded = pad_axis(ctx, keep_mask_f32, 1, 0, padded_time - time);
424
425 auto q_proj = linear_native(ctx, input, weights.attention.q_weight, weights.attention.q_bias, config_.hidden_size);
426 auto k_proj = linear_native(ctx, input, weights.attention.k_weight, weights.attention.k_bias, config_.hidden_size);
427 auto v_proj = linear_native(ctx, input, weights.attention.v_weight, weights.attention.v_bias, config_.hidden_size);
428 if (padded_time > time) {
429 q_proj = pad_axis(ctx, q_proj, 1, 0, padded_time - time);
430 k_proj = pad_axis(ctx, k_proj, 1, 0, padded_time - time);
431 v_proj = pad_axis(ctx, v_proj, 1, 0, padded_time - time);
432 }
433 if (projected_pos_emb.has_value()) {
434 core::validate_rank_between(*projected_pos_emb, 3, 3, "longform_attn_gpu.projected_pos_emb");
435 core::validate_last_dim(*projected_pos_emb, config_.hidden_size, "longform_attn_gpu.projected_pos_emb");
436 if (projected_pos_emb->shape.dims[1] != pos_emb.shape.dims[1]) {
437 throw std::runtime_error("Longformer GPU projected_pos_emb frames must match pos_emb frames");
438 }
439 }
440 auto p_proj = projected_pos_emb.has_value()
441 ? *projected_pos_emb
442 : linear_native(ctx, pos_emb, weights.pos_weight, std::nullopt, config_.hidden_size);
443
444 auto q = adetail::permute_tensor(ctx, adetail::reshape_heads(ctx, q_proj, config_.num_heads, head_dim), {0, 2, 1, 3});
445 auto k = adetail::permute_tensor(ctx, adetail::reshape_heads(ctx, k_proj, config_.num_heads, head_dim), {0, 2, 1, 3});
446 auto v = adetail::permute_tensor(ctx, adetail::reshape_heads(ctx, v_proj, config_.num_heads, head_dim), {0, 2, 1, 3});
447 auto p = adetail::permute_tensor(ctx, adetail::reshape_heads(ctx, p_proj, config_.num_heads, head_dim), {0, 2, 1, 3});
448 if (p.shape.dims[0] == 1 && batch > 1) {
449 p = RepeatModule({core::TensorShape::from_dims({batch, p.shape.dims[1], p.shape.dims[2], p.shape.dims[3]})}).build(ctx, p);

Callers 3

slice_axisFunction · 0.45
concat_axisFunction · 0.45
repeat_to_shapeFunction · 0.45

Calls 15

validate_rank_betweenFunction · 0.85
maxFunction · 0.85
wrap_tensorFunction · 0.85
ggml_castFunction · 0.85
validate_last_dimFunction · 0.85
RepeatModuleClass · 0.85
ggml_addFunction · 0.85
concat_allFunction · 0.85
ggml_scaleFunction · 0.85
reshape_tensorFunction · 0.85
ggml_soft_maxFunction · 0.85
ensure_contiguous_layoutFunction · 0.85

Tested by

no test coverage detected