Skip to content

Commit

Permalink
Add robust automatic testing (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
saharNooby authored Apr 20, 2023
1 parent 7b28076 commit 1be9fda
Show file tree
Hide file tree
Showing 15 changed files with 532 additions and 36 deletions.
59 changes: 58 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,49 @@ env:
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}

jobs:
ubuntu-latest-cmake-sanitizer:
runs-on: ubuntu-latest

continue-on-error: true

strategy:
matrix:
sanitizer: [ADDRESS, THREAD, UNDEFINED]
build_type: [Debug, Release]
accelerate: [ON, OFF]

steps:
- name: Clone
id: checkout
uses: actions/checkout@v3
with:
submodules: 'recursive'

- name: Dependencies
id: depends
run: |
sudo apt-get update
sudo apt-get install build-essential
- name: Build
id: cmake_build
run: |
mkdir build
cd build
cmake .. -DRWKV_SANITIZE_${{ matrix.sanitizer }}=ON -DGGML_SANITIZE_${{ matrix.sanitizer }}=ON -DRWKV_ACCELERATE=${{ matrix.accelerate }} -DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
cmake --build . --config ${{ matrix.build_type }}
- name: Test
id: cmake_test
run: |
cd build
ctest --verbose
ubuntu-latest-cmake:
runs-on: ubuntu-latest

continue-on-error: true

steps:
- name: Clone
id: checkout
Expand All @@ -42,6 +82,12 @@ jobs:
cmake -DBUILD_SHARED_LIBS=ON ..
cmake --build . --config Release
- name: Test
id: cmake_test
run: |
cd build
ctest --verbose
- name: Get commit hash
id: commit
if: ${{ ( github.event_name == 'push' && github.ref == 'refs/heads/master' ) || github.event.inputs.create_release == 'true' }}
Expand Down Expand Up @@ -71,6 +117,8 @@ jobs:
macOS-latest-cmake:
runs-on: macOS-latest

continue-on-error: true

steps:
- name: Clone
id: checkout
Expand All @@ -86,12 +134,19 @@ jobs:
- name: Build
id: cmake_build
# FMA disabled because it gives "Illegal instruction" in GitHub Actions runner
run: |
mkdir build
cd build
cmake -DBUILD_SHARED_LIBS=ON -DRWKV_AVX2=OFF ..
cmake -DBUILD_SHARED_LIBS=ON -DRWKV_AVX2=OFF -DRWKV_FMA=OFF ..
cmake --build . --config Release
- name: Test
id: cmake_test
run: |
cd build
ctest --verbose
- name: Get commit hash
id: commit
if: ${{ ( github.event_name == 'push' && github.ref == 'refs/heads/master' ) || github.event.inputs.create_release == 'true' }}
Expand Down Expand Up @@ -121,6 +176,8 @@ jobs:
windows-latest-cmake:
runs-on: windows-latest

continue-on-error: true

strategy:
matrix:
include:
Expand Down
42 changes: 18 additions & 24 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ endif()

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
set(RWKV_STANDALONE ON)

# configure project version
# TODO
else()
set(RWKV_STANDALONE OFF)
endif()

if (EMSCRIPTEN)
set(BUILD_SHARED_LIBS_DEFAULT OFF)

Expand All @@ -31,27 +22,25 @@ else()
endif()
endif()


#
# Option list
#

# general
# General
option(RWKV_STATIC "rwkv: static link libraries" OFF)
option(RWKV_NATIVE "rwkv: enable -march=native flag" OFF)
option(RWKV_LTO "rwkv: enable link time optimization" OFF)

# debug
# Debug
option(RWKV_ALL_WARNINGS "rwkv: enable all compiler warnings" ON)
option(RWKV_ALL_WARNINGS_3RD_PARTY "rwkv: enable all compiler warnings in 3rd party libs" OFF)
option(RWKV_GPROF "rwkv: enable gprof" OFF)

# sanitizers
# Sanitizers
option(RWKV_SANITIZE_THREAD "rwkv: enable thread sanitizer" OFF)
option(RWKV_SANITIZE_ADDRESS "rwkv: enable address sanitizer" OFF)
option(RWKV_SANITIZE_UNDEFINED "rwkv: enable undefined sanitizer" OFF)

