Repository navigation
fix: count sampled tokens with np.count_nonzero - #4251
Merged
faresobeid merged 1 commit intoOct 24, 2026
Merged
Conversation
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
merged commit Oct 24, 2026
0025738
into
feat/weight-version-labels
12 of 13 checks passed
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
node_policy_versionscounts sampled tokens withnp.count_nonzero(node.mask)instead ofsum(node.mask).MessageNode.maskis a numpy bool array since perf(v1): per-token node streams as numpy arrays verifiers#2961, which perf: per-token streams as numpy arrays, micro batches as raw buffers #4226 pulled into main. Python'ssum()walks that array one token at a time and returns annp.int64.np.count_nonzerois vectorized, accepts lists too, and theint()cast keeps the metric values as plain ints.test_token_policy_agesbuilds its masks as numpy arrays, which is the type main now produces.Targets #4141.
Checks:
ruff checkandruff format --checkpass. I also rantoken_policy_ageson 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 needstorchand 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.maskis a numpy bool array (from verifiers):node_policy_versionsusesint(np.count_nonzero(node.mask))instead ofsum(node.mask), avoiding slow element-wise summation and keeping metrics as plain Pythonints rather thannp.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.