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

feat: Add Mobile CI. #26

Merged
merged 16 commits into from
Nov 10, 2023
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
41 changes: 38 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ permissions:
jobs:
gtest:
name: Unit Tests
runs-on: self-hosted
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/[email protected]
Expand All @@ -31,7 +31,7 @@ jobs:
with:
python-version: '3.9'
cache: 'pip' # caching pip dependencies
- run: cd test && pip install -r requirements.txt
- run: cd test && pip install -r requirements.txt && pip install cmake
- name: Build
run: |
cmake -DTEST=on -Bbuild && cmake --build build --target all
Expand All @@ -51,6 +51,41 @@ jobs:
post: |
COMMIT_MESSAGE="$(git show -s --format=%s)" python3 ../tools/ci/report.py


mobile_test:
name: Unit Tests on Mobile
runs-on: self-hosted
steps:
- name: Checkout
uses: actions/[email protected]
with:
submodules: true
- name: Security Check
# Check if is a forked repo, if so, do not run action job.
if: github.repository != 'UbiquitousLearning/mllm'
run: |
echo "This is a forked repo, stop running"
exit 1
- uses: actions/setup-python@v4
with:
python-version: '3.9'
cache: 'pip' # caching pip dependencies
- run: cd test && pip install -r requirements.txt && pip install cmake
- name: Prepare & Build
run: |
bash tools/ci/build_arm64.sh
- name: Test
uses: gacts/run-and-post-run@v1
env:
FEISHU_TOKEN: ${{ secrets.FEISHU_TOKEN }}
FEISHU_AT: ${{ secrets.FEISHU_AT }}
PR_NUMBER: ${{ github.event.number }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
IS_MOBILE: 1
with:
run: adb devices && python3 ./tools/ci/mobile_test.py
post: |
COMMIT_MESSAGE="$(git show -s --format=%s)" python3 ./tools/ci/report.py




5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
build*/
build/
bin/
bin*/
.clangd/
compile_commands.json
*.bin
Expand All @@ -19,4 +20,6 @@ __pycache__/
_deps
.uuid
*.cmake
Makefile
Makefile
*.model
*.mllm
153 changes: 85 additions & 68 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,152 +1,169 @@
cmake_minimum_required(VERSION 3.10)

project(mllm)
cmake_policy(SET CMP0074 NEW)
cmake_policy(SET CMP0074 NEW)
set(CMAKE_CXX_STANDARD 17)
if (ARM)
option(ARM "build on ARM" OFF)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm" OR ${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
message(STATUS "ARM detected")
set(ARM ON)
set(ANDROID_PLATFORM android-28)

# add_compile_options(-std=c++17)
endif()

if(ARM)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/../bin-arm)
else()
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/../bin)
endif ()
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
endif()

# temp executable name
set(TEST_EXE main_test)
option(TEST "test mode" ON)
option(QUANT "quantize tools" ON)
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")

if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
cmake_policy(SET CMP0135 NEW)
endif ()
endif()

include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)


# find_program(CLANG_TIDY_EXE NAMES "clang-tidy" REQUIRED)
#set(CMAKE_CXX_CLANG_TIDY "clang-tidy")

# set(CMAKE_CXX_CLANG_TIDY "clang-tidy")
option(DEBUG "debug print" OFF)

# backend options
option(NNAPI "Enable NNAPI" OFF)

if(CMAKE_HOST_UNIX)
message("current platform: Linux ")
# set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -std=c++14 -stdlib=libc++")

# set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -std=c++14 -stdlib=libc++")
elseif(CMAKE_HOST_WIN32)
message("current platform: Windows ")
else()
message("current platform: unkonw ")
message("current platform: unknown ")
endif()

aux_source_directory(${PROJECT_SOURCE_DIR}/src DIR_SRC)
aux_source_directory(${PROJECT_SOURCE_DIR}/src DIR_SRC)

# aux_source_directory(${PROJECT_SOURCE_DIR}/src/backends/cpu DIR_SRC_CPU)
aux_source_directory(${PROJECT_SOURCE_DIR}/src/express DIR_SRC_EXP)
#aux_source_directory(${PROJECT_SOURCE_DIR}/src/quantize DIR_SRC_QUANT)
aux_source_directory(${PROJECT_SOURCE_DIR}/src/memory DIR_SRC_MEM_MANAGER)
aux_source_directory(${PROJECT_SOURCE_DIR}/examples EMP_SRC)
aux_source_directory(${PROJECT_SOURCE_DIR}/test TEST_SRC)
aux_source_directory(${PROJECT_SOURCE_DIR}/src/quantizer QUANT_SRC)
aux_source_directory(${PROJECT_SOURCE_DIR}/src/express DIR_SRC_EXP)

