Skip to content

Qualcomm AI Engine Direct - [GenAI Pipeline] PRA2 - Runner and Evaluator - #25353

Open
DannyYuyang-quic wants to merge 1 commit into
pytorch:mainfrom
CodeLinaro:dev1/danny/pr-A2
Open

DannyYuyang-quic wants to merge 1 commit into
pytorch:mainfrom
CodeLinaro:dev1/danny/pr-A2

Conversation

@DannyYuyang-quic

Copy link
Copy Markdown
Contributor

Summary

This PR is the second of Phase 2 Stream A and closes Phase 2. A1 prepared and quantized LLM/MLLM models, and B1/B2 lowered them to .pte. A2 adds the inference stage:

  • deploying the compiled .pte artifacts on an Android/OE-Linux device or on the x86 host;
  • running the evaluation methods requested on the CLI (prompt_eval, tasks_eval, sqnr_eval) and returning one result per method;
  • the remaining CLI wiring, so the pipeline runs end to end for LLM and MLLM models;
  • the llama_stories_260k parity test against legacy llama.py, which gates the Phase 3 deletion of the legacy flow.

Nothing under examples/ changes. The legacy llama.py device-evaluator flow remains CI's reference until Phase 3. Every external call (Device, the x86 runner, lm_eval, PTE metadata) stays behind an injectable adapter or platform.

What's included

Inference strategy (strategies/inference/executorch_inference_strategy.py)

  • Drives one runner adapter and a list of evaluators.
  • Resolves the inputs once at entry: PTE paths from --pre-gen-pte when compilation was skipped, and the runtime tokenizer override. Everything inside inference reads input_config.runtime_tokenizer_path only.
  • Pushes the artifacts once for all methods, wraps the adapter as a DeployedModel for each evaluator, and builds each method's InferenceResult with throughput plus results.json / performance.json.

Runner adapters (strategies/inference/)

  • llm_runner_adapter.py / mllm_runner_adapter.py (new): qnn_llama_runner / qnn_multimodal_runner adapters. They own the runner flags and decode the runner outputs (generated text, dumped logits). They do not know where the runner runs.
  • device_runner_adapter.py: the DeviceRunnerAdapter protocol (push_artifacts → get_metadata → push_inputs → execute → pull_results) is now request-typed instead of taking a free-form options dict.
  • default_device_runner_adapter.py: the generic qnn_executor_runner path through export_utils.Device, migrated to the request types. The CLI does not build this path.

Runner platforms (inference/platforms/, new)

  • RunnerPlatform decides how files reach the runner, how it is launched, and how outputs come back.
    • DevicePlatform runs on an Android/OE-Linux device over adb through Device.
    • X86Platform runs the x86 build against the QNN SDK's x86 libraries and rewrites input-list entries to host paths, as the legacy flow does.
  • get_runner_platform(qnn_config, enable_x86_64) selects one from the CLI flags.

Runner I/O and helpers (inference/, new)

  • runner_io.py: typed requests/outputs: GenerateRequest/GenerateOutput, ForwardRequest/ForwardOutput, ExecuteRequest/ExecuteOutput, and RunnerMetadata, read once from the decoder PTE.
  • runner_constants.py: runner flag values (EVAL_MODE, DECODER_MODEL_VERSION, MODALITY_INPUT_FLAG_MAP). A parity test checks them against the legacy decoder_constants until Phase 3.
  • utils.py: PTE selection (get_pte_paths), runtime tokenizer check, ControlArgs with inference overrides, PTE metadata (retrieve_info_from_pte), shared runner flags, logits loading and dequantization (post_process_logits), and MLLM media preprocessing (preprocess_encoder_inputs).
    • These are the framework's own copies of the legacy helpers. preprocess_encoder_inputs still lazily imports its processor, media loaders, modality configs and special tokens from examples/ (see Deliberately deferred).

Evaluation (evaluation/, new)

  • Evaluator.evaluate(model, control_args, input_config, metadata) -> EvaluationScore is stateless. An evaluator decides what to feed the deployed model and how to score its predictions; it knows nothing about adapters, files or platforms.
