Repository navigation
tts(magpietts): share model weights across synthesizers in one process #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -344,14 +344,36 @@ class MagpieStreamingWorkspace { | |
|
|
||
| class MagpieStreamingRuntime::Impl { | ||
| public: | ||
| MagpieModel magpie; | ||
| std::shared_ptr<MagpieModel> magpie; | ||
| nc::NanoCodecModel codec; | ||
| std::unique_ptr<MagpieStreamingWorkspace> 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<std::string, std::weak_ptr<MagpieModel>> shared_weights; | ||
|
|
||
| std::shared_ptr<MagpieModel> | ||
| 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<std::mutex> lock(shared_weights_mutex); | ||
| auto model = std::make_shared<MagpieModel>(); | ||
| if (auto owner = shared_weights[key].lock()) { | ||
| return model->shareWeights(std::move(owner)) ? model : nullptr; | ||
|
Comment on lines
+363
to
+368
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Resolve the model path to an absolute, normalized path before the cache lookup and use that same path to load the file. |
||
| } | ||
| if (!model->load(fname, uma_mode, force_cpu, verbose)) { | ||
| return nullptr; | ||
| } | ||
| shared_weights[key] = model; | ||
| return model; | ||
| } | ||
| } // namespace | ||
|
|
||
| MagpieStreamingRuntime::MagpieStreamingRuntime() : impl_(std::make_unique<Impl>()) {} | ||
|
|
||
|
|
@@ -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<MagpieStreamingWorkspace>(impl_->magpie, impl_->codec); | ||
| impl_->workspace = std::make_unique<MagpieStreamingWorkspace>(*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<std::string> | ||
| MagpieStreamingRuntime::speakerNames() const { | ||
| std::vector<std::string> 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<std::vector<int32_t>>& 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<MagpieStreamingWorkspace>(impl_->magpie, impl_->codec); | ||
| impl_->workspace = std::make_unique<MagpieStreamingWorkspace>(*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); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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<const MagpieModel> 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; | ||
|
Comment on lines
+719
to
+721
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Retain the owner before copying borrowed resource handles. If Retain As per path instructions, “Recoverable runtime failures—including allocation or setup failures—must not reach process-terminating checks or uncaught exceptions across API/thread boundaries.”
|
||
| cuda_unified_memory = owner->cuda_unified_memory; | ||
| text_embedding = owner->text_embedding; | ||
| audio_embeddings = owner->audio_embeddings; | ||
|
Comment on lines
+719
to
+724
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Finish the one-time repack before another stream can read the shared weights. Disabling in-place repacking alone is insufficient: the separate buffers are also published before their repack finishes. Knowledge Base Used: Text-to-speech synthesis
Comment on lines
+719
to
+724
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Set |
||
| 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; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolve relative filenames before using them as cache keys.
Load
model.gguffrom/models/a, keep that runtime alive, then change the working directory to/models/band load a differentmodel.gguf. Both calls use the same key. The second runtime silently borrows/models/a/model.ggufinstead of loading the requested file.Resolve the filename to an absolute path before lookup. Use that same resolved path for loading. This preserves sharing for the same file without confusing files in different working directories.