Update tim lite api (#48)

* Add lenet sample with TIM-LITE

A lenet sample with TIM-LITE executable.

Signed-off-by: zhao.xia <zhao.xia@verisilicon.com>

* Update TIM-LITE API

Update handle usage.
Use Execution::Trigger instead of Execution::Exec

Signed-off-by: zhao.xia <zhao.xia@verisilicon.com>

* Update lenet lite case to use new api

Signed-off-by: zhao.xia <zhao.xia@verisilicon.com>
This commit is contained in:
Nightingale 2021-05-17 22:08:10 +08:00 committed by GitHub
parent 66dd29703e
commit 90e451749f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 38544 additions and 79 deletions

View File

@ -22,9 +22,10 @@
*
*****************************************************************************/
#ifndef __TIM_LITE_EXECUTION_H__
#define __TIM_LITE_EXECUTION_H__
#ifndef TIM_LITE_EXECUTION_H_
#define TIM_LITE_EXECUTION_H_
#include <cstdint>
#include <vector>
#include <memory>
#include "tim/lite/handle.h"
@ -36,13 +37,9 @@ class Execution {
public:
static std::shared_ptr<Execution> Create(
const void* executable, size_t executable_size);
template <typename HandleType, typename... Params>
std::shared_ptr<HandleType> RegisterHandle(Params... parameters) {
return std::make_shared<HandleType>(parameters...);
};
virtual Execution& BindInputs(std::vector<std::shared_ptr<Handle>> handles) = 0;
virtual Execution& BindOutputs(std::vector<std::shared_ptr<Handle>> handles) = 0;
virtual bool Exec() = 0;
virtual Execution& BindInputs(const std::vector<std::shared_ptr<Handle>>& handles) = 0;
virtual Execution& BindOutputs(const std::vector<std::shared_ptr<Handle>>& handles) = 0;
virtual bool Trigger() = 0;
};
}

View File

@ -22,8 +22,8 @@
*
*****************************************************************************/
#ifndef __TIM_LITE_HANDLE_H__
#define __TIM_LITE_HANDLE_H__
#ifndef TIM_LITE_HANDLE_H_
#define TIM_LITE_HANDLE_H_
#include <memory>

13
samples/lenet_lite/BUILD Normal file
View File

@ -0,0 +1,13 @@
cc_test(
name = "lenet_lite_asymu8_cc",
copts = [
"-Werror", "-std=c++14"
],
srcs = [
"lenet_lite_asymu8.cc",
"lenet_lite_asymu8_executable.h"
],
deps = [
"//:tim-lite_interface"
],
)

View File

