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

Function build_self_attention

src/framework/modules/hubert_encoder.cpp:191–232  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

189 const core::TensorValue & hidden_btc,
190 const HubertEncoderWeights & weights,
191 int64_t layer_index) {
192 const auto & config = weights.config;
193 const int64_t head_dim = config.hidden_size / config.num_attention_heads;
194 const std::string prefix = "encoder.layers." + std::to_string(layer_index) + ".attention";
195 auto q = LinearModule({config.hidden_size, config.hidden_size, true, GGML_PREC_F32})
196 .build(ctx, hidden_btc, linear_weights(weights, prefix + ".q_proj"));
197 auto k = LinearModule({config.hidden_size, config.hidden_size, true, GGML_PREC_F32})
198 .build(ctx, hidden_btc, linear_weights(weights, prefix + ".k_proj"));
199 auto v = LinearModule({config.hidden_size, config.hidden_size, true, GGML_PREC_F32})
200 .build(ctx, hidden_btc, linear_weights(weights, prefix + ".v_proj"));
201 q = core::reshape_tensor(
202 ctx,
203 contiguous(ctx, q),
204 core::TensorShape::from_dims({hidden_btc.shape.dims[0], hidden_btc.shape.dims[1], config.num_attention_heads, head_dim}));
205 k = core::reshape_tensor(
206 ctx,
207 contiguous(ctx, k),
208 core::TensorShape::from_dims({hidden_btc.shape.dims[0], hidden_btc.shape.dims[1], config.num_attention_heads, head_dim}));
209 v = core::reshape_tensor(
210 ctx,
211 contiguous(ctx, v),
212 core::TensorShape::from_dims({hidden_btc.shape.dims[0], hidden_btc.shape.dims[1], config.num_attention_heads, head_dim}));
213 q = TransposeModule({{0, 2, 1, 3}, 4}).build(ctx, q);
214 k = TransposeModule({{0, 2, 1, 3}, 4}).build(ctx, k);
215 v = TransposeModule({{0, 2, 1, 3}, 4}).build(ctx, v);
216 const auto k_t = TransposeModule({{0, 1, 3, 2}, 4}).build(ctx, k);
217 auto scores = MatMulModule().build(ctx, q, k_t);
218 scores = scale(ctx, scores, static_cast<float>(1.0 / std::sqrt(static_cast<double>(head_dim))));
219 auto attn = core::wrap_tensor(ggml_soft_max(ctx.ggml, contiguous(ctx, scores).tensor), scores.shape, GGML_TYPE_F32);
220 auto context = MatMulModule().build(ctx, attn, v);
221 context = TransposeModule({{0, 2, 1, 3}, 4}).build(ctx, context);
222 context = core::reshape_tensor(
223 ctx,
224 contiguous(ctx, context),
225 core::TensorShape::from_dims({hidden_btc.shape.dims[0], hidden_btc.shape.dims[1], config.hidden_size}));
226 return LinearModule({config.hidden_size, config.hidden_size, true, GGML_PREC_F32})
227 .build(ctx, context, linear_weights(weights, prefix + ".out_proj"));
228}
229
230core::TensorValue build_feed_forward(
231 core::ModuleBuildContext & ctx,
232 const core::TensorValue & hidden_btc,
233 const HubertEncoderWeights & weights,
234 int64_t layer_index) {
235 const auto & config = weights.config;

Callers 1

build_hubert_graphFunction · 0.70

Calls 11

LinearModuleClass · 0.85
reshape_tensorFunction · 0.85
TransposeModuleClass · 0.85
MatMulModuleClass · 0.85
wrap_tensorFunction · 0.85
ggml_soft_maxFunction · 0.85
linear_weightsFunction · 0.70
contiguousFunction · 0.70
scaleFunction · 0.70
to_stringFunction · 0.50
buildMethod · 0.45

Tested by

no test coverage detected