Skip to content

fix(code_gen): apply 1e-6 float tolerance to LCB stdout comparison - #2956

Merged
fgalko-oss merged 4 commits into
mainfrom
fgalko/fix-lcb-float-tolerance
Sep 2, 2026
Merged

fgalko-oss merged 4 commits into
mainfrom
fgalko/fix-lcb-float-tolerance

Conversation

@fgalko-oss

@fgalko-oss fgalko-oss commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes four sources of false negatives in LiveCodeBench grading: correct solutions that were scored Wrong Answer or Runtime Error by the grader rather than by any fault of the model.

All four were found by diffing against the Nemotron-Cascade evaluation harness and then confirmed against real rollouts from a 1362-rollout DeepSeek-V4-Flash LCB v6 run.

# Defect Was scored Rollouts affected
1 stdout float comparison required exact Decimal equality Wrong Answer 12
2 sys.stdout.buffer.write crashed (StringIO has no .buffer) Runtime Error 9
3 int→str limited to CPython's 4300-digit default Runtime Error 0 in this run
4 call-based float lists never got tolerance Wrong Answer 0 in this run

Measured effect: 93.39% → 94.93% (1272 → 1293 of 1362). The Cascade harness independently reports 95.05% on the same generations, so this closes the gap to within roughly one rollout.

1. Float tolerance on stdout comparison

grade_stdio compared answers token-by-token as exact Decimals. Competitive judges accept 1e-6 absolute or relative error, so a correct solution printing 0.333333 against a reference of 0.333333333333 was a false negative. Real examples recovered from the run:

our output expected
6.06449510224597965191 6.06449510224597979401
0.076923076923077 0.076923076923
17.142857142857142 17.142857142857142350

