| 1311 | } |
| 1312 | |
| 1313 | HeartMuLaDecoderResult run_step(const std::vector<float> & embedding, int64_t codebook_index) { |
| 1314 | allocate_workspace(); |
| 1315 | const auto & full_config = runtime_->assets().mula_config; |
| 1316 | const auto & config = full_config.decoder; |
| 1317 | if (static_cast<int64_t>(embedding.size()) != batch_size_ * full_config.backbone.embed_dim) { |
| 1318 | throw std::runtime_error("HeartMuLa decoder cached step embedding payload size mismatch"); |
| 1319 | } |
| 1320 | if (codebook_index < 0 || codebook_index >= full_config.audio_num_codebooks - 1) { |
| 1321 | throw std::runtime_error("HeartMuLa decoder cached step codebook index is out of range"); |
| 1322 | } |
| 1323 | if (step_cache_.valid_steps() >= cache_steps_) { |
| 1324 | throw std::runtime_error("HeartMuLa decoder cached step exceeds cache capacity"); |
| 1325 | } |
| 1326 | ggml_backend_tensor_set(input_, embedding.data(), 0, embedding.size() * sizeof(float)); |
| 1327 | const int32_t position = static_cast<int32_t>(step_cache_.current_end()); |
| 1328 | ggml_backend_tensor_set(positions_, &position, 0, sizeof(int32_t)); |
| 1329 | const int32_t cache_slot = static_cast<int32_t>(step_cache_.valid_steps()); |
| 1330 | ggml_backend_tensor_set(cache_slot_, &cache_slot, 0, sizeof(int32_t)); |
| 1331 | const int32_t codebook = static_cast<int32_t>(codebook_index); |
| 1332 | ggml_backend_tensor_set(codebook_index_, &codebook, 0, sizeof(int32_t)); |
| 1333 | std::fill( |
| 1334 | attention_mask_buffer_.begin(), |
| 1335 | attention_mask_buffer_.end(), |
| 1336 | ggml_fp32_to_fp16(-std::numeric_limits<float>::infinity())); |
| 1337 | for (int64_t i = 0; i < step_cache_.valid_steps(); ++i) { |
| 1338 | attention_mask_buffer_[static_cast<size_t>(i)] = ggml_fp32_to_fp16(0.0F); |
| 1339 | } |
| 1340 | attention_mask_buffer_[static_cast<size_t>(cache_slot)] = ggml_fp32_to_fp16(0.0F); |
| 1341 | ggml_backend_tensor_set( |
| 1342 | attention_mask_, |
| 1343 | attention_mask_buffer_.data(), |
| 1344 | 0, |
| 1345 | attention_mask_buffer_.size() * sizeof(ggml_fp16_t)); |
| 1346 | core::set_backend_threads(runtime_->backend(), runtime_->threads()); |
| 1347 | const ggml_status status = engine::core::compute_backend_graph(runtime_->backend(), graph_); |
| 1348 | ggml_backend_synchronize(runtime_->backend()); |
| 1349 | if (status != GGML_STATUS_SUCCESS) { |
| 1350 | throw std::runtime_error("HeartMuLa decoder cached step graph compute failed"); |
| 1351 | } |
| 1352 | HeartMuLaDecoderResult out; |
| 1353 | out.logits.vocab_size = full_config.audio_vocab_size; |
| 1354 | out.logits.values.resize(static_cast<size_t>(batch_size_ * full_config.audio_vocab_size)); |
| 1355 | ggml_backend_tensor_get(logits_output_, out.logits.values.data(), 0, out.logits.values.size() * sizeof(float)); |
| 1356 | out.last_hidden.dims = config.embed_dim; |
| 1357 | out.last_hidden.values.resize(static_cast<size_t>(batch_size_ * config.embed_dim)); |
| 1358 | ggml_backend_tensor_get(hidden_output_, out.last_hidden.values.data(), 0, out.last_hidden.values.size() * sizeof(float)); |
| 1359 | step_cache_.advance_after_direct_append(1); |
| 1360 | return out; |
| 1361 | } |
| 1362 | |
| 1363 | private: |
| 1364 | void allocate_workspace() { |
no test coverage detected