Skip to content

Upgrade transformers to 4.48.0 for llama2 - #24302

Merged
xadupre merged 46 commits into
mainfrom
syang/transformers-upgrade-llama2
Apr 18, 2025
Merged

Upgrade transformers to 4.48.0 for llama2#24302
xadupre merged 46 commits into
mainfrom
syang/transformers-upgrade-llama2

Conversation

@selenayang888

@selenayang888 selenayang888 commented Apr 3, 2025

Copy link
Copy Markdown
Contributor

Description

Upgrade Transformers to 4.48.0 for llama2, this version deprecated the old format of past_key_value, the current format is DynamicCache. So, we need to add patches to dynamo exporter in llama2.

Thanks to @xadupre who made the changes to add the patches to dynamo exporter, and implements patches to transformers 4.48.0 which don't export and convert dynamic_axes into dynamic shapes.

Comment thread onnxruntime/python/tools/transformers/models/llama/llama_inputs.py Fixed
import numpy as np
import packaging.version as pv
import torch
import transformers

Check notice

Code scanning / CodeQL

Module is imported with 'import' and 'import from'

Module 'transformers' is imported with both 'import' and 'import from'. Module 'onnxruntime.test.python.transformers' is imported with both 'import' and 'import from'.

Copilot Autofix

AI over 1 year ago

To fix the issue, we will:

  1. Remove the from transformers import AutoConfig statement on line 29.
  2. Update all references to AutoConfig in the code to use transformers.AutoConfig instead.
  3. Ensure that the functionality remains unchanged.

This approach eliminates the redundant import while maintaining the same behavior.


Suggested changeset 1
onnxruntime/python/tools/transformers/models/llama/llama_parity.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/models/llama/llama_parity.py b/onnxruntime/python/tools/transformers/models/llama/llama_parity.py
--- a/onnxruntime/python/tools/transformers/models/llama/llama_parity.py
+++ b/onnxruntime/python/tools/transformers/models/llama/llama_parity.py
@@ -28,3 +28,3 @@
 from models.torch_export_patches.cache_helper import make_dynamic_cache
-from transformers import AutoConfig
+
 from transformers.cache_utils import DynamicCache
@@ -36,3 +36,3 @@
 
-def get_sequence_lengths(args: argparse.Namespace, config: AutoConfig):
+def get_sequence_lengths(args: argparse.Namespace, config: transformers.AutoConfig):
     past_sequence_length, curr_sequence_length = (8, 1) if args.use_past_kv else (0, 8)
@@ -42,3 +42,3 @@
 
-def get_inputs(args: argparse.Namespace, config: AutoConfig):
+def get_inputs(args: argparse.Namespace, config: transformers.AutoConfig):
     # Dummy values for parity
@@ -105,3 +105,3 @@
     pytorch_model: None | torch.nn.Module = None,
-    config: None | AutoConfig = None,
+    config: None | transformers.AutoConfig = None,
 ):
EOF
@@ -28,3 +28,3 @@
from models.torch_export_patches.cache_helper import make_dynamic_cache
from transformers import AutoConfig

from transformers.cache_utils import DynamicCache
@@ -36,3 +36,3 @@

def get_sequence_lengths(args: argparse.Namespace, config: AutoConfig):
def get_sequence_lengths(args: argparse.Namespace, config: transformers.AutoConfig):
past_sequence_length, curr_sequence_length = (8, 1) if args.use_past_kv else (0, 8)
@@ -42,3 +42,3 @@

def get_inputs(args: argparse.Namespace, config: AutoConfig):
def get_inputs(args: argparse.Namespace, config: transformers.AutoConfig):
# Dummy values for parity
@@ -105,3 +105,3 @@
pytorch_model: None | torch.nn.Module = None,
config: None | AutoConfig = None,
config: None | transformers.AutoConfig = None,
):
Copilot is powered by AI and may make mistakes. Always verify output.
Comment on lines +10 to +19
def _catch_produce_guards_and_solve_constraints(
previous_function: Callable,
fake_mode: "FakeTensorMode",
gm: "torch.fx.GraphModule",
dynamic_shapes: dict[str, Any] | tuple[Any] | list[Any] | None,
equalities_inputs: "EqualityConstraint", # noqa: F821
original_signature: inspect.Signature,
_is_torch_jit_trace: bool = False,
verbose: int = 0,
):

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 explicit return statements at the end of the functions _catch_produce_guards_and_solve_constraints and patch__check_input_constraints_for_graph. This will ensure that the functions always return a value explicitly, making the code easier to read and understand.

  • For _catch_produce_guards_and_solve_constraints, we will add return None at the end of the function.
  • For patch__check_input_constraints_for_graph, we will also add return None at the end of the function.
