Skip to content

Commit 290dd5a

Browse files
authored
[BUGFIX] Fix container slimmable prewarming (#259)
* [BUGFIX] Avoid prewarming container slimmable model when SetSlimmableSize doesn't change which model is active. * [TEST] Ensure consistent model state before processing in test_container Added calls to ResetAndPrewarm in test_container_default_is_max_size to guarantee that both predictions start from the same model state, enhancing test reliability.
1 parent 2316d6f commit 290dd5a

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

NAM/container.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,25 @@ void ContainerModel::Reset(const double sampleRate, const int maxBufferSize)
6969

7070
void ContainerModel::SetSlimmableSize(const double val)
7171
{
72-
_active_index = _submodels.size() - 1;
72+
auto active_index = _submodels.size() - 1;
7373
for (size_t i = 0; i < _submodels.size(); ++i)
7474
{
7575
if (val < _submodels[i].max_value)
7676
{
77-
_active_index = i;
77+
active_index = i;
7878
break;
7979
}
8080
}
81-
81+
if (active_index == _active_index) // No change to active model, so nothing to do
82+
{
83+
return;
84+
}
85+
// Setting _active_index puts the model in the RT path, so prewarm before doing that
8286
const double sr = mHaveExternalSampleRate ? mExternalSampleRate : mExpectedSampleRate;
83-
_active_model().ResetAndPrewarm(sr, GetMaxBufferSize());
87+
_submodels[active_index].model->ResetAndPrewarm(sr, GetMaxBufferSize());
88+
89+
// Finally set when we're ready:
90+
_active_index = active_index;
8491
}
8592

8693
// =============================================================================

tools/test/test_container.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,8 @@ void test_container_default_is_max_size()
314314
NAM_SAMPLE* in_ptr = input.data();
315315
NAM_SAMPLE* out_ptr;
316316

317+
// Ensure both predictions start from identical model state.
318+
dsp->ResetAndPrewarm(sample_rate, buffer_size);
317319
// Process with default (should be max size)
318320
out_ptr = out_default.data();
319321
dsp->process(&in_ptr, &out_ptr, buffer_size);
@@ -322,6 +324,7 @@ void test_container_default_is_max_size()
322324
auto* slimmable = dynamic_cast<nam::SlimmableModel*>(dsp.get());
323325
assert(slimmable != nullptr);
324326
slimmable->SetSlimmableSize(1.0);
327+
dsp->ResetAndPrewarm(sample_rate, buffer_size);
325328
out_ptr = out_max.data();
326329
dsp->process(&in_ptr, &out_ptr, buffer_size);
327330

0 commit comments

Comments
 (0)