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

Function attention_from_heads

src/models/vibevoice/decoder.cpp:97–131  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

95}
96
97core::TensorValue attention_from_heads(
98 core::ModuleBuildContext & ctx,
99 const core::TensorValue & q_heads,
100 const core::TensorValue & k_heads,
101 const core::TensorValue & v_heads,
102 int64_t dim,
103 const std::optional<core::TensorValue> & attention_mask) {
104 const modules::MatMulModule matmul;
105 auto scores = matmul.build(
106 ctx,
107 q_heads,
108 modules::TransposeModule({{0, 1, 3, 2}, k_heads.shape.rank}).build(ctx, k_heads));
109 core::TensorValue attn;
110 if (attention_mask.has_value()) {
111 scores = core::ensure_backend_addressable_layout(ctx, scores);
112 attn = core::wrap_tensor(
113 ggml_soft_max_ext(
114 ctx.ggml,
115 scores.tensor,
116 attention_mask->tensor,
117 1.0F / std::sqrt(static_cast<float>(dim)),
118 0.0F),
119 scores.shape,
120 GGML_TYPE_F32);
121 } else {
122 scores = core::wrap_tensor(
123 ggml_scale(ctx.ggml, scores.tensor, 1.0F / std::sqrt(static_cast<float>(dim))),
124 scores.shape,
125 GGML_TYPE_F32);
126 scores = core::wrap_tensor(ggml_diag_mask_inf(ctx.ggml, scores.tensor, 0), scores.shape, GGML_TYPE_F32);
127 scores = core::ensure_backend_addressable_layout(ctx, scores);
128 attn = core::wrap_tensor(ggml_soft_max(ctx.ggml, scores.tensor), scores.shape, GGML_TYPE_F32);
129 }
130 return matmul.build(ctx, attn, v_heads);
131}
132
133core::TensorValue flash_attention_from_grouped_heads_view_kv(
134 core::ModuleBuildContext & ctx,

Calls 8

TransposeModuleClass · 0.85
wrap_tensorFunction · 0.85
ggml_soft_max_extFunction · 0.85
ggml_scaleFunction · 0.85
ggml_diag_mask_infFunction · 0.85
ggml_soft_maxFunction · 0.85
buildMethod · 0.45

Tested by

no test coverage detected