Suggested changeset 1
onnxruntime/python/tools/transformers/models/torch_export_patches/patches/patch_torch.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/models/torch_export_patches/patches/patch_torch.py b/onnxruntime/python/tools/transformers/models/torch_export_patches/patches/patch_torch.py
--- a/onnxruntime/python/tools/transformers/models/torch_export_patches/patches/patch_torch.py
+++ b/onnxruntime/python/tools/transformers/models/torch_export_patches/patches/patch_torch.py
@@ -43,3 +43,3 @@
             )
-
+        return None
 
@@ -66,3 +66,3 @@
             )
-
+        return None
 
EOF
@@ -43,3 +43,3 @@
)

return None

@@ -66,3 +66,3 @@
)

return None

Copilot is powered by AI and may make mistakes. Always verify output.
Comment on lines +46 to +52
def patch__check_input_constraints_for_graph(
previous_function: Callable,
input_placeholders: list[torch.fx.Node],
flat_args_with_path,
range_constraints,
verbose: int = 0,
) -> None:

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 function patch__check_input_constraints_for_graph. This ensures that the function consistently returns None when no exception is raised, making the code easier to read and understand.

  • Add an explicit return None statement at the end of the function patch__check_input_constraints_for_graph.
  • This change should be made in the file onnxruntime/python/tools/transformers/models/torch_export_patches/patches/patch_torch.py.
Suggested changeset 1
onnxruntime/python/tools/transformers/models/torch_export_patches/patches/patch_torch.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/models/torch_export_patches/patches/patch_torch.py b/onnxruntime/python/tools/transformers/models/torch_export_patches/patches/patch_torch.py
--- a/onnxruntime/python/tools/transformers/models/torch_export_patches/patches/patch_torch.py
+++ b/onnxruntime/python/tools/transformers/models/torch_export_patches/patches/patch_torch.py
@@ -66,3 +66,3 @@
             )
-
+        return None
 
EOF
@@ -66,3 +66,3 @@
)

return None

Copilot is powered by AI and may make mistakes. Always verify output.
Comment on lines +304 to +306
# if config.print_specializations:
# self.log.warning(
# "Specializing %s to %s", self.var_to_sources[a][0].name(), tgt

Check notice

Code scanning / CodeQL

Commented-out code

This comment appears to contain commented-out code.

Copilot Autofix

AI over 1 year ago

To fix the problem, we should remove the commented-out code entirely. This will clean up the code and eliminate any potential confusion for future developers. If the logging functionality is still required, it should be properly implemented with a configuration check.

  • Remove the commented-out code on lines 304-308.
  • Ensure that the removal does not affect the existing functionality of the code.
Suggested changeset 1
onnxruntime/python/tools/transformers/models/torch_export_patches/patches/patch_torch.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/models/torch_export_patches/patches/patch_torch.py b/onnxruntime/python/tools/transformers/models/torch_export_patches/patches/patch_torch.py
--- a/onnxruntime/python/tools/transformers/models/torch_export_patches/patches/patch_torch.py
+++ b/onnxruntime/python/tools/transformers/models/torch_export_patches/patches/patch_torch.py
@@ -303,7 +303,7 @@
 
