Skip to content

Commit

Permalink
add 'GetOutputInfos' and 'GetInputInfos' interface (PaddlePaddle#232)
Browse files Browse the repository at this point in the history
add GetOutputInfos GetInputInfos
  • Loading branch information
heliqi authored Sep 15, 2022
1 parent 6963296 commit 0805ead
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 43 deletions.
7 changes: 5 additions & 2 deletions fastdeploy/backends/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@

#pragma once

#include "fastdeploy/backends/common/multiclass_nms.h"
#include "fastdeploy/core/fd_tensor.h"
#include <iostream>
#include <memory>
#include <string>
#include <vector>

#include "fastdeploy/backends/common/multiclass_nms.h"
#include "fastdeploy/core/fd_tensor.h"

namespace fastdeploy {

struct TensorInfo {
Expand Down Expand Up @@ -56,6 +57,8 @@ class BaseBackend {
virtual int NumOutputs() const = 0;
virtual TensorInfo GetInputInfo(int index) = 0;
virtual TensorInfo GetOutputInfo(int index) = 0;
virtual std::vector<TensorInfo> GetInputInfos() = 0;
virtual std::vector<TensorInfo> GetOutputInfos() = 0;
virtual bool Infer(std::vector<FDTensor>& inputs,
std::vector<FDTensor>* outputs) = 0;
};
Expand Down
42 changes: 32 additions & 10 deletions fastdeploy/backends/openvino/ov_backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,27 +107,34 @@ bool OpenVINOBackend::InitFromPaddle(const std::string& model_file,
// So here will reorder it's inputs and outputs
std::string model_content;
ReadBinaryFromFile(model_file, &model_content);
auto reader = paddle2onnx::PaddleReader(model_content.c_str(), model_content.size());
auto reader =
paddle2onnx::PaddleReader(model_content.c_str(), model_content.size());
if (reader.num_inputs != input_infos.size()) {
FDERROR << "The number of inputs from PaddleReader:" << reader.num_inputs << " not equal to the number of inputs from OpenVINO:" << input_infos.size() << "." << std::endl;
FDERROR << "The number of inputs from PaddleReader:" << reader.num_inputs
<< " not equal to the number of inputs from OpenVINO:"
<< input_infos.size() << "." << std::endl;
return false;
}
if (reader.num_outputs != output_infos.size()) {
FDERROR << "The number of outputs from PaddleReader:" << reader.num_outputs << " not equal to the number of outputs from OpenVINO:" << output_infos.size() << "." << std::endl;
FDERROR << "The number of outputs from PaddleReader:" << reader.num_outputs
<< " not equal to the number of outputs from OpenVINO:"
<< output_infos.size() << "." << std::endl;
return false;
}
for (int i = 0; i < reader.num_inputs; ++i) {
auto iter = input_infos.find(std::string(reader.inputs[i].name));
if (iter == input_infos.end()) {
FDERROR << "Cannot find input name:" << reader.inputs[i].name << " from OpenVINO model." << std::endl;
FDERROR << "Cannot find input name:" << reader.inputs[i].name
<< " from OpenVINO model." << std::endl;
return false;
}
input_infos_.push_back(iter->second);
}
for (int i = 0; i < reader.num_outputs; ++i) {
auto iter = output_infos.find(std::string(reader.outputs[i].name));
if (iter == output_infos.end()) {
FDERROR << "Cannot find output name:" << reader.outputs[i].name << " from OpenVINO model." << std::endl;
FDERROR << "Cannot find output name:" << reader.outputs[i].name
<< " from OpenVINO model." << std::endl;
return false;
}
output_infos_.push_back(iter->second);
Expand All @@ -146,6 +153,14 @@ TensorInfo OpenVINOBackend::GetInputInfo(int index) {
return input_infos_[index];
}

std::vector<TensorInfo> OpenVINOBackend::GetInputInfos() {
return input_infos_;
}

std::vector<TensorInfo> OpenVINOBackend::GetOutputInfos() {
return output_infos_;
}

TensorInfo OpenVINOBackend::GetOutputInfo(int index) {
FDASSERT(index < NumOutputs(),
"The index: %d should less than the number of outputs: %d.", index,
Expand Down Expand Up @@ -181,27 +196,34 @@ bool OpenVINOBackend::InitFromOnnx(const std::string& model_file,
// So here will reorder it's inputs and outputs
std::string model_content;
ReadBinaryFromFile(model_file, &model_content);
auto reader = paddle2onnx::OnnxReader(model_content.c_str(), model_content.size());
auto reader =
paddle2onnx::OnnxReader(model_content.c_str(), model_content.size());
if (reader.num_inputs != input_infos.size()) {
FDERROR << "The number of inputs from OnnxReader:" << reader.num_inputs << " not equal to the number of inputs from OpenVINO:" << input_infos.size() << "." << std::endl;
FDERROR << "The number of inputs from OnnxReader:" << reader.num_inputs
<< " not equal to the number of inputs from OpenVINO:"
<< input_infos.size() << "." << std::endl;
return false;
}
if (reader.num_outputs != output_infos.size()) {
FDERROR << "The number of outputs from OnnxReader:" << reader.num_outputs << " not equal to the number of outputs from OpenVINO:" << output_infos.size() << "." << std::endl;
FDERROR << "The number of outputs from OnnxReader:" << reader.num_outputs
<< " not equal to the number of outputs from OpenVINO:"
<< output_infos.size() << "." << std::endl;
return false;
}
for (int i = 0; i < reader.num_inputs; ++i) {
auto iter = input_infos.find(std::string(reader.inputs[i].name));
if (iter == input_infos.end()) {
FDERROR << "Cannot find input name:" << reader.inputs[i].name << " from OpenVINO model." << std::endl;
FDERROR << "Cannot find input name:" << reader.inputs[i].name
<< " from OpenVINO model." << std::endl;
return false;
}
input_infos_.push_back(iter->second);
}
for (int i = 0; i < reader.num_outputs; ++i) {
auto iter = output_infos.find(std::string(reader.outputs[i].name));
if (iter == output_infos.end()) {
FDERROR << "Cannot find output name:" << reader.outputs[i].name << " from OpenVINO model." << std::endl;
FDERROR << "Cannot find output name:" << reader.outputs[i].name
<< " from OpenVINO model." << std::endl;
return false;
}
output_infos_.push_back(iter->second);
Expand Down
2 changes: 2 additions & 0 deletions fastdeploy/backends/openvino/ov_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class OpenVINOBackend : public BaseBackend {

TensorInfo GetInputInfo(int index) override;
TensorInfo GetOutputInfo(int index) override;
std::vector<TensorInfo> GetInputInfos() override;
std::vector<TensorInfo> GetOutputInfos() override;

private:
void InitTensorInfo(const std::vector<ov::Output<ov::Node>>& ov_outputs,
Expand Down
18 changes: 18 additions & 0 deletions fastdeploy/backends/ort/ort_backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,16 @@ TensorInfo OrtBackend::GetInputInfo(int index) {
return info;
}

std::vector<TensorInfo> OrtBackend::GetInputInfos() {
auto size = inputs_desc_.size();
std::vector<TensorInfo> infos;
infos.reserve(size);
for (auto i = 0; i < size; i++) {
infos.emplace_back(GetInputInfo(i));
}
return infos;
}

TensorInfo OrtBackend::GetOutputInfo(int index) {
FDASSERT(index < NumOutputs(),
"The index: %d should less than the number of outputs: %d.", index,
Expand All @@ -259,6 +269,14 @@ TensorInfo OrtBackend::GetOutputInfo(int index) {
return info;
}

std::vector<TensorInfo> OrtBackend::GetOutputInfos() {
std::vector<TensorInfo> infos;
for (auto i = 0; i < outputs_desc_.size(); i++) {
infos.emplace_back(GetOutputInfo(i));
}
return infos;
}

void OrtBackend::InitCustomOperators() {
#ifndef NON_64_PLATFORM
if (custom_operators_.size() == 0) {
Expand Down
2 changes: 2 additions & 0 deletions fastdeploy/backends/ort/ort_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class OrtBackend : public BaseBackend {

TensorInfo GetInputInfo(int index);
TensorInfo GetOutputInfo(int index);
std::vector<TensorInfo> GetInputInfos() override;
std::vector<TensorInfo> GetOutputInfos() override;
static std::vector<OrtCustomOp*> custom_operators_;
void InitCustomOperators();

Expand Down
6 changes: 4 additions & 2 deletions fastdeploy/backends/paddle/paddle_backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ TensorInfo PaddleBackend::GetInputInfo(int index) {
return inputs_desc_[index];
}

std::vector<TensorInfo> PaddleBackend::GetInputInfo() { return inputs_desc_; }
std::vector<TensorInfo> PaddleBackend::GetInputInfos() { return inputs_desc_; }

TensorInfo PaddleBackend::GetOutputInfo(int index) {
FDASSERT(index < NumOutputs(),
Expand All @@ -94,7 +94,9 @@ TensorInfo PaddleBackend::GetOutputInfo(int index) {
return outputs_desc_[index];
}

std::vector<TensorInfo> PaddleBackend::GetOutputInfo() { return outputs_desc_; }
std::vector<TensorInfo> PaddleBackend::GetOutputInfos() {
return outputs_desc_;
}

bool PaddleBackend::Infer(std::vector<FDTensor>& inputs,
std::vector<FDTensor>* outputs) {
Expand Down
4 changes: 2 additions & 2 deletions fastdeploy/backends/paddle/paddle_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ class PaddleBackend : public BaseBackend {

TensorInfo GetInputInfo(int index);
TensorInfo GetOutputInfo(int index);
std::vector<TensorInfo> GetInputInfo();
std::vector<TensorInfo> GetOutputInfo();
std::vector<TensorInfo> GetInputInfos() override;
std::vector<TensorInfo> GetOutputInfos() override;

private:
paddle_infer::Config config_;
Expand Down
17 changes: 17 additions & 0 deletions fastdeploy/backends/tensorrt/trt_backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,14 @@ TensorInfo TrtBackend::GetInputInfo(int index) {
return info;
}

std::vector<TensorInfo> TrtBackend::GetInputInfos() {
std::vector<TensorInfo> infos;
for (auto i = 0; i < inputs_desc_.size(); i++) {
infos.emplace_back(GetInputInfo(i));
}
return infos;
}

TensorInfo TrtBackend::GetOutputInfo(int index) {
FDASSERT(index < NumOutputs(),
"The index: %d should less than the number of outputs: %d.", index,
Expand All @@ -600,4 +608,13 @@ TensorInfo TrtBackend::GetOutputInfo(int index) {
info.dtype = GetFDDataType(outputs_desc_[index].dtype);
return info;
}

std::vector<TensorInfo> TrtBackend::GetOutputInfos() {
std::vector<TensorInfo> infos;
for (auto i = 0; i < outputs_desc_.size(); i++) {
infos.emplace_back(GetOutputInfo(i));
}
return infos;
}

} // namespace fastdeploy
2 changes: 2 additions & 0 deletions fastdeploy/backends/tensorrt/trt_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class TrtBackend : public BaseBackend {
int NumOutputs() const { return outputs_desc_.size(); }
TensorInfo GetInputInfo(int index);
TensorInfo GetOutputInfo(int index);
std::vector<TensorInfo> GetInputInfos() override;
std::vector<TensorInfo> GetOutputInfos() override;

~TrtBackend() {
if (parser_) {
Expand Down
28 changes: 28 additions & 0 deletions fastdeploy/core/fd_tensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,33 @@ void FDTensor::PrintInfo(const std::string& prefix) {
<< ", min=" << min << std::endl;
}

bool FDTensor::AllocFn(size_t nbytes) {
if (device == Device::GPU) {
#ifdef WITH_GPU
return FDDeviceAllocator()(&buffer_, nbytes);
#else
FDASSERT(false,
"The FastDeploy FDTensor allocator didn't compile under "
"-DWITH_GPU=ON,"
"so this is an unexpected problem happend.");
#endif
}
return FDHostAllocator()(&buffer_, nbytes);
}

void FDTensor::FreeFn() {
if (external_data_ptr != nullptr) external_data_ptr = nullptr;
if (buffer_ != nullptr) {
if (device == Device::GPU) {
#ifdef WITH_GPU
FDDeviceFree()(buffer_);
#endif
} else {
FDHostFree()(buffer_);
}
buffer_ = nullptr;
}
}

FDTensor::FDTensor(const std::string& tensor_name) { name = tensor_name; }
} // namespace fastdeploy
30 changes: 3 additions & 27 deletions fastdeploy/core/fd_tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,33 +98,9 @@ struct FASTDEPLOY_DECL FDTensor {
// prefix will also be printed as tag
void PrintInfo(const std::string& prefix = "TensorInfo: ");

bool AllocFn(size_t nbytes) {
if (device == Device::GPU) {
#ifdef WITH_GPU
return FDDeviceAllocator()(&buffer_, nbytes);
#else
FDASSERT(false,
"The FastDeploy FDTensor allocator didn't compile under "
"-DWITH_GPU=ON,"
"so this is an unexpected problem happend.");
#endif
}
return FDHostAllocator()(&buffer_, nbytes);
}

void FreeFn() {
if (external_data_ptr != nullptr) external_data_ptr = nullptr;
if (buffer_ != nullptr) {
if (device == Device::GPU) {
#ifdef WITH_GPU
FDDeviceFree()(buffer_);
#endif
} else {
FDHostFree()(buffer_);
}
buffer_ = nullptr;
}
}
bool AllocFn(size_t nbytes);

void FreeFn();

FDTensor() {}
explicit FDTensor(const std::string& tensor_name);
Expand Down
9 changes: 9 additions & 0 deletions fastdeploy/fastdeploy_runtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

#include "fastdeploy/fastdeploy_runtime.h"

#include "fastdeploy/utils/unique_ptr.h"
#include "fastdeploy/utils/utils.h"

Expand Down Expand Up @@ -297,6 +298,14 @@ TensorInfo Runtime::GetOutputInfo(int index) {
return backend_->GetOutputInfo(index);
}

std::vector<TensorInfo> Runtime::GetInputInfos() {
return backend_->GetInputInfos();
}

std::vector<TensorInfo> Runtime::GetOutputInfos() {
return backend_->GetOutputInfos();
}

bool Runtime::Infer(std::vector<FDTensor>& input_tensors,
std::vector<FDTensor>* output_tensors) {
return backend_->Infer(input_tensors, output_tensors);
Expand Down
2 changes: 2 additions & 0 deletions fastdeploy/fastdeploy_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ struct FASTDEPLOY_DECL Runtime {
int NumOutputs() { return backend_->NumOutputs(); }
TensorInfo GetInputInfo(int index);
TensorInfo GetOutputInfo(int index);
std::vector<TensorInfo> GetInputInfos();
std::vector<TensorInfo> GetOutputInfos();

RuntimeOption option;

Expand Down

0 comments on commit 0805ead

Please sign in to comment.