Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NNAPI env init #16

Merged
merged 38 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
b9e4983
feat: nnapi config referencing mnn
oreomaker Sep 20, 2023
71e3563
rename
oreomaker Sep 20, 2023
e9bb14d
Merge branch 'develop' into develop_zh
oreomaker Sep 23, 2023
30a7c8c
feat: init nnapi backend template
oreomaker Sep 23, 2023
5f3be27
feat: nnapi backend init
oreomaker Sep 25, 2023
2d42a98
feat: nnapi op gen shell
oreomaker Sep 25, 2023
ab42c2e
Merge branch 'develop' into develop_zh
oreomaker Sep 25, 2023
f90c3e5
style: change macro style
oreomaker Sep 25, 2023
66f90fa
Merge branch 'develop' into develop_zh
oreomaker Sep 27, 2023
74392fd
merge: merge branch 'develop' into develop-zh
oreomaker Oct 7, 2023
37c93c3
chore: add android 64 build shell
oreomaker Oct 9, 2023
7a3259b
chore: add nnapi compile definition
oreomaker Oct 9, 2023
2e187c0
chore: ndk toolchain refac
oreomaker Oct 9, 2023
027ab6e
merge: Merge branch 'develop' into develop-zh
oreomaker Oct 9, 2023
f497c17
chore: remove nnapi op gen shell
oreomaker Oct 9, 2023
f396284
feat: nnapi add op init
oreomaker Oct 12, 2023
8c3d908
refactor: add nnapi common op
oreomaker Oct 13, 2023
7d83369
feat: nnapi backend build scalar
oreomaker Oct 13, 2023
d64448c
refactor: op name in nnapi backend build operation
oreomaker Oct 14, 2023
7605286
feat: nnapi add
oreomaker Oct 14, 2023
65309f5
chore: nnapi build and test
oreomaker Oct 15, 2023
672f205
merge: mgerge branch 'develop' into develop-zh
oreomaker Oct 16, 2023
f2e2a72
fix: 更改nnapi函数签名
oreomaker Oct 16, 2023
c2afead
merge: Merge branch 'develop' into develop-zh
oreomaker Oct 17, 2023
a466c43
merge: Merge branch 'develop' into develop-zh
oreomaker Oct 19, 2023
cc169b7
chore: remove nnapi_test.cc
oreomaker Oct 19, 2023
f044851
Merge branch 'develop' into develop-zh
oreomaker Oct 21, 2023
f4ae75a
Merge branch 'develop' into develop-zh
oreomaker Oct 25, 2023
fd1686c
chore: nnapi test env init
oreomaker Oct 25, 2023
cae4b27
Merge branch 'develop' into develop-zh
oreomaker Oct 25, 2023
2f7bcc5
fix: std::setiosflags, quantizeq4 include
oreomaker Oct 25, 2023
7040def
chore: nnapi build script
oreomaker Oct 25, 2023
330a702
chore: add nnapi device exe script
oreomaker Oct 25, 2023
b9d4f6c
feat: nnapi net convert
oreomaker Oct 25, 2023
5917ade
chore: remove unused include
oreomaker Oct 25, 2023
61a1a52
style: code style in nnapi add
oreomaker Oct 25, 2023
03495a6
fix: nnapi symbol load
oreomaker Oct 25, 2023
fea45d2
Merge branch 'develop' into develop-zh
oreomaker Oct 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ FetchContent_Declare(
# find_program(CLANG_TIDY_EXE NAMES "clang-tidy" REQUIRED)
#set(CMAKE_CXX_CLANG_TIDY "clang-tidy")

option(DEBUG "debug print" OFF)
# backend options
option(NNAPI "Enable NNAPI" OFF)

if(CMAKE_HOST_UNIX)
message("current platform: Linux ")
# set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -std=c++14 -stdlib=libc++")
Expand Down Expand Up @@ -85,3 +89,13 @@ if (TEST)

endif ()

# nnapi temp test
if(NNAPI)
include(/Users/luis/Library/Android/sdk/ndk/25.1.8937393/build/cmake/android.toolchain.cmake)
add_definitions(-DNNAPI_ENABLED)
# TODO: better debug log
add_definitions(-DDEBUG)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/src/backends/nnapi)
add_executable(nnapi_test ${PROJECT_SOURCE_DIR}/demo/nnapi_test.cpp ${DIR_SRC_CPU} ${DIR_SRC_EXP} ${DIR_SRC} ${DIR_SRC_QUANT})
target_link_libraries(nnapi_test MLLM_CPU MLLM_NNAPI)
endif()
71 changes: 71 additions & 0 deletions demo/nnapi_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <iostream>
#include "Net.hpp"
#include "Executor.hpp"
#include "NetParameter.hpp"
#include "Types.hpp"
#include "express/Express.hpp"