-            # if config.print_specializations:
-            #    self.log.warning(
-            #         "Specializing %s to %s", self.var_to_sources[a][0].name(), tgt
-            #     )
-            #     self.log.debug("SPECIALIZATION", stack_info=True)
+
+
+
+
+
         assert msg != "range_refined_to_singleton", (
EOF
@@ -303,7 +303,7 @@

# if config.print_specializations:
# self.log.warning(
# "Specializing %s to %s", self.var_to_sources[a][0].name(), tgt
# )
# self.log.debug("SPECIALIZATION", stack_info=True)





assert msg != "range_refined_to_singleton", (
Copilot is powered by AI and may make mistakes. Always verify output.
Comment on lines +281 to +288
# if input_ids.shape[1] == 0:
# inputs_embeds = inputs_embeds[:, -cache_position.shape[0] :]
# else:
# if cache_position[-1] >= input_ids.shape[1]:
# input_ids = input_ids[:, -cache_position.shape[0] :]
# else:
# if input_ids.shape[1] != cache_position.shape[0]:
# input_ids = input_ids[:, cache_position]

Check notice

Code scanning / CodeQL

Commented-out code

This comment appears to contain commented-out code.

Copilot Autofix

AI over 1 year ago

To fix the problem, we should remove the commented-out code entirely. This will make the code cleaner and less confusing for future developers. The functionality of the code will remain unchanged as the commented-out code is not executed.

Suggested changeset 1
onnxruntime/python/tools/transformers/models/torch_export_patches/patches/patch_transformers.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/models/torch_export_patches/patches/patch_transformers.py b/onnxruntime/python/tools/transformers/models/torch_export_patches/patches/patch_transformers.py
--- a/onnxruntime/python/tools/transformers/models/torch_export_patches/patches/patch_transformers.py
+++ b/onnxruntime/python/tools/transformers/models/torch_export_patches/patches/patch_transformers.py
@@ -279,11 +279,3 @@
         else:
-            # This is the code we need to implemented with torch.cond.
-            # if input_ids.shape[1] == 0:
-            #     inputs_embeds = inputs_embeds[:, -cache_position.shape[0] :]
-            # else:
-            #     if cache_position[-1] >= input_ids.shape[1]:
-            #         input_ids = input_ids[:, -cache_position.shape[0] :]
-            #     else:
-            #         if input_ids.shape[1] != cache_position.shape[0]:
-            #             input_ids = input_ids[:, cache_position]
+            
             def branch_1(inputs_embeds, cache_position):
EOF
@@ -279,11 +279,3 @@
else:
# This is the code we need to implemented with torch.cond.
# if input_ids.shape[1] == 0:
# inputs_embeds = inputs_embeds[:, -cache_position.shape[0] :]
# else:
# if cache_position[-1] >= input_ids.shape[1]:
# input_ids = input_ids[:, -cache_position.shape[0] :]
# else:
# if input_ids.shape[1] != cache_position.shape[0]:
# input_ids = input_ids[:, cache_position]

def branch_1(inputs_embeds, cache_position):
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/llama/llama_inputs.py
Comment thread onnxruntime/python/tools/transformers/models/llama/llama_parity.py
Comment thread onnxruntime/python/tools/transformers/models/llama/llama_parity.py
Comment thread onnxruntime/python/tools/transformers/models/llama/llama_parity.py Fixed

@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/llama/llama_inputs.py
Comment thread onnxruntime/python/tools/transformers/models/llama/llama_parity.py
Comment thread onnxruntime/python/tools/transformers/models/llama/llama_parity.py
xadupre and others added 5 commits April 17, 2025 10:07
…y.py

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…y.py

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…s.py

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

@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/torch_export_patches/__init__.py Outdated
xadupre and others added 4 commits April 17, 2025 16:51
…hes/__init__.py

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…hes/__init__.py

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…hes/__init__.py

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…hes/__init__.py

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

@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/torch_export_patches/__init__.py Outdated
…hes/__init__.py

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

@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.

…hes/__init__.py

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@xadupre
xadupre merged commit c19a496 into main Apr 18, 2025
@xadupre
xadupre deleted the syang/transformers-upgrade-llama2 branch April 18, 2025 07:32
ashrit-ms pushed a commit that referenced this pull request Apr 24, 2025
### Description
Upgrade Transformers to 4.48.0 for llama2, this version deprecated the
old format of past_key_value, the current format is DynamicCache. So, we
need to add patches to dynamo exporter in llama2.

Thanks to @xadupre who made the changes to add the patches to dynamo
exporter, and implements patches to transformers 4.48.0 which don't
export and convert dynamic_axes into dynamic shapes.

---------

Co-authored-by: xadupre <xadupre@microsoft.com>
Co-authored-by: Xavier Dupré <xadupre@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
intbf pushed a commit to intbf/onnxruntime that referenced this pull request Apr 25, 2025
### Description
Upgrade Transformers to 4.48.0 for llama2, this version deprecated the
old format of past_key_value, the current format is DynamicCache. So, we
need to add patches to dynamo exporter in llama2.

Thanks to @xadupre who made the changes to add the patches to dynamo
exporter, and implements patches to transformers 4.48.0 which don't
export and convert dynamic_axes into dynamic shapes.

---------

Co-authored-by: xadupre <xadupre@microsoft.com>
Co-authored-by: Xavier Dupré <xadupre@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Signed-off-by: bfilipek <bartlomiej.filipek@intel.com>
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.

5 participants