-
Notifications
You must be signed in to change notification settings - Fork 452
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix bloom KV cache usage in ORTForCausalLM (#1152)
* fix bloom pkv usage with num_beams>1 * Update optimum/onnxruntime/modeling_decoder.py Co-authored-by: Michael Benayoun <mickbenayoun@gmail.com> * Update optimum/onnxruntime/modeling_decoder.py Co-authored-by: Michael Benayoun <mickbenayoun@gmail.com> * Update optimum/onnxruntime/modeling_decoder.py Co-authored-by: Michael Benayoun <mickbenayoun@gmail.com> * remove transformers import --------- Co-authored-by: Michael Benayoun <mickbenayoun@gmail.com>
- Loading branch information
1 parent
bc5f825
commit 2eab7ab
Showing
3 changed files
with
99 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from typing import TYPE_CHECKING, Tuple | ||
|
||
|
||
if TYPE_CHECKING: | ||
import torch | ||
|
||
|
||
def bloom_convert_to_standard_cache( | ||
past_key_value: Tuple[Tuple["torch.Tensor", "torch.Tensor"]], batch_size: int | ||
) -> Tuple[Tuple["torch.Tensor", "torch.Tensor"]]: | ||
""" | ||
Standardizes the format of the cache so as to match most implementations, i.e. to tuple(tuple([batch_size, | ||
num_heads, ...])) | ||
""" | ||
batch_size_times_num_heads, head_dim, seq_length = past_key_value[0][0].shape | ||
num_heads = batch_size_times_num_heads // batch_size | ||
# key: [batch_size * num_heads, head_dim, seq_length] -> [batch_size, num_heads, head_dim, seq_length] | ||
# value: [batch_size * num_heads, seq_length, head_dim] -> [batch_size, num_heads, seq_length, head_dim] | ||
return tuple( | ||
( | ||
layer_past[0].view(batch_size, num_heads, head_dim, seq_length), | ||
layer_past[1].view(batch_size, num_heads, seq_length, head_dim), | ||
) | ||
for layer_past in past_key_value | ||
) | ||
|
||
|
||
def bloom_convert_to_bloom_cache( | ||
past_key_value: Tuple[Tuple["torch.Tensor", "torch.Tensor"]] | ||
) -> Tuple[Tuple["torch.Tensor", "torch.Tensor"]]: | ||
""" | ||
Converts the cache to the format expected by Bloom, i.e. to tuple(tuple([batch_size * num_heads, ...])) | ||
""" | ||
batch_size, num_heads, head_dim, seq_length = past_key_value[0][0].shape | ||
batch_size_times_num_heads = batch_size * num_heads | ||
# key: [batch_size, num_heads, head_dim, seq_length] -> [batch_size * num_heads, head_dim, seq_length] | ||
# value: [batch_size, num_heads, seq_length, head_dim] -> [batch_size * num_heads, seq_length, head_dim] | ||
return tuple( | ||
( | ||
layer_past[0].view(batch_size_times_num_heads, head_dim, seq_length), | ||
layer_past[1].view(batch_size_times_num_heads, seq_length, head_dim), | ||
) | ||
for layer_past in past_key_value | ||
) |