基于 SOC 职业分类
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/mindspore-ai/akg --skill triton-cuda-debugging命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
正在显示 SKILL.md
矩阵乘法矩阵乘法 A[M, K] @ B[K, N] = C[M, N]中,大K维度矩阵乘法(K>>M,N)优化:针对M/N较小但K极大(如M=N=256,K=131072)的场景,Split-K切分K维度并行化、Workspace+Reduce替代全局同步,实现显著性能提升
Triton Ascend hard API restrictions and forbidden syntax. MUST-follow rules that apply to every kernel: forbidden control flow (return/break/continue/lambda/while), tensor slice/index restrictions, scalar conversion rules, BLOCK_SIZE upper bound. Violating any of these produces a compile or runtime error on Ascend.
Triton Ascend 性能优化通用策略: BLOCK_SIZE 选择 (1024-2048 for elementwise, must be <65536), grid configuration (use VEC_CORE_NUM / CUBE_CORE_NUM, 2D/3D grid for matmul / conv / reduce, 1D grid + inner loop for elementwise / pointwise), 256B alignment for memory transfers, autotune block-size patterns, fp16 / fp32 precision conversion. Bind via keywords like matmul, elementwise, reduce, block_size, grid, autotune, alignment, fp16, fp32, tile, interleaved-loop, cube-core, vec-core.
| name | triton-cuda-debugging |
| description | Triton CUDA 调试排查清单和常见错误速查表,包括编译错误、运行时错误、精度问题和性能问题的诊断方法。适用于 CUDA 内核代码出现错误需要定位原因、或需要验证代码正确性的调试场景 |
| category | implementation |
| version | 1.0.0 |
| metadata | {"backend":"cuda","dsl":"triton_cuda"} |
.contiguous() 确保内存连续?tl.make_block_ptr?tl.constexpr 是否只在内核参数中使用?tl.atomic_add 等)?| 错误类型 | 典型症状 | 常见原因 | 解决方案 |
|---|---|---|---|
| Return 语句 | 编译失败 | Kernel 中使用 return | 移除 return,使用 mask 代替 |
| Break/Continue | 编译失败 | 不支持控制流跳转 | 用 mask 或重构逻辑 |
| Lambda 表达式 | 编译失败 | 不支持 lambda | 改用普通函数或内联 |
| 类型错误 | 编译失败 | constexpr 类型不匹配 | 检查 tl.constexpr 声明 |
| 错误类型 | 典型症状 | 常见原因 | 解决方案 |
|---|---|---|---|
| 内存越界 | CUDA error | 缺少 mask | 添加 mask 或 boundary_check |
| 形状不匹配 | 维度错误 | stride 计算错误 | 检查 stride 参数 |
| 非法内存访问 | Segfault | 指针计算错误 | 验证偏移计算 |
| 共享内存溢出 | Launch failed | num_stages 过大 | 减少 num_stages |
| 错误类型 | 典型症状 | 常见原因 | 解决方案 |
|---|---|---|---|
| NaN/Inf | 结果异常 | Softmax 溢出 | 减去最大值 |
| 精度损失 | 结果不准确 | 全程使用 fp16 累加 | 使用 float32 累加 |
| 除零错误 | NaN | 方差或和为零 | 添加 eps |
| 负数开方 | NaN | 方差为负 | tl.maximum(var, 0.0) |
| 问题类型 | 典型症状 | 常见原因 | 解决方案 |
|---|---|---|---|
| 性能差 | 比 PyTorch 慢 | 未使用 autotune | 添加 autotune |
| 带宽低 | 内存受限 | 非合并访问 | 确保合并访问 |
| Occupancy 低 | GPU 利用率低 | 寄存器/共享内存超限 | 减小 BLOCK_SIZE |
| L2 缓存差 | MatMul 性能低 | 未使用 Grouped Ordering | 添加 L2 缓存优化 |
步骤:
常见修复:
# 错误:使用 return
@triton.jit
def kernel(ptr, n, BLOCK: tl.constexpr):
pid = tl.program_id(0)
if pid >= n:
return # 编译错误!
# ...
# 正确:使用 mask
@triton.jit
def kernel(ptr, n, BLOCK: tl.constexpr):
pid = tl.program_id(0)
offsets = pid * BLOCK + tl.arange(0, BLOCK)
mask = offsets < n
data = tl.load(ptr + offsets, mask=mask, other=0.0)
# ... 所有代码在同一层级
步骤:
调试技巧:
# 打印调试信息(host 侧)
print(f"Grid: {grid}, BLOCK_SIZE: {BLOCK_SIZE}")
print(f"Shape: {input_tensor.shape}, Stride: {input_tensor.stride()}")
print(f"Contiguous: {input_tensor.is_contiguous()}")
步骤:
验证方法:
# 与 PyTorch 原生实现对比
output_triton = model_new(x)
output_torch = torch.softmax(x, dim=-1) # 或其他原生实现
diff = (output_triton - output_torch).abs().max()
print(f"Max diff: {diff.item()}")
assert diff < 1e-5, "Results mismatch!"
步骤:
.contiguous())性能分析:
import time
# 预热
for _ in range(10):
_ = model(x)
# 测试
torch.cuda.synchronize()
start = time.time()
for _ in range(100):
_ = model(x)
torch.cuda.synchronize()
elapsed = time.time() - start
print(f"Average time: {elapsed/100*1000:.2f} ms")
错误代码:
numerator = tl.exp(x) # 可能溢出
修复:
max_val = tl.max(x, axis=0)
x_stable = x - max_val
numerator = tl.exp(x_stable)
错误代码:
# 每个线程跳跃访问
offsets = pid + tl.arange(0, BLOCK_SIZE) * stride
data = tl.load(ptr + offsets)
修复:
# 连续访问
offsets = pid * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)
data = tl.load(ptr + offsets, mask=offsets < n)
错误代码:
triton.Config({...}, num_stages=8, num_warps=8) # 共享内存不足
修复:
triton.Config({...}, num_stages=3, num_warps=4) # 减少 stage 数
# 分析 kernel 性能
ncu --set full python script.py
# 分析内存带宽
ncu --metrics sm__throughput.avg.pct_of_peak_sustained_elapsed python script.py
# 检查内存错误
compute-sanitizer python script.py
# 大数据难以调试,先用小数据
x_small = torch.randn(4, 8, device='cuda', dtype=torch.float16)
output = model(x_small)
print(output) # 手动验证结果
# 始终与 PyTorch 原生实现对比
torch.testing.assert_close(output_triton, output_torch, rtol=1e-4, atol=1e-5)
调试 Triton-CUDA 代码的关键:
最佳实践: 先保证正确性,再优化性能!