| 529 | std::shared_ptr<ThinkerWeightsRuntime> runtime, |
| 530 | int64_t prompt_steps, |
| 531 | int64_t audio_tokens, |
| 532 | size_t graph_arena_bytes) |
| 533 | : runtime_(std::move(runtime)), |
| 534 | prompt_steps_(prompt_steps), |
| 535 | audio_tokens_(audio_tokens) { |
| 536 | if (prompt_steps_ <= 0) { |
| 537 | throw std::runtime_error("Qwen3 ASR thinker classification requires positive prompt length"); |
| 538 | } |
| 539 | if (audio_tokens_ < 0 || audio_tokens_ > prompt_steps_) { |
| 540 | throw std::runtime_error("Qwen3 ASR thinker classification audio token count is invalid"); |
| 541 | } |
| 542 | const auto build_start = Clock::now(); |
| 543 | ggml_init_params params{graph_arena_bytes, nullptr, true}; |
| 544 | ctx_.reset(ggml_init(params)); |
| 545 | if (ctx_ == nullptr) { |
| 546 | throw std::runtime_error("failed to initialize Qwen3 ASR thinker classification graph context"); |
| 547 | } |
| 548 | const auto & config = runtime_->assets().config.text_decoder; |
| 549 | const auto & weights = runtime_->weights(); |
| 550 | core::ModuleBuildContext ctx{ctx_.get(), "qwen3_asr.thinker.classify", runtime_->backend_type()}; |
| 551 | token_ids_input_ = ggml_new_tensor_1d(ctx_.get(), GGML_TYPE_I32, prompt_steps_); |
| 552 | audio_embeddings_ = ggml_new_tensor_2d(ctx_.get(), GGML_TYPE_F32, config.hidden_size, std::max<int64_t>(audio_tokens_, 1)); |
| 553 | audio_positions_ = ggml_new_tensor_1d(ctx_.get(), GGML_TYPE_I64, std::max<int64_t>(audio_tokens_, 1)); |
| 554 | auto x = prompt_embeddings( |
| 555 | ctx, |
| 556 | weights, |
| 557 | config, |
| 558 | token_ids_input_, |
| 559 | audio_embeddings_, |
| 560 | audio_positions_, |
| 561 | prompt_steps_, |
| 562 | audio_tokens_); |
| 563 | positions_ = ggml_new_tensor_1d(ctx_.get(), GGML_TYPE_I32, prompt_steps_); |
| 564 | auto positions = core::wrap_tensor(positions_, core::TensorShape::from_dims({prompt_steps_}), GGML_TYPE_I32); |
| 565 | |
| 566 | auto decoder_config = runtime_->decoder_config(); |
| 567 | decoder_config.logits_mode = modules::CausalDecoderLogitsMode::AllSteps; |
| 568 | auto decoder_out = modules::CausalDecoderModule(decoder_config) |
| 569 | .build(ctx, x, positions, make_qwen3_decoder_weights(weights)); |
| 570 | auto token_ids = engine::sampling::GreedyDecodeModule().build(ctx, decoder_out.logits); |
| 571 | token_ids_ = token_ids.tensor; |
| 572 | ggml_set_output(token_ids_); |
| 573 | graph_ = ggml_new_graph_custom(ctx_.get(), 65536, false); |
| 574 | ggml_build_forward_expand(graph_, token_ids_); |
| 575 | const auto try_alloc = [&]() { |
| 576 | gallocr_.reset(ggml_gallocr_new(ggml_backend_get_default_buffer_type(runtime_->backend()))); |
| 577 | return gallocr_ != nullptr && |
| 578 | ggml_gallocr_reserve(gallocr_.get(), graph_) && |
| 579 | ggml_gallocr_alloc_graph(gallocr_.get(), graph_); |
| 580 | }; |
| 581 | if (!try_alloc() && |
| 582 | (engine::core::trim_backend_pools(runtime_->backend()), !try_alloc())) { |
| 583 | throw std::runtime_error("failed to allocate Qwen3 ASR thinker classification graph"); |
| 584 | } |
| 585 | position_ids_ = modules::decoder_position_ids(prompt_steps_); |
| 586 | debug::timing_log_scalar("qwen3_asr.thinker.classify.graph.build_ms", engine::debug::elapsed_ms(build_start, Clock::now())); |
| 587 | debug::trace_log_scalar("qwen3_asr.thinker.classify_prompt_steps", prompt_steps_); |
| 588 | } |
no test coverage detected