Skip to content

Commit

Permalink
implement simplemath (#4905)
Browse files Browse the repository at this point in the history
* complete abs, fmod and sin function in simplemath.h

* remove some unused variables in simplemath.cpp

* modify test-coverage.yml and add some functions to simplemath.cpp

* modify erf.cpp which included math.h

* include platform.h for NCNN_SIMPLEMATH definition

* move utility constants and functions in simplemath.h to simplemath.cpp

* guard simplemath functions with extern "C"

* add NCNN_EXPORT macro in simplemath.h

* include plateform.h and guard all declarations with NCNN_SIMPLEMATH

* clean unused code in test_unaryop.cpp

* guard #include <vector> with NCNN_SIMPLEMATH in benchncnn.cpp

* add 'static' to guard functions that not declarated in header file

* modify sin and cos with better implementation

---------

Co-authored-by: HonestDeng <HonestDeng@users.noreply.github.com>
  • Loading branch information
HonestDeng and HonestDeng committed Oct 23, 2023
1 parent a6decad commit a1e3ebf
Show file tree
Hide file tree
Showing 146 changed files with 818 additions and 245 deletions.
8 changes: 8 additions & 0 deletions .ci/linux-x64-cpu-gcc.yml
Expand Up @@ -117,3 +117,11 @@ jobs:
cmake --build . -j $(nproc)
- name: test-simplestl-simpleomp
run: cd build-simplestl-simpleomp && ctest --output-on-failure -j $(nproc)
- name: build-simplestl-simplemath
run: |
mkdir build-simplestl-simplemath && cd build-simplestl-simplemath
cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/host-c.gcc.toolchain.cmake -DNCNN_STDIO=ON -DNCNN_STRING=ON -DNCNN_SIMPLESTL=ON -DNCNN_SIMPLEMATH=ON -DNCNN_BUILD_TESTS=ON -DNCNN_BUILD_BENCHMARK=OFF -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF ..
cmake --build . -j $(nproc)
- name: test-simplestl-simplemath
run: cd build-simplestl-simplemath && ctest --output-on-failure -j $(nproc)

44 changes: 44 additions & 0 deletions .ci/test-coverage.yml
Expand Up @@ -908,3 +908,47 @@ jobs:
lcov --list lcov.info
- name: codecov
run: ./codecov -t ${{settings.CODECOV_TOKEN.access_token}} -C ${{ ci.sha }} -B ${{ ci.head_ref }} -f build/lcov.info

linux-gcc-x64-simplemath:
name: linux-gcc-x64-simplemath

runs-on:
pool-name: docker
container:
image: bkci/ci:ubuntu
steps:
- name: checkout
checkout: self
with:
strategy: FRESH_CHECKOUT
enableSubmodule: false
enableGitLfs: false

- name: install-deps
run: |
apt-get update
apt-get install -y lcov
curl https://uploader.codecov.io/verification.gpg | gpg --no-default-keyring --keyring trustedkeys.gpg --import
curl -Os https://uploader.codecov.io/latest/linux/codecov
curl -Os https://uploader.codecov.io/latest/linux/codecov.SHA256SUM
curl -Os https://uploader.codecov.io/latest/linux/codecov.SHA256SUM.sig
gpgv codecov.SHA256SUM.sig codecov.SHA256SUM
shasum -a 256 -c codecov.SHA256SUM
chmod +x codecov
- name: build
run: |
mkdir build && cd build
cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/host-c.gcc.toolchain.cmake -DCMAKE_BUILD_TYPE=debug -DNCNN_COVERAGE=ON -DNCNN_STDIO=ON -DNCNN_STRING=ON -DNCNN_SIMPLESTL=ON -DNCNN_SIMPLEMATH=ON -DNCNN_BUILD_TESTS=ON -DNCNN_BUILD_BENCHMARK=OFF -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF ..
cmake --build . -j $(nproc)
- name: test
run: cd build && ctest --output-on-failure -j $(nproc)
- name: lcov-collect
run: |
cd build
lcov -d ./src -c -o lcov.info
lcov -r lcov.info '/usr/*' -o lcov.info
lcov -r lcov.info '*/build/*' -o lcov.info
lcov --list lcov.info
- name: codecov
run: ./codecov -t ${{settings.CODECOV_TOKEN.access_token}} -C ${{ ci.sha }} -B ${{ ci.head_ref }} -f build/lcov.info
11 changes: 11 additions & 0 deletions .github/workflows/linux-aarch64-cpu-gcc.yml
Expand Up @@ -86,6 +86,17 @@ jobs:
export PATH=$GITHUB_WORKSPACE/qemu-install/bin:$PATH
cd build-noint8
TESTS_EXECUTABLE_LOADER=qemu-aarch64 TESTS_EXECUTABLE_LOADER_ARGUMENTS="-L;/usr/aarch64-linux-gnu" ctest --output-on-failure -j 2
- name: build-simplestl-simplemath
run: |
mkdir build-simplestl-simplemath && cd build-simplestl-simplemath
cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/aarch64-linux-gnu-c.toolchain.cmake -DNCNN_STDIO=ON -DNCNN_STRING=ON -DNCNN_SIMPLESTL=ON -DNCNN_SIMPLEMATH=ON -DNCNN_BUILD_TESTS=ON -DNCNN_BUILD_BENCHMARK=OFF -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF ..
cmake --build . -j 2
- name: test-simplestl-simplemath
run: |
export PATH=$GITHUB_WORKSPACE/qemu-install/bin:$PATH
cd build-simplestl-simplemath
TESTS_EXECUTABLE_LOADER=qemu-aarch64 TESTS_EXECUTABLE_LOADER_ARGUMENTS="-L;/usr/aarch64-linux-gnu" ctest --output-on-failure -j 2
linux-gcc-arm82:
runs-on: ubuntu-20.04
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Expand Up @@ -56,6 +56,7 @@ option(NCNN_INSTALL_SDK "install ncnn library and headers" ON)
option(NCNN_SIMPLEOCV "minimal opencv structure emulation" OFF)
option(NCNN_SIMPLEOMP "minimal openmp runtime emulation" OFF)
option(NCNN_SIMPLESTL "minimal cpp stl structure emulation" OFF)
option(NCNN_SIMPLEMATH "minimal cmath" OFF)
option(NCNN_THREADS "build with threads" ON)
option(NCNN_BENCHMARK "print benchmark information for every layer" OFF)
option(NCNN_C_API "build with C api" ON)
Expand Down
3 changes: 3 additions & 0 deletions benchmark/benchncnn.cpp
Expand Up @@ -25,7 +25,10 @@
#include "datareader.h"
#include "net.h"
#include "gpu.h"

#ifndef NCNN_SIMPLESTL
#include <vector>
#endif

class DataReaderFromEmpty : public ncnn::DataReader
{
Expand Down
8 changes: 4 additions & 4 deletions src/CMakeLists.txt
Expand Up @@ -39,6 +39,7 @@ set(ncnn_SRCS
simpleocv.cpp
simpleomp.cpp
simplestl.cpp
simplemath.cpp
)

if(ANDROID)
Expand Down Expand Up @@ -207,7 +208,7 @@ if(NOT NCNN_SHARED_LIB)
set_target_properties(ncnn PROPERTIES COMPILE_FLAGS -DNCNN_STATIC_DEFINE)
endif()

if(NCNN_SIMPLESTL)
if(NCNN_SIMPLESTL AND NOT NCNN_SIMPLEMATH)
# link math lib explicitly
target_link_libraries(ncnn PUBLIC m)
endif()
Expand Down Expand Up @@ -260,7 +261,6 @@ if(NCNN_THREADS)
if(TARGET Threads::Threads)
target_link_libraries(ncnn PUBLIC Threads::Threads)
endif()

if(NCNN_SIMPLEOMP OR NCNN_SIMPLESTL)
target_link_libraries(ncnn PUBLIC pthread)
endif()
Expand Down Expand Up @@ -580,6 +580,7 @@ if(NCNN_INSTALL_SDK)
simpleocv.h
simpleomp.h
simplestl.h
simplemath.h
vulkan_header_fix.h
${CMAKE_CURRENT_BINARY_DIR}/ncnn_export.h
${CMAKE_CURRENT_BINARY_DIR}/layer_shader_type_enum.h
Expand All @@ -598,5 +599,4 @@ endif()
# add ncnn and generate-spirv to a virtual project group
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set_property(TARGET ncnn PROPERTY FOLDER "libncnn")
set_property(TARGET ncnn-generate-spirv PROPERTY FOLDER "libncnn")

set_property(TARGET ncnn-generate-spirv PROPERTY FOLDER "libncnn")
1 change: 0 additions & 1 deletion src/gpu.cpp
Expand Up @@ -16,7 +16,6 @@

#if NCNN_VULKAN

#include <math.h>
#include <string.h>
#include <vulkan/vulkan.h>

Expand Down
1 change: 0 additions & 1 deletion src/layer.cpp
Expand Up @@ -16,7 +16,6 @@

#include "cpu.h"

#include <math.h>
#include <string.h>

#ifdef _MSC_VER
Expand Down
2 changes: 0 additions & 2 deletions src/layer.h
Expand Up @@ -21,8 +21,6 @@
#include "paramdict.h"
#include "platform.h"

#include <math.h>

#if NCNN_VULKAN
#include "command.h"
#include "pipeline.h"
Expand Down
2 changes: 0 additions & 2 deletions src/layer/arm/binaryop_arm.cpp
Expand Up @@ -14,8 +14,6 @@

#include "binaryop_arm.h"

#include <math.h>

#if __ARM_NEON
#include <arm_neon.h>
#include "neon_mathfun.h"
Expand Down
2 changes: 0 additions & 2 deletions src/layer/arm/binaryop_arm_asimdhp.cpp
Expand Up @@ -14,8 +14,6 @@

#include "binaryop_arm.h"

#include <math.h>

#if __ARM_NEON
#include <arm_neon.h>
#include "neon_mathfun.h"
Expand Down
2 changes: 1 addition & 1 deletion src/layer/arm/cast_arm_bf16.cpp
Expand Up @@ -14,7 +14,7 @@

#include "cpu.h"
#include "mat.h"
#include <math.h>

namespace ncnn {

#include "cast_bf16.h"
Expand Down
2 changes: 0 additions & 2 deletions src/layer/arm/gelu_arm.cpp
Expand Up @@ -14,8 +14,6 @@

#include "gelu_arm.h"

#include <math.h>

#if __ARM_NEON
#include <arm_neon.h>
#include "neon_mathfun.h"
Expand Down
2 changes: 0 additions & 2 deletions src/layer/arm/gelu_arm_asimdhp.cpp
Expand Up @@ -14,8 +14,6 @@

#include "gelu_arm.h"

#include <math.h>

#if __ARM_NEON
#include <arm_neon.h>
#include "neon_mathfun.h"
Expand Down
2 changes: 0 additions & 2 deletions src/layer/arm/gru_arm.cpp
Expand Up @@ -14,8 +14,6 @@

#include "gru_arm.h"

#include <math.h>

#if __ARM_NEON
#include <arm_neon.h>
#endif // __ARM_NEON
Expand Down
2 changes: 0 additions & 2 deletions src/layer/arm/gru_arm_asimdhp.cpp
Expand Up @@ -14,8 +14,6 @@

#include "gru_arm.h"

#include <math.h>

#if __ARM_NEON
#include <arm_neon.h>
#endif // __ARM_NEON
Expand Down
2 changes: 0 additions & 2 deletions src/layer/arm/innerproduct_arm.h
Expand Up @@ -16,8 +16,6 @@
#define LAYER_INNERPRODUCT_ARM_H

#include "innerproduct.h"
#include <cmath>
#include <cstdlib>

namespace ncnn {

Expand Down
2 changes: 0 additions & 2 deletions src/layer/arm/interp_arm.cpp
Expand Up @@ -14,8 +14,6 @@

#include "interp_arm.h"

#include <math.h>

#if __ARM_NEON
#include <arm_neon.h>
#endif // __ARM_NEON
Expand Down
2 changes: 0 additions & 2 deletions src/layer/arm/interp_arm_asimdhp.cpp
Expand Up @@ -14,8 +14,6 @@

#include "interp_arm.h"

#include <math.h>

#if __ARM_NEON
#include <arm_neon.h>
#endif // __ARM_NEON
Expand Down
2 changes: 0 additions & 2 deletions src/layer/arm/lrn_arm.cpp
Expand Up @@ -14,8 +14,6 @@

#include "lrn_arm.h"

#include <math.h>

#if __ARM_NEON
#include <arm_neon.h>
#include "neon_mathfun.h"
Expand Down
2 changes: 0 additions & 2 deletions src/layer/arm/lstm_arm.cpp
Expand Up @@ -14,8 +14,6 @@

#include "lstm_arm.h"

#include <math.h>

#if __ARM_NEON
#include <arm_neon.h>
#endif // __ARM_NEON
Expand Down
2 changes: 0 additions & 2 deletions src/layer/arm/lstm_arm_asimdhp.cpp
Expand Up @@ -14,8 +14,6 @@

#include "lstm_arm.h"

#include <math.h>

#if __ARM_NEON
#include <arm_neon.h>
#endif // __ARM_NEON
Expand Down
2 changes: 0 additions & 2 deletions src/layer/arm/mish_arm.cpp
Expand Up @@ -14,8 +14,6 @@

#include "mish_arm.h"

#include <math.h>

#if __ARM_NEON
#include <arm_neon.h>
#include "neon_mathfun.h"
Expand Down
2 changes: 0 additions & 2 deletions src/layer/arm/mish_arm_asimdhp.cpp
Expand Up @@ -14,8 +14,6 @@

#include "mish_arm.h"

#include <math.h>

#if __ARM_NEON
#include <arm_neon.h>
#include "neon_mathfun.h"
Expand Down
2 changes: 0 additions & 2 deletions src/layer/arm/quantize_arm.cpp
Expand Up @@ -15,8 +15,6 @@

#include "quantize_arm.h"

#include <math.h>

#if __ARM_NEON
#include <arm_neon.h>
#endif // __ARM_NEON
Expand Down
2 changes: 0 additions & 2 deletions src/layer/arm/quantize_arm_asimdhp.cpp
Expand Up @@ -14,8 +14,6 @@

#include "quantize_arm.h"

#include <math.h>

#if __ARM_NEON
#include <arm_neon.h>
#endif // __ARM_NEON
Expand Down
2 changes: 0 additions & 2 deletions src/layer/arm/requantize_arm.cpp
Expand Up @@ -15,8 +15,6 @@

#include "requantize_arm.h"

#include <math.h>

#if __ARM_NEON
#include <arm_neon.h>
#endif // __ARM_NEON
Expand Down
2 changes: 0 additions & 2 deletions src/layer/arm/rnn_arm.cpp
Expand Up @@ -14,8 +14,6 @@

#include "rnn_arm.h"

#include <math.h>

#if __ARM_NEON
#include <arm_neon.h>
#endif // __ARM_NEON
Expand Down
2 changes: 0 additions & 2 deletions src/layer/arm/rnn_arm_asimdhp.cpp
Expand Up @@ -14,8 +14,6 @@

#include "rnn_arm.h"

#include <math.h>

#if __ARM_NEON
#include <arm_neon.h>
#endif // __ARM_NEON
Expand Down
2 changes: 0 additions & 2 deletions src/layer/arm/sigmoid_arm.cpp
Expand Up @@ -14,8 +14,6 @@

#include "sigmoid_arm.h"

#include <math.h>

#if __ARM_NEON
#include <arm_neon.h>
#include "neon_mathfun.h"
Expand Down
2 changes: 0 additions & 2 deletions src/layer/arm/sigmoid_arm_asimdhp.cpp
Expand Up @@ -14,8 +14,6 @@

#include "sigmoid_arm.h"

#include <math.h>

#if __ARM_NEON
#include <arm_neon.h>
#include "neon_mathfun.h"
Expand Down
1 change: 0 additions & 1 deletion src/layer/arm/softmax_arm.cpp
Expand Up @@ -15,7 +15,6 @@
#include "softmax_arm.h"

#include <float.h>
#include <math.h>

#if __ARM_NEON
#include <arm_neon.h>
Expand Down
1 change: 0 additions & 1 deletion src/layer/arm/softmax_arm_asimdhp.cpp
Expand Up @@ -15,7 +15,6 @@
#include "softmax_arm.h"

#include <float.h>
#include <math.h>

#if __ARM_NEON
#include <arm_neon.h>
Expand Down
2 changes: 0 additions & 2 deletions src/layer/arm/swish_arm.cpp
Expand Up @@ -14,8 +14,6 @@

#include "swish_arm.h"

#include <math.h>

#if __ARM_NEON
#include <arm_neon.h>
#include "neon_mathfun.h"
Expand Down
2 changes: 0 additions & 2 deletions src/layer/arm/swish_arm_asimdhp.cpp
Expand Up @@ -14,8 +14,6 @@

#include "swish_arm.h"

#include <math.h>

#if __ARM_NEON
#include <arm_neon.h>
#include "neon_mathfun.h"
Expand Down

0 comments on commit a1e3ebf

Please sign in to comment.