| 338 | } |
| 339 | |
| 340 | RunResult run_impl( |
| 341 | engine::runtime::IVoiceTaskSession & session, |
| 342 | const engine::runtime::TaskSpec & task_spec, |
| 343 | const engine::runtime::TaskRequest & request, |
| 344 | bool prepare_first) { |
| 345 | const auto started = std::chrono::steady_clock::now(); |
| 346 | if (prepare_first) { |
| 347 | session.prepare(engine::runtime::build_preparation_request(request)); |
| 348 | } |
| 349 | if (task_spec.mode == engine::runtime::RunMode::Offline) { |
| 350 | auto * offline = dynamic_cast<engine::runtime::IOfflineVoiceTaskSession *>(&session); |
| 351 | if (offline == nullptr) { |
| 352 | throw std::runtime_error("selected model does not support offline mode"); |
| 353 | } |
| 354 | auto result = offline->run(request); |
| 355 | const auto ended = std::chrono::steady_clock::now(); |
| 356 | return { |
| 357 | std::move(result), |
| 358 | std::chrono::duration<double, std::milli>(ended - started).count(), |
| 359 | }; |
| 360 | } |
| 361 | |
| 362 | auto * streaming = dynamic_cast<engine::runtime::IStreamingVoiceTaskSession *>(&session); |
| 363 | if (streaming == nullptr) { |
| 364 | throw std::runtime_error("selected model does not support streaming mode"); |
| 365 | } |
| 366 | if (!request.audio_input.has_value()) { |
| 367 | throw std::runtime_error("streaming perf request requires request.audio_input"); |
| 368 | } |
| 369 | streaming->reset(); |
| 370 | const auto & audio = *request.audio_input; |
| 371 | constexpr int kChunkSamples = 512; |
| 372 | for (size_t offset = 0; offset < audio.samples.size(); offset += static_cast<size_t>(kChunkSamples)) { |
| 373 | const size_t available = std::min(static_cast<size_t>(kChunkSamples), audio.samples.size() - offset); |
| 374 | std::vector<float> chunk(static_cast<size_t>(kChunkSamples), 0.0f); |
| 375 | std::copy( |
| 376 | audio.samples.begin() + static_cast<std::ptrdiff_t>(offset), |
| 377 | audio.samples.begin() + static_cast<std::ptrdiff_t>(offset + available), |
| 378 | chunk.begin()); |
| 379 | streaming->process_audio_chunk(engine::runtime::AudioChunk{ |
| 380 | audio.sample_rate, |
| 381 | audio.channels, |
| 382 | static_cast<int64_t>(offset / static_cast<size_t>(std::max(1, audio.channels))), |
| 383 | std::move(chunk), |
| 384 | }); |
| 385 | } |
| 386 | auto result = streaming->finalize(); |
| 387 | const auto ended = std::chrono::steady_clock::now(); |
| 388 | return { |
| 389 | std::move(result), |
| 390 | std::chrono::duration<double, std::milli>(ended - started).count(), |
| 391 | }; |
| 392 | } |
| 393 | |
| 394 | RunResult run_once( |
| 395 | engine::runtime::IVoiceTaskSession & session, |