Skip to content

Commit

Permalink
add cvimath
Browse files Browse the repository at this point in the history
commit ce8705f49da5e5f59c2ddb3253ef88323a0cd9c4
Author: sophgo-forum-service <[email protected]>
Date:   Mon May 13 14:04:10 2024 +0800

    [feat] cvimath opensource for cv18xx soc.

    - 9e8967
  • Loading branch information
carbonfix committed May 31, 2024
1 parent e25f20f commit 83dc491
Show file tree
Hide file tree
Showing 55 changed files with 18,671 additions and 0 deletions.
1 change: 1 addition & 0 deletions .version/2024-05-31.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@
| cvibuilder | cvibuilder | https://github.com/sophgo/cvibuilder.git | sg200x-dev | 4309f2a |
| cvikernel | cvikernel | https://github.com/sophgo/cvikernel.git | sg200x-dev | 9f1f57a |
| cviruntime | cviruntime | https://github.com/sophgo/cviruntime.git | sg200x-dev | 3f49386 |
| cvimath | cvimath | https://github.com/sophgo/cvimath.git | sg200x-dev | ce8705f |
108 changes: 108 additions & 0 deletions cvimath/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
---
Language: Cpp
# BasedOnStyle: Google
AccessModifierOffset: -1
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Left
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: true
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 100
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: true
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeCategories:
- Regex: '^<.*\.h>'
Priority: 1
- Regex: '^<.*'
Priority: 2
- Regex: '.*'
Priority: 3
IncludeIsMainRegex: '([-_](test|unittest))?$'
IndentCaseLabels: true
IndentWidth: 2
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: false
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Left
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Auto
TabWidth: 8
UseTab: Never
...

5 changes: 5 additions & 0 deletions cvimath/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.vscode
build
install


85 changes: 85 additions & 0 deletions cvimath/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
project(cvimath)

cmake_minimum_required(VERSION 3.2.2)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS ON)
#set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
#set(CMAKE_INSTALL_RPATH "\${ORIGIN}/../lib;\${ORIGIN}/")

if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
set(CMAKE_BUILD_TYPE "Release")
endif()

if("${CMAKE_TOOLCHAIN_FILE}" STREQUAL "")
message("No toolchain file found. Using host compiler.")
if ("${CMAKE_INSTALL_PREFIX}" STREQUAL "/usr/local")
set(CMAKE_INSTALL_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}/install")
endif()
else()
if ("${CMAKE_INSTALL_PREFIX}" STREQUAL "/usr/local")
set(CMAKE_INSTALL_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}/install_soc")
endif()
endif()

set(CMAKE_C_INIT "-fsigned-char -fPIC -Werror=all -fdiagnostics-color=always")
set(CMAKE_CXX_INIT "-fsigned-char -fPIC -Werror=all -fdiagnostics-color=always -std=gnu++11")
if("${CMAKE_BUILD_TYPE}" STREQUAL "Release" OR "${CMAKE_BUILD_TYPE}" STREQUAL "RELEASE")
set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_C_INIT} -O3" )
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_INIT} -O3" )
elseif("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
set( SAFETY_FLAGS "-Werror -Wall -Wextra -ggdb -fno-strict-aliasing")
set( SAFETY_FLAGS "${SAFETY_FLAGS} -fsanitize=address")
set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_C_INIT} -g -O0 ${SAFETY_FLAGS}")
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_INIT} -g -O0 ${SAFETY_FLAGS}" )
else()
message(FATAL_ERROR "No build type!!!")
endif()

message("==================================================")
message("[Summary]")
message("C compiler ${CMAKE_C_COMPILER}")
message("CXX compiler ${CMAKE_CXX_COMPILER}")
message("Build type ${CMAKE_BUILD_TYPE}")
message("Install dir ${CMAKE_INSTALL_PREFIX}")
message("==================================================")

# Add externel libs
set( TPU_LD "-L${TPU_SDK_ROOT}/lib")
set( TPU_KERNEL_LIB "${TPU_LD} -lcvikernel")
# wait cvimath/cviruntime so are generated
set( TEST_LIBS cvimath cviruntime)

# Add include path and set tpu libraries.
include_directories(
${TPU_SDK_ROOT}/include
${CVI_EXTRA}/include
"${CMAKE_CURRENT_SOURCE_DIR}/include")

# https://stackoverflow.com/questions/30250494/ctest-not-detecting-tests
enable_testing()

# ctest config
if (NOT CMAKE_CROSSCOMPILING)
if (ENABLE_TEST STREQUAL "ON")
add_subdirectory(tests)
endif()
endif()

add_subdirectory(src)
add_subdirectory(sample)

# export header
file(GLOB HEADERS
include/cvimath.h
include/cvimath_internal.h
include/test_cvikernel_util.h
)

# export sample
#file(GLOB SAMPLES sample/*)

