Open
update model_opti_list: 引入 common.yaml 公共环境配置,统一模型清单填写规则 #48
WNMAdd on 9 days ago
update model_opti_list: 引入 common.yaml 公共环境配置,统一模型清单填写规则 #48
Open
Total: 45 files changed+6712-20
| @@ -26,7 +26,15 @@ custom_op放置了自定义算子开发工程,包含自定义算子和对应 | |||
| 26 | 对应CANN版本:A3 AscendC 基线 9.1.0 及以上;A5 GE 交付件要求 9.3.0 辅流接口 | 26 | 对应CANN版本:A3 AscendC 基线 9.1.0 及以上;A5 GE 交付件要求 9.3.0 辅流接口 |
| 27 | 是否回合CANN版本:否 | 27 | 是否回合CANN版本:否 |
| 28 | 适配芯片版本:Ascend910_93(实测 Ascend910_9382)、Ascend950PR(dav-3510);A5 GE 当前仅支持 FP16、N1=14/N2=2/D=64 | 28 | 适配芯片版本:Ascend910_93(实测 Ascend910_9382)、Ascend950PR(dav-3510);A5 GE 当前仅支持 FP16、N1=14/N2=2/D=64 |
| 29 | - 4、xxx | 29 | + 4、causal_conv1d |
| 30 | + 基本功能:GDN 因果深度卷积(causal depthwise conv + 可选 SiLU)自定义算子;dim-last 输入,支持 prefill / decode 两种运行模式 | ||
| 31 | + 算子功能:将 in_proj 后的 `(B, S, D)` 数据直接做 causal conv(weight `(K, D)`,`K∈[2,4]`,动态 S),可选融合 SiLU;decode 模式读写 convStates 并支持 cacheIndices / numAcceptedTokens;另提供 packed QKV 融合路径 | ||
| 32 | + 插件功能:无(GE 自定义算子,随图编译/执行) | ||
| 33 | + 适配模型:onejev(GDN 类模型) | ||
| 34 | + 对应CANN版本:CANN9.2.1 | ||
| 35 | + 是否回合CANN版本:否 | ||
| 36 | + 适配芯片版本:Ascend910B、Ascend910_93(Ascend910_9382 实测);Ascend950 arch35 kernel 路径保留但默认未启用 | ||
| 37 | + 5、xxx | ||
| 30 | 基本功能: | 38 | 基本功能: |
| 31 | 算子工程: | 39 | 算子工程: |
| 32 | 插件工程: | 40 | 插件工程: |
| @@ -0,0 +1,6 @@ | |||
| 1 | +build/ | ||
| 2 | +build_out/ | ||
| 3 | +*.o | ||
| 4 | +*.so | ||
| 5 | +*.run | ||
| 6 | +__pycache__/ | ||
| @@ -0,0 +1,58 @@ | |||
| 1 | +# ---------------------------------------------------------------------------------------------------------- | ||
| 2 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | +# ---------------------------------------------------------------------------------------------------------- | ||
| 10 | + | ||
| 11 | +cmake_minimum_required(VERSION 3.16.0) | ||
| 12 | +project(causal_conv1d_op_prj) | ||
| 13 | +find_package(ASC REQUIRED) | ||
| 14 | + | ||
| 15 | +# ============================================================================= | ||
| 16 | +# 架构代际配置 | ||
| 17 | +# arch22: ascend910b -> causal_conv1d_arch22.cpp | ||
| 18 | +# ============================================================================= | ||
| 19 | + | ||
| 20 | +set(ARCH22_COMPUTE_UNITS ascend910b ascend910_93) | ||
| 21 | + | ||
| 22 | +# 如果用户未指定,则编译所有支持的芯片 | ||
| 23 | +if(NOT DEFINED ASCEND_COMPUTE_UNIT OR ASCEND_COMPUTE_UNIT STREQUAL "") | ||
| 24 | + set(ASCEND_COMPUTE_UNIT ${ARCH22_COMPUTE_UNITS}) | ||
| 25 | +endif() | ||
| 26 | + | ||
| 27 | +set(package_name causal_conv1d_custom) | ||
| 28 | + | ||
| 29 | +npu_op_package(${package_name} | ||
| 30 | + TYPE RUN | ||
| 31 | + CONFIG | ||
| 32 | + INSTALL_PATH ${CMAKE_BINARY_DIR} | ||
| 33 | +) | ||
| 34 | + | ||
| 35 | + | ||
| 36 | +if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/op_host") | ||
| 37 | + add_subdirectory(op_host) | ||
| 38 | +endif() | ||
| 39 | + | ||
| 40 | +if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/op_kernel") | ||
| 41 | + add_subdirectory(op_kernel) | ||
| 42 | +endif() | ||
| 43 | + | ||
| 44 | +if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/op_graph") | ||
| 45 | + add_subdirectory(op_graph) | ||
| 46 | +endif() | ||
| 47 | + | ||
| 48 | +if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/framework") | ||
| 49 | + add_subdirectory(framework) | ||
| 50 | +endif() | ||
| 51 | + | ||
| 52 | +# ============================================================================= | ||
| 53 | +# IMPORTANT: cmake's default 'all' target does NOT build 'binary'! | ||
| 54 | +# To build kernel binary, use one of: | ||
| 55 | +# 1. bash build.sh --soc=<soc> (Recommended) | ||
| 56 | +# 2. cmake --build . --target all binary package | ||
| 57 | +# ============================================================================= | ||
| 58 | +message(WARNING "cmake 'make' does NOT build kernel binary by default. Use: bash build.sh --soc=<soc>") | ||
| @@ -0,0 +1,181 @@ | |||
| 1 | +# causal_conv1d | ||
| 2 | + | ||
| 3 | +## 产品支持情况 | ||
| 4 | + | ||
| 5 | +| 产品 | 是否支持 | | ||
| 6 | +| :----------------------------------------- | :------:| | ||
| 7 | +| <term>Ascend 950PR/Ascend 950DT</term> | × | | ||
| 8 | +| <term>Atlas A3 训练系列产品/Atlas A3 推理系列产品</term> | √ | | ||
| 9 | +| <term>Atlas A2 训练系列产品/Atlas A2 推理系列产品</term> | √ | | ||
| 10 | +| <term>Atlas 200I/500 A2 推理产品</term> | × | | ||
| 11 | +| <term>Atlas 推理系列产品</term> | × | | ||
| 12 | +| <term>Atlas 训练系列产品</term> | × | | ||
| 13 | + | ||
| 14 | +> **说明**:本工程默认按 ARCH22(`ascend910b` / `ascend910_93`)编译 kernel,已在 Ascend910_93(Ascend910_9382)CANN 9.2.1 实测验证。`op_kernel/arch35/` 保留了 A5(dav-3510)kernel 路径,但 `op_kernel/CMakeLists.txt` 默认未启用,A5 需另行打通构建并验证。 | ||
| 15 | + | ||
| 16 | +## 功能说明 | ||
| 17 | + | ||
| 18 | +CausalConv1d 是 GDN(Gated Delta Net)中的因果深度卷积算子,将 in_proj 矩阵乘之后的 dim-last `(B, S, D)` 数据直接做 causal depthwise conv(可选融合 SiLU),避免额外的 transpose / 激活算子。权重布局为 `(K, D)`,卷积宽度 `K ∈ [2, 4]`,序列长度 `S` 支持动态。 | ||
| 19 | + | ||
| 20 | +算子同时覆盖两种运行模式: | ||
| 21 | + | ||
| 22 | +- **prefill / fn(`runMode=0`)**:支持 3D batch 模式 `(batch, seqlen, dim)` 与 2D varlen 模式 `(cu_seqlen, dim)`(通过 `queryStartLoc` 切分请求);可从 `convStates` 读取初始历史状态,并通过 `initialStateMode` 控制是否初始化状态。 | ||
| 23 | +- **decode / update(`runMode=1`)**:逐 token 更新,读取并回写 `convStates`;支持 `cacheIndices` 做状态槽位映射、`numAcceptedTokens` 做投机解码多 token 更新。 | ||
| 24 | + | ||
| 25 | +`activationMode=2` 额外提供 packed QKV 融合路径:在卷积 + SiLU 后对 Q/K 做 per-head RMSNorm 并以 FP16 打包输出(受 `packedQDim/packedKDim/packedVDim/packedHeadDim` 约束),用于 GDN 的 QKV 混合输出。 | ||
| 26 | + | ||
| 27 | +### 计算公式 | ||
| 28 | + | ||
| 29 | +对每个通道 `d`,因果卷积(`width=K`,历史不足部分补 0 或取 `convStates` 中的历史): | ||
| 30 | + | ||
| 31 | +$$ | ||
| 32 | +y[t, d] = \sum_{k=0}^{K-1} weight[k, d] \cdot x[t-(K-1)+k, d] + bias[d] | ||
| 33 | +$$ | ||
| 34 | + | ||
| 35 | +其中 `t-(K-1)+k < 0` 时取历史状态(prefill 由 `convStates` 提供,decode 由状态槽位提供)。 | ||
| 36 | + | ||
| 37 | +可选 SiLU 激活: | ||
| 38 | + | ||
| 39 | +$$ | ||
| 40 | +y[t, d] = y[t, d] \cdot \mathrm{sigmoid}(y[t, d]) | ||
| 41 | +$$ | ||
| 42 | + | ||
| 43 | +`activationMode=2` 时,在 SiLU 之后对 Q/K 各 head 做 RMSNorm 并按 head 组合打包为 FP16 输出。 | ||
| 44 | + | ||
| 45 | +### 计算示例 | ||
| 46 | + | ||
| 47 | +`width=4`、`dim=1`、无历史状态,输入 `x = [x0, x1, x2, x3]`,`weight = [w0, w1, w2, w3]`: | ||
| 48 | + | ||
| 49 | +``` | ||
| 50 | +y[0] = w0*0 + w1*0 + w2*0 + w3*x0 | ||
| 51 | +y[1] = w0*0 + w1*0 + w2*x0 + w3*x1 | ||
| 52 | +y[2] = w0*0 + w1*x0 + w2*x1 + w3*x2 | ||
| 53 | +y[3] = w0*x0 + w1*x1 + w2*x2 + w3*x3 | ||
| 54 | +``` | ||
| 55 | + | ||
| 56 | +若 `activationMode=1`,每个 `y[t]` 再乘以 `sigmoid(y[t])`。 | ||
| 57 | + | ||
| 58 | +### 使用场景 | ||
| 59 | + | ||
| 60 | +GDN / 线性注意力类 LLM 的 causal depthwise conv(conv + SiLU)融合;常见于 onejev 等模型的 prefill 与 decode 路径。 | ||
| 61 | + | ||
| 62 | +## 参数说明 | ||
| 63 | + | ||
| 64 | +### 输入参数 | ||
| 65 | + | ||
| 66 | +输入顺序与 IR 定义一致:`x, weight, bias, convStates, queryStartLoc, cacheIndices, initialStateMode, numAcceptedTokens`。 | ||
| 67 | + | ||
| 68 | +| 参数名 | 输入/输出 | 描述 | 数据类型 | 数据格式 | 维度(shape) | 非连续Tensor | | ||
| 69 | +|-------|----------|------|---------|---------|------------|-------------| | ||
| 70 | +| x | 输入 | 待卷积数据,dim-last;prefill 3D 或 2D varlen,decode 2D `(batch, dim)` | FLOAT16、BFLOAT16 | ND | 3D `(batch, seqlen, dim)` / 2D `(cu_seqlen, dim)` / 2D decode `(batch, dim)` | × | | ||
| 71 | +| weight | 输入 | 深度卷积权重 | 与 x 相同 | ND | `(width, dim)`,`width ∈ [2, 4]` | × | | ||
| 72 | +| bias | 可选输入 | 逐通道偏置 | 与 x 相同 | ND | `(dim,)` | × | | ||
| 73 | +| convStates | 输入 | 卷积历史状态槽位;prefill 可传 dummy 零状态 | 与 x 相同 | ND | `(num_cache_lines, state_len, dim)`,`state_len >= width-1` | × | | ||
| 74 | +| queryStartLoc | 可选输入 | varlen 请求边界(累积前缀和) | INT64 | ND | `(batch+1,)`,首元素 0、末元素等于 cu_seqlen | × | | ||
| 75 | +| cacheIndices | 可选输入 | 请求到 convStates 槽位的映射 | INT64 | ND | `(batch,)`,取值 `[0, num_cache_lines)` 或 `padSlotId` | × | | ||
| 76 | +| initialStateMode | 可选输入 | prefill 每个请求是否初始化状态,1=初始化 | INT64 | ND | `(batch,)`,取值 0/1 | × | | ||
| 77 | +| numAcceptedTokens | 可选输入 | decode / 投机解码每个请求接受 token 数 | INT64 | ND | `(batch,)`,取值 ≥0 且不超过 seqlen / 2D token 数 | × | | ||
| 78 | + | ||
| 79 | +### 输出参数 | ||
| 80 | + | ||
| 81 | +| 参数名 | 输入/输出 | 描述 | 数据类型 | 数据格式 | 维度(shape) | 非连续Tensor | | ||
| 82 | +|-------|----------|------|---------|---------|------------|-------------| | ||
| 83 | +| y | 输出 | 卷积(可选 SiLU)后的结果 | FLOAT16、BFLOAT16(缺省与 x 一致) | ND | 与 x 相同 | - | | ||
| 84 | + | ||
| 85 | +> `activationMode=2` 时输出为 packed QKV(FP16 或 BF16),布局由 `packedQDim/packedKDim/packedVDim/packedHeadDim` 决定。 | ||
| 86 | + | ||
| 87 | +### 属性参数 | ||
| 88 | + | ||
| 89 | +| 属性名 | 类型 | 默认值 | 含义 | | ||
| 90 | +|-------|------|-------|------| | ||
| 91 | +| activationMode | Int | 0 | 0=NONE,1=SILU,2=SILU_PACKED_QKV | | ||
| 92 | +| padSlotId | Int | -1 | `cacheIndices` 中表示无效/填充槽位的取值 | | ||
| 93 | +| runMode | Int | 0 | 0=prefill/fn,1=decode/update | | ||
| 94 | + | ||
| 95 | +## 约束说明 | ||
| 96 | + | ||
| 97 | +### shape 约束 | ||
| 98 | + | ||
| 99 | +- `x` 仅支持 2D 或 3D:2D 在 `runMode=0` 下为 varlen `(cu_seqlen, dim)`,在 `runMode=1` 下为 decode `(batch, dim)`;3D 为 `(batch, seqlen, dim)` | ||
| 100 | +- `weight` 必须为 2D `(width, dim)`,且 `width ∈ [2, 4]` | ||
| 101 | +- `convStates` 必须为 3D `(num_cache_lines, state_len, dim)`,`num_cache_lines > 0`,`state_len >= width-1` | ||
| 102 | +- `bias` 必须为 1D `(dim,)`;`queryStartLoc`、`cacheIndices`、`initialStateMode`、`numAcceptedTokens` 必须为 1D | ||
| 103 | +- `queryStartLoc.size == batch+1`,且 `queryStartLoc[0] == 0`、`queryStartLoc[last] == cu_seqlen`、序列非递减 | ||
| 104 | +- `cacheIndices.size == batch`;缺省时要求 `num_cache_lines >= batch`,按 identity 映射 | ||
| 105 | +- `initialStateMode.size == batch`;仅 `runMode=0` 支持 | ||
| 106 | +- `numAcceptedTokens.size == batch`;仅 `runMode=1` 支持,且当前要求 `width=4`;3D update 要求 `state_len >= (width-1) + (seqlen-1)` | ||
| 107 | +- `cu_seqlen` 不能超过 `int32` 索引上限 | ||
| 108 | + | ||
| 109 | +### 数据类型约束 | ||
| 110 | + | ||
| 111 | +- `x`、`weight`、`convStates`、`bias`、`y` 数据类型必须一致,支持 FLOAT16 / BFLOAT16 | ||
| 112 | +- `queryStartLoc`、`cacheIndices`、`initialStateMode`、`numAcceptedTokens` 必须为 INT64 | ||
| 113 | +- `activationMode=2` 仅支持 BF16 输入、FP16/BF16 输出 | ||
| 114 | + | ||
| 115 | +### 值域与对齐约束 | ||
| 116 | + | ||
| 117 | +- `dim` 必须按 32 字节对齐,即 `dim % 16 == 0`(FP16/BF16);`x`/`weight`/`convStates` 最后一维与 `bias` 长度必须一致 | ||
| 118 | +- `activationMode=2` 额外要求:`runMode=0`、2D varlen、`width=4`、无 bias、`qDim == kDim`、`headDim == 128`、Q/K/V 维度均按 128 对齐且 `qDim + kDim + vDim == dim` | ||
| 119 | +- `padSlotId` 之外的 `cacheIndices` 取值必须落在 `[0, num_cache_lines)` 且不超过 `int32` 范围 | ||
| 120 | +- `numAcceptedTokens` 必须非负;3D update 下不超过 `seqlen`,2D decode 下不超过 1 | ||
| 121 | + | ||
| 122 | +### 可选输入/内存约束 | ||
| 123 | + | ||
| 124 | +- GE 图上建议**连接前四个输入**(`x, weight, bias, convStates`)。可选的 `bias` 占位输入若在导出图中被压缩掉,会改变节点输入列表,导致 tiling 的 `convStates` 索引错位;bias 全零时可传零张量,数值精确等价 | ||
| 125 | +- prefill(`runMode=0`)不读取 `convStates` 内容,可传 `(1, width-1, dim)` 的零张量占位 | ||
| 126 | +- prefill 路径按 token 分块并在多核间映射;decode 路径按 `batch × dim` 分核,UB 预算不足时按 `baseDim` 切分 | ||
| 127 | +- `initialStateMode` 非空时使用状态快照 workspace,由 tiling 按 `num_cache_lines × state_len × dim × 2B` 申请 | ||
| 128 | + | ||
| 129 | +## 工程结构 | ||
| 130 | + | ||
| 131 | +``` | ||
| 132 | +causal_conv1d/ | ||
| 133 | +├── op_graph/ # 算子原型(REG_OP) | ||
| 134 | +│ └── causal_conv1d_proto.h | ||
| 135 | +├── op_host/ # OpDef、InferShape、Tiling | ||
| 136 | +│ ├── causal_conv1d_def.cpp | ||
| 137 | +│ ├── causal_conv1d_proto.cpp | ||
| 138 | +│ ├── causal_conv1d_tiling.cpp | ||
| 139 | +│ ├── causal_conv1d_tiling.h | ||
| 140 | +│ ├── causal_conv1d_tiling_planner.h | ||
| 141 | +│ ├── causal_conv1d_tiling_utils.h | ||
| 142 | +│ └── causal_conv1d_tiling_validation.h | ||
| 143 | +├── op_kernel/ # AscendC kernel | ||
| 144 | +│ ├── causal_conv1d.cpp | ||
| 145 | +│ ├── causal_conv1d.h | ||
| 146 | +│ ├── causal_conv1d_fn.h / causal_conv1d_fn_tasks.h | ||
| 147 | +│ ├── causal_conv1d_update.h | ||
| 148 | +│ ├── causal_conv1d_common.h | ||
| 149 | +│ ├── causal_conv1d_tiling_data.h / causal_conv1d_tiling_key.h | ||
| 150 | +│ └── arch35/ # A5 kernel 路径(默认未启用) | ||
| 151 | +├── compat/tiling_base/ # xllm 依赖的 tiling 兼容头(CANN 9.2.1 未自带) | ||
| 152 | +├── CMakeLists.txt | ||
| 153 | +└── build.sh | ||
| 154 | +``` | ||
| 155 | + | ||
| 156 | +## 与图/模型接入说明 | ||
| 157 | + | ||
| 158 | +- 本算子无 ONNX 插件,作为 GE 自定义算子参与构图、编译与执行;模型中节点名即 `CausalConv1d` | ||
| 159 | +- 输入按 IR 顺序连接:`x, weight, bias, convStates, queryStartLoc, cacheIndices, initialStateMode, numAcceptedTokens`,按运行模式选择性地提供可选输入 | ||
| 160 | +- prefill 只需保证前四个输入存在;decode 需要正确提供 `convStates`(及按需的 `cacheIndices` / `numAcceptedTokens`) | ||
| 161 | +- 算子安装到 `$ASCEND_OPP_PATH/vendors/causal_conv1d_custom`,由标准 vendor 扫描自动发现,无需手动 dlopen | ||
| 162 | + | ||
| 163 | +## 编译与安装 | ||
| 164 | + | ||
| 165 | +```bash | ||
| 166 | +source /usr/local/Ascend/ascend-toolkit/latest/set_env.sh | ||
| 167 | +bash build.sh --soc=ascend910_93 # A2 使用 --soc=ascend910b | ||
| 168 | +bash build/custom_opp_openEuler_aarch64.run --quiet | ||
| 169 | +``` | ||
| 170 | + | ||
| 171 | +- 重新编译前先执行 `bash build.sh --make_clean` | ||
| 172 | +- 产物:`build/custom_opp_<os>_<arch>.run`,安装后位于 `$ASCEND_OPP_PATH/vendors/causal_conv1d_custom` | ||
| 173 | +- 支持的 SoC:`ascend910b`、`ascend910_93`(`build.sh --list-socs` 可查看) | ||
| 174 | + | ||
| 175 | +## 来源与适配说明 | ||
| 176 | + | ||
| 177 | +- kernel 与 host 代码来自 `xllm_ops/xllm_ops/causal_conv1d`(CANN Open Software License Agreement v2.0) | ||
| 178 | +- 工程骨架参考本仓 `custom_op/grouped_broadcast` 的标准 op_graph/op_host/op_kernel 布局与 `npu_op_*` cmake API | ||
| 179 | +- `compat/tiling_base/` 为 vendored xllm 兼容头(CANN 9.2.1 未提供 `tiling_base`) | ||
| 180 | +- `op_host/CMakeLists.txt` 在 codegen 硬编码的 `-std=c++11` 之后追加 `-std=c++17`,以满足 xllm host 代码的 relaxed constexpr 需求,并补充 proto 库/打包尾部 | ||
| 181 | +- `op_graph/causal_conv1d_proto.h` 为 GE IR `REG_OP` 定义,与 `op_host/causal_conv1d_def.cpp` 的 OpDef 对齐 | ||
| @@ -0,0 +1,51 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file data_copy_transpose_tiling.h | ||
| 13 | + * \brief | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#pragma once | ||
| 17 | + | ||
| 18 | +#include <vector> | ||
| 19 | +#include <graph/tensor.h> | ||
| 20 | +#include "data_copy_transpose_tiling_def.h" | ||
| 21 | + | ||
| 22 | +namespace optiling { | ||
| 23 | + | ||
| 24 | +inline void GetDataCopyTransposeTiling(const ge::Shape &dstShape, const ge::Shape &srcShape, const uint32_t typeSize, | ||
| 25 | + optiling::CopyTransposeTiling &tiling) | ||
| 26 | +{ | ||
| 27 | + constexpr int64_t B_INDEX = 0; | ||
| 28 | + constexpr int64_t N_INDEX = 1; | ||
| 29 | + constexpr int64_t S_INDEX = 2; | ||
| 30 | + constexpr int64_t H_INDEX = 3; | ||
| 31 | + std::vector<int64_t> dstShapeInfo = dstShape.GetDims(); | ||
| 32 | + std::vector<int64_t> srcShapeInfo = srcShape.GetDims(); | ||
| 33 | + | ||
| 34 | + tiling.set_dstShapeB(dstShapeInfo[B_INDEX]); | ||
| 35 | + tiling.set_dstShapeN(dstShapeInfo[N_INDEX]); | ||
| 36 | + tiling.set_dstShapeS(dstShapeInfo[S_INDEX]); | ||
| 37 | + tiling.set_dstShapeH(dstShapeInfo[H_INDEX]); | ||
| 38 | + tiling.set_dstShapeHN(tiling.get_dstShapeH() / tiling.get_dstShapeN()); | ||
| 39 | + | ||
| 40 | + tiling.set_srcShapeB(srcShapeInfo[B_INDEX]); | ||
| 41 | + tiling.set_srcShapeN(srcShapeInfo[N_INDEX]); | ||
| 42 | + tiling.set_srcShapeS(srcShapeInfo[S_INDEX]); | ||
| 43 | + tiling.set_srcShapeHN(srcShapeInfo[H_INDEX]); | ||
| 44 | + tiling.set_originalShapeNLen(tiling.get_srcShapeHN() * typeSize); | ||
| 45 | + tiling.set_shapeSHValue(tiling.get_dstShapeS() * tiling.get_dstShapeH()); | ||
| 46 | + tiling.set_shapeNsValue(tiling.get_dstShapeN() * tiling.get_dstShapeS()); | ||
| 47 | + tiling.set_shapeNsnValue(tiling.get_dstShapeN() * tiling.get_srcShapeS() * tiling.get_srcShapeN()); | ||
| 48 | + tiling.set_shapeBHValue(tiling.get_dstShapeB() * tiling.get_dstShapeH()); | ||
| 49 | +} | ||
| 50 | + | ||
| 51 | +} // namespace optiling | ||
| @@ -0,0 +1,43 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file data_copy_transpose_tiling_def.h | ||
| 13 | + * \brief | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#pragma once | ||
| 17 | + | ||
| 18 | +#include <cstdint> | ||
| 19 | +#include <register/tilingdata_base.h> | ||
| 20 | + | ||
| 21 | +namespace optiling { | ||
| 22 | + | ||
| 23 | +BEGIN_TILING_DATA_DEF(CopyTransposeTiling) | ||
| 24 | +TILING_DATA_FIELD_DEF(uint32_t, dstShapeB); | ||
| 25 | +TILING_DATA_FIELD_DEF(uint32_t, dstShapeN); | ||
| 26 | +TILING_DATA_FIELD_DEF(uint32_t, dstShapeS); | ||
| 27 | +TILING_DATA_FIELD_DEF(uint32_t, dstShapeHN); | ||
| 28 | +TILING_DATA_FIELD_DEF(uint32_t, dstShapeH); | ||
| 29 | +TILING_DATA_FIELD_DEF(uint32_t, srcShapeB); | ||
| 30 | +TILING_DATA_FIELD_DEF(uint32_t, srcShapeN); | ||
| 31 | +TILING_DATA_FIELD_DEF(uint32_t, srcShapeS); | ||
| 32 | +TILING_DATA_FIELD_DEF(uint32_t, srcShapeHN); | ||
| 33 | +TILING_DATA_FIELD_DEF(uint32_t, originalShapeNLen); | ||
| 34 | +TILING_DATA_FIELD_DEF(uint32_t, shapeSHValue); | ||
| 35 | +TILING_DATA_FIELD_DEF(uint32_t, shapeNsValue); | ||
| 36 | +TILING_DATA_FIELD_DEF(uint32_t, shapeNsnValue); | ||
| 37 | +TILING_DATA_FIELD_DEF(uint32_t, invalidParamCopyTransposeTiling); | ||
| 38 | +TILING_DATA_FIELD_DEF(uint32_t, shapeBHValue); | ||
| 39 | +TILING_DATA_FIELD_DEF(uint32_t, paramsAlign); | ||
| 40 | +END_TILING_DATA_DEF; | ||
| 41 | +REGISTER_TILING_DATA_CLASS(CopyTransposeTilingOp, CopyTransposeTiling) | ||
| 42 | + | ||
| 43 | +} // namespace optiling | ||
| @@ -0,0 +1,73 @@ | |||
| 1 | +#ifndef OPS_BUILT_IN_OP_TILING_ERROR_LOG_H_ | ||
| 2 | +#define OPS_BUILT_IN_OP_TILING_ERROR_LOG_H_ | ||
| 3 | + | ||
| 4 | +#include <cstdio> | ||
| 5 | +#include <string> | ||
| 6 | + | ||
| 7 | +#include "tiling_base/tiling_util.h" | ||
| 8 | +#include "toolchain/slog.h" | ||
| 9 | + | ||
| 10 | +#define OP_LOGI(opname, ...) | ||
| 11 | +#define OP_LOGD(opname, ...) | ||
| 12 | + | ||
| 13 | +#define OP_LOGW(opname, ...) \ | ||
| 14 | + do { \ | ||
| 15 | + (void)(opname); \ | ||
| 16 | + std::printf("[WARN] "); \ | ||
| 17 | + std::printf(__VA_ARGS__); \ | ||
| 18 | + std::printf("\n"); \ | ||
| 19 | + } while (0) | ||
| 20 | + | ||
| 21 | +#define OP_LOGE_WITHOUT_REPORT(opname, ...) \ | ||
| 22 | + do { \ | ||
| 23 | + (void)(opname); \ | ||
| 24 | + std::printf("[ERRORx] "); \ | ||
| 25 | + std::printf(__VA_ARGS__); \ | ||
| 26 | + std::printf("\n"); \ | ||
| 27 | + } while (0) | ||
| 28 | + | ||
| 29 | +#define OP_LOGE(opname, ...) \ | ||
| 30 | + do { \ | ||
| 31 | + (void)(opname); \ | ||
| 32 | + std::printf("[ERROR] "); \ | ||
| 33 | + std::printf(__VA_ARGS__); \ | ||
| 34 | + std::printf("\n"); \ | ||
| 35 | + } while (0) | ||
| 36 | + | ||
| 37 | +namespace optiling { | ||
| 38 | + | ||
| 39 | +#define VECTOR_INNER_ERR_REPORT_TILIING(op_name, err_msg, ...) \ | ||
| 40 | + do { \ | ||
| 41 | + OP_LOGE_WITHOUT_REPORT(op_name, err_msg, ##__VA_ARGS__); \ | ||
| 42 | + } while (0) | ||
| 43 | + | ||
| 44 | +#define OP_CHECK_IF(cond, log_func, expr) \ | ||
| 45 | + do { \ | ||
| 46 | + if (cond) { \ | ||
| 47 | + log_func; \ | ||
| 48 | + expr; \ | ||
| 49 | + } \ | ||
| 50 | + } while (0) | ||
| 51 | + | ||
| 52 | +#define OP_TILING_CHECK(cond, log_func, expr) \ | ||
| 53 | + do { \ | ||
| 54 | + if (cond) { \ | ||
| 55 | + log_func; \ | ||
| 56 | + expr; \ | ||
| 57 | + } \ | ||
| 58 | + } while (0) | ||
| 59 | + | ||
| 60 | +#define OP_CHECK_NULL_WITH_CONTEXT(context, ptr) \ | ||
| 61 | + do { \ | ||
| 62 | + if ((ptr) == nullptr) { \ | ||
| 63 | + OP_LOGE(context->GetNodeType(), "%s is null", #ptr); \ | ||
| 64 | + return ge::GRAPH_FAILED; \ | ||
| 65 | + } \ | ||
| 66 | + } while (0) | ||
| 67 | + | ||
| 68 | +} // namespace optiling | ||
| 69 | + | ||
| 70 | +using Ops::Xllm::CeilAlign; | ||
| 71 | +using Ops::Xllm::CeilDiv; | ||
| 72 | + | ||
| 73 | +#endif // OPS_BUILT_IN_OP_TILING_ERROR_LOG_H_ | ||
| @@ -0,0 +1,265 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file tiling_base.h | ||
| 13 | + * \brief | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#pragma once | ||
| 17 | + | ||
| 18 | +#include <sstream> | ||
| 19 | +#include <exe_graph/runtime/tiling_context.h> | ||
| 20 | +#include <graph/utils/type_utils.h> | ||
| 21 | +#include "tiling/platform/platform_ascendc.h" | ||
| 22 | +#include "tiling_base/error_log.h" | ||
| 23 | + | ||
| 24 | +#ifndef XLLM_OP_MODULE_ID | ||
| 25 | +#if defined(OP) | ||
| 26 | +#define XLLM_OP_MODULE_ID OP | ||
| 27 | +#else | ||
| 28 | +// CANN 9.1 中 OP_MODULE_ID 为 log.h 内 constexpr,9.0 中不存在;统一回退固定模块 ID 63(仅影响日志过滤,不影响功能) | ||
| 29 | +#define XLLM_OP_MODULE_ID 63 | ||
| 30 | +#endif | ||
| 31 | +#endif | ||
| 32 | + | ||
| 33 | +#ifdef ASCENDC_OP_TEST | ||
| 34 | +#define ASCENDC_EXTERN_C extern "C" | ||
| 35 | +#else | ||
| 36 | +#define ASCENDC_EXTERN_C | ||
| 37 | +#endif | ||
| 38 | + | ||
| 39 | +namespace Ops { | ||
| 40 | +namespace Xllm { | ||
| 41 | +namespace OpTiling { | ||
| 42 | + | ||
| 43 | +struct AiCoreParams { | ||
| 44 | + uint64_t ubSize = 0; | ||
| 45 | + uint64_t blockDim = 0; | ||
| 46 | + uint64_t aicNum = 0; | ||
| 47 | + uint64_t l1Size = 0; | ||
| 48 | + uint64_t l0aSize = 0; | ||
| 49 | + uint64_t l0bSize = 0; | ||
| 50 | + uint64_t l0cSize = 0; | ||
| 51 | +}; | ||
| 52 | + | ||
| 53 | +struct CompileInfoCommon { | ||
| 54 | + uint32_t aivNum; | ||
| 55 | + uint32_t aicNum; | ||
| 56 | + uint64_t ubSize; | ||
| 57 | + uint64_t l1Size; | ||
| 58 | + uint64_t l0aSize; | ||
| 59 | + uint64_t l0bSize; | ||
| 60 | + uint64_t l0cSize; | ||
| 61 | + uint64_t l2CacheSize; | ||
| 62 | + int64_t coreNum; | ||
| 63 | + int32_t socVersion; | ||
| 64 | + uint32_t rsvd; | ||
| 65 | +}; | ||
| 66 | + | ||
| 67 | +struct FlashAttentionScoreGradCompileInfo { | ||
| 68 | + uint32_t aivNum; | ||
| 69 | + uint32_t aicNum; | ||
| 70 | + uint64_t ubSize; | ||
| 71 | + uint64_t l1Size; | ||
| 72 | + uint64_t l0aSize; | ||
| 73 | + uint64_t l0bSize; | ||
| 74 | + uint64_t l0cSize; | ||
| 75 | + uint64_t l2CacheSize; | ||
| 76 | + int64_t coreNum; | ||
| 77 | + platform_ascendc::SocVersion socVersion; | ||
| 78 | +}; | ||
| 79 | + | ||
| 80 | +struct FACompileInfoCommon { | ||
| 81 | + uint32_t aivNum; | ||
| 82 | + uint32_t aicNum; | ||
| 83 | + uint64_t ubSize; | ||
| 84 | + uint64_t l1Size; | ||
| 85 | + uint64_t l0aSize; | ||
| 86 | + uint64_t l0bSize; | ||
| 87 | + uint64_t l0cSize; | ||
| 88 | + uint64_t l2CacheSize; | ||
| 89 | + int64_t coreNum; | ||
| 90 | + int32_t socVersion; | ||
| 91 | + uint32_t rsvd; | ||
| 92 | +}; | ||
| 93 | + | ||
| 94 | +class TilingBaseClass { | ||
| 95 | +public: | ||
| 96 | + explicit TilingBaseClass(gert::TilingContext* context) : context_(context) | ||
| 97 | + {} | ||
| 98 | + | ||
| 99 | + virtual ~TilingBaseClass() = default; | ||
| 100 | + | ||
| 101 | + // Tiling执行框架 | ||
| 102 | + // 1、GRAPH_SUCCESS: 成功,并且不需要继续执行后续Tiling类的实现 | ||
| 103 | + // 2、GRAPH_FAILED: 失败,中止整个Tiling流程 | ||
| 104 | + // 3、GRAPH_PARAM_INVALID: 本类不支持,需要继续往下执行其他Tiling类的实现 | ||
| 105 | + ge::graphStatus DoTiling() | ||
| 106 | + { | ||
| 107 | + auto ret = GetShapeAttrsInfo(); | ||
| 108 | + if (ret != ge::GRAPH_SUCCESS) { | ||
| 109 | + return ret; | ||
| 110 | + } | ||
| 111 | + ret = GetPlatformInfo(); | ||
| 112 | + if (ret != ge::GRAPH_SUCCESS) { | ||
| 113 | + return ret; | ||
| 114 | + } | ||
| 115 | + if (!IsCapable()) { | ||
| 116 | + return ge::GRAPH_PARAM_INVALID; | ||
| 117 | + } | ||
| 118 | + ret = DoOpTiling(); | ||
| 119 | + if (ret != ge::GRAPH_SUCCESS) { | ||
| 120 | + return ret; | ||
| 121 | + } | ||
| 122 | + ret = DoLibApiTiling(); | ||
| 123 | + if (ret != ge::GRAPH_SUCCESS) { | ||
| 124 | + return ret; | ||
| 125 | + } | ||
| 126 | + ret = GetWorkspaceSize(); | ||
| 127 | + if (ret != ge::GRAPH_SUCCESS) { | ||
| 128 | + return ret; | ||
| 129 | + } | ||
| 130 | + ret = PostTiling(); | ||
| 131 | + if (ret != ge::GRAPH_SUCCESS) { | ||
| 132 | + return ret; | ||
| 133 | + } | ||
| 134 | + context_->SetTilingKey(GetTilingKey()); | ||
| 135 | + DumpTilingInfo(); | ||
| 136 | + return ge::GRAPH_SUCCESS; | ||
| 137 | + } | ||
| 138 | + | ||
| 139 | + // 更新 context | ||
| 140 | + virtual void Reset(gert::TilingContext* context) | ||
| 141 | + { | ||
| 142 | + context_ = context; | ||
| 143 | + } | ||
| 144 | + | ||
| 145 | +protected: | ||
| 146 | + virtual bool IsCapable() = 0; | ||
| 147 | + // 1、获取平台信息比如CoreNum、UB/L1/L0C资源大小 | ||
| 148 | + virtual ge::graphStatus GetPlatformInfo() = 0; | ||
| 149 | + // 2、获取INPUT/OUTPUT/ATTR信息 | ||
| 150 | + virtual ge::graphStatus GetShapeAttrsInfo() = 0; | ||
| 151 | + // 3、计算数据切分TilingData | ||
| 152 | + virtual ge::graphStatus DoOpTiling() = 0; | ||
| 153 | + // 4、计算高阶API的TilingData | ||
| 154 | + virtual ge::graphStatus DoLibApiTiling() = 0; | ||
| 155 | + // 5、计算TilingKey | ||
| 156 | + [[nodiscard]] virtual uint64_t GetTilingKey() const = 0; | ||
| 157 | + // 6、计算Workspace 大小 | ||
| 158 | + virtual ge::graphStatus GetWorkspaceSize() = 0; | ||
| 159 | + // 7、保存Tiling数据 | ||
| 160 | + virtual ge::graphStatus PostTiling() = 0; | ||
| 161 | + // 8、Dump Tiling数据 | ||
| 162 | + virtual void DumpTilingInfo() | ||
| 163 | + { | ||
| 164 | + int32_t enable = CheckLogLevel(static_cast<int32_t>(XLLM_OP_MODULE_ID), DLOG_DEBUG); | ||
| 165 | + if (enable != 1) { | ||
| 166 | + return; | ||
| 167 | + } | ||
| 168 | + auto buf = (uint32_t*)context_->GetRawTilingData()->GetData(); | ||
| 169 | + auto bufLen = context_->GetRawTilingData()->GetDataSize(); | ||
| 170 | + std::ostringstream oss; | ||
| 171 | + oss << "Start to dump tiling info. tilingkey:" << context_->GetTilingKey() << ", tiling data size:" << bufLen | ||
| 172 | + << ", content:"; | ||
| 173 | + for (size_t i = 0; i < bufLen / sizeof(uint32_t); i++) { | ||
| 174 | + oss << *(buf + i) << ","; | ||
| 175 | + if (oss.str().length() > 640) { // Split according to 640 to avoid truncation | ||
| 176 | + OP_LOGD(context_, "%s", oss.str().c_str()); | ||
| 177 | + oss.str(""); | ||
| 178 | + } | ||
| 179 | + } | ||
| 180 | + OP_LOGD(context_, "%s", oss.str().c_str()); | ||
| 181 | + } | ||
| 182 | + | ||
| 183 | + static uint32_t CalcTschBlockDim(uint32_t sliceNum, uint32_t aicCoreNum, uint32_t aivCoreNum) | ||
| 184 | + { | ||
| 185 | + uint32_t ration; | ||
| 186 | + if (aicCoreNum == 0 || aivCoreNum == 0 || aicCoreNum > aivCoreNum) { | ||
| 187 | + return sliceNum; | ||
| 188 | + } | ||
| 189 | + ration = aivCoreNum / aicCoreNum; | ||
| 190 | + return (sliceNum + (ration - 1)) / ration; | ||
| 191 | + } | ||
| 192 | + | ||
| 193 | + template <typename T> | ||
| 194 | + [[nodiscard]] std::string GetShapeDebugStr(const T& shape) const | ||
| 195 | + { | ||
| 196 | + std::ostringstream oss; | ||
| 197 | + oss << "["; | ||
| 198 | + if (shape.GetDimNum() > 0) { | ||
| 199 | + for (size_t i = 0; i < shape.GetDimNum() - 1; ++i) { | ||
| 200 | + oss << shape.GetDim(i) << ", "; | ||
| 201 | + } | ||
| 202 | + oss << shape.GetDim(shape.GetDimNum() - 1); | ||
| 203 | + } | ||
| 204 | + oss << "]"; | ||
| 205 | + return oss.str(); | ||
| 206 | + } | ||
| 207 | + | ||
| 208 | + [[nodiscard]] std::string GetTensorDebugStr( | ||
| 209 | + const gert::StorageShape* shape, const gert::CompileTimeTensorDesc* tensor) | ||
| 210 | + { | ||
| 211 | + if (shape == nullptr || tensor == nullptr) { | ||
| 212 | + return "nil "; | ||
| 213 | + } | ||
| 214 | + std::ostringstream oss; | ||
| 215 | + oss << "(dtype: " << ge::TypeUtils::DataTypeToSerialString(tensor->GetDataType()) << "),"; | ||
| 216 | + oss << "(shape:" << GetShapeDebugStr(shape->GetStorageShape()) << "),"; | ||
| 217 | + oss << "(ori_shape:" << GetShapeDebugStr(shape->GetOriginShape()) << "),"; | ||
| 218 | + oss << "(format: " | ||
| 219 | + << ge::TypeUtils::FormatToSerialString( | ||
| 220 | + static_cast<ge::Format>(ge::GetPrimaryFormat(tensor->GetStorageFormat()))) | ||
| 221 | + << "),"; | ||
| 222 | + oss << "(ori_format: " << ge::TypeUtils::FormatToSerialString(tensor->GetOriginFormat()) << ") "; | ||
| 223 | + return oss.str(); | ||
| 224 | + } | ||
| 225 | + | ||
| 226 | + [[nodiscard]] std::string GetTilingContextDebugStr() | ||
| 227 | + { | ||
| 228 | + std::ostringstream oss; | ||
| 229 | + for (size_t i = 0; i < context_->GetComputeNodeInfo()->GetInputsNum(); ++i) { | ||
| 230 | + oss << "input" << i << ": "; | ||
| 231 | + oss << GetTensorDebugStr(context_->GetInputShape(i), context_->GetInputDesc(i)); | ||
| 232 | + } | ||
| 233 | + | ||
| 234 | + for (size_t i = 0; i < context_->GetComputeNodeInfo()->GetOutputsNum(); ++i) { | ||
| 235 | + oss << "output" << i << ": "; | ||
| 236 | + oss << GetTensorDebugStr(context_->GetOutputShape(i), context_->GetOutputDesc(i)); | ||
| 237 | + } | ||
| 238 | + return oss.str(); | ||
| 239 | + } | ||
| 240 | + | ||
| 241 | + [[nodiscard]] std::string GetTilingDataDebugStr() const | ||
| 242 | + { | ||
| 243 | + auto rawTilingData = context_->GetRawTilingData(); | ||
| 244 | + auto rawTilingDataSize = rawTilingData->GetDataSize(); | ||
| 245 | + auto data = reinterpret_cast<const int32_t*>(rawTilingData->GetData()); | ||
| 246 | + size_t len = rawTilingDataSize / sizeof(int32_t); | ||
| 247 | + std::ostringstream oss; | ||
| 248 | + for (size_t i = 0; i < len; i++) { | ||
| 249 | + oss << data[i] << ", "; | ||
| 250 | + } | ||
| 251 | + return oss.str(); | ||
| 252 | + } | ||
| 253 | + | ||
| 254 | +protected: | ||
| 255 | + gert::TilingContext* context_ = nullptr; | ||
| 256 | + std::unique_ptr<platform_ascendc::PlatformAscendC> ascendcPlatform_{nullptr}; | ||
| 257 | + uint32_t blockDim_{0}; | ||
| 258 | + uint64_t workspaceSize_{0}; | ||
| 259 | + uint64_t tilingKey_{0}; | ||
| 260 | + AiCoreParams aicoreParams_; | ||
| 261 | +}; | ||
| 262 | + | ||
| 263 | +} // namespace OpTiling | ||
| 264 | +} // namespace Xllm | ||
| 265 | +} // namespace Ops | ||
| @@ -0,0 +1,63 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file tiling_key.h | ||
| 13 | + * \brief | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#pragma once | ||
| 17 | + | ||
| 18 | +#include <cstdint> | ||
| 19 | + | ||
| 20 | +namespace Ops { | ||
| 21 | +namespace Xllm { | ||
| 22 | +namespace OpTiling { | ||
| 23 | +constexpr uint64_t RecursiveSum() | ||
| 24 | +{ | ||
| 25 | + return 0; | ||
| 26 | +} | ||
| 27 | + | ||
| 28 | +constexpr uint64_t kBase = 10; // 10进制进位基数 | ||
| 29 | +template <typename T, typename... Args> constexpr uint64_t RecursiveSum(T templateId, Args... templateIds) | ||
| 30 | +{ | ||
| 31 | + return static_cast<uint64_t>(templateId) + kBase * RecursiveSum(templateIds...); | ||
| 32 | +} | ||
| 33 | + | ||
| 34 | +// TilingKey 的生成规则: | ||
| 35 | +// FlashAttentionScore/FlashAttentionScoreGrad 十进制位组装tiling key,包含以下关键参数,从低位到高位依次是:Ub0, Ub1, | ||
| 36 | +// Block, DataType, Format, Sparse, 特化模板 Ub0、Ub1: | ||
| 37 | +// 表示Ub核内切分的轴,使用枚举AxisEnum表示,因为我们允许最多切分两根轴,所以存在UB0和UB1,如果没有UB核内切分, | ||
| 38 | +// 那么填AXIS_NONE。UB0和UB1各占一个十进制位; | ||
| 39 | +// Block: 表示UB用来分核的轴,使用枚举AxisEnum表示,占一个十进制位; | ||
| 40 | +// DataType: 表示当前tiling key支持的输入输出的数据类型,使用枚举SupportedDtype来表示,占一个十进制位 | ||
| 41 | +// Format: 表示当前tiling key支持的Format, 使用枚举InputLayout表示,占一个十进制位 | ||
| 42 | +// Sparse: 表示当前tiling key是否支持Sparse,使用枚举SparseCapability表示,占一个十进制位 | ||
| 43 | +// 其余特化场景,定义自己的位域和值 | ||
| 44 | +// usage: get tilingKey from inputted types | ||
| 45 | +// uint64_t tilingKey = GET_FLASHATTENTION_TILINGKEY(AxisEnum::AXIS_S1, AxisEnum::AXIS_S2, AxisEnum::AXIS_N2, | ||
| 46 | +// SupportedDtype::FLOAT32, InputLayout::BSH, SparseCapability::SUPPORT_ALL) | ||
| 47 | + | ||
| 48 | +constexpr uint64_t TILINGKEYOFFSET = uint64_t(10000000000000000000UL); // 10^19 | ||
| 49 | +template <typename... Args> constexpr uint64_t GET_TILINGKEY(Args... templateIds) | ||
| 50 | +{ | ||
| 51 | + return TILINGKEYOFFSET + RecursiveSum(templateIds...); | ||
| 52 | +} | ||
| 53 | + | ||
| 54 | +// usage: get tilingKey from inputted types | ||
| 55 | +// uint64_t tilingKey = TILINGKEY(S2, S1, N2, FLOAT32, BSND, ALL) | ||
| 56 | + | ||
| 57 | +#define TILINGKEY(ub2, ub1, block, dtype, layout, sparse) \ | ||
| 58 | + (GET_TILINGKEY(AxisEnum::ub2, AxisEnum::ub1, AxisEnum::block, DtypeEnum::dtype, LayoutEnum::layout, \ | ||
| 59 | + SparseEnum::sparse)) | ||
| 60 | + | ||
| 61 | +} // namespace Optiling | ||
| 62 | +} // namespace Xllm | ||
| 63 | +} // namespace Ops | ||
| @@ -0,0 +1,350 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file tiling_templates_registry.h | ||
| 13 | + * \brief | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#pragma once | ||
| 17 | + | ||
| 18 | +#include <map> | ||
| 19 | +#include <string> | ||
| 20 | +#include <memory> | ||
| 21 | +#include "exe_graph/runtime/tiling_context.h" | ||
| 22 | +#include "tiling_base/tiling_base.h" | ||
| 23 | +#include "tiling_base/error_log.h" | ||
| 24 | + | ||
| 25 | +namespace Ops { | ||
| 26 | +namespace Xllm { | ||
| 27 | +namespace OpTiling { | ||
| 28 | + | ||
| 29 | +template <typename T> | ||
| 30 | +std::unique_ptr<TilingBaseClass> TILING_CLASS(gert::TilingContext* context) | ||
| 31 | +{ | ||
| 32 | + return std::unique_ptr<T>(new (std::nothrow) T(context)); | ||
| 33 | +} | ||
| 34 | + | ||
| 35 | +using TilingClassCase = std::unique_ptr<TilingBaseClass> (*)(gert::TilingContext*); | ||
| 36 | + | ||
| 37 | +class TilingCases { | ||
| 38 | +public: | ||
| 39 | + explicit TilingCases(std::string op_type) : op_type_(std::move(op_type)) | ||
| 40 | + {} | ||
| 41 | + | ||
| 42 | + template <typename T> | ||
| 43 | + void AddTiling(int32_t priority) | ||
| 44 | + { | ||
| 45 | + OP_CHECK_IF( | ||
| 46 | + cases_.find(priority) != cases_.end(), OP_LOGE(op_type_, "There are duplicate registrations."), return); | ||
| 47 | + cases_[priority] = TILING_CLASS<T>; | ||
| 48 | + OP_CHECK_IF( | ||
| 49 | + cases_[priority] == nullptr, | ||
| 50 | + OP_LOGE(op_type_, "Register op tiling func failed, please check the class name."), return); | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + const std::map<int32_t, TilingClassCase>& GetTilingCases() | ||
| 54 | + { | ||
| 55 | + return cases_; | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | +private: | ||
| 59 | + std::map<int32_t, TilingClassCase> cases_; | ||
| 60 | + const std::string op_type_; | ||
| 61 | +}; | ||
| 62 | + | ||
| 63 | +// --------------------------------Interfacce with soc version -------------------------------- | ||
| 64 | +class TilingRegistryNew { | ||
| 65 | +public: | ||
| 66 | + TilingRegistryNew() = default; | ||
| 67 | + | ||
| 68 | +#ifdef ASCENDC_OP_TEST | ||
| 69 | + static TilingRegistryNew& GetInstance(); | ||
| 70 | +#else | ||
| 71 | + static TilingRegistryNew& GetInstance() | ||
| 72 | + { | ||
| 73 | + static TilingRegistryNew registry_impl_; | ||
| 74 | + return registry_impl_; | ||
| 75 | + } | ||
| 76 | +#endif | ||
| 77 | + | ||
| 78 | + std::shared_ptr<TilingCases> RegisterOp(const std::string& op_type, int32_t soc_version) | ||
| 79 | + { | ||
| 80 | + auto soc_iter = registry_map_.find(soc_version); | ||
| 81 | + if (soc_iter == registry_map_.end()) { | ||
| 82 | + std::map<std::string, std::shared_ptr<TilingCases>> op_type_map; | ||
| 83 | + op_type_map[op_type] = std::shared_ptr<TilingCases>(new (std::nothrow) TilingCases(op_type)); | ||
| 84 | + registry_map_[soc_version] = op_type_map; | ||
| 85 | + } else { | ||
| 86 | + if (soc_iter->second.find(op_type) == soc_iter->second.end()) { | ||
| 87 | + soc_iter->second[op_type] = std::shared_ptr<TilingCases>(new (std::nothrow) TilingCases(op_type)); | ||
| 88 | + } | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + OP_CHECK_IF( | ||
| 92 | + registry_map_[soc_version][op_type] == nullptr, | ||
| 93 | + OP_LOGE(op_type, "Register tiling func failed, please check the class name."), return nullptr); | ||
| 94 | + return registry_map_[soc_version][op_type]; | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + ge::graphStatus DoTilingImpl(gert::TilingContext* context) | ||
| 98 | + { | ||
| 99 | + int32_t soc_version = (int32_t)platform_ascendc::SocVersion::RESERVED_VERSION; | ||
| 100 | + const char* op_type = context->GetNodeType(); | ||
| 101 | + fe::PlatFormInfos* platformInfoPtr = context->GetPlatformInfo(); | ||
| 102 | + if (platformInfoPtr == nullptr) { | ||
| 103 | + auto compileInfoPtr = static_cast<const CompileInfoCommon*>(context->GetCompileInfo()); | ||
| 104 | + OP_CHECK_IF( | ||
| 105 | + compileInfoPtr == nullptr, OP_LOGE(op_type, "compileInfoPtr is null."), return ge::GRAPH_FAILED); | ||
| 106 | + soc_version = compileInfoPtr->socVersion; | ||
| 107 | + OP_LOGD(context, "soc version in compileInfo is %d", soc_version); | ||
| 108 | + } else { | ||
| 109 | + auto ascendcPlatform = platform_ascendc::PlatformAscendC(platformInfoPtr); | ||
| 110 | + soc_version = static_cast<int32_t>(ascendcPlatform.GetSocVersion()); | ||
| 111 | + OP_LOGD(context, "soc version is %d", soc_version); | ||
| 112 | + if (soc_version == (int32_t)platform_ascendc::SocVersion::RESERVED_VERSION) { | ||
| 113 | + OP_LOGE(op_type, "Do op tiling failed, cannot find soc version."); | ||
| 114 | + return ge::GRAPH_FAILED; | ||
| 115 | + } | ||
| 116 | + } | ||
| 117 | + auto tilingTemplateRegistryMap = GetTilingTemplates(op_type, soc_version); | ||
| 118 | + for (auto it = tilingTemplateRegistryMap.begin(); it != tilingTemplateRegistryMap.end(); ++it) { | ||
| 119 | + auto tilingTemplate = it->second(context); | ||
| 120 | + if (tilingTemplate != nullptr) { | ||
| 121 | + ge::graphStatus status = tilingTemplate->DoTiling(); | ||
| 122 | + if (status != ge::GRAPH_PARAM_INVALID) { | ||
| 123 | + OP_LOGD(context, "Do general op tiling success priority=%d", it->first); | ||
| 124 | + return status; | ||
| 125 | + } | ||
| 126 | + OP_LOGD(context, "Ignore general op tiling priority=%d", it->first); | ||
| 127 | + } | ||
| 128 | + } | ||
| 129 | + OP_LOGE(op_type, "Do op tiling failed, no valid template is found."); | ||
| 130 | + return ge::GRAPH_FAILED; | ||
| 131 | + } | ||
| 132 | + | ||
| 133 | + ge::graphStatus DoTilingImpl(gert::TilingContext* context, const std::vector<int32_t>& priorities) | ||
| 134 | + { | ||
| 135 | + int32_t soc_version; | ||
| 136 | + const char* op_type = context->GetNodeType(); | ||
| 137 | + auto platformInfoPtr = context->GetPlatformInfo(); | ||
| 138 | + if (platformInfoPtr == nullptr) { | ||
| 139 | + auto compileInfoPtr = reinterpret_cast<const CompileInfoCommon*>(context->GetCompileInfo()); | ||
| 140 | + OP_CHECK_IF( | ||
| 141 | + compileInfoPtr == nullptr, OP_LOGE(op_type, "compileInfoPtr is null."), return ge::GRAPH_FAILED); | ||
| 142 | + soc_version = compileInfoPtr->socVersion; | ||
| 143 | + OP_LOGD(context, "soc version in compileInfo is %d", soc_version); | ||
| 144 | + } else { | ||
| 145 | + auto ascendcPlatform = platform_ascendc::PlatformAscendC(platformInfoPtr); | ||
| 146 | + soc_version = static_cast<int32_t>(ascendcPlatform.GetSocVersion()); | ||
| 147 | + OP_LOGD(context, "soc version is %d", soc_version); | ||
| 148 | + } | ||
| 149 | + | ||
| 150 | + auto tilingTemplateRegistryMap = GetTilingTemplates(op_type, soc_version); | ||
| 151 | + for (auto priority_id : priorities) { | ||
| 152 | + auto tilingCaseIter = tilingTemplateRegistryMap.find(priority_id); | ||
| 153 | + if (tilingCaseIter != tilingTemplateRegistryMap.end()) { | ||
| 154 | + auto templateFunc = tilingCaseIter->second(context); | ||
| 155 | + if (templateFunc != nullptr) { | ||
| 156 | + ge::graphStatus status = templateFunc->DoTiling(); | ||
| 157 | + if (status == ge::GRAPH_SUCCESS) { | ||
| 158 | + OP_LOGD(context, "Do general op tiling success priority=%d", priority_id); | ||
| 159 | + return status; | ||
| 160 | + } | ||
| 161 | + OP_LOGD(context, "Ignore general op tiling priority=%d", priority_id); | ||
| 162 | + } | ||
| 163 | + } | ||
| 164 | + } | ||
| 165 | + return ge::GRAPH_FAILED; | ||
| 166 | + } | ||
| 167 | + | ||
| 168 | + const std::map<int32_t, TilingClassCase>& GetTilingTemplates(const std::string& op_type, int32_t soc_version) | ||
| 169 | + { | ||
| 170 | + auto soc_iter = registry_map_.find(soc_version); | ||
| 171 | + OP_CHECK_IF( | ||
| 172 | + soc_iter == registry_map_.end(), | ||
| 173 | + OP_LOGE(op_type, "Get op tiling func failed, please check the soc version %d", soc_version), | ||
| 174 | + return empty_tiling_case_); | ||
| 175 | + auto op_iter = soc_iter->second.find(op_type); | ||
| 176 | + OP_CHECK_IF( | ||
| 177 | + op_iter == soc_iter->second.end(), OP_LOGE(op_type, "Get op tiling func failed, please check the op name."), | ||
| 178 | + return empty_tiling_case_); | ||
| 179 | + return op_iter->second->GetTilingCases(); | ||
| 180 | + } | ||
| 181 | + | ||
| 182 | +private: | ||
| 183 | + std::map<int32_t, std::map<std::string, std::shared_ptr<TilingCases>>> registry_map_; // key is socversion | ||
| 184 | + const std::map<int32_t, TilingClassCase> empty_tiling_case_{}; | ||
| 185 | +}; | ||
| 186 | + | ||
| 187 | +class RegisterNew { | ||
| 188 | +public: | ||
| 189 | + explicit RegisterNew(std::string op_type) : op_type_(std::move(op_type)) | ||
| 190 | + {} | ||
| 191 | + | ||
| 192 | + template <typename T> | ||
| 193 | + RegisterNew& tiling(int32_t priority, int32_t soc_version) | ||
| 194 | + { | ||
| 195 | + auto tilingCases = TilingRegistryNew::GetInstance().RegisterOp(op_type_, soc_version); | ||
| 196 | + OP_CHECK_IF( | ||
| 197 | + tilingCases == nullptr, OP_LOGE(op_type_, "Register op tiling failed, please the op name."), return *this); | ||
| 198 | + tilingCases->AddTiling<T>(priority); | ||
| 199 | + return *this; | ||
| 200 | + } | ||
| 201 | + | ||
| 202 | + template <typename T> | ||
| 203 | + RegisterNew& tiling(int32_t priority, const std::vector<int32_t>& soc_versions) | ||
| 204 | + { | ||
| 205 | + for (int32_t soc_version : soc_versions) { | ||
| 206 | + auto tilingCases = TilingRegistryNew::GetInstance().RegisterOp(op_type_, soc_version); | ||
| 207 | + OP_CHECK_IF( | ||
| 208 | + tilingCases == nullptr, OP_LOGE(op_type_, "Register op tiling failed, please the op name."), | ||
| 209 | + return *this); | ||
| 210 | + tilingCases->AddTiling<T>(priority); | ||
| 211 | + } | ||
| 212 | + return *this; | ||
| 213 | + } | ||
| 214 | + | ||
| 215 | +private: | ||
| 216 | + const std::string op_type_; | ||
| 217 | +}; | ||
| 218 | + | ||
| 219 | +// --------------------------------Interfacce without soc version -------------------------------- | ||
| 220 | +class TilingRegistry { | ||
| 221 | +public: | ||
| 222 | + TilingRegistry() = default; | ||
| 223 | + | ||
| 224 | +#ifdef ASCENDC_OP_TEST | ||
| 225 | + static TilingRegistry& GetInstance(); | ||
| 226 | +#else | ||
| 227 | + static TilingRegistry& GetInstance() | ||
| 228 | + { | ||
| 229 | + static TilingRegistry registry_impl_; | ||
| 230 | + return registry_impl_; | ||
| 231 | + } | ||
| 232 | +#endif | ||
| 233 | + | ||
| 234 | + std::shared_ptr<TilingCases> RegisterOp(const std::string& op_type) | ||
| 235 | + { | ||
| 236 | + if (registry_map_.find(op_type) == registry_map_.end()) { | ||
| 237 | + registry_map_[op_type] = std::shared_ptr<TilingCases>(new (std::nothrow) TilingCases(op_type)); | ||
| 238 | + } | ||
| 239 | + OP_CHECK_IF( | ||
| 240 | + registry_map_[op_type] == nullptr, | ||
| 241 | + OP_LOGE(op_type, "Register tiling func failed, please check the class name."), return nullptr); | ||
| 242 | + return registry_map_[op_type]; | ||
| 243 | + } | ||
| 244 | + | ||
| 245 | + ge::graphStatus DoTilingImpl(gert::TilingContext* context) | ||
| 246 | + { | ||
| 247 | + const char* op_type = context->GetNodeType(); | ||
| 248 | + auto tilingTemplateRegistryMap = GetTilingTemplates(op_type); | ||
| 249 | + for (auto it = tilingTemplateRegistryMap.begin(); it != tilingTemplateRegistryMap.end(); ++it) { | ||
| 250 | + auto tilingTemplate = it->second(context); | ||
| 251 | + if (tilingTemplate != nullptr) { | ||
| 252 | + ge::graphStatus status = tilingTemplate->DoTiling(); | ||
| 253 | + if (status != ge::GRAPH_PARAM_INVALID) { | ||
| 254 | + OP_LOGD(context, "Do general op tiling success priority=%d", it->first); | ||
| 255 | + return status; | ||
| 256 | + } | ||
| 257 | + OP_LOGD(context, "Ignore general op tiling priority=%d", it->first); | ||
| 258 | + } | ||
| 259 | + } | ||
| 260 | + OP_LOGE(op_type, "Do op tiling failed, no valid template is found."); | ||
| 261 | + return ge::GRAPH_FAILED; | ||
| 262 | + } | ||
| 263 | + | ||
| 264 | + ge::graphStatus DoTilingImpl(gert::TilingContext* context, const std::vector<int32_t>& priorities) | ||
| 265 | + { | ||
| 266 | + const char* op_type = context->GetNodeType(); | ||
| 267 | + auto tilingTemplateRegistryMap = GetTilingTemplates(op_type); | ||
| 268 | + for (auto priorityId : priorities) { | ||
| 269 | + auto templateFunc = tilingTemplateRegistryMap[priorityId](context); | ||
| 270 | + if (templateFunc != nullptr) { | ||
| 271 | + ge::graphStatus status = templateFunc->DoTiling(); | ||
| 272 | + if (status == ge::GRAPH_SUCCESS) { | ||
| 273 | + OP_LOGD(context, "Do general op tiling success priority=%d", priorityId); | ||
| 274 | + return status; | ||
| 275 | + } | ||
| 276 | + if (status != ge::GRAPH_PARAM_INVALID) { | ||
| 277 | + OP_LOGD(context, "Do op tiling failed"); | ||
| 278 | + return status; | ||
| 279 | + } | ||
| 280 | + OP_LOGD(context, "Ignore general op tiling priority=%d", priorityId); | ||
| 281 | + } | ||
| 282 | + } | ||
| 283 | + OP_LOGE(op_type, "Do op tiling failed, no valid template is found."); | ||
| 284 | + return ge::GRAPH_FAILED; | ||
| 285 | + } | ||
| 286 | + | ||
| 287 | + const std::map<int32_t, TilingClassCase>& GetTilingTemplates(const std::string& op_type) | ||
| 288 | + { | ||
| 289 | + OP_CHECK_IF( | ||
| 290 | + registry_map_.find(op_type) == registry_map_.end(), | ||
| 291 | + OP_LOGE(op_type, "Get op tiling func failed, please check the op name."), return empty_tiling_case_); | ||
| 292 | + return registry_map_[op_type]->GetTilingCases(); | ||
| 293 | + } | ||
| 294 | + | ||
| 295 | +private: | ||
| 296 | + std::map<std::string, std::shared_ptr<TilingCases>> registry_map_; | ||
| 297 | + const std::map<int32_t, TilingClassCase> empty_tiling_case_; | ||
| 298 | +}; | ||
| 299 | + | ||
| 300 | +class Register { | ||
| 301 | +public: | ||
| 302 | + explicit Register(std::string op_type) : op_type_(std::move(op_type)) | ||
| 303 | + {} | ||
| 304 | + | ||
| 305 | + template <typename T> | ||
| 306 | + Register& tiling(int32_t priority) | ||
| 307 | + { | ||
| 308 | + auto tilingCases = TilingRegistry::GetInstance().RegisterOp(op_type_); | ||
| 309 | + OP_CHECK_IF( | ||
| 310 | + tilingCases == nullptr, OP_LOGE(op_type_, "Register op tiling failed, please the op name."), return *this); | ||
| 311 | + tilingCases->AddTiling<T>(priority); | ||
| 312 | + return *this; | ||
| 313 | + } | ||
| 314 | + | ||
| 315 | +private: | ||
| 316 | + const std::string op_type_; | ||
| 317 | +}; | ||
| 318 | +} // namespace OpTiling | ||
| 319 | +} // namespace Xllm | ||
| 320 | +} // namespace Ops | ||
| 321 | + | ||
| 322 | +// op_type: 算子名称, class_name: 注册的 tiling 类, soc_version:芯片版本号 | ||
| 323 | +// priority: tiling 类的优先级, 越小表示优先级越高, 即会优先选择这个tiling类 | ||
| 324 | +#define REGISTER_TILING_TEMPLATE_WITH_SOCVERSION(op_type, class_name, soc_versions, priority) \ | ||
| 325 | + [[maybe_unused]] uint32_t op_impl_register_template_##op_type##_##class_name##priority; \ | ||
| 326 | + static Ops::Xllm::OpTiling::RegisterNew VAR_UNUSED##op_type##class_name##priority_register = \ | ||
| 327 | + Ops::Xllm::OpTiling::RegisterNew(#op_type).tiling<class_name>(priority, soc_versions) | ||
| 328 | + | ||
| 329 | +// op_type: 算子名称, class_name: 注册的 tiling 类, | ||
| 330 | +// priority: tiling 类的优先级, 越小表示优先级越高, 即被选中的概率越大 | ||
| 331 | +#define REGISTER_TILING_TEMPLATE(op_type, class_name, priority) \ | ||
| 332 | + static Ops::Xllm::OpTiling::Register VAR_UNUSED##op_type_##class_name##priority_register = \ | ||
| 333 | + Ops::Xllm::OpTiling::Register(op_type).tiling<class_name>(priority) | ||
| 334 | + | ||
| 335 | +// op_type: 算子名称, class_name: 注册的 tiling 类, | ||
| 336 | +// soc_version: soc版本,用于区分不同的soc | ||
| 337 | +// priority: tiling 类的优先级, 越小表示优先级越高, 即会优先选择这个tiling类 | ||
| 338 | +#define REGISTER_TILING_TEMPLATE_NEW(op_type, class_name, soc_version, priority) \ | ||
| 339 | + [[maybe_unused]] uint32_t op_impl_register_template_##op_type##_##class_name##priority; \ | ||
| 340 | + static Ops::Xllm::OpTiling::RegisterNew VAR_UNUSED##op_type##class_name##priority_register = \ | ||
| 341 | + Ops::Xllm::OpTiling::RegisterNew(#op_type).tiling<class_name>(priority, soc_version) | ||
| 342 | + | ||
| 343 | +// op_type: 算子名称, class_name: 注册的 tiling 类, | ||
| 344 | +// priority: tiling 类的优先级, 越小表示优先级越高, 即被选中的概率越大 | ||
| 345 | +// 取代 REGISTER_TILING_TEMPLATE , 传入的op_type如果是字符串常量,需要去掉引号 | ||
| 346 | +#define REGISTER_OPS_TILING_TEMPLATE(op_type, class_name, priority) \ | ||
| 347 | + [[maybe_unused]] uint32_t op_impl_register_template_##op_type##_##class_name##priority; \ | ||
| 348 | + static Ops::Xllm::OpTiling::Register \ | ||
| 349 | + __attribute__((unused)) tiling_##op_type##_##class_name##_##priority##_register = \ | ||
| 350 | + Ops::Xllm::OpTiling::Register(#op_type).tiling<class_name>(priority) | ||
| @@ -0,0 +1,139 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file tiling_type.h | ||
| 13 | + * \brief | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#pragma once | ||
| 17 | + | ||
| 18 | +#include <cstdint> | ||
| 19 | + | ||
| 20 | +namespace optiling { | ||
| 21 | + | ||
| 22 | +enum class AxisEnum { | ||
| 23 | + B = 0, | ||
| 24 | + N2 = 1, | ||
| 25 | + G = 2, | ||
| 26 | + S1 = 3, | ||
| 27 | + S2 = 4, | ||
| 28 | + D = 5, | ||
| 29 | + NONE = 9, | ||
| 30 | +}; | ||
| 31 | + | ||
| 32 | +enum class DtypeEnum { | ||
| 33 | + FLOAT16 = 0, | ||
| 34 | + FLOAT32 = 1, | ||
| 35 | + BFLOAT16 = 2, | ||
| 36 | + FLOAT16_PRECISION = 3, | ||
| 37 | +}; | ||
| 38 | + | ||
| 39 | +enum class PerformanceOrientedEnum { | ||
| 40 | + BIG_BUFFER = 1, | ||
| 41 | + BIG_DOUBLE_BUFFER = 2, | ||
| 42 | +}; | ||
| 43 | + | ||
| 44 | +enum class MatmulConfig { | ||
| 45 | + NULL_CONFIG = 0, | ||
| 46 | + NORMAL_CONFIG = 1, | ||
| 47 | + MDL_CONFIG = 2 | ||
| 48 | +}; | ||
| 49 | + | ||
| 50 | +enum class PseConfig { | ||
| 51 | + NO_PSE = 0, | ||
| 52 | + EXIST_PSE = 1 | ||
| 53 | +}; | ||
| 54 | + | ||
| 55 | +enum class AttenMaskConfig { | ||
| 56 | + NO_ATTEN_MASK = 0, | ||
| 57 | + EXIST_ATTEN_MASK = 1 | ||
| 58 | +}; | ||
| 59 | + | ||
| 60 | +enum class DropOutConfig { | ||
| 61 | + NO_DROP_OUT = 0, | ||
| 62 | + EXIST_DROP_OUT = 1 | ||
| 63 | +}; | ||
| 64 | + | ||
| 65 | +enum class CubeFormatEnum { | ||
| 66 | + ND = 0, | ||
| 67 | + NZ = 1 | ||
| 68 | +}; | ||
| 69 | +enum class LayoutEnum { | ||
| 70 | + BSND = 0, | ||
| 71 | + SBND = 1, | ||
| 72 | + BNSD = 2, | ||
| 73 | + TND = 3, | ||
| 74 | + NTD_TND = 4 | ||
| 75 | +}; | ||
| 76 | + | ||
| 77 | +enum class CubeInputSourceEnum { | ||
| 78 | + GM = 0, | ||
| 79 | + L1 = 1 | ||
| 80 | +}; | ||
| 81 | + | ||
| 82 | +enum class OptionEnum { | ||
| 83 | + DISABLE = 0, | ||
| 84 | + ENABLE = 1 | ||
| 85 | +}; | ||
| 86 | + | ||
| 87 | +enum class SparseEnum { | ||
| 88 | + ALL = 0, | ||
| 89 | + NONE = 1, | ||
| 90 | + ANY = 2, | ||
| 91 | + CAUSAL = 3, | ||
| 92 | + BAND = 4, | ||
| 93 | + PREFIX = 5, | ||
| 94 | + BAND_COMPRESS = 6, | ||
| 95 | + RIGHT_DOWN_CAUSAL = 7, | ||
| 96 | + RIGHT_DOWN_CAUSAL_BAND = 8, | ||
| 97 | + BAND_LEFT_UP_CAUSAL = 9 | ||
| 98 | +}; | ||
| 99 | + | ||
| 100 | +constexpr uint64_t RecursiveSum() | ||
| 101 | +{ | ||
| 102 | + return 0; | ||
| 103 | +} | ||
| 104 | + | ||
| 105 | +constexpr int64_t base10Multiplier = 10; | ||
| 106 | + | ||
| 107 | +template <typename T, typename... Args> constexpr uint64_t RecursiveSum(T templateId, Args... templateIds) | ||
| 108 | +{ | ||
| 109 | + return static_cast<uint64_t>(templateId) + base10Multiplier * RecursiveSum(templateIds...); | ||
| 110 | +} | ||
| 111 | + | ||
| 112 | +// TilingKey 的生成规则: | ||
| 113 | +// FlashAttentionScore/FlashAttentionScoreGrad 十进制位组装tiling key,包含以下关键参数,从低位到高位依次是:Ub0, Ub1, | ||
| 114 | +// Block, DataType, Format, Sparse, 特化模板 Ub0、Ub1: | ||
| 115 | +// 表示Ub核内切分的轴,使用枚举AxisEnum表示,因为我们允许最多切分两根轴,所以存在UB0和UB1,如果没有UB核内切分, | ||
| 116 | +// 那么填AXIS_NONE。UB0和UB1各占一个十进制位; | ||
| 117 | +// Block: 表示UB用来分核的轴,使用枚举AxisEnum表示,占一个十进制位; | ||
| 118 | +// DataType: 表示当前tiling key支持的输入输出的数据类型,使用枚举SupportedDtype来表示,占一个十进制位 | ||
| 119 | +// Format: 表示当前tiling key支持的Format, 使用枚举InputLayout表示,占一个十进制位 | ||
| 120 | +// Sparse: 表示当前tiling key是否支持Sparse,使用枚举SparseCapability表示,占一个十进制位 | ||
| 121 | +// 其余特化场景,定义自己的位域和值 | ||
| 122 | +// usage: get tilingKey from inputted types | ||
| 123 | +// uint64_t tilingKey = GET_FLASHATTENTION_TILINGKEY(AxisEnum::AXIS_S1, AxisEnum::AXIS_S2, AxisEnum::AXIS_N2, | ||
| 124 | +// SupportedDtype::FLOAT32, InputLayout::BSH, SparseCapability::SUPPORT_ALL) | ||
| 125 | + | ||
| 126 | +constexpr uint64_t TILINGKEYOFFSET = uint64_t(10000000000000000000UL); // 10^19 | ||
| 127 | +template <typename... Args> constexpr uint64_t GET_TILINGKEY(Args... templateIds) | ||
| 128 | +{ | ||
| 129 | + return TILINGKEYOFFSET + RecursiveSum(templateIds...); | ||
| 130 | +} | ||
| 131 | + | ||
| 132 | +// usage: get tilingKey from inputted types | ||
| 133 | +// uint64_t tilingKey = TILINGKEY(S2, S1, N2, FLOAT32, BSND, ALL) | ||
| 134 | + | ||
| 135 | +#define TILINGKEY(ub2, ub1, block, dtype, layout, sparse) \ | ||
| 136 | + (GET_TILINGKEY(AxisEnum::ub2, AxisEnum::ub1, AxisEnum::block, DtypeEnum::dtype, LayoutEnum::layout, \ | ||
| 137 | + SparseEnum::sparse)) | ||
| 138 | + | ||
| 139 | +} // namespace optiling | ||
| @@ -0,0 +1,44 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file tiling_util.h | ||
| 13 | + * \brief | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#pragma once | ||
| 17 | + | ||
| 18 | +#include "register/op_impl_registry.h" | ||
| 19 | + | ||
| 20 | +namespace Ops { | ||
| 21 | +namespace Xllm { | ||
| 22 | +template <typename T> | ||
| 23 | +T CeilAlign(T a, T b) | ||
| 24 | +{ | ||
| 25 | + return (a + b - 1) / b * b; | ||
| 26 | +} | ||
| 27 | + | ||
| 28 | +template <typename T> | ||
| 29 | +T CeilDiv(T a, T b) | ||
| 30 | +{ | ||
| 31 | + if (b == 0) { | ||
| 32 | + return a; | ||
| 33 | + } | ||
| 34 | + return (a + b - 1) / b; | ||
| 35 | +} | ||
| 36 | +namespace OpTiling { | ||
| 37 | +bool IsRegbaseSocVersion(const gert::TilingParseContext* context); | ||
| 38 | + | ||
| 39 | +bool IsRegbaseSocVersion(const gert::TilingContext* context); | ||
| 40 | + | ||
| 41 | +const gert::Shape& EnsureNotScalar(const gert::Shape& inShape); | ||
| 42 | +} // namespace OpTiling | ||
| 43 | +} // namespace Xllm | ||
| 44 | +} // namespace Ops | ||
| @@ -0,0 +1,26 @@ | |||
| 1 | +# ---------------------------------------------------------------------------------------------------------- | ||
| 2 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | +# See the root of the software repository for the full text of the License. | ||
| 9 | +# ---------------------------------------------------------------------------------------------------------- | ||
| 10 | + | ||
| 11 | +# ============================================================================= | ||
| 12 | +# Verify proto header exists | ||
| 13 | +# ============================================================================= | ||
| 14 | +file(GLOB proto_headers "${CMAKE_CURRENT_SOURCE_DIR}/*_proto.h") | ||
| 15 | +if(NOT proto_headers) | ||
| 16 | + message(FATAL_ERROR "op_graph/ directory must contain at least one *_proto.h file with REG_OP definition") | ||
| 17 | +endif() | ||
| 18 | + | ||
| 19 | +# ============================================================================= | ||
| 20 | +# Add proto headers to GRAPH library include path | ||
| 21 | +# ============================================================================= | ||
| 22 | +if(TARGET cust_op_proto) | ||
| 23 | + target_include_directories(cust_op_proto PRIVATE | ||
| 24 | + ${CMAKE_CURRENT_SOURCE_DIR} | ||
| 25 | + ) | ||
| 26 | +endif() | ||
| @@ -0,0 +1,58 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software and you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * You may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * https://www.hiascend.com/software/cann | ||
| 9 | + * | ||
| 10 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 11 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. | ||
| 12 | + * See the License for the specific language governing permissions and limitations under the License. | ||
| 13 | + */ | ||
| 14 | + | ||
| 15 | +/*! | ||
| 16 | + * \file causal_conv1d_proto.h | ||
| 17 | + * \brief CausalConv1d 算子图模式 IR 定义(对齐 xllm_ops op_host/causal_conv1d_def.cpp) | ||
| 18 | + * | ||
| 19 | + * 输入: | ||
| 20 | + * - x: (batch, seqlen, dim) / (cu_seqlen, dim) / decode 2D,FLOAT16/BF16,dim-last | ||
| 21 | + * - weight: (width, dim),width ∈ [2,4] | ||
| 22 | + * - bias: 可选 | ||
| 23 | + * - convStates: (num_cache_lines, state_len, dim),state_len >= width-1 | ||
| 24 | + * - queryStartLoc / cacheIndices / initialStateMode / numAcceptedTokens: 可选 INT64 | ||
| 25 | + * 输出: | ||
| 26 | + * - y: 与 x 同 shape | ||
| 27 | + * 属性: | ||
| 28 | + * - activationMode: 0=NONE 1=SILU 2=SILU_PACKED_QKV | ||
| 29 | + * - padSlotId: 默认 -1 | ||
| 30 | + * - runMode: 0=prefill/fn 1=decode/update | ||
| 31 | + */ | ||
| 32 | + | ||
| 33 | +#ifndef OPS_OP_PROTO_INC_CAUSAL_CONV1D_H_ | ||
| 34 | +#define OPS_OP_PROTO_INC_CAUSAL_CONV1D_H_ | ||
| 35 | + | ||
| 36 | +#include "graph/operator_reg.h" | ||
| 37 | +#include "graph/types.h" | ||
| 38 | + | ||
| 39 | +namespace ge { | ||
| 40 | + | ||
| 41 | +REG_OP(CausalConv1d) | ||
| 42 | + .INPUT(x, TensorType({DT_FLOAT16, DT_BF16})) | ||
| 43 | + .INPUT(weight, TensorType({DT_FLOAT16, DT_BF16})) | ||
| 44 | + .OPTIONAL_INPUT(bias, TensorType({DT_FLOAT16, DT_BF16})) | ||
| 45 | + .INPUT(convStates, TensorType({DT_FLOAT16, DT_BF16})) | ||
| 46 | + .OPTIONAL_INPUT(queryStartLoc, TensorType({DT_INT64})) | ||
| 47 | + .OPTIONAL_INPUT(cacheIndices, TensorType({DT_INT64})) | ||
| 48 | + .OPTIONAL_INPUT(initialStateMode, TensorType({DT_INT64})) | ||
| 49 | + .OPTIONAL_INPUT(numAcceptedTokens, TensorType({DT_INT64})) | ||
| 50 | + .OUTPUT(y, TensorType({DT_FLOAT16, DT_BF16})) | ||
| 51 | + .ATTR(activationMode, Int, 0) | ||
| 52 | + .ATTR(padSlotId, Int, -1) | ||
| 53 | + .ATTR(runMode, Int, 0) | ||
| 54 | + .OP_END_FACTORY_REG(CausalConv1d) | ||
| 55 | + | ||
| 56 | +} // namespace ge | ||
| 57 | + | ||
| 58 | +#endif // OPS_OP_PROTO_INC_CAUSAL_CONV1D_H_ | ||
| @@ -0,0 +1,90 @@ | |||
| 1 | +set(host_ops_def_srcs | ||
| 2 | + ${CMAKE_CURRENT_SOURCE_DIR}/causal_conv1d_def.cpp | ||
| 3 | +) | ||
| 4 | +set(host_ops_infershape_srcs | ||
| 5 | + ${CMAKE_CURRENT_SOURCE_DIR}/causal_conv1d_proto.cpp | ||
| 6 | +) | ||
| 7 | +set(host_ops_tiling_srcs | ||
| 8 | + ${CMAKE_CURRENT_SOURCE_DIR}/causal_conv1d_tiling.cpp | ||
| 9 | +) | ||
| 10 | +set(host_ops_srcs | ||
| 11 | + ${host_ops_def_srcs} | ||
| 12 | + ${host_ops_infershape_srcs} | ||
| 13 | + ${host_ops_tiling_srcs} | ||
| 14 | +) | ||
| 15 | +npu_op_code_gen( | ||
| 16 | + SRC ${host_ops_srcs} | ||
| 17 | + PACKAGE ${package_name} | ||
| 18 | + OUT_DIR ${ASCEND_AUTOGEN_PATH} | ||
| 19 | + COMPILE_OPTIONS | ||
| 20 | + # the opbuild codegen hardcodes -std=c++11 first; a later -std wins, | ||
| 21 | + # and the vendored host code (xllm) requires C++17 relaxed constexpr | ||
| 22 | + -std=c++17 | ||
| 23 | + -I$ENV{ASCEND_HOME_PATH}/aarch64-linux/include | ||
| 24 | + -I$ENV{ASCEND_HOME_PATH}/aarch64-linux/asc/include/tiling | ||
| 25 | + -I$ENV{ASCEND_HOME_PATH}/aarch64-linux/pkg_inc | ||
| 26 | + -I$ENV{ASCEND_HOME_PATH}/aarch64-linux/pkg_inc/op_common | ||
| 27 | + -I$ENV{ASCEND_HOME_PATH}/aarch64-linux/pkg_inc/base | ||
| 28 | + -I$ENV{ASCEND_HOME_PATH}/aarch64-linux/pkg_inc/exe_graph | ||
| 29 | + -I$ENV{ASCEND_HOME_PATH}/aarch64-linux/pkg_inc/graph | ||
| 30 | + -I$ENV{ASCEND_HOME_PATH}/aarch64-linux/opp | ||
| 31 | + -I${CMAKE_CURRENT_SOURCE_DIR} | ||
| 32 | + -I${CMAKE_CURRENT_SOURCE_DIR}/../op_kernel | ||
| 33 | + -I${CMAKE_CURRENT_SOURCE_DIR}/../compat | ||
| 34 | +) | ||
| 35 | +npu_op_library(cust_optiling TILING | ||
| 36 | + ${host_ops_srcs} | ||
| 37 | +) | ||
| 38 | +set_target_properties(cust_optiling PROPERTIES CXX_STANDARD 17) | ||
| 39 | +target_include_directories(cust_optiling PRIVATE | ||
| 40 | + $ENV{ASCEND_HOME_PATH}/aarch64-linux/include | ||
| 41 | + $ENV{ASCEND_HOME_PATH}/aarch64-linux/asc/include/tiling | ||
| 42 | + $ENV{ASCEND_HOME_PATH}/aarch64-linux/pkg_inc | ||
| 43 | + $ENV{ASCEND_HOME_PATH}/aarch64-linux/pkg_inc/op_common | ||
| 44 | + $ENV{ASCEND_HOME_PATH}/aarch64-linux/pkg_inc/base | ||
| 45 | + $ENV{ASCEND_HOME_PATH}/aarch64-linux/opp | ||
| 46 | + ${CMAKE_CURRENT_SOURCE_DIR} | ||
| 47 | + ${CMAKE_CURRENT_SOURCE_DIR}/../op_kernel | ||
| 48 | + ${CMAKE_CURRENT_SOURCE_DIR}/../compat | ||
| 49 | +) | ||
| 50 | +set(SECURE_COMPILE_OPTIONS -fPIC -fstack-protector-all) | ||
| 51 | +set(SECURE_LINK_OPTIONS -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack) | ||
| 52 | +target_compile_options(cust_optiling PRIVATE ${SECURE_COMPILE_OPTIONS}) | ||
| 53 | +target_link_options(cust_optiling PRIVATE ${SECURE_LINK_OPTIONS}) | ||
| 54 | + | ||
| 55 | +# ============================================================================= | ||
| 56 | +# Graph proto library (auto-generated) | ||
| 57 | +# ============================================================================= | ||
| 58 | +file(GLOB proto_src ${ASCEND_AUTOGEN_PATH}/op_proto.cc) | ||
| 59 | +set_source_files_properties(${proto_src} PROPERTIES GENERATED TRUE) | ||
| 60 | +npu_op_library(cust_op_proto GRAPH | ||
| 61 | + ${host_ops_srcs} | ||
| 62 | + ${proto_src} | ||
| 63 | +) | ||
| 64 | + | ||
| 65 | +target_include_directories(cust_op_proto PRIVATE | ||
| 66 | + $ENV{ASCEND_HOME_PATH}/aarch64-linux/include | ||
| 67 | + $ENV{ASCEND_HOME_PATH}/aarch64-linux/asc/include/tiling | ||
| 68 | + $ENV{ASCEND_HOME_PATH}/aarch64-linux/pkg_inc | ||
| 69 | + $ENV{ASCEND_HOME_PATH}/aarch64-linux/pkg_inc/op_common | ||
| 70 | + $ENV{ASCEND_HOME_PATH}/aarch64-linux/pkg_inc/base | ||
| 71 | + $ENV{ASCEND_HOME_PATH}/aarch64-linux/opp | ||
| 72 | + ${CMAKE_CURRENT_SOURCE_DIR} | ||
| 73 | + ${CMAKE_CURRENT_SOURCE_DIR}/../compat | ||
| 74 | +) | ||
| 75 | +set_target_properties(cust_op_proto PROPERTIES CXX_STANDARD 17) | ||
| 76 | +target_compile_options(cust_op_proto PRIVATE ${SECURE_COMPILE_OPTIONS}) | ||
| 77 | +target_link_options(cust_op_proto PRIVATE ${SECURE_LINK_OPTIONS}) | ||
| 78 | + | ||
| 79 | +# ============================================================================= | ||
| 80 | +# Add libraries to package | ||
| 81 | +# ============================================================================= | ||
| 82 | +set(package_libs | ||
| 83 | + cust_optiling | ||
| 84 | + cust_op_proto | ||
| 85 | +) | ||
| 86 | + | ||
| 87 | +npu_op_package_add(${package_name} | ||
| 88 | + LIBRARY | ||
| 89 | + ${package_libs} | ||
| 90 | +) | ||
| @@ -0,0 +1,94 @@ | |||
| 1 | +/** | ||
| 2 | + * This program is free software, you can redistribute it and/or modify it. | ||
| 3 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. | ||
| 4 | + * This file is a part of the CANN Open Software. | ||
| 5 | + * Licensed under CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 6 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 7 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING | ||
| 8 | + * BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 9 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 10 | + */ | ||
| 11 | + | ||
| 12 | +/*! | ||
| 13 | + * \file causal_conv1d_def.cpp | ||
| 14 | + * \brief | ||
| 15 | + */ | ||
| 16 | +#include "register/op_def_registry.h" | ||
| 17 | + | ||
| 18 | +namespace ops { | ||
| 19 | + | ||
| 20 | +class CausalConv1d : public OpDef { | ||
| 21 | +public: | ||
| 22 | + explicit CausalConv1d(const char* name) : OpDef(name) | ||
| 23 | + { | ||
| 24 | + this->Input("x") | ||
| 25 | + .ParamType(REQUIRED) | ||
| 26 | + .DataType({ge::DT_FLOAT16, ge::DT_BF16}) | ||
| 27 | + .FormatList({ge::FORMAT_ND}) | ||
| 28 | + .AutoContiguous(); | ||
| 29 | + this->Input("weight") | ||
| 30 | + .ParamType(REQUIRED) | ||
| 31 | + .DataType({ge::DT_FLOAT16, ge::DT_BF16}) | ||
| 32 | + .FormatList({ge::FORMAT_ND}) | ||
| 33 | + .AutoContiguous(); | ||
| 34 | + this->Input("bias") | ||
| 35 | + .ParamType(OPTIONAL) | ||
| 36 | + .DataType({ge::DT_FLOAT16, ge::DT_BF16}) | ||
| 37 | + .FormatList({ge::FORMAT_ND}) | ||
| 38 | + .AutoContiguous(); | ||
| 39 | + this->Input("convStates") | ||
| 40 | + .ParamType(REQUIRED) | ||
| 41 | + .DataType({ge::DT_FLOAT16, ge::DT_BF16}) | ||
| 42 | + .FormatList({ge::FORMAT_ND}) | ||
| 43 | + .AutoContiguous(); | ||
| 44 | + this->Input("queryStartLoc") | ||
| 45 | + .ParamType(OPTIONAL) | ||
| 46 | + .DataTypeList({ge::DT_INT64}) | ||
| 47 | + .FormatList({ge::FORMAT_ND}) | ||
| 48 | + .ValueDepend(OPTIONAL) | ||
| 49 | + .AutoContiguous(); | ||
| 50 | + this->Input("cacheIndices") | ||
| 51 | + .ParamType(OPTIONAL) | ||
| 52 | + .DataTypeList({ge::DT_INT64}) | ||
| 53 | + .FormatList({ge::FORMAT_ND}) | ||
| 54 | + .ValueDepend(OPTIONAL) | ||
| 55 | + .AutoContiguous(); | ||
| 56 | + this->Input("initialStateMode") | ||
| 57 | + .ParamType(OPTIONAL) | ||
| 58 | + .DataTypeList({ge::DT_INT64}) | ||
| 59 | + .FormatList({ge::FORMAT_ND}) | ||
| 60 | + .ValueDepend(OPTIONAL) | ||
| 61 | + .AutoContiguous(); | ||
| 62 | + this->Input("numAcceptedTokens") | ||
| 63 | + .ParamType(OPTIONAL) | ||
| 64 | + .DataTypeList({ge::DT_INT64}) | ||
| 65 | + .FormatList({ge::FORMAT_ND}) | ||
| 66 | + .ValueDepend(OPTIONAL) | ||
| 67 | + .AutoContiguous(); | ||
| 68 | + | ||
| 69 | + this->Output("y") | ||
| 70 | + .ParamType(REQUIRED) | ||
| 71 | + .DataType({ge::DT_FLOAT16, ge::DT_BF16}) | ||
| 72 | + .FormatList({ge::FORMAT_ND}) | ||
| 73 | + .AutoContiguous(); | ||
| 74 | + | ||
| 75 | + this->Attr("activationMode").AttrType(OPTIONAL).Int(0); | ||
| 76 | + this->Attr("padSlotId").AttrType(OPTIONAL).Int(-1); | ||
| 77 | + this->Attr("runMode").AttrType(OPTIONAL).Int(0); | ||
| 78 | + | ||
| 79 | + OpAICoreConfig aicoreConfig; | ||
| 80 | + aicoreConfig.DynamicCompileStaticFlag(true) | ||
| 81 | + .DynamicFormatFlag(false) | ||
| 82 | + .DynamicRankSupportFlag(true) | ||
| 83 | + .DynamicShapeSupportFlag(true) | ||
| 84 | + .NeedCheckSupportFlag(false) | ||
| 85 | + .PrecisionReduceFlag(true) | ||
| 86 | + .ExtendCfgInfo("coreType.value", "AiCore"); | ||
| 87 | + this->AICore().AddConfig("ascend910b", aicoreConfig); | ||
| 88 | + this->AICore().AddConfig("ascend910_93", aicoreConfig); | ||
| 89 | + this->AICore().AddConfig("ascend950", aicoreConfig); | ||
| 90 | + } | ||
| 91 | +}; | ||
| 92 | +OP_ADD(CausalConv1d); | ||
| 93 | + | ||
| 94 | +} // namespace ops | ||
| @@ -0,0 +1,41 @@ | |||
| 1 | +/** | ||
| 2 | + * This program is free software, you can redistribute it and/or modify it. | ||
| 3 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. | ||
| 4 | + * This file is a part of the CANN Open Software. | ||
| 5 | + * Licensed under CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 6 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 7 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING | ||
| 8 | + * BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 9 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 10 | + */ | ||
| 11 | + | ||
| 12 | +/*! | ||
| 13 | + * \file causal_conv1d_infershape.cpp | ||
| 14 | + * \brief | ||
| 15 | + */ | ||
| 16 | +#include "register/op_impl_registry.h" | ||
| 17 | +#include "tiling_base/error_log.h" | ||
| 18 | + | ||
| 19 | +using namespace ge; | ||
| 20 | + | ||
| 21 | +namespace ops { | ||
| 22 | +static constexpr int64_t IDX_0 = 0; | ||
| 23 | + | ||
| 24 | +static ge::graphStatus InferShapeCausalConv1d(gert::InferShapeContext* context) | ||
| 25 | +{ | ||
| 26 | + OP_LOGD(context->GetNodeName(), "Begin to do InferShapeCausalConv1d"); | ||
| 27 | + | ||
| 28 | + const gert::Shape* xShape = context->GetInputShape(IDX_0); | ||
| 29 | + OP_CHECK_NULL_WITH_CONTEXT(context, xShape); | ||
| 30 | + | ||
| 31 | + gert::Shape* yShape = context->GetOutputShape(IDX_0); | ||
| 32 | + OP_CHECK_NULL_WITH_CONTEXT(context, yShape); | ||
| 33 | + | ||
| 34 | + *yShape = *xShape; | ||
| 35 | + | ||
| 36 | + OP_LOGD(context->GetNodeName(), "End to do InferShapeCausalConv1d"); | ||
| 37 | + return GRAPH_SUCCESS; | ||
| 38 | +} | ||
| 39 | + | ||
| 40 | +IMPL_OP_INFERSHAPE(CausalConv1d).InferShape(InferShapeCausalConv1d); | ||
| 41 | +} // namespace ops | ||
| @@ -0,0 +1,234 @@ | |||
| 1 | +/** | ||
| 2 | + * This program is free software, you can redistribute it and/or modify it. | ||
| 3 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. | ||
| 4 | + * This file is a part of the CANN Open Software. | ||
| 5 | + * Licensed under CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 6 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 7 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING | ||
| 8 | + * BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 9 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 10 | + */ | ||
| 11 | + | ||
| 12 | +/*! | ||
| 13 | + * \file causal_conv1d_tiling.cpp | ||
| 14 | + * \brief Host tiling orchestrator for CausalConv1D. | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | +#include "causal_conv1d_tiling_planner.h" | ||
| 18 | +#include "causal_conv1d_tiling.h" | ||
| 19 | +#include "causal_conv1d_tiling_validation.h" | ||
| 20 | +#include <cstdio> | ||
| 21 | +#include <cstdlib> | ||
| 22 | + | ||
| 23 | +namespace optiling { | ||
| 24 | + | ||
| 25 | +//using namespace Ops::Transformer::OpTiling; | ||
| 26 | +using namespace causal_conv1d_host; | ||
| 27 | + | ||
| 28 | +namespace { | ||
| 29 | + | ||
| 30 | +bool HostTilingDebugEnabled() | ||
| 31 | +{ | ||
| 32 | + const char *value = std::getenv("CAUSAL_CONV1D_HOST_TILING_DEBUG"); | ||
| 33 | + return value != nullptr && value[0] != '\0' && value[0] != '0'; | ||
| 34 | +} | ||
| 35 | + | ||
| 36 | +FnHostPlan ChoosePackedQkvFnHostPlan(gert::TilingContext *context, const CausalConv1dTilingData &tiling, | ||
| 37 | + uint64_t ubSize, uint32_t coreNum) | ||
| 38 | +{ | ||
| 39 | + FnHostPlan plan; | ||
| 40 | + if (tiling.inputMode != 0 || tiling.batch <= 0 || tiling.cuSeqlen <= 0 || tiling.dim <= 0 || | ||
| 41 | + tiling.packedHeadDim <= 0 || coreNum == 0) { | ||
| 42 | + return plan; | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + if (tiling.dim == CAUSAL_CONV1D_PACKED_QKV_LEGACY_DIM && | ||
| 46 | + tiling.packedQDim + tiling.packedKDim == CAUSAL_CONV1D_PACKED_QK_LEGACY_DIM && | ||
| 47 | + tiling.packedVDim == CAUSAL_CONV1D_PACKED_V_LEGACY_DIM) { | ||
| 48 | + plan.baseDimChoice.baseDim = CAUSAL_CONV1D_PACKED_V_LEGACY_DIM; | ||
| 49 | + plan.baseDimChoice.baseDimCnt = 2; | ||
| 50 | + } else if (tiling.dim <= MAX_DIM_TILE_SIZE) { | ||
| 51 | + plan.baseDimChoice.baseDim = tiling.dim; | ||
| 52 | + plan.baseDimChoice.baseDimCnt = 1; | ||
| 53 | + } else { | ||
| 54 | + const int64_t ubLimitedBaseDim = | ||
| 55 | + AlignDownInt64(ComputeFnUbLimitedBaseDim(ubSize), tiling.packedHeadDim); | ||
| 56 | + if (ubLimitedBaseDim <= 0) { | ||
| 57 | + return plan; | ||
| 58 | + } | ||
| 59 | + const int64_t baseDimCnt = CeilDivInt64(tiling.dim, ubLimitedBaseDim); | ||
| 60 | + const int64_t balancedBaseDim = | ||
| 61 | + AlignUpInt64(CeilDivInt64(tiling.dim, baseDimCnt), tiling.packedHeadDim); | ||
| 62 | + plan.baseDimChoice.baseDim = | ||
| 63 | + (balancedBaseDim > 0 && balancedBaseDim <= ubLimitedBaseDim) ? balancedBaseDim : ubLimitedBaseDim; | ||
| 64 | + plan.baseDimChoice.baseDimCnt = CeilDivInt64(tiling.dim, plan.baseDimChoice.baseDim); | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + plan.executionPlan = | ||
| 68 | + static_cast<FnExecutionPlan>(ResolveFnExecutionPlan(plan.baseDimChoice.baseDimCnt)); | ||
| 69 | + plan.caseKind = (plan.baseDimChoice.baseDimCnt == 1) ? FN_TILING_CASE_TOKEN_FIRST | ||
| 70 | + : FN_TILING_CASE_TOKEN_DIM_CO_SPLIT; | ||
| 71 | + plan.baseDimChoice.gridSize = tiling.batch * plan.baseDimChoice.baseDimCnt; | ||
| 72 | + plan.tokenBlockChoice = | ||
| 73 | + ChooseUnifiedFnTokenBlockPlan(context, tiling, plan.baseDimChoice, plan.executionPlan, coreNum); | ||
| 74 | + if (!plan.tokenBlockChoice.enabled) { | ||
| 75 | + return {}; | ||
| 76 | + } | ||
| 77 | + plan.tokenCoreMapping = BuildFnTokenCoreMappingChoice( | ||
| 78 | + plan.tokenBlockChoice.tokenBlockCnt, plan.baseDimChoice.baseDimCnt, plan.executionPlan, coreNum); | ||
| 79 | + if (plan.tokenCoreMapping.tokenCoreBudget <= 0 || plan.tokenCoreMapping.blockDim <= 0) { | ||
| 80 | + return {}; | ||
| 81 | + } | ||
| 82 | + plan.tokenCoreMapping.blockDim = std::min<int64_t>(plan.tokenCoreMapping.blockDim, coreNum); | ||
| 83 | + return plan; | ||
| 84 | +} | ||
| 85 | + | ||
| 86 | +} // namespace | ||
| 87 | + | ||
| 88 | +ge::graphStatus CausalConv1dTilingFunc(gert::TilingContext *context) | ||
| 89 | +{ | ||
| 90 | + uint64_t ubSize = 0; | ||
| 91 | + uint32_t coreNum = 0; | ||
| 92 | + OP_CHECK_IF(GetPlatformInfo(context, ubSize, coreNum) != ge::GRAPH_SUCCESS, | ||
| 93 | + OP_LOGE(context, "GetPlatformInfo error"), return ge::GRAPH_FAILED); | ||
| 94 | + | ||
| 95 | + CausalConv1dTilingData *tiling = context->GetTilingData<CausalConv1dTilingData>(); | ||
| 96 | + OP_CHECK_NULL_WITH_CONTEXT(context, tiling); | ||
| 97 | + OP_CHECK_IF(memset_s(tiling, sizeof(CausalConv1dTilingData), 0, sizeof(CausalConv1dTilingData)) != EOK, | ||
| 98 | + OP_LOGE(context, "set tiling data error"), return ge::GRAPH_FAILED); | ||
| 99 | + | ||
| 100 | + CausalConv1dAttrInfo attrInfo; | ||
| 101 | + OP_CHECK_IF(GetAttrsInfo(context, attrInfo) != ge::GRAPH_SUCCESS, OP_LOGE(context, "GetAttrsInfo error"), | ||
| 102 | + return ge::GRAPH_FAILED); | ||
| 103 | + bool hasBias = false; | ||
| 104 | + OP_CHECK_IF(GetShapeDtypeInfo(context, attrInfo, *tiling, hasBias) != ge::GRAPH_SUCCESS, | ||
| 105 | + OP_LOGE(context, "GetShapeDtypeInfo error"), return ge::GRAPH_FAILED); | ||
| 106 | + | ||
| 107 | + const int64_t &dim = tiling->dim; | ||
| 108 | + const int64_t &batch = tiling->batch; | ||
| 109 | + OP_CHECK_IF(dim <= 0 || batch <= 0, OP_LOGE(context, "dim/batch must be positive"), return ge::GRAPH_FAILED); | ||
| 110 | + | ||
| 111 | + const uint32_t runModeKey = static_cast<uint32_t>(attrInfo.runMode); | ||
| 112 | + const bool &isFn = (runModeKey == CAUSAL_CONV1D_TPL_RUN_MODE_FN); | ||
| 113 | + const bool &hasActivation = (attrInfo.activationMode != 0); | ||
| 114 | + const char *plannerModeTag = "update"; | ||
| 115 | + DimTileChoice baseDimChoice; | ||
| 116 | + FnExecutionPlan fnExecutionPlan = FN_EXECUTION_PLAN_INVALID; | ||
| 117 | + FnHostPlan fnHostPlan; | ||
| 118 | + const int64_t *qslData = nullptr; | ||
| 119 | + if (isFn && tiling->inputMode == 0) { | ||
| 120 | + const gert::Tensor *qslTensor = context->GetOptionalInputTensor(QUERY_START_LOC_INDEX); | ||
| 121 | + qslData = (qslTensor != nullptr) ? qslTensor->GetData<int64_t>() : nullptr; | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + if (isFn) { | ||
| 125 | + fnHostPlan = attrInfo.activationMode == CAUSAL_CONV1D_ACTIVATION_SILU_PACKED_QKV | ||
| 126 | + ? ChoosePackedQkvFnHostPlan(context, *tiling, ubSize, coreNum) | ||
| 127 | + : ChooseFnHostPlan(context, *tiling, ubSize, coreNum); | ||
| 128 | + plannerModeTag = GetFnTilingCaseName(fnHostPlan.caseKind); | ||
| 129 | + baseDimChoice = fnHostPlan.baseDimChoice; | ||
| 130 | + fnExecutionPlan = fnHostPlan.executionPlan; | ||
| 131 | + } else { | ||
| 132 | + baseDimChoice = ChooseCanonicalUpdateBaseDimChoice(context, tiling->batch, tiling->dim, coreNum); | ||
| 133 | + } | ||
| 134 | + | ||
| 135 | + OP_CHECK_IF(baseDimChoice.baseDim <= 0 || baseDimChoice.baseDimCnt <= 0 || baseDimChoice.gridSize <= 0, | ||
| 136 | + OP_LOGE(context, "invalid dim tile size selection"), return ge::GRAPH_FAILED); | ||
| 137 | + | ||
| 138 | + int64_t effectiveGridSize = baseDimChoice.gridSize; | ||
| 139 | + | ||
| 140 | + if (isFn) { | ||
| 141 | + OP_CHECK_IF(fnHostPlan.caseKind == FN_TILING_CASE_INVALID || fnExecutionPlan == FN_EXECUTION_PLAN_INVALID || | ||
| 142 | + !fnHostPlan.tokenBlockChoice.enabled || fnHostPlan.tokenBlockChoice.tokenBlockSize <= 0 || | ||
| 143 | + fnHostPlan.tokenBlockChoice.tokenBlockCnt <= 0 || fnHostPlan.tokenBlockChoice.gridSize <= 0 || | ||
| 144 | + fnHostPlan.tokenCoreMapping.tokenCoreBudget <= 0 || fnHostPlan.tokenCoreMapping.blockDim <= 0, | ||
| 145 | + OP_LOGE(context, "runMode=0 must resolve a valid unified token tiling plan"), | ||
| 146 | + return ge::GRAPH_FAILED); | ||
| 147 | + | ||
| 148 | + tiling->tokenBlockSize = fnHostPlan.tokenBlockChoice.tokenBlockSize; | ||
| 149 | + tiling->tokenBlockCnt = fnHostPlan.tokenBlockChoice.tokenBlockCnt; | ||
| 150 | + effectiveGridSize = fnHostPlan.tokenBlockChoice.gridSize; | ||
| 151 | + if (tiling->inputMode == 0) { | ||
| 152 | + fnHostPlan.tokenSeqRangePlan = | ||
| 153 | + BuildFnTokenSeqRangePlan(qslData, tiling->batch, tiling->tokenBlockSize, tiling->tokenBlockCnt); | ||
| 154 | + if (fnHostPlan.tokenSeqRangePlan.enabled) { | ||
| 155 | + tiling->hasExplicitTokenSeqRanges = 1; | ||
| 156 | + tiling->explicitTokenSeqRangeCount = fnHostPlan.tokenSeqRangePlan.rangeCount; | ||
| 157 | + for (int64_t i = 0; i < fnHostPlan.tokenSeqRangePlan.rangeCount; ++i) { | ||
| 158 | + tiling->tokenTileStartSeq[i] = fnHostPlan.tokenSeqRangePlan.tokenTileStartSeq[i]; | ||
| 159 | + tiling->tokenTileEndSeq[i] = fnHostPlan.tokenSeqRangePlan.tokenTileEndSeq[i]; | ||
| 160 | + } | ||
| 161 | + } else if (qslData != nullptr && tiling->tokenBlockCnt > MAX_FN_TOKEN_SEQ_RANGE_COUNT) { | ||
| 162 | + OP_LOGD(context, | ||
| 163 | + "FnTokenSeqRanges disabled: tokenBlockCnt[%ld] exceeds fixed tiling capacity[%ld].", | ||
| 164 | + tiling->tokenBlockCnt, MAX_FN_TOKEN_SEQ_RANGE_COUNT); | ||
| 165 | + } | ||
| 166 | + } | ||
| 167 | + OP_LOGD(context, | ||
| 168 | + "FnHostPlan(case=%s): inputMode[%ld], dim[%ld], cuSeqlen[%ld], baseDim[%ld], baseDimCnt[%ld], " | ||
| 169 | + "tokenCoreBudget[%ld], tokenBlockSize[%ld], tokenBlockCnt[%ld], tokenBlocksPerCore[%ld], " | ||
| 170 | + "tokenCoreTailCnt[%ld], explicitSeqRanges[%ld], baseGrid[%ld], phase1Grid[%ld], mappedBlockDim[%ld].", | ||
| 171 | + plannerModeTag, tiling->inputMode, tiling->dim, tiling->cuSeqlen, baseDimChoice.baseDim, | ||
| 172 | + baseDimChoice.baseDimCnt, fnHostPlan.tokenCoreMapping.tokenCoreBudget, | ||
| 173 | + fnHostPlan.tokenBlockChoice.tokenBlockSize, fnHostPlan.tokenBlockChoice.tokenBlockCnt, | ||
| 174 | + fnHostPlan.tokenCoreMapping.tokenBlocksPerCore, fnHostPlan.tokenCoreMapping.tokenCoreTailCnt, | ||
| 175 | + tiling->hasExplicitTokenSeqRanges, | ||
| 176 | + baseDimChoice.gridSize, fnHostPlan.tokenBlockChoice.gridSize, fnHostPlan.tokenCoreMapping.blockDim); | ||
| 177 | + } | ||
| 178 | + | ||
| 179 | + uint32_t blockDim = | ||
| 180 | + (effectiveGridSize < static_cast<int64_t>(coreNum)) ? static_cast<uint32_t>(effectiveGridSize) : coreNum; | ||
| 181 | + if (isFn) { | ||
| 182 | + const int64_t mappedBlockDim = std::min<int64_t>(effectiveGridSize, fnHostPlan.tokenCoreMapping.blockDim); | ||
| 183 | + OP_CHECK_IF(mappedBlockDim <= 0, OP_LOGE(context, "invalid mapped blockDim for runMode=0"), | ||
| 184 | + return ge::GRAPH_FAILED); | ||
| 185 | + blockDim = static_cast<uint32_t>(mappedBlockDim); | ||
| 186 | + } | ||
| 187 | + | ||
| 188 | + OP_LOGD(context, | ||
| 189 | + "Tiling result: mode[%s], batch[%ld], dim[%ld], baseDim[%ld], baseDimCnt[%ld], gridSize[%ld], " | ||
| 190 | + "effectiveGrid[%ld], blockDim[%u], coreNum[%u], tokenTiling[%ld,%ld], hasActivation[%d], hasBias[%d], " | ||
| 191 | + "fnPlan[%ld].", | ||
| 192 | + plannerModeTag, batch, dim, baseDimChoice.baseDim, baseDimChoice.baseDimCnt, baseDimChoice.gridSize, | ||
| 193 | + effectiveGridSize, blockDim, coreNum, tiling->tokenBlockSize, tiling->tokenBlockCnt, | ||
| 194 | + static_cast<int32_t>(hasActivation), static_cast<int32_t>(hasBias), static_cast<int64_t>(fnExecutionPlan)); | ||
| 195 | + | ||
| 196 | + context->SetBlockDim(blockDim); | ||
| 197 | + tiling->baseDim = baseDimChoice.baseDim; | ||
| 198 | + tiling->baseDimCnt = baseDimChoice.baseDimCnt; | ||
| 199 | + const uint32_t fnPlanKey = NormalizeFnPlanTilingKey(runModeKey, fnExecutionPlan); | ||
| 200 | + const uint32_t widthKey = NormalizeWidthTilingKey(runModeKey, static_cast<int32_t>(tiling->width)); | ||
| 201 | + | ||
| 202 | + if (isFn && tiling->hasInitialStateMode != 0) { | ||
| 203 | + constexpr int64_t kDtypeSize = 2; | ||
| 204 | + constexpr int64_t kSyncBytesPerBlock = 32; | ||
| 205 | + const int64_t snapshotWorkspaceSize = tiling->numCacheLines * tiling->stateLen * tiling->dim * kDtypeSize; | ||
| 206 | + const int64_t workspaceSize = | ||
| 207 | + ASCENDC_RESERVED_WORKSPACE_SIZE + snapshotWorkspaceSize; | ||
| 208 | + OP_CHECK_IF(SetWorkspaceSize(context, static_cast<size_t>(workspaceSize)) != ge::GRAPH_SUCCESS, | ||
| 209 | + OP_LOGE(context, "SetWorkspaceSize error"), return ge::GRAPH_FAILED); | ||
| 210 | + OP_CHECK_IF(context->SetScheduleMode(1) != ge::GRAPH_SUCCESS, | ||
| 211 | + OP_LOGE(context, "SetScheduleMode(1) error"), return ge::GRAPH_FAILED); | ||
| 212 | + tiling->hasInitStateWorkspace = 1; | ||
| 213 | + } else { | ||
| 214 | + OP_CHECK_IF(SetWorkspaceSize(context, 0) != ge::GRAPH_SUCCESS, OP_LOGE(context, "SetWorkspaceSize error"), | ||
| 215 | + return ge::GRAPH_FAILED); | ||
| 216 | + tiling->hasInitStateWorkspace = 0; | ||
| 217 | + } | ||
| 218 | + | ||
| 219 | + const uint64_t tilingKey = GET_TPL_TILING_KEY(runModeKey, widthKey, fnPlanKey); | ||
| 220 | + context->SetTilingKey(tilingKey); | ||
| 221 | + return ge::GRAPH_SUCCESS; | ||
| 222 | +} | ||
| 223 | + | ||
| 224 | +ge::graphStatus TilingParseForCausalConv1d(gert::TilingParseContext *context) | ||
| 225 | +{ | ||
| 226 | + OP_LOGD(context, "Enter TilingParseForCausalConv1d."); | ||
| 227 | + return ge::GRAPH_SUCCESS; | ||
| 228 | +} | ||
| 229 | + | ||
| 230 | +IMPL_OP_OPTILING(CausalConv1d) | ||
| 231 | + .Tiling(CausalConv1dTilingFunc) | ||
| 232 | + .TilingParse<CausalConv1dCompileInfo>(TilingParseForCausalConv1d); | ||
| 233 | + | ||
| 234 | +} // namespace optiling | ||
| @@ -0,0 +1,13 @@ | |||
| 1 | +#ifndef CAUSAL_CONV1D_HOST_TILING_H | ||
| 2 | +#define CAUSAL_CONV1D_HOST_TILING_H | ||
| 3 | + | ||
| 4 | +#include "register/op_impl_registry.h" | ||
| 5 | + | ||
| 6 | +namespace optiling { | ||
| 7 | + | ||
| 8 | +ge::graphStatus CausalConv1dTilingFunc(gert::TilingContext *context); | ||
| 9 | +ge::graphStatus TilingParseForCausalConv1d(gert::TilingParseContext *context); | ||
| 10 | + | ||
| 11 | +} // namespace optiling | ||
| 12 | + | ||
| 13 | +#endif // CAUSAL_CONV1D_HOST_TILING_H | ||
| @@ -0,0 +1,311 @@ | |||
| 1 | +/** | ||
| 2 | + * This program is free software, you can redistribute it and/or modify it. | ||
| 3 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. | ||
| 4 | + * This file is a part of the CANN Open Software. | ||
| 5 | + * Licensed under CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 6 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 7 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING | ||
| 8 | + * BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 9 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 10 | + */ | ||
| 11 | + | ||
| 12 | +#ifndef CAUSAL_CONV1D_TILING_PLANNER_H | ||
| 13 | +#define CAUSAL_CONV1D_TILING_PLANNER_H | ||
| 14 | + | ||
| 15 | +#include "causal_conv1d_tiling_utils.h" | ||
| 16 | +#include "../op_kernel/causal_conv1d_tiling_data.h" | ||
| 17 | +#include "tiling_base/error_log.h" | ||
| 18 | + | ||
| 19 | +namespace optiling::causal_conv1d_host { | ||
| 20 | + | ||
| 21 | +//using namespace Ops::Transformer::OpTiling; | ||
| 22 | + | ||
| 23 | +inline DimTileChoice ChooseCanonicalUpdateBaseDimChoice(gert::TilingContext *context, int64_t batch, int64_t dim, | ||
| 24 | + uint32_t coreNum) | ||
| 25 | +{ | ||
| 26 | + const int64_t candidates[] = {4096, 2048, 1024, 512, 384, 192}; | ||
| 27 | + | ||
| 28 | + auto chooseOnce = [&](bool requireExactDiv) -> DimTileChoice { | ||
| 29 | + DimTileChoice bestOver; | ||
| 30 | + int64_t bestOverGap = std::numeric_limits<int64_t>::max(); | ||
| 31 | + DimTileChoice bestUnder; | ||
| 32 | + | ||
| 33 | + for (int64_t baseDim : candidates) { | ||
| 34 | + if (baseDim <= 0) { | ||
| 35 | + continue; | ||
| 36 | + } | ||
| 37 | + if (requireExactDiv && (dim % baseDim != 0)) { | ||
| 38 | + continue; | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + const int64_t baseDimCnt = requireExactDiv ? (dim / baseDim) : CeilDivInt64(dim, baseDim); | ||
| 42 | + const int64_t gridSize = batch * baseDimCnt; | ||
| 43 | + if (gridSize <= 0) { | ||
| 44 | + continue; | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + OP_LOGD(context, | ||
| 48 | + "DimTile(update) candidate[%s]: baseDim[%ld], baseDimCnt[%ld], gridSize[%ld], coreNum[%u].", | ||
| 49 | + requireExactDiv ? "exact" : "tail", baseDim, baseDimCnt, gridSize, coreNum); | ||
| 50 | + if (gridSize >= static_cast<int64_t>(coreNum)) { | ||
| 51 | + const int64_t gap = gridSize - static_cast<int64_t>(coreNum); | ||
| 52 | + if (gap < bestOverGap) { | ||
| 53 | + bestOver = {baseDim, baseDimCnt, gridSize}; | ||
| 54 | + bestOverGap = gap; | ||
| 55 | + } | ||
| 56 | + } else if (gridSize > bestUnder.gridSize || | ||
| 57 | + (gridSize == bestUnder.gridSize && baseDim < bestUnder.baseDim)) { | ||
| 58 | + bestUnder = {baseDim, baseDimCnt, gridSize}; | ||
| 59 | + } | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + return (bestOver.baseDim != 0) ? bestOver : bestUnder; | ||
| 63 | + }; | ||
| 64 | + | ||
| 65 | + DimTileChoice result = chooseOnce(true); | ||
| 66 | + if (result.baseDim == 0) { | ||
| 67 | + result = chooseOnce(false); | ||
| 68 | + } | ||
| 69 | + OP_LOGD(context, "DimTile(update) chosen: baseDim[%ld], baseDimCnt[%ld], gridSize[%ld].", result.baseDim, | ||
| 70 | + result.baseDimCnt, result.gridSize); | ||
| 71 | + return result; | ||
| 72 | +} | ||
| 73 | + | ||
| 74 | +inline int64_t ResolveFnTokenCoreBudget(int64_t baseDimCnt, FnExecutionPlan fnExecutionPlan, uint32_t coreNum) | ||
| 75 | +{ | ||
| 76 | + if (baseDimCnt <= 0 || coreNum == 0 || fnExecutionPlan == FN_EXECUTION_PLAN_INVALID) { | ||
| 77 | + return 0; | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + int64_t tokenCoreBudget = static_cast<int64_t>(coreNum); | ||
| 81 | + if (fnExecutionPlan == FN_EXECUTION_PLAN_CUTBSD) { | ||
| 82 | + tokenCoreBudget = std::max<int64_t>(1, tokenCoreBudget / baseDimCnt); | ||
| 83 | + } | ||
| 84 | + return tokenCoreBudget; | ||
| 85 | +} | ||
| 86 | + | ||
| 87 | +inline VarlenTokenTileChoice ChooseFnTokenBlockChoice(int64_t cuSeqlen, int64_t baseDimCnt, | ||
| 88 | + FnExecutionPlan fnExecutionPlan, uint32_t coreNum); | ||
| 89 | + | ||
| 90 | +inline int64_t ComputeFnUbLimitedBaseDim(uint64_t ubSize) | ||
| 91 | +{ | ||
| 92 | + if (ubSize <= static_cast<uint64_t>(FN_UB_RESERVED_BYTES)) { | ||
| 93 | + return 0; | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + const int64_t bytesPerElem = (RING_SLOT_CNT * BF16_FP16_ELEM_BYTES) + (FN_OUT_SLOT_CNT * BF16_FP16_ELEM_BYTES) + | ||
| 97 | + (FN_CALC_FP32_SLOT_CNT * static_cast<int64_t>(sizeof(float))); | ||
| 98 | + const int64_t budgetBytes = static_cast<int64_t>(ubSize) - FN_UB_RESERVED_BYTES; | ||
| 99 | + const int64_t ubLimitedBaseDim = AlignDownInt64(budgetBytes / bytesPerElem, DIM_ALIGN_ELEMS); | ||
| 100 | + return std::min<int64_t>(MAX_DIM_TILE_SIZE, ubLimitedBaseDim); | ||
| 101 | +} | ||
| 102 | + | ||
| 103 | +inline DimTileChoice ChooseFnTokenFirstBaseDimChoice(int64_t dim) | ||
| 104 | +{ | ||
| 105 | + if (dim <= 0 || dim > MAX_DIM_TILE_SIZE) { | ||
| 106 | + return {}; | ||
| 107 | + } | ||
| 108 | + return {dim, 1, 1}; | ||
| 109 | +} | ||
| 110 | + | ||
| 111 | +inline DimTileChoice ChooseFnTokenDimCoSplitBaseDimChoice(gert::TilingContext *context, int64_t dim, uint64_t ubSize, | ||
| 112 | + uint32_t coreNum) | ||
| 113 | +{ | ||
| 114 | + if (dim <= 0) { | ||
| 115 | + return {}; | ||
| 116 | + } | ||
| 117 | + | ||
| 118 | + const int64_t ubLimitedBaseDim = ComputeFnUbLimitedBaseDim(ubSize); | ||
| 119 | + if (ubLimitedBaseDim <= 0) { | ||
| 120 | + OP_LOGD(context, "FnDimCoSplit: UB budget is too small to form a valid baseDim."); | ||
| 121 | + return {}; | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + DimTileChoice result; | ||
| 125 | + result.baseDim = ubLimitedBaseDim; | ||
| 126 | + result.baseDimCnt = CeilDivInt64(dim, result.baseDim); | ||
| 127 | + result.gridSize = result.baseDimCnt; | ||
| 128 | + | ||
| 129 | + const int64_t balancedBaseDim = AlignUpInt64(CeilDivInt64(dim, result.baseDimCnt), DIM_ALIGN_ELEMS); | ||
| 130 | + if (balancedBaseDim > 0 && balancedBaseDim <= ubLimitedBaseDim) { | ||
| 131 | + result.baseDim = balancedBaseDim; | ||
| 132 | + result.baseDimCnt = CeilDivInt64(dim, result.baseDim); | ||
| 133 | + result.gridSize = result.baseDimCnt; | ||
| 134 | + } | ||
| 135 | + | ||
| 136 | + if (coreNum == 0 || result.baseDimCnt <= 1 || result.baseDimCnt >= static_cast<int64_t>(coreNum) || | ||
| 137 | + (coreNum % result.baseDimCnt == 0)) { | ||
| 138 | + OP_LOGD(context, | ||
| 139 | + "FnDimCoSplit: dim[%ld], ubLimitedBaseDim[%ld], balancedBaseDim[%ld], baseDimCnt[%ld], coreNum[%u], " | ||
| 140 | + "adjusted[%d].", | ||
| 141 | + dim, ubLimitedBaseDim, result.baseDim, result.baseDimCnt, coreNum, 0); | ||
| 142 | + return result; | ||
| 143 | + } | ||
| 144 | + | ||
| 145 | + int64_t adjustedBaseDimCnt = result.baseDimCnt; | ||
| 146 | + while (adjustedBaseDimCnt < static_cast<int64_t>(coreNum) && (coreNum % adjustedBaseDimCnt != 0)) { | ||
| 147 | + ++adjustedBaseDimCnt; | ||
| 148 | + } | ||
| 149 | + | ||
| 150 | + if (adjustedBaseDimCnt >= static_cast<int64_t>(coreNum)) { | ||
| 151 | + OP_LOGD(context, | ||
| 152 | + "FnDimCoSplit: keep baseDimCnt[%ld] because no divisible adjustment exists under coreNum[%u].", | ||
| 153 | + result.baseDimCnt, coreNum); | ||
| 154 | + return result; | ||
| 155 | + } | ||
| 156 | + | ||
| 157 | + const int64_t adjustedBaseDim = AlignUpInt64(CeilDivInt64(dim, adjustedBaseDimCnt), DIM_ALIGN_ELEMS); | ||
| 158 | + if (adjustedBaseDim <= 0 || adjustedBaseDim > ubLimitedBaseDim || adjustedBaseDim > MAX_DIM_TILE_SIZE) { | ||
| 159 | + OP_LOGD(context, | ||
| 160 | + "FnDimCoSplit: rejected adjusted baseDim[%ld] with baseDimCnt[%ld], ubLimitedBaseDim[%ld].", | ||
| 161 | + adjustedBaseDim, adjustedBaseDimCnt, ubLimitedBaseDim); | ||
| 162 | + return result; | ||
| 163 | + } | ||
| 164 | + | ||
| 165 | + result.baseDim = adjustedBaseDim; | ||
| 166 | + result.baseDimCnt = CeilDivInt64(dim, result.baseDim); | ||
| 167 | + result.gridSize = result.baseDimCnt; | ||
| 168 | + OP_LOGD(context, | ||
| 169 | + "FnDimCoSplit: dim[%ld], ubLimitedBaseDim[%ld], adjustedBaseDim[%ld], baseDimCnt[%ld], coreNum[%u].", | ||
| 170 | + dim, ubLimitedBaseDim, result.baseDim, result.baseDimCnt, coreNum); | ||
| 171 | + return result; | ||
| 172 | +} | ||
| 173 | + | ||
| 174 | +inline TokenCoreMappingChoice BuildFnTokenCoreMappingChoice(int64_t tokenBlockCnt, int64_t baseDimCnt, | ||
| 175 | + FnExecutionPlan fnExecutionPlan, uint32_t coreNum) | ||
| 176 | +{ | ||
| 177 | + TokenCoreMappingChoice mapping; | ||
| 178 | + mapping.tokenCoreBudget = ResolveFnTokenCoreBudget(baseDimCnt, fnExecutionPlan, coreNum); | ||
| 179 | + if (tokenBlockCnt <= 0 || mapping.tokenCoreBudget <= 0 || baseDimCnt <= 0) { | ||
| 180 | + return mapping; | ||
| 181 | + } | ||
| 182 | + | ||
| 183 | + mapping.tokenBlocksPerCore = CeilDivInt64(tokenBlockCnt, mapping.tokenCoreBudget); | ||
| 184 | + mapping.tokenCoreTailCnt = | ||
| 185 | + tokenBlockCnt - (std::max<int64_t>(0, mapping.tokenBlocksPerCore - 1) * mapping.tokenCoreBudget); | ||
| 186 | + if (mapping.tokenCoreTailCnt <= 0) { | ||
| 187 | + mapping.tokenCoreTailCnt = mapping.tokenCoreBudget; | ||
| 188 | + } | ||
| 189 | + mapping.blockDim = mapping.tokenCoreBudget * baseDimCnt; | ||
| 190 | + return mapping; | ||
| 191 | +} | ||
| 192 | + | ||
| 193 | +inline FnTokenSeqRangePlan BuildFnTokenSeqRangePlan(const int64_t *qslData, int64_t batch, int64_t tokenBlockSize, | ||
| 194 | + int64_t tokenBlockCnt) | ||
| 195 | +{ | ||
| 196 | + FnTokenSeqRangePlan plan; | ||
| 197 | + if (qslData == nullptr || batch <= 0 || tokenBlockSize <= 0 || tokenBlockCnt <= 0 || | ||
| 198 | + tokenBlockCnt > MAX_FN_TOKEN_SEQ_RANGE_COUNT) { | ||
| 199 | + return plan; | ||
| 200 | + } | ||
| 201 | + | ||
| 202 | + plan.enabled = true; | ||
| 203 | + plan.rangeCount = tokenBlockCnt; | ||
| 204 | + int64_t seq = 0; | ||
| 205 | + for (int64_t tokenTileId = 0; tokenTileId < tokenBlockCnt; ++tokenTileId) { | ||
| 206 | + const int64_t tokenStart = tokenTileId * tokenBlockSize; | ||
| 207 | + const int64_t tokenEnd = tokenStart + tokenBlockSize; | ||
| 208 | + | ||
| 209 | + while (seq < batch && qslData[seq + 1] <= tokenStart) { | ||
| 210 | + ++seq; | ||
| 211 | + } | ||
| 212 | + | ||
| 213 | + int64_t endSeq = seq; | ||
| 214 | + while (endSeq < batch && qslData[endSeq] < tokenEnd) { | ||
| 215 | + ++endSeq; | ||
| 216 | + } | ||
| 217 | + | ||
| 218 | + plan.tokenTileStartSeq[tokenTileId] = seq; | ||
| 219 | + plan.tokenTileEndSeq[tokenTileId] = endSeq; | ||
| 220 | + } | ||
| 221 | + return plan; | ||
| 222 | +} | ||
| 223 | + | ||
| 224 | +inline VarlenTokenTileChoice ChooseUnifiedFnTokenBlockPlan(gert::TilingContext *context, | ||
| 225 | + const CausalConv1dTilingData &tiling, | ||
| 226 | + const DimTileChoice &baseDimChoice, | ||
| 227 | + FnExecutionPlan fnExecutionPlan, | ||
| 228 | + uint32_t coreNum) | ||
| 229 | +{ | ||
| 230 | + VarlenTokenTileChoice tokenBlockChoice; | ||
| 231 | + if ((tiling.inputMode != 0 && tiling.inputMode != 1) || tiling.batch <= 0 || tiling.cuSeqlen <= 0 || | ||
| 232 | + baseDimChoice.baseDimCnt <= 0 || coreNum == 0 || fnExecutionPlan == FN_EXECUTION_PLAN_INVALID) { | ||
| 233 | + return tokenBlockChoice; | ||
| 234 | + } | ||
| 235 | + if (tiling.hasNumAcceptedTokens != 0) { | ||
| 236 | + OP_LOGD(context, "Varlen token tiling disabled: speculative decode still uses the existing seq mapping."); | ||
| 237 | + return tokenBlockChoice; | ||
| 238 | + } | ||
| 239 | + | ||
| 240 | + tokenBlockChoice = ChooseFnTokenBlockChoice(tiling.cuSeqlen, baseDimChoice.baseDimCnt, fnExecutionPlan, coreNum); | ||
| 241 | + | ||
| 242 | + OP_LOGD(context, | ||
| 243 | + "FnTokenTile(plan=%ld): cuSeqlen[%ld], baseDimCnt[%ld], tokenBlockSize[%ld], " | ||
| 244 | + "tokenBlockCnt[%ld], gridSize[%ld].", | ||
| 245 | + static_cast<int64_t>(fnExecutionPlan), tiling.cuSeqlen, baseDimChoice.baseDimCnt, | ||
| 246 | + tokenBlockChoice.tokenBlockSize, tokenBlockChoice.tokenBlockCnt, tokenBlockChoice.gridSize); | ||
| 247 | + return tokenBlockChoice; | ||
| 248 | +} | ||
| 249 | + | ||
| 250 | +inline VarlenTokenTileChoice ChooseFnTokenBlockChoice(int64_t cuSeqlen, int64_t baseDimCnt, | ||
| 251 | + FnExecutionPlan fnExecutionPlan, uint32_t coreNum) | ||
| 252 | +{ | ||
| 253 | + VarlenTokenTileChoice tokenBlockChoice; | ||
| 254 | + const int64_t tokenCoreBudget = ResolveFnTokenCoreBudget(baseDimCnt, fnExecutionPlan, coreNum); | ||
| 255 | + if (cuSeqlen <= 0 || tokenCoreBudget <= 0) { | ||
| 256 | + return tokenBlockChoice; | ||
| 257 | + } | ||
| 258 | + | ||
| 259 | + tokenBlockChoice.enabled = true; | ||
| 260 | + const int64_t idealBlockSize = CeilDivInt64(cuSeqlen, tokenCoreBudget); | ||
| 261 | + tokenBlockChoice.tokenBlockSize = (idealBlockSize > 0) ? idealBlockSize : 1; | ||
| 262 | + tokenBlockChoice.tokenBlockCnt = CeilDivInt64(cuSeqlen, tokenBlockChoice.tokenBlockSize); | ||
| 263 | + tokenBlockChoice.gridSize = tokenBlockChoice.tokenBlockCnt * baseDimCnt; | ||
| 264 | + return tokenBlockChoice; | ||
| 265 | +} | ||
| 266 | + | ||
| 267 | +inline FnHostPlan ChooseFnHostPlan(gert::TilingContext *context, const CausalConv1dTilingData &tiling, uint64_t ubSize, | ||
| 268 | + uint32_t coreNum) | ||
| 269 | +{ | ||
| 270 | + FnHostPlan plan; | ||
| 271 | + if ((tiling.inputMode != 0 && tiling.inputMode != 1) || tiling.batch <= 0 || tiling.cuSeqlen <= 0 || | ||
| 272 | + tiling.dim <= 0 || coreNum == 0) { | ||
| 273 | + return plan; | ||
| 274 | + } | ||
| 275 | + | ||
| 276 | + if (tiling.dim <= MAX_DIM_TILE_SIZE) { | ||
| 277 | + plan.caseKind = FN_TILING_CASE_TOKEN_FIRST; | ||
| 278 | + plan.executionPlan = FN_EXECUTION_PLAN_CUTBS; | ||
| 279 | + plan.baseDimChoice = ChooseFnTokenFirstBaseDimChoice(tiling.dim); | ||
| 280 | + } else { | ||
| 281 | + plan.caseKind = FN_TILING_CASE_TOKEN_DIM_CO_SPLIT; | ||
| 282 | + plan.executionPlan = FN_EXECUTION_PLAN_CUTBSD; | ||
| 283 | + plan.baseDimChoice = ChooseFnTokenDimCoSplitBaseDimChoice(context, tiling.dim, ubSize, coreNum); | ||
| 284 | + } | ||
| 285 | + | ||
| 286 | + if (plan.baseDimChoice.baseDim <= 0 || plan.baseDimChoice.baseDimCnt <= 0) { | ||
| 287 | + return {}; | ||
| 288 | + } | ||
| 289 | + | ||
| 290 | + plan.baseDimChoice.gridSize = tiling.batch * plan.baseDimChoice.baseDimCnt; | ||
| 291 | + plan.tokenBlockChoice = | ||
| 292 | + ChooseUnifiedFnTokenBlockPlan(context, tiling, plan.baseDimChoice, plan.executionPlan, coreNum); | ||
| 293 | + if (!plan.tokenBlockChoice.enabled || plan.tokenBlockChoice.tokenBlockSize <= 0 || | ||
| 294 | + plan.tokenBlockChoice.tokenBlockCnt <= 0 || plan.tokenBlockChoice.gridSize <= 0) { | ||
| 295 | + return {}; | ||
| 296 | + } | ||
| 297 | + | ||
| 298 | + plan.tokenCoreMapping = BuildFnTokenCoreMappingChoice(plan.tokenBlockChoice.tokenBlockCnt, | ||
| 299 | + plan.baseDimChoice.baseDimCnt, plan.executionPlan, coreNum); | ||
| 300 | + if (plan.tokenCoreMapping.tokenCoreBudget <= 0 || plan.tokenCoreMapping.blockDim <= 0) { | ||
| 301 | + return {}; | ||
| 302 | + } | ||
| 303 | + if (plan.tokenCoreMapping.blockDim > static_cast<int64_t>(coreNum)) { | ||
| 304 | + plan.tokenCoreMapping.blockDim = static_cast<int64_t>(coreNum); | ||
| 305 | + } | ||
| 306 | + return plan; | ||
| 307 | +} | ||
| 308 | + | ||
| 309 | +} // namespace optiling::causal_conv1d_host | ||
| 310 | + | ||
| 311 | +#endif // CAUSAL_CONV1D_TILING_PLANNER_H | ||
| @@ -0,0 +1,176 @@ | |||
| 1 | +/** | ||
| 2 | + * This program is free software, you can redistribute it and/or modify it. | ||
| 3 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. | ||
| 4 | + * This file is a part of the CANN Open Software. | ||
| 5 | + * Licensed under CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 6 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 7 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING | ||
| 8 | + * BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 9 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 10 | + */ | ||
| 11 | + | ||
| 12 | +#ifndef CAUSAL_CONV1D_TILING_UTILS_H | ||
| 13 | +#define CAUSAL_CONV1D_TILING_UTILS_H | ||
| 14 | + | ||
| 15 | +#include "../op_kernel/causal_conv1d_tiling_key.h" | ||
| 16 | +#include "register/op_impl_registry.h" | ||
| 17 | +#include "tiling/platform/platform_ascendc.h" | ||
| 18 | +#include "platform/platform_infos_def.h" | ||
| 19 | + | ||
| 20 | +namespace optiling::causal_conv1d_host { | ||
| 21 | + | ||
| 22 | +constexpr uint32_t X_INDEX = 0; | ||
| 23 | +constexpr uint32_t WEIGHT_INDEX = 1; | ||
| 24 | +constexpr uint32_t BIAS_INDEX = 2; | ||
| 25 | +constexpr uint32_t CONV_STATES_INDEX = 3; | ||
| 26 | +constexpr uint32_t QUERY_START_LOC_INDEX = 4; | ||
| 27 | +constexpr uint32_t CACHE_INDICES_INDEX = 5; | ||
| 28 | +constexpr uint32_t INITIAL_STATE_MODE_INDEX = 6; | ||
| 29 | +constexpr uint32_t NUM_ACCEPTED_TOKENS_INDEX = 7; | ||
| 30 | + | ||
| 31 | +constexpr int32_t ATTR_ACTIVATION_MODE_INDEX = 0; | ||
| 32 | +constexpr int32_t ATTR_PAD_SLOT_ID_INDEX = 1; | ||
| 33 | +constexpr int32_t ATTR_RUN_MODE_INDEX = 2; | ||
| 34 | +constexpr int32_t ATTR_PACKED_Q_DIM_INDEX = 3; | ||
| 35 | +constexpr int32_t ATTR_PACKED_K_DIM_INDEX = 4; | ||
| 36 | +constexpr int32_t ATTR_PACKED_V_DIM_INDEX = 5; | ||
| 37 | +constexpr int32_t ATTR_PACKED_HEAD_DIM_INDEX = 6; | ||
| 38 | + | ||
| 39 | +constexpr int64_t ASCENDC_RESERVED_WORKSPACE_SIZE = 16 * 1024 * 1024; | ||
| 40 | + | ||
| 41 | +struct CausalConv1dCompileInfo { | ||
| 42 | + uint64_t ubSize = 0; | ||
| 43 | + uint32_t coreNum = 0; | ||
| 44 | +}; | ||
| 45 | + | ||
| 46 | +struct CausalConv1dAttrInfo { | ||
| 47 | + int64_t activationMode = 0; | ||
| 48 | + int64_t padSlotId = -1; | ||
| 49 | + int64_t runMode = 0; | ||
| 50 | + int64_t packedQDim = 0; | ||
| 51 | + int64_t packedKDim = 0; | ||
| 52 | + int64_t packedVDim = 0; | ||
| 53 | + int64_t packedHeadDim = 0; | ||
| 54 | +}; | ||
| 55 | + | ||
| 56 | +struct DimTileChoice { | ||
| 57 | + int64_t baseDim = 0; | ||
| 58 | + int64_t baseDimCnt = 0; | ||
| 59 | + int64_t gridSize = 0; | ||
| 60 | +}; | ||
| 61 | + | ||
| 62 | +struct VarlenTokenTileChoice { | ||
| 63 | + bool enabled = false; | ||
| 64 | + int64_t tokenBlockSize = 0; | ||
| 65 | + int64_t tokenBlockCnt = 0; | ||
| 66 | + int64_t gridSize = 0; | ||
| 67 | +}; | ||
| 68 | + | ||
| 69 | +enum FnTilingCaseKind : int64_t { | ||
| 70 | + FN_TILING_CASE_INVALID = 0, | ||
| 71 | + FN_TILING_CASE_TOKEN_FIRST = 1, | ||
| 72 | + FN_TILING_CASE_TOKEN_DIM_CO_SPLIT = 2, | ||
| 73 | +}; | ||
| 74 | + | ||
| 75 | +struct TokenCoreMappingChoice { | ||
| 76 | + int64_t tokenCoreBudget = 0; | ||
| 77 | + int64_t tokenBlocksPerCore = 0; | ||
| 78 | + int64_t tokenCoreTailCnt = 0; | ||
| 79 | + int64_t blockDim = 0; | ||
| 80 | +}; | ||
| 81 | + | ||
| 82 | +constexpr int64_t MAX_FN_TOKEN_SEQ_RANGE_COUNT = 128; | ||
| 83 | + | ||
| 84 | +struct FnTokenSeqRangePlan { | ||
| 85 | + bool enabled = false; | ||
| 86 | + int64_t rangeCount = 0; | ||
| 87 | + int64_t tokenTileStartSeq[MAX_FN_TOKEN_SEQ_RANGE_COUNT] = {}; | ||
| 88 | + int64_t tokenTileEndSeq[MAX_FN_TOKEN_SEQ_RANGE_COUNT] = {}; | ||
| 89 | +}; | ||
| 90 | + | ||
| 91 | +struct FnHostPlan { | ||
| 92 | + FnTilingCaseKind caseKind = FN_TILING_CASE_INVALID; | ||
| 93 | + FnExecutionPlan executionPlan = FN_EXECUTION_PLAN_INVALID; | ||
| 94 | + DimTileChoice baseDimChoice; | ||
| 95 | + VarlenTokenTileChoice tokenBlockChoice; | ||
| 96 | + TokenCoreMappingChoice tokenCoreMapping; | ||
| 97 | + FnTokenSeqRangePlan tokenSeqRangePlan; | ||
| 98 | +}; | ||
| 99 | + | ||
| 100 | +constexpr int64_t DIM_ALIGN_BYTES = 32; | ||
| 101 | +constexpr int64_t BF16_FP16_ELEM_BYTES = 2; | ||
| 102 | +constexpr int64_t DIM_ALIGN_ELEMS = DIM_ALIGN_BYTES / BF16_FP16_ELEM_BYTES; | ||
| 103 | +constexpr int64_t MAX_DIM_TILE_SIZE = 4096; | ||
| 104 | +constexpr int64_t FN_UB_RESERVED_BYTES = 512; | ||
| 105 | +constexpr int64_t RING_SLOT_CNT = 5; | ||
| 106 | +constexpr int64_t FN_OUT_SLOT_CNT = 2; | ||
| 107 | +constexpr int64_t FN_CALC_FP32_SLOT_CNT = 8; // weight(4) + recurrence/output/state scratch(4) | ||
| 108 | + | ||
| 109 | +inline constexpr uint32_t NormalizeFnPlanTilingKey(uint32_t runModeKey, FnExecutionPlan fnExecutionPlan) | ||
| 110 | +{ | ||
| 111 | + if (runModeKey != CAUSAL_CONV1D_TPL_RUN_MODE_FN) { | ||
| 112 | + return CAUSAL_CONV1D_TPL_FN_PLAN_INVALID; | ||
| 113 | + } | ||
| 114 | + switch (fnExecutionPlan) { | ||
| 115 | + case FN_EXECUTION_PLAN_CUTBS: | ||
| 116 | + return CAUSAL_CONV1D_TPL_FN_PLAN_CUTBS; | ||
| 117 | + case FN_EXECUTION_PLAN_CUTBSD: | ||
| 118 | + return CAUSAL_CONV1D_TPL_FN_PLAN_CUTBSD; | ||
| 119 | + default: | ||
| 120 | + return CAUSAL_CONV1D_TPL_FN_PLAN_INVALID; | ||
| 121 | + } | ||
| 122 | +} | ||
| 123 | + | ||
| 124 | +inline constexpr uint32_t NormalizeWidthTilingKey(uint32_t runModeKey, int32_t width) | ||
| 125 | +{ | ||
| 126 | + if (runModeKey != CAUSAL_CONV1D_TPL_RUN_MODE_FN) { | ||
| 127 | + return CAUSAL_CONV1D_TPL_WIDTH_RUNTIME; | ||
| 128 | + } | ||
| 129 | + switch (width) { | ||
| 130 | + case 2: | ||
| 131 | + return CAUSAL_CONV1D_TPL_WIDTH_2; | ||
| 132 | + case 3: | ||
| 133 | + return CAUSAL_CONV1D_TPL_WIDTH_3; | ||
| 134 | + case 4: | ||
| 135 | + return CAUSAL_CONV1D_TPL_WIDTH_4; | ||
| 136 | + default: | ||
| 137 | + return CAUSAL_CONV1D_TPL_WIDTH_RUNTIME; | ||
| 138 | + } | ||
| 139 | +} | ||
| 140 | + | ||
| 141 | +inline int64_t CeilDivInt64(int64_t x, int64_t y) | ||
| 142 | +{ | ||
| 143 | + return (x + y - 1) / y; | ||
| 144 | +} | ||
| 145 | + | ||
| 146 | +inline int64_t AlignDownInt64(int64_t value, int64_t align) | ||
| 147 | +{ | ||
| 148 | + if (align <= 0 || value <= 0) { | ||
| 149 | + return 0; | ||
| 150 | + } | ||
| 151 | + return (value / align) * align; | ||
| 152 | +} | ||
| 153 | + | ||
| 154 | +inline int64_t AlignUpInt64(int64_t value, int64_t align) | ||
| 155 | +{ | ||
| 156 | + if (align <= 0 || value <= 0) { | ||
| 157 | + return 0; | ||
| 158 | + } | ||
| 159 | + return CeilDivInt64(value, align) * align; | ||
| 160 | +} | ||
| 161 | + | ||
| 162 | +inline const char *GetFnTilingCaseName(FnTilingCaseKind caseKind) | ||
| 163 | +{ | ||
| 164 | + switch (caseKind) { | ||
| 165 | + case FN_TILING_CASE_TOKEN_FIRST: | ||
| 166 | + return "token_first"; | ||
| 167 | + case FN_TILING_CASE_TOKEN_DIM_CO_SPLIT: | ||
| 168 | + return "token_dim_co_split"; | ||
| 169 | + default: | ||
| 170 | + return "invalid"; | ||
| 171 | + } | ||
| 172 | +} | ||
| 173 | + | ||
| 174 | +} // namespace optiling::causal_conv1d_host | ||
| 175 | + | ||
| 176 | +#endif // CAUSAL_CONV1D_TILING_UTILS_H | ||