From b837c452ca9c1c89b1a026106646278a4e22df8e Mon Sep 17 00:00:00 2001 From: Moonsik Song Date: Fri, 20 Nov 2026 00:05:37 +0000 Subject: [PATCH] tts(magpietts): share model weights across synthesizers in one process Synthesizers created in one process with the same MagpieTTS GGUF (same uma mode and CPU/GPU placement) now borrow one copy of the weights through a process-wide weak_ptr registry. Each synthesizer still opens its own backend on the owner's device (with the same CUDA stream priority as the owner), so CUDA graphs, K/V arenas and the codec stay per synthesizer. The owner's weights live until the last borrower is released. Magpie v2607 f16 on L40S: four synthesizers use 3,730 MiB instead of 5,362 MiB (1,234 MiB -> 690 MiB per extra synthesizer). Outputs are byte-identical. Signed-off-by: Moonsik Song --- src/tts/magpietts/magpietts.cpp | 53 ++++++++++++++++++-------- src/tts/magpietts/model.cpp | 67 ++++++++++++++++++++++++++++----- src/tts/magpietts/model.h | 6 +++ 3 files changed, 102 insertions(+), 24 deletions(-) diff --git a/src/tts/magpietts/magpietts.cpp b/src/tts/magpietts/magpietts.cpp index de6dda0d..ad154452 100644 --- a/src/tts/magpietts/magpietts.cpp +++ b/src/tts/magpietts/magpietts.cpp @@ -344,14 +344,36 @@ class MagpieStreamingWorkspace { class MagpieStreamingRuntime::Impl { public: - MagpieModel magpie; + std::shared_ptr magpie; nc::NanoCodecModel codec; std::unique_ptr workspace; }; namespace { GgmlLogFilter magpie_ggml_logs; + +// Magpie weights loaded in this process, keyed by file and placement. Runtimes that load the +// same model borrow one copy (MagpieModel::shareWeights) instead of uploading it again. +std::mutex shared_weights_mutex; +std::map> shared_weights; + +std::shared_ptr +load_magpie_shared( + const std::string& fname, magpietts_uma_mode uma_mode, bool force_cpu, bool verbose) { + const std::string key = + fname + "|" + std::to_string((int)uma_mode) + "|" + (force_cpu ? "cpu" : "auto"); + std::lock_guard lock(shared_weights_mutex); + auto model = std::make_shared(); + if (auto owner = shared_weights[key].lock()) { + return model->shareWeights(std::move(owner)) ? model : nullptr; + } + if (!model->load(fname, uma_mode, force_cpu, verbose)) { + return nullptr; + } + shared_weights[key] = model; + return model; } +} // namespace MagpieStreamingRuntime::MagpieStreamingRuntime() : impl_(std::make_unique()) {} @@ -364,14 +386,15 @@ MagpieStreamingRuntime::load( magpie_ggml_logs.set_verbose(verbose); ggml_log_set(GgmlLogFilter::callback, &magpie_ggml_logs); impl_->workspace.reset(); - if (!impl_->magpie.load(magpie_model, uma_mode, magpie_cpu, verbose)) { + impl_->magpie = load_magpie_shared(magpie_model, uma_mode, magpie_cpu, verbose); + if (!impl_->magpie) { return false; } if (!impl_->codec.load(codec_model, codec_cpu, verbose)) { impl_->magpie.reset(); return false; } - impl_->workspace = std::make_unique(impl_->magpie, impl_->codec); + impl_->workspace = std::make_unique(*impl_->magpie, impl_->codec); return true; } @@ -382,24 +405,24 @@ MagpieStreamingRuntime::sampleRate() const { int MagpieStreamingRuntime::speakerCount() const { - return impl_ ? impl_->magpie.hparams.baked_speakers : 0; + return impl_ && impl_->magpie ? impl_->magpie->hparams.baked_speakers : 0; } std::vector MagpieStreamingRuntime::speakerNames() const { std::vector out; - if (!impl_ || !impl_->magpie.gguf) { + if (!impl_ || !impl_->magpie || !impl_->magpie->gguf) { return out; } - const int64_t id = gguf_find_key(impl_->magpie.gguf, "magpietts.speaker_names"); - if (id < 0 || gguf_get_kv_type(impl_->magpie.gguf, id) != GGUF_TYPE_ARRAY || - gguf_get_arr_type(impl_->magpie.gguf, id) != GGUF_TYPE_STRING) { + const int64_t id = gguf_find_key(impl_->magpie->gguf, "magpietts.speaker_names"); + if (id < 0 || gguf_get_kv_type(impl_->magpie->gguf, id) != GGUF_TYPE_ARRAY || + gguf_get_arr_type(impl_->magpie->gguf, id) != GGUF_TYPE_STRING) { return out; } - const size_t n = gguf_get_arr_n(impl_->magpie.gguf, id); + const size_t n = gguf_get_arr_n(impl_->magpie->gguf, id); out.reserve(n); for (size_t i = 0; i < n; ++i) { - const char* value = gguf_get_arr_str(impl_->magpie.gguf, id, i); + const char* value = gguf_get_arr_str(impl_->magpie->gguf, id, i); if (value && value[0]) { out.emplace_back(value); } @@ -410,12 +433,12 @@ MagpieStreamingRuntime::speakerNames() const { const std::string& MagpieStreamingRuntime::tokenizerProfile() const { static const std::string empty; - return impl_ ? impl_->magpie.tokenizer_profile : empty; + return impl_ && impl_->magpie ? impl_->magpie->tokenizer_profile : empty; } int MagpieStreamingRuntime::textVocabSize() const { - return impl_ ? impl_->magpie.hparams.text_vocab_size : 0; + return impl_ && impl_->magpie ? impl_->magpie->hparams.text_vocab_size : 0; } void @@ -2079,17 +2102,17 @@ MagpieStreamingRuntime::synthesize( magpie_stream_params& params, const std::vector>& token_chunks, const magpie_pcm_callback& pcm_callback, stream_run_metrics& metrics, const char* run_label, bool write_codes) { - if (!impl_) { + if (!impl_ || !impl_->magpie) { return false; } stream_audio_outputs outputs; outputs.sample_rate = impl_->codec.sampleRate(); outputs.pcm_callback = pcm_callback; if (!impl_->workspace) { - impl_->workspace = std::make_unique(impl_->magpie, impl_->codec); + impl_->workspace = std::make_unique(*impl_->magpie, impl_->codec); } return stream_magpie_to_audio( - impl_->magpie, impl_->codec, *impl_->workspace, params, token_chunks, outputs, metrics, + *impl_->magpie, impl_->codec, *impl_->workspace, params, token_chunks, outputs, metrics, run_label, write_codes); } diff --git a/src/tts/magpietts/model.cpp b/src/tts/magpietts/model.cpp index 32d273c9..5012a5a1 100644 --- a/src/tts/magpietts/model.cpp +++ b/src/tts/magpietts/model.cpp @@ -647,6 +647,7 @@ MagpieModel::operator=(MagpieModel&& other) noexcept { backend = other.backend; buffer = other.buffer; cuda_unified_memory = other.cuda_unified_memory; + weights_owner = std::move(other.weights_owner); text_embedding = other.text_embedding; audio_embeddings = std::move(other.audio_embeddings); baked_context = other.baked_context; @@ -685,8 +686,64 @@ MagpieModel::load( return true; } +static void +magpietts_prioritize_backend(ggml_backend_t backend) { +#if defined(GGML_USE_CUDA) && defined(NEMO_SPEECH_GGML_PATCHED) + if (backend && ggml_backend_is_cuda(backend)) { + // The decoder/local-transformer stream is the latency-critical path; run it at the + // highest CUDA stream priority so the concurrent NanoCodec worker (priority -2) and + // default-priority side streams do not delay its small kernels. + constexpr int kHighestPriority = -100; // clamped to the device range by ggml + ggml_backend_cuda_set_stream_priority(backend, kHighestPriority); + } +#else + (void)backend; +#endif +} + +bool +MagpieModel::shareWeights(std::shared_ptr owner) { + reset(); + if (!owner || !owner->loaded()) { + return false; + } + backend = ggml_backend_dev_init(ggml_backend_get_device(owner->backend), nullptr); + if (!backend) { + fprintf(stderr, "failed to initialize ggml backend for shared MagpieTTS weights\n"); + return false; + } + magpietts_prioritize_backend(backend); + hparams = owner->hparams; + tokenizer_profile = owner->tokenizer_profile; + nemo_version = owner->nemo_version; + gguf = owner->gguf; + ctx = owner->ctx; + buffer = owner->buffer; + cuda_unified_memory = owner->cuda_unified_memory; + text_embedding = owner->text_embedding; + audio_embeddings = owner->audio_embeddings; + baked_context = owner->baked_context; + final_proj_w = owner->final_proj_w; + final_proj_b = owner->final_proj_b; + lt_in_w = owner->lt_in_w; + lt_in_b = owner->lt_in_b; + lt_out_w = owner->lt_out_w; + lt_out_b = owner->lt_out_b; + encoder = owner->encoder; + decoder = owner->decoder; + local = owner->local; + weights_owner = std::move(owner); + return true; +} + void MagpieModel::reset() { + if (weights_owner) { // borrowed: the owner frees them + buffer = nullptr; + gguf = nullptr; + ctx = nullptr; + weights_owner.reset(); + } if (buffer) { ggml_backend_buffer_free(buffer); buffer = nullptr; @@ -880,15 +937,7 @@ magpietts_model_load_impl( if (!model.backend) { model.backend = ggml_backend_init_by_type(GGML_BACKEND_DEVICE_TYPE_CPU, nullptr); } -#if defined(GGML_USE_CUDA) && defined(NEMO_SPEECH_GGML_PATCHED) - if (model.backend && ggml_backend_is_cuda(model.backend)) { - // The decoder/local-transformer stream is the latency-critical path; run it at the - // highest CUDA stream priority so the concurrent NanoCodec worker (priority -2) and - // default-priority side streams do not delay its small kernels. - constexpr int kHighestPriority = -100; // clamped to the device range by ggml - ggml_backend_cuda_set_stream_priority(model.backend, kHighestPriority); - } -#endif + magpietts_prioritize_backend(model.backend); if (!model.backend) { fprintf(stderr, "failed to initialize ggml backend\n"); return false; diff --git a/src/tts/magpietts/model.h b/src/tts/magpietts/model.h index 55a0c9c4..3a18bec9 100644 --- a/src/tts/magpietts/model.h +++ b/src/tts/magpietts/model.h @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -270,6 +271,10 @@ class MagpieModel { bool load( const std::string& fname, magpietts_uma_mode uma_mode = MAGPIETTS_UMA_AUTO, bool force_cpu = false, bool verbose = false); + // Borrow owner's weights (gguf, ctx, buffer, tensors) and open a fresh backend on the same + // device, so several runtimes in one process keep a single copy of the weights on the GPU. + // Only the weights are shared: each borrower computes on its own backend (own CUDA stream). + bool shareWeights(std::shared_ptr owner); void reset(); bool loaded() const { return gguf != nullptr && ctx != nullptr && backend != nullptr; } @@ -282,6 +287,7 @@ class MagpieModel { ggml_backend_t backend = nullptr; ggml_backend_buffer_t buffer = nullptr; bool cuda_unified_memory = false; + std::shared_ptr weights_owner; // set when the weights are borrowed ggml_tensor* text_embedding = nullptr; std::vector audio_embeddings;