autotune#181
Conversation
Ascend 自动调优使用说明本文档面向算子使用者,说明如何用 Ascend autotune 自动搜索 tile block 参数和编译参数。 1. 最简单配置先在定义 import triton
import triton.language as tl
import triton.backends.dicp_triton.ascend_autotune_hooks # noqa: F401然后声明需要搜索的 tile block 参数,例如 ATTENTION_SEARCH_HINTS = {
"search_params": {
"params": ["BLOCK_M", "BLOCK_N"],
},
}在 kernel 上使用: @triton.autotune(
configs=[],
key=["N_CTX", "HEAD_DIM"],
hints=ATTENTION_SEARCH_HINTS,
)
@triton.jit
def attention_kernel(
Q, K, V, Out,
N_CTX: tl.constexpr,
HEAD_DIM: tl.constexpr,
BLOCK_M: tl.constexpr,
BLOCK_N: tl.constexpr,
):
...这里推荐写
这就是最小推荐用法。它会自动做两类搜索:
2. 调用方式调用 kernel 时不需要传 grid = lambda meta: (
triton.cdiv(N_CTX, meta["BLOCK_M"]),
)
attention_kernel[grid](
Q, K, V, Out,
N_CTX,
HEAD_DIM,
)第一次遇到新的 3. 可选:控制 block 搜索范围默认搜索值为: [16, 32, 64, 128, 256, 512, 1024, 2048]如果想缩小范围,可以指定 ATTENTION_SEARCH_HINTS = {
"search_params": {
"params": ["BLOCK_M", "BLOCK_N"],
"values": [32, 64, 128, 256],
},
}也可以调整最终进入编译参数搜索的 shape 数量: ATTENTION_SEARCH_HINTS = {
"search_params": {
"params": ["BLOCK_M", "BLOCK_N"],
"shape_final_top_k": 3,
},
}4. 可选:打开调试输出Python 中打开 search debug: ATTENTION_SEARCH_HINTS = {
"search_params": {
"params": ["BLOCK_M", "BLOCK_N"],
"debug": True,
},
}环境变量打开 autotune 结果和 timing 输出: export TRITON_PRINT_AUTOTUNING=1
export TRITON_PRINT_AUTOTUNING_TIMINGS=15. 可选:手写候选 config如果不想自动搜索 block 参数,也可以手写候选 @triton.autotune(
configs=[
triton.Config({"BLOCK_M": 64, "BLOCK_N": 64}),
triton.Config({"BLOCK_M": 128, "BLOCK_N": 64}),
triton.Config({"BLOCK_M": 128, "BLOCK_N": 128}),
],
key=["N_CTX", "HEAD_DIM"],
)
@triton.jit
def attention_kernel(..., BLOCK_M: tl.constexpr, BLOCK_N: tl.constexpr):
...这种模式只在给定候选里选最快;推荐优先使用 6. 注意事项
|
No description provided.