Commit 6030a44
authored
Refactor(regen): emit generation boundary as loss mask for on-policy data, eliminate redundant re-tokenize roundtrip (vllm-project#729)
# Purpose
The current on-policy response regeneration has re-tokenization
roundtrip redundancy. This PR helps eliminate the roundtrip:
```
BEFORE AFTER
regen -> text regen -> return_token_ids
write conversations write input_ids + loss_mask (boundary)
re-tokenize + REGEX/HF mask _preprocess_batch passthrough (no masking)
vLLM -> hidden states vLLM -> hidden states
```
The masking machinery still exists (off-policy needs it until PR2); it
is just no longer on the on-policy path.
## Concrete example — a 2-turn conversation
Before (one row of text, masked later by regex over the whole
rendering):
```json
{"id": "sample_7", "conversations": [{"from":"human","value":"2+2?"}, {"from":"gpt","value":"4"}, {"from":"human","value":"3+3?"}, {"from":"gpt","value":"6"}]}
```
After (one row per assistant turn, mask already applied):
```json
{"id":"sample_7_turn0", "input_ids":[<prompt>,<"4">], "loss_mask":[0,0,0,1,1], "conversations":[...]}
{"id":"sample_7_turn1", "input_ids":[<prompt+history>,<"6">], "loss_mask":[0,...,0,1,1], "conversations":[...]}
```
`loss_mask` 0 is the prompt the target conditioned on; 1 is what it
generated. No `{% generation %}` markers, no regex — just the boundary
the serving engine reports.
## Why
On-policy data never needed regex masking: we generated the response, so
we already know where the assistant tokens are, and capturing that
boundary is exact.
It is also correct for multi-turn reasoning models, where each turn
supervises the `<think>` it actually generated while history keeps only
parsed content — matching inference. The regex path cannot reproduce
this, because the same turn tokenizes differently as "generated" vs as
"history".
## What changes (observable)
| | Before | After |
|---|---|---|
| Output schema | `conversations` (text) | `input_ids` + `loss_mask` (+
`conversations`, review-only) |
| Rows per conversation | 1 | 1 per assistant turn |
| Mask made | downstream regex / HF | generation boundary |
| Endpoint | text out | must support `return_token_ids` |
`conversations` is retained as an inert human-review twin of
`input_ids`: `_preprocess_batch` routes on `input_ids`/`loss_mask`
(checked first) and drops `conversations` before training, so it is
never re-masked.
## Tests
`build_boundary_sample` produces the boundary mask; pre-tokenized rows
pass through `_preprocess_batch` without a processor, and a review
`conversations` field is ignored. Off-policy suite unchanged (56 passed
/ 2 skipped).
## Scope — step 1 of 5 ("renderer is the single source of truth")
1. (this PR) on-policy → generation boundary; stop feeding the regex
path. (Please note that this PR is a concrete standalone PR. No
speculative scaffolding. )
2. To unify the chat template generation tag. we should vendor
generation-tagged template from trl
huggingface/trl#4879.
3. on-policy → we have to diverge a bit from pure on-policy regen: real
inference has a thinking compression mechanism - newer models, e.g.,
GLM5.2, Qwen3.6 only keep the latest turn's thinking, and drop 0:n-1
thinking. But we need all thinking because that's where speculative
decoding can helps. PR1 is correct (completely on policy, no divergence)
but inefficient because it fan out multiturn to multi-row jsonl. PR3 can
linearize them back to single-row, with some tradeoff. This is also the
same solution with trl, llama-factory for multi-turn SFT. We can discuss
further on the tradeoff when I have a draft PR.
4. off-policy → vLLM `/render` this path can be decoupled from the regex
& hf path
5. delete the regex/HF masking once both producers are redirected.
small followup PRs:
`--turn-dropout` not compatible with this PR. need to run some tests on
real data to see if we should improve it or remove it.
## Checklist
I have filled in:
- [x] The purpose of the PR, such as "Fix some issue (link existing
issues this PR will resolve)".
- [x] The test plan/results, such as providing test command and pasting
the results.
- [ ] (Optional) The necessary documentation update.
- [x] I (a human) have written or reviewed the code in this pr to the
best of my ability.
---------
Signed-off-by: Ranran Haoran Zhang <ranzhang@redhat.com>1 parent 5330499 commit 6030a44
5 files changed
Lines changed: 315 additions & 145 deletions
File tree
- docs
- cli
- user_guide/tutorials
- scripts/response_regeneration
- src/speculators/data_generation
- tests/unit/scripts
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
96 | | - | |
| 96 | + | |
97 | 97 | | |
98 | 98 | | |
99 | 99 | | |
| |||
117 | 117 | | |
118 | 118 | | |
119 | 119 | | |
120 | | - | |
| 120 | + | |
121 | 121 | | |
122 | 122 | | |
123 | 123 | | |
124 | | - | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
125 | 128 | | |
126 | | - | |
127 | | - | |
| 129 | + | |
| 130 | + | |
128 | 131 | | |
129 | 132 | | |
130 | 133 | | |
131 | 134 | | |
132 | | - | |
133 | 135 | | |
134 | | - | |
135 | | - | |
| 136 | + | |
136 | 137 | | |
137 | 138 | | |
138 | 139 | | |
139 | 140 | | |
140 | | - | |
141 | | - | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
142 | 145 | | |
143 | | - | |
| 146 | + | |
144 | 147 | | |
145 | 148 | | |
146 | 149 | | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
| 150 | + | |
151 | 151 | | |
152 | 152 | | |
153 | 153 | | |
| 154 | + | |
154 | 155 | | |
155 | 156 | | |
156 | 157 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
53 | | - | |
| 53 | + | |
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
57 | | - | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
58 | 61 | | |
59 | | - | |
60 | | - | |
| 62 | + | |
| 63 | + | |
61 | 64 | | |
62 | 65 | | |
63 | 66 | | |
64 | 67 | | |
65 | | - | |
66 | 68 | | |
67 | 69 | | |
68 | 70 | | |
69 | 71 | | |
70 | 72 | | |
71 | 73 | | |
| 74 | + | |
| 75 | + | |
72 | 76 | | |
73 | 77 | | |
74 | 78 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
10 | 9 | | |
11 | 10 | | |
12 | 11 | | |
| |||
187 | 186 | | |
188 | 187 | | |
189 | 188 | | |
190 | | - | |
| 189 | + | |
191 | 190 | | |
192 | | - | |
193 | | - | |
194 | | - | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
195 | 199 | | |
196 | 200 | | |
197 | 201 | | |
| |||
203 | 207 | | |
204 | 208 | | |
205 | 209 | | |
206 | | - | |
207 | | - | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
208 | 215 | | |
209 | 216 | | |
210 | 217 | | |
| |||
270 | 277 | | |
271 | 278 | | |
272 | 279 | | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
273 | 293 | | |
274 | 294 | | |
275 | 295 | | |
| |||
280 | 300 | | |
281 | 301 | | |
282 | 302 | | |
283 | | - | |
| 303 | + | |
284 | 304 | | |
285 | | - | |
286 | | - | |
287 | | - | |
288 | | - | |
| 305 | + | |
| 306 | + | |
289 | 307 | | |
290 | 308 | | |
291 | 309 | | |
| |||
295 | 313 | | |
296 | 314 | | |
297 | 315 | | |
| 316 | + | |
298 | 317 | | |
299 | | - | |
300 | | - | |
301 | 318 | | |
302 | | - | |
303 | | - | |
304 | | - | |
305 | | - | |
| 319 | + | |
306 | 320 | | |
307 | 321 | | |
308 | 322 | | |
309 | 323 | | |
310 | | - | |
311 | 324 | | |
312 | 325 | | |
313 | 326 | | |
314 | | - | |
315 | 327 | | |
316 | 328 | | |
317 | 329 | | |
318 | 330 | | |
319 | 331 | | |
| 332 | + | |
320 | 333 | | |
321 | 334 | | |
322 | 335 | | |
| |||
326 | 339 | | |
327 | 340 | | |
328 | 341 | | |
329 | | - | |
330 | | - | |
331 | | - | |
332 | | - | |
333 | | - | |
334 | | - | |
335 | | - | |
336 | | - | |
337 | | - | |
338 | | - | |
339 | | - | |
340 | | - | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
341 | 345 | | |
342 | | - | |
343 | | - | |
344 | | - | |
345 | | - | |
346 | | - | |
347 | | - | |
348 | | - | |
349 | | - | |
350 | | - | |
351 | | - | |
352 | | - | |
353 | | - | |
354 | | - | |
355 | | - | |
356 | | - | |
357 | | - | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
358 | 355 | | |
359 | | - | |
360 | | - | |
361 | | - | |
362 | | - | |
363 | | - | |
364 | | - | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
365 | 386 | | |
366 | 387 | | |
367 | 388 | | |
368 | | - | |
369 | | - | |
| 389 | + | |
370 | 390 | | |
371 | | - | |
372 | | - | |
| 391 | + | |
373 | 392 | | |
374 | 393 | | |
375 | 394 | | |
| 395 | + | |
376 | 396 | | |
377 | 397 | | |
378 | 398 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
507 | 507 | | |
508 | 508 | | |
509 | 509 | | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
510 | 541 | | |
511 | 542 | | |
512 | 543 | | |
| |||
517 | 548 | | |
518 | 549 | | |
519 | 550 | | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
520 | 556 | | |
521 | 557 | | |
522 | 558 | | |
| |||
617 | 653 | | |
618 | 654 | | |
619 | 655 | | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
620 | 669 | | |
621 | | - | |
| 670 | + | |
622 | 671 | | |
623 | 672 | | |
624 | 673 | | |
| |||
627 | 676 | | |
628 | 677 | | |
629 | 678 | | |
630 | | - | |
631 | | - | |
632 | 679 | | |
633 | 680 | | |
634 | 681 | | |
| |||
0 commit comments