project6
2102146c01
refactor(moe): translate thrust mode.cu pipeline — unique_consecutive replaces manual boundary detect
thrust/examples/mode.cu entire design (80 lines):
Complete GPU pipeline: sort → unique_count → reduce_by_key → max_element
Key operations:
1. thrust::sort — bring equal keys together (we already do: argsort)
2. thrust::unique_count — precompute number of unique keys for allocation
3. thrust::reduce_by_key(data, constant_iterator<1>) — count per key
4. thrust::max_element — find the mode (highest count)
Design principle: every step is a GPU primitive, no CPU round-trips.
constant_iterator<1> trick: turns reduce_by_key into count_by_key.
Translation to MoE segment detection:
Previous (4 GPU ops + CPU tensors):
changes = cat([True, sorted[1:] != sorted[:-1]])
seg_starts = changes.nonzero()
seg_ends = cat([seg_starts[1:], tensor([len])])
seg_eids = sorted[seg_starts]
Now (1 fused GPU op):
seg_eids, _, seg_counts = torch.unique_consecutive(sorted, return_counts=True)
seg_ends = seg_counts.cumsum(0)
seg_starts = cat([0, seg_ends[:-1]])
unique_consecutive IS mode.cu's sort+reduce_by_key fused: it returns
(unique_keys, inverse, counts) — exactly the data mode.cu builds from
reduce_by_key(data, constant_iterator<1>, keys_out, counts_out).
3 fewer GPU kernel launches per MoE forward.
CCCL source: thrust/examples/mode.cu
Maps to: qwen3_6_scripts/qwen3_5.py (_pure_pytorch_experts)
2026-08-07 09:17:23 +00:00
..
2026-07-30 16:06:20 +00:00
2026-07-30 16:06:20 +00:00
2026-08-07 07:55:04 +00:00
2026-08-07 08:56:50 +00:00
2026-07-30 16:06:20 +00:00
2026-08-05 08:24:43 +00:00
2026-08-07 07:05:40 +00:00
2026-07-30 16:06:20 +00:00
2026-08-05 08:24:43 +00:00
2026-08-06 06:33:26 +00:00
2026-08-07 08:56:36 +00:00
2026-08-07 04:37:44 +00:00
2026-08-07 04:44:18 +00:00
2026-08-07 06:20:02 +00:00
2026-07-30 16:06:20 +00:00
2026-08-07 02:46:46 +00:00
2026-08-07 08:18:41 +00:00
2026-08-07 09:17:23 +00:00
2026-08-07 07:45:28 +00:00
2026-08-05 08:36:52 +00:00
2026-08-06 02:55:51 +00:00
2026-07-30 16:06:20 +00:00
2026-07-30 16:06:20 +00:00
2026-08-07 09:01:57 +00:00
2026-08-05 08:36:52 +00:00
2026-08-07 06:36:12 +00:00
2026-08-07 01:54:52 +00:00