Method Evaluator Models What it does
prompt_eval PromptEvaluator LLM, MLLM Generates text for --prompt and reports the generations.
tasks_eval LmEvalEvaluator LLM Runs lm_eval tasks (--eval-tasks), answering each model call with a KV-mode runner forward.
sqnr_eval SqnrEvaluator LLM Scores one KV-mode runner forward against host goldens from the FP32 module and the saved QDQ graph.
default DefaultEvaluator — Runs a compiled method on caller-supplied samples through DefaultDeviceRunnerAdapter. Not reachable from the CLI.
  • evaluator_lookup.get_evaluators(eval_methods, multimodal) maps method names to evaluators. Multimodal models support prompt_eval only.

Responsibility split

Layer Owns Does not know
Platform How files reach the runner, how it is launched, how outputs come back What the model is, what the flags mean
Adapter Which runner, its flags, decoding its outputs, PTE metadata Where the runner runs, what to ask, how to score
Evaluator What to feed the deployed model, how to score the predictions Adapters, PTE layout, files, platforms, throughput
Strategy Driving the three above, throughput, result files How to score, how to run

Dependencies point one way: strategies/inference → evaluation / inference → configs. genai_pipeline.py does not depend on evaluation and reads no inference-specific keys.

CLI (cli.py)

  • Inference arguments: prompts, runtime tokenizer, lookahead shape, eval methods/tasks/limit, device/x86 target.
  • Validation that mirrors legacy export_llama, including the tasks_eval / sqnr_eval model-mode checks.
  • skip_quantize for the modality encoder when more than one --image-path is given (deferred from B2).
  • get_inference_stage and result logging.

Legacy parity

  • tasks_eval rejects lookahead and warns for hybrid; sqnr_eval warns for any non-kv mode. Only their forwards run in kv mode; --model-mode is unchanged for the other methods. This matches what legacy actually does: its (TASKS_EVAL or SQNR_EVAL) in eval_methods check only ever tests tasks_eval.
  • With more than one --image-path, the vision encoder skips quantization and compiles in FP16, as legacy llama.py does.
  • --profile-level, --dump-intermediate-outputs, --htp-performance-mode and --direct-build-folder are not exposed. The legacy llama flow inherits them from the shared parser but never wires them into its compile specs or runner commands, and llama has no direct-mode runner.
  • --skip-push is not exposed. Device skips every push under it, including the token files tasks_eval / sqnr_eval push per request and the MLLM media inputs, so those runs would silently read stale inputs on device.
  • --pre-gen-pte reads only directories produced by this pipeline. Legacy llama.py file names depend on model mode and decoder name; an unrecognized directory fails with the list of expected file names.

Config surface

  • InferenceInputConfig: pte_paths replaces artifact_paths; adds artifact_dir, the host model_module / example_inputs SQNR needs, and inference_options. The orchestrator forwards them.
  • InferenceOutputConfig.results: Dict[str, InferenceResult], one per method. This is a public API change: results replaces the inference_results, performance_metrics, eval_results and etdump fields.
  • artifact_keys.pte_file_name(model_name, artifact_key=None): the single PTE naming rule shared by compilation (writes) and inference (--pre-gen-pte lookup): {model}_{key}.pte per component, {model}.pte for a single-graph model. The compilation strategy now calls it; file names do not change.
  • control_args.py: PROMPT_EVAL / TASKS_EVAL / SQNR_EVAL constants, shared by the CLI choices and the evaluator lookup.
  • model_lookup.py: get_modality / validate_modality_inputs for MLLM media inputs.

Deliberately deferred

  • Returning results to CI over a socket (--ip / --port). The legacy CI entry point is unchanged.
  • Profiling, intermediate-output dumping, and direct mode for the LLM runners.
  • Wiring DefaultEvaluator / DefaultDeviceRunnerAdapter into the CLI.
  • Moving TokenizerWrapper, DecoderInference, EagerEvalWrapper, and the processor, media loaders, modality configs and special tokens preprocess_encoder_inputs uses out of examples/. They are shared with A1 and are left for a follow-up decouple PR.