@ -0,0 +1,141 @@
/****************************************************************************
*
* Copyright (c) 2021 Vivante Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*****************************************************************************/
#include <algorithm>
#include <cstdint>
#include <tuple>
#include <iomanip>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include "tim/lite/execution.h"
#include "tim/lite/handle.h"
#include "lenet_lite_asymu8_executable.h"
std::vector<uint8_t> input_data = {
0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 2, 0, 0, 8, 0,
3, 0, 7, 0, 2, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 3, 1, 1, 0, 14, 0, 0, 3, 0,
2, 4, 0, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 4, 3, 0, 0, 0, 5, 0, 4, 0, 0,
0, 0, 10, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 6, 5, 0, 2, 0, 9, 0, 12, 2, 0, 5, 1, 0,
0, 2, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3, 0, 33, 0, 0, 155, 186, 55, 17, 22, 0, 0, 3, 9, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2, 0, 167, 253, 255, 235, 255, 240, 134, 36, 0, 6, 1, 4, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 6, 87,
240, 251, 254, 254, 237, 255, 252, 191, 27, 0, 0, 5, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 19, 226, 255, 235,
255, 255, 254, 242, 255, 255, 68, 12, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 4, 1, 58, 254, 255, 158, 0, 2,
47, 173, 253, 247, 255, 65, 4, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 162, 240, 248, 92, 8, 0, 13, 0,
88, 249, 244, 148, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 17, 64, 244, 255, 210, 0, 0, 1, 2, 0, 52, 223,
255, 223, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 144, 245, 255, 142, 0, 4, 9, 0, 6, 0, 37, 222, 226,
42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 73,
255, 243, 104, 0, 0, 0, 0, 11, 0, 0, 0, 235, 242, 101, 4,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 133, 245, 226,
12, 4, 15, 0, 0, 0, 0, 24, 0, 235, 246, 41, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 236, 245, 152, 0, 10,
0, 0, 0, 0, 6, 0, 28, 227, 239, 1, 6, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 227, 240, 53, 4, 0, 0, 24,
0, 1, 0, 8, 181, 249, 177, 0, 2, 0, 0, 0, 0, 4, 0,
6, 1, 5, 0, 0, 87, 246, 219, 14, 0, 0, 2, 0, 10, 7,
0, 134, 255, 249, 104, 4, 0, 0, 0, 0, 0, 8, 0, 3, 0,
0, 0, 4, 89, 255, 228, 0, 11, 0, 8, 14, 0, 0, 100, 250,
248, 236, 0, 0, 8, 0, 0, 0, 0, 5, 0, 2, 0, 0, 2,
6, 68, 250, 228, 6, 6, 0, 0, 1, 0, 140, 240, 253, 238, 51,
31, 0, 3, 0, 0, 0, 0, 0, 0, 5, 0, 0, 2, 0, 26,
215, 255, 119, 0, 21, 1, 40, 156, 233, 244, 239, 103, 0, 6, 6,
0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 225, 251,
240, 141, 118, 139, 222, 244, 255, 249, 112, 17, 0, 0, 8, 3, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 84, 245, 255, 247,
255, 249, 255, 255, 249, 132, 11, 0, 9, 3, 1, 1, 0, 0, 0,
0, 2, 0, 0, 1, 0, 0, 6, 1, 0, 166, 236, 255, 255, 248,
249, 248, 72, 0, 0, 16, 0, 16, 0, 4, 0, 0, 0, 0, 0,
0, 0, 6, 0, 0, 4, 0, 0, 20, 106, 126, 188, 190, 112, 28,
0, 21, 0, 1, 2, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0,
};
template <typename T>
static void printTopN(const T* prob, size_t outputCount, size_t topNum) {
std::vector<std::tuple<size_t, T>> data;
for (size_t i = 0; i < outputCount; i++) {
data.push_back(std::make_tuple(i, prob[i]));
}
std::sort(data.begin(), data.end(),
[](auto& a, auto& b) { return std::get<1>(a) > std::get<1>(b); });
std::cout << " --- Top" << topNum << " ---" << std::endl;
for (size_t i = 0; i < topNum; i++) {
std::cout << std::setw(3) << std::get<0>(data[i]) << ": " << std::fixed
<< std::setprecision(6) << std::get<1>(data[i]) << std::endl;
}
}
#define MEM_ALIGN(x, align) (((x) + ((align)-1)) & ~((align)-1))
int main(int argc, char** argv) {
auto exec = tim::lite::Execution::Create(lenet_executable.data(), lenet_executable.size());
if (exec) {
const size_t lenet_output_size = 10;
size_t input_sz = MEM_ALIGN(input_data.size(), 64);
size_t output_sz = MEM_ALIGN(sizeof(float) * lenet_output_size, 64);
uint8_t* input = (uint8_t*)aligned_alloc(64, input_sz);
float* output = (float*)aligned_alloc(64, output_sz);
assert(input);
assert(output);
memset(output, 0, output_sz);
memcpy(input, input_data.data(), input_data.size());
auto input_handle = std::make_shared<tim::lite::UserHandle>(
input, input_data.size());
auto output_handle = std::make_shared<tim::lite::UserHandle>(
output, lenet_output_size * sizeof(float));
exec->BindInputs({input_handle});
exec->BindOutputs({output_handle});
exec->Trigger();
printTopN(output, lenet_output_size, 5);
free(output);
free(input);
} else {
std::cout << "Load executable fail." << std::endl;
return -1;
}
return 0;
}

File diff suppressed because it is too large Load Diff

View File

