Repository navigation
perf(r3): avoid full-size copies in routed-experts validation - #2531
Open
dogeONE-bit wants to merge 1 commit into
Open
dogeONE-bit wants to merge 1 commit into
dogeONE-bit wants to merge 1 commit into
Conversation
With --use-rollout-routing-replay, BatchBuilder.convert validates every sample's routes. On the default object-store transport that loop runs serially in the rollout manager before training starts. Each call indexed the MoE layers twice (two full copies), cast one copy to int64 and built two full-size masks just to check the expert-id range. Slice the MoE layers when they are contiguous (all-MoE and first-k-dense layouts) and index once otherwise. Check the id range with torch.aminmax and fall back to the unchanged element-wise check whenever the bounds do not prove every id valid, so errors and messages stay the same. On one CPU thread the check is 5.3-5.5x faster for Qwen3-30B-A3B routes and 3.0-3.8x faster for dense-prefix layouts (GLM-4.x, Kimi-K2). AI assistance (Cursor) was used for this change. Co-authored-by: Cursor <cursoragent@cursor.com>
Author
|
When time permits, could you review the routed-experts validation change that avoids full-size copies and the benchmark evidence in the description? Thanks! |
Author
|
Would you review the routed-expert validation fast path when time permits? I would appreciate a check of the slice-view/ |
This branch has not been deployed
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
With
--use-rollout-routing-replay,BatchBuilder.convertrunsvalidate_routed_experts_tensoron every sample that carries routes. On the defaultobject-storetransport, which all R3 recipes underscripts/use, this loop runs serially in theRolloutManagerafter generation and beforetrain()can start. The manager is anum_cpus=1Ray actor, so Ray setsOMP_NUM_THREADS=1and torch runs the check on one thread. With straw, the same check runs per sample when groups are published (SampleCodec._prepare_sample).Per sample, the check:
experts[:, moe_layers, :]), which copies all routes;For a Qwen3-30B-A3B sample with 32k tokens (12.6M route entries) this takes about 210 ms of CPU.
Change
--moe-layer-frequnderscripts/models). Other layouts are indexed once instead of twice.torch.aminmax. The previous element-wise check still runs, unchanged, whenever the bounds do not prove every id valid (NaN bounds included). Accepted inputs, raised errors and messages are therefore the same as before.Benchmark
CPU only, one torch thread as in the manager actor, torch 2.15.0+cpu, Python 3.11. Routes are int32, uniform in
[0, num_experts). Each arm had one warmup and 9 interleaved runs; the table shows the median thread CPU time. The host was shared with other jobs, so I report CPU time; wall-time medians in this run were within 2 ms of these.baselineis the function from main (79aa41a),patchedis this branch.I also timed the full
BatchBuilder.convertwith R3 enabled, swapping only the validation function between arms (one warmup, 5 interleaved runs, median CPU time):At the batch size of
scripts/run-qwen3-30B-A3B.sh(32 prompts × 8 samples = 256) and similar lengths, that is about 12 s less manager time per rollout before training starts. The check also no longer allocates a per-sample int64 copy of the routes, which is twice the size of the int32 tensor.Tests
New tests in
tests/test_rollout_data_utils.py, which is already in thecpu-unittestmatrix withNUM_GPUS = 0:These tests also pass on main, as expected for a change that should not alter behaviour. Breaking the layer selection or the upper bound in the patch makes them fail.
A differential script, not included in this PR, compared the old and new function on 40,000 random inputs: uint8, int8, int16, int32, int64 and float32 including NaN; every
moe_layer_freqform; top-k 0 to 8; and injected zero layers, partial zeros, hole rows, out-of-range ids and row or shape mismatches. Exceptions, messages and warnings were identical.python tests/<file>.py:test_rollout_data_utils.py(19 passed),test_straw_r3.py(9),test_routed_experts_disk_layout.py(52),test_queue_sample_codec.py(22),test_rollout_straw_transport.py(87),test_rollout_metadata_index.py(9),test_streaming_rollout.py(29),test_sample.py(12),test_distributed_rollout.py(135).pre-commit run --files slime/utils/routed_experts.py tests/test_rollout_data_utils.pypasses.Not run: GPU and e2e R3 tests, because I have no GPU here. Trainer-side handling of the routes is unchanged.
AI assistance (Cursor) was used for this change.