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

move lut from src/libllm to src #85

Merged
merged 2 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/cmake-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ jobs:
- name: Build
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
- name: Test
run: ${{github.workspace}}\build\src\libllm\${{env.BUILD_TYPE}}\unittest.exe
run: ${{github.workspace}}\build\${{env.BUILD_TYPE}}\unittest.exe
64 changes: 63 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ option(WITH_OPENMP "Build with OpenMP." ON)
option(WITH_CUTLASS "build MatMul operators with CUTLASS." OFF)
option(MKL_PREFIX "Prefix for MKL headers and libraries." "/opt/intel/mkl")

find_package(OpenMP)

set(CMAKE_CUDA_RUNTIME_LIBRARY Static)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

Expand Down Expand Up @@ -81,4 +83,64 @@ if(HAVE_FP16)
add_compile_definitions(LIBLLM_HAVE_FP16)
endif()

add_subdirectory("src/libllm")
add_subdirectory("src")

set(libllm_LIBADD lut ${CMAKE_DL_LIBS})

if(WITH_OPENMP)
set(libllm_LIBADD ${libllm_LIBADD} OpenMP::OpenMP_CXX)
endif()

add_library(libllm SHARED $<TARGET_OBJECTS:libllm_static>)
target_link_libraries(libllm ${libllm_LIBADD} )
set_property(TARGET libllm PROPERTY OUTPUT_NAME llm)
if(UNIX AND NOT APPLE)
target_link_options(libllm PUBLIC "-Wl,--no-undefined")
endif()

set(unittest_LIBADD
libllm_static
libllm_test
lut_test
${libllm_LIBADD}
catch2)
if (WITH_MKL)
set(unittest_LIBADD
${unittest_LIBADD}
mkl_intel_lp64
mkl_intel_thread
mkl_core
iomp5)
endif()

set(benchmark_LIBADD
libllm_static
${libllm_LIBADD})

add_library(catch2 STATIC "../third_party/catch2/catch_amalgamated.cpp")
add_executable(unittest "src/libllm/test_main.cc")
target_include_directories(unittest PRIVATE "src")

target_link_libraries(unittest ${unittest_LIBADD})

add_executable(benchmark "src/libllm/benchmark_main.cc")
target_include_directories(benchmark PRIVATE "src")
target_link_libraries(benchmark ${benchmark_LIBADD})

if (WITH_CUDA)
add_library(llmplugincublas SHARED $<TARGET_OBJECTS:llmplugincublas_static>)
target_include_directories(llmplugincublas PRIVATE ${libllm_INCDIR})
target_link_libraries(llmplugincublas lut CUDA::cublas)
if(UNIX)
target_link_options(llmplugincublas PUBLIC "-Wl,--no-undefined")
endif(UNIX)
endif()

enable_testing()
add_test(NAME unittest COMMAND $<TARGET_FILE:unittest>)

add_custom_target(llm
ALL
DEPENDS libllm
COMMAND go build -o $<PATH:GET_PARENT_PATH,$<TARGET_FILE:unittest>>/llm${CMAKE_EXECUTABLE_SUFFIX} ${CMAKE_SOURCE_DIR}/go/bin
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/go/bin)
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_subdirectory("lut")
add_subdirectory("libllm")
133 changes: 17 additions & 116 deletions src/libllm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,8 @@
find_package(OpenMP)
if (OPENMP_FOUND)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif()

set(lut_SOURCES
"lut/internal/log.cc"
"lut/internal/sprintf.cc"
"lut/base64.cc"
"lut/error.cc"
"lut/flags.cc"
"lut/half.cc"
"lut/ini_config.cc"
"lut/is_debug.cc"
"lut/path.cc"
"lut/random.cc"
"lut/reader.cc"
"lut/strings.cc"
"lut/time.cc"
"lut/thread_pool.cc"
"lut/zip_file.cc")

