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

Method build

src/models/sortformer_diar/modules.cpp:44–103  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

42}
43
44core::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()})

Callers 15

compute_whisper_log_melFunction · 0.45
ensure_graphMethod · 0.45
repeat_likeFunction · 0.45
transpose_bct_btcFunction · 0.45
channel_layer_norm_bctFunction · 0.45
grn_btcFunction · 0.45
build_astral_projectedFunction · 0.45
addFunction · 0.45
mulFunction · 0.45
sigmoidFunction · 0.45
tanh_valueFunction · 0.45
slice_axisFunction · 0.45

Calls 15

validate_rank_betweenFunction · 0.85
validate_last_dimFunction · 0.85
LinearModuleClass · 0.85
TransposeModuleClass · 0.85
wrap_tensorFunction · 0.85
ggml_scaleFunction · 0.85
MatMulModuleClass · 0.85
ggml_soft_max_extFunction · 0.85
reshape_tensorFunction · 0.85
AddModuleClass · 0.85
LayerNormModuleClass · 0.85

Tested by

no test coverage detected