已开启
支持纯 Torch DSL 算子构建 custom 和 builtin 包 #7
zyh_gitcode创建于 4 天前
支持纯 Torch DSL 算子构建 custom 和 builtin 包 #7
已开启
共 6 个文件变更+118-11
| @@ -785,7 +785,7 @@ endif() | |||
| 785 | 785 | ||
| 786 | target_link_libraries( | 786 | target_link_libraries( |
| 787 | cust_proto | 787 | cust_proto |
| 788 | - PUBLIC ${OPHOST_NAME}_infer_obj | 788 | + PUBLIC $<TARGET_NAME_IF_EXISTS:${OPHOST_NAME}_infer_obj> |
| 789 | PUBLIC $<$<TARGET_EXISTS:${OPGRAPH_NAME}_gentask_obj>:${OPGRAPH_NAME}_gentask_obj> | 789 | PUBLIC $<$<TARGET_EXISTS:${OPGRAPH_NAME}_gentask_obj>:${OPGRAPH_NAME}_gentask_obj> |
| 790 | ) | 790 | ) |
| 791 | 791 | ||
| @@ -180,13 +180,22 @@ function(gen_opgraph_symbol) | |||
| 180 | endfunction() | 180 | endfunction() |
| 181 | 181 | ||
| 182 | function(gen_opapi_symbol) | 182 | function(gen_opapi_symbol) |
| 183 | - # opapi shared | 183 | + set(opapi_sources) |
| 184 | - add_library(${OPAPI_NAME} SHARED | 184 | + foreach(obj_target IN ITEMS |
| 185 | - $<$<TARGET_EXISTS:${OPHOST_NAME}_cannbotdsl_opapi_obj>:$<TARGET_OBJECTS:${OPHOST_NAME}_cannbotdsl_opapi_obj>> | 185 | + ${OPHOST_NAME}_cannbotdsl_opapi_obj |
| 186 | - $<$<TARGET_EXISTS:${OPHOST_NAME}_opapi_obj>:$<TARGET_OBJECTS:${OPHOST_NAME}_opapi_obj>> | 186 | + ${OPHOST_NAME}_opapi_obj |
| 187 | - $<$<TARGET_EXISTS:opbuild_gen_aclnn_all>:$<TARGET_OBJECTS:opbuild_gen_aclnn_all>> | 187 | + opbuild_gen_aclnn_all |
| 188 | - $<$<TARGET_EXISTS:opbase_util_objs>:$<TARGET_OBJECTS:opbase_util_objs>> | 188 | + opbase_util_objs |
| 189 | ) | 189 | ) |
| 190 | + if(TARGET ${obj_target}) | ||
| 191 | + list(APPEND opapi_sources $<TARGET_OBJECTS:${obj_target}>) | ||
| 192 | + endif() | ||
| 193 | + endforeach() | ||
| 194 | + # Native-only operators can have Host and Device artifacts without an opapi library. | ||
| 195 | + if(NOT opapi_sources) | ||
| 196 | + return() | ||
| 197 | + endif() | ||
| 198 | + add_library(${OPAPI_NAME} SHARED ${opapi_sources}) | ||
| 190 | 199 | ||
| 191 | target_link_libraries(${OPAPI_NAME} | 200 | target_link_libraries(${OPAPI_NAME} |
| 192 | PUBLIC | 201 | PUBLIC |
| @@ -207,9 +216,11 @@ function(gen_opapi_symbol) | |||
| 207 | endfunction() | 216 | endfunction() |
| 208 | 217 | ||
| 209 | function(gen_built_in_opapi_symbol) | 218 | function(gen_built_in_opapi_symbol) |
| 210 | - install(TARGETS ${OPAPI_NAME} | 219 | + if(TARGET ${OPAPI_NAME}) |
| 211 | - LIBRARY DESTINATION ${ACLNN_LIB_INSTALL_DIR} | 220 | + install(TARGETS ${OPAPI_NAME} |
| 212 | - ) | 221 | + LIBRARY DESTINATION ${ACLNN_LIB_INSTALL_DIR} |
| 222 | + ) | ||
| 223 | + endif() | ||
| 213 | endfunction() | 224 | endfunction() |
| 214 | 225 | ||
| 215 | function(gen_cust_opapi_symbol) | 226 | function(gen_cust_opapi_symbol) |
| @@ -537,7 +548,9 @@ endfunction() | |||
| 537 | function(gen_cust_symbol) | 548 | function(gen_cust_symbol) |
| 538 | gen_opapi_symbol() | 549 | gen_opapi_symbol() |
| 539 | 550 | ||
| 540 | - gen_cust_opapi_symbol() | 551 | + if(TARGET ${OPAPI_NAME}) |
| 552 | + gen_cust_opapi_symbol() | ||
| 553 | + endif() | ||
| 541 | 554 | ||
| 542 | gen_cust_optiling_symbol() | 555 | gen_cust_optiling_symbol() |
| 543 | 556 | ||
| @@ -0,0 +1,44 @@ | |||
| 1 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 2 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 3 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 4 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 5 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 6 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 7 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 8 | + | ||
| 9 | +"""Minimal Add example for contiguous FP32 tensors with shape [128].""" | ||
| 10 | + | ||
| 11 | +import cannbotdsl as cbd | ||
| 12 | +from cannbotdsl import Tensor, aot, dim3, dtypes, host, kernel, simt, vf | ||
| 13 | + | ||
| 14 | + | ||
| 15 | +@kernel | ||
| 16 | +def _add_kernel(x: Tensor, y: Tensor, out: Tensor): | ||
| 17 | + with vf(mode="simt"): | ||
| 18 | + index = simt.thread_idx()[0] | ||
| 19 | + out[index] = x[index] + y[index] | ||
| 20 | + | ||
| 21 | + | ||
| 22 | +@host | ||
| 23 | +def _run_add(x: Tensor, y: Tensor, out: Tensor): | ||
| 24 | + _add_kernel[dim3(1), dim3(128)](x, y, out) | ||
| 25 | + | ||
| 26 | + | ||
| 27 | +@aot.compile_cache | ||
| 28 | +def compile_add(): | ||
| 29 | + spec = cbd.TensorSpec((128,), dtypes.float32) | ||
| 30 | + return cbd.compile(_run_add, spec, spec, spec) | ||
| 31 | + | ||
| 32 | + | ||
| 33 | +def add(x, y): | ||
| 34 | + import torch | ||
| 35 | + | ||
| 36 | + out = torch.empty_like(x) | ||
| 37 | + provider = compile_add() | ||
| 38 | + provider(x, y, out) | ||
| 39 | + return out | ||
| 40 | + | ||
| 41 | + | ||
| 42 | +@aot.export("add") | ||
| 43 | +def export_add(): | ||
| 44 | + compile_add() | ||
| @@ -0,0 +1,11 @@ | |||
| 1 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 2 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 3 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 4 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 5 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 6 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 7 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 8 | + | ||
| 9 | +from .add import add | ||
| 10 | + | ||
| 11 | +__all__ = ["add"] | ||
| @@ -0,0 +1,26 @@ | |||
| 1 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 2 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 3 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 4 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 5 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 6 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 7 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 8 | + | ||
| 9 | +import torch | ||
| 10 | +from torch.library import impl | ||
| 11 | + | ||
| 12 | +from cann_ops_transformer.op_builder import get_as_library | ||
| 13 | + | ||
| 14 | +get_as_library().define("add(Tensor x, Tensor y) -> Tensor") | ||
| 15 | + | ||
| 16 | + | ||
| 17 | +@impl(get_as_library(), "add", "Meta") | ||
| 18 | +def _add_meta(x, y): | ||
| 19 | + return torch.empty_like(x) | ||
| 20 | + | ||
| 21 | + | ||
| 22 | +@impl(get_as_library(), "add", "PrivateUse1") | ||
| 23 | +def add(x: torch.Tensor, y: torch.Tensor) -> torch.Tensor: | ||
| 24 | + from .op_dsl.add import add as _implementation | ||
| 25 | + | ||
| 26 | + return _implementation(x, y) | ||