|
7 | 7 | # TODO: reenable pyre after fixing the issues |
8 | 8 | # pyre-ignore-all-errors |
9 | 9 |
|
10 | | -import copy |
11 | | -import getpass |
12 | 10 | import json |
13 | 11 | import logging |
14 | 12 | import os |
|
17 | 15 | from typing import Dict |
18 | 16 |
|
19 | 17 | import torch |
20 | | -from executorch.backends.qualcomm._passes import FoldQDQ, TagQuantIO |
21 | | -from executorch.backends.qualcomm._passes.i64_to_i32 import I64toI32 |
22 | | -from executorch.backends.qualcomm._passes.qnn_pass_manager import ( |
23 | | - get_capture_program_passes, |
24 | | -) |
25 | | -from executorch.backends.qualcomm._passes.utils import ( |
26 | | - get_passes_dependency_for_capture_program, |
27 | | -) |
28 | | - |
29 | | -from executorch.backends.qualcomm.builders.utils import is_graph_output |
30 | | -from executorch.backends.qualcomm.quantizer.custom_annotation import ( |
31 | | - annotate_prefill_kv_output, |
32 | | -) |
33 | | - |
34 | | -from executorch.backends.qualcomm.serialization.qc_schema import QcomChipset |
35 | | - |
36 | | -from executorch.backends.qualcomm.utils.constants import ( |
37 | | - QCOM_PASS_ACTIVATE_KEY, |
38 | | - QCOM_PASS_ARGS_KWARGS_DEFAULTS_KEY, |
39 | | -) |
40 | 18 | from executorch.backends.qualcomm.utils.utils import ( |
41 | 19 | generate_htp_compiler_spec, |
42 | 20 | generate_qnn_executorch_compiler_spec, |
|
49 | 27 | from executorch.examples.qualcomm.oss_scripts.llama.dataset import DatasetBuilder |
50 | 28 | from executorch.examples.qualcomm.oss_scripts.llama.decoder_constants import ( |
51 | 29 | AUDIO_ENCODER, |
| 30 | + DECODE_QDQ_FILENAME, |
52 | 31 | DECODER_GRAPH_NAMES, |
53 | 32 | EVAL_MODE, |
54 | 33 | PROMPT_EVAL, |
|
65 | 44 | SqnrEval, |
66 | 45 | TaskEval, |
67 | 46 | ) |
68 | | -from executorch.examples.qualcomm.oss_scripts.llama.decoder_utils import ( |
69 | | - graph_module_inference, |
70 | | -) |
71 | | -from executorch.examples.qualcomm.oss_scripts.llama.model.static_llama import ( |
72 | | - LlamaModel, |
73 | | - ModelArgs, |
74 | | -) |
75 | | -from executorch.examples.qualcomm.oss_scripts.llama.range_setting_pt2e import ( |
76 | | - make_custom_quantizer, |
77 | | -) |
78 | | -from executorch.examples.qualcomm.oss_scripts.llama.static_llm_quant_recipe import ( |
79 | | - StaticLLMQuantRecipe, |
80 | | -) |
81 | 47 | from executorch.examples.qualcomm.oss_scripts.llama.tokenizer import TokenizerWrapper |
82 | 48 | from executorch.examples.qualcomm.oss_scripts.llama.wrappers import ( |
83 | 49 | MultiModalManager, |
84 | 50 | next_power_of_two, |
85 | 51 | ) |
86 | | - |
87 | 52 | from executorch.examples.qualcomm.utils import setup_common_args_and_variables |
88 | | -from executorch.exir.capture._config import ExecutorchBackendConfig |
89 | | -from executorch.exir.dialects._ops import ops as exir_ops |
90 | | -from executorch.exir.passes.memory_planning_pass import MemoryPlanningPass |
91 | | -from executorch.extension.llm.custom_ops import model_sharding |
92 | | -from executorch.extension.llm.export.builder import DType |
93 | | -from pytorch_tokenizers import get_tokenizer, TiktokenTokenizer |
94 | | -from pytorch_tokenizers.llama2c import Llama2cTokenizer as SentencePieceTokenizer |
| 53 | +from torchao.quantization.utils import compute_error |
95 | 54 |
|
96 | 55 |
|
97 | 56 | sys.setrecursionlimit(4096) |
@@ -267,15 +226,41 @@ def inference( |
267 | 226 | tokenizer=tokenizer, |
268 | 227 | runtime_tokenizer_path=runtime_tokenizer_path, |
269 | 228 | ) |
270 | | - sqnr = sqnr_evaluator.run(prompt=prompt) |
271 | | - logging.info(f"SQNR Eval Score: {sqnr}") |
| 229 | + sqnr, golden_logits, _ = sqnr_evaluator.run(prompt=prompt) |
| 230 | + logging.info(f"SQNR Eval Score between FP32 nn.Module and QNN: {sqnr}") |
272 | 231 | eval_results.update( |
273 | 232 | { |
274 | 233 | "sqnr": sqnr, |
275 | 234 | "inference_speed": sqnr_evaluator.inference_speed, |
276 | 235 | } |
277 | 236 | ) |
278 | 237 |
|
| 238 | + qdq_ep_path = ( |
| 239 | + f"{args.pre_gen_pte}/{DECODE_QDQ_FILENAME}" |
| 240 | + if args.pre_gen_pte |
| 241 | + else f"{args.artifact}/{DECODE_QDQ_FILENAME}" |
| 242 | + ) |
| 243 | + if os.path.exists(qdq_ep_path): |
| 244 | + qdq_ep = torch.export.load(qdq_ep_path) |
| 245 | + qdq_sqnr_evaluator = SqnrEval( |
| 246 | + source_model=qdq_ep.module(), |
| 247 | + example_input=source_model.get_example_inputs(), |
| 248 | + args=args, |
| 249 | + pte_path=pte_path, |
| 250 | + tokenizer=tokenizer, |
| 251 | + runtime_tokenizer_path=runtime_tokenizer_path, |
| 252 | + ) |
| 253 | + qdq_sqnr, cpu_qdq_logits, _ = qdq_sqnr_evaluator.run(prompt=prompt) |
| 254 | + eval_results["qdq_sqnr"] = qdq_sqnr |
| 255 | + logging.info(f"SQNR Eval Score between CPU QDQ and QNN: {qdq_sqnr}") |
| 256 | + logging.info( |
| 257 | + f"SQNR Eval Score between FP32 nn.Module and CPU QDQ: {compute_error(golden_logits, cpu_qdq_logits).item()}" |
| 258 | + ) |
| 259 | + else: |
| 260 | + logging.info( |
| 261 | + f"Couldn't find saved qdq_ep under {qdq_ep_path}, skip eval sqnr for CPU QDQ." |
| 262 | + ) |
| 263 | + |
279 | 264 | if TASKS_EVAL in args.eval_methods: |
280 | 265 | # Generate the eval wrapper |
281 | 266 | ppl_evaluator = TaskEval( |
|
0 commit comments