Skip to content

Commit 83f5ada

Browse files
committed
Change cmake to support compile ascendc kernels
1 parent 52d05d0 commit 83f5ada

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
@@ -809,69 +809,14 @@ if (LLAMA_CANN)
809809
810810
# Set headers
811811
set(CANN_INCLUDE_DIRS "${CANN_INSTALL_DIR}/include" "${CANN_INSTALL_DIR}/include/aclnn")
812-
# Find libs
813-
set(CANN_LIBRARIES "")
814-
# TODO: optimize find libs.
815-
# * libascendcl.so
816-
if (LLAMA_CANN)
817-
set(lib_dir "${CANN_INSTALL_DIR}/acllib/lib64")
818-
find_library(found_lib_ascendcl NAMES ascendcl PATHS ${lib_dir} NO_DEFAULT_PATH)
819-
if (found_lib_ascendcl)
820-
set(lib_ascendcl ${found_lib_ascendcl})
821-
list(APPEND CANN_LIBRARIES ${lib_ascendcl})
822-
message(STATUS "CANN: libascendcl.so is found at ${lib_dir}")
823-
else()
824-
set(LLAMA_CANN OFF)
825-
message(WARNING "CANN: Missing libascendcl.so. Turning off LLAMA_CANN")
826-
endif()
827-
endif()
828-
829-
# * libnnopbase.so
830-
if (LLAMA_CANN)
831-
set(lib_dir "${CANN_INSTALL_DIR}/acllib/lib64")
832-
find_library(found_lib_nnopbase NAMES nnopbase PATHS ${lib_dir} NO_DEFAULT_PATH)
833-
if (found_lib_nnopbase)
834-
set(lib_nnopbase ${found_lib_nnopbase})
835-
list(APPEND CANN_LIBRARIES ${lib_nnopbase})
836-
message(STATUS "CANN: libnnopbase.so is found at ${lib_dir}")
837-
else()
838-
set(LLAMA_CANN OFF)
839-
message(WARNING "CANN: Missing libnnopbase.so. Turning off LLAMA_CANN")
840-
endif()
841-
endif()
842812
843-
# * libopapi.so
813+
# Set libs
844814
if (LLAMA_CANN)
845-
set(lib_dir "${CANN_INSTALL_DIR}/lib64")
846-
find_library(found_lib_opapi NAMES opapi PATHS ${lib_dir} NO_DEFAULT_PATH)
847-
if (found_lib_opapi)
848-
set(lib_opapi ${found_lib_opapi})
849-
list(APPEND CANN_LIBRARIES ${lib_opapi})
850-
message(STATUS "CANN: libopapi.so is found at ${lib_dir}")
851-
else()
852-
set(LLAMA_CANN OFF)
853-
message(WARNING "CANN: Missing libopapi.so. Turning off LLAMA_CANN")
854-
endif()
855-
endif()
815+
# Build Ascendc kernels.
816+
add_subdirectory(ggml-cann/kernels)
817+
list(APPEND CANN_LIBRARIES ascendcl nnopbase opapi acl_op_compiler ascendc_kernels)
818+
LINK_DIRECTORIES(${LINK_DIRECTORIES} ${CANN_INSTALL_DIR}/lib64)
856819
857-
# * libacl_op_compiler.so
858-
if (LLAMA_CANN)
859-
set(lib_dir "${CANN_INSTALL_DIR}/lib64")
860-
find_library(found_lib_acl_op_compiler NAMES acl_op_compiler PATHS ${lib_dir} NO_DEFAULT_PATH)
861-
if (found_lib_acl_op_compiler)
862-
set(lib_acl_op_compiler ${found_lib_acl_op_compiler})
863-
list(APPEND CANN_LIBRARIES ${lib_acl_op_compiler})
864-
message(STATUS "CANN: libacl_op_compiler.so is found at ${lib_dir}")
865-
else()
866-
set(LLAMA_CANN OFF)
867-
message(WARNING "CANN: Missing libacl_op_compiler.so. Turning off LLAMA_CANN")
868-
endif()
869-
endif()
870-
871-
# Set headers and libs
872-
if (LLAMA_CANN)
873-
message(STATUS "CANN: CANN_INCLUDE_DIRS = ${CANN_INCLUDE_DIRS}")
874-
message(STATUS "CANN: CANN_LIBRARIES = ${CANN_LIBRARIES}")
875820
set(GGML_HEADERS_CANN ggml-cann.h)
876821
file(GLOB GGML_SOURCES_CUDA "ggml-cann/*.cpp")
877822
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)