Skip to content

Commit a75bc78

Browse files
committed
Merge branch 'flagos-ai:master' into w
2 parents 4224225 + b522242 commit a75bc78

70 files changed

Lines changed: 2304 additions & 367 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
"""
2+
Benchmark: fused_add_rms_norm
3+
Compares: FlagGems vs torch.compile vs vLLM (if available)
4+
"""
5+
6+
import time
7+
8+
import torch
9+
10+
import flag_gems
11+
12+
13+
# ── reference: naive torch ──────────────────────────────────────────────
14+
def torch_fused_add_rms_norm(x, residual, weight, eps=1e-5):
15+
x = x + residual
16+
variance = x.pow(2).mean(-1, keepdim=True)
17+
return x * torch.rsqrt(variance + eps) * weight, x
18+
19+
20+
# ── reference: torch.compile ────────────────────────────────────────────
21+
@torch.compile
22+
def compiled_fused_add_rms_norm(x, residual, weight, eps=1e-5):
23+
x = x + residual
24+
variance = x.pow(2).mean(-1, keepdim=True)
25+
return x * torch.rsqrt(variance + eps) * weight, x
26+
27+
28+
# ── reference: vLLM ─────────────────────────────────────────────────────
29+
try:
30+
import os
31+
32+
os.environ["VLLM_CONFIGURE_LOGGING"] = "0"
33+
from vllm._custom_ops import fused_add_rms_norm as vllm_fused_add_rms_norm
34+
35+
HAS_VLLM = True
36+
except (ImportError, AttributeError):
37+
HAS_VLLM = False
38+
print("vLLM not available, skipping vLLM baseline\n")
39+
40+
41+
# ── benchmark helper ────────────────────────────────────────────────────
42+
def bench_fn(fn, warmup=20, rep=100):
43+
for _ in range(warmup):
44+
fn()
45+
torch.cuda.synchronize()
46+
t0 = time.perf_counter()
47+
for _ in range(rep):
48+
fn()
49+
torch.cuda.synchronize()
50+
t1 = time.perf_counter()
51+
return (t1 - t0) / rep * 1000 # ms
52+
53+
54+
# ── main ────────────────────────────────────────────────────────────────
55+
shapes = [
56+
(1, 4096),
57+
(32, 4096),
58+
(128, 4096),
59+
(512, 4096),
60+
(1024, 4096),
61+
(4096, 4096),
62+
(128, 8192),
63+
(128, 11008),
64+
]
65+
dtypes = [torch.float16, torch.bfloat16]
66+
67+
print(f"{'shape':>18s} {'dtype':>10s} | {'naive':>8s} {'compile':>8s}", end="")
68+
if HAS_VLLM:
69+
print(f" {'vllm':>8s}", end="")
70+
print(f" {'flaggems':>8s} (ms)")
71+
print("-" * 80)
72+
73+
for shape in shapes:
74+
for dtype in dtypes:
75+
M, N = shape
76+
device = "cuda"
77+
eps = 1e-5
78+
79+
x_ref = torch.randn(M, N, dtype=dtype, device=device)
80+
r_ref = torch.randn(M, N, dtype=dtype, device=device)
81+
w = torch.randn(N, dtype=dtype, device=device)
82+
83+
# ── naive torch ──
84+
t_naive = bench_fn(
85+
lambda: torch_fused_add_rms_norm(x_ref.clone(), r_ref.clone(), w, eps)
86+
)
87+
88+
# ── torch.compile ──
89+
# warmup compile
90+
_ = compiled_fused_add_rms_norm(x_ref.clone(), r_ref.clone(), w, eps)
91+
t_compile = bench_fn(
92+
lambda: compiled_fused_add_rms_norm(x_ref.clone(), r_ref.clone(), w, eps)
93+
)
94+
95+
# ── vLLM ──
96+
if HAS_VLLM:
97+
# vLLM's fused_add_rms_norm is in-place: (x, residual) modified
98+
def run_vllm():
99+
xc = x_ref.clone()
100+
rc = r_ref.clone()
101+
vllm_fused_add_rms_norm(xc, rc, w, eps)
102+
103+
t_vllm = bench_fn(run_vllm)
104+
105+
# ── FlagGems ──
106+
def run_gems():
107+
xc = x_ref.clone()
108+
rc = r_ref.clone()
109+
flag_gems.fused_add_rms_norm(xc, rc, (N,), w, eps)
110+
111+
t_gems = bench_fn(run_gems)
112+
113+
# ── print ──
114+
tag = f"({M}, {N})"
115+
print(f"{tag:>18s} {str(dtype):>10s} | {t_naive:8.3f} {t_compile:8.3f}", end="")
116+
if HAS_VLLM:
117+
print(f" {t_vllm:8.3f}", end="")
118+
print(f" {t_gems:8.3f}")

benchmark/conftest.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pytest
66
import torch
7+
import yaml
78

89
import flag_gems
910
from benchmark.attri_util import (
@@ -58,6 +59,7 @@ def __init__(self):
5859

5960

6061
Config = BenchConfig()
62+
REGISTERED_MARKS = []
6163

6264

6365
def pytest_addoption(parser):
@@ -156,9 +158,21 @@ def pytest_addoption(parser):
156158
),
157159
)
158160

161+
parser.addoption(
162+
"--collect-marks",
163+
action="store_true",
164+
help="Collect the tests with marker information without executing them",
165+
)
166+
159167

160168
def pytest_configure(config):
161169
global Config # noqa: F824
170+
global REGISTERED_MARKS
171+
172+
REGISTERED_MARKS = {
173+
marker.split(":")[0].strip() for marker in config.getini("markers")
174+
}
175+
162176
mode_value = config.getoption(
163177
"--mode" if vendor_name != "kunlunxin" else "--fg_mode"
164178
)
@@ -278,3 +292,33 @@ def extract_and_log_op_attributes(request):
278292
yield
279293
if Config.record_log and op_attributes:
280294
emit_record_logger(json.dumps(op_attributes, indent=2))
295+
296+
297+
def pytest_collection_modifyitems(session, config, items):
298+
if config.getoption("--collect-marks"):
299+
report = []
300+
for item in items:
301+
data = {}
302+
303+
# Collect some general information
304+
if item.cls:
305+
data["class"] = item.cls.__name__
306+
data["test_case"] = item.name
307+
if item.originalname:
308+
data["function"] = item.originalname
309+
data["file"] = item.location[0]
310+
311+
all_marks = list(item.iter_markers())
312+
op_marks = [
313+
mark.name
314+
for mark in all_marks
315+
if mark.name not in BUILTIN_MARKS and mark.name not in REGISTERED_MARKS
316+
]
317+
318+
data["marks"] = op_marks
319+
report.append(data)
320+
321+
print(yaml.dump(report, indent=2))
322+
323+
# Skip all tests
324+
items.clear()

benchmark/core_shapes.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ BlasBenchmark:
5454
- [16, 4096, 4096, 4096]
5555
shape_desc: "B, M, N, K" # shapes are defined as (B, M, N, K)
5656

57+
groupmm:
58+
shapes:
59+
- [16, 512, 2048]
60+
- [16, 2560, 2048]
61+
- [64, 2048, 128]
62+
shape_desc: "Groups, N, K" # shapes are defined as (Groups, N, K)
63+
5764
MvAndOuterBenchmark:
5865
shapes:
5966
- [384, 384]

0 commit comments

Comments
 (0)