using namespace mllm;

// For Visualization and Debug
void display(NetParameter *net) {
std::cout << "===NetParameter===" << std::endl;
for (auto *op : net->net_ops) {
std::cout << "===NetOP===" << std::endl;
std::cout << "op->name:" << op->name << std::endl;
std::cout << "op->type:" << op->type << std::endl;
std::cout << "op input" << op->in.size() << std::endl;
for (auto *input : op->in) {
std::cout << "==Input==\ninput.name:" << input->name << std::endl;
if (input->in != nullptr) {
std::cout << "input op:" << input->in->name << std::endl;
}
std::cout << "input in subgraph:" << (input->subgraph == net) << std::endl;
std::cout << std::endl;
}
std::cout << "op output" << op->out.size() << std::endl;
for (auto *output : op->out) {
std::cout << "output.name:" << output->name << std::endl;
std::cout << "output op:" << output->out.size() << std::endl;
if (!output->out.empty()) {
std::cout << "output op:" << output->out[0]->name << std::endl;
}
}
std::cout << std::endl;
}
}

void display(Context *c) {
for (auto sub : c->sub_param_) {
display(&sub);
}
}

void fullTensor(shared_ptr<Tensor> input_tensor, Net net, vector<int> shape, float value) {
input_tensor->setBackend(net.backends()[BackendType::MLLM_CPU]);
input_tensor->reshape(shape);
input_tensor->alloc();
input_tensor->fullData<float>(value);
}

int main() {
std::cout << "===NNAPI Test===" << std::endl;
Context *c = new Context();

auto *a = _Input(c);
auto *b = _Add(c, {a, a});

BackendConfig bn;
Net net(c->sub_param_, bn);
net.convert(BackendType::MLLM_NNAPI);
display(c);

Executor ex(&net);
shared_ptr<Tensor> input = std::make_shared<Tensor>();
fullTensor(input, net, {1, 1, 1, 1}, 1);
ex.execute(input);

auto result = ex.result();
result[0]->printData<float>();
return 0;
}
15 changes: 15 additions & 0 deletions project/android/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# mllm android project

## How to build

```shell
export ANDROID_NDK=/path/to/ndk
cd bulid_64
sh ../build.sh
```

## How to run

```shell
sh ../device_exe.sh
```
13 changes: 13 additions & 0 deletions project/android/build_64.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
cmake ../../../ \
-DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \
-DCMAKE_BUILD_TYPE=Release \
-DANDROID_ABI="arm64-v8a" \
-DANDROID_STL=c++_static \
-DANDROID_NATIVE_API_LEVEL=android-27 \
-DNATIVE_LIBRARY_OUTPUT=. -DNATIVE_INCLUDE_OUTPUT=. $1 $2 $3 \
-DNNAPI=ON \
-DDEBUG=ON \
-DTEST=OFF \

make -j4
9 changes: 9 additions & 0 deletions project/android/device_exe.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
# NOTE: just for dev convinience
adb push ../bin/nnapi_test /data/local/tmp
# if push failed, exit
if [ $? -ne 0 ]; then
echo "adb push failed"
exit 1
fi
adb shell ./data/local/tmp/nnapi_test
20 changes: 19 additions & 1 deletion src/Net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
#include "MemoryManager.hpp"
#include "Op.hpp"
#include "Types.hpp"
#include "backends/cpu/CPUAdd.hpp"
#include "backends/cpu/CPUBackend.hpp"
#ifdef NNAPI_ENABLED
#include "backends/nnapi/NNAPIBackend.hpp"
#endif
#include <map>
#include <vector>

namespace mllm {
Net::Net(const vector<NetParameter> &param, BackendConfig config) :
net_param_(param), config_(config) {
Expand All @@ -19,6 +22,9 @@ Net::Net(const vector<NetParameter> &param, BackendConfig config) :
break;
}
backends_.emplace(BackendType::MLLM_CPU, new CPUBackend(mm));
#ifdef NNAPI_ENABLED
backends_.emplace(BackendType::MLLM_NNAPI, new NNAPIBackend(mm));
#endif

auto *in_tensor = net_param_[0].net_tensors[0];
tensors_[in_tensor->name] = std::make_shared<Tensor>(backends_[BackendType::MLLM_CPU]);
Expand Down Expand Up @@ -59,6 +65,18 @@ void Net::convert() {
subGraphs_["G" + std::to_string(i)] = subg_1;
}
}

void Net::convert(BackendType backend_type) {
for (int i = 0; i < (int)net_param_.size(); ++i) {
auto &sub_param = net_param_[i];
sub_param.topologySort();
shared_ptr<Graph> subg_1;
subg_1.reset(new Graph(sub_param, backends_[backend_type], tensors_));
subGraphs_["G" + std::to_string(i)] = subg_1;
}
printf("Net convert\n");
}

