04 · Design verifiable rewards
Choose what to reward in an invoice extractor, then test whether the reward changes the records you would send to accounts payable.
- Part 2
- Compare training stages
- Model
Qwen/Qwen2.5-0.5B-Instruct- Data
- Course Invoice v1, visible text with separately stored gold records
- Hardware
- Tesla T4, in Google Colab
You maintain an extractor that sends invoice records to accounts payable. Invoice SF-878544 calls for a payment of EUR 2.677,76. One candidate record changes EUR to USD. Another leaves out the packing inserts. Both keep the payable digits unchanged. A payment in the wrong currency can settle the wrong amount, and a missing row prevents reconciliation against the items on the invoice.
Synthetic development invoice
SF-878544 / supplier Sample Supplier 5196 Date of issue 2026-06-08 / All amounts EUR Packing insert: 247,97 each for 8 units Meter label: 177,06 each for 1 units Rack inspection: 29,84 each for 7 units Net 2.369,70 Sales tax 308,06 Payable 2.677,76
Which candidate would your JSON format check accept? Which would you send to accounts payable?
Reveal the exact records and check the decision
The JSON format check accepts both candidates. You should hold both. Check the denomination against the printed currency and reconcile each printed row before release.
SF-878544 · constructed replay
- correct · format 1.000 · semantic 1.000
- { "invoice_id": "SF-878544", "issued_on": "2026-06-08", "supplier": "Sample Supplier 5196", "currency": "EUR", "line_items": [ { "description": "Packing insert", "quantity": 8, "unit_minor": 24797 }, { "description": "Meter label", "quantity": 1, "unit_minor": 17706 }, { "description": "Rack inspection", "quantity": 7, "unit_minor": 2984 } ], "subtotal_minor": 236970, "tax_minor": 30806, "total_minor": 267776 }
- missing row · format 1.000 · semantic 0.910
- { "invoice_id": "SF-878544", "issued_on": "2026-06-08", "supplier": "Sample Supplier 5196", "currency": "EUR", "line_items": [ { "description": "Meter label", "quantity": 1, "unit_minor": 17706 }, { "description": "Rack inspection", "quantity": 7, "unit_minor": 2984 } ], "subtotal_minor": 236970, "tax_minor": 30806, "total_minor": 267776 }
- wrong currency · format 1.000 · semantic 0.936
- { "invoice_id": "SF-878544", "issued_on": "2026-06-08", "supplier": "Sample Supplier 5196", "currency": "USD", "line_items": [ { "description": "Packing insert", "quantity": 8, "unit_minor": 24797 }, { "description": "Meter label", "quantity": 1, "unit_minor": 17706 }, { "description": "Rack inspection", "quantity": 7, "unit_minor": 2984 } ], "subtotal_minor": 236970, "tax_minor": 30806, "total_minor": 267776 }
- reordered rows · format 1.000 · semantic 1.000
- { "invoice_id": "SF-878544", "issued_on": "2026-06-08", "supplier": "Sample Supplier 5196", "currency": "EUR", "line_items": [ { "description": "Rack inspection", "quantity": 7, "unit_minor": 2984 }, { "description": "Meter label", "quantity": 1, "unit_minor": 17706 }, { "description": "Packing insert", "quantity": 8, "unit_minor": 24797 } ], "subtotal_minor": 236970, "tax_minor": 30806, "total_minor": 267776 }
Check the record you would pay
An exact record has every scored field and every line item in the printed order. For SF-878544, both the currency substitution and the missing packing-insert row fail this check. The held-out score asks the same question on a new set of invoices.
Both reward variants tied the supervised anchor
Exact-record accuracy and paired percentage-point change on 240 held-out invoices
1The Prompted base got 0/240 exact and 0/240 schema-valid. In the familiar billing memo below, it represents the supplier as an object, writes a string for the unit price and uses decimal major units for totals that should be integer minor units.
Read the billing memo and the observed baseline output
Sample Supplier 8519 billing memo / 2026-08-09 Account invoice SF-732205 [USD] 1 x Bench calibration @ 96.66 per unit Total due 103.43 including tax 6.77; net amount 96.66.
- Expected record
- {"invoice_id":"SF-732205","issued_on":"2026-08-09","supplier":"Sample Supplier 8519","currency":"USD","line_items":[{"description":"Bench calibration","quantity":1,"unit_minor":9666}],"subtotal_minor":9666,"tax_minor":677,"total_minor":10343}
- Prompted base
- ```json { "invoice_id": "SF-732205", "issued_on": "2026-08-09", "supplier": { "name": "Supplier 8519" }, "currency": "USD", "line_items": [ { "description": "Bench calibration", "quantity": 1, "unit_minor": "unit" } ], "subtotal_minor": 96.66, "tax_minor": 6.77, "total_minor": 96.66 } ```
- Format GRPO
- {"invoice_id":"SF-732205","issued_on":"2026-08-09","supplier":"Sample Supplier 8519","currency":"USD","line_items":[{"description":"Bench calibration","quantity":1,"unit_minor":9666}],"subtotal_minor":10343,"tax_minor":677,"total_minor":9666}
- Semantic GRPO
- {"invoice_id":"SF-732205","issued_on":"2026-08-09","supplier":"Sample Supplier 8519","currency":"USD","line_items":[{"description":"Bench calibration","quantity":1,"unit_minor":9666}],"subtotal_minor":10343,"tax_minor":677,"total_minor":9666}
Decide what earns credit
A reward should answer the business question: would the resulting invoice record be accepted? Valid syntax is necessary but cannot stand in for the correct amount and row.
The format reward uses a key-and-type gate: a record receives one point when the expected keys and field types are present. The semantic reward applies the same gate and then grades the printed fields, the line items and whether the model emitted an end-of-sequence token (EOS). For scalar fields such as currency, the check uses the source value.
combined = 0.45 * correct + 0.45 * row_score + 0.1 * float(natural_stop)Let g be the key-and-type gate, F the fraction of correct scalar fields, R the line-item F1 and E the EOS indicator. T and P are the true and predicted multisets of line items. Their intersection counts a repeated row at most as many times as it occurs in both.
The USD substitution retains six of seven scalar fields for a semantic reward of 0.936. Dropping the packing inserts leaves two correct rows against three expected, so row F1 is 0.800 and the reward is 0.910. Reversing all three rows preserves the multiset and earns 1.000 although the ordered record fails exact scoring. A complete record without EOS can still pass the content check and receives 0.900 under the reward.
Reordered rows earn full semantic reward and fail exact scoring
Reward and component scores on the same development invoice, scale 0 to 1
2| Replay case | Format | Semantic | Fields | Row F1 | EOS | Exact |
|---|---|---|---|---|---|---|
| correct | 1.000 | 1.000 | 1.000 | 1.000 | 1.000 | Pass |
| missing row | 1.000 | 0.910 | 1.000 | 0.800 | 1.000 | Fail |
| duplicate row | 1.000 | 0.936 | 1.000 | 0.857 | 1.000 | Fail |
| wrong currency | 1.000 | 0.936 | 0.857 | 1.000 | 1.000 | Fail |
| reordered rows | 1.000 | 1.000 | 1.000 | 1.000 | 1.000 | Fail |
| missing EOS | 1.000 | 0.900 | 1.000 | 1.000 | 0.000 | Pass |
| truncated JSON | 0.000 | 0.000 | 0.000 | 0.000 | 0.000 | Fail |
DeepSeek-R1-Zero also checks correctness and format separately. Its paper describes reference-based maths checks, predefined code tests and a format check for reasoning tags. The invoice lab applies the same distinction to printed facts and schema validity. DeepSeek-R1, §2.2
Completion deserves a separate measure. NVIDIA reported that outputs reaching the 9,000-token cap fell from 185 to 35 when it compared SFT and GRPO development checkpoints on 2,078 ParseBench pages, with matching decoding and scoring. That comparison covers a document parsing recipe with rewards for extraction, structure and completion. NVIDIA, Figure 3 · Measurement notes
If you reward fewer steps, award the bonus only after the response passes the record and completion checks. Keep it smaller than the reward gap between a correct and an incorrect record, and count the saved steps among correct responses. For example, a short answer that drops the last invoice line should still lose to a longer, complete extraction.
The s1 paper reports selecting 1,000 prompts and reasoning traces for quality, difficulty and diversity. Its budget forcing changes how long the model reasons at inference. When the model tries to stop early, the decoder can append “Wait” and continue. That is a decoding choice to test on a fixed checkpoint before adding an efficiency reward to training. s1, §§2–3
At larger scale, TRL’s asynchronous GRPO trainer can generate rollouts on a separate vLLM server while the training GPU updates the policy. Track the rollout’s policy version and bound how far generation can get ahead of training. The invoice experiment above uses a single T4. TRL asynchronous GRPO documentation
Check whether the reward can teach
Group relative policy optimisation (GRPO) samples several responses to the same prompt. It subtracts the group mean from each response reward and divides by the group’s standard deviation. The denominator has a small stabiliser. DeepSeekMath introduced this within-prompt comparison and sampled 64 responses per prompt in its published setting. Here, each group has four invoice responses. DeepSeekMath, §4
Consider sampling four copies of the wrong-currency JSON from the opening. Even though the semantic grader would penalise all four, the centred rewards would all be zero. The penalty separates candidates when the group contains answers with different scores. In the measured run, both mean rewards were 1.000 across all 10 groups per variant. Each group had zero reward variance. The logs record zero gradient norms and zero KL throughout the scored updates, and the two final adapter fingerprints match.
Every sampled group gave all four responses the same reward
Mean rewards, share of groups without reward variation, and cap hits without EOS
3Check the task before checking its difficulty. First replay a known correct solution and a plausible wrong one to test the grader. Then sample the frozen base several times on the training candidates and count groups with both passes and failures. In one screening pass, Hugging Face reports that 171 of 442 scored tasks met this criterion. Keep the held-out roster outside the filter so it still measures how the adapter handles easy, difficult and intermediate tasks. Hugging Face task screening · Ledger rollout extension
The separate replay illustrates two changes to the sampling policy. It drew four groups in two attempts and logged two groups with zero variance. The length ramp subtracts 0 at 216 and 224 tokens, 0.5 at 240 and 1.0 at 256. Those values come from deterministic diagnostics. The fitted comparisons used the same fixed rollout settings in both branches.
| Replay | Values to track |
|---|---|
| Bounded redraw | 2 attempts · 4 groups drawn · 2 zero-variance groups |
| Soft length penalty | 216 tokens: 0 · 224: 0 · 240: −0.5 · 256: −1.0 |
In DAPO, the published AIME 2024 cumulative ablation records avg@32 moving from 42 to 50 after adding dynamic sampling to the preceding recipe. That experiment also changed other training settings in earlier increments. In this lab the redraw replay shows how to count extra sampling effort before changing the fit. DAPO, §§3.2–3.4 and Table 1
Open the weakest layouts
The SFT anchor and both GRPO variants scored 95/96 exact on familiar layouts, 51/96 on novel layouts, and 0/48 on the separate layout. Yet the separate layout produced 47/48 schema-valid records, with mean row F1 of 88.9%. All three methods emitted EOS for all 240 invoices.
Valid JSON masked complete-record failures on the separate layout
Exact records, schema validity and mean row F1 by held-out layout group
4On separate-layout invoice SF-375561, both reward variants include the printed field label “Biller” in the supplier value. On novel-layout invoice SF-265176, both use the balance payable as the subtotal. The records are readable and their row content matches, but the value errors prevent payment and reconciliation.
Document SF-375561 Biller Sample Supplier 8052 / As of 2025-03-27 / USD 4 x Door seal @ 165.43 per unit Gross 708.04 Breakdown: net 661.72 and taxes 46.32.
- Expected record
- {"invoice_id":"SF-375561","issued_on":"2025-03-27","supplier":"Sample Supplier 8052","currency":"USD","line_items":[{"description":"Door seal","quantity":4,"unit_minor":16543}],"subtotal_minor":66172,"tax_minor":4632,"total_minor":70804}
- Format GRPO
- {"invoice_id":"SF-375561","issued_on":"2025-03-27","supplier":"Biller Sample Supplier 8052","currency":"USD","line_items":[{"description":"Door seal","quantity":4,"unit_minor":16543}],"subtotal_minor":66172,"tax_minor":4632,"total_minor":70804}
- Semantic GRPO
- {"invoice_id":"SF-375561","issued_on":"2025-03-27","supplier":"Biller Sample Supplier 8052","currency":"USD","line_items":[{"description":"Door seal","quantity":4,"unit_minor":16543}],"subtotal_minor":66172,"tax_minor":4632,"total_minor":70804}
Inspect the novel-layout subtotal failure
Billing details / SF-265176 Sold by Sample Supplier 3177 Document date 2026-05-20 Invoice currency USD Balance payable 715.50 Packing insert: 95.26 each for 2 units Mounting plate: 68.31 each for 7 units Merchandise 668.69 Tax 46.81
- Expected record
- {"invoice_id":"SF-265176","issued_on":"2026-05-20","supplier":"Sample Supplier 3177","currency":"USD","line_items":[{"description":"Packing insert","quantity":2,"unit_minor":9526},{"description":"Mounting plate","quantity":7,"unit_minor":6831}],"subtotal_minor":66869,"tax_minor":4681,"total_minor":71550}
- Format GRPO
- {"invoice_id":"SF-265176","issued_on":"2026-05-20","supplier":"Sample Supplier 3177","currency":"USD","line_items":[{"description":"Packing insert","quantity":2,"unit_minor":9526},{"description":"Mounting plate","quantity":7,"unit_minor":6831}],"subtotal_minor":71550,"tax_minor":4681,"total_minor":71550}
- Semantic GRPO
- {"invoice_id":"SF-265176","issued_on":"2026-05-20","supplier":"Sample Supplier 3177","currency":"USD","line_items":[{"description":"Packing insert","quantity":2,"unit_minor":9526},{"description":"Mounting plate","quantity":7,"unit_minor":6831}],"subtotal_minor":71550,"tax_minor":4681,"total_minor":71550}
Follow the paired decisions
Compare the exact score invoice by invoice. A tied accuracy can hide cases that improve and cases that regress. Here, there were zero changes in either direction across all three layout groups. Comparing the recorded completion strings also gives a match on all 240 invoices across the SFT anchor and both GRPO variants.
Every invoice tied between the reward arms
Per-invoice changes in the ordered exact score, including ties, across all three layout groups.
5Familiar · 96
Improved 0 · Tied 96 · Regressed 0
Novel · 96
Improved 0 · Tied 96 · Regressed 0
Separate layout · 48
Improved 0 · Tied 48 · Regressed 0
The zero-width supplier-family bootstrap interval follows from every observed paired difference being zero. It describes the observed roster and training budget. A new training seed or rollout roster could present different groups.
Tülu 3's paper likewise checks downstream performance independently of verifier rewards. Its Figure 19 shows that higher verifier rewards can coexist with a lower overall evaluation score. Tülu 3, §6 and Figure 19
Count the extra training cost
Both reward arms completed ten optimiser updates on the same checked rollout order, with 40 sampled completions each. The full format fit took 58.19 seconds and the full semantic fit 61.97 seconds, a measured difference of 3.77 seconds. Peak allocated and reserved memory were the same to two decimal places. The separate one-update preflight took 21.88 seconds, peaked at 4.76 GiB allocated and 5.13 GiB reserved, and passed the predeclared bounds.
Both reward updates added trainer time without improving extraction
Ten optimiser updates and 40 sampled completions per GRPO arm
6What to keep
Keep the SFT anchor for the next evaluation round. The measured GRPO arms cost extra training time and produced the same outputs. Keep the semantic verifier for checking mutations, including the currency and missing row on SF-878544. Keep an order-sensitive check if the displayed row order is part of your contract. The separate-layout performance warrants further work before automatic release. The reserved release split remains untouched.
Before spending a full training budget, sample several groups from the intended anchor and measure within-group reward variance. If the rewards tie, replay the verifier against errors you expect to see and check whether your sampling policy can surface both successes and failures.
| Decision | Evidence to use |
|---|---|
| Keep the SFT anchor | The measured GRPO arms cost extra training time and tied it on the recorded outputs. |
| Keep the field-and-row verifier | The worked SF-878544 candidate passed the format check with the wrong currency and a missing row. |
| Check row order separately | The multiset gives credit for matching rows regardless of position. A ledger contract can require the displayed order as well. |
| Reserve the release split | The separate-layout results expose a coverage gap that another round of development should address before a release decision. |
- Grade the submitted record. Compare currency, printed total and every line against gold. A well-formed object can still encode a bad payment instruction.
- Test each error in isolation. Replay a mutation with a missing row or a wrong currency so you know which part of the reward responds.
- Check whether examples can teach. Sample from the intended parent and inspect within-prompt spread before scheduling a longer fit.
- Keep the evaluator independent. Use exact record scoring and raw paired outputs to judge the new checkpoint. The training reward is a diagnostic of the samples it saw.
- Include unfamiliar layouts. A candidate should demonstrate useful performance on the formats you expect to receive before it can replace the anchor.
- Next to try, not tested here: Compare a development pool screened for both successful and failed completions with the unscreened pool, while keeping the evaluation roster fixed.
How it was built
The notebook ran on a Tesla T4 using Qwen2.5-0.5B-Instruct, revision 7ae557604adf67be50417f59c2c2f167def9a775, with seed 42. The supervised roster has 384 examples and the reward roster has 48 prompts. The evaluation roster has 240 invoices from 20 supplier families, disjoint from training.
The SFT trainer checked the collated response mask against the chat template, masking through the assistant prompt and after the first EOS. It completed 32 updates at 8 examples per update. Both GRPO arms started from the same merged checkpoint with fresh adapters and matched initial weights, rollout order, optimiser settings and decoding. Both used sampling temperature 0.8, KL coefficient 0.04 and a 256-token completion cap. Held-out comparisons used greedy decoding. The independent scorer grades the decoded record outside the reward:
record = score_record(extract(text), invoice)| Measurement | Setting |
|---|---|
| Packages | PyTorch 2.14.1+cu130, Transformers 4.51.3, PEFT 0.15.2, TRL 0.19.1, Accelerate 1.6.0, Datasets 3.6.0 |
| Merged SFT anchor SHA-256 | 73d8b2d40347caa9a21110f2846df507cde0016e893b8255559c5966fde82a5f |
| Both initial adapter fingerprints | 05c4f6b62233931f06fb7dbf3124c8651d1173c69738b2f9a9ce1fcefc057170 |
| Both final adapter fingerprints | bd71ef3a1ea6362c24e5374139a155701c84678e3c4a0c7650be0b6dccabd97b |
| Trainer timing boundary | CUDA synchronised around each full trainer call, including rollout generation for GRPO |
| T4 preflight bounds | At least 12 GiB free before profiling, at most 45 seconds, 10 GiB allocated and 12 GiB reserved while keeping 2 GiB headroom |
Download the executed notebook · Run report · Held-out roster and expected answers · Exact model outputs · Rollout log · Mutation replay · T4 profile
Run it yourself
Start with notebook §2 for the invoice reward, §5 for the matched rollout fits and §8 for the paired comparison. Predict the outcome of each change, make it on its own, then compare the indicated output.
- Add an order-aware term to the mutation replay and rerun SF-878544. Check the “reordered rows” case against the expected record, and check whether the correct record still earns full credit.
- Increase the redraw budget in the diagnostic. Check attempts, groups drawn and zero-variance groups to see whether the extra sampling bought a preference signal.
- Move the start of the soft length ramp. Compare penalties for 216, 224, 240 and 256 tokens. Decide which complete records could receive a different reward even when their invoice content is unchanged.
Continue to Lesson 05 · Distil traces and protect existing skills.
How is this page?
Last updated on