Skip to content

Commit

Permalink
Change cmake to support compile ascendc kernels
Browse files Browse the repository at this point in the history
  • Loading branch information
hipudding committed Apr 18, 2024
1 parent 52d05d0 commit 83f5ada
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 62 deletions.
65 changes: 5 additions & 60 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -809,69 +809,14 @@ if (LLAMA_CANN)

# Set headers
set(CANN_INCLUDE_DIRS "${CANN_INSTALL_DIR}/include" "${CANN_INSTALL_DIR}/include/aclnn")
# Find libs
set(CANN_LIBRARIES "")
# TODO: optimize find libs.
# * libascendcl.so
if (LLAMA_CANN)
set(lib_dir "${CANN_INSTALL_DIR}/acllib/lib64")
find_library(found_lib_ascendcl NAMES ascendcl PATHS ${lib_dir} NO_DEFAULT_PATH)
if (found_lib_ascendcl)
set(lib_ascendcl ${found_lib_ascendcl})
list(APPEND CANN_LIBRARIES ${lib_ascendcl})
message(STATUS "CANN: libascendcl.so is found at ${lib_dir}")
else()
set(LLAMA_CANN OFF)
message(WARNING "CANN: Missing libascendcl.so. Turning off LLAMA_CANN")
endif()
endif()

# * libnnopbase.so
if (LLAMA_CANN)
set(lib_dir "${CANN_INSTALL_DIR}/acllib/lib64")
find_library(found_lib_nnopbase NAMES nnopbase PATHS ${lib_dir} NO_DEFAULT_PATH)
if (found_lib_nnopbase)
set(lib_nnopbase ${found_lib_nnopbase})
list(APPEND CANN_LIBRARIES ${lib_nnopbase})
message(STATUS "CANN: libnnopbase.so is found at ${lib_dir}")
else()
set(LLAMA_CANN OFF)
message(WARNING "CANN: Missing libnnopbase.so. Turning off LLAMA_CANN")
endif()
endif()

# * libopapi.so
# Set libs
if (LLAMA_CANN)
set(lib_dir "${CANN_INSTALL_DIR}/lib64")
find_library(found_lib_opapi NAMES opapi PATHS ${lib_dir} NO_DEFAULT_PATH)
if (found_lib_opapi)
set(lib_opapi ${found_lib_opapi})
list(APPEND CANN_LIBRARIES ${lib_opapi})
message(STATUS "CANN: libopapi.so is found at ${lib_dir}")
else()
set(LLAMA_CANN OFF)
message(WARNING "CANN: Missing libopapi.so. Turning off LLAMA_CANN")
endif()
endif()
# Build Ascendc kernels.
add_subdirectory(ggml-cann/kernels)
list(APPEND CANN_LIBRARIES ascendcl nnopbase opapi acl_op_compiler ascendc_kernels)
LINK_DIRECTORIES(${LINK_DIRECTORIES} ${CANN_INSTALL_DIR}/lib64)

# * libacl_op_compiler.so
if (LLAMA_CANN)
set(lib_dir "${CANN_INSTALL_DIR}/lib64")
find_library(found_lib_acl_op_compiler NAMES acl_op_compiler PATHS ${lib_dir} NO_DEFAULT_PATH)
if (found_lib_acl_op_compiler)
set(lib_acl_op_compiler ${found_lib_acl_op_compiler})
list(APPEND CANN_LIBRARIES ${lib_acl_op_compiler})
message(STATUS "CANN: libacl_op_compiler.so is found at ${lib_dir}")
else()
set(LLAMA_CANN OFF)
message(WARNING "CANN: Missing libacl_op_compiler.so. Turning off LLAMA_CANN")
endif()
endif()

# Set headers and libs
if (LLAMA_CANN)
message(STATUS "CANN: CANN_INCLUDE_DIRS = ${CANN_INCLUDE_DIRS}")
message(STATUS "CANN: CANN_LIBRARIES = ${CANN_LIBRARIES}")
set(GGML_HEADERS_CANN ggml-cann.h)
file(GLOB GGML_SOURCES_CUDA "ggml-cann/*.cpp")
list(APPEND GGML_SOURCES_CANN "ggml-cann.cpp")
Expand Down
10 changes: 8 additions & 2 deletions ggml-cann/aclnn_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <cstring>
#include <vector>

#include "kernels/ascendc_kernels.h"

void ggml_cann_repeat(ggml_backend_cann_context& ctx, ggml_tensor* dst) {
ggml_tensor* src = dst->src[0];
GGML_ASSERT(ggml_can_repeat(src, dst));
Expand Down Expand Up @@ -139,8 +141,8 @@ void aclnn_concat(ggml_backend_cann_context& ctx, aclTensor *acl_src0,
aclTensor *acl_src1, aclTensor *acl_dst, int64_t concat_dim,
ggml_tensor* bind_tensor) {

std::vector<aclTensor*> tmp{acl_src0, acl_src1};
aclTensorList* tensorList = aclCreateTensorList(tmp.data(), tmp.size());
aclTensor* tensors[] = {acl_src0, acl_src1};
aclTensorList* tensorList = aclCreateTensorList(tensors, 2);

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

//ACL_CHECK(aclDestroyTensor(acl_src0));
//ACL_CHECK(aclDestroyTensor(acl_src1));
ACL_CHECK(aclDestroyTensorList(tensorList));
ACL_CHECK(aclDestroyTensor(acl_dst));

}

void ggml_cann_concat(ggml_backend_cann_context& ctx, ggml_tensor* dst) {
Expand Down
19 changes: 19 additions & 0 deletions ggml-cann/kernels/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
if(NOT SOC_VERSION)
set(SOC_VERSION "ascend910b3")
endif()
set(ASCEND_CANN_PACKAGE_PATH ${CANN_INSTALL_DIR})
set(RUN_MODE "npu" CACHE STRING "run mode: npu/sim/cpu")

if(EXISTS ${ASCEND_CANN_PACKAGE_PATH}/compiler/tikcpp/ascendc_kernel_cmake)
set(ASCENDC_CMAKE_DIR ${ASCEND_CANN_PACKAGE_PATH}/compiler/tikcpp/ascendc_kernel_cmake)
elseif(EXISTS ${ASCEND_CANN_PACKAGE_PATH}/ascendc_devkit/tikcpp/samples/cmake)
set(ASCENDC_CMAKE_DIR ${ASCEND_CANN_PACKAGE_PATH}/ascendc_devkit/tikcpp/samples/cmake)
else()
message(FATAL_ERROR "ascendc_kernel_cmake does not exist, please check whether the compiler package is installed.")
endif()

include(${ASCENDC_CMAKE_DIR}/ascendc.cmake)

ascendc_library(ascendc_kernels STATIC
threshold_opencv_kernel.cpp
)
4 changes: 4 additions & 0 deletions ggml-cann/kernels/ascendc_kernels.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#ifndef ASCENDC_KERNELS_H
#define ASCENDC_KERNELS_H

#endif //ASCENDC_KERNELS_H

0 comments on commit 83f5ada

Please sign in to comment.