防作弊架构#
KernelGenBench 采用三层防作弊机制,防止基准规避行为,确保生成的内核实际执行计算。
概述#
防作弊架构防范生成代码可能出现的以下「作弊」行为:
调用已有 API 而非实现计算逻辑
绕过 Triton 编译
使用隐藏的缓存机制
L1:AST 静态扫描#
目的#
Enforce a whitelist-based approach: most torch.* API calls are forbidden.
Only tensor creation, dtype helpers, and constants are allowed.
方法#
解析生成的抽象语法树(AST)以检测并阻止:
Whitelist (allowed torch APIs):
torch.empty, torch.zeros, torch.randn, torch.range, torch.float16, etc.
Detected patterns (blocked):
阻止模式 |
原因 |
|---|---|
|
Prevents using torch.sum/mean/mm/reductions |
|
Prevents input sniffing from test harness |
|
Prevents raw memory access |
Module-level |
Prevents inter-iteration result caching |
|
Using pre-existing implementations |
|
Dynamic code execution |
Import alias / |
Catches obfuscation attempts |
实现#
# Blocked calls are detected via AST parsing
# Any attempt to call blacklisted APIs results in immediate rejection
L2:幽灵回放#
目的#
验证 Triton 内核确实被执行,而非被绕过。
方法#
正常执行内核,捕获输出
在内存中将
@triton.jit装饰的函数替换为空操作使用相同输入重新执行
比较输出
逻辑#
如果输出完全相同,则 Triton 内核从未被调用 → 检测到作弊
如果输出不同,则内核确实被执行 → 有效
L3:硬件分析#
目的#
在硬件层面确认 Triton 特定的执行。
方法#
使用 torch.profiler 验证底层追踪日志中是否存在 Triton 特定的签名。
可用性#
平台 |
L3 支持 |
|---|---|
NVIDIA |
✓ |
非 NVIDIA |
✗ |
非 NVIDIA 平台由于缺乏等效的分析工具,仅依赖 L1 和 L2。
验证流程#
Generated Kernel
│
▼
┌─────────────┐
│ L1: AST Scan│─── Fail ──► Reject
└─────────────┘
│ Pass
▼
┌─────────────┐
│ L2: Ghost │─── Fail ──► Reject
│ Replay │
└─────────────┘
│ Pass
▼
┌─────────────┐
│ L3: Profile │─── Fail ──► Reject
│ (NVIDIA) │
└─────────────┘
│ Pass
▼
Accept
防作弊的重要性#
如果没有防作弊措施,模型可能会:
无需实际计算即可获得高「精度」
掩盖内核生成能力不足
使基准测试结果失效
KernelGenBench 确保评估结果反映真实的内核生成能力。