Skip to content

perf(r3): avoid full-size copies in routed-experts validation - #2531

Open
dogeONE-bit wants to merge 1 commit into
THUDM:mainfrom
dogeONE-bit:perf/r3-validate-aminmax
Open

dogeONE-bit wants to merge 1 commit into
THUDM:mainfrom
dogeONE-bit:perf/r3-validate-aminmax

Conversation

@dogeONE-bit

Copy link
Copy Markdown

Problem

With --use-rollout-routing-replay, BatchBuilder.convert runs validate_routed_experts_tensor on every sample that carries routes. On the default object-store transport, which all R3 recipes under scripts/ use, this loop runs serially in the RolloutManager after generation and before train() can start. The manager is a num_cpus=1 Ray actor, so Ray sets OMP_NUM_THREADS=1 and 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:

  1. indexes the MoE layers with a Python list (experts[:, moe_layers, :]), which copies all routes;
  2. repeats that for the range check, casts the copy to int64 and builds two full-size masks.

For a Qwen3-30B-A3B sample with 32k tokens (12.6M route entries) this takes about 210 ms of CPU.

Change

  • Use a slice view when the MoE layers are contiguous, which covers all-MoE and first-k-dense layouts (every --moe-layer-freq under scripts/models). Other layouts are indexed once instead of twice.
  • Check the expert-id range with 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. baseline is the function from main (79aa41a), patched is this branch.

Layout (layers, top-k) Rows Baseline Patched Speedup
Qwen3-30B-A3B (48 MoE, 8) 8,192 49.5 ms 9.4 ms 5.3x
Qwen3-30B-A3B (48 MoE, 8) 32,768 211.3 ms 38.6 ms 5.5x
GLM-4.7-30B-A3B (1 dense + 46 MoE, 4) 8,192 19.2 ms 6.0 ms 3.2x
GLM-4.7-30B-A3B (1 dense + 46 MoE, 4) 32,768 102.7 ms 31.2 ms 3.3x
Kimi-K2-Thinking (1 dense + 60 MoE, 8) 8,192 56.1 ms 18.4 ms 3.0x
Kimi-K2-Thinking (1 dense + 60 MoE, 8) 32,768 263.2 ms 76.1 ms 3.5x
GLM-4.5/4.7-355B (3 dense + 89 MoE, 8) 8,192 72.8 ms 19.3 ms 3.8x
GLM-4.5/4.7-355B (3 dense + 89 MoE, 8) 32,768 395.1 ms 111.9 ms 3.5x

I also timed the full BatchBuilder.convert with R3 enabled, swapping only the validation function between arms (one warmup, 5 interleaved runs, median CPU time):

  • 64 Qwen3-30B-A3B-shaped samples (lognormal lengths, mean about 10.8k tokens, 0.99 GiB of routes): 3.68 s → 0.70 s, saving 46.6 ms per sample.
  • 24 GLM-4.5/4.7-355B-shaped samples (mean about 13.9k tokens): 3.74 s → 1.04 s, saving 113 ms per sample.

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 the cpu-unittest matrix with NUM_GPUS = 0:

    • the range check matches the element-wise definition for uint8, int32 and int64 routes on a dense-prefix, a strided and a gapped layout (the last two take the indexing path);
    • a missing MoE layer is reported with the right layer id on a strided layout;
    • uint8 id 255 is accepted with 256 experts.

    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_freq form; 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.py passes.

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.

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>
@dogeONE-bit

Copy link
Copy Markdown
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!

@dogeONE-bit

Copy link
Copy Markdown
Author

Would you review the routed-expert validation fast path when time permits? I would appreciate a check of the slice-view/aminmax path and its fallback to the original validation when the bounds do not establish valid expert IDs. Thanks!

This branch has not been deployed

No deployments
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.

1 participant