Skip to content

Commit 004637b

Browse files
committed
Change cmake to support compile ascendc kernels
1 parent a4ae386 commit 004637b

File tree

4 files changed

+36
-62
lines changed

4 files changed

+36
-62
lines changed

CMakeLists.txt

Lines changed: 5 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -824,69 +824,14 @@ if (LLAMA_CANN)
824824
825825
# Set headers
826826
set(CANN_INCLUDE_DIRS "${CANN_INSTALL_DIR}/include" "${CANN_INSTALL_DIR}/include/aclnn")
827-
# Find libs
828-
set(CANN_LIBRARIES "")
829-
# TODO: optimize find libs.
830-
# * libascendcl.so
831-
if (LLAMA_CANN)
832-
set(lib_dir "${CANN_INSTALL_DIR}/acllib/lib64")
833-
find_library(found_lib_ascendcl NAMES ascendcl PATHS ${lib_dir} NO_DEFAULT_PATH)
834-
if (found_lib_ascendcl)
835-
set(lib_ascendcl ${found_lib_ascendcl})
836-
list(APPEND CANN_LIBRARIES ${lib_ascendcl})
837-
message(STATUS "CANN: libascendcl.so is found at ${lib_dir}")
838-
else()
839-
set(LLAMA_CANN OFF)
840-
message(WARNING "CANN: Missing libascendcl.so. Turning off LLAMA_CANN")
841-
endif()
842-
endif()
843-
844-
# * libnnopbase.so
845-
if (LLAMA_CANN)
846-
set(lib_dir "${CANN_INSTALL_DIR}/acllib/lib64")
847-
find_library(found_lib_nnopbase NAMES nnopbase PATHS ${lib_dir} NO_DEFAULT_PATH)
848-
if (found_lib_nnopbase)
849-
set(lib_nnopbase ${found_lib_nnopbase})
850-
list(APPEND CANN_LIBRARIES ${lib_nnopbase})
851-
message(STATUS "CANN: libnnopbase.so is found at ${lib_dir}")
852-
else()
853-
set(LLAMA_CANN OFF)
854-
message(WARNING "CANN: Missing libnnopbase.so. Turning off LLAMA_CANN")
855-
endif()
856-
endif()
857827
858-
# * libopapi.so
828+
# Set libs
859829
if (LLAMA_CANN)
860-
set(lib_dir "${CANN_INSTALL_DIR}/lib64")
861-
find_library(found_lib_opapi NAMES opapi PATHS ${lib_dir} NO_DEFAULT_PATH)
862-
if (found_lib_opapi)
863-
set(lib_opapi ${found_lib_opapi})
864-
list(APPEND CANN_LIBRARIES ${lib_opapi})
865-
message(STATUS "CANN: libopapi.so is found at ${lib_dir}")
866-
else()
867-
set(LLAMA_CANN OFF)
868-
message(WARNING "CANN: Missing libopapi.so. Turning off LLAMA_CANN")
869-
endif()
870-
endif()
830+
# Build Ascendc kernels.
831+
add_subdirectory(ggml-cann/kernels)
832+
list(APPEND CANN_LIBRARIES ascendcl nnopbase opapi acl_op_compiler ascendc_kernels)
833+
LINK_DIRECTORIES(${LINK_DIRECTORIES} ${CANN_INSTALL_DIR}/lib64)
871834
872-
# * libacl_op_compiler.so
873-
if (LLAMA_CANN)
874-
set(lib_dir "${CANN_INSTALL_DIR}/lib64")
875-
find_library(found_lib_acl_op_compiler NAMES acl_op_compiler PATHS ${lib_dir} NO_DEFAULT_PATH)
876-
if (found_lib_acl_op_compiler)
877-
set(lib_acl_op_compiler ${found_lib_acl_op_compiler})
878-
list(APPEND CANN_LIBRARIES ${lib_acl_op_compiler})
879-
message(STATUS "CANN: libacl_op_compiler.so is found at ${lib_dir}")
880-
else()
881-
set(LLAMA_CANN OFF)
882-
message(WARNING "CANN: Missing libacl_op_compiler.so. Turning off LLAMA_CANN")
883-
endif()
884-
endif()
885-
886-
# Set headers and libs
887-
if (LLAMA_CANN)
888-
message(STATUS "CANN: CANN_INCLUDE_DIRS = ${CANN_INCLUDE_DIRS}")
889-
message(STATUS "CANN: CANN_LIBRARIES = ${CANN_LIBRARIES}")
890835
set(GGML_HEADERS_CANN ggml-cann.h)
891836
file(GLOB GGML_SOURCES_CUDA "ggml-cann/*.cpp")
892837
list(APPEND GGML_SOURCES_CANN "ggml-cann.cpp")

