Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KV-cache for T5 model #1358

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions megatron/core/models/T5/t5_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,10 @@ def forward(
# Decoder position ids
decoder_position_ids = t5_position_ids(decoder_input_ids)

# Add offset to the postion_ids when kv-cache is enabled
if inference_params is not None:
decoder_position_ids = decoder_position_ids + inference_params.sequence_len_offset

# Decoder embedding.
if self.pre_process:
decoder_input = self.embedding(
Expand Down
3 changes: 2 additions & 1 deletion megatron/core/transformer/transformer_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,12 @@ def forward(
pre_cross_attn_layernorm_output = self.pre_cross_attn_layernorm(hidden_states)

# Cross attention.
# Cross attention do not need kv_cache, we set the inference_params to None
attention_output_with_bias = self.cross_attention(
pre_cross_attn_layernorm_output,
attention_mask=context_mask,
key_value_states=context,
inference_params=inference_params,
inference_params=None,
)

if isinstance(attention_output_with_bias, dict) and "context" in attention_output_with_bias:
Expand Down