CuTile 自动调优
使用 exhaustive_search API,并通过“一次性调优/缓存/直接启动”模式为 CuTile 内核添加自动调优功能。
说明
- 分类:使用决策树确定搜索维度(仅占用率或完整 tile 搜索)。
- 设计搜索空间:选择匹配模板,通过架构过滤器将配置控制在 ≤30 个。
- 实现:添加
exhaustive_search+ 缓存 +ct.launch;原地操作使用拆分缓冲。 - 测试:验证自动调优开启及
DISABLE_AUTOTUNE=1时的正确性。 - 验证:与固定最佳配置进行 A/B 对比。
- 缩减:剪枝无效配置,目标每架构 ≤8 个。
快速参考(仅占用率)
from types import SimpleNamespace
from cuda.tile.tune import exhaustive_search
import cuda.tile as ct
def _my_autotune_configs():
for occ in [1, 2, 4, 8]:
yield SimpleNamespace(occupancy=occ)
_autotune_cache = {}
def my_op(x, output):
stream = torch.cuda.current_stream()
NUM_SM = torch.cuda.get_device_properties(x.device).multi_processor_count
cache_key = (x.shape, x.dtype, str(x.device))
if cache_key not in _autotune_cache:
configs = list(_my_autotune_configs())
result = exhaustive_search(
configs, stream,
grid_fn=lambda cfg: (min(NUM_SM * cfg.occupancy, M), 1, 1),
kernel=my_kernel,
args_fn=lambda cfg: (x, output),
hints_fn=lambda cfg: {"occupancy": cfg.occupancy},
)
best_cfg = result.best.config
tuned_kernel = my_kernel.replace_hints(occupancy=best_cfg.occupancy)
_autotune_cache[cache_key] = (best_cfg, tuned_kernel)
cfg, tuned_kernel = _autotune_cache[cache_key]
grid = (min(NUM_SM * cfg.occupancy, M), 1, 1)
ct.launch(stream, grid, tuned_kernel, (x, output))
关键规则:缓存配置与内核对象;原地内核使用拆分缓冲;exhaustive_search 需要 Sequence;搜索空间必须包含原始固定配置。
决策树
- 计算密集型且有多个可调维度 → 完整搜索(
TILE_M × TILE_N × TILE_K × occupancy × num_ctas) - 双 GEMM 融合 → 低占用率 1–2,保守 tile
- 均衡型 / 内存密集型 → 仅占用率搜索
[1,2,4,8]
常见陷阱
- 原地内核未使用拆分缓冲导致数据损坏
- 自动调优编译超时(配置过多)
- 冷缓存导致性能偏差
- NCU 性能分析干扰调优
search_space生成器耗尽- FP8 精度损失
- 热路径上重复
replace_hints导致重编译
参考文档
- API 参考、工作流、陷阱、参数设计、搜索策略、模板、硬件约束等详细内容见
references/目录。
范围
仅涵盖自动调优配置(搜索空间、exhaustive_search、缓存、ct.launch、DISABLE_AUTOTUNE 回退),不修改内核代码、数学标志、性能提示、内存访问模式、代码生成或算法。