Skip to content

Commit 9cd5cbe

Browse files
gongsu832cjvolzkatungld
authored
embed libzdnn in model.so (llvm#1324)
* - Build libzdnn.a with -fPIC and embed in model.so when -maccel=NNPA specified - Add CompilerConfigMap to store states associated with certain options - Move options in main() to CompilerOptions.cpp - Fix compiler warning in Stickify.cpp Signed-off-by: Gong Su <[email protected]> * - fix NNPA_ENABLED for lit test - install zdnn.h so we no longer need third_party/zdnn-lib - move options back to onnx-mlir.cpp::main (consolidation requires much more effort, deferred) - use DEPENDS in add_onnx_mlir_library for libzdnn dependency Signed-off-by: Gong Su <[email protected]> * Remove zdnn-lib from .gitmodules Signed-off-by: Gong Su <[email protected]> * Make libzdnn ALL target so it gets built before other NNPA components Signed-off-by: Gong Su <[email protected]> * Fix libzdnn dependency for NNPA components Signed-off-by: Gong Su <[email protected]> * Build NNPA in dev image as well Signed-off-by: Gong Su <[email protected]> * Comment out BYPRODUCTS in target libzdnn since generator support requires cmake 3.20+ which is not yet available on official Ubuntu Focal Signed-off-by: Gong Su <[email protected]> * - Force setting cached MLIR_DIR to honor command line argument - unset cached LLVM_DIR so it changes along with MLIR_DIR - surround third_party with set(CMAKE_MESSAGE_LOG_LEVEL NOTICE) and set(CMAKE_MESSAGE_LOG_LEVEL STATUS) to mask out their cmake output so we can see more clearly output by onnx-mlir only. third_party cmake output can still be turned on by the --log-level command line option Signed-off-by: Gong Su <[email protected]> * revert set(CMAKE_MESSAGE_LOG_LEVEL NOTICE) and set(CMAKE_MESSAGE_LOG_LEVEL STATUS) around third_party to make another PR Signed-off-by: Gong Su <[email protected]> * revert force setting cached MLIR_DIR and unsetting cached LLVM_DIR to make another PR Signed-off-by: Gong Su <[email protected]> Co-authored-by: Charles Volzka <[email protected]> Co-authored-by: Tung D. Le <[email protected]>
1 parent 36fd275 commit 9cd5cbe

30 files changed

+169
-39
lines changed

.gitmodules

-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,3 @@
1010
[submodule "third_party/benchmark"]
1111
path = third_party/benchmark
1212
url = https://github.com/google/benchmark.git
13-
[submodule "third_party/zdnn-lib"]
14-
path = third_party/zdnn-lib
15-
url = https://github.com/IBM/zDNN.git

CMakeLists.txt

+2-10
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,21 @@ if (MSVC)
3636
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -wd4125")
3737
endif()
3838

39-
# Define preprocessor for accelerators.
40-
set(NNPA_ENABLED 0)
41-
if (ACCELERATORS_TO_BUILD)
42-
foreach(t ${ACCELERATORS_TO_BUILD})
43-
if (${t} STREQUAL "NNPA")
44-
set(NNPA_ENABLED 1)
45-
endif()
46-
endforeach(t)
47-
endif(ACCELERATORS_TO_BUILD)
48-
4939
# Enable warnings as errors
5040
# Leverage the imported LLVM_ENABLE_WERROR for compiler logic
5141
set(LLVM_ENABLE_WERROR ${ONNX_MLIR_ENABLE_WERROR})
5242

5343
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$<CONFIG>/lib)
5444
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$<CONFIG>/lib)
5545
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$<CONFIG>/bin)
46+
set(CMAKE_INCLUDE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$<CONFIG>/include)
5647

5748
set(ONNX_MLIR_SRC_ROOT ${CMAKE_CURRENT_SOURCE_DIR})
5849
set(ONNX_MLIR_BIN_ROOT ${CMAKE_CURRENT_BINARY_DIR})
5950

