| 369 | if (!ctx_) { |
| 370 | ctx_.reset(ggml_init({4ull * 1024 * 1024, nullptr, true})); |
| 371 | if (!ctx_) throw std::runtime_error("failed to initialize Qwen3 text projection graph"); |
| 372 | core::ModuleBuildContext ctx{ctx_.get(), "qwen3_tts.text_projection"}; |
| 373 | constants_.begin_graph(); |
| 374 | auto x = core::make_tensor(ctx, GGML_TYPE_F32, |
| 375 | core::TensorShape::from_dims({capacity_, config_.text_hidden_size})); |
| 376 | input_ = x.tensor; |
| 377 | if (bf16_activations_) x = modules::Bf16RoundModule().build(ctx, x); |
| 378 | x = modules::LinearModule({config_.text_hidden_size, config_.text_hidden_size, true}).build( |
| 379 | ctx, x, binding::linear_data(constants_, weights_.text_projection_fc1.weight, |
| 380 | weights_.text_projection_fc1.bias)); |
| 381 | if (bf16_activations_) x = modules::Bf16RoundModule().build(ctx, x); |
| 382 | x = modules::SiluModule().build(ctx, x); |
| 383 | if (bf16_activations_) x = modules::Bf16RoundModule().build(ctx, x); |
| 384 | x = modules::LinearModule({config_.text_hidden_size, config_.hidden_size, true}).build( |
| 385 | ctx, x, binding::linear_data(constants_, weights_.text_projection_fc2.weight, |
| 386 | weights_.text_projection_fc2.bias)); |
| 387 | if (bf16_activations_) x = modules::Bf16RoundModule().build(ctx, x); |
| 388 | output_ = x.tensor; |
| 389 | ggml_set_input(input_); |
| 390 | ggml_set_output(output_); |
| 391 | graph_ = ggml_new_graph_custom(ctx_.get(), 64, false); |
| 392 | ggml_build_forward_expand(graph_, output_); |
| 393 | constants_.finish_graph(); |
| 394 | constants_.ensure_uploaded(); |
| 395 | allocator_.reset(ggml_gallocr_new(ggml_backend_get_default_buffer_type(backend_))); |
| 396 | if (!allocator_ || !ggml_gallocr_reserve(allocator_.get(), graph_) || |
| 397 | !ggml_gallocr_alloc_graph(allocator_.get(), graph_)) { |
| 398 | throw std::runtime_error("failed to allocate Qwen3 text projection graph"); |
| 399 | } |
| 400 | } |
| 401 | // Independent rows use a fixed tile so previous request lengths cannot |
| 402 | // change the projection's matmul shape or numerical results. |
| 403 | std::vector<float> padded(static_cast<size_t>(capacity_ * config_.text_hidden_size), 0.0F); |
| 404 | core::set_backend_threads(backend_, threads_); |
| 405 | std::vector<float> output(static_cast<size_t>(rows * config_.hidden_size)); |
| 406 | for (int64_t first = 0; first < rows; first += capacity_) { |
| 407 | const int64_t count = std::min(capacity_, rows - first); |
| 408 | std::fill(padded.begin(), padded.end(), 0.0F); |
| 409 | std::copy_n(input.begin() + first * config_.text_hidden_size, |
| 410 | count * config_.text_hidden_size, padded.begin()); |
| 411 | ggml_backend_tensor_set(input_, padded.data(), 0, padded.size() * sizeof(float)); |
| 412 | if (core::compute_backend_graph(backend_, graph_) != GGML_STATUS_SUCCESS) { |
| 413 | throw std::runtime_error("Qwen3 text projection graph compute failed"); |
| 414 | } |
| 415 | ggml_backend_tensor_get(output_, output.data() + first * config_.hidden_size, |
| 416 | 0, count * config_.hidden_size * sizeof(float)); |
| 417 | } |
| 418 | return output; |
| 419 | } |
| 420 | |
| 421 | private: |
| 422 | ggml_backend_t backend_; |
| 423 | int threads_; |
| 424 | const Qwen3TalkerWeights & weights_; |
| 425 | const Qwen3TTSTalkerConfig & config_; |
| 426 | core::ConstantTensorCache constants_; |
| 427 | std::unique_ptr<ggml_context, GgmlContextDeleter> ctx_; |
| 428 | std::unique_ptr<ggml_gallocr, decltype(&ggml_gallocr_free)> allocator_{nullptr, ggml_gallocr_free}; |
no test coverage detected