Repository navigation
feat: add sdpa_math operator (Ascend Triton + P800 exact-P/A1) - #10
Conversation
|
Added P800/Kunlunxin as the second
Raw data and repro commands are in |
1ea7fee to
88a3b57
Compare
- _probs_kernel 增 HAS_PV:QK→softmax→P 落地→PV 同 tile 累加,1 次启动 - causal 尾块补零收进 kernel,P 一律 empty(去掉独立 torch.zeros 启动) - 选择判据按实际处理 score tile 数(causal 截断后),阈值 16: 15 tiles 0.98x · 21 tiles 1.07x · 64 tiles 1.16x(同面积 128x2048 反而 0.85x) - dropout_p>0 恒走两段式(掩码须先作用在 P) - 实测:decode 0.287→0.217ms(0.92x→1.17x)、tail 0.285→0.219ms(0.97x→1.16x), 大形状按阈值走两段式持平;门禁 FAIL 0(triton +7.8%,阈值内) - 回归:黄金 175/175 worst 3.906e-3 不变;三层 ascend910/cpu 全绿
- performance.md §5 补栈级差距拆解(GEMM 5.2x / UB 192KB / P 往返 / launch 97µs) 与 JIT 口径,新增 §6 单 kernel 融合实验(tiles 阈值表 + 18 组实测) - development/test-report/REPORT/README: 6 形状性能表更新为融合后数字, decode/tail 遗留项改记 A1 包装 0.86x,后续优化项标记已完成 - ops/README.md 索引行 1.20-2.05x → 1.16-2.03x
88a3b57 to
944b050
Compare
- _d1(): 仅要求 stride(-1)==1,transposed 输入免物化(单次 ~14µs) - P+O 合并为一次 empty + 两个 view(省一次分配/启动) - 直调口径: decode 195.2→149.5µs (-23%)、prefill 288.2→223.8µs (-22%) - 融合阈值 16→56: e2e decode_cache (17-18 tiles) 强制融合 -15%、 decode_re (36-55 tiles) -9%;64+/prefill 136 tiles 仍走两段式。 依据为 e2e 流水口径实测(latency 单次同步口径在 32-55 tiles 低估融合) - 回归: 175/175 worst 3.906e-3 不变、example 双端全绿、门禁 FAIL 0
- performance.md §8: decode 差距归因(GC 丢注册测量陷阱、同进程 A/B、 host/device 分离、e2e 分段、布局排除) - performance.md §9: e2e 三场景结果 + 本轮三项改动分解(跳 contiguous、 P+O 合并分配、融合阈值 16→56)与口径分歧说明 - §2/§4 数字随 2026-10-02 复测更新(环境漂移同向,门禁 FAIL 0) - §6/development.md/REPORT.md 阈值引用 16→56 同步 - probes/ 入库 8 个有效探针(去掉绝对路径,相对 repo 定位)
- §4: triton +7.9% / torch -0.4% / reference -0.8%(rebase 后 FAIL 0), 撤回窗口性 +13.9% 漂移的临时读数 - §2/§5: 跨日绝对值标注共享机 ±5% 波动,判定看同运行 speedup/趋势
2026-10-02 更新总结(最终态)本轮改动(
总体性能(vs 同契约原生 math,NPU fp16,2026-10-02):
端到端(4 层 D512 H8 + 熵正则消费 P):prefill 2.6 vs 2.9ms(-10%) · decode_re 297 vs 304ms(反超) · decode_cache 291 vs 241ms(+21%,归因:设备 kernel 时间已 ≈ native,残余为 host 发射 + 注册包装 ~120µs·call⁻¹,见 §8) 验证:黄金 175/175(worst 3.906e-3 不变)· 天花板:vs |
|
📦 后续维护提示(合入后补记,#16 审计整改关联): 本 PR 合入后 ops/ 做了全库审计整改(#16),与你相关的变化:
|
|
按“后续维护提示”做作者自查时发现并修复一个当前平台问题:多算子 perf registry 加载后, |
概述
新增算子
ops/sdpa_math——aten::_scaled_dot_product_attention_math(SDPA 的 math 后端,返回(out, attn_probs)双输出),A1 路线接管,与既有ops/sdpa目录独立、代码零依赖。为"必须拿到注意力概率图"的消费方(蒸馏 / 熵正则 / rollout / 对齐分析)提供一个快而正确的实现。实现
reference.py(纯 PyTorch 数学权威)·kernel/torch_level.py(ATen 组合,CPU 交付)·kernel/triton_level.py(Triton 两段式,NPU 交付)register_a1()成对注册AutogradPrivateUse1+PrivateUse1/AutogradCPU+CPU;每次注册绑定独立_Fn闭包(同进程多 impl 不互相覆盖);grad 走autograd.Function(dout+dprobs双路梯度),no_grad/inference 直通probes/native_semantics.py(6 节实测证据)+ 黄金互验 + 测试三重锁定验证(ascend910 与 cpu 双 profile)
F.sdpa(MATH)命中perf_compareFAIL 0 · WARN 0,基线triton 0.504ms / 8.524 TFLOPS性能(NPU fp16,基线 = 同样返回
(out,P)的原生 math 后端)CPU fp32 六形状 1.05–1.40x。对照不返回 P 的
F.sdpa融合路径约慢 4x —— 物化概率图的契约代价,判卷只用同契约基线。有意分歧与遗留
-inf遮蔽实现(对齐F.sdpa),与 native 直调的 0/1 加性怪癖有意分歧 → 44 组黄金在--impl native下跳过并注明autograd.Function包装 ~0.078ms 属 torch 图节点开销,非 kernel 可控dropout_p>0的 A1 路径out=P@V走 ATen matmul(dropout 非性能路径,register.py已注明)交付物
README+REPORT+ 报告四件套(development / test-report / accuracy / performance)+ 175 组黄金 + 4 脚本 + 3 层测试 + native 语义 probe;已登记ops/README.md索引、common/perf_registry.py与docs/known-issues.md #19。2026-10-01 update: P800 / Kunlunxin second platform
1e69c53adds the P800 profile and backend path:kernel/torch_level.pyATen composition;AutogradCUDA+CUDApaired keys;flag_gems.only_enable(["gelu"]);P800 validation
torch @ cuda:1)P800 fp16 performance
Native baseline is the same private math op and therefore has the same
(output, attention)contract.Raw data:
ops/sdpa_math/reports/perf_p800-kunlunxin.json.2026-10-01 update: 单 kernel 融合(小形状)
e0b9c87+09ce990:_probs_kernel增加HAS_PV模式 —— QK→softmax→P→PV 在一个 kernel 内完成(P 块只在寄存器里、O 直写,probs仍按契约物化),causal 尾块补零也收进 kernel(去掉独立torch.zeros启动)。dropout_p>0恒走两段式(掩码须先作用在 P)。SDPA_MATH_FUSED=on/off/auto可强制(仅实验)。example.py ascend910与cpu三层全绿 · 门禁 FAIL 0 · WARN 0(triton 0.543 vs 基线 0.504,+7.8%,阈值内)。reports/performance.md §5–§6。2026-10-02 update: 微优化 + 融合阈值重定标(16→56)+ 端到端
7885074+6ce4b59+83482a1(报告新增 §8/§9,8 个有效探针入库probes/):_d1()仅要求stride(-1)==1(transposed 输入免 contiguous,单次 ~14µs);P+O 合并为一次empty+ 两个 view(省一次设备启动)。直调口径 decode 195.2→149.5µs(-23%)、prefill 288.2→223.8µs(-22%)。probes/e2e_probe.py,4 层 D512 H8 + 每层熵正则消费 P,fp16 注册路径):prefill 2.6 vs 2.9ms(-10%)、decode_re 297 vs 304ms(反超 native)、decode_cache 291 vs 241ms(+21%——差距构成见 §8:attn 调用 host 发射/注册包装 ~120µs·call⁻¹,设备 kernel 时间已接近 native)。register_a1()返回的 lib 列表不被持有会随 GC 失效(早期多支探针 "ours" 实为 native,已作废);修正后同进程 A/B 显示 prefill ours 反超(382.8 vs 489.7µs)、decode 落后 ~39µs、设备时间 native 189.2 vs ours 直调 196.5µs → 差距主要在 host 侧(triton launch 83.7 + 分配 19.4 + python ~34)。example.py双端三层全绿 · Ascend 门禁 FAIL 0 · WARN 0(triton 0.544 vs 基线 0.504,+7.9%)。2026-10-02 update: P800 exact-P fast path
88a3b57replaces the P800 production selection withkernel/p800_fast_level.py, while preserving the operator's exact(output, attention_probs)contract:The fast path applies to fp16/bf16 no-mask/no-dropout calls. fp32, masks, dropout, and direct autograd construction fall back to
torch_level; A1 continues to use the custom mathematical backward.Updated P800 validation
p800 @ cuda:1)Updated P800 fp16 performance
A non-equivalent no-P
F.sdpareference remains 1.98–4.46x faster because it does not materializeP; this is documented separately rather than used as a same-contract speedup. No same-contract A100 private-math measurement is available yet.2026-10-02 update: FlagOS end-to-end test
944b050addsops/sdpa_math/script/e2e_flagos.pyand packages the operator asops.sdpa_math.Workload
aten::_scaled_dot_product_attention_mathops.sdpa_math.register_a1onAutogradCUDA+CUDAE2E correctness
E2E latency
Shared-machine repeated runs show:
dprobsentropy pathThe model-level gain is diluted by GEMM/MLP work. Training can consume the fast-forward gain through the custom mathematical backward, especially the probability-map gradient path; this is recorded as the next optimization target rather than hidden.
Raw report:
ops/sdpa_math/reports/e2e_flagos_p800-kunlunxin.json.2026-10-07 update: P800 fused backward and E2E A/B
4671389extends the P800 fast path into A1 backward.Implementation
doutusesaten::_scaled_dot_product_efficient_attention_backward.dprobscomputes the exact softmax Jacobian from the already materialized probability map.SDPA_MATH_P800_FUSED_BWD=0can disable the new branch for attribution.Correctness
Updated FlagOS E2E
The mini-LLM E2E now separates ordinary CE training from CE plus probability-map entropy training.
Fused-backward off/on A/B:
E2E correctness remains green: logits L∞ 0.015625, top-1 agreement 99.375%, greedy sequence exact match, and selected gradient L∞ values remain below 2e-4.