The call-based path already had np.allclose (adopted from Nemotron-Cascade's code_verifier_utils), so only stdin/stdout problems were affected — which is exactly where float-output problems live (AtCoder abc374_d, abc375_b, abc385_f).

Integers stay exact. The comment above this comparison explains why the path used Decimal rather than np.isclose:

## otherwise gotcha: np.isclose(50000000000000000, 50000000000000001) = True

That concern is real, so tolerance applies only to tokens that are genuinely floating-point (containing . or an exponent). 50000000000000000 vs 50000000000000001 still fails, with a regression test pinning it. Non-finite values are rejected rather than run through the tolerance branch.

Worth noting this is stricter than a float()-based comparator: parsing with float() silently collapses integers above 2^53, so an off-by-one on a 1e18-scale answer would pass. Keeping Decimal avoids that.

2. sys.stdout.buffer.write support

Capturing replaced sys.stdout with a StringIO, which has no .buffer, so the standard fast-output idiom raised AttributeError and was recorded as a Runtime Error. Capture now goes through a BytesIO wrapped in a write_through TextIOWrapper, so text and byte writes share one stream and keep their relative order.

This is the output-side counterpart of the sys.stdin.buffer fix in #2824. 9 of the 16 Runtime Errors in the measured run are exactly this AttributeError.

3. Large integer output

The graded environment kept CPython's default 4300-digit cap on int→str conversion, so printing a large answer raised ValueError and was scored a Runtime Error. import_string now raises the limit, alongside the existing recursion-limit bump. This did not fire in the measured run but will on any problem with a large integer answer.

4. Call-based float lists

Only scalar predictions were passed to np.allclose, so a returned list of floats within tolerance was scored Wrong Answer. Now compared element-wise when both sides are lists.

Deliberately not changed

Two differences from the Cascade harness alter accept/reject semantics rather than fixing a defect, so they are left for a protocol decision rather than folded in here:

  • Case-insensitive Yes/No. The Cascade harness accepts YES against Yes; this PR does not. Many judges are genuinely case-sensitive, so matching it silently would be a scoring change, not a fix.
  • Memory cap. reliability_guard caps at 4 GB here and is uncapped in the Cascade harness. That accounts for the single MemoryError in the measured run.

Test plan

  • resources_servers/code_gen/tests/test_float_tolerance.py (13 tests): tolerance acceptance, per-token enforcement across a line, the large-integer gotcha, small-integer off-by-one, token-count mismatch, scientific notation, non-finite input.
  • resources_servers/code_gen/tests/test_grader_environment.py (13 tests): stdout.buffer writes, text/byte write ordering, close() as a no-op, stdout restoration, the int→str limit in both directions, call-based float lists including length mismatch and integer lists.
  • Full code_gen suite green: 33 passed.
  • End-to-end through run_test: all previously failing idioms now accepted, YES/Yes still rejected as intended, and the ten float-tolerance cases unregressed in both directions.
  • Ruff lint, import order and format clean on both changed files (pinned v0.9.9, matching .pre-commit-config.yaml).

Scores can only move up, and only where the grader was previously wrong. This also removes false negatives from RL code verification, where a wrongly-rejected correct rollout is a bad training signal.

Checklist

  • I have read the contributing guidelines.
  • The change is focused; unrelated "drive-by" edits are tracked as separate issues/PRs.
  • Tests added or updated and pass locally, or N/A for docs-only / non-code changes (so CI unit/server checks pass when applicable).
  • Pre-commit checks pass locally (pre-commit run --all-files) (so CI lint/format/copyright pass).
  • All commits have DCO sign-off (git commit -s) (so the DCO check passes).

The stdout grading path compared answers as exact Decimals, so any
floating-point output that did not match the reference digit for digit was
scored Wrong Answer. Competitive-programming judges accept an absolute or
relative error of 1e-6, so a correct solution printing 0.333333 against a
reference of 0.333333333333 was a false negative.

The call-based path already handles this via np.allclose (adopted from
Nemotron-Cascade's code_verifier_utils), so only stdin/stdout problems were
affected. AtCoder-style float-output problems are exactly that case.

Tolerance is applied only to tokens that are actually floating-point, i.e.
containing a decimal point or an exponent. Integers keep exact comparison,
which preserves the reason this path used Decimal in the first place: the
np.isclose(50000000000000000, 50000000000000001) == True gotcha called out
in the comment above the comparison. Non-finite values are rejected rather
than compared, so inf/nan cannot pass through the tolerance branch.

Adds tests covering tolerance acceptance, per-token enforcement across a
line, the large-integer gotcha, token-count mismatch and non-finite input.

Signed-off-by: fgalko <[email protected]>
…CB grading

Three further false-negative sources in LCB grading, found by diffing against
the Nemotron-Cascade evaluation harness and confirmed against real rollouts.

stdout.buffer: Capturing swapped sys.stdout for a StringIO, which has no
.buffer attribute, so any solution using the sys.stdout.buffer.write fast-output
idiom died with AttributeError and was scored a Runtime Error. Capture through a
BytesIO with a write_through TextIOWrapper instead, so text and byte writes share
one stream and keep their relative order. This is the output-side counterpart of
the sys.stdin.buffer fix in #2824.

Large integers: the graded environment kept CPython's default 4300-digit cap on
int-to-str conversion, so printing a large answer raised ValueError and was also
scored a Runtime Error. import_string now raises the limit, alongside the existing
recursion-limit bump.

Call-based float lists: only scalar predictions got np.allclose, so a returned
list of floats within tolerance was scored Wrong Answer. Compare element-wise
when both sides are lists.

Measured on a 1362-rollout DeepSeek-V4-Flash LiveCodeBench v6 run: 9 of the 16
Runtime Errors are exactly the stdout.buffer AttributeError. Together with the
float tolerance fix this recovers 21 rollouts, 93.39% -> 94.93%, which lines up
with the 95.05% the Cascade harness reports on the same generations.

Deliberately unchanged: case-insensitive Yes/No matching and the 4GB memory cap
both alter accept/reject semantics rather than fixing a defect, so they are left
for a protocol decision.

Signed-off-by: fgalko <[email protected]>
@cmunley1

cmunley1 commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

/ok to test a68488a

@cmunley1

cmunley1 commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

/claude review

@claude

claude Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

SHIP WITH CARE

Solid, well-tested change to the LiveCodeBench grader. The stdout Capturing rewrite (BytesIO+TextIOWrapper) correctly supports the sys.stdout.buffer.write fast-output idiom and preserves ordering; the sys.set_int_max_str_digits(1<<20) bump unblocks large-integer answers; and the stdio float-tolerance path is carefully gated behind token_is_floating_point so integers still require exact match (TestIntegersStayExact proves it). Good test coverage across all three behaviors.

One RISK (inline, testing_util.py:356): the new call-based float-list branch uses raw np.allclose(float(p), float(e)) element-wise with no floating-point guard, reintroducing the exact np.isclose(50000000000000000, 50000000000000001) == True large-integer false positive that the stdio path and this PR's own token_is_floating_point/TestIntegersStayExact defend against. For call-based tasks returning integer lists, a numerically-close-but-wrong answer can be silently scored correct — corrupting eval scores and training rewards. Gate the tolerance on the values actually being floats (or reuse decimal_tokens_match) so both grading paths share one policy. test_exact_int_list_still_passes uses small ints that already match exactly, so it doesn't catch this.

Everything else looks correct — no async/API/dependency concerns.

Comment on lines +356 to +363
if not tmp_result and isinstance(prediction, list) and isinstance(gt_out, list):
try:
tmp_result = len(prediction) == len(gt_out) and all(
np.allclose(float(predicted_item), float(expected_item))
for predicted_item, expected_item in zip(prediction, gt_out)
)
except Exception:
pass

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RISK — reintroduces the large-integer false positive the rest of this PR guards against.

WHAT BREAKS: this new call-based branch runs np.allclose(float(p), float(e)) element-wise on any list, with no floating-point guard. float(50000000000000001) == float(50000000000000000) == 5e16 (same IEEE-754 double), and np.allclose's default rtol=1e-5 yields a ~5e11 tolerance at that magnitude — so an off-by-one list of large integers is scored correct (tmp_result = True). This is exactly the np.isclose(50000000000000000, 50000000000000001) == True gotcha called out in the stdio path (line ~499) and asserted against in TestIntegersStayExact. The stdio side gates tolerance behind token_is_floating_point; this branch does not.

BLAST RADIUS: call-based LiveCodeBench tasks returning integer lists where the model is numerically close but wrong → silent Wrong-Answer→Correct flips. Corrupts eval scores and any training reward derived from this verifier.

FIX: gate the tolerance on the values actually being floating-point, mirroring decimal_tokens_match — e.g. only fall through to np.allclose when isinstance(predicted_item, float) or isinstance(expected_item, float), and require exact equality for int/int pairs. Or reuse the Decimal-based decimal_tokens_match logic here so both grading paths share one tolerance policy.

Review catch: the new call-based float-list branch ran np.allclose on every
element with no type gate, which reintroduced exactly the large-integer false
positive the stdio path and this PR's own token_is_floating_point guard defend
against. float(50000000000000001) and float(50000000000000000) are the same
IEEE-754 double, and np.allclose's default rtol allows ~5e11 at that magnitude,
so an off-by-one list of large integers was scored correct. Confirmed against
the grader before the fix.

The same flaw already existed for scalars: the pre-existing
np.allclose(float(prediction), float(gt_out)) line accepted a scalar
50000000000000001 against 50000000000000000. Both now route through one
values_are_close helper that only applies tolerance when at least one side is
genuinely a float, mirroring the stdio policy, and requires exact equality for
integer pairs. Booleans never reach the tolerance branch.

Fixing the scalar case tightens behaviour beyond the reported list case: it can
only turn silently-accepted wrong answers into correct rejections, never the
reverse.

Tests pin the reported case (large-int list), the scalar case, a small-int list
off by one, the helper's float gate, and boolean handling.

Signed-off-by: fgalko <[email protected]>
@cmunley1

cmunley1 commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

/ok to test 6a7673a

@fgalko-oss
fgalko-oss enabled auto-merge (squash) September 2, 2026 19:26
@fgalko-oss
fgalko-oss merged commit 71564da into main Sep 2, 2026
31 checks passed
@fgalko-oss
fgalko-oss deleted the fgalko/fix-lcb-float-tolerance branch September 2, 2026 19:30
fgalko-oss added a commit that referenced this pull request Sep 3, 2026
…son (#3051)

## What does this PR do?

Accepts any capitalisation of a `Yes`/`No` verdict in the LiveCodeBench
stdout comparison, and only for those two words.

Codeforces-style statements print a Yes/No verdict and explicitly accept
any capitalisation, so a solution answering `YES` against a reference of
`Yes` is correct on the platform but was scored Wrong Answer here.

## Scope

Deliberately narrow. Tolerance for capitalisation applies only when the
predicted line is exactly `yes` or `no` (in any case) and both sides
agree case-insensitively. Every other answer stays case-sensitive, so
problems whose answer is a name or a word are unaffected, and a line
with more than one token is not treated as a verdict at all.

Verified end-to-end through the grader:

| submitted | expected | verdict |
|---|---|---|
| `YES` | `Yes` | accepted |
| `no` | `No` | accepted |
| `NO` | `Yes` | rejected |
| `SNUKE` | `Snuke` | rejected (stays case-sensitive) |
| `FIRST` | `First` | rejected (stays case-sensitive) |
| `alice` | `Alice` | rejected (stays case-sensitive) |
| `YES 1` | `Yes 1` | rejected (multi-token, not a verdict) |

## Why this is separate from #2956

The four changes in #2956 fixed grader faults: correct solutions being
scored wrong because of a defect (exact float equality, `StringIO`
having no `.buffer`, the int→str digit cap, missing list tolerance).
This one is different in kind — it changes accept/reject semantics to
match a platform convention, so it deserves its own review rather than
riding along.

## Effect on LiveCodeBench scores

**None measured on v6.** Across a 1362-rollout DeepSeek-V4-Flash LCB v6
run, none of the 66 wrong-answer failures differ from their reference by
capitalisation alone, so the score is unchanged (the run's projected
accuracy stays at 94.93%). The behaviour matters for Codeforces-sourced
problems rather than the AtCoder/LeetCode mix in v6.

## Test plan

- New
`resources_servers/code_gen/tests/test_case_insensitive_verdict.py` (14
tests): all capitalisation variants of Yes and No, `yes` not matching
`no`, verdicts not matching unrelated answers, other words staying
case-sensitive, and multi-token lines rejected.
- Full code_gen suite green: 52 passed.
- End-to-end through `run_test` on the 7 cases in the table above, all
behaving as intended in both directions.
- Ruff lint, import order and format clean on both changed files (pinned
v0.9.9, matching `.pre-commit-config.yaml`).

## Checklist

- [x] I have read the [contributing
guidelines](https://docs.nvidia.com/nemo/gym/latest/contribute/development-setup).
- [x] The change is focused; unrelated "drive-by" edits are tracked as
separate issues/PRs.
- [x] Tests added or updated and pass locally, or N/A for docs-only /
non-code changes (so CI unit/server checks pass when applicable).
- [x] Pre-commit checks pass locally (`pre-commit run --all-files`) (so
CI lint/format/copyright pass).
- [x] All commits have DCO sign-off (`git commit -s`) (so the DCO check
passes).

Signed-off-by: fgalko <[email protected]>
jkyi-nvidia pushed a commit that referenced this pull request Sep 11, 2026
…2956)

## What does this PR do?

Fixes four sources of **false negatives** in LiveCodeBench grading:
correct solutions that were scored Wrong Answer or Runtime Error by the
grader rather than by any fault of the model.

All four were found by diffing against the Nemotron-Cascade evaluation
harness and then confirmed against real rollouts from a 1362-rollout
DeepSeek-V4-Flash LCB v6 run.

| # | Defect | Was scored | Rollouts affected |
|---|---|---|---|
| 1 | stdout float comparison required exact `Decimal` equality | Wrong
Answer | 12 |
| 2 | `sys.stdout.buffer.write` crashed (`StringIO` has no `.buffer`) |
Runtime Error | 9 |
| 3 | int→str limited to CPython's 4300-digit default | Runtime Error |
0 in this run |
| 4 | call-based float **lists** never got tolerance | Wrong Answer | 0
in this run |

**Measured effect: 93.39% → 94.93%** (1272 → 1293 of 1362). The Cascade
harness independently reports **95.05%** on the same generations, so
this closes the gap to within roughly one rollout.

## 1. Float tolerance on stdout comparison

`grade_stdio` compared answers token-by-token as exact `Decimal`s.
Competitive judges accept 1e-6 absolute or relative error, so a correct
solution printing `0.333333` against a reference of `0.333333333333` was
a false negative. Real examples recovered from the run:

| our output | expected |
|---|---|
| `6.06449510224597965191` | `6.06449510224597979401` |
| `0.076923076923077` | `0.076923076923` |
| `17.142857142857142` | `17.142857142857142350` |

The call-based path already had `np.allclose` (adopted from
Nemotron-Cascade's `code_verifier_utils`), so only stdin/stdout problems
were affected — which is exactly where float-output problems live
(AtCoder `abc374_d`, `abc375_b`, `abc385_f`).

**Integers stay exact.** The comment above this comparison explains why
the path used `Decimal` rather than `np.isclose`:

```
## otherwise gotcha: np.isclose(50000000000000000, 50000000000000001) = True
```

That concern is real, so tolerance applies only to tokens that are
genuinely floating-point (containing `.` or an exponent).
`50000000000000000` vs `50000000000000001` still fails, with a
regression test pinning it. Non-finite values are rejected rather than
run through the tolerance branch.

Worth noting this is stricter than a `float()`-based comparator: parsing
with `float()` silently collapses integers above 2^53, so an off-by-one
on a 1e18-scale answer would pass. Keeping `Decimal` avoids that.

## 2. `sys.stdout.buffer.write` support

`Capturing` replaced `sys.stdout` with a `StringIO`, which has no
`.buffer`, so the standard fast-output idiom raised `AttributeError` and
was recorded as a Runtime Error. Capture now goes through a `BytesIO`
wrapped in a `write_through` `TextIOWrapper`, so text and byte writes
share one stream and keep their relative order.

This is the output-side counterpart of the `sys.stdin.buffer` fix in
#2824. **9 of the 16 Runtime Errors** in the measured run are exactly
this `AttributeError`.

## 3. Large integer output

The graded environment kept CPython's default 4300-digit cap on int→str
conversion, so printing a large answer raised `ValueError` and was
scored a Runtime Error. `import_string` now raises the limit, alongside
the existing recursion-limit bump. This did not fire in the measured run
but will on any problem with a large integer answer.

## 4. Call-based float lists

Only scalar predictions were passed to `np.allclose`, so a returned list
of floats within tolerance was scored Wrong Answer. Now compared
element-wise when both sides are lists.

## Deliberately not changed

Two differences from the Cascade harness alter accept/reject semantics
rather than fixing a defect, so they are left for a protocol decision
rather than folded in here:

- **Case-insensitive Yes/No.** The Cascade harness accepts `YES` against
`Yes`; this PR does not. Many judges are genuinely case-sensitive, so
matching it silently would be a scoring change, not a fix.
- **Memory cap.** `reliability_guard` caps at 4 GB here and is uncapped
in the Cascade harness. That accounts for the single `MemoryError` in
the measured run.

## Test plan

- `resources_servers/code_gen/tests/test_float_tolerance.py` (13 tests):
tolerance acceptance, per-token enforcement across a line, the
large-integer gotcha, small-integer off-by-one, token-count mismatch,
scientific notation, non-finite input.
- `resources_servers/code_gen/tests/test_grader_environment.py` (13
tests): `stdout.buffer` writes, text/byte write ordering, `close()` as a
no-op, stdout restoration, the int→str limit in both directions,
call-based float lists including length mismatch and integer lists.
- Full code_gen suite green: **33 passed**.
- End-to-end through `run_test`: all previously failing idioms now
accepted, `YES`/`Yes` still rejected as intended, and the ten
float-tolerance cases unregressed in both directions.
- Ruff lint, import order and format clean on both changed files (pinned
v0.9.9, matching `.pre-commit-config.yaml`).

Scores can only move up, and only where the grader was previously wrong.
This also removes false negatives from RL code verification, where a
wrongly-rejected correct rollout is a bad training signal.

## Checklist

- [x] I have read the [contributing
guidelines](https://docs.nvidia.com/nemo/gym/latest/contribute/development-setup).
- [x] The change is focused; unrelated "drive-by" edits are tracked as
separate issues/PRs.
- [x] Tests added or updated and pass locally, or N/A for docs-only /
non-code changes (so CI unit/server checks pass when applicable).
- [x] Pre-commit checks pass locally (`pre-commit run --all-files`) (so
CI lint/format/copyright pass).
- [x] All commits have DCO sign-off (`git commit -s`) (so the DCO check
passes).

---------

Signed-off-by: fgalko <[email protected]>
Signed-off-by: Junkeun Yi <[email protected]>
jkyi-nvidia pushed a commit that referenced this pull request Sep 11, 2026
…son (#3051)

## What does this PR do?

Accepts any capitalisation of a `Yes`/`No` verdict in the LiveCodeBench
stdout comparison, and only for those two words.

Codeforces-style statements print a Yes/No verdict and explicitly accept
any capitalisation, so a solution answering `YES` against a reference of
`Yes` is correct on the platform but was scored Wrong Answer here.

## Scope

Deliberately narrow. Tolerance for capitalisation applies only when the
predicted line is exactly `yes` or `no` (in any case) and both sides
agree case-insensitively. Every other answer stays case-sensitive, so
problems whose answer is a name or a word are unaffected, and a line
with more than one token is not treated as a verdict at all.

Verified end-to-end through the grader:

| submitted | expected | verdict |
|---|---|---|
| `YES` | `Yes` | accepted |
| `no` | `No` | accepted |
| `NO` | `Yes` | rejected |
| `SNUKE` | `Snuke` | rejected (stays case-sensitive) |
| `FIRST` | `First` | rejected (stays case-sensitive) |
| `alice` | `Alice` | rejected (stays case-sensitive) |
| `YES 1` | `Yes 1` | rejected (multi-token, not a verdict) |

## Why this is separate from #2956

The four changes in #2956 fixed grader faults: correct solutions being
scored wrong because of a defect (exact float equality, `StringIO`
having no `.buffer`, the int→str digit cap, missing list tolerance).
This one is different in kind — it changes accept/reject semantics to
match a platform convention, so it deserves its own review rather than
riding along.

## Effect on LiveCodeBench scores

**None measured on v6.** Across a 1362-rollout DeepSeek-V4-Flash LCB v6
run, none of the 66 wrong-answer failures differ from their reference by
capitalisation alone, so the score is unchanged (the run's projected
accuracy stays at 94.93%). The behaviour matters for Codeforces-sourced
problems rather than the AtCoder/LeetCode mix in v6.

## Test plan

- New
`resources_servers/code_gen/tests/test_case_insensitive_verdict.py` (14
tests): all capitalisation variants of Yes and No, `yes` not matching
`no`, verdicts not matching unrelated answers, other words staying
case-sensitive, and multi-token lines rejected.
- Full code_gen suite green: 52 passed.
- End-to-end through `run_test` on the 7 cases in the table above, all
behaving as intended in both directions.
- Ruff lint, import order and format clean on both changed files (pinned
v0.9.9, matching `.pre-commit-config.yaml`).

## Checklist

- [x] I have read the [contributing
guidelines](https://docs.nvidia.com/nemo/gym/latest/contribute/development-setup).
- [x] The change is focused; unrelated "drive-by" edits are tracked as
separate issues/PRs.
- [x] Tests added or updated and pass locally, or N/A for docs-only /
non-code changes (so CI unit/server checks pass when applicable).
- [x] Pre-commit checks pass locally (`pre-commit run --all-files`) (so
CI lint/format/copyright pass).
- [x] All commits have DCO sign-off (`git commit -s`) (so the DCO check
passes).

Signed-off-by: fgalko <[email protected]>
Signed-off-by: Junkeun Yi <[email protected]>

This branch was previously deployed

1 inactive deployment
public — 6a7673ae Deployed Sep 2, 2026 by copy-pr-bot[bot] via release / finalize / notify #2452
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants