fix(autotune): 修复 @triton.heuristics 包裹 kernel 的 cache key 命名空间错误
无人认领0 条评论更新 2026-05-09
用到的专长:Triton 运行时与代码生成、autotune 机制、cache 管理
目标:修复 flex_gemm/utils/autotuner.py 中 _get_function_cache_key 函数,使其正确处理 @triton.heuristics 包裹的 kernel,确保 cache key 始终为 flex_gemm.kernels.triton.spconv.<file>.<kernel_name> 格式。
为什么值得长期做:autotune cache 是 FlexGEMM 性能核心(安装时合并到 ~/.flex_gemm/),cache key 不一致会导致 heuristic-wrapped kernel 的缓存失效或污染,直接影响推理性能。此 bug 涉及代码生成与 Triton 运行时交互,是候选人 kernel 专长的直接应用。
第一个 PR 的边界:仅修改 flex_gemm/utils/autotuner.py 的 _get_function_cache_key 函数 + 新增 tests/test_autotune_cache_key.py。不改动现有 cache 格式或 kernel 代码。
认领留言(英文,可直接贴到 Issue)
Hi @JeffreyXiang, I'd like to work on fixing the inconsistent autotune cache key issue (#26). The root cause is clear: when a kernel is wrapped with @triton.heuristics, the Heuristics object's __module__ is 'triton.runtime.autotuner' instead of the actual kernel module. I'll fix _get_function_cache_key to unwrap Heuristics and get the original JITFunction's module. Before I start: should this fix target main or dev/all_triton branch? I'll have a PR ready within a few days.
大致实施方案
- 阅读 flex_gemm/utils/autotuner.py 第 359-362 行及周边代码,理解 _get_function_cache_key 逻辑
- 检查 triton.runtime.autotuner.Heuristics 类的 __module__ 属性,确认其值为 triton.runtime.autotuner
- 修改 _get_function_cache_key:当 fn 是 Heuristics 实例时,尝试从 fn.fn 或 fn.inner_fn 获取原始 JITFunction 的 __module__
- 添加单元测试:mock 一个 @triton.heuristics 包裹的函数,验证 cache key 格式正确
- 在 dev/all_triton 分支上测试(或 cherry-pick 到 main 验证兼容性)
可能涉及的目录或文件
- flex_gemm/utils/autotuner.py(核心修改)
- tests/ 下新增 test_autotune_cache_key.py(新增)
- 可能需要检查 flex_gemm/kernels/triton/spconv/ 下哪些 kernel 使用了 @triton.heuristics
验收方式
- Mac 上运行:python -c 'from flex_gemm.utils.autotuner import _get_function_cache_key; ...' 验证 key 格式
- 新增 tests/test_autotune_cache_key.py(新增),mock Heuristics 对象测试
- 检查现有 autotune_cache.json 中是否有 triton.runtime.autotuner.* 前缀的条目
开工前问题与风险
向维护者确认
- 此修复应基于 main 还是 dev/all_triton 分支?Issue 标题标注了 dev/all_triton,但 main 可能也有同样问题
- 是否有其他 kernel 装饰器组合(如 @triton.heuristics + @triton.autotune)需要同时处理?
风险
- Heuristics 对象的内部结构可能随 Triton 版本变化,需做版本兼容
- 修改 cache key 格式可能导致现有用户的 autotune_cache.json 失效(需文档说明)