Support cross attention kv cache #187
Open
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.

To avoid excessive computation we want to support kv cache for cross attention in Whisper.
Fundamentally we only run
k_projandv_projonce on the encoder output hidden state, at the first token generation, then we should keep thekey_statesandvalue_statesand reuse them in all the subsequent token generation.For whisper-large-v3-turbo, where we have 4 layers of decoder:
Without KV cache in
encoder_attn, we are doing 2 1280x1280 MM for each layer, so in total 8 1280x1280 MM for each token generated. This largely impacts token/sec perf number.This PR replaces
encoder_attnwith aWhisperCrossAttentionclass, where we replacesifcondition withtorch.cond. The logic becomes:Notice that we still have 1 extra read and 1 extra write, but it should be much faster than MM.