6051
set(ONNX_MLIR_LIBRARY_PATH ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
6152
set(ONNX_MLIR_RUNTIME_PATH ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
53+
set(ONNX_MLIR_INCLUDE_PATH ${CMAKE_INCLUDE_OUTPUT_DIRECTORY})
6254

6355
set(ONNX_MLIR_VENDOR ${PACKAGE_VENDOR} CACHE STRING
6456
"Vendor-specific text for showing with version information.")

MLIR.cmake

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ include_directories(${MLIR_INCLUDE_DIRS})
2727
add_definitions(${LLVM_DEFINITIONS})
2828

2929
set(BUILD_SHARED_LIBS ${LLVM_ENABLE_SHARED_LIBS} CACHE BOOL "" FORCE)
30-
message(STATUS "BUILD_SHARED_LIBS : " ${BUILD_SHARED_LIBS})
30+
message(STATUS "BUILD_SHARED_LIBS : " ${BUILD_SHARED_LIBS})
3131

3232
# onnx uses exceptions, so we need to make sure that LLVM_REQUIRES_EH is set to ON, so that
3333
# the functions from HandleLLVMOptions and AddLLVM don't disable exceptions.
3434
set(LLVM_REQUIRES_EH ON)
35-
message(STATUS "LLVM_REQUIRES_EH : " ${LLVM_REQUIRES_EH})
35+
message(STATUS "LLVM_REQUIRES_EH : " ${LLVM_REQUIRES_EH})
3636

3737
# LLVM_HOST_TRIPLE is exported as part of the llvm config, so we should be able to leverage it.
3838
# If, for some reason, it is not set, default to an empty string which is the old default behavior of onnx-mlir.
3939
set(ONNX_MLIR_DEFAULT_TRIPLE "${LLVM_HOST_TRIPLE}" CACHE STRING "Default triple for onnx-mlir.")
40-
message(STATUS "ONNX_MLIR_DEFAULT_TRIPLE: " ${ONNX_MLIR_DEFAULT_TRIPLE})
40+
message(STATUS "ONNX_MLIR_DEFAULT_TRIPLE : " ${ONNX_MLIR_DEFAULT_TRIPLE})
4141

4242
# If CMAKE_INSTALL_PREFIX was not provided explicitly and we are not using an install of
4343
# LLVM and a CMakeCache.txt exists,
@@ -50,7 +50,7 @@ if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT AND NOT LLVM_INSTALL_PREFIX)
5050
set(CMAKE_INSTALL_PREFIX ${prefix} CACHE PATH "" FORCE)
5151
endif()
5252
endif()
53-
message(STATUS "CMAKE_INSTALL_PREFIX : " ${CMAKE_INSTALL_PREFIX})
53+
message(STATUS "CMAKE_INSTALL_PREFIX : " ${CMAKE_INSTALL_PREFIX})
5454

5555
# The tablegen functions below are modeled based on the corresponding functions
5656
# in mlir: https://github.com/llvm/llvm-project/blob/main/mlir/cmake/modules/AddMLIR.cmake

docker/Dockerfile.onnx-mlir-dev

+6-1
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ RUN LLVM_PROJECT_ROOT=${WORK_DIR}/llvm-project \
3030
# Build onnx-mlir and run tests
3131
&& cd ${ONNX_MLIR_ROOT} \
3232
&& rm -rf build && mkdir -p build && cd build \
33+
# Dev image is built with NNPA acclerator (currently on s390x only)
34+
&& BUILD_NNPA=${BUILD_NNPA:-$([ "$(uname -m)" = "s390x" ] && echo NNPA || \
35+
[ "$(uname -m)" = "x86_64" ] && echo || \
36+
[ "$(uname -m)" = "ppc64le" ] && echo || echo)} \
3337
&& MLIR_DIR=${LLVM_PROJECT_ROOT}/build/lib/cmake/mlir \
34-
cmake .. \
38+
cmake -DCMAKE_BUILD_TYPE=Debug \
39+
-DACCELERATORS_TO_BUILD=${BUILD_NNPA} .. \
3540
&& make -j${NPROC} \
3641
&& make -j${NPROC} LIT_OPTS=-v check-onnx-lit \
3742
# Dev image is built without SIMD, placeholder for easy SIMD enablement

include/onnx-mlir/Runtime/OMInstrument.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#include <malloc.h>
3737
#endif // #ifdef __APPLE__
3838

39-
#include "include/onnx-mlir/Compiler/OMCompilerMacros.h"
39+
#include <onnx-mlir/Compiler/OMCompilerMacros.h>
4040

4141
#ifdef __cplusplus
4242
extern "C" {

src/Accelerators/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ if (ACCELERATORS_TO_BUILD)
88
add_subdirectory(${t})
99
list(APPEND ACCEL_LIST "${t}Accel")
1010
endforeach(t)
11+
message(STATUS "Accelerators : ${ACCEL_LIST}")
12+
else()
13+
message(STATUS "Accelerators : NONE")
1114
endif (ACCELERATORS_TO_BUILD)
1215

1316
# Generate the Accelerators.inc file using a cmake script.

src/Accelerators/NNPA/CMakeLists.txt

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
# SPDX-License-Identifier: Apache-2.0
22

3+
set(NNPA_ENABLED 1 CACHE BOOL "flag indicating NNPA enabled")
4+
35
set(NNPA_SRC_ROOT "${CMAKE_CURRENT_SOURCE_DIR}")
46
set(NNPA_BIN_ROOT "${CMAKE_CURRENT_BINARY_DIR}")
57

68
set(NNPA_LIBRARY_PATH ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
79
set(NNPA_RUNTIME_PATH ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
10+
set(NNPA_INCLUDE_PATH ${CMAKE_INCLUDE_OUTPUT_DIRECTORY})
811

912
set(NNPA_ONNX_MLIR_SRC_ROOT ${ONNX_MLIR_SRC_ROOT})
1013
set(NNPA_ONNX_MLIR_BIN_ROOT ${ONNX_MLIR_BIN_ROOT})
@@ -19,6 +22,9 @@ else()
1922
set(ZDNN_LIBRARY_ENABLED 0)
2023
endif()
2124

25+
include(zdnn.cmake)
26+
setup_zdnn(v1.0.0)
27+
2228
add_subdirectory(Dialect)
2329
add_subdirectory(Conversion)
2430
add_subdirectory(Support)
@@ -46,4 +52,3 @@ add_onnx_mlir_library(NNPAAccel
4652
OMZLowOps
4753
OMZLowToLLVM
4854
)
49-

src/Accelerators/NNPA/Conversion/ONNXToZHigh/CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ add_onnx_mlir_library(OMONNXToZHigh
77

88
DEPENDS
99
OMONNXONNXToZHighIncGen
10+
libzdnn
1011

1112
LINK_LIBS PUBLIC
1213
OMONNXToKrnl
1314
OMZHighOps
15+
16+
ACCEL_INCLUDE_DIRS PRIVATE
17+
${NNPA_INCLUDE_PATH}
1418
)
1519

1620
add_onnx_mlir_rewriter(RewriteONNXForZHigh)
@@ -22,8 +26,12 @@ add_onnx_mlir_library(OMRewriteONNXForZHigh
2226

2327
DEPENDS
2428
OMONNXRewriteONNXForZHighIncGen
29+
libzdnn
2530

2631
LINK_LIBS PUBLIC
2732
OMONNXToKrnl
2833
OMZHighOps
34+
35+
ACCEL_INCLUDE_DIRS PRIVATE
36+
${NNPA_INCLUDE_PATH}
2937
)

src/Accelerators/NNPA/Conversion/ZLowToLLVM/CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@ add_onnx_mlir_library(OMZLowToLLVM
22
ZLowToLLVM.cpp
33
ZLowToLLVMCommon.cpp
44

5+
DEPENDS
6+
libzdnn
7+
58
LINK_LIBS PUBLIC
69
MLIRLLVMCommonConversion
710
OMLayoutHelper
811
OMZLowOps
12+
13+
ACCEL_INCLUDE_DIRS PRIVATE
14+
${NNPA_INCLUDE_PATH}
915
)

src/Accelerators/NNPA/Conversion/ZLowToLLVM/ZLowToLLVM.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include "src/Accelerators/NNPA/Conversion/ZLowToLLVM/ZLowToLLVMCommon.hpp"
1919
#include "src/Accelerators/NNPA/Dialect/ZLow/ZLowOps.hpp"
2020
#include "src/Accelerators/NNPA/Support/LayoutHelper.hpp"
21-
#include "third_party/zdnn-lib/zdnn/zdnn.h"
21+
#include "zdnn.h"
2222

2323
using namespace mlir;
2424
using namespace onnx_mlir;

src/Accelerators/NNPA/Conversion/ZLowToLLVM/ZLowToLLVMCommon.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
2020
#include "mlir/Dialect/StandardOps/IR/Ops.h"
2121

22-
#include "third_party/zdnn-lib/zdnn/zdnn.h"
22+
#include "zdnn.h"
2323

2424
using namespace mlir;
2525

src/Accelerators/NNPA/Conversion/ZLowToLLVM/ZLowToLLVMCommon.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include "mlir/Dialect/StandardOps/IR/Ops.h"
1919
#include "mlir/Transforms/DialectConversion.h"
2020

21-
#include "third_party/zdnn-lib/zdnn/zdnn.h"
21+
#include "zdnn.h"
2222

2323
namespace onnx_mlir {
2424
namespace zlow {

src/Accelerators/NNPA/Dialect/ZHigh/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ add_onnx_mlir_library(OMZHighOps
1414
OMZHighIncGen
1515
OMONNXZHighCombineIncGen
1616
OMShapeInferenceOpInterfaceIncGen
17+
libzdnn
1718

1819
LINK_LIBS PUBLIC
1920
OMONNXOps # Use ONNXShapeHelper
2021
OMLayoutHelper
2122
MLIRIR
23+
24+
ACCEL_INCLUDE_DIRS PRIVATE
25+
${NNPA_INCLUDE_PATH}
2226
)

src/Accelerators/NNPA/Dialect/ZHigh/ZHighOps.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#include "src/Accelerators/NNPA/Dialect/ZHigh/ZHighOps.hpp"
3434
#include "src/Accelerators/NNPA/Dialect/ZHigh/ZHighShapeHelper.hpp"
3535
#include "src/Accelerators/NNPA/Support/LayoutHelper.hpp"
36-
#include "third_party/zdnn-lib/zdnn/zdnn.h"
36+
#include "zdnn.h"
3737

3838
using namespace mlir;
3939

src/Accelerators/NNPA/NNPAAccelerator.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ NNPAAccelerator *NNPAAccelerator::getInstance() {
4848
NNPAAccelerator::NNPAAccelerator() : Accelerator(Accelerator::Kind::NNPA) {
4949
LLVM_DEBUG(llvm::dbgs() << "Creating an NNPA accelerator\n");
5050
acceleratorTargets.push_back(this);
51+
addCompilerConfig(CCM_SHARED_LIB_DEPS, {"zdnn"});
5152
};
5253

5354
NNPAAccelerator::~NNPAAccelerator() { delete instance; }

src/Accelerators/NNPA/Support/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
add_onnx_mlir_library(OMLayoutHelper
22
LayoutHelper.cpp
33

4+
DEPENDS
5+
libzdnn
6+
47
LINK_LIBS PUBLIC
58
MLIRIR
69

710
ACCEL_INCLUDE_DIRS PRIVATE
811
${NNPA_SRC_ROOT}
912
${NNPA_BIN_ROOT}
13+
${NNPA_INCLUDE_PATH}
1014
)
1115

1216
add_onnx_mlir_library(OMNNPASupport

src/Accelerators/NNPA/Support/LayoutHelper.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#pragma once
1414

1515
#include "mlir/IR/BuiltinAttributes.h"
16-
#include "third_party/zdnn-lib/zdnn/zdnn.h"
16+
#include "zdnn.h"
1717

1818
namespace onnx_mlir {
1919

src/Accelerators/NNPA/Transform/ZHigh/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ add_onnx_mlir_library(OMZHighConstPropagation
88

99
DEPENDS
1010
OMONNXZHighConstPropagationIncGen
11+
libzdnn
1112

1213
LINK_LIBS PUBLIC
1314
MLIRRewrite
@@ -16,6 +17,9 @@ add_onnx_mlir_library(OMZHighConstPropagation
1617
OMStickify
1718
OMZHighOps
1819
OMONNXOps
20+
21+
ACCEL_INCLUDE_DIRS PRIVATE
22+
${NNPA_INCLUDE_PATH}
1923
)
2024

2125
add_onnx_mlir_rewriter(ZHighLayoutPropagation)

src/Accelerators/NNPA/Transform/ZHigh/Stickify/CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ add_onnx_mlir_library(OMStickify
22
Convert.cpp
33
Stickify.cpp
44

5+
DEPENDS
6+
libzdnn
7+
58
LINK_LIBS PUBLIC
69
LLVMSupport
10+
11+
ACCEL_INCLUDE_DIRS PRIVATE
12+
${NNPA_INCLUDE_PATH}
713
)

src/Accelerators/NNPA/Transform/ZHigh/Stickify/Stickify.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,8 @@ void free_aligned_4k(void *aligned_ptr) {
262262
// Functions from third_party/zdnn-lib/zdnn/utils.c
263263
uint64_t get_num_elements(const zdnn_ztensor *ztensor, elements_mode mode) {
264264
uint64_t num_elements = 1;
265-
uint32_t *dims_ptr;
266-
int i;
265+
uint32_t *dims_ptr = NULL;
266+
int i = 0;
267267

268268
// Setup how to loop over the shape based on the mode.
269269
switch (mode) {

src/Accelerators/NNPA/Transform/ZHigh/Stickify/Stickify.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
#pragma once
1616

17-
#include "third_party/zdnn-lib/zdnn/zdnn.h"
17+
#include "zdnn.h"
1818
#include "llvm/ADT/ArrayRef.h"
1919
#include "llvm/ADT/SmallVector.h"
2020

src/Accelerators/NNPA/zdnn.cmake

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
function(setup_zdnn version)
4+
set(ZDNN_GITHUB_URL https://github.com/IBM/zDNN)
5+
set(ZDNN_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/zDNN)
6+
set(ZDNN_TOPDIR ${ZDNN_PREFIX}/src/zdnn)
7+
set(ZDNN_OBJDIR ${ZDNN_TOPDIR}/zdnn/obj)
8+
set(ZDNN_LIBDIR ${ZDNN_TOPDIR}/zdnn/lib)
9+
10+
ExternalProject_Add(zdnn
11+
GIT_REPOSITORY ${ZDNN_GITHUB_URL}
12+
GIT_TAG ${version}
13+
PREFIX ${ZDNN_PREFIX}
14+
BUILD_IN_SOURCE ON
15+
16+
# Skip autoconf and configure if config.make already exists
17+
CONFIGURE_COMMAND sh -c "[ -f config.make ] || (autoconf && ./configure)"
18+
19+
# We build libzdnn.so so that obj/*.o are compiled with -fPIC
20+
# Then we create libzdnn.a ourselves from these PIC .o since
21+
# we want to embed libzdnn.a into model.so.
22+
#
23+
# Note we use sh to run the command. Otherwise, cmake will
24+
# generate quotes around ${ZDNN_OBJDIR}/*.o due to the *
25+
# in the path, which breaks ar.
26+
#
27+
# Set MAKEFLAGS to remove -j option passed down from the top
28+
# level make which produces a warning about jobserver unavailable.
29+
BUILD_COMMAND sh -c "MAKEFLAGS=--no-print-directory \
30+
make -j$(nproc) -C zdnn lib/libzdnn.so && \
31+
ar -rc ${ZDNN_LIBDIR}/libzdnn.a ${ZDNN_OBJDIR}/*.o"
32+
33+
INSTALL_COMMAND ""
34+
)
35+
36+
add_custom_target(libzdnn
37+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
38+
${ZDNN_LIBDIR}/libzdnn.a ${NNPA_LIBRARY_PATH}/libzdnn.a
39+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
40+
${ZDNN_TOPDIR}/zdnn/zdnn.h ${NNPA_INCLUDE_PATH}/zdnn.h
41+
DEPENDS zdnn
42+
#BYPRODUCTS ${NNPA_LIBRARY_PATH}/libzdnn.a ${NNPA_INCLUDE_PATH}/zdnn.h
43+
)
44+
45+
install(FILES ${NNPA_LIBRARY_PATH}/libzdnn.a DESTINATION lib)
46+
endfunction(setup_zdnn)

0 commit comments

Comments
 (0)