Unit tests (15 new files, 6 updated)

  • New: test_cli.py, test_llama_stories_260k_e2e.py, evaluation/test_{evaluator,evaluator_lookup,prompt,tasks,sqnr,default}.py, inference/test_{runner_io,runner_constants,platforms,inference_utils}.py, strategies/inference/test_{llm_runner_adapter,mllm_runner_adapter,default_device_runner_adapter}.py.
  • Updated: test_executorch_inference_strategy.py, test_inference_stage.py, test_inference_input_config.py, test_genai_pipeline.py, test_artifact_keys.py (pte_file_name), test_tag_ios_parity.py (legacy TextDecoder imported lazily).
  • test_llama_stories_260k_e2e.py is the x86 parity test: it checks generations against legacy llama.py and the sqnr_eval SQNR against a threshold. It is skipped without its environment variables.

PR Review Checklist

  • All new classes follow single responsibility (one class per file) - Yes.
  • All dependencies are injected via constructor with sensible defaults - Yes.
  • All external calls are behind injectable interfaces - Yes (Device, the x86 runner, lm_eval and PTE metadata sit behind a platform or adapter).
  • Unit tests cover every public method - Yes.
  • Docstrings on all public classes and methods - Yes.
  • Type annotations on all function signatures - Yes.
  • Logging follows the strategy in the LLD - Yes.
  • No existing behavior changed - Partly. No examples/ files are touched. Changes to merged Phase 2 code:
    • InferenceOutputConfig fields are replaced by results, and InferenceInputConfig.artifact_paths becomes pte_paths.
    • The DeviceRunnerAdapter protocol is request-typed, and DefaultDeviceRunnerAdapter is migrated to it.
    • The compilation strategy names PTEs through pte_file_name; the names are unchanged.

Related PRs

Phase 2 flow. PR-A1, PR-B1 and PR-B2 are merged. PR-A2 depends on PR-B2 and closes Phase 2 by wiring the runner and adding the parity test that gates the Phase 3 deletion of the legacy flow.

PR-A1 (merged) ──┐
                 ├─► PR-B2 (merged) ──► PR-A2 (this PR)
PR-B1 (merged) ──┘

Phase 1 (merged):

Phase 2:

  • PR B1: Compilation foundation - merged
  • PR A1: Model registry, loader and quantizer adapters, multi-graph quantization - merged
  • PR B2: Multi-graph compilation - merged
  • PR A2: this PR, depends on B2.

Test plan

Run all genai_pipeline tests:

python -m pytest backends/qualcomm/genai_pipeline/tests/ -v

Result:

629 passed, 1 skipped, 222 subtests passed in 18.70s

The skipped test is test_llama_stories_260k_e2e.py, which needs its environment variables.

Run all tests with coverage:

python -m pytest backends/qualcomm/genai_pipeline/tests/ \
  --cov=backends/qualcomm/genai_pipeline \
  --cov-config=backends/qualcomm/.coveragerc \
  --cov-report=term-missing

Result for the files this PR adds or changes:

