Skip to content

Update T5 Onnx Export and Optimization - #23949

Merged
tianleiwu merged 10 commits into
mainfrom
tlwu/t5_onnx_new_format
Mar 23, 2025
Merged

Update T5 Onnx Export and Optimization#23949
tianleiwu merged 10 commits into
mainfrom
tlwu/t5_onnx_new_format

Conversation

@tianleiwu

@tianleiwu tianleiwu commented Mar 8, 2025

Copy link
Copy Markdown
Contributor

Description

Previously, the encoder onnx model adds extra initialization for decoder to generate kv cache from prompt. It is not necessary. Here we redesign onnx export for T5 model to output two separate models for encode and decoder.

Move Linear that generates cross features based on encoder_hidden_states to encoder onnx model. In this way, the encoder does not need output encoder_hidden_states, and only need output the features for cross attention used in decoder.

Major changes:
-[x] update t5 onnx export script
-[x] update convert_generation script
-[x] update beam search to support changes of inputs and outputs (detail can be found below).
-[x] add a tiny t5 model, and enable the generation test for T5 in Linux CI pipelines.

Example change in inputs and outputs for one layer model:
Encoder Inputs:

  • encoder_input_ids: int32 (B, encode_sequence_length)
  • encoder_attention_mask: int32 (B, encode_sequence_length)
  • decoder_input_ids: int32 (B, 1)

Encoder Outputs:

  • logits: (B, 1, vocab_size)
  • encoder_hidden_states: (B, encode_sequence_length, encoder_hidden_size)
  • present_key_self_0: (B, num_heads, 1, head_size)
  • present_value_self_0: (B, num_heads, 1, head_size)
  • present_key_cross_0: (B, num_heads, encode_sequence_length, head_size)
  • present_value_cross_0: (B, num_heads, encode_sequence_length, head_size)

Decoder Inputs:

  • input_ids: int32 (B, 1)
  • encoder_input_ids: int32 (B, encode_sequence_length) (optional for old format; removed in new format)
  • encoder_attention_mask: int32 (B, encode_sequence_length)
  • encoder_hidden_states: (B, encode_sequence_length, encoder_hidden_size) (optional for old format; removed in new format)
  • past_key_self_0: (B, num_heads, past_decode_sequence_length, head_size)
  • past_value_self_0: (B, num_heads, past_decode_sequence_length, head_size)
  • past_key_cross_0: (B, num_heads, encode_sequence_length, head_size)
  • past_value_cross_0: (B, num_heads, encode_sequence_length, head_size)

Decoder Outputs:

  • logits: (B, 1, vocab_size)
  • present_key_self_0: (B, num_heads, past_decode_sequence_length + 1, head_size)
  • present_value_self_0: (B, num_heads, past_decode_sequence_length + 1, head_size)

Known issues:

  • Some postprocessing (like converting to use decoder masked MHA, past and present buffer sharing) is not done. Could be a future work item to integrate with onnxruntime-genai.

Motivation and Context

Make the encoder onnx model simpler and more efficient in inference (no need to output encoder_hidden_states).

@tianleiwu
tianleiwu marked this pull request as draft March 8, 2025 02:13
@tianleiwu
tianleiwu force-pushed the tlwu/t5_onnx_new_format branch from 435dae0 to 5ce3639 Compare March 10, 2025 07:09
Comment on lines +3101 to +3092
def test_gpt_model(
args: argparse.Namespace,
sentences: list[str] | None = None,
is_greedy: bool = False,
):

Check notice

Code scanning / CodeQL

Explicit returns mixed with implicit (fall through) returns

Mixing implicit and explicit returns may indicate an error as implicit returns always return None.

Copilot Autofix

AI over 1 year ago

To fix the problem, we need to add an explicit return statement at the end of the test_gpt_model function to ensure that it always returns a value explicitly. This will make it clear to other developers that the function can return None and that this is intentional.

  • Add an explicit return None statement at the end of the test_gpt_model function.
  • This change should be made in the file onnxruntime/python/tools/transformers/convert_generation.py.