set(libllm_SOURCES
"cpu/kernel/fallback.cc"
"cpu/kernel/interface.cc"
Expand Down Expand Up @@ -72,37 +54,26 @@ set(libllm_SOURCES
"whisper.cc"
"../../third_party/ruapu/ruapu.cc")

set(unittest_SOURCES
set(libllm_test_SOURCES
"cpu/kernel/benchmark.cc"
"cpu/kernel/interface_test.cc"
"cpu/log_mel_spectrogram_test.cc"
"cpu/test.cc"
"lut/path_test.cc"
"lut/strings_test.cc"
"llama_test.cc"
"module_test.cc"
"operator_tester.cc"
"tensor_test.cc"
"test_helper.cc"
"test_main.cc")

set(llm_SOURCES
"dialog_manager.cc"
"llm_main.cc")
"test_helper.cc")


set(benchmark_SOURCES "benchmark_main.cc")

set(libllm_INCDIR ".." "../../third_party")
set(libllm_LIBADD lut ${CMAKE_DL_LIBS})

if(WITH_OPENMP)
if(NOT OPENMP_FOUND)
message(FATAL_ERROR "WITH_OPENMP=ON build OpenMP package not found.")
endif()
set(libllm_SOURCES ${libllm_SOURCES} "mp_openmp.cc")
set(libllm_INCDIR ${libllm_INCDIR} ${OpenMP_CXX_INCLUDE_DIRS})
set(libllm_LIBADD ${libllm_LIBADD} OpenMP::OpenMP_CXX)
else()
set(libllm_SOURCES ${libllm_SOURCES} "mp_thread_pool.cc")
endif()
Expand Down Expand Up @@ -135,40 +106,15 @@ if (WITH_CUDA)
"cuda/transform.cu"
"cuda/unfold.cu")

set(unittest_SOURCES ${unittest_SOURCES} "cuda/test.cc")
set(libllm_test_SOURCES ${libllm_test_SOURCES} "cuda/test.cc")

if (WITH_CUTLASS)
set(libllm_SOURCES ${libllm_SOURCES} "cuda/gemm_cutlass.cu")
endif(WITH_CUTLASS)

set(llmextcublas_SOURCES
set(llmplugincublas_SOURCES
"cuda/gemm_cublas.cc"
"lut/internal/log.cc")
endif()

# OS specific code
if(WIN32)
set(libllm_SOURCES
${libllm_SOURCES}
"lut/path_windows.cc"
"lut/platform_windows.cc"
"lut/shared_library_windows.cc")
endif()
if(UNIX)
set(libllm_SOURCES
${libllm_SOURCES}
"lut/platform_linux.cc"
"lut/shared_library_linux.cc")
endif()
if(UNIX AND APPLE)
set(libllm_SOURCES
${libllm_SOURCES}
"lut/path_darwin.cc")
endif()
if(UNIX AND NOT APPLE)
set(libllm_SOURCES
${libllm_SOURCES}
"lut/path_linux.cc")
"../lut/internal/log.cc")
endif()

