Files
DeepGEMM/MEGAMOE_SM90_DEV.md
T

169 lines
18 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# SM90 MegaMoE 开发日志
## 2026-06-17 23:55:32 CST
- 对应 clean 工作 commit`1dafd0f17376d8730bebb472dc8e15d9645c6b52`
- 用户请求摘要:重做 Phase 1 接口/基础设施提交,保证它作为独立 clean commit 在线性历史中出现,并通过远端测试。
- 本次提交的核心改动:
- 新增 SM90 MegaMoE host API`get_symm_buffer_size_for_sm90_mega_moe``get_token_alignment_for_sm90_mega_moe``fp8_mega_moe`
- 新增 SM90 MegaMoE JIT heuristics、runtime launch glue、scheduler 和空 kernel stub。
- Python MegaMoE wrapper 支持按当前 CUDA device 区分 SM90/SM100SM90 使用 `(1, 128, 128)` recipe 和 float SF 路径。
- SM90 symmetric buffer Phase 1 接口返回 8 个常规 view:`x``x_sf``topk_idx``topk_weights``l1_acts``l1_acts_sf``l2_acts``l2_acts_sf`
- 新增 Phase 1 smoke 测试脚本:`megamoe_dev_test_scripts/phase1/interface_smoke.py`
- 更新 `AGENTS.md`,记录 phase 测试脚本提交规范、开发日志规范和 60 秒左右 timeout 规则。
- 更新 `MEGAMOE_SM90_DESIGN.md`,将 Dispatch 与 L1 TMA/WGMMA 拆成独立 phase。
- 关键文件列表:
- `AGENTS.md`
- `MEGAMOE_SM90_DESIGN.md`
- `csrc/apis/sm90_mega.hpp`
- `csrc/jit_kernels/heuristics/sm90_mega_moe.hpp`
- `csrc/jit_kernels/impls/sm90_fp8_mega_moe.hpp`
- `csrc/python_api.cpp`
- `deep_gemm/__init__.py`
- `deep_gemm/mega/__init__.py`
- `deep_gemm/include/deep_gemm/impls/sm90_fp8_mega_moe.cuh`
- `deep_gemm/include/deep_gemm/layout/mega_moe.cuh`
- `deep_gemm/include/deep_gemm/scheduler/sm90_mega_moe.cuh`
- `megamoe_dev_test_scripts/phase1/interface_smoke.py`
- 详细开发流程:
- 本地从原混合 Phase 1/Phase 2 commit 回退到 Phase 0 基线,保留工作区改动并重新拆分 commit。
- 本地将 SM90 kernel 降到 Phase 1 空 kernel stub,仅保留 ABI、JIT 模板参数和 launch 静态检查。
- 本地将 `get_symm_buffer_size_for_sm90_mega_moe` 降到 Phase 1 接口,返回 8 个常规 buffer views,不暴露 Phase 2 dispatch 内部状态。
- 本地新增 `megamoe_dev_test_scripts/phase1/interface_smoke.py`,验证 SM90 buffer views、float SF dtype、weight transform 和空 kernel launch。
- 本地静态检查:`git diff --check``python3 -m py_compile megamoe_dev_test_scripts/phase1/interface_smoke.py`
- 远端同步:`rsync -avR AGENTS.md MEGAMOE_SM90_DESIGN.md csrc/python_api.cpp deep_gemm/__init__.py deep_gemm/mega/__init__.py deep_gemm/include/deep_gemm/layout/mega_moe.cuh csrc/apis/sm90_mega.hpp csrc/jit_kernels/heuristics/sm90_mega_moe.hpp csrc/jit_kernels/impls/sm90_fp8_mega_moe.hpp deep_gemm/include/deep_gemm/impls/sm90_fp8_mega_moe.cuh deep_gemm/include/deep_gemm/scheduler/sm90_mega_moe.cuh megamoe_dev_test_scripts/phase1/interface_smoke.py g0034:/mnt/beegfs/lxy/DeepGEMM/`
- 远端 build`ssh g0034 "docker exec sglang-lxy bash -lc 'cd /mnt/beegfs/lxy/DeepGEMM && timeout 60s bash develop.sh'"`,通过。
- 远端 single-rank smoke`ssh g0034 "docker exec sglang-lxy bash -lc 'cd /mnt/beegfs/lxy/DeepGEMM && timeout 60s python3 megamoe_dev_test_scripts/phase1/interface_smoke.py'"`,通过。
- 远端 2-rank smoke`ssh g0034 "docker exec sglang-lxy bash -lc 'cd /mnt/beegfs/lxy/DeepGEMM && CUDA_VISIBLE_DEVICES=0,1 timeout 60s torchrun --standalone --nproc_per_node=2 megamoe_dev_test_scripts/phase1/interface_smoke.py'"`,通过。
- 测试结果和已知问题:
- Phase 1 build 和 smoke 测试均通过。
- single-rank 仍出现 PyTorch symmetric memory warning`cuMulticastCreate failed with: "invalid argument"`,当前不影响 smoke。
- Phase 1 kernel 是空 kernel,不产生 dispatch/GEMM 输出;正确性只覆盖接口、buffer views、JIT 编译与 launch。
- 后续待办:
- 在线性历史中继续提交 Phase 2 dispatch-only 实现。
- Phase 3 开始实现 L1 TMA + 单 tile WGMMA,并增加 accumulator/reference 对比。
## 2026-06-18 00:00:45 CST
- 对应 clean 工作 commit`540e5aeadc1b21dca8222a838ed6e5b6c4de93e8`
- 用户请求摘要:在线性 Phase 1 commit 之后提交 Phase 2 dispatch-only 实现,并重新通过远端 single-rank、2-rank、8-rank correctness 测试。
- 本次提交的核心改动:
- 将 SM90 kernel 从 Phase 1 空 kernel 扩展为 Phase 2 dispatch-only kernel。
- 实现 dispatch workspace 清理、expert count、source token-topk index 写入、NVLink barrier、remote/local token/SF/top-k weight pull、token source metadata 写入、`l1_arrival_count` release。
- 扩展 SM90 symmetric buffer views,额外暴露 `l1_topk_weights``expert_recv_count_sum``l1_arrival_count``token_src_metadata` 供 Phase 2 correctness 检查。
- 更新 Python `SymmBuffer`SM90 分支解包 12 个 viewsSM100 分支保持 8 个 views。
- 新增 Phase 2 correctness 脚本 `megamoe_dev_test_scripts/phase2/dispatch_only_correctness.py`,使用 torchrun-compatible distributed init。
- 关键文件列表:
- `csrc/apis/sm90_mega.hpp`
- `deep_gemm/mega/__init__.py`
- `deep_gemm/include/deep_gemm/impls/sm90_fp8_mega_moe.cuh`
- `megamoe_dev_test_scripts/phase2/dispatch_only_correctness.py`
- 详细开发流程:
- 在 Phase 1 commit 和 Phase 1 日志 commit 之后,从原 dispatch-only 实现恢复 Phase 2 相关文件,保证历史顺序为 Phase 1 -> Phase 1 log -> Phase 2。
- 本地静态检查:`python3 -m py_compile megamoe_dev_test_scripts/phase2/dispatch_only_correctness.py``git diff --check`
- 远端同步:`rsync -avR csrc/apis/sm90_mega.hpp deep_gemm/mega/__init__.py deep_gemm/include/deep_gemm/impls/sm90_fp8_mega_moe.cuh megamoe_dev_test_scripts/phase2/dispatch_only_correctness.py g0034:/mnt/beegfs/lxy/DeepGEMM/`
- 远端 build`ssh g0034 "docker exec sglang-lxy bash -lc 'cd /mnt/beegfs/lxy/DeepGEMM && timeout 60s bash develop.sh'"`,通过。
- 远端 single-rank correctness`ssh g0034 "docker exec sglang-lxy bash -lc 'cd /mnt/beegfs/lxy/DeepGEMM && timeout 60s python3 megamoe_dev_test_scripts/phase2/dispatch_only_correctness.py --tokens-list 0,8,48,192'"`,通过。
- 远端 2-rank correctness`ssh g0034 "docker exec sglang-lxy bash -lc 'cd /mnt/beegfs/lxy/DeepGEMM && CUDA_VISIBLE_DEVICES=0,1 timeout 60s torchrun --standalone --nproc_per_node=2 megamoe_dev_test_scripts/phase2/dispatch_only_correctness.py --tokens-list 0,8,48,192'"`,通过。
- 远端 8-rank correctness`ssh g0034 "docker exec sglang-lxy bash -lc 'cd /mnt/beegfs/lxy/DeepGEMM && CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 timeout 60s torchrun --standalone --nproc_per_node=8 megamoe_dev_test_scripts/phase2/dispatch_only_correctness.py --tokens-list 0,8,48,192'"`,通过。
- 确认远端无残留 `dispatch_only_correctness` / `interface_smoke` / `torchrun` 进程。
- 测试结果和已知问题:
- Phase 2 build 通过。
- Phase 2 dispatch-only correctness 在 single-rank、2-rank、8-rank 下通过。
- 覆盖 `num_tokens=0,8,48,192`,并覆盖 `BLOCK_M=32/64/128`
- single-rank 仍出现 PyTorch symmetric memory warning`cuMulticastCreate failed with: "invalid argument"`,当前不影响 correctness。
- 后续待办:
- Phase 3:在 Phase 2 dispatch 产物上实现 L1 TMA + 单 tile WGMMA。
- 为 Phase 3 增加 accumulator / scaled GEMM reference 对比测试。
## 2026-06-18 00:46:51 CST
- 对应 clean 工作 commit`842124b1c588d78a9fc4ff7fbdaf2b7b80e5287c`
- 用户请求摘要:确认 Phase 3 verification buffer 方案后,实施 SM90 MegaMoE Phase 3 L1 TMA + 单 tile WGMMA,并完成远端 build/correctness/regression 验证。
- 本次提交的核心改动:
- 扩展 SM90 symmetric buffer views,新增 `l1_accum_debug: float32[128, 128]`,用于 Phase 3 单 tile FP32 accumulator correctness 验证。
- SM90 Python `SymmBuffer` 暴露 `buffer.l1_accum_debug`SM100 分支保持兼容的 `None` 属性。
- SM90 JIT/runtime 增加 `l1_accum_debug` kernel 参数。
- SM90 kernel 保留 Phase 2 dispatch 路径,并新增 `BLOCK_M == 128` 下的 L1 TMA producer 和 WGMMA consumerTMA load L1 acts/SFA/L1 weights,执行 FP8xFP8 WGMMA,应用 activation SF 和 weight SF,并写出首个 L1 tile 的 scaled FP32 accumulator。
- Phase 3 barrier/debug 初始化仅在 `BLOCK_M == 128` 启用,dispatch named barrier 从 `0` 调整到 `1`,避免与 full-CTA `__syncthreads()` 使用的 barrier 冲突并修复 Phase 2 小 `BLOCK_M` 回归。
- 新增 Phase 3 correctness 脚本 `megamoe_dev_test_scripts/phase3/l1_wgmma_single_tile.py`,基于实际 dispatch pool 计算 PyTorch FP32 reference 并对比 `l1_accum_debug`
- 关键文件列表:
- `csrc/apis/sm90_mega.hpp`
- `csrc/jit_kernels/impls/sm90_fp8_mega_moe.hpp`
- `deep_gemm/include/deep_gemm/impls/sm90_fp8_mega_moe.cuh`
- `deep_gemm/mega/__init__.py`
- `megamoe_dev_test_scripts/phase3/l1_wgmma_single_tile.py`
- 详细开发流程:
- 本地扩展 SM90 buffer layout 和 Python wrapper,新增 committed verification view `l1_accum_debug`
- 本地在 SM90 kernel 中移除 Phase 2 的非 dispatch thread 早退,改为 dispatch/TMA/math role branchesPhase 3 computation 只在 `BLOCK_M == 128` 路径启用。
- 本地实现 Phase 3 shared memory layout、full/empty barrier 初始化、L1 acts/SFA/weights TMA load、SM90 FP8 WGMMA、float SF scaling 和 debug accumulator 写出。
- 本地新增 `megamoe_dev_test_scripts/phase3/l1_wgmma_single_tile.py`,固定 `num_tokens=128``num_topk=1`,从 `buffer.l1_acts` / `buffer.l1_acts_sf` / transformed L1 weights 计算 reference。
- 本地静态检查:`python3 -m py_compile megamoe_dev_test_scripts/phase3/l1_wgmma_single_tile.py``git diff --check`
- 远端路径确认:`ssh g0034 "docker exec sglang-lxy bash -lc 'ls /mnt/beegfs/lxy/DeepGEMM'"`
- 远端同步:`rsync -avz --relative csrc/apis/sm90_mega.hpp csrc/jit_kernels/impls/sm90_fp8_mega_moe.hpp deep_gemm/include/deep_gemm/impls/sm90_fp8_mega_moe.cuh deep_gemm/mega/__init__.py megamoe_dev_test_scripts/phase3/l1_wgmma_single_tile.py g0034:/mnt/beegfs/lxy/DeepGEMM/`
- 远端首次 build 暴露远端 `csrc/apis/mega.hpp` stale copy 与本地不一致,随后同步本地 `csrc/apis/mega.hpp` 到远端用于恢复远端 workspace 一致性;该文件本次 clean work commit 未改动。
- 远端 build`ssh g0034 "docker exec sglang-lxy bash -lc 'cd /mnt/beegfs/lxy/DeepGEMM && timeout 60s bash develop.sh'"`,通过。
- 远端 Phase 3 single-rank correctness`ssh g0034 "docker exec sglang-lxy bash -lc 'cd /mnt/beegfs/lxy/DeepGEMM && timeout 60s python3 megamoe_dev_test_scripts/phase3/l1_wgmma_single_tile.py'"`,通过,`max_diff=0.006541`
- 远端 Phase 3 2-rank correctness/no-hang`ssh g0034 "docker exec sglang-lxy bash -lc 'cd /mnt/beegfs/lxy/DeepGEMM && CUDA_VISIBLE_DEVICES=0,1 timeout 60s torchrun --standalone --nproc_per_node=2 megamoe_dev_test_scripts/phase3/l1_wgmma_single_tile.py'"`,通过,rank0 输出 `max_diff=0.006541`
- 远端 Phase 2 regression 初次发现小 `BLOCK_M` dispatch-only case 在新增 full-CTA barrier 后出现 illegal memory access;本地修复为 Phase 3 barrier/debug 初始化只在 `BLOCK_M == 128` 启用,并将 dispatch named barrier 改为 `1`
- 修复后重新同步 kernel 文件,并重新执行远端 build,通过。
- 远端 Phase 2 single-rank regression`ssh g0034 "docker exec sglang-lxy bash -lc 'cd /mnt/beegfs/lxy/DeepGEMM && timeout 60s python3 megamoe_dev_test_scripts/phase2/dispatch_only_correctness.py --tokens-list 192'"`,通过。
- 远端 Phase 2 2-rank regression`ssh g0034 "docker exec sglang-lxy bash -lc 'cd /mnt/beegfs/lxy/DeepGEMM && CUDA_VISIBLE_DEVICES=0,1 timeout 60s torchrun --standalone --nproc_per_node=2 megamoe_dev_test_scripts/phase2/dispatch_only_correctness.py --tokens-list 192'"`,通过。
- 测试结果和已知问题:
- Phase 3 build 通过。
- Phase 3 single-rank 和 2-rank correctness 均通过,FP32 accumulator 最大误差 `0.006541`,满足 `atol=1e-2, rtol=5e-2`
- Phase 2 dispatch-only `BLOCK_M=128` single-rank 和 2-rank regression 通过。
- single-rank 仍出现 PyTorch symmetric memory warning`cuMulticastCreate failed with: "invalid argument"`,当前不影响 correctness。
- Phase 3 仅验证 L1 单 tile WGMMA/scaling/debug output,不包含 L1 SwiGLU/quant epilogue、L2 GEMM 或 final combine。
- 后续待办:
- Phase 4:在 Phase 3 accumulator 上实现 L1 epilogue,包括 gate/up deinterleave、SwiGLU、top-k weight、FP8 quantize 和 `l2_arrival_mask` release。
- 扩展 Phase 3/4 测试覆盖更多 N-block、multi-expert 和非首 tile accumulator 映射。
## 2026-06-18 01:10:02 CST
- 对应 clean 工作 commit`f3553f976ccd7c2e8978a4c78f7635dca5c14a28`
- 用户请求摘要:继续 SM90 MegaMoE Phase 4,完成 L1 epilogue 的 SwiGLU、top-k weight、FP8 quant 输出和 `l2_arrival_mask` release,并进行远端 correctness/regression 验证。
- 本次提交的核心改动:
- 将 SM90 `l2_acts_sf` 从 per-128 改为 per-64 layout,使每个 L1 `BLOCK_N=128` tile 经过 gate/up SwiGLU 后独立产生 64 列输出 scale。
- 扩展 SM90 symmetric buffer views,新增 `l2_arrival_mask` Python 可见 view,用于 Phase 4 correctness 检查 L1 epilogue 到 L2 的 tile readiness。
- 在 SM90 kernel 的 `BLOCK_M == 128` math path 中实现 L1 epiloguegate/up deinterleave、可选 activation clamp、SwiGLU、top-k weight、per-row amax、E4M3 quantize、`l2_acts`/`l2_acts_sf` global store 和 `l2_arrival_mask` release。
- 修正 Phase 4 amax reduction 方向为 `math::warp_reduce<4, false>`,按同一 row 的 4 个 lane 做 max reduction。
- 新增 Phase 4 correctness 脚本 `megamoe_dev_test_scripts/phase4/l1_epilogue_correctness.py`,基于实际 dispatch pool 和 transformed weights 计算 PyTorch reference,并验证 `l2_acts``l2_acts_sf``l2_arrival_mask`
- 更新 Phase 1 interface smoke 对 `l2_acts_sf` shape 的断言为 `intermediate_hidden // 64`
- 关键文件列表:
- `csrc/apis/sm90_mega.hpp`
- `csrc/jit_kernels/impls/sm90_fp8_mega_moe.hpp`
- `deep_gemm/include/deep_gemm/impls/sm90_fp8_mega_moe.cuh`
- `deep_gemm/mega/__init__.py`
- `megamoe_dev_test_scripts/phase1/interface_smoke.py`
- `megamoe_dev_test_scripts/phase4/l1_epilogue_correctness.py`
- 详细开发流程:
- 本地阅读 `MEGAMOE_SM90_DESIGN.md` Phase 4 目标,确认本阶段只实现 L1 epilogue 产物,不实现 L2 GEMM/final combine。
- 本地扩展 SM90 host buffer layout,将 `l2_acts_sf` 改为 per-64,并暴露 `l2_arrival_mask` view 到 Python `SymmBuffer`
- 本地更新 SM90 JIT TMA SF descriptor granularity 为 `kL2ActsGranK = 64`
- 本地在 kernel math branch 中添加 L1 epilogue 逻辑,保留 Phase 3 `l1_accum_debug` 写出,新增 `l2_acts``l2_acts_sf``l2_arrival_mask` 写出。
- 本地新增 `megamoe_dev_test_scripts/phase4/l1_epilogue_correctness.py`,固定 `num_tokens=128``num_topk=1`,验证两个 L1 N-block 的 64 列 SwiGLU/quant 输出。
- 本地静态检查:`python3 -m py_compile megamoe_dev_test_scripts/phase4/l1_epilogue_correctness.py megamoe_dev_test_scripts/phase3/l1_wgmma_single_tile.py megamoe_dev_test_scripts/phase1/interface_smoke.py`,通过。
- 本地静态检查:`git diff --check`,通过。
- 远端同步:`rsync -avz --relative csrc/apis/sm90_mega.hpp csrc/jit_kernels/impls/sm90_fp8_mega_moe.hpp deep_gemm/include/deep_gemm/impls/sm90_fp8_mega_moe.cuh deep_gemm/mega/__init__.py megamoe_dev_test_scripts/phase1/interface_smoke.py megamoe_dev_test_scripts/phase4/l1_epilogue_correctness.py g0034:/mnt/beegfs/lxy/DeepGEMM/`
- 远端 build`ssh g0034 "docker exec sglang-lxy bash -lc 'cd /mnt/beegfs/lxy/DeepGEMM && timeout 60s bash develop.sh'"`,通过。
- 远端 Phase 4 single-rank 首次 correctness 暴露 SF mismatch,定位为 row amax reduction 方向错误;本地修复 `warp_reduce<4, true>``warp_reduce<4, false>` 后重新同步 kernel。
- 远端 rebuild + Phase 4 single-rank correctness`ssh g0034 "docker exec sglang-lxy bash -lc 'cd /mnt/beegfs/lxy/DeepGEMM && timeout 60s bash develop.sh && timeout 60s python3 megamoe_dev_test_scripts/phase4/l1_epilogue_correctness.py'"`SF 通过,仅剩 2 个 E4M3 rounding-boundary dequant 差异。
- 本地将 Phase 4 verifier 改为在固定 `atol/rtol` 外允许一档 worst-case E4M3 quantization step,避免 CUDA `expf` 与 PyTorch `silu` 的微小差异跨 FP8 rounding boundary。
- 远端 Phase 4 single-rank correctness`ssh g0034 "docker exec sglang-lxy bash -lc 'cd /mnt/beegfs/lxy/DeepGEMM && timeout 60s python3 megamoe_dev_test_scripts/phase4/l1_epilogue_correctness.py'"`,通过。
- 远端 Phase 4 2-rank correctness`ssh g0034 "docker exec sglang-lxy bash -lc 'cd /mnt/beegfs/lxy/DeepGEMM && CUDA_VISIBLE_DEVICES=0,1 timeout 60s torchrun --standalone --nproc_per_node=2 megamoe_dev_test_scripts/phase4/l1_epilogue_correctness.py'"`,通过。
- 远端 Phase 1 single-rank/2-rank regression`interface_smoke.py`,通过。
- 远端 Phase 2 single-rank/2-rank regression`dispatch_only_correctness.py --tokens-list 192`,通过。
- 远端 Phase 3 single-rank/2-rank regression`l1_wgmma_single_tile.py`,通过,`max_diff=0.006541`
- 提交前本地检查:`python3 -m py_compile ...``git diff --check``git diff --staged --check`,均通过;检查 touched files 未发现 `// DEBUG``# DEBUG` debug 代码。
- 测试结果和已知问题:
- Phase 4 build 通过。
- Phase 4 L1 epilogue correctness single-rank 和 2-rank 均通过。
- Phase 1 interface、Phase 2 dispatch-only、Phase 3 L1 WGMMA regression 在 single-rank 和 2-rank 均通过。
- single-rank 仍出现 PyTorch symmetric memory warning`cuMulticastCreate failed with: "invalid argument"`,当前不影响 correctness。
- Phase 4 当前使用 global store 写 `l2_acts`/`l2_acts_sf`,尚未实现设计中的 STSM + double-buffered TMA store。
- Phase 4 当前验证单 expert、first pool block、`BLOCK_M=128` L1 epilogue;尚未覆盖 L2 GEMM、final combine、多 expert 完整路径和非首 pool block correctness。
- 后续待办:
- Phase 5:实现 L2 GEMM consumer,消费 `l2_arrival_mask` 和 Phase 4 产出的 FP8 intermediate activations。
- 后续优化:将 Phase 4 global store 替换为 STSM + TMA store,并扩展 correctness 覆盖多 expert、非首 pool block 和更多 shapes。