#install(FILES ${SAMPLES} DESTINATION samples/cvimath)
install(FILES ${CMAKE_SOURCE_DIR}/toolchain/toolchain-aarch64-linux.cmake DESTINATION samples/cvimath)
install(FILES ${HEADERS} DESTINATION include/cvimath)
21 changes: 21 additions & 0 deletions cvimath/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# CviMath

## How to build

### Requirements

1. MLIR SDK

SOC mode

```
$ mkdir build
$ cd build
$ cmake -G Ninja .. -DCMAKE_BUILD_WITH_INSTALL_RPATH=ON \
-DTOOLCHAIN_ROOT_DIR=${PWD}/../../gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu \
-DCMAKE_TOOLCHAIN_FILE=${PWD}/../toolchain/toolchain-aarch64-linux.cmake \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX= \
-DTPU_SDK_ROOT=
$ ninja -j8 && ninja install
```
8 changes: 8 additions & 0 deletions cvimath/clang-format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
CLANG_ROOT=$(readlink -f $SCRIPT_DIR)

find $CLANG_ROOT/include -regex '.*\.\(cpp\|h\|hpp\|cc\|c\|cxx\|inc\)' -exec clang-format -i {} \;
find $CLANG_ROOT/src -regex '.*\.\(cpp\|h\|hpp\|cc\|c\|cxx\|inc\)' -exec clang-format -i {} \;
find $CLANG_ROOT/tests -regex '.*\.\(cpp\|h\|hpp\|cc\|c\|cxx\|inc\)' -exec clang-format -i {} \;
find $CLANG_ROOT/sample -regex '.*\.\(cpp\|h\|hpp\|cc\|c\|cxx\|inc\)' -exec clang-format -i {} \;
84 changes: 84 additions & 0 deletions cvimath/include/cvimath.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#ifndef CVIMATH_H
#define CVIMATH_H

#include <stdint.h>

// public function
#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief This function calculated the unit length of a precahed i8 feature array
*
* @param precached Prefetched feature array in 1-D. Format: feature1, feature2, ...
* @param unit_precached_arr Output unit length.
* @param data_length The length of the feature.
* @param data_num The number of features.
*/
void cvm_gen_precached_i8_unit_length(int8_t *precached, float *unit_precached_arr,
const uint32_t data_length, const uint32_t data_num);

/**
* @brief This function calculated the unit length of a precahed u8 feature array
*
* @param precached Prefetched feature array in 1-D. Format: feature1, feature2, ...
* @param unit_precached_arr Output unit length.
* @param data_length The length of the feature.
* @param data_num The number of features.
*/
void cvm_gen_precached_u8_unit_length(uint8_t *precached, float *unit_precached_arr,
const uint32_t data_length, const uint32_t data_num);

/**
* @brief Do inner product matching on i8 feature with given precached feature array.
*
* @param feature The input i8 feature to be compared.
* @param precached The precached feature array in 1-D.
* @param unit_precached_arr The unit length array of the precached.
* @param k_index The output matching index result in order.
* @param k_value The output matching value result in order.
* @param buffer The buffer used by this function, same length as precached.
* @param data_length The length of the single feature.
* @param data_num The number of features of the feature array.
* @param k Top k results, affects the length of k_index and k_value.
*/
void cvm_cpu_i8data_ip_match(int8_t *feature, int8_t *precached, float *unit_precached_arr,
uint32_t *k_index, float *k_value, float *buffer,
const uint32_t data_length, const uint32_t data_num, const uint32_t k);

/**
* @brief Do inner product matching on u8 feature with given precached feature array.
*
* @param feature The input u8 feature to be compared.
* @param precached The precached feature array in 1-D.
* @param unit_precached_arr The unit length array of the precached.
* @param k_index The output matching index result in order.
* @param k_value The output matching value result in order.
* @param buffer The buffer used by this function, same length as precached.
* @param data_length The length of the single feature.
* @param data_num The number of features of the feature array.
* @param k Top k results, affects the length of k_index and k_value.
*/
void cvm_cpu_u8data_ip_match(uint8_t *feature, uint8_t *precached, float *unit_precached_arr,
uint32_t *k_index, float *k_value, float *buffer,
const uint32_t data_length, const uint32_t data_num, const uint32_t k);

// Legacy support for hj.
inline void __attribute__((always_inline))
cvm_gen_db_i8_unit_length(int8_t *precached, float *unit_precached_arr, const uint32_t data_length,
const uint32_t data_num) {
cvm_gen_precached_i8_unit_length(precached, unit_precached_arr, data_length, data_num);
}

inline void __attribute__((always_inline))
cvm_gen_db_unit_length(uint8_t *precached, float *unit_precached_arr, const uint32_t data_length,
const uint32_t data_num) {
cvm_gen_precached_u8_unit_length(precached, unit_precached_arr, data_length, data_num);
}

#ifdef __cplusplus
}
#endif

#endif // CVIMATH_H
Loading

0 comments on commit 83dc491

Please sign in to comment.