diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..94017c7 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,34 @@ + +# Options +option(BUILD_SHARED_LIBS "Build using shared libraries" OFF ) +option(CMAKE_BUILD_TYPE "Build type" Debug ) +option(STATIC_LINKING "Link statically" TRUE ) + +option(X86 "Build for x86" OFF ) +option(RISCV "Build for RISC-V" OFF ) + + +add_library(target-riscv INTERFACE) +target_compile_features(target-riscv INTERFACE c_std_11) + +add_library(target-x86 INTERFACE) +target_compile_features(target-x86 INTERFACE c_std_11) + +target_link_options(target-x86 INTERFACE -static) +target_link_options(target-riscv INTERFACE -static) + +target_compile_options(target-x86 INTERFACE -march=native) +target_link_options(target-x86 INTERFACE -march=native) + + +set(WRAP_SPECS_FILE "htif_wrap.specs") +set(SPECS_FILE "htif_nano.specs") +set(LIBGLOSS_DIR "$ENV{RISCV}/riscv64-unknown-elf/lib/") + +target_compile_options(target-riscv INTERFACE -fno-common -fno-builtin-printf) +target_compile_options(target-riscv INTERFACE -mcmodel=medany -march=rv64gcv_zfh -mabi=lp64d) +target_compile_options(target-riscv INTERFACE -Wl,-Map=output.map -specs=${SPECS_FILE} -specs=${WRAP_SPECS_FILE} -T ${CMAKE_SOURCE_DIR}/htif.ld) +target_link_options(target-x86 INTERFACE -fno-common -fno-builtin-printf) +target_link_options(target-riscv INTERFACE -mcmodel=medany -march=rv64gcv_zfh -mabi=lp64d) +target_link_options(target-riscv INTERFACE -Wl,-Map=output.map -specs=${SPECS_FILE} -specs=${WRAP_SPECS_FILE} -T ${CMAKE_SOURCE_DIR}/htif.ld) + diff --git a/example/fast-depth/CMakeLists.txt b/example/fast-depth/CMakeLists.txt index 13e76a8..c3960ee 100644 --- a/example/fast-depth/CMakeLists.txt +++ b/example/fast-depth/CMakeLists.txt @@ -1,34 +1,19 @@ -# the CMake files are still very hacky -# TODO: organize the build system properly - cmake_minimum_required(VERSION 3.10) project(fast-depth LANGUAGES C) -# Options -option(BUILD_SHARED_LIBS "Build using shared libraries" OFF ) -option(CMAKE_BUILD_TYPE "Build type" Debug ) -option(STATIC_LINKING "Link statically" TRUE ) - -option(X86 "Build for x86" OFF ) -option(RISCV "Build for RISC-V" OFF ) - - add_executable(fast-depth main.c) target_include_directories(fast-depth PUBLIC inc) target_compile_features(fast-depth INTERFACE c_std_11) - -set(CMAKE_C_FLAGS "-O3 -Wall -Wextra") -set(CMAKE_EXE_LINKER_FLAGS "-static") - if (X86) - message("Building for x86") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=native") + message("fast-depth: building for x86") + target_link_libraries(fast-depth PUBLIC target-x86) + elseif (RISCV) - message("Building for RISC-V") + message("fast-depth: building for RISC-V") # CMake toolchain definition for RISC-V GCC toolchain set(CMAKE_SYSTEM_NAME "Generic" CACHE STRING "") set(CMAKE_SYSTEM_PROCESSOR "riscv" CACHE STRING "") @@ -43,25 +28,17 @@ elseif (RISCV) set(CMAKE_OBJCOPY "${TOOLCHAIN_PREFIX}objcopy") set(CMAKE_SIZE "${TOOLCHAIN_PREFIX}size") set(CMAKE_STRIP "${TOOLCHAIN_PREFIX}ld") - - set(WRAP_SPECS_FILE "htif_wrap.specs") - set(SPECS_FILE "htif_nano.specs") - set(LIBGLOSS_DIR "$ENV{RISCV}/riscv64-unknown-elf/lib/") - - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-common -fno-builtin-printf") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mcmodel=medany -march=rv64gcv_zfh -mabi=lp64d") - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Map=output.map -specs=${SPECS_FILE} -specs=${WRAP_SPECS_FILE} -T ${CMAKE_SOURCE_DIR}/htif.ld") + + target_link_libraries(fast-depth PUBLIC target-riscv) endif () +add_compile_options(-O3 -Wall -Wextra) target_compile_options(fast-depth PRIVATE -u _printf_float) +add_subdirectory(../../ ./build/) -message("Compiler Flags: ${CMAKE_C_FLAGS}") -message("Linker Flags: ${CMAKE_EXE_LINKER_FLAGS}") - - -add_subdirectory(../../nn ./build/nn) +add_subdirectory(../../nn/ ./build/nn) target_link_libraries(fast-depth PUBLIC nn) target_link_libraries(fast-depth PUBLIC m) diff --git a/example/fast-depth/main.c b/example/fast-depth/main.c index 4dd0ac1..bbda924 100644 --- a/example/fast-depth/main.c +++ b/example/fast-depth/main.c @@ -67,16 +67,16 @@ void showASCIIImage(Tensor *tensor) { } } -static void enable_vector_operations() { - unsigned long mstatus; - asm volatile("csrr %0, mstatus" : "=r"(mstatus)); - mstatus |= 0x00000600 | 0x00006000 | 0x00018000; - asm volatile("csrw mstatus, %0"::"r"(mstatus)); -} +// static void enable_vector_operations() { +// unsigned long mstatus; +// asm volatile("csrr %0, mstatus" : "=r"(mstatus)); +// mstatus |= 0x00000600 | 0x00006000 | 0x00018000; +// asm volatile("csrw mstatus, %0"::"r"(mstatus)); +// } int main() { - enable_vector_operations(); + // enable_vector_operations(); Model *model = malloc(sizeof(Model)); @@ -89,9 +89,9 @@ int main() { // NN_fill_F32(&model->x, 0.0); memcpy((uint8_t *)model->x.data, (uint8_t *)model_input_data, (size_t)model_input_end - (size_t)model_input_start); - cycles = READ_CSR("mcycle"); + // cycles = READ_CSR("mcycle"); forward(model); - cycles = READ_CSR("mcycle") - cycles; + // cycles = READ_CSR("mcycle") - cycles; printf("cycles: %lu\n", cycles); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt deleted file mode 100644 index 7e03f14..0000000 --- a/test/CMakeLists.txt +++ /dev/null @@ -1,77 +0,0 @@ - -# the CMake files are still very hacky -# TODO: organize the build system properly - -cmake_minimum_required(VERSION 3.15) - -set(PROJECT_NAME "example") - -set(PROJECT_INCLUDES - inc -) - -set(PROJECT_SOURCES - src/main.c -) - -project(${PROJECT_NAME}) - - -set(RISCV_DIR $ENV{RISCV}) -set(CMAKE_CXX_COMPILER_FORCED TRUE) -set(CMAKE_C_STANDARD 17) -set(CMAKE_CXX_STANDARD_REQUIRED ON) - - -# Add optimization flags -set(CMAKE_BUILD_TYPE Debug) -set(BUILD_SHARED_LIBS OFF) -set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "") -set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2") -set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O2") - -set(STATIC_LINKING TRUE) - -set(WRAP_SPECS_FILE "${RISCV_DIR}/riscv64-unknown-elf/lib/htif_wrap.specs") -set(SPECS_FILE "${RISCV_DIR}/riscv64-unknown-elf/lib/htif_nano.specs") -set(LIBGLOSS_DIR "${RISCV_DIR}/riscv64-unknown-elf/lib/") - - -# CMake toolchain definition for RISC-V GCC toolchain -set(CMAKE_SYSTEM_NAME "Generic" CACHE STRING "") -set(CMAKE_SYSTEM_PROCESSOR "riscv" CACHE STRING "") - -set(TOOLCHAIN_PREFIX "riscv64-unknown-elf-") - -set(CMAKE_C_COMPILER "${TOOLCHAIN_PREFIX}gcc") -set(CMAKE_ASM_COMPILER "${TOOLCHAIN_PREFIX}gcc") -set(CMAKE_CXX_COMPILER "${TOOLCHAIN_PREFIX}g++") -set(CMAKE_AR "${TOOLCHAIN_PREFIX}ar") -set(CMAKE_LINKER "{TOOLCHAIN_PREFIX}ld") -set(CMAKE_OBJCOPY "${TOOLCHAIN_PREFIX}objcopy") -set(CMAKE_SIZE "${TOOLCHAIN_PREFIX}size") -set(CMAKE_STRIP "${TOOLCHAIN_PREFIX}ld") - - -if(AVX) - set(CMAKE_C_FLAGS "-march=native") -endif() - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Og -mcmodel=medany -march=rv64gcv_zfh -mabi=lp64d -fno-common -fno-builtin-printf") -set(CMAKE_C_FLAGS ${CMAKE_CXX_FLAGS}) -set(CMAKE_EXE_LINKER_FLAGS "-static -lm -lstdc++ -Wl,-Map=output.map -L${LIBGLOSS_DIR} -specs=${SPECS_FILE} -specs=${WRAP_SPECS_FILE} -T ${CMAKE_SOURCE_DIR}/htif.ld") - - -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -u _printf_float") - -message(CMAKE_C_FLAGS: ${CMAKE_C_FLAGS}) - -add_executable(${PROJECT_NAME} ${PROJECT_SOURCES}) -target_include_directories(${PROJECT_NAME} PUBLIC ${PROJECT_INCLUDES}) - -add_subdirectory(../nn ./build/nn) -target_link_libraries(${PROJECT_NAME} PUBLIC nn) - -target_compile_options(${PROJECT_NAME} PRIVATE -u _printf_float) - - diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..66d481b --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,45 @@ +cmake_minimum_required(VERSION 3.10) + +project(test LANGUAGES C) + + +add_executable(test src/main.c) +target_include_directories(test PUBLIC inc) + +target_compile_features(test INTERFACE c_std_11) + +if (X86) + message("test: building for x86") + target_link_libraries(test PUBLIC target-x86) + +elseif (RISCV) + message("test: building for RISC-V") + # CMake toolchain definition for RISC-V GCC toolchain + set(CMAKE_SYSTEM_NAME "Generic" CACHE STRING "") + set(CMAKE_SYSTEM_PROCESSOR "riscv" CACHE STRING "") + + set(TOOLCHAIN_PREFIX "riscv64-unknown-elf-") + + set(CMAKE_C_COMPILER "${TOOLCHAIN_PREFIX}gcc") + set(CMAKE_ASM_COMPILER "${TOOLCHAIN_PREFIX}gcc") + set(CMAKE_CXX_COMPILER "${TOOLCHAIN_PREFIX}g++") + set(CMAKE_AR "${TOOLCHAIN_PREFIX}ar") + set(CMAKE_LINKER "{TOOLCHAIN_PREFIX}ld") + set(CMAKE_OBJCOPY "${TOOLCHAIN_PREFIX}objcopy") + set(CMAKE_SIZE "${TOOLCHAIN_PREFIX}size") + set(CMAKE_STRIP "${TOOLCHAIN_PREFIX}ld") + + target_link_libraries(test PUBLIC target-riscv) +endif () + +add_compile_options(-O3 -Wall -Wextra) + +target_compile_options(test PRIVATE -u _printf_float) + +add_subdirectory(../ ./build/) + +add_subdirectory(../nn/ ./build/nn) +target_link_libraries(test PUBLIC nn) + +target_link_libraries(test PUBLIC m) + diff --git a/test/htif.ld b/tests/htif.ld similarity index 100% rename from test/htif.ld rename to tests/htif.ld diff --git a/test/src/main.c b/tests/src/main.c similarity index 83% rename from test/src/main.c rename to tests/src/main.c index f04cd1e..3b1b414 100644 --- a/test/src/main.c +++ b/tests/src/main.c @@ -4,7 +4,7 @@ #include #include "nn.h" -#include "riscv_vector.h" +// #include "riscv_vector.h" void print_bits_half(float16_t x) { @@ -46,8 +46,11 @@ uint8_t compareResult(float golden, float actual) { } int main() { - for (size_t i = 0; i < 100; i += 1) { - float x = rand() / (float)RAND_MAX * 1000.0f; + // for (size_t i = 0; i < 100; i += 1) { + // float x = rand() / (float)RAND_MAX * 1000.0f; + + float x = (float)(0x47ca9334); + float16_t x_compressed = NN_floatToHalf(x); float x_decompressed = NN_halfToFloat(x_compressed); @@ -55,7 +58,10 @@ int main() { print_bits_half(x_compressed); print_bits(x_decompressed); + printf("%f\t", x); + printf("%f\n", x_decompressed); + compareResult(x, x_decompressed); - } + // } return 0; } \ No newline at end of file diff --git a/test/src/main_avx.c b/tests/src/main_avx.c similarity index 100% rename from test/src/main_avx.c rename to tests/src/main_avx.c diff --git a/test/src/main_matlib.c b/tests/src/main_matlib.c similarity index 100% rename from test/src/main_matlib.c rename to tests/src/main_matlib.c diff --git a/test/test.py b/tests/test.py similarity index 100% rename from test/test.py rename to tests/test.py