Skip to content

fix(calibrate): run records_from_labeled with gradients off - #832

Merged
NandhaKishorM merged 2 commits into
NandhaKishorM:mainfrom
abhijithneilabraham:fix/records-from-labeled-no-grad
Oct 2, 2026
Merged

NandhaKishorM merged 2 commits into
NandhaKishorM:mainfrom
abhijithneilabraham:fix/records-from-labeled-no-grad

Conversation

@abhijithneilabraham

Copy link
Copy Markdown
Contributor

What

records_from_labeled now runs under @torch.no_grad(). This is a one line change in laya/calibrate.py, plus a test in tests/test_calibration_persistence.py that calls it on a loaded agent.

Why

records_from_labeled raised RuntimeError: Can't call numpy() on Tensor that requires grad on any loaded Agent, so the path the Agent.fit_temperatures docstring points to for building calibration records did not work.

Agent._forward converts the logits to numpy and expects its caller to have turned gradients off. predict_batch does that. records_from_labeled did not.

The script from #826 on the convaiinnovations/laya checkpoint:

main:         RuntimeError: Can't call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.
this branch:  [(0, array([ 4.2743545, -2.4033804], dtype=float32), array([1., 0.], dtype=float32), 2)]

How it was verified

python tests/test_calibration_persistence.py
python tests/test_calibrate.py
ruff check laya/ --select=E9,F63,F7,F82,F401,F811 --line-length=120
python -m compileall -q laya/ tests/
  • tests/test_calibration_persistence.py: 7 tests, OK (6 on main). With the calibrate.py change removed: the new test fails with the RuntimeError above.
  • The new test loads the tiny local checkpoint that file already builds and runs the model's own forward pass. The stand-in agents in tests/test_calibrate.py return numpy from _forward, so they could not catch this.
  • tests/test_calibrate.py: 132 passed, 0 failed.
  • I also ran every script suite and pytest suite that the CI tests job runs, in a fresh Python 3.12 environment installed with pip install -e ".[mcp,serve,langchain]" httpx pytest: all passed.
  • ruff: all checks passed. compileall: clean.

Checklist

  • Focused on one change (split unrelated work into another PR)
  • Rebased on the latest main
  • Tests pass locally
  • Docs or examples updated when the public API changed (no API change)

Fixes #826

Agent._forward converts the logits to numpy and expects its caller to have
turned gradients off. predict_batch does that, records_from_labeled did not, so
it raised RuntimeError: Can't call numpy() on Tensor that requires grad on any
loaded Agent.
The stand-in agents in test_calibrate.py return numpy from _forward, so they
never reach the failing line. This one loads the tiny local checkpoint and runs
the model's own forward pass.
@NandhaKishorM
NandhaKishorM merged commit 253b831 into NandhaKishorM:main Oct 2, 2026
@NandhaKishorM

Copy link
Copy Markdown
Owner

Merged for 0.3.24.

Verified on the integrated branch rather than on the PR alone: the full shared test list plus the pytest lane green, compileall clean, both TypeScript packages green (laya-ts 597 tests, the SDK 26), and a decision diff against 0.3.23 on the real english and multilingual checkpoints (74 answers over choice, score and noul; short and long states; budgeted calls and predict_long) showing zero decision flips and a max probability delta of 0.000000.

Thank you.

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.

[bug]: records_from_labeled fails on a loaded Agent with "Can't call numpy() on Tensor that requires grad"

2 participants