# aux_source_directory(${PROJECT_SOURCE_DIR}/src/quantize DIR_SRC_QUANT)
aux_source_directory(${PROJECT_SOURCE_DIR}/src/memory DIR_SRC_MEM_MANAGER)
aux_source_directory(${PROJECT_SOURCE_DIR}/examples EMP_SRC)
aux_source_directory(${PROJECT_SOURCE_DIR}/test TEST_SRC)
aux_source_directory(${PROJECT_SOURCE_DIR}/src/quantizer QUANT_SRC)

include_directories(${PROJECT_SOURCE_DIR}/src)
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${PROJECT_SOURCE_DIR}/thirdpartys/include)

add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/src/backends/cpu)

# if compile to x86_64
if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^(x86_64|i686|AMD64)$")
message(STATUS "x86_64 detected")
add_compile_options(-mf16c)
add_compile_options(-mavx2)
elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm" OR ${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "^(x86_64|i686|AMD64)$")
message(STATUS "x86_64 detected")
add_compile_options(-mf16c)
add_compile_options(-mavx2)
elseif(${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm" OR ${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
message(STATUS "ARM detected")
endif()
if (QUANT)

if(QUANT)
include_directories(${PROJECT_SOURCE_DIR}/src/quantizer)
file(GLOB_RECURSE MLLM_QUANT

${PROJECT_SOURCE_DIR}/src/backends/cpu/quantize/*.hpp
${PROJECT_SOURCE_DIR}/src/backends/cpu/quantize/*.cpp
${PROJECT_SOURCE_DIR}/src/backends/cpu/quantize/*.hpp
${PROJECT_SOURCE_DIR}/src/backends/cpu/quantize/*.cpp
)
# MLLM Quantizer is /src/quantizer/*.cpp but except main.cpp

# MLLM Quantizer is /src/quantizer/*.cpp but except main.cpp
file(GLOB_RECURSE MLLM_QUANTIZER
${CMAKE_CURRENT_LIST_DIR}/src/quantizer/*.cpp
${CMAKE_CURRENT_LIST_DIR}/src/quantizer/*.hpp)
list(REMOVE_ITEM MLLM_QUANTIZER ${CMAKE_CURRENT_LIST_DIR}/src/quantizer/main.cpp)
${CMAKE_CURRENT_LIST_DIR}/src/quantizer/*.cpp
${CMAKE_CURRENT_LIST_DIR}/src/quantizer/*.hpp)
list(REMOVE_ITEM MLLM_QUANTIZER ${CMAKE_CURRENT_LIST_DIR}/src/quantizer/main.cpp)
message(STATUS "MLLM_Quant: ${MLLM_QUANT}")
add_executable(
quantize
${PROJECT_SOURCE_DIR}/src/quantizer/main.cpp
${MLLM_QUANT}
${MLLM_QUANTIZER}
# ${DIR_SRC}
${PROJECT_SOURCE_DIR}/src/ParamLoader.cpp
quantize
${PROJECT_SOURCE_DIR}/src/quantizer/main.cpp
${MLLM_QUANT}
${MLLM_QUANTIZER}

# ${DIR_SRC}
${PROJECT_SOURCE_DIR}/src/ParamLoader.cpp
)
endif()


endif ()


if (TEST)
if(TEST)
add_definitions(-DTEST)
FetchContent_MakeAvailable(googletest)
enable_testing()
include_directories(${PROJECT_SOURCE_DIR}/test)

# MLLM Tests
file(GLOB_RECURSE MLLM_TEST
${CMAKE_CURRENT_LIST_DIR}/test/*.cpp
${CMAKE_CURRENT_LIST_DIR}/test/*.hpp
${CMAKE_CURRENT_LIST_DIR}/test/*.cpp
${CMAKE_CURRENT_LIST_DIR}/test/*.hpp
)
list(REMOVE_ITEM MLLM_TEST ${CMAKE_CURRENT_LIST_DIR}/test/TestSystemMemoryManager.cpp)
list(REMOVE_ITEM MLLM_TEST ${CMAKE_CURRENT_LIST_DIR}/test/TestMemoryPoolManager.cpp)


message(STATUS "MLLM_TEST: ${MLLM_TEST}")

# Add ${DIR_SRC_QUANT} to ${DIR_SRC}
# if ()
add_executable(
MLLM_TEST
${PROJECT_SOURCE_DIR}/test/main.cpp
${MLLM_TEST}
${DIR_SRC_CPU} ${DIR_SRC_MEM_MANAGER} ${DIR_SRC_EXP} ${DIR_SRC} ${MLLM_QUANTIZER}
MLLM_TEST
${PROJECT_SOURCE_DIR}/test/main.cpp
${MLLM_TEST}
${DIR_SRC_CPU} ${DIR_SRC_MEM_MANAGER} ${DIR_SRC_EXP} ${DIR_SRC} ${MLLM_QUANTIZER}
)

target_link_libraries(
MLLM_TEST
GTest::gtest_main
MLLM_CPU
MLLM_TEST
GTest::gtest_main
MLLM_CPU
)
add_executable(
memoryPoolTest
${PROJECT_SOURCE_DIR}/test/TestMemoryPoolManager.cpp
${DIR_SRC_MEM_MANAGER} ${PROJECT_SOURCE_DIR}/src/MemoryManager.hpp
${DIR_SRC_MEM_MANAGER} ${PROJECT_SOURCE_DIR}/src/MemoryManager.hpp
)
include(GoogleTest)
gtest_discover_tests(MLLM_TEST)
endif ()

#else ()
# No Unit Test.
add_executable(${TEST_EXE} ${PROJECT_SOURCE_DIR}/demo/main_test.cpp ${DIR_SRC_CPU} ${DIR_SRC_MEM_MANAGER} ${DIR_SRC_EXP} ${DIR_SRC} #${DIR_SRC_QUANT}
src/tokenizers/Tokenizer.cpp
src/tokenizers/Tokenizer.hpp
src/tokenizers/BPE/Bpe.cpp
src/tokenizers/BPE/Bpe.hpp)
target_link_libraries(${TEST_EXE} MLLM_CPU )

# if not cross-compiling
if(NOT ARM)
gtest_discover_tests(MLLM_TEST)
endif()
endif()

# else ()
# No Unit Test.
add_executable(${TEST_EXE} ${PROJECT_SOURCE_DIR}/demo/main_test.cpp ${DIR_SRC_CPU} ${DIR_SRC_MEM_MANAGER} ${DIR_SRC_EXP} ${DIR_SRC} # ${DIR_SRC_QUANT}
src/tokenizers/Tokenizer.cpp
src/tokenizers/Tokenizer.hpp
src/tokenizers/BPE/Bpe.cpp
src/tokenizers/BPE/Bpe.hpp)
target_link_libraries(${TEST_EXE} MLLM_CPU)

# nnapi temp test
if(NNAPI)
# include(/Users/luis/Library/Android/sdk/ndk/25.1.8937393/build/cmake/android.toolchain.cmake)
include(${ANDROID_NDK}/build/cmake/android.toolchain.cmake)
add_definitions(-DNNAPI_ENABLED)

# TODO: better debug log
add_definitions(-DDEBUG)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/src/backends/nnapi)
add_executable(nnapi_test ${PROJECT_SOURCE_DIR}/demo/nnapi_test.cpp ${DIR_SRC_CPU} ${DIR_SRC_EXP} ${DIR_SRC} )#${DIR_SRC_QUANT})
add_executable(nnapi_test ${PROJECT_SOURCE_DIR}/demo/nnapi_test.cpp ${DIR_SRC_CPU} ${DIR_SRC_EXP} ${DIR_SRC}) # ${DIR_SRC_QUANT})
target_link_libraries(nnapi_test MLLM_CPU MLLM_NNAPI)
endif()

2 changes: 1 addition & 1 deletion src/Op.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class Op {
return NO_ERROR;
}

virtual ErrorCode load(ParamLoader &loader) {
virtual ErrorCode load(AbstructLoader &loader) {
// check inputs shape
// reshape outputs
#ifdef DEBUG
Expand Down
15 changes: 11 additions & 4 deletions src/ParamLoader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,15 @@ static std::string readString(FILE *fp_) {
delete[] tmp;
return str;
}

#define _MAGIC_NUMBER 20012
class ParamLoader {
class AbstructLoader {
public:
virtual bool load(mllm::Tensor *tensor) = 0;
virtual bool load(std::shared_ptr<mllm::Tensor> tensor) = 0;
virtual DataType getDataType(string name) = 0;
};
class ParamLoader : public AbstructLoader {
friend class QuantWriter;

public:
Expand All @@ -53,11 +60,11 @@ class ParamLoader {
ParamLoader(void *buffer);
#endif
~ParamLoader();
bool load(mllm::Tensor *tensor);
bool load(std::shared_ptr<mllm::Tensor> tensor);
bool load(mllm::Tensor *tensor) override;
bool load(std::shared_ptr<mllm::Tensor> tensor) override;
vector<std::string> getParamNames();
std::tuple<uint8_t *, uint64_t> load(string name);
DataType getDataType(string name);
DataType getDataType(string name) override;

private:
FILE *fp_;
Expand Down
2 changes: 1 addition & 1 deletion src/backends/cpu/CPUAttention.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ ErrorCode CPUAttention::execute(vector<shared_ptr<Tensor>> inputs, vector<shared
// outputs[0]->printData<float>();
return NO_ERROR;
}
ErrorCode CPUAttention::load(ParamLoader &loader) {
ErrorCode CPUAttention::load(AbstructLoader &loader) {
Q_proj_->load(loader);
K_proj_->load(loader);
V_proj_->load(loader);
Expand Down
2 changes: 1 addition & 1 deletion src/backends/cpu/CPUAttention.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class CPUAttention final : public Op {
virtual ~CPUAttention() = default;
virtual ErrorCode reshape(vector<shared_ptr<Tensor>> inputs, vector<shared_ptr<Tensor>> outputs) override;
virtual ErrorCode setUp(vector<shared_ptr<Tensor>> inputs, vector<shared_ptr<Tensor>> outputs) override;
virtual ErrorCode load(ParamLoader &loader) override;
virtual ErrorCode load(AbstructLoader &loader) override;
virtual ErrorCode execute(vector<shared_ptr<Tensor>> inputs, vector<shared_ptr<Tensor>> outputs) override;
virtual ErrorCode free(vector<shared_ptr<Tensor>> inputs, vector<shared_ptr<Tensor>> outputs) override;

Expand Down
3 changes: 2 additions & 1 deletion src/backends/cpu/CPUEmbedding.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "CPUEmbedding.hpp"
#include "ParamLoader.hpp"
mllm::CPUEmbedding::CPUEmbedding(mllm::Backend *bn, string opName, int hiddenSize, int vocabSize) :
Op(bn, opName), hiddenSize_(hiddenSize), vocabSize_(vocabSize) {
CHECK_GT(hiddenSize_, 0);
Expand All @@ -18,7 +19,7 @@ ErrorCode mllm::CPUEmbedding::reshape(vector<shared_ptr<Tensor>> inputs, vector<
return Op::reshape(inputs, outputs);
}

ErrorCode mllm::CPUEmbedding::load(mllm::ParamLoader &loader) {
ErrorCode mllm::CPUEmbedding::load(mllm::AbstructLoader &loader) {
weight_.setName(name() + ".weight");
weight_.reshape(1, 1, vocabSize_, hiddenSize_);
weight_.setDtype(loader.getDataType(weight_.name()));
Expand Down
2 changes: 1 addition & 1 deletion src/backends/cpu/CPUEmbedding.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class CPUEmbedding final : public Op {
explicit CPUEmbedding(Backend *bn, string opName, int hiddenSize, int vocabSize);
~CPUEmbedding() override = default;
ErrorCode reshape(vector<shared_ptr<Tensor>> inputs, vector<shared_ptr<Tensor>> outputs) override;
ErrorCode load(ParamLoader &loader) override;
ErrorCode load(AbstructLoader &loader) override;
ErrorCode free(vector<shared_ptr<Tensor>> inputs, vector<shared_ptr<Tensor>> outputs) override;
ErrorCode execute(vector<shared_ptr<Tensor>> inputs, vector<shared_ptr<Tensor>> outputs) override;

Expand Down
Loading
Loading