Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DeepLearning] Add openmp support #156

Closed
wants to merge 1 commit into from

Conversation

xlinsist
Copy link
Collaborator

@xlinsist xlinsist commented Oct 31, 2024

Add openmp support for batchmatmul op and bert, tinyllama model.


To cross-compile libomp.so on RV:

STEP ONE: modify the corresponding part in llvm/openmp/runtime/src/CMakeLists.txt like this:

libomp_get_libflags(LIBOMP_CONFIGURED_LIBFLAGS)
# Build libomp library. Add LLVMSupport dependency if building in-tree with libomptarget profiling enabled.
if(OPENMP_STANDALONE_BUILD OR (NOT OPENMP_ENABLE_LIBOMP_PROFILING))
add_library(omp ${LIBOMP_LIBRARY_KIND} ${LIBOMP_SOURCE_FILES})
target_compile_options(omp PRIVATE ${CMAKE_C_FLAGS}) # new added
# Linking command will include libraries in LIBOMP_CONFIGURED_LIBFLAGS
target_link_libraries(omp ${LIBOMP_CONFIGURED_LIBFLAGS} ${LIBOMP_DL_LIBS})

STEP TWO: use the following configuration to cross compile openmp with clang.

export BUILD_LOCAL_LLVM_DIR=/root/intern/buddy-mlir/llvm/build &&
export BUILD_RISCV_GNU_TOOLCHAIN_DIR=/root/intern/buddy-mlir/thirdparty/riscv-gnu-toolchain/install &&
export RISCV_GNU_TOOLCHAIN_SYSROOT_DIR=${BUILD_RISCV_GNU_TOOLCHAIN_DIR}/sysroot/

cmake -G Ninja ../llvm \
    -DLLVM_ENABLE_PROJECTS="clang;openmp" \
    -DLLVM_TARGETS_TO_BUILD="RISCV" \
    -DCMAKE_SYSTEM_NAME=Linux \
    -DCMAKE_VERBOSE_MAKEFILE=ON \
    -DCMAKE_C_COMPILER=${BUILD_LOCAL_LLVM_DIR}/bin/clang \
    -DCMAKE_CXX_COMPILER=${BUILD_LOCAL_LLVM_DIR}/bin/clang++ \
    -DCMAKE_C_FLAGS="--target=riscv64-unknown-linux-gnu --sysroot=${RISCV_GNU_TOOLCHAIN_SYSROOT_DIR} --gcc-toolchain=${BUILD_RISCV_GNU_TOOLCHAIN_DIR}" \
    -DCMAKE_CXX_FLAGS="--target=riscv64-unknown-linux-gnu --sysroot=${RISCV_GNU_TOOLCHAIN_SYSROOT_DIR} --gcc-toolchain=${BUILD_RISCV_GNU_TOOLCHAIN_DIR}" \
    -DLLVM_TABLEGEN=${BUILD_LOCAL_LLVM_DIR}/bin/llvm-tblgen \
    -DCLANG_TABLEGEN=${BUILD_LOCAL_LLVM_DIR}/bin/clang-tblgen \
    -DLLVM_DEFAULT_TARGET_TRIPLE=riscv64-unknown-linux-gnu \
    -DLLVM_TARGET_ARCH=RISCV64 \
    -DCMAKE_BUILD_TYPE=Release \
    -DOPENMP_ENABLE_LIBOMPTARGET=Off \
	-DLIBOMP_ARCH=riscv64 \
	-DLIBOMP_ENABLE_SHARED=On

ninja  clang lli omp

TinyLlama performance output:

// X86 platform
----------------------------------------------------------------------------
Benchmark                                  Time             CPU   Iterations
----------------------------------------------------------------------------
DL_MODEL_TINYLLAMA/scalar             237142 ms       237115 ms            1
DL_MODEL_TINYLLAMA/matmul_opt          15745 ms        15742 ms            1
DL_MODEL_TINYLLAMA/matmul_opt_omp      13696 ms        13594 ms            1

// RV platform
----------------------------------------------------------------------------
Benchmark                                  Time             CPU   Iterations
----------------------------------------------------------------------------
DL_MODEL_TINYLLAMA/scalar             >1h
DL_MODEL_TINYLLAMA/matmul_opt         342555 ms       342539 ms            1
DL_MODEL_TINYLLAMA/matmul_opt_omp     245805 ms       245794 ms            1

@xlinsist xlinsist force-pushed the omp branch 2 times, most recently from 1d92a08 to b05d4cc Compare October 31, 2024 17:46
Comment on lines +23 to +37
# BUDDY project.
set(BUDDY_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(BUDDY_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/bin)
set(BUDDY_MLIR_BINARY_DIR ${BUDDY_MLIR_BUILD_DIR}/bin)
set(BUDDY_BENCHMARK_DEEP_LEARNING_DIR ${BUDDY_SOURCE_DIR}/benchmarks/DeepLearning)

set(BUILD_TESTS OFF CACHE BOOL "Build tests")
set(BUILD_VALIDATION OFF CACHE BOOL "Build validations")
set(BUILD_CORRECTNESS OFF CACHE BOOL "Build correctness checking")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${BUDDY_BINARY_DIR})

# MLIR binary directory.
set(LLVM_MLIR_BINARY_DIR ${BUDDY_MLIR_BUILD_DIR}/../llvm/build/bin)
set(LLVM_MLIR_LIBRARY_DIR ${BUDDY_MLIR_BUILD_DIR}/../llvm/build/lib)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the original variable setups above.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add openmp support for Bert.

@xlinsist
Copy link
Collaborator Author

Relate commits have been merged, closing it.

@xlinsist xlinsist closed this Jan 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant