MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
HubertPhonemeModel.cpp
Go to the documentation of this file.
2
8
9namespace MayaFlux::Yantra {
10
11namespace {
12
16
17 constexpr uint32_t k_feature_layers = 7;
18 constexpr uint32_t k_features = 512;
19 constexpr uint32_t k_width = HubertPhonemeModel::hidden_width;
20 constexpr uint32_t k_expanded = 3072;
21 constexpr uint32_t k_blocks = 12;
22 constexpr uint32_t k_heads = 12;
23 constexpr uint32_t k_head_width = k_width / k_heads;
24 constexpr uint32_t k_position_kernel = 128;
25 constexpr uint32_t k_position_groups = 16;
26 constexpr uint32_t k_classes = HubertPhonemeModel::classes;
27 constexpr float k_norm_epsilon = 1.0e-5F;
28 constexpr double k_input_epsilon = 1.0e-7;
29
30 constexpr std::array<uint32_t, k_feature_layers> k_feature_kernels { 10, 3, 3, 3, 3, 2, 2 };
31 constexpr std::array<uint32_t, k_feature_layers> k_feature_strides { 5, 2, 2, 2, 2, 2, 2 };
32
33 constexpr std::array<std::string_view, k_classes> k_labels {
34 "AA", "AE", "AH", "AO", "AW", "AY", "B", "CH", "D", "DH", "EH", "ER", "EY", "F", "G", "HH", "IH", "IY", "JH", "K",
35 "L", "M", "N", "NG", "OW", "OY", "P", "R", "S", "SH", "T", "TH", "UH", "UW", "V", "W", "Y", "Z", "ZH",
36 "[UNK]", "[PAD]", "<s>", "</s>"
37 };
38
39 constexpr std::array<uint32_t, 15> k_vowels { 0, 1, 2, 3, 4, 5, 10, 11, 12, 16, 17, 24, 25, 32, 33 };
40
41 enum Front : size_t {
42 FeatureWeight0,
43 FeatureNormWeight = FeatureWeight0 + k_feature_layers,
44 FeatureNormBias,
45 ProjectionNormWeight,
46 ProjectionNormBias,
47 ProjectionWeight,
48 ProjectionBias,
49 PositionBias,
50 EncoderNormWeight,
51 EncoderNormBias,
52 HeadWeight,
53 HeadBias,
54 FrontCount
55 };
56
57 enum Block : size_t {
58 QueryWeight,
59 QueryBias,
60 KeyWeight,
61 KeyBias,
62 ValueWeight,
63 ValueBias,
64 OutputWeight,
65 OutputBias,
66 NormWeight,
67 NormBias,
68 ExpandWeight,
69 ExpandBias,
70 ReduceWeight,
71 ReduceBias,
72 FinalNormWeight,
73 FinalNormBias,
74 BlockCount
75 };
76
77 enum Position : size_t {
78 PositionGain,
79 PositionDirection,
80 PositionCount
81 };
82
83 enum Buffer : uint32_t {
84 Audio,
85 WideA,
86 WideB,
87 Stats,
88 Hidden,
89 Positional,
90 Query,
91 Key,
92 Value,
93 Scores,
94 Expanded,
95 Posterior,
96 WeightsFront,
97 WeightsPosition,
98 WeightsBlock0
99 };
100
101 TensorKernels::Conv1d feature_layer(uint32_t index)
102 {
103 return {
104 .c_in = index == 0 ? 1U : k_features,
105 .c_out = k_features,
106 .kernel = k_feature_kernels[index],
107 .stride = k_feature_strides[index],
108 .activation = index == 0 ? Activation::None : Activation::Gelu,
109 };
110 }
111
112 std::vector<TensorRequirement> front_specs()
113 {
114 std::vector<TensorRequirement> specs;
115 specs.reserve(FrontCount);
116 for (uint32_t i = 0; i < k_feature_layers; ++i) {
117 specs.push_back({ std::format("hubert.feature_extractor.conv_layers.{}.conv.weight", i),
118 { k_features, i == 0 ? 1U : k_features, k_feature_kernels[i] } });
119 }
120 specs.push_back({ "hubert.feature_extractor.conv_layers.0.layer_norm.weight", { k_features } });
121 specs.push_back({ "hubert.feature_extractor.conv_layers.0.layer_norm.bias", { k_features } });
122 specs.push_back({ "hubert.feature_projection.layer_norm.weight", { k_features } });
123 specs.push_back({ "hubert.feature_projection.layer_norm.bias", { k_features } });
124 specs.push_back({ "hubert.feature_projection.projection.weight", { k_width, k_features } });
125 specs.push_back({ "hubert.feature_projection.projection.bias", { k_width } });
126 specs.push_back({ "hubert.encoder.pos_conv_embed.conv.bias", { k_width } });
127 specs.push_back({ "hubert.encoder.layer_norm.weight", { k_width } });
128 specs.push_back({ "hubert.encoder.layer_norm.bias", { k_width } });
129 specs.push_back({ "lm_head.weight", { k_classes, k_width } });
130 specs.push_back({ "lm_head.bias", { k_classes } });
131 return specs;
132 }
133
134 std::vector<TensorRequirement> block_specs(uint32_t block)
135 {
136 const std::string prefix = std::format("hubert.encoder.layers.{}.", block);
137 const auto dense = [&](std::vector<TensorRequirement>& specs, std::string_view name, uint32_t out, uint32_t in) {
138 specs.push_back({ std::format("{}{}.weight", prefix, name), { out, in } });
139 specs.push_back({ std::format("{}{}.bias", prefix, name), { out } });
140 };
141 const auto norm = [&](std::vector<TensorRequirement>& specs, std::string_view name) {
142 specs.push_back({ std::format("{}{}.weight", prefix, name), { k_width } });
143 specs.push_back({ std::format("{}{}.bias", prefix, name), { k_width } });
144 };
145 std::vector<TensorRequirement> specs;
146 specs.reserve(BlockCount);
147 dense(specs, "attention.q_proj", k_width, k_width);
148 dense(specs, "attention.k_proj", k_width, k_width);
149 dense(specs, "attention.v_proj", k_width, k_width);
150 dense(specs, "attention.out_proj", k_width, k_width);
151 norm(specs, "layer_norm");
152 dense(specs, "feed_forward.intermediate_dense", k_expanded, k_width);
153 dense(specs, "feed_forward.output_dense", k_width, k_expanded);
154 norm(specs, "final_layer_norm");
155 return specs;
156 }
157
158 std::vector<TensorRequirement> position_specs()
159 {
160 return {
161 { "hubert.encoder.pos_conv_embed.conv.weight_g", { 1, 1, k_position_kernel } },
162 { "hubert.encoder.pos_conv_embed.conv.weight_v", { k_width, k_width / k_position_groups, k_position_kernel } },
163 };
164 }
165
166 TensorKernels::PackedWeights pack(const Kakshya::TensorSet& weights, const std::vector<TensorRequirement>& specs)
167 {
168 auto packed = TensorKernels::pack_weights(weights, specs);
169 if (!packed) {
171 std::source_location::current(), "HubertPhonemeModel: {}", packed.error());
172 }
173 return std::move(*packed);
174 }
175
176 /**
177 * The position weights the convolution runs with: each kernel tap's direction scaled to that tap's gain, the
178 * norm of a tap taken over every output and input channel.
179 */
180 std::vector<float> fold_position_weights(const TensorKernels::PackedWeights& packed)
181 {
182 const size_t rows = packed.offsets.size() > PositionDirection
183 ? (packed.values.size() - packed.offsets[PositionDirection]) / k_position_kernel
184 : 0;
185 const float* gain = packed.values.data() + packed.offsets[PositionGain];
186 const float* direction = packed.values.data() + packed.offsets[PositionDirection];
187
188 std::array<double, k_position_kernel> norms {};
189 for (size_t row = 0; row < rows; ++row) {
190 for (size_t tap = 0; tap < k_position_kernel; ++tap) {
191 const double value = direction[row * k_position_kernel + tap];
192 norms[tap] += value * value;
193 }
194 }
195 std::vector<float> folded(rows * k_position_kernel);
196 for (size_t row = 0; row < rows; ++row) {
197 for (size_t tap = 0; tap < k_position_kernel; ++tap) {
198 const double norm = std::sqrt(norms[tap]);
199 folded[row * k_position_kernel + tap] = norm > 0.0
200 ? static_cast<float>(static_cast<double>(gain[tap]) * direction[row * k_position_kernel + tap] / norm)
201 : 0.0F;
202 }
203 }
204 return folded;
205 }
206
207 std::array<uint32_t, k_feature_layers> feature_lengths(size_t samples)
208 {
209 std::array<uint32_t, k_feature_layers> lengths {};
210 auto length = static_cast<uint32_t>(samples);
211 for (uint32_t i = 0; i < k_feature_layers; ++i) {
212 length = TensorKernels::conv1d_length(feature_layer(i), length);
213 lengths[i] = length;
214 }
215 return lengths;
216 }
217
218 std::vector<float> frame_major(const std::vector<float>& rows, size_t channels, size_t frames)
219 {
220 std::vector<float> out(rows.size());
221 for (size_t channel = 0; channel < channels; ++channel) {
222 for (size_t frame = 0; frame < frames; ++frame) {
223 out[frame * channels + channel] = rows[channel * frames + frame];
224 }
225 }
226 return out;
227 }
228
229}
230
232 std::shared_ptr<GpuDispatchRecorder> recording;
233 std::unique_ptr<ShaderExecutionContext<>> context;
235 std::array<TensorKernels::PackedWeights, k_blocks> blocks;
236 size_t resident { 0 };
237
238 Impl(const Kakshya::TensorSet& weights, std::shared_ptr<GpuDispatchRecorder> shared)
239 : recording(shared ? std::move(shared) : std::make_shared<GpuDispatchRecorder>(true))
240 , context(std::make_unique<ShaderExecutionContext<>>(
241 GpuComputeConfig { .shader_path = TensorKernels::conv1d_shader, .workgroup_size = { TensorKernels::element_workgroup, 1, 1 }, .push_constant_size = sizeof(TensorKernels::Conv1dPC) }))
242 {
243 context->set_recording(recording);
244 if (!context->ensure_gpu_ready()) {
246 std::source_location::current(), "HubertPhonemeModel needs a graphics device and none is available");
247 }
248
249 front = pack(weights, front_specs());
250 TensorKernels::upload_packed_weights(*context, 0, WeightsFront, front);
251 front.values = std::vector<float> {};
252
253 for (uint32_t block = 0; block < k_blocks; ++block) {
254 blocks[block] = pack(weights, block_specs(block));
255 TensorKernels::upload_packed_weights(*context, 0, WeightsBlock0 + block, blocks[block]);
256 blocks[block].values = std::vector<float> {};
257 }
258
259 const auto folded = fold_position_weights(pack(weights, position_specs()));
260 context->ensure_shared_buffer(0, WeightsPosition, folded.size(), GpuBufferBinding::ElementType::FLOAT32,
262 context->upload_shared_raw(0, WeightsPosition, reinterpret_cast<const uint8_t*>(folded.data()), folded.size() * sizeof(float));
263 }
264
265 void ensure_capacity(size_t samples, const std::array<uint32_t, k_feature_layers>& lengths)
266 {
268 constexpr auto type = GpuBufferBinding::ElementType::FLOAT32;
269 const size_t frames = lengths.back();
270 context->ensure_shared_buffer(0, Audio, samples, type, BufferUsageHint::COMPUTE);
271 context->ensure_shared_buffer(0, WideA, static_cast<size_t>(k_features) * lengths[0], type, BufferUsageHint::COMPUTE);
272 context->ensure_shared_buffer(0, WideB, static_cast<size_t>(k_features) * lengths[1], type, BufferUsageHint::COMPUTE);
273 context->ensure_shared_buffer(0, Stats, 2 * std::max<size_t>(k_features, frames), type, BufferUsageHint::COMPUTE);
274 for (const uint32_t buffer : { Hidden, Positional, Query, Key, Value }) {
275 context->ensure_shared_buffer(0, buffer, k_width * frames, type, BufferUsageHint::COMPUTE);
276 }
277 context->ensure_shared_buffer(0, Scores, k_heads * frames * frames, type, BufferUsageHint::COMPUTE);
278 context->ensure_shared_buffer(0, Expanded, k_expanded * frames, type, BufferUsageHint::COMPUTE);
279 context->ensure_shared_buffer(0, Posterior, k_classes * frames, type);
280 }
281
282 [[nodiscard]] uint64_t address(uint32_t buffer) const
283 {
284 return context->shared_buffer_address(0, buffer);
285 }
286
287 void upload_normalised(std::span<const float> samples)
288 {
289 double sum = 0.0;
290 for (const float sample : samples) {
291 sum += sample;
292 }
293 const double mean = sum / static_cast<double>(samples.size());
294 double square = 0.0;
295 for (const float sample : samples) {
296 square += (sample - mean) * (sample - mean);
297 }
298 const double scale = 1.0 / std::sqrt(square / static_cast<double>(samples.size()) + k_input_epsilon);
299
300 std::vector<float> normalised(samples.size());
301 for (size_t i = 0; i < samples.size(); ++i) {
302 normalised[i] = static_cast<float>((samples[i] - mean) * scale);
303 }
304 context->upload_shared_raw(0, Audio, reinterpret_cast<const uint8_t*>(normalised.data()), normalised.size() * sizeof(float));
305 }
306
307 [[nodiscard]] std::vector<float> run(std::span<const float> samples)
308 {
309 const auto lengths = feature_lengths(samples.size());
310 const uint32_t frames = lengths.back();
311 ensure_capacity(samples.size(), lengths);
312 upload_normalised(samples);
313
314 const auto front_at = [&](size_t slot) { return front.address(address(WeightsFront), slot); };
315 const auto block_at = [&](uint32_t block, size_t slot) { return blocks[block].address(address(WeightsBlock0 + block), slot); };
316
317 std::vector<DependencyStage> stages;
318 const auto dense = [&](uint64_t src, uint32_t in, uint64_t weight, uint64_t bias, uint64_t dst, uint32_t out, Activation activation = Activation::None) {
319 stages.push_back(TensorKernels::conv1d_stage({ .c_in = in, .c_out = out, .kernel = 1, .activation = activation }, src, weight, bias, dst, 1, frames));
320 };
321 const auto layer_norm = [&](uint64_t data, uint32_t channels, uint64_t weight, uint64_t bias) {
322 stages.push_back(TensorKernels::normalize_stats_stage(data, address(Stats), 1, channels, frames, k_norm_epsilon));
323 stages.push_back(TensorKernels::normalize_apply_stage(data, address(Stats), weight, bias, data, 1, channels, frames, NormAffine::Along));
324 };
325
326 uint64_t features = address(Audio);
327 auto length = static_cast<uint32_t>(samples.size());
328 for (uint32_t i = 0; i < k_feature_layers; ++i) {
329 const uint64_t dst = address(i % 2 == 0 ? WideA : WideB);
330 stages.push_back(TensorKernels::conv1d_stage(feature_layer(i), features, front_at(FeatureWeight0 + i), 0, dst, 1, length));
331 if (i == 0) {
332 stages.push_back(TensorKernels::normalize_stats_stage(dst, address(Stats), k_features, lengths[0], 1, k_norm_epsilon));
333 stages.push_back(TensorKernels::normalize_apply_stage(dst, address(Stats), front_at(FeatureNormWeight), front_at(FeatureNormBias),
334 dst, k_features, lengths[0], 1, NormAffine::Outer, Activation::Gelu));
335 }
336 features = dst;
337 length = lengths[i];
338 }
339
340 const uint64_t hidden = address(Hidden);
341 layer_norm(features, k_features, front_at(ProjectionNormWeight), front_at(ProjectionNormBias));
342 dense(features, k_features, front_at(ProjectionWeight), front_at(ProjectionBias), hidden, k_width);
343
345 { .c_in = k_width, .c_out = k_width, .kernel = k_position_kernel, .groups = k_position_groups, .pad = k_position_kernel / 2, .trim = 1, .activation = Activation::Gelu },
346 hidden, address(WeightsPosition), front_at(PositionBias), address(Positional), 1, frames));
347 stages.push_back(TensorKernels::add_stage(hidden, address(Positional), hidden, k_width * frames));
348 layer_norm(hidden, k_width, front_at(EncoderNormWeight), front_at(EncoderNormBias));
349
350 const float score_scale = 1.0F / std::sqrt(static_cast<float>(k_head_width));
351 for (uint32_t block = 0; block < k_blocks; ++block) {
352 dense(hidden, k_width, block_at(block, QueryWeight), block_at(block, QueryBias), address(Query), k_width);
353 dense(hidden, k_width, block_at(block, KeyWeight), block_at(block, KeyBias), address(Key), k_width);
354 dense(hidden, k_width, block_at(block, ValueWeight), block_at(block, ValueBias), address(Value), k_width);
355 stages.push_back(TensorKernels::attention_scores_stage(address(Query), address(Key), address(Scores), k_heads, k_head_width, frames, score_scale));
356 stages.push_back(TensorKernels::softmax_stage(address(Scores), k_heads * frames, frames, 1));
357 stages.push_back(TensorKernels::attention_mix_stage(address(Scores), address(Value), address(Query), k_heads, k_head_width, frames));
358 dense(address(Query), k_width, block_at(block, OutputWeight), block_at(block, OutputBias), address(Key), k_width);
359 stages.push_back(TensorKernels::add_stage(hidden, address(Key), hidden, k_width * frames));
360 layer_norm(hidden, k_width, block_at(block, NormWeight), block_at(block, NormBias));
361
362 dense(hidden, k_width, block_at(block, ExpandWeight), block_at(block, ExpandBias), address(Expanded), k_expanded, Activation::Gelu);
363 dense(address(Expanded), k_expanded, block_at(block, ReduceWeight), block_at(block, ReduceBias), address(Key), k_width);
364 stages.push_back(TensorKernels::add_stage(hidden, address(Key), hidden, k_width * frames));
365 layer_norm(hidden, k_width, block_at(block, FinalNormWeight), block_at(block, FinalNormBias));
366 }
367
368 dense(hidden, k_width, front_at(HeadWeight), front_at(HeadBias), address(Posterior), k_classes);
369 stages.push_back(TensorKernels::softmax_stage(address(Posterior), 1, k_classes, frames));
370
371 context->record_stages(stages);
372 resident = frames;
373
374 std::vector<float> rows(static_cast<size_t>(k_classes) * frames);
375 context->download_shared(0, Posterior, rows.data(), rows.size() * sizeof(float));
376 return frame_major(rows, k_classes, frames);
377 }
378};
379
381{
382 if (std::string reason = TensorKernels::check_tensors(weights, front_specs()); !reason.empty()) {
383 return reason;
384 }
385 if (std::string reason = TensorKernels::check_tensors(weights, position_specs()); !reason.empty()) {
386 return reason;
387 }
388 for (uint32_t block = 0; block < k_blocks; ++block) {
389 if (std::string reason = TensorKernels::check_tensors(weights, block_specs(block)); !reason.empty()) {
390 return reason;
391 }
392 }
393 return {};
394}
395
396std::string_view HubertPhonemeModel::label(uint32_t index) noexcept
397{
398 return index < k_labels.size() ? k_labels[index] : std::string_view {};
399}
400
401bool HubertPhonemeModel::is_vowel(uint32_t index) noexcept
402{
403 return std::ranges::find(k_vowels, index) != k_vowels.end();
404}
405
407{
408 return { k_labels, phonemes, blank_class, k_vowels };
409}
410
411uint32_t HubertPhonemeModel::frame_count(size_t samples) noexcept
412{
413 if (samples < frame_span || samples > std::numeric_limits<uint32_t>::max()) {
414 return 0;
415 }
416 return feature_lengths(samples).back();
417}
418
420 : HubertPhonemeModel(weights, nullptr)
421{
422}
423
424HubertPhonemeModel::HubertPhonemeModel(const Kakshya::TensorSet& weights, std::shared_ptr<GpuDispatchRecorder> recording)
425{
426 if (const std::string reason = diagnose(weights); !reason.empty()) {
428 std::source_location::current(), "HubertPhonemeModel: {}", reason);
429 }
430 m_impl = std::make_unique<Impl>(weights, std::move(recording));
431}
432
434
435std::vector<float> HubertPhonemeModel::process(std::span<const float> samples)
436{
437 if (samples.size() > max_samples) {
439 std::source_location::current(),
440 "HubertPhonemeModel: an utterance of {} samples is longer than the {} one call accepts; run it in pieces, for instance the intervals a voice activity analysis gives",
441 samples.size(), max_samples);
442 }
443 if (samples.size() < frame_span) {
444 m_impl->resident = 0;
445 return {};
446 }
447 return m_impl->run(samples);
448}
449
451{
452 const size_t frames = m_impl->resident;
453 if (frames == 0) {
454 return {};
455 }
456 std::vector<float> rows(hidden_width * frames);
457 m_impl->context->download_shared(0, Hidden, rows.data(), rows.size() * sizeof(float));
458 return frame_major(rows, hidden_width, frames);
459}
460
462{
463 return m_impl->resident;
464}
465
467{
468 m_impl->context->ensure_shared_buffer(0, Posterior, k_classes, GpuBufferBinding::ElementType::FLOAT32);
469 return { .context = m_impl->context.get(), .set = 0, .binding = Posterior };
470}
471
473{
474 m_impl->context->ensure_shared_buffer(0, Hidden, k_width, GpuBufferBinding::ElementType::FLOAT32,
476 return { .context = m_impl->context.get(), .set = 0, .binding = Hidden };
477}
478
479} // namespace MayaFlux::Yantra
A HuBERT base network with a phoneme head run on the GPU, one phoneme distribution per 20 ms frame.
The stages that run the tensor kernels.
Checking a set of tensors against what a network needs, and packing it into one float32 buffer.
Shared command buffer for GPU dispatch work.
DeviceBuffer posterior_buffer()
The device buffer of the distributions, classes rows of frames.
static constexpr uint32_t phonemes
Classes 0 to phonemes - 1 are phonemes.
HubertPhonemeModel(const Kakshya::TensorSet &weights)
Upload the weights and prepare the GPU resources.
DeviceBuffer hidden_buffer()
The device buffer of the states, hidden_width rows of frames.
static constexpr uint32_t classes
Scores per frame.
static bool is_vowel(uint32_t index) noexcept
Whether a class is a vowel phoneme.
static std::string diagnose(const Kakshya::TensorSet &weights)
Check that a tensor set holds the weights the network needs.
static constexpr uint32_t blank_class
Class the network emits between phonemes.
static PhonemeVocabulary vocabulary() noexcept
The vocabulary of this network: the names, the phoneme count, the blank class and the vowels.
static std::string_view label(uint32_t index) noexcept
Name of a class: the ARPABET symbol of a phoneme, or a bracketed name for the others.
std::vector< float > process(std::span< const float > samples)
Run one utterance.
static constexpr uint32_t frame_span
Samples one frame's features are computed from.
size_t resident_frames() const noexcept
Frames the latest utterance left on the device.
static constexpr uint32_t hidden_width
Width of the state before the final layer.
static constexpr size_t max_samples
Longest utterance one call accepts, 30 s.
std::vector< float > hidden_states()
The state before the final layer for every frame of the latest utterance.
static uint32_t frame_count(size_t samples) noexcept
Frames an utterance of this many samples gives, 0 below frame_span samples.
Concrete GpuExecutionContext for a single fixed shader with fixed bindings.
@ ComputeMatrix
Compute operations (Yantra - algorithms, matrices, DSP).
@ Yantra
DSP algorithms, computational units, matrix operations, Grammar.
BufferUsageHint
Semantic usage hint for buffer allocation and memory properties.
@ COMPUTE
Storage buffer for compute shaders (device-local).
NormAffine
Which index selects the weight and bias applied after a normalisation.
Definition Layers.hpp:66
DependencyStage softmax_stage(uint64_t data, uint32_t outer, uint32_t extent, uint32_t inner)
One nn_softmax dispatch: softmax of a tensor [outer, extent, inner] along extent, in place.
Definition Stages.hpp:228
DependencyStage grouped_conv1d_stage(const GroupedConv1d &layer, uint64_t src, uint64_t weights, uint64_t bias, uint64_t dst, uint32_t batch, uint32_t t_in)
One nn_conv1d_grouped dispatch: [batch, c_in, t_in] to [batch, c_out, grouped_conv1d_length].
Definition Stages.hpp:110
Activation
Output activation of a layer.
Definition Layers.hpp:16
DependencyStage add_stage(uint64_t a, uint64_t b, uint64_t dst, uint32_t count)
One nn_add dispatch: the element-wise sum of two sequences of count floats.
Definition Stages.hpp:197
DependencyStage normalize_apply_stage(uint64_t src, uint64_t stats, uint64_t weight, uint64_t bias, uint64_t dst, uint32_t outer, uint32_t extent, uint32_t inner, NormAffine affine, Activation activation=Activation::None)
The nn_normalize dispatch that writes (x - mean) * scale from the measures of normalize_stats_stage.
Definition Stages.hpp:175
DependencyStage normalize_stats_stage(uint64_t src, uint64_t stats, uint32_t outer, uint32_t extent, uint32_t inner, float epsilon)
The nn_normalize dispatch that measures every row of a tensor [outer, extent, inner] along extent.
Definition Stages.hpp:152
DependencyStage attention_scores_stage(uint64_t q, uint64_t k, uint64_t scores, uint32_t heads, uint32_t dim, uint32_t time, float scale)
One nn_attention_scores dispatch: scale times the dot product of every query with every key,...
Definition Stages.hpp:214
std::string check_tensors(const Kakshya::TensorSet &weights, std::span< const TensorRequirement > required)
Whether a set holds every required tensor with the required shape.
constexpr uint32_t element_workgroup
Invocations per workgroup of the element-wise kernels.
Definition Layouts.hpp:15
uint32_t conv1d_length(const Conv1d &layer, uint32_t t_in)
Output length of a convolution, 0 when the window does not fit the padded input.
Definition Layers.hpp:88
std::expected< PackedWeights, std::string > pack_weights(const Kakshya::TensorSet &weights, std::span< const TensorRequirement > required)
Convert the required tensors to float32 and lay them out in the order of required.
DependencyStage attention_mix_stage(uint64_t weights, uint64_t v, uint64_t dst, uint32_t heads, uint32_t dim, uint32_t time)
One nn_attention_mix dispatch: the values of every position mixed by each query's attention weights.
Definition Stages.hpp:245
DependencyStage conv1d_stage(const Conv1d &layer, uint64_t src, uint64_t weights, uint64_t bias, uint64_t dst, uint32_t batch, uint32_t t_in)
One nn_conv1d dispatch: [batch, c_in, t_in] to [batch, c_out, conv1d_length].
Definition Stages.hpp:70
void upload_packed_weights(GpuDispatchCore &context, uint32_t set, size_t binding, const PackedWeights &packed)
Allocate a shared buffer on a context and fill it with packed weights.
Activation
Output activation of a layer.
Definition Layers.hpp:16
static constexpr DomainSpec Audio
Domain constant for Audio domain.
Definition Creator.hpp:497
Named tensors that belong together, such as a model's weights or I/O.
Definition Tensor.hpp:167
enum MayaFlux::Portal::Graphics::GpuBufferBinding::ElementType FLOAT32
Plain-data description of the compute shader to dispatch.
Where a result of the latest utterance is on the device.
std::unique_ptr< ShaderExecutionContext<> > context
Impl(const Kakshya::TensorSet &weights, std::shared_ptr< GpuDispatchRecorder > shared)
void upload_normalised(std::span< const float > samples)
std::vector< float > run(std::span< const float > samples)
std::shared_ptr< GpuDispatchRecorder > recording
void ensure_capacity(size_t samples, const std::array< uint32_t, k_feature_layers > &lengths)
std::array< TensorKernels::PackedWeights, k_blocks > blocks
The classes of a phoneme network: their names, how many are phonemes, the blank class,...
Push constants of nn_conv1d.
Definition Layouts.hpp:51
One convolution layer over [batch, c_in, t_in], with weights [c_out, c_in, kernel].
Definition Layers.hpp:31
The required tensors as float32 in one run, in the order they were required.