Commit 2f70f0f
Allow overriding past_present_share_buffer in LMEvaluator ortgenai backend (microsoft#2569)
## Describe your changes
The `ortgenai` lm-eval backend (`LMEvalORTGenAIEvaluator`) always
derived `past_present_share_buffer` from the exported
`genai_config.json`, with **no way to override it** from a
recipe/evaluator config. Some models (e.g. **Gemma 4**) export with
shared past/present buffers *enabled* but require it *disabled* for
correct KV-cache handling during evaluation. Additionally, `LMEvaluator`
gave recipe authors no way to enable a model's chat template or few-shot
prompting, so instruction-tuned models were evaluated 0-shot on raw
prompts and scored near-random. Today these gaps force recipe authors to
bypass `LMEvaluator` and write a standalone `eval.py` that calls
`lm_eval.simple_evaluate` directly — the declarative Olive config path
simply produces wrong results for these models.
This PR makes these settings overridable end-to-end and adds optional
sample logging:
- **`LMEvalORTGenAIEvaluator`** now accepts an explicit
`past_present_share_buffer` argument that takes precedence over the
exported value. The resolution logic is extracted into a small
`_resolve_past_present_share_buffer(override, genai_config)` helper
(explicit override wins; otherwise fall back to the exported value,
defaulting to `False`).
- **`LMEvaluator`** gains a `model_args` dict that is forwarded verbatim
to the lm-eval model backend constructor, so genai-specific options can
be set from the evaluator config. User-provided `model_args` override
the Olive-derived defaults (`batch_size`, `max_length`, `ep`, ...).
Default is `{}`, so existing configs are unaffected.
- **`LMEvaluator` prompting controls**: new `apply_chat_template`,
`system_instruction`, `fewshot_as_multiturn` and `num_fewshot` options,
forwarded to `lm_eval.simple_evaluate`. Instruction-tuned models score
near-random on multiple-choice tasks when evaluated 0-shot on raw
prompts; wrapping requests in the model's chat template and matching the
card's few-shot protocol fixes this. Empirically, on a Gemma-4 E2B
export a German MMMLU subset went from ~0.16–0.24 (0-shot, no template)
to ~0.52–0.68 (5-shot + chat template). Defaults preserve lm-eval's
legacy behavior (no chat template, no system prompt, task-default
few-shot), so existing configs are unaffected.
- **`LMEvaluator` sample logging**: a new `sample_log_num` (and optional
`sample_log_dir`) option. When `sample_log_num > 0`, lm-eval's
`log_samples` is enabled and the first N per-task predictions —
question, options, resolved prediction (argmax of choice loglikelihoods
to letter + text, or the raw generation), target, and `acc` — are
written to `<task>_samples.jsonl`. Rather than duplicating the writer,
`OliveEvaluator.save_sample_log` is decoupled to take a `(name,
sample_log_dir)` pair instead of a `Metric`, so `LMEvaluator` reuses the
exact same JSONL writer as the accuracy evaluators; a small
`_lmeval_samples_to_output` helper maps lm-eval sample records into an
`OliveModelOutput` (carrying question/options/prediction_index/acc via
`extras`). This makes it possible to inspect why a task scores low (e.g.
distinguishing a genuinely weak/quantized model from a broken eval).
Default is `0`, so existing configs are unaffected.
With this, such models can be evaluated with a standard `LMEvaluator`
config instead of a bespoke script:
```json
{
"evaluators": {
"mmlu": {
"type": "LMEvaluator",
"tasks": ["mmlu"],
"model_class": "ortgenai",
"num_fewshot": 5,
"apply_chat_template": true,
"fewshot_as_multiturn": true,
"sample_log_num": 100,
"model_args": { "past_present_share_buffer": false }
}
}
}
```
## Checklist before requesting a review
- [x] Add unit tests for this change.
- [x] Make sure all tests can pass.
- [ ] Update documents if necessary.
- [x] Lint and apply fixes to your code by running `lintrunner -a`
- [x] Is this a user-facing change? If yes, give a description of this
change to be included in the release notes.
**Release note:** `LMEvaluator` now accepts a `model_args` dict
forwarded to the lm-eval model backend; `apply_chat_template`,
`system_instruction`, `fewshot_as_multiturn` and `num_fewshot` prompting
options forwarded to `lm_eval.simple_evaluate` (so instruction-tuned
models can be evaluated with their chat template and the card's few-shot
protocol); and a `sample_log_num`/`sample_log_dir` option to log the
first N per-task predictions vs. targets to a JSONL file for debugging
(reusing the accuracy evaluators' `save_sample_log` writer). The
`ortgenai` backend accepts an explicit `past_present_share_buffer`
override (previously only read from `genai_config.json`).
## (Optional) Issue link
---------
Co-authored-by: jiafatom <jiafatom@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent a01bae9 commit 2f70f0f
5 files changed
Lines changed: 311 additions & 39 deletions
File tree
- olive/evaluator
- test
- evaluator
- passes/onnx
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
545 | 545 | | |
546 | 546 | | |
547 | 547 | | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
548 | 559 | | |
549 | 560 | | |
550 | 561 | | |
| |||
553 | 564 | | |
554 | 565 | | |
555 | 566 | | |
| 567 | + | |
556 | 568 | | |
557 | 569 | | |
558 | 570 | | |
| |||
563 | 575 | | |
564 | 576 | | |
565 | 577 | | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
566 | 581 | | |
567 | 582 | | |
568 | 583 | | |
| |||
602 | 617 | | |
603 | 618 | | |
604 | 619 | | |
605 | | - | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
606 | 624 | | |
607 | 625 | | |
608 | 626 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
260 | 260 | | |
261 | 261 | | |
262 | 262 | | |
263 | | - | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
264 | 266 | | |
265 | 267 | | |
266 | 268 | | |
267 | 269 | | |
268 | | - | |
269 | | - | |
270 | | - | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
271 | 275 | | |
272 | 276 | | |
273 | 277 | | |
274 | 278 | | |
275 | 279 | | |
276 | 280 | | |
277 | 281 | | |
278 | | - | |
279 | | - | |
280 | | - | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
281 | 285 | | |
282 | 286 | | |
283 | 287 | | |
| |||
311 | 315 | | |
312 | 316 | | |
313 | 317 | | |
314 | | - | |
| 318 | + | |
315 | 319 | | |
316 | 320 | | |
317 | 321 | | |
318 | | - | |
| 322 | + | |
319 | 323 | | |
320 | 324 | | |
321 | 325 | | |
| |||
793 | 797 | | |
794 | 798 | | |
795 | 799 | | |
796 | | - | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
797 | 803 | | |
798 | 804 | | |
799 | 805 | | |
| |||
1490 | 1496 | | |
1491 | 1497 | | |
1492 | 1498 | | |
1493 | | - | |
| 1499 | + | |
1494 | 1500 | | |
1495 | 1501 | | |
1496 | 1502 | | |
| |||
1694 | 1700 | | |
1695 | 1701 | | |
1696 | 1702 | | |
1697 | | - | |
| 1703 | + | |
| 1704 | + | |
| 1705 | + | |
1698 | 1706 | | |
1699 | 1707 | | |
1700 | 1708 | | |
| |||
1929 | 1937 | | |
1930 | 1938 | | |
1931 | 1939 | | |
1932 | | - | |
| 1940 | + | |
| 1941 | + | |
| 1942 | + | |
1933 | 1943 | | |
1934 | 1944 | | |
1935 | 1945 | | |
| |||
2004 | 2014 | | |
2005 | 2015 | | |
2006 | 2016 | | |
2007 | | - | |
| 2017 | + | |
| 2018 | + | |
| 2019 | + | |
2008 | 2020 | | |
2009 | 2021 | | |
2010 | 2022 | | |
| |||
2056 | 2068 | | |
2057 | 2069 | | |
2058 | 2070 | | |
| 2071 | + | |
| 2072 | + | |
| 2073 | + | |
| 2074 | + | |
| 2075 | + | |
| 2076 | + | |
| 2077 | + | |
| 2078 | + | |
| 2079 | + | |
| 2080 | + | |
| 2081 | + | |
| 2082 | + | |
| 2083 | + | |
| 2084 | + | |
| 2085 | + | |
| 2086 | + | |
| 2087 | + | |
| 2088 | + | |
| 2089 | + | |
| 2090 | + | |
| 2091 | + | |
| 2092 | + | |
| 2093 | + | |
| 2094 | + | |
| 2095 | + | |
| 2096 | + | |
| 2097 | + | |
| 2098 | + | |
| 2099 | + | |
| 2100 | + | |
| 2101 | + | |
| 2102 | + | |
| 2103 | + | |
| 2104 | + | |
| 2105 | + | |
| 2106 | + | |
| 2107 | + | |
| 2108 | + | |
| 2109 | + | |
| 2110 | + | |
| 2111 | + | |
| 2112 | + | |
| 2113 | + | |
| 2114 | + | |
| 2115 | + | |
| 2116 | + | |
| 2117 | + | |
| 2118 | + | |
| 2119 | + | |
| 2120 | + | |
| 2121 | + | |
| 2122 | + | |
| 2123 | + | |
| 2124 | + | |
| 2125 | + | |
| 2126 | + | |
| 2127 | + | |
| 2128 | + | |
| 2129 | + | |
| 2130 | + | |
| 2131 | + | |
| 2132 | + | |
| 2133 | + | |
| 2134 | + | |
| 2135 | + | |
| 2136 | + | |
| 2137 | + | |
| 2138 | + | |
| 2139 | + | |
| 2140 | + | |
2059 | 2141 | | |
2060 | 2142 | | |
2061 | 2143 | | |
| |||
2125 | 2207 | | |
2126 | 2208 | | |
2127 | 2209 | | |
2128 | | - | |
| 2210 | + | |
| 2211 | + | |
| 2212 | + | |
| 2213 | + | |
| 2214 | + | |
| 2215 | + | |
| 2216 | + | |
| 2217 | + | |
| 2218 | + | |
| 2219 | + | |
| 2220 | + | |
| 2221 | + | |
2129 | 2222 | | |
2130 | 2223 | | |
2131 | 2224 | | |
2132 | 2225 | | |
2133 | 2226 | | |
2134 | | - | |
2135 | | - | |
| 2227 | + | |
| 2228 | + | |
2136 | 2229 | | |
2137 | 2230 | | |
2138 | 2231 | | |
2139 | 2232 | | |
| 2233 | + | |
| 2234 | + | |
| 2235 | + | |
| 2236 | + | |
| 2237 | + | |
| 2238 | + | |
2140 | 2239 | | |
2141 | 2240 | | |
| 2241 | + | |
| 2242 | + | |
| 2243 | + | |
| 2244 | + | |
| 2245 | + | |
| 2246 | + | |
| 2247 | + | |
| 2248 | + | |
2142 | 2249 | | |
2143 | 2250 | | |
2144 | 2251 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
40 | 40 | | |
41 | 41 | | |
42 | 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 | + | |
0 commit comments