Commit c333a11
lucasliu
feat(api): add logprobs and top_logprobs support (OpenAI standard)
Implements OpenAI-compatible logprobs/top_logprobs across the inference
pipeline, closing a P0 Ollama-parity gap for downstream eval tooling
(LangChain, RAG evaluators, classifier wrappers).
Pipeline:
- InferenceRequest gains includeLogprobs + topLogprobsCount; Token gains
logprob + topLogprobs; InferenceResult aggregates tokenLogprobs.
- FusedBatchScheduler.computeRawLogprobs runs inside mlxContainer.perform;
raw (Sendable) data crosses the actor boundary and tokenizer decoding
happens outside.
- APIServer maps to OpenAILogprobs/Entry/TopLogprob with snake_case keys;
populated on both non-stream choices and stream chunks.
Correctness:
- Spec decoding bypassed when logprobs requested so every accepted token
carries logprob data (no silent gaps).
- Streaming logprobs attached to only the first SSE chunk per decode
token, preventing double-counting when ThinkingParser splits a token
into reasoning + content chunks.
- bytes field populated from UTF-8 per OpenAI spec.
Performance:
- Top-K via argPartition (O(N) + O(K log K)) instead of full argSort
over the entire vocabulary on every decode step.
- Sampled-token-only path (top_logprobs=0) skips eval(logSoftmax) and
pulls a single scalar.
Tests: +15 (4 semantic invariant tests on top of plumbing/round-trip):
order preservation, nil aggregation, UTF-8 bytes (ASCII + CJK),
descending top_logprobs and non-positive log-probability invariants.1 parent 5d1e8bc commit c333a11
5 files changed
Lines changed: 513 additions & 31 deletions
File tree
- Sources
- NovaMLXAPI
- NovaMLXCore
- NovaMLXEngine
- Tests/NovaMLXAPITests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1728 | 1728 | | |
1729 | 1729 | | |
1730 | 1730 | | |
1731 | | - | |
| 1731 | + | |
| 1732 | + | |
| 1733 | + | |
1732 | 1734 | | |
1733 | 1735 | | |
1734 | 1736 | | |
| |||
1819 | 1821 | | |
1820 | 1822 | | |
1821 | 1823 | | |
1822 | | - | |
| 1824 | + | |
| 1825 | + | |
1823 | 1826 | | |
1824 | 1827 | | |
1825 | 1828 | | |
| |||
1865 | 1868 | | |
1866 | 1869 | | |
1867 | 1870 | | |
1868 | | - | |
| 1871 | + | |
| 1872 | + | |
| 1873 | + | |
1869 | 1874 | | |
1870 | 1875 | | |
1871 | 1876 | | |
| |||
1899 | 1904 | | |
1900 | 1905 | | |
1901 | 1906 | | |
| 1907 | + | |
| 1908 | + | |
| 1909 | + | |
| 1910 | + | |
| 1911 | + | |
| 1912 | + | |
| 1913 | + | |
1902 | 1914 | | |
1903 | 1915 | | |
1904 | 1916 | | |
| |||
1993 | 2005 | | |
1994 | 2006 | | |
1995 | 2007 | | |
1996 | | - | |
| 2008 | + | |
1997 | 2009 | | |
| 2010 | + | |
1998 | 2011 | | |
1999 | 2012 | | |
2000 | 2013 | | |
| |||
2005 | 2018 | | |
2006 | 2019 | | |
2007 | 2020 | | |
2008 | | - | |
| 2021 | + | |
2009 | 2022 | | |
| 2023 | + | |
2010 | 2024 | | |
2011 | 2025 | | |
2012 | 2026 | | |
| |||
2586 | 2600 | | |
2587 | 2601 | | |
2588 | 2602 | | |
| 2603 | + | |
| 2604 | + | |
| 2605 | + | |
| 2606 | + | |
| 2607 | + | |
| 2608 | + | |
| 2609 | + | |
| 2610 | + | |
| 2611 | + | |
| 2612 | + | |
| 2613 | + | |
| 2614 | + | |
| 2615 | + | |
| 2616 | + | |
| 2617 | + | |
| 2618 | + | |
| 2619 | + | |
| 2620 | + | |
| 2621 | + | |
| 2622 | + | |
| 2623 | + | |
| 2624 | + | |
| 2625 | + | |
| 2626 | + | |
| 2627 | + | |
| 2628 | + | |
2589 | 2629 | | |
2590 | 2630 | | |
2591 | 2631 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
180 | 180 | | |
181 | 181 | | |
182 | 182 | | |
| 183 | + | |
| 184 | + | |
183 | 185 | | |
184 | 186 | | |
185 | | - | |
| 187 | + | |
186 | 188 | | |
187 | 189 | | |
188 | 190 | | |
| |||
199 | 201 | | |
200 | 202 | | |
201 | 203 | | |
| 204 | + | |
202 | 205 | | |
203 | 206 | | |
204 | 207 | | |
| |||
225 | 228 | | |
226 | 229 | | |
227 | 230 | | |
228 | | - | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
229 | 234 | | |
230 | 235 | | |
231 | 236 | | |
| |||
251 | 256 | | |
252 | 257 | | |
253 | 258 | | |
| 259 | + | |
| 260 | + | |
254 | 261 | | |
255 | 262 | | |
256 | 263 | | |
| |||
475 | 482 | | |
476 | 483 | | |
477 | 484 | | |
| 485 | + | |
478 | 486 | | |
479 | 487 | | |
480 | | - | |
| 488 | + | |
481 | 489 | | |
482 | 490 | | |
483 | 491 | | |
484 | | - | |
| 492 | + | |
485 | 493 | | |
486 | 494 | | |
487 | 495 | | |
| 496 | + | |
488 | 497 | | |
489 | 498 | | |
490 | 499 | | |
| |||
528 | 537 | | |
529 | 538 | | |
530 | 539 | | |
| 540 | + | |
531 | 541 | | |
532 | 542 | | |
533 | | - | |
| 543 | + | |
534 | 544 | | |
535 | 545 | | |
536 | 546 | | |
537 | | - | |
| 547 | + | |
538 | 548 | | |
539 | 549 | | |
540 | 550 | | |
| 551 | + | |
541 | 552 | | |
542 | 553 | | |
543 | 554 | | |
| |||
561 | 572 | | |
562 | 573 | | |
563 | 574 | | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
564 | 614 | | |
565 | 615 | | |
566 | 616 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
217 | 217 | | |
218 | 218 | | |
219 | 219 | | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
220 | 224 | | |
221 | 225 | | |
222 | 226 | | |
| |||
243 | 247 | | |
244 | 248 | | |
245 | 249 | | |
246 | | - | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
247 | 253 | | |
248 | 254 | | |
249 | 255 | | |
| |||
270 | 276 | | |
271 | 277 | | |
272 | 278 | | |
| 279 | + | |
| 280 | + | |
273 | 281 | | |
274 | 282 | | |
275 | 283 | | |
| |||
307 | 315 | | |
308 | 316 | | |
309 | 317 | | |
| 318 | + | |
310 | 319 | | |
311 | 320 | | |
312 | 321 | | |
| |||
316 | 325 | | |
317 | 326 | | |
318 | 327 | | |
319 | | - | |
| 328 | + | |
| 329 | + | |
320 | 330 | | |
321 | 331 | | |
322 | 332 | | |
| |||
326 | 336 | | |
327 | 337 | | |
328 | 338 | | |
| 339 | + | |
329 | 340 | | |
330 | 341 | | |
331 | 342 | | |
| |||
335 | 346 | | |
336 | 347 | | |
337 | 348 | | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
338 | 361 | | |
339 | 362 | | |
340 | 363 | | |
341 | 364 | | |
| 365 | + | |
342 | 366 | | |
343 | 367 | | |
344 | 368 | | |
345 | | - | |
| 369 | + | |
346 | 370 | | |
347 | 371 | | |
348 | 372 | | |
| 373 | + | |
349 | 374 | | |
350 | 375 | | |
351 | 376 | | |
| |||
0 commit comments