2021-01-11 18:16:03 +08:00
|
|
|
/****************************************************************************
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2020 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 "tim/vx/graph.h"
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
#include "context_private.h"
|
|
|
|
|
#include "graph_private.h"
|
|
|
|
|
#include "tensor_private.h"
|
2021-02-19 17:53:24 +08:00
|
|
|
#include "operation_private.h"
|
|
|
|
|
|
2021-01-11 18:16:03 +08:00
|
|
|
#include "tim/vx/context.h"
|
2021-02-19 17:53:24 +08:00
|
|
|
#include "tim/vx/ops/nbg.h"
|
2021-01-11 18:16:03 +08:00
|
|
|
#include "vsi_nn_pub.h"
|
|
|
|
|
|
|
|
|
|
namespace tim {
|
|
|
|
|
namespace vx {
|
|
|
|
|
|
|
|
|
|
GraphImpl::GraphImpl(ContextImpl* context)
|
|
|
|
|
: context_(context),
|
|
|
|
|
graph_(vsi_nn_CreateGraph(context_->context(), 0, 0)),
|
2021-02-19 17:53:24 +08:00
|
|
|
tensor_placeholder_(nullptr) {}
|
2021-01-11 18:16:03 +08:00
|
|
|
|
|
|
|
|
GraphImpl::~GraphImpl() { vsi_nn_ReleaseGraph(&graph_); }
|
|
|
|
|
|
|
|
|
|
vsi_nn_graph_t* GraphImpl::graph() { return graph_; }
|
|
|
|
|
|
|
|
|
|
void GraphImpl::AddInput(vsi_nn_tensor_id_t id) {
|
|
|
|
|
if (inputs_.end() == std::find(inputs_.begin(), inputs_.end(), id)) {
|
|
|
|
|
inputs_.push_back(id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GraphImpl::AddOutput(vsi_nn_tensor_id_t id) {
|
|
|
|
|
if (outputs_.end() == std::find(outputs_.begin(), outputs_.end(), id)) {
|
|
|
|
|
outputs_.push_back(id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<Tensor> GraphImpl::CreateTensor(const TensorSpec& spec,
|
|
|
|
|
const void* data) {
|
|
|
|
|
return std::make_shared<TensorImpl>(this, spec, data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<Tensor> GraphImpl::CreateTensorPlaceHolder() {
|
|
|
|
|
if (!tensor_placeholder_) {
|
|
|
|
|
tensor_placeholder_ = std::make_shared<TensorPlaceholder>(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tensor_placeholder_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GraphImpl::Compile() {
|
2021-02-19 17:53:24 +08:00
|
|
|
bool status = true;
|
|
|
|
|
|
|
|
|
|
std::call_once(setio_once_, [&status, this]() {
|
|
|
|
|
status = (vsi_nn_SetGraphInputs(this->graph_, this->inputs_.data(), this->inputs_.size()) &&
|
|
|
|
|
vsi_nn_SetGraphOutputs(this->graph_, this->outputs_.data(), this->outputs_.size()));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
std::call_once(setup_once_, [&status, this](){
|
|
|
|
|
status = (VSI_SUCCESS == vsi_nn_SetupGraph(this->graph_, true));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
std::call_once(verify_graph_once_, [&status, this]() {
|
|
|
|
|
status = (VSI_SUCCESS == vsi_nn_VerifyGraph(this->graph_));
|
|
|
|
|
});
|
2021-01-11 18:16:03 +08:00
|
|
|
|
2021-02-19 17:53:24 +08:00
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GraphImpl::CompileToBinary(void* buf, size_t* size) {
|
|
|
|
|
bool status = true;
|
|
|
|
|
std::call_once(setio_once_, [&status, this]() {
|
|
|
|
|
status = (vsi_nn_SetGraphInputs(this->graph_, this->inputs_.data(), this->inputs_.size()) &&
|
|
|
|
|
vsi_nn_SetGraphOutputs(this->graph_,this->outputs_.data(), this->outputs_.size()));
|
|
|
|
|
});
|
2021-01-11 18:16:03 +08:00
|
|
|
|
2021-02-19 17:53:24 +08:00
|
|
|
std::call_once(setup_once_, [&status, this](){
|
|
|
|
|
status = (VSI_SUCCESS == vsi_nn_SetupGraph(this->graph_, true));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return ((status) && (VSI_SUCCESS == vsi_nn_GenerateNBG(graph_, buf, size)));
|
2021-01-11 18:16:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GraphImpl::Run() {
|
2021-02-19 17:53:24 +08:00
|
|
|
return ((Compile()) && (VSI_SUCCESS == vsi_nn_RunGraph(graph_)));
|
2021-01-11 18:16:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace vx
|
|
|
|
|
} // namespace tim
|