# instruction set specific
# Instruction set specific
option(RWKV_AVX "rwkv: enable AVX" ON)
option(RWKV_AVX2 "rwkv: enable AVX2" ON)
option(RWKV_AVX512 "rwkv: enable AVX512" OFF)
Expand All @@ -72,16 +61,19 @@ find_package(Threads REQUIRED)

if (NOT MSVC)
if (RWKV_SANITIZE_THREAD)
message(STATUS "Using RWKV_SANITIZE_THREAD")
add_compile_options(-fsanitize=thread)
link_libraries(-fsanitize=thread)
endif()

if (RWKV_SANITIZE_ADDRESS)
message(STATUS "Using RWKV_SANITIZE_ADDRESS")
add_compile_options(-fsanitize=address -fno-omit-frame-pointer)
link_libraries(-fsanitize=address)
endif()

if (RWKV_SANITIZE_UNDEFINED)
message(STATUS "Using RWKV_SANITIZE_UNDEFINED")
add_compile_options(-fsanitize=undefined)
link_libraries(-fsanitize=undefined)
endif()
Expand All @@ -98,6 +90,7 @@ if (APPLE AND RWKV_ACCELERATE)
message(WARNING "Accelerate framework not found")
endif()
endif()

if (RWKV_OPENBLAS)
if (RWKV_STATIC)
set(BLA_STATIC ON)
Expand Down Expand Up @@ -136,7 +129,7 @@ if (RWKV_ALL_WARNINGS)
-Wno-unused-function
)
else()
# todo : msvc
# TODO [llama.cpp]: msvc
endif()

