-
Notifications
You must be signed in to change notification settings - Fork 607
Arm backend: Add initial Llama model test case #8679
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
674b125
Arm: Add initial Llama model test case
mansnils e7c117b
Reduce number of delegates in test_llama_arm.py
mansnils 3cfda9b
Merge remote-tracking branch 'upstream/main' into topics
mansnils 8115a6c
Merge remote-tracking branch 'upstream/main' into topics
mansnils 203cea5
Call llama3_2_vision/install_requirements.sh during Arm backend setup
mansnils afbf5e4
Rename test_llama_arm.py to test_llama.py
mansnils 25dfa11
Skip test_llama_tosa_MI if no input parameters are given
mansnils ec90735
Update review comments
mansnils d523b6f
Merge branch 'main' into topics
digantdesai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,39 @@ | ||
# Copyright 2025 Arm Limited and/or its affiliates. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
|
||
import logging | ||
|
||
import torch.fx as fx | ||
from executorch.backends.arm.operator_support.tosa_supported_operators import ( | ||
register_tosa_support_check, | ||
SupportedTOSAOperatorCheck, | ||
) | ||
from executorch.backends.arm.tosa_specification import TosaSpecification | ||
from executorch.backends.arm.tosa_utils import getNodeArgs | ||
from executorch.exir.dialects._ops import ops as exir_ops | ||
|
||
logger = logging.getLogger(__name__) | ||
logger.setLevel(logging.WARNING) | ||
|
||
|
||
@register_tosa_support_check | ||
class SliceCopySupported(SupportedTOSAOperatorCheck): | ||
targets = [exir_ops.edge.aten.slice_copy.Tensor] | ||
|
||
tosa_specs = [ | ||
TosaSpecification.create_from_string("TOSA-0.80+BI"), | ||
TosaSpecification.create_from_string("TOSA-0.80+MI"), | ||
] | ||
|
||
def is_node_tosa_supported(self, node: fx.Node, tosa_spec: TosaSpecification) -> bool: # type: ignore[override, misc] | ||
if tosa_spec not in self.tosa_specs: | ||
return False | ||
|
||
inputs = getNodeArgs(node) | ||
if len(inputs) == 5 and (step := inputs[4].number) != 1: | ||
logging.warning(f"{node.target} with step size of {step} not supported.") | ||
return False | ||
return True |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,120 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# Copyright 2025 Arm Limited and/or its affiliates. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import logging | ||
|
||
import os | ||
import sys | ||
import unittest | ||
|
||
import torch | ||
|
||
from executorch.backends.arm.test import common, conftest | ||
from executorch.backends.arm.test.tester.arm_tester import ArmTester | ||
from executorch.examples.models.llama.export_llama_lib import ( | ||
build_args_parser, | ||
get_llama_model, | ||
) | ||
|
||
|
||
# Add project dir to sys path to workaround importlib.import_module() conditions in model_factory.py | ||
this_files_dir = os.path.dirname(os.path.abspath(__file__)) | ||
project_dir = os.path.abspath(os.path.join(this_files_dir, "../../../..")) | ||
sys.path.append(project_dir) | ||
|
||
logger = logging.getLogger(__name__) | ||
logger.setLevel(logging.INFO) | ||
|
||
|
||
class TestLlama(unittest.TestCase): | ||
""" | ||
Test class of Llama models. Type of Llama model depends on command line parameters: | ||
--llama_inputs <path to .pt file> <path to json file> | ||
Example: --llama_inputs stories110M/stories110M.pt stories110M/params.json | ||
""" | ||
|
||
def prepare_model(self): | ||
|
||
checkpoint = None | ||
params_file = None | ||
if conftest.is_option_enabled("llama_inputs"): | ||
param_list = conftest.get_option("llama_inputs") | ||
assert ( | ||
isinstance(param_list, list) and len(param_list) == 2 | ||
), "invalid number of inputs for --llama_inputs" | ||
checkpoint = param_list[0] | ||
params_file = param_list[1] | ||
assert isinstance(checkpoint, str) and isinstance( | ||
params_file, str | ||
), "invalid input for --llama_inputs" | ||
else: | ||
logging.warning( | ||
"Skipping Llama test because of lack of input. To run use --llama_inputs <.pt> <.json>" | ||
) | ||
return None, None, None | ||
|
||
assert os.path.isfile(checkpoint) and os.path.isfile( | ||
params_file | ||
), "Invalid file paths" | ||
|
||
# TODO: Enable key value cache | ||
args = [ | ||
"--disable_dynamic_shape", | ||
"-c", | ||
checkpoint, | ||
"-p", | ||
params_file, | ||
"--model", | ||
"stories110m", | ||
] | ||
parser = build_args_parser() | ||
args = parser.parse_args(args) | ||
|
||
llama_model, llama_inputs, llama_meta = get_llama_model(args) | ||
|
||
# TODO: Remove workaround since attention mask should not be persistent, | ||
# it only works if input shape is always the same | ||
freqs_c = "freqs_cos" | ||
freqs_s = "freqs_sin" | ||
for i in range(llama_model.n_layers): | ||
val = llama_model.layers[i].attention.get_buffer("mask") | ||
llama_model.layers[i].attention.register_buffer( | ||
"mask", val, persistent=True | ||
) | ||
val = llama_model.layers[i].attention.rope.get_buffer(freqs_c) | ||
llama_model.layers[i].attention.rope.register_buffer( | ||
freqs_c, val, persistent=True | ||
) | ||
val = llama_model.layers[i].attention.rope.get_buffer(freqs_s) | ||
llama_model.layers[i].attention.rope.register_buffer( | ||
freqs_s, val, persistent=True | ||
) | ||
|
||
return llama_model, llama_inputs, llama_meta | ||
|
||
def test_llama_tosa_MI(self): | ||
llama_model, llama_inputs, llama_meta = self.prepare_model() | ||
|
||
if llama_model is None and llama_inputs is None and llama_meta is None: | ||
return | ||
|
||
with torch.no_grad(): | ||
( | ||
ArmTester( | ||
llama_model, | ||
example_inputs=llama_inputs, | ||
compile_spec=common.get_tosa_compile_spec("TOSA-0.80+MI"), | ||
constant_methods=llama_meta, | ||
) | ||
.export() | ||
.to_edge_transform_and_lower() | ||
.check_count({"torch.ops.higher_order.executorch_call_delegate": 14}) | ||
.to_executorch() | ||
.run_method_and_compare_outputs( | ||
inputs=llama_inputs, atol=1.8, rtol=0.01 # TODO: decrease tolerance | ||
) | ||
) |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.