HuBERT - KV Cache and other optimizations
The paper says audio feature extraction adapts a KV cache strategy to provide pseudo-contextual information to implement real time processing on CPU.
We employ the HuBERT [33] for audio feature extraction, incorporating transformer [34] acceleration techniques to enable realtime streaming audio processing. Specifically, we adopt a KV cache strategy [35] to provide pseudo-contextual information extracted from a fixed corpus, concatenated with incoming audio stream segments. This approach allows the HuBERT model pre-trained on long utterances to maintain high-quality features even on extremely short audio segments. Complementarily, we implement causal mask to ensure proper attention allocation to the actual input while significantly reducing computational complexity. These optimizations enable our model to process 0.4-second audio stream units in realtime on CPU environment. We utilize these enhanced features for both model training and inference.
But from the source code it is not implemented in that way. The demo code executes the inferrence on GPU with 3+5+2 clips, so it does not reuse any contextual information and each inferrence is performed individually. This means it causes redundant computations, and the HuBERT model is the biggest one and increased a lot of load to GPU. Do you have the clue where we can find the model which can predict on CPU in real time?
Thanks in advance
@wangjia184 For convenience, our default configuration is set up to run all models on GPU using TensorRT. However, you can run the hubert.onnx model directly on CPU by modifying the corresponding configuration in cfg['base_cfg']['hubert_cfg']. Just update the execution provider settings to use CPU instead.
Hi @digital-avatar
The question was not regarding CPU or GPU, it is about the optimization (e.g. KV cache strategy)
Specifically, we adopt a KV cache strategy [35] to provide pseudo-contextual information extracted from a fixed corpus, concatenated with incoming audio stream segments. This approach allows the HuBERT model pre-trained on long utterances to maintain high-quality features even on extremely short audio segments.
It sounds that, to implement the KV cache strategy, some intermediate layers' result should be cached and supply to the model in the next run?
Further, the paper specifies that the pseudo-context comes "from a fixed corpus". This implies they pre-compute and store the Key-Value (KV) pairs from one or more representative audio samples (likely containing phonemes, background sounds, speaker characteristics, etc. relevant to the task).
I am wondering how to implement the optimization mentioned in the paper
@wangjia184 To be more specific, we first use a short audio clip (approximately tens of seconds) containing both silence and TTS-generated speech. After processing this audio through HuBERT, we save the past_key_value outputs from each attention layer.
During actual inference (and process training-set), these pre-saved past_key_value values are fixed and used as initial states. They have already been embedded as attributes in the hubert.onnx model for convenience.
This helps stabilize the model's behavior and improves efficiency during real-time inference.
Thanks @digital-avatar
The paper states that a KV cache strategy is used to provide pseudo-contextual information from previous audio segments, enabling real-time processing on CPU. However, when inspecting the provided hubert.onnx model (using Netron), I notice that the model only has one input (for the audio) and does not have additional inputs for the past key/value states (which are typically required for transformer models using KV caching).
Without these inputs, it appears that the model cannot reuse the key/value states from previous chunks, which would lead to redundant computations and prevent achieving the real-time CPU performance described.
Could you please clarify:
- Is my expectation correct?
- Is the provided ONNX model the one that includes the KV cache optimization?
- If not, would it be possible to provide the optimized model or the code that implements the KV cache?
Alternatively, if the optimization is applied in a different way (e.g., within the model structure itself), could you explain how it works? This would greatly help in understanding and reproducing the real-time CPU inference as described.
I also found this, and since I know Hubert uses bi-directional attention, which cannot utilize a KV cache, I am really curious to know how the author implements this or if they have pretrained a new streaming version of Hubert?
I also found this, and since I know Hubert uses bi-directional attention, which cannot utilize a KV cache, I am really curious to know how the author implements this or if they have pretrained a new streaming version of Hubert?
I sketch out the difference between our streaming approach and offline bidirectional attention. The new streaming inputs use unidirectional attention, but the pre-computed fixed parts that we concatenate still have bidirectional attention.