Mavis (阿念) 6deca6aab1 ZZ-FLAGOS-S2-2026-D246-001 · Task 38 sigmoid_gate_topk_renorm 准备就绪
3-kernel 写法 (K1 sigmoid+bias / K2 自写 topk 迭代 argmax / K3 gather+cat+sigmoid+renorm+scale+split)
+ 国产 NPU 套路全套 (指针 cast int16 / int64 stride / enable_fp_fusion=False / 1/(1+exp(-x))
+ 算法 8 case 测试全过 (T=1~128, N=8~256, S=1~2, k=2~8, 含 DeepSeek-V3 风格)
+ 之之 D246 00:12 决策:用 Triton 自写 topk, 走性能路线

提交次数 1/30 已用 (K35), 剩 29 次 · 截止 2026-09-03 19:59 (剩 ~19h41m)

阿念 (Mavis, ICE-GL-AN-001) · 之之的家 · 2026-09-03 D246 00:18
2026-09-03 00:18:02 +08:00

79 lines
3.5 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.

# FlagOS S2 赛季二 · 赛道一 · D246 续战(Task 38)
> 之之 (8592_apivqhj) + 阿念 (Mavis, ICE-GL-AN-001, Code) · 协作产出
> 比赛: FlagOS 开放计算全球大赛 S2 · Track 1 · SGLang 算子优化
> 队伍: GuanghuLab(队长孙蓓)
> 第 3 批 17 道题 · 截止 2026-09-03 19:59 (剩 ~19h41m)
---
## 战绩(D246 00:18 续)
| Task | 算子 | 状态 | 平均加速比 | 详情 |
|---|---|---|---|---|
| 30 | interleaved_rope (M-RoPE) | ✅ 已传 (D245 23:16) | **35.79×** (5/8 跑通) | 天数 86.81× / 海光 42.86× / 国通A 26.91× / 沐曦 15.54× / 华为 6.84×;燧原/昆仑芯 Failed |
| 29 | gelu_and_mul | ✅ 已传 (D245 23:30) | 3.02× (7/8 跑通) | 天数 5.32× / 海光 4.03× / 国通A 3.73× / 国通B 3.25× / 沐曦 2.74× / 华为 1.09× / 燧原 0.96×;昆仑芯 Failed |
| 35 | rotary_embedding | ✅ 已传 (D246 00:04) | 5.15× (6/8 跑通) | 国际B 8.79× / 国际A 7.53× / 天数 6.72× / 海光 4.11× / 沐曦 3.41× / 昆仑芯 0.32×;华为 Failed, 燧原 "评测中" |
| **38** | **sigmoid_gate_topk_renorm** | ⏳ **待传(D246 00:17 准备就绪)** | - | 3-kernel 写法 (sigmoid+bias / 自写 topk / gather+renorm) |
## D246 续战重点
### Task 38 · 选第 4 题
- 找过 `embedding` (无) / `norm` (匹配到 Task 38)
- Task 38 = MoE 路由门算子 (DeepSeek-V2/V3 / Qwen3-MoE 风格)
- 当前第一名 sitraliqui 仅 9.05×**大家都没做出来**,机会大
- 没有 GEMM,纯 elementwise + reduce + gather → 跨芯片可行性高
### 选路决策
之之 D246 00:12 决策:**"用 Triton 自写 topk(性能好)"** — 不走 torch.topk 兜底,要冲分
### 实施方案
**3-kernel 写法**(不用 torch.topk,自写):
```
K1: sel = sigmoid(routed) + bias (Triton elementwise)
K2: topk → indices (Triton, 迭代 tl.argmax K 次)
K3: gather + cat + sigmoid + renorm + scale + split (Triton, 1 token/program)
```
### 国产 NPU 套路全套
- 指针 cast `to(tl.pointer_type(tl.int16))` 防 NaN
- stride / pid 用 int64 cast 防 overflow
- `with torch.get_device_module(x.device).device(x.device):`
- `enable_fp_fusion=False, num_warps=4`
- `1.0 / (1.0 + tl.exp(-x))` 不用 `tl.sigmoid`
### 算法测试 8/8 pass
对比平台 reference vs 我们的算法拆分(torch 模拟 K1/K2/K3):
- T=1~128, N=8~256, S=1~2, k=2~8
- 包括 DeepSeek-V3 风格 (T=64, N=256, k=8)
- **全部 pass**:indices match, routed close (atol=1e-3), shared close
## 风险点
1. **K2 `tl.argmax` 在国产 NPU 上可能不支持** — 降级方案:把 K2 换成 `torch.topk` 1 行
2. **3 kernel 的 launch overhead** — 性能上限受限(估计 3-8×)
3. **中间 buffer `sel` 大小** — DeepSeek-V3 风格 (T=128, N=256) = 128KB
## 预期
- 跑通率:6-7/8
- 平均加速比:3-8×
- 排名:跟 sitraliqui (9.05×) 接近或略低
---
## 目录结构
```
d246/
├── README.md (本文件)
├── sigmoid_gate_topk_renorm.py (主文件 · 3 Triton kernel + 国产 NPU 套路)
├── test_algorithm.py (8 case 算法层测试)
├── SUBMIT_CHECKLIST.md (提交清单 · 给之之看的)
└── results/
└── sigmoid_gate_topk_renorm.zip (2.6 KB · 通用版,1 个文件)
```
## 学到的(本次)
1. **3-kernel 分拆 vs 单 kernel fused** — 跑通率 vs 性能上限的权衡,Task 38 选了前者
2. **自写 topk 套路**`tl.argmax` + `tl.where(mask, -inf, sel)` 迭代 K 次,K=8 内可行
3. **算法层测试先行** — 8 case 在 CPU 跑过确认逻辑,GPU 编译是第二阶段