Skip to content

fix: count sampled tokens with np.count_nonzero - #4251

Merged
faresobeid merged 1 commit into
feat/weight-version-labelsfrom
fix/weight-version-count-nonzero
Oct 24, 2026
Merged

faresobeid merged 1 commit into
feat/weight-version-labelsfrom
fix/weight-version-count-nonzero

Conversation

@mikasenghaas

@mikasenghaas mikasenghaas commented Oct 24, 2026 •

Copy link
Copy Markdown
Member

Summary

Targets #4141.

Checks: ruff check and ruff format --check pass. I also ran token_policy_ages on the test case with list masks and with numpy masks. Both returned (20, 8, 3) as plain ints. I did not run pytest, because the import chain needs torch and the test machine (macOS) does not have it.

🤖 Generated with Claude Code


Note

Low Risk
Small metric-calculation fix for numpy masks; behavior unchanged aside from correct int types and performance.

Overview
Fixes sampled-token counting now that MessageNode.mask is a numpy bool array (from verifiers): node_policy_versions uses int(np.count_nonzero(node.mask)) instead of sum(node.mask), avoiding slow element-wise summation and keeping metrics as plain Python ints rather than np.int64.

The unit test builds trace masks with np.array(...) to match production types.

Reviewed by Cursor Bugbot for commit 5a96878. Bugbot is set up for automated code reviews on this repo. Configure here.

MessageNode.mask is a numpy bool array since verifiers #2961. Python's
sum() walks it one element at a time and returns an np.int64. Use
np.count_nonzero instead, which is vectorized and also accepts lists.

Build the test masks as numpy arrays to cover the shape that main now
produces.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@faresobeid
faresobeid merged commit 0025738 into feat/weight-version-labels Oct 24, 2026
12 of 13 checks passed
@faresobeid
faresobeid deleted the fix/weight-version-count-nonzero branch October 24, 2026 18:54
faresobeid pushed a commit that referenced this pull request Oct 24, 2026
## Summary

- `node_policy_versions` counts sampled tokens with
`np.count_nonzero(node.mask)` instead of `sum(node.mask)`.
`MessageNode.mask` is a numpy bool array since
PrimeIntellect-ai/verifiers#2961, which #4226 pulled into main. Python's
`sum()` walks that array one token at a time and returns an `np.int64`.
`np.count_nonzero` is vectorized, accepts lists too, and the `int()`
cast keeps the metric values as plain ints.
- `test_token_policy_ages` builds its masks as numpy arrays, which is
the type main now produces.

Targets #4141.

Checks: `ruff check` and `ruff format --check` pass. I also ran
`token_policy_ages` on the test case with list masks and with numpy
masks. Both returned `(20, 8, 3)` as plain ints. I did not run pytest,
because the import chain needs `torch` and the test machine (macOS) does
not have it.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Low Risk**
> Small metric-calculation fix for numpy masks; behavior unchanged aside
from correct int types and performance.
> 
> **Overview**
> **Fixes sampled-token counting** now that `MessageNode.mask` is a
numpy bool array (from verifiers): `node_policy_versions` uses
`int(np.count_nonzero(node.mask))` instead of `sum(node.mask)`, avoiding
slow element-wise summation and keeping metrics as plain Python `int`s
rather than `np.int64`.
> 
> The unit test builds trace masks with `np.array(...)` to match
production types.
> 
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
5a96878. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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