Suggested changeset 1
onnxruntime/python/tools/transformers/convert_generation.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/onnxruntime/python/tools/transformers/convert_generation.py b/onnxruntime/python/tools/transformers/convert_generation.py
--- a/onnxruntime/python/tools/transformers/convert_generation.py
+++ b/onnxruntime/python/tools/transformers/convert_generation.py
@@ -3297,2 +3297,3 @@
 
+    return None
 def test_t5_model(args: argparse.Namespace, sentences: list[str] | None = None):
EOF
@@ -3297,2 +3297,3 @@

return None
def test_t5_model(args: argparse.Namespace, sentences: list[str] | None = None):
Copilot is powered by AI and may make mistakes. Always verify output.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can commit the suggested changes from lintrunner.

Comment thread onnxruntime/python/tools/transformers/models/t5/convert_to_onnx.py
Comment thread onnxruntime/python/tools/transformers/models/t5/convert_to_onnx.py
Comment thread onnxruntime/python/tools/transformers/models/t5/convert_to_onnx.py
Comment thread onnxruntime/python/tools/transformers/models/t5/convert_to_onnx.py
Comment thread onnxruntime/python/tools/transformers/models/t5/convert_to_onnx.py
Comment thread onnxruntime/python/tools/transformers/models/t5/t5_helper.py
Comment thread onnxruntime/python/tools/transformers/models/t5/t5_helper.py
Comment thread onnxruntime/python/tools/transformers/models/t5/t5_helper.py
Comment thread onnxruntime/python/tools/transformers/models/t5/t5_helper.py
@tianleiwu
tianleiwu marked this pull request as ready for review March 15, 2025 01:24
Comment thread onnxruntime/test/testdata/transformers/tiny_t5/tiny_t5.py Fixed
@tianleiwu
tianleiwu force-pushed the tlwu/t5_onnx_new_format branch from 1b69e4f to 2df8206 Compare March 21, 2025 06:46
Comment thread onnxruntime/python/tools/transformers/onnx_model_t5.py Fixed
@tianleiwu tianleiwu changed the title Update T5 Onnx Export Update T5 Onnx Export and Optimization Mar 21, 2025
Comment thread onnxruntime/contrib_ops/cpu/transformers/subgraph_t5_decoder.cc Outdated
Comment thread onnxruntime/python/tools/transformers/models/t5/convert_to_onnx.py Outdated
@kunal-vaishnavi

Copy link
Copy Markdown
Contributor

Adding a note here to check that the changes to the encoder and decoder subgraphs for T5 do not affect Whisper. There are CI tests here that were added to ensure backwards compatibility with the WhisperBeamSearch op.

Comment thread onnxruntime/python/tools/transformers/onnx_model_t5.py Outdated
Comment thread onnxruntime/python/tools/transformers/onnx_model_t5.py Outdated
Comment thread onnxruntime/test/python/transformers/test_generation.py Outdated
@kunal-vaishnavi

Copy link
Copy Markdown
Contributor

Can we add T5 to the Big Models CI Pipeline?

@tianleiwu

tianleiwu commented Mar 21, 2025

Copy link
Copy Markdown
Contributor Author

Can we add T5 to the Big Models CI Pipeline?

It is not needed. The T5 tests in test_generation.py is enabled in CI pipeline so there is end to end tests there.

Comment thread onnxruntime/python/tools/transformers/benchmark_helper.py Fixed
Comment thread onnxruntime/contrib_ops/cpu/transformers/subgraph_t5_decoder.cc
Comment thread onnxruntime/python/tools/transformers/convert_generation.py Outdated
Comment thread onnxruntime/python/tools/transformers/models/t5/convert_to_onnx.py Outdated
@tianleiwu
tianleiwu merged commit d84314c into main Mar 23, 2025
@tianleiwu
tianleiwu deleted the tlwu/t5_onnx_new_format branch March 23, 2025 04:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants