Skip to content

[FEATURE] Options to disable prewarming during .Reset()#285

Merged
sdatkinson merged 7 commits into
mainfrom
270-no-prewarm
Jun 12, 2026
Merged

[FEATURE] Options to disable prewarming during .Reset()#285
sdatkinson merged 7 commits into
mainfrom
270-no-prewarm

Conversation

@sdatkinson

@sdatkinson sdatkinson commented Jun 10, 2026

Copy link
Copy Markdown
Owner

Description

Resolves #270

  • DSP::SetPrewarmOnReset() to toggle whether reset triggers prewarming. Default to true to avoid mistakes; folks can use false to optimize their code (with care!).
  • ScopedPrewarmOnResetDefault allows setting a context default before constructing DSP instances in case they call Reset internally during their constructor.
  • Thread-local gPrewarmOnResetDefault is used to determine whether DSP instances prewarm on reset.

Checklist

  • Verify that this can successfully suppress prewarming in NeuralAmpModelerPlugin's load path

@sdatkinson

Copy link
Copy Markdown
Owner Author

I've got this wired up in NeuralAmpModelerPlugin and have verified that I'm not visiting the prewarm routine more than once when changing models, regardless of whether the slim knob is set to 0 or 1.

So I'm feeling pretty good about this as a solution.

Code coming later in NeuralAmpModelerPlugin to demonstrate, but it's basically a no-prewarm context in _StageModel while the model is being initialized and packed into the resampling container; on exit, is SetPrewarmOnReset(true) and then finally Reset(). Since that's in the plugin code, I feel reasonably confident that other ways to build around get_dsp might be well-enough anticipated...so hopefully this does it!

@sdatkinson sdatkinson marked this pull request as ready for review June 10, 2026 23:49
@sdatkinson

Copy link
Copy Markdown
Owner Author

Here's the method:

std::string NeuralAmpModeler::_StageModel(const WDL_String& modelPath)
{
  WDL_String previousNAMPath = mNAMPath;
  try
  {
    auto dspPath = std::filesystem::u8path(modelPath.Get());
    std::unique_ptr<ResamplingNAM> temp = nullptr;
    {
      nam::ScopedPrewarmOnResetDefault scoped_prewarm_default(false);
      std::unique_ptr<nam::DSP> model = nam::get_dsp(dspPath);

      // Check that the model has 1 input and 1 output channel
      if (model->NumInputChannels() != 1)
      {
        throw std::runtime_error("Model must have 1 input channel, but has "
                                 + std::to_string(model->NumInputChannels()));
      }
      if (model->NumOutputChannels() != 1)
      {
        throw std::runtime_error("Model must have 1 output channel, but has "
                                 + std::to_string(model->NumOutputChannels()));
      }

      temp = std::make_unique<ResamplingNAM>(std::move(model), GetSampleRate());
      if (nam::SlimmableModel* slimmable = temp->GetSlimmableModel())
      {
        slimmable->SetSlimmableSize(GetParam(kSlim)->Value());
      }
    }
    temp->SetPrewarmOnReset(true);
    temp->Reset(GetSampleRate(), GetBlockSize());
    mStagedModel = std::move(temp);
    mNAMPath = modelPath;
    SendControlMsgFromDelegate(kCtrlTagModelFileBrowser, kMsgTagLoadedModel, mNAMPath.GetLength(), mNAMPath.Get());
  }
  catch (std::runtime_error& e)
  {
    SendControlMsgFromDelegate(kCtrlTagModelFileBrowser, kMsgTagLoadFailed);

    if (mStagedModel != nullptr)
    {
      mStagedModel = nullptr;
    }
    mNAMPath = previousNAMPath;
    std::cerr << "Failed to read DSP module" << std::endl;
    std::cerr << e.what() << std::endl;
    return e.what();
  }
  return "";
}

@sdatkinson

Copy link
Copy Markdown
Owner Author

cc @mikeoliphant if you want to have a look before I merge this.

@mikeoliphant

Copy link
Copy Markdown
Contributor

So a thread-local flag? Seems like that should be enough to prevent the possibility of inter-plugin crosstalk.

@sdatkinson sdatkinson merged commit e49c93e into main Jun 12, 2026
4 checks passed
@sdatkinson sdatkinson deleted the 270-no-prewarm branch June 12, 2026 00:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide a method to temporarily suppress model prewarm behavior

2 participants