Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,17 @@ dense_gemm(a_fp8, b_fp8, scale_a, scale_b, output, mode="w8a8-mxfp8")
print(output.shape, output.dtype)
```

Native BF16 needs no quantization; pass `None` for the scale factors:

```python
output = torch.empty((M, N), device="cuda", dtype=torch.bfloat16)
dense_gemm(a, b, None, None, output, mode="bf16")
print(output.shape, output.dtype)
```

## Roadmap

- **Complete GEMM coverage**: add W4A8, BF16 and other GEMM types to build a full quantization-type support matrix, so users can pick the trade-off between inference speed and accuracy they need.
- **Complete GEMM coverage**: add W4A8 and other GEMM types to build a full quantization-type support matrix, so users can pick the trade-off between inference speed and accuracy they need.
- **Higher-performance MegaKernels**: cover more compute-communication fusion scenarios and further overlap compute with memory traffic.

## Contributing
Expand Down
10 changes: 9 additions & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,17 @@ dense_gemm(a_fp8, b_fp8, scale_a, scale_b, output, mode="w8a8-mxfp8")
print(output.shape, output.dtype)
```

原生 BF16 不需要量化,scale factor 传 `None` 即可:

```python
output = torch.empty((M, N), device="cuda", dtype=torch.bfloat16)
dense_gemm(a, b, None, None, output, mode="bf16")
print(output.shape, output.dtype)
```

## 路线图

- **全面的 GEMM 支持矩阵**:覆盖 W4A8、BF16 等类型的 GEMM,打造全量化类型的 GEMM 支持矩阵,满足不同推理性能与效果需求下的选择。
- **全面的 GEMM 支持矩阵**:覆盖 W4A8 等类型的 GEMM,打造全量化类型的 GEMM 支持矩阵,满足不同推理性能与效果需求下的选择。
- **更高性能的巨内核方案**:覆盖更多场景的通算融合,以及计算与访存 overlap 的优化方案。

## 如何贡献
Expand Down
20 changes: 20 additions & 0 deletions benchmark/ci_configs/gemm/batch_bf16.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
scenario: batch_gemm
mode: bf16

# Shapes: [B, M, N, K].
shapes:
- [8, 8192, 1024, 4096]
- [8192, 64, 128, 4096]
- [8192, 64, 512, 576]
- [8192, 64, 512, 64]
- [8192, 64, 256, 64]
- [8, 1024, 4096, 4096]
- [8, 128, 4096, 4096]
- [4, 256, 7168, 4096]
- [1, 8192, 4096, 4096]
sm_count: -1

backends: [halops]
cold_l2_cache: true
refcheck: false
refcheck_reference: golden
20 changes: 20 additions & 0 deletions benchmark/ci_configs/gemm/dense_bf16.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
scenario: dense_gemm
mode: bf16

shapes:
- [64, 4096, 4096]
- [64, 32768, 1024]
- [128, 4096, 4096]
- [128, 128, 4096]
- [256, 4096, 4096]
- [1024, 4096, 4096]
- [1024, 1600, 4096]
- [1024, 32768, 1024]
- [4096, 4096, 4096]
- [8192, 32768, 1024]
sm_count: -1

backends: [halops]
cold_l2_cache: true
refcheck: false
refcheck_reference: golden
21 changes: 21 additions & 0 deletions benchmark/configs/gemm/batch_bf16.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Shapes: [B, M, N, K].
scenario: batch_gemm
mode: bf16

shapes:
- [8, 8192, 1024, 4096]
- [8192, 64, 128, 4096]
- [8192, 64, 512, 576]
- [8192, 64, 512, 64]
- [8192, 64, 256, 64]
- [8, 1024, 4096, 4096]
- [8, 128, 4096, 4096]
- [4, 256, 7168, 4096]
- [1, 8192, 4096, 4096]

sm_count: -1

backends: [halops, fi, fi_cutlass, bmm]
cold_l2_cache: true
refcheck: true
refcheck_reference: golden
19 changes: 19 additions & 0 deletions benchmark/configs/gemm/dense_bf16.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
scenario: dense_gemm
mode: bf16
shapes:
- [64, 4096, 4096]
- [64, 32768, 1024]
- [128, 4096, 4096]
- [128, 128, 4096]
- [256, 4096, 4096]
- [1024, 4096, 4096]
- [1024, 1600, 4096]
- [1024, 32768, 1024]
- [4096, 4096, 4096]
- [8192, 32768, 1024]
sm_count: -1

backends: [halops, fi, deepgemm]
cold_l2_cache: true
refcheck: true
refcheck_reference: golden
19 changes: 19 additions & 0 deletions benchmark/configs/gemm/group_contiguous_bf16.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
scenario: group_contiguous
mode: bf16

# Shapes: [num_experts, max_m, N, K].
shapes:
- [4, 128, 256, 512]
- [4, 256, 1024, 1024]
- [8, 512, 7168, 3584]
- [10, 4096, 7168, 3584]
- [4, 128, 4096, 4096]
- [8, 512, 4096, 4096]
- [8, 1024, 4096, 4096]
- [10, 4096, 4096, 4096]
sm_count: -1

backends: [halops, fi, deepgemm]
cold_l2_cache: true
refcheck: true
refcheck_reference: golden
19 changes: 19 additions & 0 deletions benchmark/configs/gemm/group_masked_bf16.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
scenario: group_masked
mode: bf16

# Shapes: [num_experts, max_m, N, K].
shapes:
- [4, 128, 256, 512]
- [4, 256, 1024, 1024]
- [8, 512, 7168, 3584]
- [10, 4096, 7168, 3584]
- [4, 128, 4096, 4096]
- [8, 512, 4096, 4096]
- [8, 1024, 4096, 4096]
- [10, 4096, 4096, 4096]
sm_count: -1

backends: [halops, fi, deepgemm]
cold_l2_cache: true
refcheck: true
refcheck_reference: golden
7 changes: 6 additions & 1 deletion benchmark/core/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@


def _is_gemm_scenario(scenario):
_GEMM_SCENARIOS = ("dense_gemm", "group_masked", "group_contiguous")
_GEMM_SCENARIOS = (
"dense_gemm",
"group_masked",
"group_contiguous",
"batch_gemm",
)
return True if scenario in _GEMM_SCENARIOS else False


Expand Down
17 changes: 15 additions & 2 deletions benchmark/docs/config_schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,20 @@

普通参数:

- `mode`(默认 `w8a8-mxfp8`):支持 `w8a8-mxfp8` 和 `w4a4-nvfp4`(kernel 与 `group_wise_quant` 也支持 `w4a4-mxfp4`,但 benchmark 暂未生成对应的量化输入)。
- `mode`(默认 `w8a8-mxfp8`):支持 `w8a8-mxfp8`、`w4a4-nvfp4` 与原生 BF16 `bf16`(kernel 与 `group_wise_quant` 也支持 `w4a4-mxfp4`,但 benchmark 暂未生成对应的量化输入)。`bf16` 模式下不生成量化输入,`sfa`/`sfb` 为 `None`,且要求 `K` 为 `64` 的倍数。
- `sm_count`(默认 `-1`):`-1` 表示不限制 SM 数量。

### `batch_gemm`

遍历维度参数:

- `shapes`(必选):`[B, M, N, K]` 列表,每项生成一个 case;第 `b` 个 batch 计算 `input[b] @ weight[b]^T`。

普通参数:

- `mode`(默认 `bf16`):当前仅支持原生 BF16 `bf16`,要求 `K` 为 `64` 的倍数。
- `sm_count`(默认 `-1`)。

### `group_masked` 和 `group_contiguous`

遍历维度参数:
Expand All @@ -114,7 +125,7 @@

普通参数:

- `mode`(默认 `w8a8-mxfp8`):支持 `w8a8-mxfp8` 和 `w4a4-nvfp4`(grouped kernel 与 `group_wise_quant` 也支持 `w4a4-mxfp4`,但 benchmark 暂未生成对应的量化输入)。
- `mode`(默认 `w8a8-mxfp8`):支持 `w8a8-mxfp8`、`w4a4-nvfp4` 与原生 BF16 `bf16`(grouped kernel 与 `group_wise_quant` 也支持 `w4a4-mxfp4`,但 benchmark 暂未生成对应的量化输入)。`bf16` 模式要求 `K` 为 `64` 的倍数。
- `sm_count`(默认 `-1`)。

