add source file

This commit is contained in:
Neucrack
2024-04-25 16:22:02 +08:00
parent ee323a029b
commit fdb6c6e927
1633 changed files with 386243 additions and 0 deletions

9
test/cvi_tpu_demo/.gitignore vendored Normal file
View File

@@ -0,0 +1,9 @@
build
dist
.config.mk
.flash.conf.json
data
/CMakeLists.txt
__pycache__

View File

@@ -0,0 +1,5 @@
Hello World Project based on MaixCDK
====
Hello world example code for MaixCDK of Sipeed, build method please visit [MaixCDK](https://github.com/sipeed/MaixCDK).

View File

@@ -0,0 +1,74 @@
############### Add include ###################
list(APPEND ADD_INCLUDE "include"
)
list(APPEND ADD_PRIVATE_INCLUDE "")
###############################################
############ Add source files #################
# list(APPEND ADD_SRCS "src/main.c"
# "src/test.c"
# )
append_srcs_dir(ADD_SRCS "src") # append source file in src dir to var ADD_SRCS
# list(REMOVE_ITEM COMPONENT_SRCS "src/test2.c")
# FILE(GLOB_RECURSE EXTRA_SRC "src/*.c")
# FILE(GLOB EXTRA_SRC "src/*.c")
# list(APPEND ADD_SRCS ${EXTRA_SRC})
# aux_source_directory(src ADD_SRCS) # collect all source file in src dir, will set var ADD_SRCS
# append_srcs_dir(ADD_SRCS "src") # append source file in src dir to var ADD_SRCS
# list(REMOVE_ITEM COMPONENT_SRCS "src/test.c")
# set(ADD_ASM_SRCS "src/asm.S")
# list(APPEND ADD_SRCS ${ADD_ASM_SRCS})
# SET_PROPERTY(SOURCE ${ADD_ASM_SRCS} PROPERTY LANGUAGE C) # set .S ASM file as C language
# SET_SOURCE_FILES_PROPERTIES(${ADD_ASM_SRCS} PROPERTIES COMPILE_FLAGS "-x assembler-with-cpp -D BBBBB")
###############################################
###### Add required/dependent components ######
list(APPEND ADD_REQUIREMENTS basic cvi_tpu opencv)
###############################################
###### Add link search path for requirements/libs ######
# list(APPEND ADD_LINK_SEARCH_PATH "${CONFIG_TOOLCHAIN_PATH}/lib")
# list(APPEND ADD_REQUIREMENTS pthread m) # add system libs, pthread and math lib for example here
# set (OpenCV_DIR opencv/lib/cmake/opencv4)
# find_package(OpenCV REQUIRED)
###############################################
############ Add static libs ##################
# list(APPEND ADD_STATIC_LIB "lib/libtest.a")
###############################################
#### Add compile option for this component ####
#### Just for this component, won't affect other
#### modules, including component that depend
#### on this component
# list(APPEND ADD_DEFINITIONS_PRIVATE -DAAAAA=1)
#### Add compile option for this component
#### and components denpend on this component
# list(APPEND ADD_DEFINITIONS -DAAAAA222=1
# -DAAAAA333=1)
###############################################
############ Add static libs ##################
#### Update parent's variables like CMAKE_C_LINK_FLAGS
# set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,--start-group libmaix/libtest.a -ltest2 -Wl,--end-group" PARENT_SCOPE)
###############################################
######### Add files need to download #########
# list(APPEND ADD_FILE_DOWNLOADS "{
# 'url': 'https://*****/abcde.tar.xz',
# 'urls': [], # backup urls, if url failed, will try urls
# 'sites': [], # download site, user can manually download file and put it into dl_path
# 'sha256sum': '',
# 'filename': 'abcde.tar.xz',
# 'path': 'toolchains/xxxxx',
# 'check_files': []
# }"
# )
#
# then extracted file in ${DL_EXTRACTED_PATH}/toolchains/xxxxx,
# you can directly use then, for example use it in add_custom_command
##############################################
# register component, DYNAMIC or SHARED flags will make component compiled to dynamic(shared) lib
register_component()

View File

View File

@@ -0,0 +1,3 @@
#pragma once

View File

@@ -0,0 +1,146 @@
#include "maix_basic.hpp"
#include "main.h"
using namespace maix;
#include <stdio.h>
#include <fstream>
#include <string>
#include <numeric>
#include <cviruntime.h>
#include <opencv2/opencv.hpp>
#define IMG_RESIZE_DIMS 256,256
#define BGR_MEAN 103.94,116.78,123.68
#define INPUT_SCALE 0.017
static void usage(char **argv) {
printf("Usage:\n");
printf(" %s cvimodel image.jpg label_file\n", argv[0]);
}
int _main(int argc, char **argv) {
if (argc != 4) {
usage(argv);
exit(-1);
}
// load model file
const char *model_file = argv[1];
CVI_MODEL_HANDLE model = nullptr;
int ret = CVI_NN_RegisterModel(model_file, &model);
if (CVI_RC_SUCCESS != ret) {
printf("CVI_NN_RegisterModel failed, err %d\n", ret);
exit(1);
}
printf("CVI_NN_RegisterModel succeeded\n");
// get input output tensors
CVI_TENSOR *input_tensors;
CVI_TENSOR *output_tensors;
int32_t input_num;
int32_t output_num;
CVI_NN_GetInputOutputTensors(model, &input_tensors, &input_num, &output_tensors,
&output_num);
CVI_TENSOR *input = CVI_NN_GetTensorByName(CVI_NN_DEFAULT_TENSOR, input_tensors, input_num);
assert(input);
printf("input, name:%s\n", input->name);
CVI_TENSOR *output = CVI_NN_GetTensorByName(CVI_NN_DEFAULT_TENSOR, output_tensors, output_num);
assert(output);
float qscale = CVI_NN_TensorQuantScale(input);
printf("qscale:%f\n", qscale);
CVI_SHAPE shape = CVI_NN_TensorShape(input);
// nchw
int32_t height = shape.dim[2];
int32_t width = shape.dim[3];
// imread
cv::Mat image;
image = cv::imread(argv[2]);
if (!image.data) {
printf("Could not open or find the image\n");
return -1;
}
// resize
cv::resize(image, image, cv::Size(IMG_RESIZE_DIMS)); // linear is default
// crop
cv::Size size = cv::Size(height, width);
cv::Rect crop(cv::Point(0.5 * (image.cols - size.width),
0.5 * (image.rows - size.height)), size);
image = image(crop);
// split
cv::Mat channels[3];
for (int i = 0; i < 3; i++) {
channels[i] = cv::Mat(height, width, CV_8SC1);
}
cv::split(image, channels);
// normalize
float mean[] = {BGR_MEAN};
for (int i = 0; i < 3; ++i) {
channels[i].convertTo(channels[i], CV_8SC1, INPUT_SCALE * qscale,
-1 * mean[i] * INPUT_SCALE * qscale);
}
// fill to input tensor
int8_t *ptr = (int8_t *)CVI_NN_TensorPtr(input);
int channel_size = height * width;
for (int i = 0; i < 3; ++i) {
memcpy(ptr + i * channel_size, channels[i].data, channel_size);
}
// run inference
CVI_NN_Forward(model, input_tensors, input_num, output_tensors, output_num);
printf("CVI_NN_Forward succeeded\n");
// output result
std::vector<std::string> labels;
std::ifstream file(argv[3]);
if (!file) {
printf("Didn't find synset_words file\n");
exit(1);
} else {
std::string line;
while (std::getline(file, line)) {
labels.push_back(std::string(line));
}
}
int32_t top_num = 5;
float *prob = (float *)CVI_NN_TensorPtr(output);
int32_t count = CVI_NN_TensorCount(output);
// find top-k prob and cls
std::vector<size_t> idx(count);
std::iota(idx.begin(), idx.end(), 0);
std::sort(idx.begin(), idx.end(), [&prob](size_t idx_0, size_t idx_1) {return prob[idx_0] > prob[idx_1];});
// show results.
printf("------\n");
for (size_t i = 0; i < top_num; i++) {
int top_k_idx = idx[i];
printf(" %f, idx %d", prob[top_k_idx], top_k_idx);
if (!labels.empty())
printf(", %s", labels[top_k_idx].c_str());
printf("\n");
}
printf("------\n");
CVI_NN_CleanupModel(model);
printf("CVI_NN_CleanupModel succeeded\n");
return 0;
}
int main(int argc, char* argv[])
{
// Catch SIGINT signal(e.g. Ctrl + C), and set exit flag to true.
signal(SIGINT, [](int sig){ app::set_exit_flag(true); });
// Use CATCH_EXCEPTION_RUN_RETURN to catch exception,
// if we don't catch exception, when program throw exception, the objects will not be destructed.
// So we catch exception here to let resources be released(call objects' destructor) before exit.
CATCH_EXCEPTION_RUN_RETURN(_main, -1, argc, argv);
}

9
test/cvi_tpu_matmul/.gitignore vendored Normal file
View File

@@ -0,0 +1,9 @@
build
dist
.config.mk
.flash.conf.json
data
/CMakeLists.txt
__pycache__

View File

@@ -0,0 +1,5 @@
Hello World Project based on MaixCDK
====
Hello world example code for MaixCDK of Sipeed, build method please visit [MaixCDK](https://github.com/sipeed/MaixCDK).

View File

@@ -0,0 +1,74 @@
############### Add include ###################
list(APPEND ADD_INCLUDE "include"
)
list(APPEND ADD_PRIVATE_INCLUDE "")
###############################################
############ Add source files #################
# list(APPEND ADD_SRCS "src/main.c"
# "src/test.c"
# )
append_srcs_dir(ADD_SRCS "src") # append source file in src dir to var ADD_SRCS
# list(REMOVE_ITEM COMPONENT_SRCS "src/test2.c")
# FILE(GLOB_RECURSE EXTRA_SRC "src/*.c")
# FILE(GLOB EXTRA_SRC "src/*.c")
# list(APPEND ADD_SRCS ${EXTRA_SRC})
# aux_source_directory(src ADD_SRCS) # collect all source file in src dir, will set var ADD_SRCS
# append_srcs_dir(ADD_SRCS "src") # append source file in src dir to var ADD_SRCS
# list(REMOVE_ITEM COMPONENT_SRCS "src/test.c")
# set(ADD_ASM_SRCS "src/asm.S")
# list(APPEND ADD_SRCS ${ADD_ASM_SRCS})
# SET_PROPERTY(SOURCE ${ADD_ASM_SRCS} PROPERTY LANGUAGE C) # set .S ASM file as C language
# SET_SOURCE_FILES_PROPERTIES(${ADD_ASM_SRCS} PROPERTIES COMPILE_FLAGS "-x assembler-with-cpp -D BBBBB")
###############################################
###### Add required/dependent components ######
list(APPEND ADD_REQUIREMENTS basic cvi_tpu pthread)
###############################################
###### Add link search path for requirements/libs ######
# list(APPEND ADD_LINK_SEARCH_PATH "${CONFIG_TOOLCHAIN_PATH}/lib")
# list(APPEND ADD_REQUIREMENTS pthread m) # add system libs, pthread and math lib for example here
# set (OpenCV_DIR opencv/lib/cmake/opencv4)
# find_package(OpenCV REQUIRED)
###############################################
############ Add static libs ##################
# list(APPEND ADD_STATIC_LIB "lib/libtest.a")
###############################################
#### Add compile option for this component ####
#### Just for this component, won't affect other
#### modules, including component that depend
#### on this component
# list(APPEND ADD_DEFINITIONS_PRIVATE -DAAAAA=1)
#### Add compile option for this component
#### and components denpend on this component
# list(APPEND ADD_DEFINITIONS -DAAAAA222=1
# -DAAAAA333=1)
###############################################
############ Add static libs ##################
#### Update parent's variables like CMAKE_C_LINK_FLAGS
# set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,--start-group libmaix/libtest.a -ltest2 -Wl,--end-group" PARENT_SCOPE)
###############################################
######### Add files need to download #########
# list(APPEND ADD_FILE_DOWNLOADS "{
# 'url': 'https://*****/abcde.tar.xz',
# 'urls': [], # backup urls, if url failed, will try urls
# 'sites': [], # download site, user can manually download file and put it into dl_path
# 'sha256sum': '',
# 'filename': 'abcde.tar.xz',
# 'path': 'toolchains/xxxxx',
# 'check_files': []
# }"
# )
#
# then extracted file in ${DL_EXTRACTED_PATH}/toolchains/xxxxx,
# you can directly use then, for example use it in add_custom_command
##############################################
# register component, DYNAMIC or SHARED flags will make component compiled to dynamic(shared) lib
register_component()

View File

View File

@@ -0,0 +1,3 @@
#pragma once

View File

@@ -0,0 +1,136 @@
#include "maix_basic.hpp"
#include "main.h"
using namespace maix;
#include <iostream>
#include <random>
#include <functional>
#include <chrono>
#include <algorithm>
#include <cmath>
#include <numeric>
#include <vector>
#include "cviruntime.h"
#include "cviruntime_extra.h"
using system_clock = std::chrono::system_clock;
using duration = std::chrono::duration<double, std::milli>;
static void matmul(int8_t *left, int8_t *right, int32_t *res,
int32_t m, int32_t k, int32_t n) {
for (auto _m = 0; _m < m; ++_m) {
for (auto _n = 0; _n < n; ++_n) {
int yi = _m * n + _n;
res[yi] = 0;
for (auto _k = 0; _k < k; ++_k) {
auto li = _m * k + _k;
auto ri = _k * n + _n;
res[yi] += left[li] * right[ri];
}
}
}
}
int _main(int argc, char **argv) {
if (argc < 4)
return 1;
uint32_t m = std::atoi(argv[1]);
uint32_t k = std::atoi(argv[2]);
uint32_t n = std::atoi(argv[3]);
std::cout << "L shape: (" << m << "x" << k << ")"
<< ", R shape: (" << n << "x" << k << ")"
<< "\n";
CVI_RT_HANDLE ctx;
CVI_RT_Init(&ctx);
CVI_RT_MEM mem_a = CVI_RT_MemAlloc(ctx, m * k);
CVI_RT_MEM mem_b = CVI_RT_MemAlloc(ctx, k * n);
CVI_RT_MEM mem_r = CVI_RT_MemAlloc(ctx, m * n * sizeof(int));
// create matmul kernel function
auto kfn = CVI_NN_PrepareMatrixMulKernelFunc(ctx, CVI_FMT_INT8, m, k, n);
auto vptr_a = (int8_t *)CVI_RT_MemGetVAddr(mem_a);
auto vptr_b = (int8_t *)CVI_RT_MemGetVAddr(mem_b);
std::random_device rd;
auto randint_gen = [&](int begin, int end) {
return std::bind(std::uniform_int_distribution<int>(begin, end),
std::default_random_engine(rd()));
};
auto random = randint_gen(-128, 127);
for (uint32_t i = 0; i < m * k; ++i) {
vptr_a[i] = random();
}
for (uint32_t i = 0; i < k * n; ++i) {
vptr_b[i] = random();
}
std::vector<int32_t> ref(m * n);
auto start0 = system_clock::now();
matmul(vptr_a, vptr_b, ref.data(), m, k, n);
auto end0 = system_clock::now();
duration d0 = end0 - start0;
std::cout << "soft run duration: " << d0.count() << "(ms)\n";
// flush cpu cache data of left & right matrixes.
CVI_RT_MemFlush(ctx, mem_a);
CVI_RT_MemFlush(ctx, mem_b);
// run matrix mul kernel function
auto start = system_clock::now();
CVI_NN_RunKernelFunc(kfn, 3,
CVI_RT_MemGetPAddr(mem_a),
CVI_RT_MemGetPAddr(mem_b),
CVI_RT_MemGetPAddr(mem_r));
CVI_RT_MemInvld(ctx, mem_r);
auto end = system_clock::now();
duration d = end - start;
std::cout << "hardware run duration: " << d.count() << "(ms)\n";
// get result and compare with reference
int32_t *vptr_r = (int32_t *)CVI_RT_MemGetVAddr(mem_r);
uint32_t err = 0;
for (uint32_t i = 0; i < m; ++i) {
for (uint32_t j = 0; j < n; ++j) {
uint32_t idx = i * n + j;
if (vptr_r[idx] != ref[idx]) {
std::cout << "compare fail, index: <" << i << "," << j << "> expect: "
<< ref[idx] << ", got: " << vptr_r[idx] << "\n";
err++;
}
}
}
CVI_RT_MemFree(ctx, mem_r);
CVI_RT_MemFree(ctx, mem_b);
CVI_RT_MemFree(ctx, mem_a);
CVI_NN_DestroyKernelFunc(kfn);
CVI_RT_DeInit(ctx);
if (err) {
return 1;
} else {
return 0;
}
}
int main(int argc, char* argv[])
{
// Catch SIGINT signal(e.g. Ctrl + C), and set exit flag to true.
signal(SIGINT, [](int sig){ app::set_exit_flag(true); });
// Use CATCH_EXCEPTION_RUN_RETURN to catch exception,
// if we don't catch exception, when program throw exception, the objects will not be destructed.
// So we catch exception here to let resources be released(call objects' destructor) before exit.
CATCH_EXCEPTION_RUN_RETURN(_main, -1, argc, argv);
}

29
test/test_projects/auto_test.sh Executable file
View File

@@ -0,0 +1,29 @@
#!/bin/bash
set -e
set -x
python -m pip install -r ../../requirements.txt
chmod +x test_cases.sh
echo "
-----------------------------------
--- ---
--- test for board linux ---
--- ---
-----------------------------------
"
./test_cases.sh linux 1
echo "-- test for board linux end\n\n"
# echo "
# -----------------------------------
# --- ---
# --- test for board m2dock ---
# --- ---
# -----------------------------------
# "
# ./test_cases.sh m2dock 0
# echo "-- test for board m2dock end\n\n"

View File

@@ -0,0 +1,56 @@
#!/bin/bash
set -e
set -x
platform=$1
need_run=$2
cd ../../examples
cd hello_world
maixcdk distclean
maixcdk build -p $platform
ls -al build/hello_world | awk '{print "\nDebug program file size: " $5 "B\n"}'
if [ "${need_run}x" == "1x" ]; then
maixcdk run
fi
maixcdk clean
maixcdk distclean
maixcdk build --release -p $platform
ls -al build/hello_world | awk '{print "\nRelease program file size: " $5 "B\n"}'
maixcdk distclean
cd ..
cd multi_threads
maixcdk distclean
maixcdk build -p $platform
ls -al build/multi_threads | awk '{print "\nDebug program file size: " $5 "B\n"}'
if [ "${need_run}x" == "1x" ]; then
maixcdk run
fi
maixcdk clean
maixcdk distclean
maixcdk build --release -p $platform
ls -al build/multi_threads | awk '{print "\nRelease program file size: " $5 "B\n"}'
maixcdk distclean
cd ..
# opencv_demo
cd opencv_demo
maixcdk distclean
maixcdk build -p $platform
ls -al build/opencv_demo | awk '{print "\nDebug program file size: " $5 "B\n"}'
if [ "${need_run}x" == "1x" ]; then
maixcdk run
fi
maixcdk clean
maixcdk distclean
maixcdk build --release -p $platform
ls -al build/opencv_demo | awk '{print "\nRelease program file size: " $5 "B\n"}'
maixcdk distclean
cd ..