Commit a01bae9
Merge 3 existing PR related to OnnxDiscrepancyCheck + llama.cpp integration (microsoft#2546)
## Describe your changes
Merges microsoft#2536, microsoft#2535, microsoft#2534.
Additionally adds llama.cpp integration and other improvements to
`OnnxDiscrepancyCheck`:
- **New `llama_cpp` flag** (`bool`, default `False`) on
`OnnxDiscrepancyCheck` — when enabled, converts the reference
HuggingFace model to GGUF format using `convert_hf_to_gguf.py` from
llama.cpp and compares inference with llama.cpp.
- **New `llama_cpp_env_path` parameter** (`Optional[str]`) — path to the
`llama_env` virtual environment where `llama-cpp-python` and
`convert_hf_to_gguf.py` are installed (defaults to `"llama_env"`
relative to cwd). The virtual environment also isolates potentially
conflicting versions of `torch`, `transformers`, etc.
- **`compare_llama_cpp()` method** — saves the reference model and
tokenizer to `output_dir/hf_model` (alongside the saved test
model/report output) using `save_pretrained` (standard HuggingFace
format), then:
1. Calls `convert_hf_to_gguf.py` (the official llama.cpp conversion CLI)
inside `llama_env` via subprocess to produce a GGUF F32 file at
`output_dir/model.gguf`.
2. Runs a self-contained inference helper script inside `llama_env` via
`subprocess.run` to measure first-token latency with `llama_cpp.Llama`.
Results include first-token match vs PyTorch, `llama_cpp_ttft_s`,
`llama_cpp_ttfn_s`, `llama_cpp_total_time_s`,
`llama_cpp_speedup_vs_pytorch`, and `llama_cpp_speedup_vs_onnx`. All
`llama_cpp`/`gguf` imports are strictly isolated to the subprocess — the
main Olive process never imports them.
- **New `--test_llama_path` CLI option** — specifies the path to the
`llama_env` virtual environment when running `olive optimize --test`.
When provided, the injected `OnnxDiscrepancyCheck` pass automatically
enables `llama_cpp=True` and forwards the path as `llama_cpp_env_path`.
Using `--test_llama_path` without `--test` emits a warning.
- **Improved `--test_metrics` parsing** — now accepts both
space-separated (`--test_metrics mae speedup`) and comma-separated
(`--test_metrics mae,speedup`) forms. A `_parse_test_metrics` type
function handles splitting and validation per token, and a
`_flatten_test_metrics` helper normalises the result before it is
forwarded to the pass.
- **Fixed `add_discrepancy_check_pass` update-in-place** — when a config
was previously generated by `olive optimize --dry_run --test`, the
`OnnxDiscrepancyCheck` pass was already present and was silently skipped
on subsequent `olive run --test --test_metrics …` calls. The function
now updates the existing pass in-place, refreshing
`reference_model_path` (resolved to absolute), `report_output_dir`,
metric settings (`max_mae` / `timing_iterations`), and llama.cpp
settings — so `--test_metrics`, `--output_path`, and `--test_llama_path`
from `olive run` always take effect.
- **Fixed test model persistence across engine cache hits** —
`ModelBuilder` now copies the test model directory to
`reference_hf_model/` alongside the generated ONNX output in the engine
cache. `OnnxDiscrepancyCheck` falls back to this cached copy when
`reference_model_path` (e.g. `out/tiny-test`) no longer exists on disk,
fixing the `OSError: Repo id must be in the form 'repo_name' or
'namespace/repo_name'` error that occurred on subsequent `olive run`
calls that hit the model-builder cache.
- **New `SaveTestModelConfig` pass**
(`olive/passes/pytorch/save_test_model_config.py`) — a new Olive pass
injected at the beginning of the passes list when `--test` is active. It
takes an `HfModelHandler`, writes `config.json` (with the reduced
hidden-layer count) and the Olive test-model marker file to
`test_model_path`, and returns the model unchanged. This ensures
`test_model_path` always exists as a config-only directory before
`ModelBuilder` or any downstream pass needs it, replacing the previous
`_save_test_model_config_for_dry_run` standalone function.
- **CI workflow** (`test-model-fast.yml`) — new step that creates a
`llama_env` virtual environment, installs `gguf`, `safetensors`,
`llama-cpp-python` (from pre-built CPU wheels at
`https://abetlen.github.io/llama-cpp-python/whl/cpu`), `transformers`,
`sentencepiece`, and `protobuf`, and downloads `convert_hf_to_gguf.py`
from the llama.cpp GitHub repository.
- **Updated documentation** (`cli-fast-test.md`) — clarifies WHERE the
2-layer reduction happens (`_apply_test_model_config` in
`olive/common/hf/utils.py`, called during the model-builder pass of
`olive run`) and WHEN `out/tiny-test` is created (by the
`SaveTestModelConfig` pass on first `olive run`, completed with weights
by `ModelBuilder`); documents the cache fallback behaviour; explains the
`--test_llama_path` option and `llama_env` setup; and clarifies that
`--test_metrics` is always respected even when the config was generated
by `olive optimize --dry_run --test`.
## Checklist before requesting a review
- [ ] Add unit tests for this change.
- [ ] Make sure all tests can pass.
- [ ] Update documents if necessary.
- [ ] Lint and apply fixes to your code by running `lintrunner -a`
- [ ] Is this a user-facing change? If yes, give a description of this
change to be included in the release notes.
## (Optional) Issue link
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Xiaoyu <85524621+xiaoyu-work@users.noreply.github.com>1 parent a477918 commit a01bae9
17 files changed
Lines changed: 2423 additions & 220 deletions
File tree
- .github/workflows
- docs/source/how-to/cli
- olive
- cli
- common/hf
- passes
- onnx
- pytorch
- test
- cli
- common
- passes
- onnx
- pytorch
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
33 | 45 | | |
34 | 46 | | |
35 | 47 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
10 | | - | |
11 | 9 | | |
12 | 10 | | |
13 | 11 | | |
| |||
17 | 15 | | |
18 | 16 | | |
19 | 17 | | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
| 18 | + | |
34 | 19 | | |
35 | 20 | | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
| 21 | + | |
42 | 22 | | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
62 | 27 | | |
63 | | - | |
| 28 | + | |
64 | 29 | | |
65 | | - | |
| 30 | + | |
66 | 31 | | |
67 | | - | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
68 | 37 | | |
69 | | - | |
70 | | - | |
71 | | - | |
| 38 | + | |
72 | 39 | | |
73 | | - | |
| 40 | + | |
74 | 41 | | |
75 | | - | |
76 | | - | |
77 | | - | |
| 42 | + | |
0 commit comments