add_compile_options(
Expand All @@ -157,7 +150,7 @@ if (RWKV_LTO)
endif()

# Architecture specific
# TODO: probably these flags need to be tweaked on some architectures
# TODO [llama.cpp]: probably these flags need to be tweaked on some architectures
# feel free to update the Makefile for your architecture and send a pull request or issue
message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
if (NOT MSVC)
Expand All @@ -178,12 +171,12 @@ endif()
if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm" OR ${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
message(STATUS "ARM detected")
if (MSVC)
# TODO: arm msvc?
# TODO [llama.cpp]: arm msvc?
else()
if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
add_compile_options(-mcpu=native)
endif()
# TODO: armv6,7,8 version specific flags
# TODO [llama.cpp]: armv6,7,8 version specific flags
endif()
elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^(x86_64|i686|AMD64)$")
message(STATUS "x86 detected")
Expand Down Expand Up @@ -214,7 +207,7 @@ elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^(x86_64|i686|AMD64)$")
endif()
endif()
else()
# TODO: support PowerPC
# TODO [llama.cpp]: support PowerPC
message(STATUS "Unknown architecture")
endif()

Expand All @@ -232,15 +225,16 @@ if (BUILD_SHARED_LIBS)
set_target_properties(ggml PROPERTIES POSITION_INDEPENDENT_CODE ON)
endif()

add_library(rwkv
rwkv.cpp
rwkv.h)
add_library(rwkv rwkv.cpp rwkv.h)

target_include_directories(rwkv PUBLIC .)
target_compile_features(rwkv PUBLIC cxx_std_11) # don't bump
target_compile_features(rwkv PUBLIC cxx_std_11) # Don't bump
target_link_libraries(rwkv PRIVATE ggml ${RWKV_EXTRA_LIBS})

if (BUILD_SHARED_LIBS)
set_target_properties(rwkv PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_compile_definitions(rwkv PRIVATE RWKV_SHARED RWKV_BUILD)
endif()

enable_testing()
add_subdirectory(tests)
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Georgi Gerganov
Copyright (c) 2023 saharNooby

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ Loading LoRA checkpoints in [Blealtan's format](https://github.com/Blealtan/RWKV
**TODO (contributions welcome!)**:

1. Measure latency and perplexity of different model sizes (169M to 14B) and data types (`FP32`, `FP16`, `Q4_0`, `Q4_1`, `Q4_1_O`)
2. Test on Linux (including Colab) and MacOS
3. Make required memory calculation more robust (see [#4](https://github.com/saharNooby/rwkv.cpp/issues/4))
2. Make required memory calculation more robust (see [#4](https://github.com/saharNooby/rwkv.cpp/issues/4))

## How to use

Expand Down
2 changes: 1 addition & 1 deletion ggml
Submodule ggml updated from 538e51 to 033090
14 changes: 8 additions & 6 deletions rwkv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ struct rwkv_context * rwkv_init_from_file(const char * file_path, uint32_t n_thr
struct ggml_init_params params;
params.mem_size = memory_required;
params.mem_buffer = NULL;
params.no_alloc = false;
struct ggml_context * ctx = ggml_init(params);

std::unordered_map<std::string, struct ggml_tensor *> parameters;
Expand Down Expand Up @@ -587,19 +588,20 @@ bool rwkv_eval(struct rwkv_context * ctx, int32_t token, float * state_in, float
}

void rwkv_free(struct rwkv_context * ctx) {
ctx->model->layers.~vector();
free(ctx->model);
delete[] ctx->state_parts;
ggml_free(ctx->ctx);

delete ctx->model;
delete ctx->state_parts;
delete ctx;
free(ctx->graph);
free(ctx);
}

bool rwkv_quantize_model_file(const char * model_file_path_in, const char * model_file_path_out, uint32_t q_type) {
RWKV_ASSERT_FALSE(q_type == 2 || q_type == 3 || q_type == 4, "Unsupported quantization type %d", q_type);

// Needed to initialize FP16 lookup table
{
struct ggml_init_params params = { 0, NULL };
struct ggml_init_params params = { 0, NULL, false };
struct ggml_context * ctx = ggml_init(params);
ggml_free(ctx);
}
Expand Down Expand Up @@ -789,7 +791,7 @@ bool rwkv_quantize_model_file(const char * model_file_path_in, const char * mode

printf("original size = %8.2f MB\n", total_size_orig / 1024.0 / 1024.0);
printf("quantized size = %8.2f MB\n", total_size_new / 1024.0 / 1024.0);
printf("compression ratio = %8.2f%%\n", 1.0 * total_size_orig / total_size_new);
printf("compression ratio = %8.2f\n", 1.0 * total_size_orig / total_size_new);

{
int64_t sum_all = 0;
Expand Down
2 changes: 1 addition & 1 deletion rwkv.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ extern "C" {
// Returns false on any error. Error messages would be printed to stderr.
// - model_file_path_in: path to model file in ggml format, must be either FP32 or FP16.
// - model_file_path_out: quantized model will be written here.
// - q_type: set to 2 for GGML_TYPE_Q4_0, set to 3 for GGML_TYPE_Q4_1.
// - q_type: set to 2 for GGML_TYPE_Q4_0, set to 3 for GGML_TYPE_Q4_1, set to 4 for GGML_TYPE_Q4_1_O.
RWKV_API bool rwkv_quantize_model_file(const char * model_file_path_in, const char * model_file_path_out, uint32_t q_type);

// Returns system information string.
Expand Down
15 changes: 15 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function(rwkv_add_test source)
get_filename_component(TEST_TARGET ${source} NAME_WE)
add_executable(${TEST_TARGET} ${source})
target_link_libraries(${TEST_TARGET} PRIVATE ggml rwkv)
add_test(NAME ${TEST_TARGET} COMMAND $<TARGET_FILE:${TEST_TARGET}> ${ARGN})
endfunction()

file(COPY tiny-rwkv-660K-FP32.bin DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
file(COPY tiny-rwkv-660K-FP16.bin DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
file(COPY expected_logits.bin DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

rwkv_add_test(test_ggml_basics.c)
rwkv_add_test(test_Q4_1_O.c)
rwkv_add_test(test_Q4_1_O_large_matmul.c)
rwkv_add_test(test_tiny_rwkv.c)
Binary file added tests/expected_logits.bin
Binary file not shown.
Loading

0 comments on commit 1be9fda

Please sign in to comment.