-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathtest_llama.py
More file actions
133 lines (104 loc) · 4 KB
/
Copy pathtest_llama.py
File metadata and controls
133 lines (104 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# 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
from typing import Tuple
import pytest
import torch
from executorch.backends.arm._passes import InsertCastForOpsWithInt64InputPass
from executorch.backends.arm.test import conftest
from executorch.backends.arm.test.tester.test_pipeline import (
TosaPipelineBI,
TosaPipelineMI,
)
from executorch.examples.models.llama.export_llama_lib import (
build_args_parser,
get_llama_model,
)
from executorch.extension.llm.export.config.llm_config import LlmConfig
input_t = Tuple[torch.Tensor]
# 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__)
class TestLlama:
"""
Test class of Llama models. Type of Llama model depends on command line parameters:
--llama_inputs <path to .pt file> <path to json file> <name of model variant>
Example: --llama_inputs stories110M/stories110M.pt stories110M/params.json stories110m
For more examples and info see examples/models/llama/README.md.
"""
def prepare_model(self):
checkpoint = None
params_file = None
usage = "To run use --llama_inputs <.pt/.pth> <.json> <name>"
if conftest.is_option_enabled("llama_inputs"):
param_list = conftest.get_option("llama_inputs")
if not isinstance(param_list, list) or len(param_list) != 3:
raise RuntimeError(
f"Invalid number of inputs for --llama_inputs. {usage}"
)
if not all(isinstance(param, str) for param in param_list):
raise RuntimeError(
f"All --llama_inputs are expected to be strings. {usage}"
)
checkpoint = param_list[0]
params_file = param_list[1]
model_name = param_list[2]
else:
logger.warning(
"Skipping Llama tests because of missing --llama_inputs. {usage}"
)
return None, None, None
assert os.path.isfile(checkpoint) and os.path.isfile(
params_file
), "Invalid file paths"
logger.info("Running test_llama.py")
# TODO: Enable key value cache
args = [
"--disable_dynamic_shape",
"-c",
checkpoint,
"-p",
params_file,
"--model",
model_name,
]
parser = build_args_parser()
args = parser.parse_args(args)
llm_config = LlmConfig.from_args(args)
llama_model, llama_inputs, llama_meta = get_llama_model(llm_config)
return llama_model, llama_inputs, llama_meta
def test_llama_tosa_MI():
llama_model, llama_inputs, llama_meta = TestLlama().prepare_model()
if llama_model is None or llama_inputs is None:
pytest.skip("Missing model and/or input files")
with torch.no_grad():
pipeline = TosaPipelineMI[input_t](
llama_model,
llama_inputs,
aten_op=[],
exir_op=[],
use_to_edge_transform_and_lower=True,
transform_passes=[InsertCastForOpsWithInt64InputPass()],
)
pipeline.run()
def test_llama_tosa_BI():
llama_model, llama_inputs, llama_meta = TestLlama().prepare_model()
if llama_model is None or llama_inputs is None:
pytest.skip("Missing model and/or input files")
with torch.no_grad():
pipeline = TosaPipelineBI[input_t](
llama_model,
llama_inputs,
aten_op=[],
exir_op=[],
use_to_edge_transform_and_lower=True,
)
pipeline.run()