| 42 | } |
| 43 | |
| 44 | core::TensorValue SortformerMaskedSelfAttentionModule::build( |
| 45 | core::ModuleBuildContext & ctx, |
| 46 | const core::TensorValue & input, |
| 47 | const SortformerMaskedSelfAttentionWeights & weights, |
| 48 | const core::TensorValue & attention_mask) const { |
| 49 | core::validate_rank_between(input, 3, 3, "input"); |
| 50 | core::validate_last_dim(input, config_.hidden_size, "input"); |
| 51 | core::validate_rank_between(attention_mask, 2, 4, "attention_mask"); |
| 52 | const int64_t frames = input.shape.dims[1]; |
| 53 | if (attention_mask.shape.rank == 2 && |
| 54 | (attention_mask.shape.dims[0] != frames || attention_mask.shape.dims[1] != frames)) { |
| 55 | throw std::runtime_error("Sortformer 2D attention mask shape must be [frames, frames]"); |
| 56 | } |
| 57 | if (attention_mask.shape.rank == 4 && |
| 58 | (attention_mask.shape.dims[0] != input.shape.dims[0] || |
| 59 | attention_mask.shape.dims[1] != 1 || |
| 60 | attention_mask.shape.dims[2] != frames || |
| 61 | attention_mask.shape.dims[3] != frames)) { |
| 62 | throw std::runtime_error("Sortformer 4D attention mask shape must be [batch, 1, frames, frames]"); |
| 63 | } |
| 64 | |
| 65 | const int64_t head_dim = config_.hidden_size / config_.num_heads; |
| 66 | const float attn_scale = 1.0f / std::sqrt(std::sqrt(static_cast<float>(head_dim))); |
| 67 | |
| 68 | auto q = modules::LinearModule({config_.hidden_size, config_.hidden_size, weights.query.bias.has_value()}) |
| 69 | .build(ctx, input, weights.query); |
| 70 | auto k = modules::LinearModule({config_.hidden_size, config_.hidden_size, weights.key.bias.has_value()}) |
| 71 | .build(ctx, input, weights.key); |
| 72 | auto v = modules::LinearModule({config_.hidden_size, config_.hidden_size, weights.value.bias.has_value()}) |
| 73 | .build(ctx, input, weights.value); |
| 74 | |
| 75 | q = reshape_heads(ctx, q, config_.num_heads, head_dim); |
| 76 | k = reshape_heads(ctx, k, config_.num_heads, head_dim); |
| 77 | v = reshape_heads(ctx, v, config_.num_heads, head_dim); |
| 78 | |
| 79 | auto q_heads = modules::TransposeModule({{0, 2, 1, 3}, 4}).build(ctx, q); |
| 80 | auto k_heads = modules::TransposeModule({{0, 2, 1, 3}, 4}).build(ctx, k); |
| 81 | auto v_heads = modules::TransposeModule({{0, 2, 1, 3}, 4}).build(ctx, v); |
| 82 | |
| 83 | q_heads = ensure_contiguous(ctx, q_heads); |
| 84 | k_heads = ensure_contiguous(ctx, k_heads); |
| 85 | q_heads = core::wrap_tensor(ggml_scale(ctx.ggml, q_heads.tensor, attn_scale), q_heads.shape, GGML_TYPE_F32); |
| 86 | k_heads = core::wrap_tensor(ggml_scale(ctx.ggml, k_heads.tensor, attn_scale), k_heads.shape, GGML_TYPE_F32); |
| 87 | |
| 88 | auto k_transposed = modules::TransposeModule({{0, 1, 3, 2}, 4}).build(ctx, k_heads); |
| 89 | auto scores = modules::MatMulModule().build(ctx, q_heads, k_transposed); |
| 90 | auto attn = core::wrap_tensor( |
| 91 | ggml_soft_max_ext(ctx.ggml, ensure_contiguous(ctx, scores).tensor, attention_mask.tensor, 1.0f, 0.0f), |
| 92 | scores.shape, |
| 93 | GGML_TYPE_F32); |
| 94 | auto context = modules::MatMulModule().build(ctx, attn, v_heads); |
| 95 | context = modules::TransposeModule({{0, 2, 1, 3}, 4}).build(ctx, context); |
| 96 | context = ensure_contiguous(ctx, context); |
| 97 | context = core::reshape_tensor( |
| 98 | ctx, |
| 99 | context, |
| 100 | core::TensorShape::from_dims({input.shape.dims[0], input.shape.dims[1], config_.hidden_size})); |
| 101 | return modules::LinearModule({config_.hidden_size, config_.hidden_size, weights.output.bias.has_value()}) |
no test coverage detected