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

Function attention

src/models/miocodec/graph_ops.cpp:370–419  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

368 const MioCodecTransformerLayerWeights & weights,
369 const MioCodecTransformerWeights & config,
370 bool exact_rope) {
371 const int64_t head_dim = config.head_dim;
372 const float scale = 1.0F / std::sqrt(static_cast<float>(head_dim));
373 auto qkv = engine::modules::LinearModule({config.dim, 3 * config.dim, weights.qkv_proj.bias.has_value()}).build(
374 ctx,
375 input,
376 weights.qkv_proj);
377 const auto qkv_axis = static_cast<int>(qkv.shape.rank - 1);
378 auto q = engine::modules::SliceModule({qkv_axis, 0, config.dim}).build(ctx, qkv);
379 auto k = engine::modules::SliceModule({qkv_axis, config.dim, config.dim}).build(ctx, qkv);
380 auto v = engine::modules::SliceModule({qkv_axis, 2 * config.dim, config.dim}).build(ctx, qkv);
381 q = core::reshape_tensor(ctx, core::ensure_backend_addressable_layout(ctx, q), core::TensorShape::from_dims({input.shape.dims[0], input.shape.dims[1], config.heads, head_dim}));
382 k = core::reshape_tensor(ctx, core::ensure_backend_addressable_layout(ctx, k), core::TensorShape::from_dims({input.shape.dims[0], input.shape.dims[1], config.heads, head_dim}));
383 v = core::reshape_tensor(ctx, core::ensure_backend_addressable_layout(ctx, v), core::TensorShape::from_dims({input.shape.dims[0], input.shape.dims[1], config.heads, head_dim}));
384 if (exact_rope) {
385 q = apply_rope_exact(ctx, constants, q, head_dim);
386 k = apply_rope_exact(ctx, constants, k, head_dim);
387 } else {
388 q = apply_rope_fast(ctx, q, positions, head_dim);
389 k = apply_rope_fast(ctx, k, positions, head_dim);
390 }
391 auto qh = engine::modules::TransposeModule({{0, 2, 1, 3}, q.shape.rank}).build(ctx, q);
392 auto kh = engine::modules::TransposeModule({{0, 2, 1, 3}, k.shape.rank}).build(ctx, k);
393 auto vh = engine::modules::TransposeModule({{0, 2, 1, 3}, v.shape.rank}).build(ctx, v);
394 auto scores = engine::modules::MatMulModule{}.build(
395 ctx,
396 qh,
397 engine::modules::TransposeModule({{0, 1, 3, 2}, kh.shape.rank}).build(ctx, kh));
398 auto attn = core::wrap_tensor(
399 ggml_soft_max_ext(ctx.ggml, core::ensure_backend_addressable_layout(ctx, scores).tensor, mask.tensor, scale, 0.0F),
400 scores.shape,
401 GGML_TYPE_F32);
402 auto context = engine::modules::MatMulModule{}.build(ctx, attn, vh);
403 context = core::ensure_backend_addressable_layout(
404 ctx,
405 engine::modules::TransposeModule({{0, 2, 1, 3}, context.shape.rank}).build(ctx, context));
406 context = core::reshape_tensor(ctx, context, core::TensorShape::from_dims({input.shape.dims[0], input.shape.dims[1], config.dim}));
407 return engine::modules::LinearModule({config.dim, config.dim, weights.out_proj.bias.has_value()}).build(
408 ctx,
409 context,
410 weights.out_proj);
411}
412
413std::pair<core::TensorValue, std::optional<core::TensorValue>> adaln(
414 core::ModuleBuildContext & ctx,
415 const core::TensorValue & input,
416 const core::TensorValue & condition,
417 const MioCodecAdaLayerNormWeights & weights,
418 int64_t dim,
419 bool return_gate) {
420 auto normalized = engine::modules::LayerNormModule({dim, 1.0e-5F, false, false}).build(ctx, input, {});
421 const int64_t out_dim = return_gate ? 3 * dim : 2 * dim;
422 auto params = engine::modules::LinearModule(

Callers 1

transformerFunction · 0.70

Calls 10

LinearModuleClass · 0.85
SliceModuleClass · 0.85
reshape_tensorFunction · 0.85
apply_rope_exactFunction · 0.85
apply_rope_fastFunction · 0.85
TransposeModuleClass · 0.85
wrap_tensorFunction · 0.85
ggml_soft_max_extFunction · 0.85
buildMethod · 0.45

Tested by

no test coverage detected