[perf] Vectorize generate_batch_meta's force_fetch readiness check - #152
Merged
0oshowero0 merged 1 commit intoAug 12, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves controller-side metadata generation performance by removing per-sample Python loops in the force_fetch readiness calculation and by reducing per-field membership-check overhead when assembling custom backend metadata.
Changes:
- Vectorizes
generate_batch_meta(..., mode="force_fetch")readiness computation via a batched row/column gather and row-wise reduction. - Optimizes
get_field_custom_backend_metafield filtering by convertingfield_namesto asetfor O(1) membership checks.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+1439
to
+1450
| rows = np.fromiter(batch_global_indexes, dtype=np.int64, count=batch_size) | ||
| in_range = rows < partition.production_status.shape[0] | ||
| if in_range.any(): | ||
| row_idx = torch.from_numpy(rows[in_range]) | ||
| col_idx = torch.as_tensor(field_indices, dtype=torch.long) | ||
| ready = ( | ||
| (partition.production_status[row_idx[:, None], col_idx] == 1) | ||
| .all(dim=1) | ||
| .to(torch.int8) | ||
| .numpy() | ||
| ) | ||
| production_status[in_range] = ready |
0oshowero0
approved these changes
Aug 12, 2026
CLA Signature Guide@Chase-Rong , thanks for your pull request. The following commit(s) are not associated with a signed Contributor License Agreement (CLA).
To sign CLA, click here. To check if your email is configured correctly, refer to the FAQs. Once you've signed the CLA or updating your email, please comment |
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.
Problem
The
force_fetchbranch decided per-sample readiness with a Python loop:production_statusis anint8tensor andfield_indicesis a Python list, so everyiteration pays:
field_indicesto a tensor and allocates afresh result tensor (1 ATen dispatch),
== 1, allocating a bool tensor (2nd dispatch),torch.all(...)reducing to a 0-dim tensor (3rd dispatch),.item()whenifconverts that 0-dim tensor to a Python bool.That is ~3 dispatches, 2 tensor allocations, one list→tensor conversion and one
tensor→scalar conversion per sample, to compare 20 bytes of
int8. Measured~52us per sample — the actual comparison is nanoseconds, so essentially all of it is
per-op overhead. On the controller's single request-handling thread that came to
~40ms for 512 samples and ~100ms for 1024.
Separately,
get_field_custom_backend_metatested field membership against a listonce per (sample, field) pair, i.e. O(N x F^2) where F is the registered field count.
Change
One batched gather instead of the loop:
row_idx[:, None]withcol_idxpulls thewhole
(batch, fields)submatrix in a single index, then one== 1and one.all(dim=1)row-reduction. The bounds check becomes a vectorisedrows < shape[0]instead of a per-iteration Python
if.The number of bytes read is unchanged. What goes away is the fixed cost paid once per
sample: dispatches drop from ~3N to ~6, list→tensor conversions from N to 1, and the
Python loop disappears entirely.
Also make the membership test use a set, making that helper O(N x F).
Measurements
Ascend NPU + openYuanrong, verl GRPO, 28 registered fields, on
mainat 750719e withthis PR applied alone (the other two perf branches reverted to baseline). Medians;
40+ calls per bucket per run:
generate_batch_meta, 512 samplesgenerate_batch_meta, 1024 samplesget_field_custom_backend_meta, 512get_field_custom_backend_meta, 1024Split of the 512-sample saving: 29.5ms from the loop, 3.8ms from the set. At 1024 it
is 61.0ms and 26.0ms — the helper's share grows because its baseline scales
super-linearly (7.3 -> 32.8ms for 2x the samples).
The ratio barely moves with batch size (6.0x vs 7.7x) because the old cost was
O(N) x large constantand the new one isO(N) x small constant. Two independentbaseline runs in the same session measured 40.43 and 44.44ms for the 512 bucket
(+-2% around the mean), so the ratio is well outside run-to-run noise.
Distribution matters more than the mean: before, nearly every batch call took
40-100ms; after, they are consistently under 20ms.
Scope and correctness
Three call sites benefit:
kv_retrieve_meta,_handle_get_partition_meta_request,and
get_metadata(mode="force_fetch")directly.Behaviour is unchanged, including the out-of-range guard for global indexes beyond
production_statusand the emptyfield_indicescase. Verified byte-identicalproduction_statusagainst the old loop for fully-ready, half-ready, none-ready,out-of-range and empty-
data_fieldsinputs.