ggml-cann/aclnn_ops.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include <cstring>
2424
#include <vector>
2525

26+
#include "kernels/ascendc_kernels.h"
27+
2628
void ggml_cann_repeat(ggml_backend_cann_context& ctx, ggml_tensor* dst) {
2729
ggml_tensor* src = dst->src[0];
2830
GGML_ASSERT(ggml_can_repeat(src, dst));
@@ -139,8 +141,8 @@ void aclnn_concat(ggml_backend_cann_context& ctx, aclTensor *acl_src0,
139141
aclTensor *acl_src1, aclTensor *acl_dst, int64_t concat_dim,
140142
ggml_tensor* bind_tensor) {
141143

142-
std::vector<aclTensor*> tmp{acl_src0, acl_src1};
143-
aclTensorList* tensorList = aclCreateTensorList(tmp.data(), tmp.size());
144+
aclTensor* tensors[] = {acl_src0, acl_src1};
145+
aclTensorList* tensorList = aclCreateTensorList(tensors, 2);
144146

145147
uint64_t workspaceSize = 0;
146148
aclOpExecutor* executor;
@@ -156,7 +158,11 @@ void aclnn_concat(ggml_backend_cann_context& ctx, aclTensor *acl_src0,
156158
aclrtStream main_stream = ctx.stream();
157159
ACL_CHECK(aclnnCat(workspaceAddr, workspaceSize, executor, main_stream));
158160

161+
//ACL_CHECK(aclDestroyTensor(acl_src0));
162+
//ACL_CHECK(aclDestroyTensor(acl_src1));
159163
ACL_CHECK(aclDestroyTensorList(tensorList));
164+
ACL_CHECK(aclDestroyTensor(acl_dst));
165+
160166
}
161167

162168
void ggml_cann_concat(ggml_backend_cann_context& ctx, ggml_tensor* dst) {

ggml-cann/kernels/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
if(NOT SOC_VERSION)
2+
set(SOC_VERSION "ascend910b3")
3+
endif()
4+
set(ASCEND_CANN_PACKAGE_PATH ${CANN_INSTALL_DIR})
5+
set(RUN_MODE "npu" CACHE STRING "run mode: npu/sim/cpu")
6+
7+
if(EXISTS ${ASCEND_CANN_PACKAGE_PATH}/compiler/tikcpp/ascendc_kernel_cmake)
8+
set(ASCENDC_CMAKE_DIR ${ASCEND_CANN_PACKAGE_PATH}/compiler/tikcpp/ascendc_kernel_cmake)
9+
elseif(EXISTS ${ASCEND_CANN_PACKAGE_PATH}/ascendc_devkit/tikcpp/samples/cmake)
10+
set(ASCENDC_CMAKE_DIR ${ASCEND_CANN_PACKAGE_PATH}/ascendc_devkit/tikcpp/samples/cmake)
11+
else()
12+
message(FATAL_ERROR "ascendc_kernel_cmake does not exist, please check whether the compiler package is installed.")
13+
endif()
14+
15+
include(${ASCENDC_CMAKE_DIR}/ascendc.cmake)
16+
17+
ascendc_library(ascendc_kernels STATIC
18+
threshold_opencv_kernel.cpp
19+
)

ggml-cann/kernels/ascendc_kernels.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#ifndef ASCENDC_KERNELS_H
2+
#define ASCENDC_KERNELS_H
3+
4+
#endif //ASCENDC_KERNELS_H

0 commit comments

Comments
 (0)