Name                                                                                                     Stmts   Miss Branch BrPart  Cover   Missing
----------------------------------------------------------------------------------------------------------------------------------------------------
backends/qualcomm/genai_pipeline/artifact_keys.py                                                           12      0      0      0   100%
backends/qualcomm/genai_pipeline/cli.py                                                                    252     36     72     13    84%   432-439, 452, 457, 462, 469, 475, 479, 488, 611-622, 656, 743-755, 780-814, 1049-1050, 1054, 1071
backends/qualcomm/genai_pipeline/compilation/compile_spec_builder.py                                        58      1     18      0    99%   192
backends/qualcomm/genai_pipeline/compilation/component_spec_policy.py                                       48      0     10      0   100%
backends/qualcomm/genai_pipeline/compilation/model_layout.py                                                10      0      0      0   100%
backends/qualcomm/genai_pipeline/compilation/pass_policy.py                                                179      1     68      4    98%   689->696, 701, 724->733, 726->733
backends/qualcomm/genai_pipeline/configs/compilation_input_config.py                                        16      0      0      0   100%
backends/qualcomm/genai_pipeline/configs/compilation_output_config.py                                        8      0      0      0   100%
backends/qualcomm/genai_pipeline/configs/inference_input_config.py                                          15      0      0      0   100%
backends/qualcomm/genai_pipeline/configs/inference_output_config.py                                         13      0      0      0   100%
backends/qualcomm/genai_pipeline/configs/model_preparation_input_config.py                                   7      0      0      0   100%
backends/qualcomm/genai_pipeline/configs/model_preparation_output_config.py                                 15      0      0      0   100%
backends/qualcomm/genai_pipeline/configs/quantization_input_config.py                                       14      0      0      0   100%
backends/qualcomm/genai_pipeline/configs/quantization_output_config.py                                       6      0      0      0   100%
backends/qualcomm/genai_pipeline/control_args.py                                                           163      1     28      2    98%   157->160, 315
backends/qualcomm/genai_pipeline/datasets/calibration/calibration_data_adapter.py                            5      0      0      0   100%
backends/qualcomm/genai_pipeline/datasets/calibration/default_calibration_data_adapter.py                   34      0      6      0   100%
backends/qualcomm/genai_pipeline/datasets/calibration/llm_calibration_data_adapter.py                       31     23     10      0    20%   49-52, 76-113
backends/qualcomm/genai_pipeline/datasets/calibration/mllm_calibration_data_adapter.py                      37     29     16      0    15%   47-50, 74-127
backends/qualcomm/genai_pipeline/datasets/collators/llm_collator.py                                         16     12      6      0    18%   27-58
backends/qualcomm/genai_pipeline/datasets/collators/mllm_collator.py                                        17     13      6      0    17%   28-63
backends/qualcomm/genai_pipeline/datasets/dataset_lookup.py                                                 82     69     32      0    11%   21-72, 77-88, 97-108, 140-144, 148-149, 153-154, 158-159, 163-164, 175-208, 213-217, 225-229
backends/qualcomm/genai_pipeline/datasets/dataset_options.py                                                40      0      2      1    98%   112->115
backends/qualcomm/genai_pipeline/datasets/evaluation/default_evaluation_data_adapter.py                     41     30     16      0    19%   49-112
backends/qualcomm/genai_pipeline/datasets/evaluation/evaluation_data_adapter.py                              5      0      0      0   100%
backends/qualcomm/genai_pipeline/datasets/loaders/llm/hf_dataset_adapter.py                                 16     16      0      0     0%   9-67
backends/qualcomm/genai_pipeline/datasets/loaders/llm/lm_eval_adapter.py                                    17     17      0      0     0%   9-73
backends/qualcomm/genai_pipeline/datasets/loaders/llm/message_sample_adapter.py                             17     17      0      0     0%   9-66
backends/qualcomm/genai_pipeline/datasets/loaders/mllm/message_sample_adapter.py                            24     24      2      0     0%   9-104
backends/qualcomm/genai_pipeline/datasets/training/default_training_data_adapter.py                         41     21     16      1    40%   74-114
backends/qualcomm/genai_pipeline/datasets/training/training_data_adapter.py                                  5      0      0      0   100%
backends/qualcomm/genai_pipeline/engine_proxy.py                                                            20      0      4      0   100%
backends/qualcomm/genai_pipeline/evaluation/default.py                                                      14      0      0      0   100%
backends/qualcomm/genai_pipeline/evaluation/evaluator.py                                                    15      0      0      0   100%
backends/qualcomm/genai_pipeline/evaluation/evaluator_lookup.py                                             16      0      2      0   100%
backends/qualcomm/genai_pipeline/evaluation/prompt.py                                                       11      0      0      0   100%
backends/qualcomm/genai_pipeline/evaluation/sqnr.py                                                         66      0      8      0   100%
backends/qualcomm/genai_pipeline/evaluation/tasks.py                                                        33      0      8      0   100%
backends/qualcomm/genai_pipeline/exceptions.py                                                              20      0      6      0   100%
backends/qualcomm/genai_pipeline/export_inputs.py                                                           11      0      4      0   100%
backends/qualcomm/genai_pipeline/genai_pipeline.py                                                          99      0     12      0   100%
backends/qualcomm/genai_pipeline/graph_bundle.py                                                            21      0      6      0   100%
backends/qualcomm/genai_pipeline/graph_names.py                                                              9      0      0      0   100%
backends/qualcomm/genai_pipeline/graph_roles.py                                                             17      0      4      0   100%
backends/qualcomm/genai_pipeline/inference/platforms/device.py                                              39      0      2      0   100%
backends/qualcomm/genai_pipeline/inference/platforms/x86.py                                                 52      0      8      0   100%
backends/qualcomm/genai_pipeline/inference/runner_constants.py                                               4      0      0      0   100%
backends/qualcomm/genai_pipeline/inference/runner_io.py                                                     54      0     12      0   100%
backends/qualcomm/genai_pipeline/inference/utils.py                                                        106      0     40      1    99%   294->276
backends/qualcomm/genai_pipeline/model_components/embedding.py                                               2      0      0      0   100%
backends/qualcomm/genai_pipeline/model_lookup.py                                                           208    150     78      3    22%   46-48, 64-65, 94-139, 216-317, 347-415, 426-432, 443-445, 476-497, 519-525, 575-592, 602-615, 654, 682-691, 715-732, 756-772
backends/qualcomm/genai_pipeline/pipeline_context.py                                                        52      0     14      0   100%
backends/qualcomm/genai_pipeline/pipeline_stage.py                                                           5      0      0      0   100%
backends/qualcomm/genai_pipeline/quant_utilities.py                                                        140     95     76      1    27%   37, 58-81, 153-165, 177-194, 208-349
backends/qualcomm/genai_pipeline/source_transform/checkpoint_key_remap.py                                    9      1      0      0    89%   27
backends/qualcomm/genai_pipeline/source_transform/dtype_override.py                                          7      0      2      0   100%
backends/qualcomm/genai_pipeline/source_transform/embedding_scale.py                                         7      0      2      0   100%
backends/qualcomm/genai_pipeline/source_transform/linear_to_conv2d.py                                       12      0      6      0   100%
backends/qualcomm/genai_pipeline/source_transform/rms_norm_offset.py                                         9      0      4      0   100%
backends/qualcomm/genai_pipeline/source_transform/rope_layout.py                                            13      0      2      0   100%
backends/qualcomm/genai_pipeline/stages/compilation_stage.py                                                14      0      0      0   100%
backends/qualcomm/genai_pipeline/stages/inference_stage.py                                                  14      0      0      0   100%
backends/qualcomm/genai_pipeline/stages/model_preparation_stage.py                                          14      1      0      0    93%   30
backends/qualcomm/genai_pipeline/stages/quantization_stage.py                                               14      0      0      0   100%
backends/qualcomm/genai_pipeline/strategies/compilation/compilation_strategy.py                              7      0      0      0   100%
backends/qualcomm/genai_pipeline/strategies/compilation/compiler_adapter.py                                 13      0      0      0   100%
backends/qualcomm/genai_pipeline/strategies/compilation/executorch_compilation_strategy.py                 130      0     40      0   100%
backends/qualcomm/genai_pipeline/strategies/inference/device_runner_adapter.py                              13      0      0      0   100%
backends/qualcomm/genai_pipeline/strategies/inference/executorch_inference_strategy.py                     102      0     20      0   100%
backends/qualcomm/genai_pipeline/strategies/inference/inference_strategy.py                                  7      0      0      0   100%
backends/qualcomm/genai_pipeline/strategies/inference/llm_runner_adapter.py                                 72      0     22      0   100%
backends/qualcomm/genai_pipeline/strategies/inference/mllm_runner_adapter.py                                73      1     12      1    98%   130
backends/qualcomm/genai_pipeline/strategies/model_preparation/executorch_model_preparation_strategy.py      92      2     26      1    97%   235-236
backends/qualcomm/genai_pipeline/strategies/model_preparation/llm_loader_adapter.py                         84     20     24      4    76%   103, 152, 178, 185-192, 243-264, 285-298, 360
backends/qualcomm/genai_pipeline/strategies/model_preparation/mllm_loader_adapter.py                       120     32     38      9    72%   128, 135, 141, 149, 152, 232, 282, 367-389, 407-420, 468, 475-479, 487-499, 517
backends/qualcomm/genai_pipeline/strategies/model_preparation/model_loader_adapter.py                       12      0      0      0   100%
backends/qualcomm/genai_pipeline/strategies/model_preparation/model_preparation_strategy.py                  7      0      0      0   100%
backends/qualcomm/genai_pipeline/strategies/quantization/executorch_quantization_strategy.py               200      9     90     15    92%   125->131, 132->138, 458->457, 459->458, 528->604, 530, 554, 568, 607, 609, 651, 682->681, 683->682, 707-708, 742->744, 747
backends/qualcomm/genai_pipeline/strategies/quantization/llm_quantizer_adapter.py                           28      0      4      0   100%
backends/qualcomm/genai_pipeline/strategies/quantization/mllm_quantizer_adapter.py                          31      0      2      0   100%
backends/qualcomm/genai_pipeline/strategies/quantization/quantization_strategy.py                            7      0      0      0   100%
backends/qualcomm/genai_pipeline/strategies/quantization/quantizer_adapter.py                                9      0      0      0   100%
backends/qualcomm/genai_pipeline/unquantized_bundles.py                                                     12      0      4      0   100%
----------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL                                                                                                     3390    621    890     56    79%
======================================================================== 629 passed, 1 skipped, 222 subtests passed in 66.62s (0:01:06) =========================================================================