# CPU specific code
Expand All @@ -193,76 +139,31 @@ if(LIBLLM_KERNEL_X86_64)
"cpu/kernel/avx2.cc"
PROPERTIES COMPILE_FLAGS "-mavx2 -mfma -mf16c")
endif(UNIX)
set(unittest_SOURCES
${unittest_SOURCES}
set(libllm_test_SOURCES
${libllm_test_SOURCES}
"cpu/kernel/avx2_test.cc"
"cpu/kernel/avx512_test.cc")
endif()

if(LIBLLM_KERNEL_AARCH64)
set(libllm_SOURCES ${libllm_SOURCES} "cpu/kernel/asimdhp.cc")
set(unittest_SOURCES
${unittest_SOURCES}
set(libllm_test_SOURCES
${libllm_test_SOURCES}
"cpu/test_float16.cc"
"cpu/kernel/asimdhp_test.cc")
endif()

add_library(lut STATIC ${lut_SOURCES})
set_target_properties(lut PROPERTIES CXX_VISIBILITY_PRESET hidden)
target_include_directories(lut PRIVATE ${libllm_INCDIR})
if (WITH_CUDA)
add_library(llmplugincublas_static OBJECT ${llmplugincublas_SOURCES})
target_compile_options(llmplugincublas_static PRIVATE "-DLIBLLM_EXPORTS")
target_include_directories(llmplugincublas_static PRIVATE ${libllm_INCDIR})
endif()

add_library(libllm_static OBJECT ${libllm_SOURCES})
target_compile_options(libllm_static PRIVATE "-DLIBLLM_EXPORTS")
set_target_properties(libllm_static PROPERTIES CXX_VISIBILITY_PRESET hidden)
set_target_properties(libllm_static PROPERTIES CUDA_VISIBILITY_PRESET hidden)
target_include_directories(libllm_static PRIVATE ${libllm_INCDIR})

add_library(libllm SHARED $<TARGET_OBJECTS:libllm_static>)
target_link_libraries(libllm ${libllm_LIBADD} )
set_property(TARGET libllm PROPERTY OUTPUT_NAME llm)
if(UNIX AND NOT APPLE)
target_link_options(libllm PUBLIC "-Wl,--no-undefined")
endif()

set(unittest_LIBADD
libllm_static
${libllm_LIBADD}
catch2)
if (WITH_MKL)
set(unittest_LIBADD
${unittest_LIBADD}
mkl_intel_lp64
mkl_intel_thread
mkl_core
iomp5)
endif()

set(benchmark_LIBADD
libllm_static
${libllm_LIBADD})

add_library(catch2 STATIC "../../third_party/catch2/catch_amalgamated.cpp")
add_executable(unittest ${unittest_SOURCES})
target_include_directories(unittest PRIVATE ${libllm_INCDIR})
target_link_libraries(unittest ${unittest_LIBADD})

add_executable(benchmark ${benchmark_SOURCES})
target_include_directories(benchmark PRIVATE ${libllm_INCDIR})
target_link_libraries(benchmark ${benchmark_LIBADD})

if (WITH_CUDA)
add_library(llmextcublas SHARED ${llmextcublas_SOURCES})
target_include_directories(llmextcublas PRIVATE ${libllm_INCDIR})
target_link_libraries(llmextcublas lut CUDA::cublas)
if(UNIX)
target_link_options(llmextcublas PUBLIC "-Wl,--no-undefined")
endif(UNIX)
endif()

enable_testing()
add_test(NAME unittest COMMAND $<TARGET_FILE:unittest>)

add_custom_target(llmbin
ALL
DEPENDS libllm
COMMAND go build -o $<PATH:GET_PARENT_PATH,$<TARGET_FILE:unittest>>/llm${CMAKE_EXECUTABLE_SUFFIX} ${CMAKE_SOURCE_DIR}/go/bin
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/go/bin)
add_library(libllm_test OBJECT ${libllm_test_SOURCES})
target_include_directories(libllm_test PRIVATE ${libllm_INCDIR})
8 changes: 4 additions & 4 deletions src/libllm/benchmark_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
#include "libllm/functional.h"
#include "libllm/llama.h"
#include "libllm/llm.h"
#include "libllm/lut/error.h"
#include "libllm/lut/flags.h"
#include "libllm/lut/random.h"
#include "libllm/lut/time.h"
#include "libllm/model_for_generation.h"
#include "libllm/operators.h"
#include "lut/error.h"
#include "lut/flags.h"
#include "lut/random.h"
#include "lut/time.h"

constexpr int MagicNumber = 0x55aa;
constexpr double MaxWait = 10;
Expand Down
5 changes: 3 additions & 2 deletions src/libllm/bpe_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// restriction, including without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
Expand All @@ -21,7 +21,8 @@

#include <memory>
#include <string>
#include "libllm/lut/ini_config.h"

#include "lut/ini_config.h"

namespace libllm {

Expand Down
17 changes: 8 additions & 9 deletions src/libllm/bpe_encoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// restriction, including without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
Expand All @@ -19,18 +19,18 @@

#include "libllm/bpe_encoder.h"

#include "libllm/lut/strings.h"
#include "lut/strings.h"

namespace libllm {

BPEEncoder::BPEEncoder(const BPEModel *model, const BPEConfig &config)
: _model(model),
_config(&config),
_header(nullptr) {}
_header(nullptr) {
}

void BPEEncoder::initQueue() {
Symbol *p = _header->next,
*q = p->next;
Symbol *p = _header->next, *q = p->next;
while (q) {
addBigramIfExist(p, q);
p = q;
Expand Down Expand Up @@ -69,8 +69,7 @@ std::vector<int> BPEEncoder::encode(const std::string &s) {
}

void BPEEncoder::addBigramIfExist(Symbol *left, Symbol *right) {
if (left == _header || right == nullptr ||
_model->isSpecialToken(right->tokenId) ||
if (left == _header || right == nullptr || _model->isSpecialToken(right->tokenId) ||
_model->isSpecialToken(left->tokenId)) {
return;
}
Expand Down Expand Up @@ -119,7 +118,7 @@ BPEEncoder::Symbol *BPEEncoder::mergeBigram(const Bigram &bigram) {

std::vector<std::string> BPEEncoder::splitBytes(const std::string &s) {
std::vector<std::string> l;

char buffer[2] = " ";
for (char ch : s) {
buffer[0] = ch;
Expand All @@ -137,7 +136,7 @@ BPEEncoder::Symbol *BPEEncoder::appendToken(Symbol *tail, int tokenId) {
symbol->next = nullptr;

tail->next = symbol;

return symbol;
}

Expand Down
Loading
Loading