Datasets:
Upload folder using huggingface_hub
Browse files- README.md +0 -33
- reference/26_moe_token_preprocess.py +7 -2
- reference/31_fused_moe_fwd.py +6 -0
- reference/49_moe_ep_balanced.py +7 -0
- reference/50_moe_ep_wide.py +7 -0
- reference/64_gnn_neighbor_sampling.py +1 -4
- utils/input_output_tensors.py +37 -51
README.md
CHANGED
|
@@ -10,39 +10,6 @@ tags:
|
|
| 10 |
- benchmark
|
| 11 |
size_categories:
|
| 12 |
- n<1K
|
| 13 |
-
dataset_info:
|
| 14 |
-
features:
|
| 15 |
-
- name: problem_id
|
| 16 |
-
dtype: int64
|
| 17 |
-
- name: stem
|
| 18 |
-
dtype: large_string
|
| 19 |
-
- name: reference_code
|
| 20 |
-
dtype: large_string
|
| 21 |
-
- name: reference_path
|
| 22 |
-
dtype: large_string
|
| 23 |
-
- name: input_tensor_spec_path
|
| 24 |
-
dtype: large_string
|
| 25 |
-
- name: world_size
|
| 26 |
-
dtype: int64
|
| 27 |
-
- name: default_m
|
| 28 |
-
dtype: int64
|
| 29 |
-
- name: default_n
|
| 30 |
-
dtype: int64
|
| 31 |
-
- name: default_dtype
|
| 32 |
-
dtype: large_string
|
| 33 |
-
- name: default_trials
|
| 34 |
-
dtype: int64
|
| 35 |
-
splits:
|
| 36 |
-
- name: train
|
| 37 |
-
num_bytes: 282750
|
| 38 |
-
num_examples: 87
|
| 39 |
-
download_size: 85789
|
| 40 |
-
dataset_size: 282750
|
| 41 |
-
configs:
|
| 42 |
-
- config_name: default
|
| 43 |
-
data_files:
|
| 44 |
-
- split: train
|
| 45 |
-
path: data/train-*
|
| 46 |
---
|
| 47 |
|
| 48 |
# ParallelKernelBench (benchmark)
|
|
|
|
| 10 |
- benchmark
|
| 11 |
size_categories:
|
| 12 |
- n<1K
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
---
|
| 14 |
|
| 15 |
# ParallelKernelBench (benchmark)
|
reference/26_moe_token_preprocess.py
CHANGED
|
@@ -43,6 +43,11 @@ def solution(
|
|
| 43 |
expert_mask: torch.Tensor,
|
| 44 |
num_experts: int,
|
| 45 |
group: Optional[dist.ProcessGroup] = None,
|
| 46 |
-
) -> Tuple[
|
| 47 |
group = group or dist.group.WORLD
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
expert_mask: torch.Tensor,
|
| 44 |
num_experts: int,
|
| 45 |
group: Optional[dist.ProcessGroup] = None,
|
| 46 |
+
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
|
| 47 |
group = group or dist.group.WORLD
|
| 48 |
+
input_splits, output_splits, per_local_expert, sum_per_local_expert = _preprocess_impl(
|
| 49 |
+
expert_mask=expert_mask, num_experts=num_experts, ep_group=group
|
| 50 |
+
)
|
| 51 |
+
input_splits_t = torch.tensor(input_splits, dtype=torch.long)
|
| 52 |
+
output_splits_t = torch.tensor(output_splits, dtype=torch.long)
|
| 53 |
+
return (input_splits_t, output_splits_t, per_local_expert, sum_per_local_expert)
|
reference/31_fused_moe_fwd.py
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from typing import List, Optional, Tuple, Union
|
| 2 |
|
| 3 |
import torch
|
|
|
|
| 1 |
+
# Expert-parallel (EP) fused MoE forward — BASE case.
|
| 2 |
+
#
|
| 3 |
+
# This is the generic fused MoE forward pass: router (softmax + top-k) -> token
|
| 4 |
+
# permutation -> all_to_all dispatch -> per-expert SiLU MLP -> all_to_all combine ->
|
| 5 |
+
# weighted unpermute. Here the expert count is fixed (num_experts = 8) regardless of
|
| 6 |
+
# world size, so the EP load pattern depends on the launch configuration.
|
| 7 |
from typing import List, Optional, Tuple, Union
|
| 8 |
|
| 9 |
import torch
|
reference/49_moe_ep_balanced.py
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from typing import List, Optional, Tuple, Union
|
| 2 |
|
| 3 |
import torch
|
|
|
|
| 1 |
+
# Expert-parallel (EP) fused MoE forward — BALANCED EP.
|
| 2 |
+
#
|
| 3 |
+
# Same kernel as problem 31 (router -> permute -> all_to_all dispatch -> per-expert
|
| 4 |
+
# SiLU MLP -> all_to_all combine -> unpermute), but the harness sets
|
| 5 |
+
# num_experts == world_size, i.e. exactly one expert per rank. With uniform routing
|
| 6 |
+
# this gives the balanced all_to_all dispatch pattern (each rank sends/receives a
|
| 7 |
+
# roughly equal token count).
|
| 8 |
from typing import List, Optional, Tuple, Union
|
| 9 |
|
| 10 |
import torch
|
reference/50_moe_ep_wide.py
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from typing import List, Optional, Tuple, Union
|
| 2 |
|
| 3 |
import torch
|
|
|
|
| 1 |
+
# Expert-parallel (EP) fused MoE forward — WIDE EP.
|
| 2 |
+
#
|
| 3 |
+
# Same kernel as problem 31 (router -> permute -> all_to_all dispatch -> per-expert
|
| 4 |
+
# SiLU MLP -> all_to_all combine -> unpermute), but the harness sets
|
| 5 |
+
# num_experts == world_size * 2, i.e. multiple experts hosted per rank. This stresses
|
| 6 |
+
# the per-rank local expert loop and the larger, more fragmented all_to_all token
|
| 7 |
+
# dispatch relative to the balanced case.
|
| 8 |
from typing import List, Optional, Tuple, Union
|
| 9 |
|
| 10 |
import torch
|
reference/64_gnn_neighbor_sampling.py
CHANGED
|
@@ -25,10 +25,7 @@ def _sample_one_hop_csc_dist(
|
|
| 25 |
take = min(k, deg) if k >= 0 else deg
|
| 26 |
|
| 27 |
if take > 0:
|
| 28 |
-
|
| 29 |
-
perm = torch.randint(deg, (take,), device=input_nodes.device)
|
| 30 |
-
else:
|
| 31 |
-
perm = torch.randperm(deg, device=input_nodes.device)[:take]
|
| 32 |
sampled_nodes.append(row[start:end].index_select(0, perm))
|
| 33 |
sampled_edges.append(torch.arange(start, end, device=input_nodes.device).index_select(0, perm))
|
| 34 |
|
|
|
|
| 25 |
take = min(k, deg) if k >= 0 else deg
|
| 26 |
|
| 27 |
if take > 0:
|
| 28 |
+
perm = torch.arange(take, device=input_nodes.device)
|
|
|
|
|
|
|
|
|
|
| 29 |
sampled_nodes.append(row[start:end].index_select(0, perm))
|
| 30 |
sampled_edges.append(torch.arange(start, end, device=input_nodes.device).index_select(0, perm))
|
| 31 |
|
utils/input_output_tensors.py
CHANGED
|
@@ -7,8 +7,6 @@ consistent tensor creation and saving behavior.
|
|
| 7 |
|
| 8 |
import os
|
| 9 |
import json
|
| 10 |
-
import copy
|
| 11 |
-
import importlib.util
|
| 12 |
import math
|
| 13 |
|
| 14 |
import torch
|
|
@@ -53,7 +51,6 @@ def save_tensor(output, logs_dir: str, rank: int) -> str:
|
|
| 53 |
# but may include Python scalars / dicts / dataclasses (e.g. problem 4, problems 100–105).
|
| 54 |
# - solution(tensor) for single-tensor problems: x is (tensor,)
|
| 55 |
# - solution(t1, t2) for multi-arg problems: x is (t1, t2, ...)
|
| 56 |
-
# Problems 100–105: solution(rank, world_size, cfg, input_ids).
|
| 57 |
# Output from solution_fn may still be a single tensor or a tuple; save_tensor() handles both.
|
| 58 |
# ---------------------------------------------------------------------------
|
| 59 |
|
|
@@ -62,7 +59,6 @@ def _seed(problem_id: int, rank: int, trial: int = 0) -> None:
|
|
| 62 |
torch.manual_seed(42 + problem_id * 1000 + rank + trial * 1_000_003)
|
| 63 |
|
| 64 |
_PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
| 65 |
-
_REF_MODULES_CACHE: dict[int, object] = {}
|
| 66 |
|
| 67 |
def _round_up_multiple(n: int, m: int) -> int:
|
| 68 |
return ((n + m - 1) // m) * m
|
|
@@ -98,50 +94,6 @@ def _moe_narrow_num_experts(world_size: int) -> int:
|
|
| 98 |
def _linear(in_features: int, out_features: int, dtype: torch.dtype, device) -> torch.nn.Linear:
|
| 99 |
return torch.nn.Linear(in_features, out_features).to(device=device, dtype=dtype)
|
| 100 |
|
| 101 |
-
def _load_reference_module(problem_id: int):
|
| 102 |
-
if problem_id in _REF_MODULES_CACHE:
|
| 103 |
-
return _REF_MODULES_CACHE[problem_id]
|
| 104 |
-
stem = {
|
| 105 |
-
100: "100_deepseek_v3_671b_tp_attn_ep_moe",
|
| 106 |
-
101: "101_gemma3_27b_tp_attn_tp_mlp",
|
| 107 |
-
102: "102_llama32_3b_tp_attn_tp_mlp",
|
| 108 |
-
103: "103_olmo_3_32b_tp_attn_tp_mlp",
|
| 109 |
-
104: "104_qwen3_235b_tp_attn_ep_moe",
|
| 110 |
-
105: "105_qwen3_code_flash_30b_tp_attn_ep_moe",
|
| 111 |
-
106: "106_deepseek_v3_671b_cp_ulysses_attn_ep_moe",
|
| 112 |
-
107: "107_gemma3_27b_cp_ulysses_attn_tp_mlp",
|
| 113 |
-
108: "108_llama32_3b_cp_ulysses_attn_tp_mlp",
|
| 114 |
-
109: "109_olmo_3_32b_cp_ulysses_attn_tp_mlp",
|
| 115 |
-
110: "110_qwen3_235b_cp_ulysses_attn_ep_moe",
|
| 116 |
-
111: "111_qwen3_code_flash_30b_cp_ulysses_attn_ep_moe",
|
| 117 |
-
}[problem_id]
|
| 118 |
-
path = os.path.join(_PROJECT_ROOT, "reference", f"{stem}.py")
|
| 119 |
-
spec = importlib.util.spec_from_file_location(f"ref_{stem}", path)
|
| 120 |
-
mod = importlib.util.module_from_spec(spec)
|
| 121 |
-
spec.loader.exec_module(mod)
|
| 122 |
-
_REF_MODULES_CACHE[problem_id] = mod
|
| 123 |
-
return mod
|
| 124 |
-
|
| 125 |
-
def _align_model_args_100(cfg, world_size: int) -> None:
|
| 126 |
-
"""ModelArgs for reference/100: TP/EP divisibility constraints."""
|
| 127 |
-
cfg.n_layers = 2
|
| 128 |
-
for attr in ("dim", "inter_dim", "moe_inter_dim"):
|
| 129 |
-
v = getattr(cfg, attr)
|
| 130 |
-
if v % world_size:
|
| 131 |
-
setattr(cfg, attr, _round_up_multiple(v, world_size))
|
| 132 |
-
if cfg.vocab_size % world_size:
|
| 133 |
-
cfg.vocab_size = _round_up_multiple(cfg.vocab_size, world_size)
|
| 134 |
-
if cfg.n_heads % world_size:
|
| 135 |
-
cfg.n_heads = _round_up_multiple(cfg.n_heads, world_size)
|
| 136 |
-
if cfg.n_routed_experts % world_size:
|
| 137 |
-
cfg.n_routed_experts = _round_up_multiple(cfg.n_routed_experts, world_size)
|
| 138 |
-
shared = cfg.n_shared_experts * cfg.moe_inter_dim
|
| 139 |
-
guard = 0
|
| 140 |
-
while shared % world_size and guard < 4096:
|
| 141 |
-
cfg.moe_inter_dim += 1
|
| 142 |
-
shared = cfg.n_shared_experts * cfg.moe_inter_dim
|
| 143 |
-
guard += 1
|
| 144 |
-
|
| 145 |
def _common_attn_dims(base_shape, world_size):
|
| 146 |
"""Shared (B, T, num_heads, head_dim) from base_shape (M, N)."""
|
| 147 |
M, N = base_shape
|
|
@@ -277,6 +229,39 @@ def _build_polar_azimuth_groups(azimuth_size: int):
|
|
| 277 |
polar_rank = dist.get_rank(polar_group)
|
| 278 |
return azimuth_group, polar_group, azimuth_rank, polar_rank, azimuth_size, polar_size
|
| 279 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
def create_input_tensor(
|
| 281 |
rank: int,
|
| 282 |
world_size: int,
|
|
@@ -295,7 +280,7 @@ def create_input_tensor(
|
|
| 295 |
Args:
|
| 296 |
rank: Process rank (0..world_size-1)
|
| 297 |
world_size: Total number of processes
|
| 298 |
-
problem_id: Problem ID (e.g. 1–
|
| 299 |
base_shape: Base tensor shape tuple (e.g., (M, N))
|
| 300 |
dtype: Tensor data type
|
| 301 |
trial: Non-negative index; changes RNG for problems that use random inputs (trial=0 is legacy behavior).
|
|
@@ -438,7 +423,7 @@ def create_input_tensor(
|
|
| 438 |
elif problem_id == 21:
|
| 439 |
_seed(problem_id, rank, trial)
|
| 440 |
grad_tensors = [torch.randn(base_shape, dtype=dtype, device=dev) for _ in range(3)]
|
| 441 |
-
return (grad_tensors, 1.0, 2.0,
|
| 442 |
|
| 443 |
# 22: clip_grad_norm_ep
|
| 444 |
elif problem_id == 22:
|
|
@@ -446,7 +431,8 @@ def create_input_tensor(
|
|
| 446 |
non_ep = [torch.randn(base_shape, dtype=dtype, device=dev)]
|
| 447 |
ep_size = max(1, world_size // 2)
|
| 448 |
ep = [torch.randn(base_shape, dtype=dtype, device=dev)]
|
| 449 |
-
|
|
|
|
| 450 |
|
| 451 |
# 23: grad_acc_loss
|
| 452 |
elif problem_id == 23:
|
|
|
|
| 7 |
|
| 8 |
import os
|
| 9 |
import json
|
|
|
|
|
|
|
| 10 |
import math
|
| 11 |
|
| 12 |
import torch
|
|
|
|
| 51 |
# but may include Python scalars / dicts / dataclasses (e.g. problem 4, problems 100–105).
|
| 52 |
# - solution(tensor) for single-tensor problems: x is (tensor,)
|
| 53 |
# - solution(t1, t2) for multi-arg problems: x is (t1, t2, ...)
|
|
|
|
| 54 |
# Output from solution_fn may still be a single tensor or a tuple; save_tensor() handles both.
|
| 55 |
# ---------------------------------------------------------------------------
|
| 56 |
|
|
|
|
| 59 |
torch.manual_seed(42 + problem_id * 1000 + rank + trial * 1_000_003)
|
| 60 |
|
| 61 |
_PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
| 62 |
|
| 63 |
def _round_up_multiple(n: int, m: int) -> int:
|
| 64 |
return ((n + m - 1) // m) * m
|
|
|
|
| 94 |
def _linear(in_features: int, out_features: int, dtype: torch.dtype, device) -> torch.nn.Linear:
|
| 95 |
return torch.nn.Linear(in_features, out_features).to(device=device, dtype=dtype)
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
def _common_attn_dims(base_shape, world_size):
|
| 98 |
"""Shared (B, T, num_heads, head_dim) from base_shape (M, N)."""
|
| 99 |
M, N = base_shape
|
|
|
|
| 229 |
polar_rank = dist.get_rank(polar_group)
|
| 230 |
return azimuth_group, polar_group, azimuth_rank, polar_rank, azimuth_size, polar_size
|
| 231 |
|
| 232 |
+
def _build_ep_fsdp_groups(ep_size: int):
|
| 233 |
+
"""
|
| 234 |
+
Build a 2D EP x EP-FSDP process grid for problem 22 (EP-aware grad clipping).
|
| 235 |
+
Rank layout: rank = ep_fsdp_idx * ep_size + ep_idx. The EP group reduces across
|
| 236 |
+
experts (contiguous blocks of ep_size); the EP-FSDP group reduces across the FSDP
|
| 237 |
+
replicas of each expert (strided). The two groups tile the world, so reducing an
|
| 238 |
+
EP grad term over both stages sums it across every rank exactly once.
|
| 239 |
+
|
| 240 |
+
Returns (ep_group, ep_fsdp_group). For a degenerate EP layout (ep_size <= 1 or it
|
| 241 |
+
does not divide world_size) the EP term is reduced over the whole world in a single
|
| 242 |
+
stage: (None, WORLD).
|
| 243 |
+
"""
|
| 244 |
+
world_size = dist.get_world_size()
|
| 245 |
+
rank = dist.get_rank()
|
| 246 |
+
if ep_size <= 1 or world_size % ep_size != 0:
|
| 247 |
+
return None, dist.group.WORLD
|
| 248 |
+
|
| 249 |
+
ep_fsdp_size = world_size // ep_size
|
| 250 |
+
ep_group = None
|
| 251 |
+
ep_fsdp_group = None
|
| 252 |
+
for blk in range(ep_fsdp_size):
|
| 253 |
+
ranks = list(range(blk * ep_size, (blk + 1) * ep_size))
|
| 254 |
+
g = dist.new_group(ranks=ranks)
|
| 255 |
+
if rank in ranks:
|
| 256 |
+
ep_group = g
|
| 257 |
+
for ep_idx in range(ep_size):
|
| 258 |
+
ranks = [ep_idx + blk * ep_size for blk in range(ep_fsdp_size)]
|
| 259 |
+
g = dist.new_group(ranks=ranks)
|
| 260 |
+
if rank in ranks:
|
| 261 |
+
ep_fsdp_group = g
|
| 262 |
+
assert ep_group is not None and ep_fsdp_group is not None
|
| 263 |
+
return ep_group, ep_fsdp_group
|
| 264 |
+
|
| 265 |
def create_input_tensor(
|
| 266 |
rank: int,
|
| 267 |
world_size: int,
|
|
|
|
| 280 |
Args:
|
| 281 |
rank: Process rank (0..world_size-1)
|
| 282 |
world_size: Total number of processes
|
| 283 |
+
problem_id: Problem ID (e.g. 1–87) from reference filename
|
| 284 |
base_shape: Base tensor shape tuple (e.g., (M, N))
|
| 285 |
dtype: Tensor data type
|
| 286 |
trial: Non-negative index; changes RNG for problems that use random inputs (trial=0 is legacy behavior).
|
|
|
|
| 423 |
elif problem_id == 21:
|
| 424 |
_seed(problem_id, rank, trial)
|
| 425 |
grad_tensors = [torch.randn(base_shape, dtype=dtype, device=dev) for _ in range(3)]
|
| 426 |
+
return (grad_tensors, 1.0, 2.0, dist.group.WORLD)
|
| 427 |
|
| 428 |
# 22: clip_grad_norm_ep
|
| 429 |
elif problem_id == 22:
|
|
|
|
| 431 |
non_ep = [torch.randn(base_shape, dtype=dtype, device=dev)]
|
| 432 |
ep_size = max(1, world_size // 2)
|
| 433 |
ep = [torch.randn(base_shape, dtype=dtype, device=dev)]
|
| 434 |
+
ep_group, ep_fsdp_group = _build_ep_fsdp_groups(ep_size)
|
| 435 |
+
return (non_ep, ep, 1.0, 2.0, ep_size, dist.group.WORLD, ep_fsdp_group, ep_group)
|
| 436 |
|
| 437 |
# 23: grad_acc_loss
|
| 438 |
elif problem_id == 23:
|