The remaining uncovered lines:

  • cli.py: --list-models, the TokenizerWrapper call inside _load_runtime_tokenizer_path (patched in tests), and the model preparation / quantization stage construction from A1.
  • model_lookup.py: mostly A1's model-config, source-transform and checkpoint-loading paths. This PR adds get_modality / validate_modality_inputs, which are not fully covered either.
  • mllm_runner_adapter.py:130: a defensive branch generate_inputs cannot reach with non-empty inputs.

default_device_runner_adapter.py is tested by test_default_device_runner_adapter.py but excluded from the report by .coveragerc. The package-wide total (79%) includes A1 modules outside this PR. Coverage will be raised to >=95% by the end of Phase 2.

Confirm the legacy flow is unaffected:

python -c "
from executorch.examples.qualcomm.oss_scripts.llama.llama import _build_parser, export_llama
from executorch.examples.qualcomm.oss_scripts.llama.wrappers import MultiModalManager, HybridAttentionSinkEvictor
print('LEGACY IMPORTS OK')
"

Result:

LEGACY IMPORTS OK

@pytorch-bot

pytorch-bot Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/25353

Note: Links to docs will display an error until the docs builds have been completed.

⚠️ 16 Awaiting Approval

As of commit fe88d55 with merge base 21c7c99 (image):

AWAITING APPROVAL - The following workflows need approval before CI can run:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 20, 2026
@DannyYuyang-quic

Copy link
Copy Markdown
Contributor Author

@pytorchbot label "release notes: qualcomm"

@pytorch-bot pytorch-bot Bot added the release notes: qualcomm Changes to the Qualcomm backend delegate label Nov 20, 2026
@DannyYuyang-quic

Copy link
Copy Markdown
Contributor Author

Hi @psiddh
This is the GenAI pipeline Phase 2-A2 PR. It implements the runners for the inference stage, for both Android devices and the x86 host. It also adds the evaluation methods from the legacy llama.py flow prompt_eval, tasks_eval (lm_eval tasks) and sqnr_eval. With this PR the GenAI Pipeline CLI runs end to end for both LLM and MLLM models.
Please have a look, thanks!

cc: @qti-horodnic @shewu-quic

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. release notes: qualcomm Changes to the Qualcomm backend delegate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant