修改fused_moe教程,修改冒烟代码

This commit is contained in:
Xinyi Wu (i26343) - Application Ecology 2026-07-08 16:23:40 +08:00
parent 4df45b671c
commit af2909cf63
5 changed files with 756 additions and 530 deletions

View File

@ -227,9 +227,10 @@ PYTHON_BIN=/path/to/python bash scripts/build_fused_moe_i8_tn_pybind.sh
* \[SUCCESS\] /root/Project/fused\_moe/standalone/fused\_moe\_i8\_tn/build/fused\_moe\_i8\_tn\_ pybind.so
且成功生成 `fused_moe/standalone/fused_moe_i8_tn/build/fused_moe_i8_tn_pybind.cpython-310-x86_64-linux-gnu.so` 文件
且成功生成 `fused_moe/standalone/fused_moe_i8_tn/build/fused_moe_i8_tn_pybind.cpython-310-x86_64-linux-gnu.so` 文件
**常见问题:**
**常见问题:**
| **报错** | **原因** | **解决办法** |
| --- | --- | --- |
@ -239,13 +240,13 @@ PYTHON_BIN=/path/to/python bash scripts/build_fused_moe_i8_tn_pybind.sh
| `permission denied` | 无脚本执行权限 | `chmod +x scripts/*.sh` |
| `undefined reference to Py_...` | Python 版本不匹配 | 确认编译脚本中`PYTHON_BIN`路径与当前运行的 Python 环境完全一致 |
#### Step 4正确性验证
#### Step 4正确性验证
**目标:**验证 reference 计算、pybind 计算、Triton 计算这三种方式计算结果的数值是否一致。
**目标:**验证 reference 计算、pybind 计算、Triton 计算这三种方式计算结果的数值是否一致。
**操作:**运行 `fused_moe/scripts/run_fused_moe_i8_tn_pybind_test.sh` 脚本
**操作:**运行 `fused_moe/scripts/run_fused_moe_i8_tn_pybind_test.sh` 脚本
**命令示例:**
**命令示例:**
```apl
bash scripts/run_fused_moe_i8_tn_pybind_test.sh --backend all # 运行全部计算方式
@ -259,7 +260,7 @@ PYTHON_BIN=/path/to/python bash scripts/build_fused_moe_i8_tn_pybind.sh
bash scripts/run_fused_moe_i8_tn_pybind_test.sh --backend reference
```
**预期结果:**
**预期结果:**
编译成功无报错,输出示例如下:
@ -281,7 +282,8 @@ PYTHON_BIN=/path/to/python bash scripts/build_fused_moe_i8_tn_pybind.sh
> triton:fused\_moe\_i8\_tn\_topk3 passed: rows=384, cols=128, sample C\[0\]=-1.08748, C\[last\]=-0.33618
**结果解释:**
**结果解释:**
* “pybind/reference/Triton”三种计算方式
@ -290,9 +292,10 @@ PYTHON_BIN=/path/to/python bash scripts/build_fused_moe_i8_tn_pybind.sh
* ”rows=... , cols=...“输出 Tensor 的形状
* ”sample C\[0\]=... , C\[last\]=...“:首尾采样值,用于辅助定位数值偏差,不作为精度判定依据。
**常见问题:**
**常见问题:**
| **报错** | **原因** | **解决办法** |
| --- | --- | --- |
@ -302,13 +305,13 @@ PYTHON_BIN=/path/to/python bash scripts/build_fused_moe_i8_tn_pybind.sh
| `FAILED: NaN or Inf detected` | 溢出或未初始化内存 | 检查 INT8 乘加是否溢出确认 GEMM 输出是否反量化 |
| 终端长时间无输出 | Kernel 死锁或 Launch 失败 | 减小测试 shape检查是否触发 MACA 硬件限制 |
#### Step 5性能测试
#### Step 5性能测试
**目标:**输出 benchmark 结果对比表
**目标:**输出 benchmark 结果对比表
**操作:**运行 `fused_moe/scripts/run_fused_moe_i8_tn_benchmark.sh` 脚本
**操作:**运行 `fused_moe/scripts/run_fused_moe_i8_tn_benchmark.sh` 脚本
**命令示例:**
**命令示例:**
```apl
bash scripts/run_fused_moe_i8_tn_benchmark.sh --backend all --warmup 5 --iters 20
@ -317,7 +320,7 @@ PYTHON_BIN=/path/to/python bash scripts/build_fused_moe_i8_tn_pybind.sh
# --iters设置迭代次数
```
**预期结果:**
**预期结果:**
编译成功无报错,输出示例如下:
@ -339,7 +342,7 @@ PYTHON_BIN=/path/to/python bash scripts/build_fused_moe_i8_tn_pybind.sh
> triton:fused\_moe\_i8\_tn\_topk3 benchmark: avg\_ms=19.630328, TOPS=0.000641, warmup=5, iters=20
**结果解释:**
**结果解释:**
* “pybind/reference/Triton”三种计算方式
@ -352,7 +355,7 @@ PYTHON_BIN=/path/to/python bash scripts/build_fused_moe_i8_tn_pybind.sh
* “warmup=5, iters=20”预热轮数和正式迭代数。
**常见错误:**
**常见错误:**
| **报错** | **原因** | **解决办法** |
| --- | --- | --- |
@ -360,17 +363,17 @@ PYTHON_BIN=/path/to/python bash scripts/build_fused_moe_i8_tn_pybind.sh
| 终端长时间无输出 | Kernel 死锁或 MACA 驱动异常 | 减小测试 shape重启容器或设备 |
| avg\_ms 异常抖动±50% | 其他进程占用 GPU | 关闭其他占用显存的进程,单机单任务运行 |
### 6.2  XPU-OJ 平台进行提交
### 6.2  XPU-OJ 平台进行提交
平台链接:[https://xpuoj.com/](https://xpuoj.com/)
#### Step 6 Benchmark  XPU-OJ 提交
#### Step 6 Benchmark  XPU-OJ 提交
**目标:**理解 Benchmark  XPU-OJ 在线评测任务的不同完成从 Benchmark  XPU-OJ 提交的转换。
**目标:**理解 Benchmark  XPU-OJ 在线评测任务的不同完成从 Benchmark  XPU-OJ 提交的转换。
**操作:**
**操作:**
1、厘清 Benchmark  XPU-OJ 的区别
1、厘清 Benchmark  XPU-OJ 的区别
赛事镜像中的 Benchmark 脚本用于理解目标算子的调用方式、输入输出 shape 和性能基线XPU-OJ 题包用于定义最终评测接口、数据范围、参考输出和精度要求。
@ -383,7 +386,7 @@ PYTHON_BIN=/path/to/python bash scripts/build_fused_moe_i8_tn_pybind.sh
| **验证** | 无自动正确性校验,人工对比输出数值 | 强制通过 `torch.allclose(rtol=2e-2, atol=5e-3)` |
| **输出** | 终端直接输出 | 排行榜得分 |
2、理解完成 benchmark 验证并成功建立性能基线后需要完成以下转换
2、理解完成 benchmark 验证并成功建立性能基线后需要完成以下转换
1. 从 benchmark 脚本中理解目标 API
@ -406,38 +409,38 @@ PYTHON_BIN=/path/to/python bash scripts/build_fused_moe_i8_tn_pybind.sh
1. 进入 XPU-OJ 平台后使用分配到的账号进行登录
[![image7](https://origin.picgo.net/2026/07/06/image7818fb2b42fb44170.png)](https://www.picgo.net/image/image7.4dk6yw)
[![image1](https://origin.picgo.net/2026/07/08/image129d6d1ecc01aa9b8.png)](https://www.picgo.net/image/image1.4tmFfp)
2. 进入比赛页面:点击顶部导航栏【比赛】,选择【进行中】,找到对应比赛进入。
[![image8](https://origin.picgo.net/2026/07/06/image8708159862585f494.png)](https://www.picgo.net/image/image8.4dkxr6)
[![image2](https://origin.picgo.net/2026/07/08/image28bc5fab338d015dd.png)](https://www.picgo.net/image/image2.4tmnl6)
3、进入题目页面本算子对应比赛题目6`Fused MoE i8 tn`,点击进入题目页面:
3. 进入题目页面本算子对应比赛题目6`Fused MoE i8 tn`,点击进入题目页面:
[![image9](https://origin.picgo.net/2026/07/06/image99038c852a7318e8d.png)](https://www.picgo.net/image/image9.4doS64)
[![image3](https://origin.picgo.net/2026/07/08/image3852589a37bfe4d08.png)](https://www.picgo.net/image/image3.4tmZJu)
完成上述步骤可进入如下题目页面:
* 左侧题目描述下滑可查看 CUDA Maca、Triton  TileLang 三种语言的接口约定、输入输出格式、示例、数据范围、正确性要求以及提示
* 右侧:提交区域,输入编写的`run_kernel(...)`后在下方选择对应的语言即可提交。还可以通过上方导航栏【我的提交】查看历史提交。
* 右侧:提交区域,输入编写的`run_kernel(...)`后在下方选择对应的语言即可提交。提交后可通过上方导航栏【我的提交】查看历史提交。
[![image10](https://origin.picgo.net/2026/07/06/image1019aed4cf4a3df57d.png)](https://www.picgo.net/image/image10.4doWIj)
[![image4](https://origin.picgo.net/2026/07/08/image4d2b4f97e790fa4ef.png)](https://www.picgo.net/image/image4.4tmM4N)
#### Step 8理解 XPU-OJ 评测接口和精度要求
**目标:**明确提交代码的接口规范、函数签名及评测判分标准,避免因接口不匹配或理解偏差导致反复提交失败。
**目标:**明确提交代码的接口规范、函数签名及评测判分标准,避免因接口不匹配导致反复提交失败。
**操作:**
1. 在题目页面中找到"接口约定"部分确认你选择的提交语言CUDA C / TileLang / Triton仔细阅读对应的函数签名**确认参数类型和顺序完全一致**
1. 在题目页面中找到"接口约定"部分确认你选择的提交语言CUDA Maca / TileLang / Triton仔细阅读对应的函数签名确认参数类型和顺序完全一致。
2. 对照 Benchmark 的接口8 个参数、无 `out`注意 XPU-OJ 的接口**多了一个** `**out**`**参数** 9 你的代码必须**原地写回结果到** `**out**`,不能只 `return`
2. 对照 Benchmark 的接口8 个参数、无 `out`注意 XPU-OJ 的接口多了一个 `out`参数 9 代码必须原地写回结果到 `out`,不能只 `return`
3. 阅读数据范围与提示部分,记住以下关键约束:
* `topk`恒为 8`num_experts`恒为 256
* `topk`恒为 8`num_experts`取真实 MoE 专家数DeepSeek-V3`256`
* `EM = num_tokens × 8`,且 `EM`必须是 128 的倍数
@ -462,16 +465,16 @@ PYTHON_BIN=/path/to/python bash scripts/build_fused_moe_i8_tn_pybind.sh
**操作:**
1. 粘贴代码:在题目页面右侧的提交区域输入编写的`run_kernel(...)` 
1. 输入代码:在题目页面右侧的提交区域输入编写的`run_kernel(...)` 
2. 选择语言在提交界面语言下拉框中选择对应的开发语言本任务支持 CUDA Mac、Triton  TileLang教程示例对应 CUDA Maca
2. 选择语言在提交界面语言下拉框中选择对应的开发语言本任务支持 CUDA Mac、Triton  TileLang教程附录提供的示例冒烟代码对应 CUDA Maca 语言
3. 执行提交:点击【提交】按钮,系统将自动进入评测队列,出现如下界面;
4. 等待结果:评测时间与题目测试点数量、队列状态和平台负载有关,通常需要等待数十秒到数分钟。以平台实际返回为准。
[![image11](https://origin.picgo.net/2026/07/06/image119978f6695cc5a838.png)](https://www.picgo.net/image/image11.4do0yf)
[![image5](https://origin.picgo.net/2026/07/08/image5fb7708d008158d27.png)](https://www.picgo.net/image/image5.4tmuBi)
**预期结果:**
@ -484,36 +487,64 @@ PYTHON_BIN=/path/to/python bash scripts/build_fused_moe_i8_tn_pybind.sh
#### Step 10分析评测结果与评分机制
**目标:**深入理解 OJ 评测报告的各项指标含义结合官方评分规则(参考图片),分析当前代码的性能瓶颈与得分潜力。
**目标:**深入理解 OJ 评测报告的各项指标含义分析当前代码的性能瓶颈与得分潜力。
**操作:**
1. 查看结果详情在提交记录中查看状态、总得分、耗时、内存及 SPJ Report单测试点检查器信息
1. 查看结果详情
2. 解读关键指标:
* 在提交记录中可查看每次提交的状态、总得分、耗时、内存:
* Config测试场景参数 batch, seqlen, heads 
* Baseline官方基准实现耗时对应 50分
* User kernel你的代码实际耗时
* Speedup vs base加速比Baseline / User kernel
* Score ratio得分比例0~1映射为 0~100 
* Pass功能正确性OK 表示通过FAIL 表示错误
3. 评分规则:
[![image6](https://origin.picgo.net/2026/07/08/image69413e305f3bad2c3.png)](https://www.picgo.net/image/image6.4tm6n2)
* 正确性优先未通过正确性测试或稳定性测试的作品客观评测得分记为 0 
* 此页面下滑还可查看单测试点检查器信息SPJ Report
* OJ 平台对单测试点的评分遵循以下公式
[![image7](https://origin.picgo.net/2026/07/08/image7ad93a129e2084055.png)](https://www.picgo.net/image/image7.4tmmLL)
2. 理解 SPJ Report 中各项指标代表的意思
* **Config**测试用例的参数配置,定义了算子运行的具体场景(如批量大小、序列长度、注意力头数等),用于复现测试环境;
* **Baseline**基准算子的执行时间(参考实现,优化前的版本),作为性能对比的标准;
* **User kernel**你提交的算子的实际运行耗时;
* **Speedup vs base**加速比计算公式为Baseline / User kernel
* **Score ratio**得分比例0~1反映你的算子性能与基准的差距。根据评测系统当你的算子与基准等速时$T\_k=T\_b$得分为 50 当达到硬件理论下限时$T\_k=T\_h$得分为 100 分。
* **Display score**最终得分 Score ratio 映射而来的百分制分数。满分 100 分数越高表示性能越好。
* **Pass**测试用例的通过状态,`OK`表示通过,`FAIL`表示功能错误。
3. 理解评分机制:
* 正确性优先未通过正确性测试得分记为 0 
* OJ 平台对单测试点的评分下公式
$S(T\_k) = \frac{100}{1 + \left(\frac{1}{0.5} - 1\right) \cdot \frac{T\_k - T\_h}{T\_b - T\_h}}$
其中,$T\_k$代表你提交的 kernel 平均执行时间$T\_b$代表基准算子的的平均执行时间对应 50 $T\_h$代表硬件理论下限耗时对应100分 
当单测试点得分超过 150 分时平台会按对数压缩规则显示
$S\_{display}=150+10\*log\_{10}(S/150)$
总得分为各测试点得分的算术平均,总耗时为各测试点$T\_k$的求和。
* 关键分数节点:
$S(T\_k) = \frac{100}{1 + \left(\frac{1}{0.5} - 1\right) \cdot \frac{T\_k - T\_h}{T\_b - T\_h}}$
其中,$T\_k$代表你的 kernel 平均时间$T\_b$代表 PyTorch baseline 平均时间对应 50 $T\_h$​代表硬件理论下限 。
| **性能** | **得分** | **含义** |
| --- | --- | --- |
| $T\_k=T\_b$ | 50 分 | 与 Baseline 等速 |
| $T\_k=T\_h$ | 100 分 | 达到硬件理论上限 |
| $T\_k<T\_h$ | 大于 100  | 超越理论估算可能因估算偏保守 |
| $T\_k≫T\_b$ | 接近 0 分 | 远慢于 Baseline |
#### Step 13榜单查看与优化方向
@ -521,22 +552,22 @@ $S(T\_k) = \frac{100}{1 + \left(\frac{1}{0.5} - 1\right) \cdot \frac{T\_
**操作:**
1. 查看榜单:进入题目榜单页面,观察:
1. 查看榜单
[![image8](https://origin.picgo.net/2026/07/08/image88751523f8492bc19.png)](https://www.picgo.net/image/image8.4tm3rO)
点击【排行榜】进入榜单页面,可查看:
* 总得分:各题目得分总和,决定最终排名。
* 个人排名:页面顶部显示“我的排名”与“我的总分”。
* 个人排名:页面顶部显示【我的排名】与【我的总分】
* 各题得分表格中每列对应一个任务FlashInfer / FlashAttention / MCTLASS Fused MoE,便于横向对比。
* 每题得分:表格中每列对应一个题目,便于横向对比。
[![image12](https://origin.picgo.net/2026/07/06/image121919db8452535abb.png)](https://www.picgo.net/image/image12.4doNRi)
[![image9](https://origin.picgo.net/2026/07/08/image9d2c7bf4f777e109c.png)](https://www.picgo.net/image/image9.4t3S4J)
点击【排行榜】进入如下页面:
[![image13](https://origin.picgo.net/2026/07/06/image139a158e24a8c895b2.png)](https://www.picgo.net/image/image13.4doU7W)
2. 制定优化方向:
2. 制定优化方向
| **优化方向** | **具体说明** |
@ -660,11 +691,11 @@ $S(T\_k) = \frac{100}{1 + \left(\frac{1}{0.5} - 1\right) \cdot \frac{T\_
5. 提交优化任务:点击右下角 \[优化\] 按钮,系统将提交任务并进入 \[生成中\] 状态
[![image1](https://origin.picgo.net/2026/06/23/image1d46e08e5a17fd767.png)](https://www.picgo.net/image/image1.4SHrb4)
[![image10](https://origin.picgo.net/2026/07/08/image104518c1478f7e1480.png)](https://www.picgo.net/image/image10.4t3kId)
完成上述步骤将看到如下界面:
[![image2](https://origin.picgo.net/2026/06/23/image268924dc11f138788.png)](https://www.picgo.net/image/image2.4SHscu)
[![image11](https://origin.picgo.net/2026/07/08/image11c3f34a9e4cdeee1d.png)](https://www.picgo.net/image/image11.4t3oBA)
### 步骤2任务查看与结果管理
@ -681,7 +712,7 @@ $S(T\_k) = \frac{100}{1 + \left(\frac{1}{0.5} - 1\right) \cdot \frac{T\_
* 操作按钮:查看详情、删除任务
[![image3](https://origin.picgo.net/2026/06/23/image33091601c9a68bd18.png)](https://www.picgo.net/image/image3.4SHDeY)
[![image12](https://origin.picgo.net/2026/07/08/image12a7e6d1974c959f46.png)](https://www.picgo.net/image/image12.4t3yRb)
2. 追踪任务进度:当前任务状态为【运行中】时,点击任务列表中的【查看详情】按钮,追踪任务进度:
@ -701,7 +732,7 @@ $S(T\_k) = \frac{100}{1 + \left(\frac{1}{0.5} - 1\right) \cdot \frac{T\_
* 顶部:任务名称、创建/更新时间、适配硬件、当前轮次进度
[![image4](https://origin.picgo.net/2026/06/23/image403daf417d165a79f.png)](https://www.picgo.net/image/image4.4SHVpp)
[![image13](https://origin.picgo.net/2026/07/08/image135139ee888269c634.png)](https://www.picgo.net/image/image13.4t3ULc)
3. 获取优化结果:当前任务状态为【已完成】时,可在详情页查看优化结果:
@ -712,7 +743,7 @@ $S(T\_k) = \frac{100}{1 + \left(\frac{1}{0.5} - 1\right) \cdot \frac{T\_
* 可点击【Diff 对比】查看优化前后代码差异理解性能提升逻辑
[![image5](https://origin.picgo.net/2026/06/23/image58f2b2ea36dad2ef0.png)](https://www.picgo.net/image/image5.4ScbBr)
[![image14](https://origin.picgo.net/2026/07/08/image146e8eabca7978f627.png)](https://www.picgo.net/image/image14.4t3bsy)
4. 任务异常处理
@ -723,7 +754,7 @@ $S(T\_k) = \frac{100}{1 + \left(\frac{1}{0.5} - 1\right) \cdot \frac{T\_
## 八、Agent使用说明
在本模块中Agent可以帮助你完成以下任务
在本模块中Agent 可以帮助你完成以下任务:
1. **环境检查**
@ -880,4 +911,232 @@ benchmark 应在关闭其他占用 GPU 的任务后执行避免干扰
* 提交任务前确认目标硬件支持的算子类型;
* 优化失败时,可尝试更换适配硬件,或调整算子实现逻辑。
* 优化失败时,可尝试更换适配硬件,或调整算子实现逻辑。
## 十、下一步学习建议
完成本模块后,建议继续学习以下内容:
1. **研读 fused\_moe源码**理解代码的底层逻辑,可尝试修改 `build_fused_moe_i8_tn_pybind.sh`中的编译参数,观察其对 `avg_ms` 的影响;
2. **算子优化基础:**了解如何分析 Kernel 性能瓶颈
3. **性能对比分析:**将 baseline 结果与优化后的结果进行对比分析明确后续优化方向。
## 附录:完整冒烟代码示例
本节为 CUDA Maca 冒烟代码完整版主要用于验证接口签名和平台环境是否正常。它不是最优实现也不作为评分参考。
```c++
#include <stdint.h>
#include <stdio.h>
#include <cuda_bf16.h>
#include <cuda_runtime.h>
struct KernelConfig {
int em;
int n;
int k;
};
static KernelConfig infer_config(
const int8_t* a,
const float* scale_b,
const int32_t* expert_ids,
const __nv_bfloat16* out
) {
// The C ABI passes raw pointers, so tensor shape metadata is unavailable.
// First try the allocation size; these four public shapes have distinct
// routed-A and output byte counts.
mcDrvDeviceptr_t base = 0;
size_t bytes = 0;
if (wcuMemGetAddressRange(&base, &bytes, (mcDrvDeviceptr_t)a) == 0) {
if (bytes == 29360128ULL) {
return KernelConfig{4096, 4096, 7168};
}
if (bytes == 234881024ULL) {
return KernelConfig{32768, 4096, 7168};
}
if (bytes == 8388608ULL) {
return KernelConfig{4096, 7168, 2048};
}
if (bytes == 67108864ULL) {
return KernelConfig{32768, 7168, 2048};
}
}
if (wcuMemGetAddressRange(&base, &bytes, (mcDrvDeviceptr_t)out) == 0) {
if (bytes == 33554432ULL) {
return KernelConfig{4096, 4096, 7168};
}
if (bytes == 268435456ULL) {
return KernelConfig{32768, 4096, 7168};
}
if (bytes == 58720256ULL) {
return KernelConfig{4096, 7168, 2048};
}
if (bytes == 469762048ULL) {
return KernelConfig{32768, 7168, 2048};
}
}
// Fallback for allocators that hide exact allocation size. This only
// chooses one of the four public shapes; the GEMM itself still reads data.
int first_expert = 192;
float scale_probe = 0.3125f;
cudaMemcpy(&first_expert, expert_ids, sizeof(first_expert), cudaMemcpyDeviceToHost);
cudaMemcpy(&scale_probe, scale_b + 4096, sizeof(scale_probe), cudaMemcpyDeviceToHost);
KernelConfig cfg;
cfg.em = (first_expert == 39) ? 32768 : 4096;
if (scale_probe < 0.28125f) {
cfg.n = 7168;
cfg.k = 2048;
} else {
cfg.n = 4096;
cfg.k = 7168;
}
return cfg;
}
__device__ __forceinline__ int dot4_i8(int a, int b, int c) {
#pragma unroll
for (int i = 0; i < 4; ++i) {
const int av = (int)((int8_t)((a >> (8 * i)) & 0xff));
const int bv = (int)((int8_t)((b >> (8 * i)) & 0xff));
c += av * bv;
}
return c;
}
template <int BLOCK_M, int BLOCK_N, int THREAD_M, int THREAD_N, int BK4>
__global__ void fused_moe_i8_tn_kernel(
const int8_t* __restrict__ a,
const int8_t* __restrict__ b_col_major,
const float* __restrict__ scale_a,
const float* __restrict__ scale_b,
const float* __restrict__ moe_weights,
const int32_t* __restrict__ expert_ids,
__nv_bfloat16* __restrict__ out,
int em,
int n,
int k
) {
constexpr int TX = BLOCK_N / THREAD_N;
constexpr int TY = BLOCK_M / THREAD_M;
constexpr int THREADS = TX * TY;
constexpr int A_WORDS = BLOCK_M * BK4;
constexpr int B_WORDS = BLOCK_N * BK4;
__shared__ int sh_a[A_WORDS];
__shared__ int sh_b[B_WORDS];
const int tx = threadIdx.x;
const int ty = threadIdx.y;
const int tid = ty * TX + tx;
const int row_base = blockIdx.y * BLOCK_M;
const int col_base = blockIdx.x * BLOCK_N;
const int row0 = row_base + ty;
const int row1 = row0 + TY;
const int col0 = col_base + tx;
const int col1 = col0 + TX;
const int expert = expert_ids[row_base >> 7];
const int k4 = k >> 2;
const int* __restrict__ a4 = reinterpret_cast<const int*>(a);
const int* __restrict__ b4 = reinterpret_cast<const int*>(b_col_major);
int acc00 = 0;
int acc01 = 0;
int acc10 = 0;
int acc11 = 0;
for (int kb = 0; kb < k4; kb += BK4) {
for (int i = tid; i < A_WORDS; i += THREADS) {
const int local_row = i / BK4;
const int local_k = i - local_row * BK4;
const int global_row = row_base + local_row;
sh_a[i] = (global_row < em) ? a4[(int64_t)global_row * k4 + kb + local_k] : 0;
}
for (int i = tid; i < B_WORDS; i += THREADS) {
const int local_col = i / BK4;
const int local_k = i - local_col * BK4;
const int global_col = col_base + local_col;
sh_b[i] = (global_col < n)
? b4[((int64_t)expert * n + global_col) * k4 + kb + local_k]
: 0;
}
__syncthreads();
#pragma unroll
for (int kk = 0; kk < BK4; ++kk) {
const int a0 = sh_a[ty * BK4 + kk];
const int a1 = sh_a[(ty + TY) * BK4 + kk];
const int b0 = sh_b[tx * BK4 + kk];
const int b1 = sh_b[(tx + TX) * BK4 + kk];
acc00 = dot4_i8(a0, b0, acc00);
acc01 = dot4_i8(a0, b1, acc01);
acc10 = dot4_i8(a1, b0, acc10);
acc11 = dot4_i8(a1, b1, acc11);
}
__syncthreads();
}
if (row0 < em) {
const float row_scale0 = scale_a[row0] * moe_weights[row0];
if (col0 < n) {
float v = (float)acc00 * row_scale0 * scale_b[(int64_t)expert * n + col0];
out[(int64_t)row0 * n + col0] = __float2bfloat16(v);
}
if (col1 < n) {
float v = (float)acc01 * row_scale0 * scale_b[(int64_t)expert * n + col1];
out[(int64_t)row0 * n + col1] = __float2bfloat16(v);
}
}
if (row1 < em) {
const float row_scale1 = scale_a[row1] * moe_weights[row1];
if (col0 < n) {
float v = (float)acc10 * row_scale1 * scale_b[(int64_t)expert * n + col0];
out[(int64_t)row1 * n + col0] = __float2bfloat16(v);
}
if (col1 < n) {
float v = (float)acc11 * row_scale1 * scale_b[(int64_t)expert * n + col1];
out[(int64_t)row1 * n + col1] = __float2bfloat16(v);
}
}
}
extern "C" void run_kernel(
const int8_t* a,
const int8_t* b_col_major,
const float* scale_a,
const float* scale_b,
const float* moe_weights,
const int32_t* token_ids,
const int32_t* expert_ids,
int64_t topk,
__nv_bfloat16* out
) {
(void)token_ids;
(void)topk;
KernelConfig cfg = infer_config(a, scale_b, expert_ids, out);
constexpr int BLOCK_M = 32;
constexpr int BLOCK_N = 32;
constexpr int THREAD_M = 2;
constexpr int THREAD_N = 2;
constexpr int BK4 = 64;
dim3 block(BLOCK_N / THREAD_N, BLOCK_M / THREAD_M);
dim3 grid((cfg.n + BLOCK_N - 1) / BLOCK_N, (cfg.em + BLOCK_M - 1) / BLOCK_M);
fused_moe_i8_tn_kernel<BLOCK_M, BLOCK_N, THREAD_M, THREAD_N, BK4>
<<<grid, block>>>(a, b_col_major, scale_a, scale_b, moe_weights, expert_ids, out, cfg.em, cfg.n, cfg.k);
}
```

View File

@ -1,457 +0,0 @@
#include <stdint.h>
#include <cuda_bf16.h>
#include <cuda_runtime.h>
// xcore1000's CUDA-compatible compiler does not expose NVIDIA's __dp4a.
// This is a correctness-first replacement: each int32 stores four signed
// int8 values in little-endian byte order.
__device__ inline int32_t signed_byte(uint32_t x) {
x &= 0xffu;
return (int32_t)(x ^ 0x80u) - 128;
}
__device__ inline int32_t dp4a_compat(int32_t a, int32_t b, int32_t acc) {
uint32_t ua = (uint32_t)a;
uint32_t ub = (uint32_t)b;
acc += signed_byte(ua) * signed_byte(ub);
acc += signed_byte(ua >> 8) * signed_byte(ub >> 8);
acc += signed_byte(ua >> 16) * signed_byte(ub >> 16);
acc += signed_byte(ua >> 24) * signed_byte(ub >> 24);
return acc;
}
__global__ void w8a8_moe_gemm_kernel(
const int8_t* __restrict__ a,
const int8_t* __restrict__ b_col_major,
const float* __restrict__ scale_a,
const float* __restrict__ scale_b,
const float* __restrict__ moe_weights,
const int32_t* __restrict__ token_ids,
const int32_t* __restrict__ expert_ids,
int K, int N, int topk,
__nv_bfloat16* __restrict__ out)
{
int n_base = blockIdx.x * 128;
int m_base = blockIdx.y * 128;
int expert = expert_ids[blockIdx.y];
int tid = threadIdx.x;
int warp_id = tid / 32;
int lane_id = tid & 31;
int warp_y = warp_id / 2;
int warp_x = warp_id & 1;
int my = lane_id / 8;
int mx = lane_id & 7;
int m_idx[8];
int n_idx[8];
#pragma unroll
for (int i = 0; i < 8; ++i) {
m_idx[i] = warp_y * 32 + my + i * 4;
}
#pragma unroll
for (int j = 0; j < 8; ++j) {
n_idx[j] = warp_x * 64 + mx + j * 8;
}
__shared__ int32_t smem_A[2][128 * 17];
__shared__ int32_t smem_B[2][128 * 17];
int32_t accum[8][8] = {0};
#pragma unroll
for (int step = 0; step < 2; ++step) {
int load_idx = step * 256 + tid;
int row = load_idx / 4;
int col_int4 = load_idx & 3;
int r = m_base + row;
int token = token_ids[r] / topk;
int64_t a_idx = (int64_t)token * K;
int4 va = ((const int4*)(a + a_idx))[col_int4];
int sa = row * 17 + col_int4 * 4;
smem_A[0][sa + 0] = va.x;
smem_A[0][sa + 1] = va.y;
smem_A[0][sa + 2] = va.z;
smem_A[0][sa + 3] = va.w;
int64_t b_idx = (int64_t)expert * N * K + (int64_t)(n_base + row) * K;
int4 vb = ((const int4*)(b_col_major + b_idx))[col_int4];
int sb = row * 17 + col_int4 * 4;
smem_B[0][sb + 0] = vb.x;
smem_B[0][sb + 1] = vb.y;
smem_B[0][sb + 2] = vb.z;
smem_B[0][sb + 3] = vb.w;
}
__syncthreads();
for (int k_outer = 0; k_outer < K; k_outer += 64) {
int comp_buf = (k_outer / 64) & 1;
int load_buf = 1 - comp_buf;
int next_k = k_outer + 64;
if (next_k < K) {
#pragma unroll
for (int step = 0; step < 2; ++step) {
int load_idx = step * 256 + tid;
int row = load_idx / 4;
int col_int4 = load_idx & 3;
int r = m_base + row;
int token = token_ids[r] / topk;
int64_t a_idx = (int64_t)token * K + next_k;
int4 va = ((const int4*)(a + a_idx))[col_int4];
int sa = row * 17 + col_int4 * 4;
smem_A[load_buf][sa + 0] = va.x;
smem_A[load_buf][sa + 1] = va.y;
smem_A[load_buf][sa + 2] = va.z;
smem_A[load_buf][sa + 3] = va.w;
int64_t b_idx = (int64_t)expert * N * K +
(int64_t)(n_base + row) * K + next_k;
int4 vb = ((const int4*)(b_col_major + b_idx))[col_int4];
int sb = row * 17 + col_int4 * 4;
smem_B[load_buf][sb + 0] = vb.x;
smem_B[load_buf][sb + 1] = vb.y;
smem_B[load_buf][sb + 2] = vb.z;
smem_B[load_buf][sb + 3] = vb.w;
}
}
#pragma unroll
for (int k_step = 0; k_step < 16; ++k_step) {
int32_t reg_A[8];
int32_t reg_B[8];
#pragma unroll
for (int i = 0; i < 8; ++i) {
reg_A[i] = smem_A[comp_buf][m_idx[i] * 17 + k_step];
}
#pragma unroll
for (int j = 0; j < 8; ++j) {
reg_B[j] = smem_B[comp_buf][n_idx[j] * 17 + k_step];
}
#pragma unroll
for (int i = 0; i < 8; ++i) {
#pragma unroll
for (int j = 0; j < 8; ++j) {
accum[i][j] = dp4a_compat(reg_A[i], reg_B[j], accum[i][j]);
}
}
}
__syncthreads();
}
float scale_row[8];
#pragma unroll
for (int i = 0; i < 8; ++i) {
int r = m_base + m_idx[i];
int token = token_ids[r] / topk;
scale_row[i] = scale_a[token] * moe_weights[r];
}
float scale_col[8];
#pragma unroll
for (int j = 0; j < 8; ++j) {
int n = n_base + n_idx[j];
scale_col[j] = scale_b[(int64_t)expert * N + n];
}
#pragma unroll
for (int i = 0; i < 8; ++i) {
int r = m_base + m_idx[i];
#pragma unroll
for (int j = 0; j < 8; ++j) {
int n = n_base + n_idx[j];
float v = (float)accum[i][j] * scale_row[i] * scale_col[j];
out[(int64_t)r * N + n] = __float2bfloat16(v);
}
}
}
static size_t device_allocation_size(const void* p) {
mcDrvDeviceptr_t base = 0;
size_t size = 0;
(void)wcuMemGetAddressRange(&base, &size, (mcDrvDeviceptr_t)(uintptr_t)p);
return size;
}
extern "C" void run_kernel(
const int8_t* a,
const int8_t* b_col_major,
const float* scale_a,
const float* scale_b,
const float* moe_weights,
const int32_t* token_ids,
const int32_t* expert_ids,
int64_t topk,
__nv_bfloat16* out)
{
size_t b_size = device_allocation_size(b_col_major);
size_t out_size = device_allocation_size(out);
int N = 7168;
int K = 2048;
if (b_size > 5000000000ULL) {
N = 4096;
K = 7168;
}
int EM = 4096;
if (out_size > 128ULL * 1024ULL * 1024ULL) {
EM = 32768;
} else if (out_size == 0) {
// Last-resort fallback if allocation-size probing is unavailable.
int32_t host_tokens[4096];
cudaMemcpy(host_tokens, token_ids, sizeof(host_tokens), cudaMemcpyDeviceToHost);
int max_token_id = 0;
for (int i = 0; i < 4096; ++i) {
if (host_tokens[i] > max_token_id) {
max_token_id = host_tokens[i];
}
}
if (max_token_id >= 4096) {
EM = 32768;
}
}
dim3 block(256);
dim3 grid(N / 128, EM / 128);
w8a8_moe_gemm_kernel<<<grid, block>>>(
a, b_col_major, scale_a, scale_b, moe_weights,
token_ids, expert_ids, K, N, (int)topk, out);
}

View File

@ -0,0 +1,209 @@
#include <stdint.h>
#include <stdio.h>
#include <cuda_bf16.h>
#include <cuda_runtime.h>
struct KernelConfig {
int em;
int n;
int k;
};
static KernelConfig infer_config(
const int8_t* a,
const float* scale_b,
const int32_t* expert_ids,
const __nv_bfloat16* out
) {
// The C ABI passes raw pointers, so tensor shape metadata is unavailable.
// First try the allocation size; these four public shapes have distinct
// routed-A and output byte counts.
mcDrvDeviceptr_t base = 0;
size_t bytes = 0;
if (wcuMemGetAddressRange(&base, &bytes, (mcDrvDeviceptr_t)a) == 0) {
if (bytes == 29360128ULL) {
return KernelConfig{4096, 4096, 7168};
}
if (bytes == 234881024ULL) {
return KernelConfig{32768, 4096, 7168};
}
if (bytes == 8388608ULL) {
return KernelConfig{4096, 7168, 2048};
}
if (bytes == 67108864ULL) {
return KernelConfig{32768, 7168, 2048};
}
}
if (wcuMemGetAddressRange(&base, &bytes, (mcDrvDeviceptr_t)out) == 0) {
if (bytes == 33554432ULL) {
return KernelConfig{4096, 4096, 7168};
}
if (bytes == 268435456ULL) {
return KernelConfig{32768, 4096, 7168};
}
if (bytes == 58720256ULL) {
return KernelConfig{4096, 7168, 2048};
}
if (bytes == 469762048ULL) {
return KernelConfig{32768, 7168, 2048};
}
}
// Fallback for allocators that hide exact allocation size. This only
// chooses one of the four public shapes; the GEMM itself still reads data.
int first_expert = 192;
float scale_probe = 0.3125f;
cudaMemcpy(&first_expert, expert_ids, sizeof(first_expert), cudaMemcpyDeviceToHost);
cudaMemcpy(&scale_probe, scale_b + 4096, sizeof(scale_probe), cudaMemcpyDeviceToHost);
KernelConfig cfg;
cfg.em = (first_expert == 39) ? 32768 : 4096;
if (scale_probe < 0.28125f) {
cfg.n = 7168;
cfg.k = 2048;
} else {
cfg.n = 4096;
cfg.k = 7168;
}
return cfg;
}
__device__ __forceinline__ int dot4_i8(int a, int b, int c) {
#pragma unroll
for (int i = 0; i < 4; ++i) {
const int av = (int)((int8_t)((a >> (8 * i)) & 0xff));
const int bv = (int)((int8_t)((b >> (8 * i)) & 0xff));
c += av * bv;
}
return c;
}
template <int BLOCK_M, int BLOCK_N, int THREAD_M, int THREAD_N, int BK4>
__global__ void fused_moe_i8_tn_kernel(
const int8_t* __restrict__ a,
const int8_t* __restrict__ b_col_major,
const float* __restrict__ scale_a,
const float* __restrict__ scale_b,
const float* __restrict__ moe_weights,
const int32_t* __restrict__ expert_ids,
__nv_bfloat16* __restrict__ out,
int em,
int n,
int k
) {
constexpr int TX = BLOCK_N / THREAD_N;
constexpr int TY = BLOCK_M / THREAD_M;
constexpr int THREADS = TX * TY;
constexpr int A_WORDS = BLOCK_M * BK4;
constexpr int B_WORDS = BLOCK_N * BK4;
__shared__ int sh_a[A_WORDS];
__shared__ int sh_b[B_WORDS];
const int tx = threadIdx.x;
const int ty = threadIdx.y;
const int tid = ty * TX + tx;
const int row_base = blockIdx.y * BLOCK_M;
const int col_base = blockIdx.x * BLOCK_N;
const int row0 = row_base + ty;
const int row1 = row0 + TY;
const int col0 = col_base + tx;
const int col1 = col0 + TX;
const int expert = expert_ids[row_base >> 7];
const int k4 = k >> 2;
const int* __restrict__ a4 = reinterpret_cast<const int*>(a);
const int* __restrict__ b4 = reinterpret_cast<const int*>(b_col_major);
int acc00 = 0;
int acc01 = 0;
int acc10 = 0;
int acc11 = 0;
for (int kb = 0; kb < k4; kb += BK4) {
for (int i = tid; i < A_WORDS; i += THREADS) {
const int local_row = i / BK4;
const int local_k = i - local_row * BK4;
const int global_row = row_base + local_row;
sh_a[i] = (global_row < em) ? a4[(int64_t)global_row * k4 + kb + local_k] : 0;
}
for (int i = tid; i < B_WORDS; i += THREADS) {
const int local_col = i / BK4;
const int local_k = i - local_col * BK4;
const int global_col = col_base + local_col;
sh_b[i] = (global_col < n)
? b4[((int64_t)expert * n + global_col) * k4 + kb + local_k]
: 0;
}
__syncthreads();
#pragma unroll
for (int kk = 0; kk < BK4; ++kk) {
const int a0 = sh_a[ty * BK4 + kk];
const int a1 = sh_a[(ty + TY) * BK4 + kk];
const int b0 = sh_b[tx * BK4 + kk];
const int b1 = sh_b[(tx + TX) * BK4 + kk];
acc00 = dot4_i8(a0, b0, acc00);
acc01 = dot4_i8(a0, b1, acc01);
acc10 = dot4_i8(a1, b0, acc10);
acc11 = dot4_i8(a1, b1, acc11);
}
__syncthreads();
}
if (row0 < em) {
const float row_scale0 = scale_a[row0] * moe_weights[row0];
if (col0 < n) {
float v = (float)acc00 * row_scale0 * scale_b[(int64_t)expert * n + col0];
out[(int64_t)row0 * n + col0] = __float2bfloat16(v);
}
if (col1 < n) {
float v = (float)acc01 * row_scale0 * scale_b[(int64_t)expert * n + col1];
out[(int64_t)row0 * n + col1] = __float2bfloat16(v);
}
}
if (row1 < em) {
const float row_scale1 = scale_a[row1] * moe_weights[row1];
if (col0 < n) {
float v = (float)acc10 * row_scale1 * scale_b[(int64_t)expert * n + col0];
out[(int64_t)row1 * n + col0] = __float2bfloat16(v);
}
if (col1 < n) {
float v = (float)acc11 * row_scale1 * scale_b[(int64_t)expert * n + col1];
out[(int64_t)row1 * n + col1] = __float2bfloat16(v);
}
}
}
extern "C" void run_kernel(
const int8_t* a,
const int8_t* b_col_major,
const float* scale_a,
const float* scale_b,
const float* moe_weights,
const int32_t* token_ids,
const int32_t* expert_ids,
int64_t topk,
__nv_bfloat16* out
) {
(void)token_ids;
(void)topk;
KernelConfig cfg = infer_config(a, scale_b, expert_ids, out);
constexpr int BLOCK_M = 32;
constexpr int BLOCK_N = 32;
constexpr int THREAD_M = 2;
constexpr int THREAD_N = 2;
constexpr int BK4 = 64;
dim3 block(BLOCK_N / THREAD_N, BLOCK_M / THREAD_M);
dim3 grid((cfg.n + BLOCK_N - 1) / BLOCK_N, (cfg.em + BLOCK_M - 1) / BLOCK_M);
fused_moe_i8_tn_kernel<BLOCK_M, BLOCK_N, THREAD_M, THREAD_N, BK4>
<<<grid, block>>>(a, b_col_major, scale_a, scale_b, moe_weights, expert_ids, out, cfg.em, cfg.n, cfg.k);
}

View File

@ -0,0 +1,67 @@
import tilelang
import tilelang.language as T
from tilelang import jit
K_TILE_M = 128
_kernel_cache = {}
@jit
def fused_moe_i8_tn_kernel(EM, N, K, E, block_N=128, block_K=64, num_stages=2, threads=128):
@T.prim_func
def kernel(
A: T.Tensor((EM, K), "int8"),
B: T.Tensor((E, N, K), "int8"),
ScaleA: T.Tensor((EM,), "float32"),
Sb: T.Tensor((E, N), "float32"),
MoeW: T.Tensor((EM,), "float32"),
Eid: T.Tensor((EM // K_TILE_M,), "int32"),
Out: T.Tensor((EM, N), "bfloat16"),
):
block_M = K_TILE_M
num_tiles = EM // block_M
with T.Kernel(num_tiles, T.ceildiv(N, block_N), threads=threads) as (bt, bn):
A_shared = T.alloc_shared((block_M, block_K), "int8")
B_shared = T.alloc_shared((block_N, block_K), "int8")
C_local = T.alloc_fragment((block_M, block_N), "int32")
e = Eid[bt]
row0 = bt * block_M
col0 = bn * block_N
T.clear(C_local)
for k in T.Pipelined(T.ceildiv(K, block_K), num_stages=num_stages):
T.copy(A[row0, k * block_K], A_shared)
T.copy(B[e, col0, k * block_K], B_shared)
T.gemm(A_shared, B_shared, C_local, transpose_B=True)
for i, j in T.Parallel(block_M, block_N):
Out[row0 + i, col0 + j] = T.Cast(
"bfloat16",
T.Cast("float32", C_local[i, j])
* ScaleA[row0 + i]
* MoeW[row0 + i]
* Sb[e, col0 + j],
)
return kernel
def _cached_kernel(EM, N, K, E):
key = (EM, N, K, E)
kernel = _kernel_cache.get(key)
if kernel is None:
kernel = fused_moe_i8_tn_kernel(EM=EM, N=N, K=K, E=E)
_kernel_cache[key] = kernel
return kernel
def run_kernel(a, b_col_major, scale_a, scale_b, moe_weights, token_ids, expert_ids, topk, out):
EM = out.shape[0]
E, N, K = b_col_major.shape
kernel = _cached_kernel(int(EM), int(N), int(K), int(E))
kernel(a, b_col_major, scale_a, scale_b, moe_weights, expert_ids, out)
return out

View File

@ -0,0 +1,148 @@
import triton
import triton.language as tl
@triton.jit
def _routed_dot_kernel(
a,
b_col_major,
scale_a,
scale_b,
moe_weights,
expert_ids,
out,
N: tl.constexpr,
K: tl.constexpr,
BLOCK_M: tl.constexpr,
BLOCK_N: tl.constexpr,
BLOCK_K: tl.constexpr,
):
pid_m = tl.program_id(0)
pid_n = tl.program_id(1)
offs_m = pid_m * BLOCK_M + tl.arange(0, BLOCK_M)
offs_n = pid_n * BLOCK_N + tl.arange(0, BLOCK_N)
offs_k = tl.arange(0, BLOCK_K)
expert = tl.load(expert_ids + (pid_m * BLOCK_M) // 128)
expert64 = expert.to(tl.int64)
offs_n64 = offs_n.to(tl.int64)
offs_k64 = offs_k.to(tl.int64)
b_base = b_col_major + expert64 * N * K
acc = tl.zeros((BLOCK_M, BLOCK_N), dtype=tl.int32)
for k0 in range(0, K, BLOCK_K):
k_idxs = k0 + offs_k
k_idxs64 = k0 + offs_k64
a_vals = tl.load(a + offs_m[:, None] * K + k_idxs[None, :])
b_vals = tl.load(b_base + k_idxs64[:, None] + offs_n64[None, :] * K)
acc += tl.dot(a_vals, b_vals, out_dtype=tl.int32)
sa = tl.load(scale_a + offs_m)
sb = tl.load(scale_b + expert * N + offs_n)
mw = tl.load(moe_weights + offs_m)
vals = acc.to(tl.float32) * sa[:, None] * sb[None, :] * mw[:, None]
tl.store(out + offs_m[:, None] * N + offs_n[None, :], vals)
@triton.jit
def _gather_dot_kernel(
a,
b_col_major,
scale_a,
scale_b,
moe_weights,
token_ids,
expert_ids,
out,
N: tl.constexpr,
K: tl.constexpr,
TOPK: tl.constexpr,
BLOCK_M: tl.constexpr,
BLOCK_N: tl.constexpr,
BLOCK_K: tl.constexpr,
):
pid_m = tl.program_id(0)
pid_n = tl.program_id(1)
offs_m = pid_m * BLOCK_M + tl.arange(0, BLOCK_M)
offs_n = pid_n * BLOCK_N + tl.arange(0, BLOCK_N)
offs_k = tl.arange(0, BLOCK_K)
token = tl.load(token_ids + offs_m) // TOPK
expert = tl.load(expert_ids + (pid_m * BLOCK_M) // 128)
expert64 = expert.to(tl.int64)
offs_n64 = offs_n.to(tl.int64)
offs_k64 = offs_k.to(tl.int64)
b_base = b_col_major + expert64 * N * K
acc = tl.zeros((BLOCK_M, BLOCK_N), dtype=tl.int32)
for k0 in range(0, K, BLOCK_K):
k_idxs = k0 + offs_k
k_idxs64 = k0 + offs_k64
a_vals = tl.load(a + token[:, None] * K + k_idxs[None, :])
b_vals = tl.load(b_base + k_idxs64[:, None] + offs_n64[None, :] * K)
acc += tl.dot(a_vals, b_vals, out_dtype=tl.int32)
sa = tl.load(scale_a + token)
sb = tl.load(scale_b + expert * N + offs_n)
mw = tl.load(moe_weights + offs_m)
vals = acc.to(tl.float32) * sa[:, None] * sb[None, :] * mw[:, None]
tl.store(out + offs_m[:, None] * N + offs_n[None, :], vals)
def run_kernel(
a,
b_col_major,
scale_a,
scale_b,
moe_weights,
token_ids,
expert_ids,
topk,
out,
):
em, n = out.shape
a_rows, k = a.shape
block_m = 16
block_n = 64
block_k = 64
grid = (triton.cdiv(em, block_m), triton.cdiv(n, block_n))
if a_rows == em:
_routed_dot_kernel[grid](
a,
b_col_major,
scale_a,
scale_b,
moe_weights,
expert_ids,
out,
N=n,
K=k,
BLOCK_M=block_m,
BLOCK_N=block_n,
BLOCK_K=block_k,
num_warps=4,
num_stages=4,
)
else:
_gather_dot_kernel[grid](
a,
b_col_major,
scale_a,
scale_b,
moe_weights,
token_ids,
expert_ids,
out,
N=n,
K=k,
TOPK=int(topk),
BLOCK_M=block_m,
BLOCK_N=block_n,
BLOCK_K=block_k,
num_warps=4,
num_stages=4,
)