@ -35,7 +35,8 @@
namespace tim {
namespace lite {
static bool _query_input_buffer_parameters(
namespace {
bool QueryInputBufferParameters(
vip_buffer_create_params_t& param, uint32_t index, vip_network network) {
uint32_t count = 0;
vip_query_network(network, VIP_NETWORK_PROP_INPUT_COUNT, &count);
@ -64,7 +65,7 @@ static bool _query_input_buffer_parameters(
return true;
}
static bool _query_output_buffer_parameters(
bool QueryOutputBufferParameters(
vip_buffer_create_params_t& param, uint32_t index, vip_network network) {
uint32_t count = 0;
vip_query_network(network, VIP_NETWORK_PROP_OUTPUT_COUNT, &count);
@ -93,6 +94,7 @@ static bool _query_output_buffer_parameters(
}
return true;
}
}
ExecutionImpl::ExecutionImpl(const void* executable, size_t executable_size) {
vip_status_e status = VIP_SUCCESS;
@ -128,12 +130,12 @@ ExecutionImpl::~ExecutionImpl() {
vip_finish_network(network_);
vip_destroy_network(network_);
}
inputs_.clear();
outputs_.clear();
input_maps_.clear();
output_maps_.clear();
vip_destroy();
}
Execution& ExecutionImpl::BindInputs(std::vector<std::shared_ptr<Handle>> handles) {
Execution& ExecutionImpl::BindInputs(const std::vector<std::shared_ptr<Handle>>& handles) {
if (!IsValid()) {
return *this;
}
@ -145,25 +147,30 @@ Execution& ExecutionImpl::BindInputs(std::vector<std::shared_ptr<Handle>> handle
status = VIP_ERROR_FAILURE;
break;
}
if (!_query_input_buffer_parameters(param, i, network_)) {
status = VIP_ERROR_FAILURE;
break;
std::shared_ptr<InternalHandle> internal_handle = nullptr;
if (input_maps_.find(handle) == input_maps_.end()) {
if (!QueryInputBufferParameters(param, i, network_)) {
status = VIP_ERROR_FAILURE;
break;
}
internal_handle = handle->impl()->Register(param);
if (!internal_handle) {
status = VIP_ERROR_FAILURE;
break;
}
input_maps_[handle] = internal_handle;
} else {
internal_handle = input_maps_.at(handle);
}
if (!handle->impl()->Register(param)) {
status = VIP_ERROR_FAILURE;
break;
}
status = vip_set_input(network_, i, handle->impl()->handle());
status = vip_set_input(network_, i, internal_handle->handle());
if (status != VIP_SUCCESS) {
break;
}
}
// Copy handles
inputs_ = handles;
return *this;
};
Execution& ExecutionImpl::BindOutputs(std::vector<std::shared_ptr<Handle>> handles) {
Execution& ExecutionImpl::BindOutputs(const std::vector<std::shared_ptr<Handle>>& handles) {
if (!IsValid()) {
return *this;
}
@ -175,25 +182,30 @@ Execution& ExecutionImpl::BindOutputs(std::vector<std::shared_ptr<Handle>> handl
status = VIP_ERROR_FAILURE;
break;
}
if (!_query_output_buffer_parameters(param, i, network_)) {
status = VIP_ERROR_FAILURE;
break;
std::shared_ptr<InternalHandle> internal_handle = nullptr;
if (output_maps_.find(handle) == output_maps_.end()) {
if (!QueryOutputBufferParameters(param, i, network_)) {
status = VIP_ERROR_FAILURE;
break;
}
internal_handle = handle->impl()->Register(param);
if (!internal_handle) {
status = VIP_ERROR_FAILURE;
break;
}
output_maps_[handle] = internal_handle;
} else {
internal_handle = output_maps_.at(handle);
}
if (!handle->impl()->Register(param)) {
status = VIP_ERROR_FAILURE;
break;
}
status = vip_set_output(network_, i, handle->impl()->handle());
status = vip_set_output(network_, i, internal_handle->handle());
if (status != VIP_SUCCESS) {
break;
}
}
// Copy handles
outputs_ = handles;
return *this;
};
bool ExecutionImpl::Exec() {
bool ExecutionImpl::Trigger() {
if (!IsValid()) {
return false;
}

View File

@ -21,11 +21,12 @@
* DEALINGS IN THE SOFTWARE.
*
*****************************************************************************/
#ifndef __TIM_LITE_EXECUTION_PRIVATE_H__
#define __TIM_LITE_EXECUTION_PRIVATE_H__
#ifndef TIM_LITE_EXECUTION_PRIVATE_H_
#define TIM_LITE_EXECUTION_PRIVATE_H_
#include <vector>
#include <memory>
#include <map>
#include "tim/lite/execution.h"
#include "handle_private.h"
@ -38,14 +39,14 @@ class ExecutionImpl : public Execution {
public :
ExecutionImpl(const void* executable, size_t executable_size);
~ExecutionImpl();
Execution& BindInputs(std::vector<std::shared_ptr<Handle>> handles) override;
Execution& BindOutputs(std::vector<std::shared_ptr<Handle>> handles) override;
bool Exec() override;
Execution& BindInputs(const std::vector<std::shared_ptr<Handle>>& handles) override;
Execution& BindOutputs(const std::vector<std::shared_ptr<Handle>>& handles) override;
bool Trigger() override;
bool IsValid() const { return valid_; };
vip_network network() { return network_; };
private:
std::vector<std::shared_ptr<Handle>> inputs_;
std::vector<std::shared_ptr<Handle>> outputs_;
std::map<std::shared_ptr<Handle>, std::shared_ptr<InternalHandle>> input_maps_;
std::map<std::shared_ptr<Handle>, std::shared_ptr<InternalHandle>> output_maps_;
bool valid_;
vip_network network_;
};

View File

@ -26,6 +26,7 @@
#include <cassert>
#include <cstdint>
#include <memory>
#include "execution_private.h"
#include "vip_lite.h"
@ -39,35 +40,36 @@ UserHandle::UserHandle(void* buffer, size_t size) {
impl_ = std::make_unique<UserHandleImpl>(buffer, size);
}
UserHandle::~UserHandle() {
impl()->Unregister();
UserHandle::~UserHandle() {}
std::shared_ptr<InternalHandle> UserHandleImpl::Register(
vip_buffer_create_params_t& params) {
auto internal_handle = std::make_shared<InternalUserHandle>(
user_buffer_, user_buffer_size_, params);
if (!internal_handle->handle()) {
internal_handle.reset();
}
return internal_handle;
}
bool UserHandleImpl::Register(vip_buffer_create_params_t& params) {
bool ret = true;
std::call_once(register_once_, [&ret, &params, this]() {
vip_status_e status = VIP_SUCCESS;
vip_buffer internal_buffer = nullptr;
status = vip_create_buffer_from_handle(&params,
this->user_buffer(),
this->user_buffer_size(),
&internal_buffer);
if (status == VIP_SUCCESS) {
this->handle_ = internal_buffer;
ret = true;
} else {
ret = false;
}
});
return ret;
}
bool UserHandleImpl::Unregister() {
InternalHandle::~InternalHandle() {
if (handle_) {
vip_destroy_buffer(handle_);
handle_ = nullptr;
}
return true;
}
InternalUserHandle::InternalUserHandle(void* user_buffer, size_t user_buffer_size,
vip_buffer_create_params_t& params) {
vip_status_e status = VIP_SUCCESS;
vip_buffer internal_buffer = nullptr;
status = vip_create_buffer_from_handle(&params,
user_buffer, user_buffer_size, &internal_buffer);
if (status == VIP_SUCCESS) {
handle_ = internal_buffer;
} else {
handle_ = nullptr;
}
}
}

View File

@ -22,8 +22,8 @@
*
*****************************************************************************/
#ifndef __TIME_LITE_HANDLE_PRIVATE_H__
#define __TIME_LITE_HANDLE_PRIVATE_H__
#ifndef TIME_LITE_HANDLE_PRIVATE_H_
#define TIME_LITE_HANDLE_PRIVATE_H_
#include <mutex>
#include "tim/lite/handle.h"
@ -32,28 +32,39 @@
namespace tim {
namespace lite {
class InternalHandle;
class HandleImpl {
public:
HandleImpl() : handle_(nullptr) {}
virtual bool Register(vip_buffer_create_params_t& params) = 0;
virtual bool Unregister() = 0;
vip_buffer handle() { return handle_; }
protected:
vip_buffer handle_;
virtual std::shared_ptr<InternalHandle> Register(
vip_buffer_create_params_t& params) = 0;
};
class UserHandleImpl : public HandleImpl {
public:
UserHandleImpl(void* buffer, size_t size)
: user_buffer_(buffer), user_buffer_size_(size) {}
bool Register(vip_buffer_create_params_t& params) override;
bool Unregister() override;
std::shared_ptr<InternalHandle> Register(
vip_buffer_create_params_t& params) override;
size_t user_buffer_size() const { return user_buffer_size_; }
void* user_buffer() { return user_buffer_; }
private:
void* user_buffer_;
size_t user_buffer_size_;
std::once_flag register_once_;
};
class InternalHandle {
public:
~InternalHandle();
vip_buffer handle() { return handle_; };
protected:
vip_buffer handle_;
};
class InternalUserHandle : public InternalHandle {
public:
InternalUserHandle(void* user_buffer, size_t user_buffer_size,
vip_buffer_create_params_t& params);
};
}