所有现有 GEMM 场景均要求 `K` 为 `32` 的倍数。
Expand All @@ -135,3 +146,5 @@
- `backends`:`halops`。

固定形状为 `hidden_size=4096`、`intermediate_size=512`、`latent_size=2048`、`num_experts=512`,配置其他形状会报错。

原生 BF16 模式要求 `K` 为 `64` 的倍数。
39 changes: 39 additions & 0 deletions benchmark/kernel/gemm/backends/deepgemm.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ def make_dense_inputs(x_bf16: torch.Tensor, w_bf16: torch.Tensor, mode: str) ->
n = w_bf16.shape[0]
out = torch.empty(m, n, dtype=torch.bfloat16, device=x_bf16.device)

if mode == "bf16":
return {
"a": x_bf16,
"b": w_bf16,
"out": out,
"mode": mode,
}

cast = (
dg.per_token_cast_to_fp4
if mode.startswith("w4a4")
Expand Down Expand Up @@ -112,6 +120,17 @@ def make_grouped_masked_inputs(
a_bf16 = x_bf16.reshape(num_groups, max_m, k)
masked_m = masked_m.to(device=dev, dtype=torch.int32)

if mode == "bf16":
out = torch.empty(num_groups, max_m, n, dtype=torch.bfloat16, device=dev)
return {
"a": a_bf16,
"b": w_bf16,
"out": out,
"masked_m": masked_m,
"max_m": max_m,
"mode": mode,
}

if mode.startswith("w4a4"):
# A: FP4 per-token (gran_k=32), pre-pack SF to UE8M0 INT
a_fp4 = torch.empty(num_groups, max_m, k // 2, dtype=torch.int8, device=dev)
Expand Down Expand Up @@ -194,6 +213,16 @@ def make_grouped_contiguous_inputs(
dev = x_bf16.device
m_indices = m_indices.to(device=dev, dtype=torch.int32)

if mode == "bf16":
out = torch.empty(m, n, dtype=torch.bfloat16, device=dev)
return {
"a": x_bf16,
"b": w_bf16,
"out": out,
"m_indices": m_indices,
"mode": mode,
}

if mode.startswith("w4a4"):
a_fp4, a_sf = dg.per_token_cast_to_fp4(x_bf16, use_ue8m0=True, gran_k=32)
a_sf_packed = dg.get_mn_major_tma_aligned_packed_ue8m0_tensor(a_sf)
Expand Down Expand Up @@ -247,6 +276,8 @@ def run_dense_gemm(inp: dict):
dg.fp8_fp4_gemm_nt(
inp["a"], inp["b"], inp["out"], recipe_a=(1, 32), recipe_b=(1, 32)
)
elif inp.get("mode") == "bf16":
dg.bf16_gemm_nt(inp["a"], inp["b"], inp["out"])
else:
dg.fp8_gemm_nt(
inp["a"], inp["b"], inp["out"], recipe_a=(1, 32), recipe_b=(1, 32)
Expand All @@ -269,6 +300,10 @@ def run_group_masked_gemm(inp: dict):
recipe_a=(1, 32),
recipe_b=(1, 32),
)
elif inp.get("mode") == "bf16":
dg.m_grouped_bf16_gemm_nt_masked(
inp["a"], inp["b"], inp["out"], inp["masked_m"], inp["max_m"]
)
else:
dg.m_grouped_fp8_gemm_nt_masked(
inp["a"], inp["b"], inp["out"], inp["masked_m"], inp["max_m"]
Expand All @@ -290,6 +325,10 @@ def run_group_contiguous_gemm(inp: dict):
recipe_a=(1, 32),
recipe_b=(1, 32),
)
elif inp.get("mode") == "bf16":
dg.m_grouped_bf16_gemm_nt_contiguous(
inp["a"], inp["b"], inp["out"], inp["m_indices"]
)
else:
dg.m_grouped_fp8_gemm_nt_contiguous(
inp["a"], inp["b"], inp["out"], inp["m_indices"]
Expand Down
27 changes: 27 additions & 0 deletions benchmark/kernel/gemm/backends/flashinfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ def run_fi_dense_gemm(inp: dict):
# scales (M_padded * K_padded,).
mode = inp.get("mode", "w8a8-mxfp8")
backend = _fi_backend(inp)
if mode == "bf16":
return flashinfer.mm_bf16(
inp["x"],
inp["w"].T,
out=inp["out"],
backend=backend,
)
if mode == "w4a4-mxfp4":
raise ValueError("flashinfer backend does not support mode 'w4a4-mxfp4' yet")
if mode == "w4a4-nvfp4":
Expand Down Expand Up @@ -75,6 +82,19 @@ def run_fi_dense_gemm(inp: dict):
)


def run_fi_batch_gemm(inp: dict):
"""Run FlashInfer batched BF16 GEMM."""
_check()
import flashinfer

return flashinfer.bmm_bf16(
inp["x"],
inp["bt"],
out=inp["out"],
backend=inp.get("backend", "cudnn"),
)


def run_fi_group_masked_gemm(inp: dict):
"""Run FlashInfer grouped masked GEMM (W8A8-MXFP8 via grouped_mm_mxfp8, W4A4 via grouped_mm_fp4)."""
_check()
Expand All @@ -83,6 +103,13 @@ def run_fi_group_masked_gemm(inp: dict):
mode = inp.get("mode", "w8a8-mxfp8")
m_indptr = inp["m_indptr"]

if mode == "bf16":
return flashinfer.grouped_mm_bf16(
inp["x"],
inp["w"],
m_indptr,
out=inp["out"],
)
if mode == "w4a4-mxfp4":
raise ValueError("flashinfer backend does not support mode 'w4a4-mxfp4' yet")
if mode == "w4a4-nvfp4":
Expand Down
30 changes: 21 additions & 9 deletions benchmark/kernel/gemm/backends/golden.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,16 @@ def run_dense_golden(inp: dict) -> torch.Tensor:
the golden is a neutral baseline that excludes quantization noise and shares
the kernels' compute and output dtype.

inp keys: x_bf16 [M, K], w_bf16 [N, K], mode ("w8a8-mxfp8" | "w4a4-nvfp4").
inp keys: x_bf16 [M, K], w_bf16 [N, K], mode ("w8a8-mxfp8" | "w4a4-nvfp4" |
"bf16").
"""
mode = inp.get("mode", "w8a8-mxfp8")
x = _dequant_to_bf16(inp["x_bf16"], mode)
w = _dequant_to_bf16(inp["w_bf16"], mode)
if mode == "bf16":
x = inp["x_bf16"]
w = inp["w_bf16"]
else:
x = _dequant_to_bf16(inp["x_bf16"], mode)
w = _dequant_to_bf16(inp["w_bf16"], mode)
return torch.matmul(x, w.t())


Expand All @@ -110,18 +115,25 @@ def run_grouped_golden(inp: dict) -> torch.Tensor:
x_bf16 [M, K] unquantized activation source (bfloat16)
w_bf16 [E, N, K] unquantized per-expert weight source (bfloat16)
m_indptr [E+1] per-group row offsets (prefix sum)
mode str "w8a8-mxfp8" (fp8) or "w4a4-nvfp4" (fp4)
mode str "w8a8-mxfp8" (fp8), "w4a4-nvfp4" (fp4), or
"bf16"

Returns:
torch.Tensor [M, N] bfloat16 reference output.
"""
mode = inp.get("mode", "w8a8-mxfp8")
x = _dequant_to_bf16(inp["x_bf16"], mode) # [M, K]
w_src = inp["w_bf16"] # [E, N, K]
if mode == "bf16":
x = inp["x_bf16"]
w_src = inp["w_bf16"]
w = w_src
else:
x = _dequant_to_bf16(inp["x_bf16"], mode)
w_src = inp["w_bf16"]
num_groups, n, k = w_src.shape
w = _dequant_to_bf16(w_src.reshape(num_groups * n, k), mode).reshape(
num_groups, n, k
)
num_groups, n, k = w_src.shape
w = _dequant_to_bf16(w_src.reshape(num_groups * n, k), mode).reshape(
num_groups, n, k
)
# Materialize offsets on CPU once to avoid per-group GPU->CPU syncs.
offsets = inp["m_indptr"].tolist()
out = torch.empty(x.shape[0], n, dtype=torch.bfloat16, device=x.device)
Expand Down
Loading
Loading