Fix LSTM real-time safety (#218)#299
Open
rhaist wants to merge 1 commit into
Open
Conversation
LSTMCell::get_hidden_state() returned an Eigen::VectorXf by value, which heap-allocated on every call. Since the LSTM runs sample-by-sample on the audio thread, this allocated once per inter-layer hop and once for the head on every single sample (e.g. ~48k * num_layers allocations/sec), making the process() hot path not real-time safe. Return a non-owning Eigen::Ref<const VectorXf> view into the internal state instead, and accept the same type in LSTMCell::process_() so the view binds without copying. Also split the gate matmul into a noalias() product plus a separate bias add so the matrix-vector product evaluates directly into the pre-allocated buffer. Add tools/test/test_lstm_realtime_safe.cpp, modeled on the existing WaveNet/FiLM real-time-safety tests, using the allocation-tracking harness to assert that LSTM::process() performs zero allocations and zero frees across single-layer, multi-layer, multi-channel, large-hidden, and consecutive-call cases. Verified the new tests catch the regression (64 allocs/64 frees over 64 frames before the fix; 0/0 after).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #218 —
LSTMis not real-time safe.LSTMCell::get_hidden_state()returned anEigen::VectorXfby value, which heap-allocates on every call. Since the LSTM runs sample-by-sample on the audio thread, this allocated once per inter-layer hop and once for the head on every single sample (~48k × num_layers allocations/sec), makingprocess()not real-time safe.Changes
get_hidden_state()now returns a non-owningEigen::Ref<const VectorXf>view into the internal state instead of a by-value copy.LSTMCell::process_()accepts the sameEigen::Refso the view binds without copying.noalias()product plus a separate bias add, so the matrix-vector product evaluates directly into the pre-allocated_ifgobuffer with no temporary.Tests
Adds
tools/test/test_lstm_realtime_safe.cpp, modeled on the existing WaveNet/FiLM real-time-safety tests, using the allocation-tracking harness to assertLSTM::process()performs zero allocations and zero frees across single-layer, multi-layer, multi-channel, large-hidden, and consecutive-call cases.Verified the new tests catch the regression: 128 allocs / 128 frees before the fix, 0 / 0 after. Full
run_testssuite passes;lstm.namruns clean end-to-end.