Commit bc52147
PR-B3 (ADR 0008 Phase B): Generate server-streaming RPC + greedy session-aware decoding
First Phase-B PR with mandatory Mac M4 integration test report
under \u00a79 (Linux CI is necessary but not sufficient).
Design choice: PR-B3 ships **greedy decoding only**. Speculative-
decoding integration (DLM proposer + AR verifier rejection sampling)
is reserved for a later PR. The wire contract is algorithm-agnostic
(GenerateResponse oneof on token_id / done / truncated), so the
upgrade path lands without breaking clients.
Three deliverables:
1. inference_engine/session/generator.py (new, ~240 lines)
- VerifierProtocol-driven GenerationCoordinator.
- Yields TokenEvent / HistoryTruncatedEvent / DoneEvent.
- Strict v0.3 validation: temperature/top_p/top_k must be in
greedy no-op defaults; max_tokens must be >= 1; session must
have prior AppendTokens. Per \u00a72.10 'no graceful degradation',
any deviation raises ValueError -> INVALID_ARGUMENT.
- HistoryTruncated emitted at most once per call, BEFORE any
TokenEvent, when the cache is in sink+window-truncated state
(matches the runtime.proto contract exactly).
- INV-1 / INV-2 enforced via SessionStore at every step.
- INV-3 byte-exact under greedy: same (session_id, history)
-> bit-identical token stream. Tested with FakeVerifier on
Linux and asserted in TestDeterminism.
2. inference_engine/server/grpc_app.py (modified, +119 lines)
- RuntimeServiceServicer.__init__ takes optional
generation_coordinator: GenerationCoordinator. None = PR-B2
UNIMPLEMENTED default preserved (regression-tested).
- Generate RPC implements server-streaming with the four typed
error mappings:
SessionNotFoundError -> NOT_FOUND
ValueError -> INVALID_ARGUMENT
InvariantViolation -> FAILED_PRECONDITION
Plus the success path streams TokenEvent -> token_id,
HistoryTruncatedEvent -> truncated, DoneEvent -> done.
- Cancellation: polls context.cancelled() between events; on
True, emits a final GenerateDone(STOP_REASON_CANCELLED) and
returns. Cancellation latency is bounded by one generation
step (the in-flight forward pass finishes before the next
poll).
- create_grpc_server factory plumbed with the new keyword.
3. tests/inference_engine/session/test_generator.py (new, 31 tests)
- TestGreedyHappyPath: 4 tests on token-then-done emission,
max_tokens cap, single Done event, total_seconds reporting.
- TestGreedyAdvancesVerifier: 4 tests confirming verifier
state mirrored onto session after every step.
- TestEos: 3 tests on EOS detection + STOP_REASON_EOS.
- TestHistoryTruncated: 3 tests on at-start emission + at-most-
once contract.
- TestValidation: 9 tests on max_tokens, sampling params, and
no-AppendTokens-prior rejection.
- TestInvariants: 2 tests on INV-1 / INV-2 propagation.
- TestDeterminism: 1 test on byte-exact greedy output across
parallel sessions.
- TestConstructorAndEventDataclasses: 4 tests on frozen dataclass
contract and constructor.
+ tests/inference_engine/server/test_grpc_app.py (extended, +11 tests)
- Generate streams tokens then done.
- EOS triggers STOP_REASON_EOS.
- HistoryTruncated frame emitted before tokens.
- Unknown session -> NOT_FOUND.
- No AppendTokens prior -> INVALID_ARGUMENT.
- max_tokens=0 -> INVALID_ARGUMENT.
- temperature=0.7 -> INVALID_ARGUMENT.
- seed accepted on the wire.
- InvariantViolation during generation -> FAILED_PRECONDITION.
- Cancellation -> STOP_REASON_CANCELLED Done frame (direct
Servicer invocation with FakeContext; cancellation latency
bounded as documented).
- Factory accepts generation_coordinator keyword.
Mac M4 reviewer aids:
scripts/smoke_grpc_generator.py
10-scenario smoke walking Generate scenarios with Append +
Generate coordinators wired against FakeVerifier.
scripts/review_pr_b3_on_mac.sh
One-shot Mac M4 reviewer producing 5 JSON artifacts:
pr-b3-mac-generator-tests-<unix>.json (31 tests, 100% on
generator.py)
pr-b3-mac-grpc-tests-<unix>.json (39 tests after PR-B3
additions, 100% on grpc_app.py)
pr-b3-mac-grpc-runtime-smoke-<unix>.json (PR-B1 regression)
pr-b3-mac-grpc-appender-smoke-<unix>.json (PR-B2 regression)
pr-b3-mac-grpc-generator-smoke-<unix>.json (PR-B3 new)
Local verification (Linux VM, py3.12):
Linux CI gate: 629 passed (was 587 + 31 generator + 11 grpc
generate). Coverage 100.00% on 1521 stmts (was
1428 + 63 generator + 30 grpc_app additions
- 0 deletions).
Per ADR 0008 \u00a79: this PR introduces a NEW MLX-reachable runtime
path (verifier.forward_block + commit_or_truncate driven by the
coordinator's per-token loop). Even though FakeVerifier covers the
Linux-side dispatch logic exhaustively, the v0.3 GA contract
requires a Mac M4 integration test report on the PR branch before
merge. The carve-out paragraph does NOT apply here. PR is opened as
Draft pending the report.
Mac M4 verification command for the PR branch:
bash scripts/review_pr_b3_on_mac.sh
The script writes 5 JSON artifacts under results/platform-tests/;
the user commits them back to this branch and the PR description is
updated to quote the smoke summary line.
Co-authored-by: FluffyAIcode <FluffyAIcode@users.noreply.github.com>1 parent d343e0c commit bc52147
7 files changed
Lines changed: 1739 additions & 10 deletions
File tree
- inference_engine
- server
- session
- scripts
- tests/inference_engine
- server
- session
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
45 | 48 | | |
46 | 49 | | |
47 | 50 | | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
48 | 56 | | |
49 | 57 | | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
50 | 73 | | |
51 | 74 | | |
52 | 75 | | |
| |||
113 | 136 | | |
114 | 137 | | |
115 | 138 | | |
| 139 | + | |
116 | 140 | | |
117 | 141 | | |
118 | 142 | | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
125 | 154 | | |
126 | 155 | | |
127 | 156 | | |
| 157 | + | |
128 | 158 | | |
129 | 159 | | |
130 | 160 | | |
| |||
184 | 214 | | |
185 | 215 | | |
186 | 216 | | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
187 | 322 | | |
188 | 323 | | |
189 | 324 | | |
| |||
233 | 368 | | |
234 | 369 | | |
235 | 370 | | |
| 371 | + | |
236 | 372 | | |
237 | 373 | | |
238 | 374 | | |
| |||
264 | 400 | | |
265 | 401 | | |
266 | 402 | | |
267 | | - | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
268 | 406 | | |
269 | 407 | | |
270 | 408 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
23 | 34 | | |
24 | 35 | | |
25 | 36 | | |
| |||
32 | 43 | | |
33 | 44 | | |
34 | 45 | | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
35 | 50 | | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
36 | 55 | | |
37 | 56 | | |
38 | 57 | | |
39 | 58 | | |
| 59 | + | |
40 | 60 | | |
41 | 61 | | |
0 commit comments