void Net::reshapeInput() {
auto *in_tensor = net_param_[0].net_tensors[0];
tensors_[in_tensor->name]->reshape(in_tensor->shape[0], in_tensor->shape[1], in_tensor->shape[2], in_tensor->shape[3]);
Expand Down
3 changes: 3 additions & 0 deletions src/Net.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class Net {
virtual ~Net() = default;

void convert();
// TODO: remove
// convert all subgraph to specified backend, just for develop
void convert(BackendType backend_type);

void reshapeInput();
void reshapeInput(vector<int> shape);
Expand Down
21 changes: 21 additions & 0 deletions src/backends/nnapi/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# NNAPI Backend
file(GLOB MLLM_NNAPI_SRC
${CMAKE_CURRENT_LIST_DIR}/*.cpp
${CMAKE_CURRENT_LIST_DIR}/*.hpp
${CMAKE_CURRENT_LIST_DIR}/*.h
)

# import android ndk cmake toolchain
include(/Users/luis/Library/Android/sdk/ndk/25.1.8937393/build/cmake/android.toolchain.cmake)

add_library(
MLLM_NNAPI
OBJECT
${MLLM_NNAPI_SRC}
)

target_include_directories(
MLLM_NNAPI
PRIVATE
${CMAKE_CURRENT_LIST_DIR}
)
38 changes: 38 additions & 0 deletions src/backends/nnapi/NNAPIAdd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "NNAPIAdd.hpp"
#include "NNAPICommonOp.hpp"
#include "Types.hpp"

namespace mllm {

NNAPIAdd::NNAPIAdd(Backend *bn) :
NNAPICommonOp(bn) {
}

ErrorCode NNAPIAdd::reshape(vector<shared_ptr<Tensor>> inputs, vector<shared_ptr<Tensor>> outputs) {
std::cout << "NNAPIAdd reshape" << std::endl;
CHECK_EQ(inputs.size(), 2);
CHECK_EQ(outputs.size(), 1);
CHECK_EQ(inputs[0]->shape(0), inputs[1]->shape(0));
CHECK_EQ(inputs[0]->shape(1), inputs[1]->shape(1));
CHECK_EQ(inputs[0]->shape(2), inputs[1]->shape(2));
CHECK_EQ(inputs[0]->shape(3), inputs[1]->shape(3));

outputs[0]->reshape(inputs[0]->shape(0), inputs[0]->shape(1), inputs[0]->shape(2), inputs[0]->shape(3));

auto inputIdxs = getTensorIdxs(inputs);
inputIdxs.push_back(buildScalar(ANEURALNETWORKS_FUSED_NONE));
return buildOperation(ANEURALNETWORKS_ADD, inputIdxs, getTensorIdxs(outputs));
}

ErrorCode NNAPIAdd::setUp(vector<shared_ptr<Tensor>> inputs, vector<shared_ptr<Tensor>> outputs) {
std::cout << "NNAPIAdd setUp" << std::endl;
// TODO: bulid nnapi operation
buildOperation(ANEURALNETWORKS_ADD, getTensorIdxs(inputs), getTensorIdxs(outputs));
return NO_ERROR;
}

ErrorCode NNAPIAdd::execute(vector<shared_ptr<Tensor>> inputs, vector<shared_ptr<Tensor>> outputs) {
std::cout << "NNAPIAdd execute do nothing" << std::endl;
return NO_ERROR;
}
} // namespace mllm
27 changes: 27 additions & 0 deletions src/backends/nnapi/NNAPIAdd.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef MLLM_NNAPIADD_H
#define MLLM_NNAPIADD_H

#include "NNAPICommonOp.hpp"
#include "NNAPIBackend.hpp"

namespace mllm {

class NNAPIAdd final : public NNAPICommonOp {
public:
NNAPIAdd(Backend *bn);
virtual ~NNAPIAdd() = default;
virtual ErrorCode reshape(vector<shared_ptr<Tensor>> inputs, vector<shared_ptr<Tensor>> outputs) override;
virtual ErrorCode setUp(vector<shared_ptr<Tensor>> inputs, vector<shared_ptr<Tensor>> outputs) override;
virtual ErrorCode execute(vector<shared_ptr<Tensor>> inputs, vector<shared_ptr<Tensor>> outputs) override;
};

class NNAPIAddCreator : public NNAPIBackend::Creator {
public:
virtual Op *create(OpParam op_param, Backend *bn) const {
return new NNAPIAdd(bn);
}
};

} // namespace mllm

#endif // MLLM_NNAPIADD_H
Loading
Loading