mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2025-10-17 06:00:59 +08:00
[Backend] add sophgo backend (#1015)
* Add Sophgo Device add sophgo backend in fastdeploy add resnet50, yolov5s, liteseg examples. * replace sophgo lib with download links; fix model.cc bug * modify CodeStyle * remove unuseful files;change the names of sophgo device and sophgo backend * sophgo support python and add python examples * remove unuseful rows in cmake according pr Co-authored-by: Zilong Xing <zilong.xing@sophgo.com>
This commit is contained in:
@@ -45,6 +45,10 @@
|
||||
#include "fastdeploy/backends/rknpu/rknpu2/rknpu2_backend.h"
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_SOPHGO_BACKEND
|
||||
#include "fastdeploy/backends/sophgo/sophgo_backend.h"
|
||||
#endif
|
||||
|
||||
namespace fastdeploy {
|
||||
|
||||
std::vector<Backend> GetAvailableBackends() {
|
||||
@@ -69,6 +73,9 @@ std::vector<Backend> GetAvailableBackends() {
|
||||
#endif
|
||||
#ifdef ENABLE_RKNPU2_BACKEND
|
||||
backends.push_back(Backend::RKNPU2);
|
||||
#endif
|
||||
#ifdef ENABLE_SOPHGO_BACKEND
|
||||
backends.push_back(Backend::SOPHGOTPU);
|
||||
#endif
|
||||
return backends;
|
||||
}
|
||||
@@ -94,6 +101,8 @@ std::string Str(const Backend& b) {
|
||||
return "Backend::POROS";
|
||||
} else if (b == Backend::RKNPU2) {
|
||||
return "Backend::RKNPU2";
|
||||
} else if (b == Backend::SOPHGOTPU) {
|
||||
return "Backend::SOPHGOTPU";
|
||||
} else if (b == Backend::OPENVINO) {
|
||||
return "Backend::OPENVINO";
|
||||
} else if (b == Backend::LITE) {
|
||||
@@ -113,6 +122,8 @@ std::ostream& operator<<(std::ostream& out, const Backend& backend) {
|
||||
out << "Backend::OPENVINO";
|
||||
} else if (backend == Backend::RKNPU2) {
|
||||
out << "Backend::RKNPU2";
|
||||
} else if (backend == Backend::SOPHGOTPU) {
|
||||
out << "Backend::SOPHGOTPU";
|
||||
} else if (backend == Backend::POROS) {
|
||||
out << "Backend::POROS";
|
||||
} else if (backend == Backend::LITE) {
|
||||
@@ -158,6 +169,15 @@ bool CheckModelFormat(const std::string& model_file,
|
||||
<< model_file << std::endl;
|
||||
return false;
|
||||
}
|
||||
} else if (model_format == ModelFormat::SOPHGO) {
|
||||
if (model_file.size() < 7 ||
|
||||
model_file.substr(model_file.size() -7, 7) != ".bmodel") {
|
||||
FDERROR
|
||||
<< "With model format of ModelFormat::SOPHGO, the model file "
|
||||
"should ends with `.bmodel`, but now it's "
|
||||
<< model_file << std::endl;
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
FDERROR
|
||||
<< "Only support model format with frontend ModelFormat::PADDLE / "
|
||||
@@ -185,6 +205,10 @@ ModelFormat GuessModelFormat(const std::string& model_file) {
|
||||
model_file.substr(model_file.size() - 5, 5) == ".rknn") {
|
||||
FDINFO << "Model Format: RKNN." << std::endl;
|
||||
return ModelFormat::RKNN;
|
||||
} else if (model_file.size() > 7 &&
|
||||
model_file.substr(model_file.size() - 7, 7) == ".bmodel") {
|
||||
FDINFO << "Model Format: SOPHGO." << std::endl;
|
||||
return ModelFormat::SOPHGO;
|
||||
}
|
||||
|
||||
FDERROR << "Cannot guess which model format you are using, please set "
|
||||
@@ -288,6 +312,11 @@ void RuntimeOption::UseAscend(){
|
||||
device = Device::ASCEND;
|
||||
}
|
||||
|
||||
void RuntimeOption::UseSophgo() {
|
||||
device = Device::SOPHGOTPUD;
|
||||
UseSophgoBackend();
|
||||
}
|
||||
|
||||
void RuntimeOption::SetExternalStream(void* external_stream) {
|
||||
external_stream_ = external_stream;
|
||||
}
|
||||
@@ -323,6 +352,15 @@ void RuntimeOption::UseOrtBackend() {
|
||||
#endif
|
||||
}
|
||||
|
||||
// use sophgoruntime backend
|
||||
void RuntimeOption::UseSophgoBackend() {
|
||||
#ifdef ENABLE_SOPHGO_BACKEND
|
||||
backend = Backend::SOPHGOTPU;
|
||||
#else
|
||||
FDASSERT(false, "The FastDeploy didn't compile with SophgoBackend.");
|
||||
#endif
|
||||
}
|
||||
|
||||
// use poros backend
|
||||
void RuntimeOption::UsePorosBackend() {
|
||||
#ifdef ENABLE_POROS_BACKEND
|
||||
@@ -564,6 +602,8 @@ bool Runtime::Init(const RuntimeOption& _option) {
|
||||
option.backend = Backend::OPENVINO;
|
||||
} else if (IsBackendAvailable(Backend::RKNPU2)) {
|
||||
option.backend = Backend::RKNPU2;
|
||||
} else if (IsBackendAvailable(Backend::SOPHGOTPU)) {
|
||||
option.backend = Backend::SOPHGOTPU;
|
||||
} else {
|
||||
FDERROR << "Please define backend in RuntimeOption, current it's "
|
||||
"Backend::UNKNOWN."
|
||||
@@ -623,7 +663,15 @@ bool Runtime::Init(const RuntimeOption& _option) {
|
||||
|
||||
FDINFO << "Runtime initialized with Backend::RKNPU2 in "
|
||||
<< Str(option.device) << "." << std::endl;
|
||||
} else {
|
||||
} else if (option.backend == Backend::SOPHGOTPU) {
|
||||
FDASSERT(option.device == Device::SOPHGOTPUD,
|
||||
"Backend::SOPHGO only supports Device::SOPHGO");
|
||||
CreateSophgoNPUBackend();
|
||||
|
||||
FDINFO << "Runtime initialized with Backend::SOPHGO in "
|
||||
<< Str(option.device) << "." << std::endl;
|
||||
}
|
||||
else {
|
||||
FDERROR << "Runtime only support "
|
||||
"Backend::ORT/Backend::TRT/Backend::PDINFER/Backend::POROS as "
|
||||
"backend now."
|
||||
@@ -926,6 +974,21 @@ void Runtime::CreateRKNPU2Backend() {
|
||||
#endif
|
||||
}
|
||||
|
||||
void Runtime::CreateSophgoNPUBackend() {
|
||||
#ifdef ENABLE_SOPHGO_BACKEND
|
||||
auto sophgo_option = SophgoBackendOption();
|
||||
FDASSERT(option.model_format == ModelFormat::SOPHGO,
|
||||
"SophgoBackend only support model format of ModelFormat::SOPHGO");
|
||||
backend_ = utils::make_unique<SophgoBackend>();
|
||||
auto casted_backend = dynamic_cast<SophgoBackend*>(backend_.get());
|
||||
FDASSERT(casted_backend->InitFromSophgo(option.model_file, sophgo_option),
|
||||
"Load model from nb file failed while initializing LiteBackend.");
|
||||
#else
|
||||
FDASSERT(false, "SophgoBackend is not available, please compiled with "
|
||||
"ENABLE_SOPHGO_BACKEND=ON.");
|
||||
#endif
|
||||
}
|
||||
|
||||
Runtime* Runtime::Clone(void* stream, int device_id) {
|
||||
Runtime* runtime = new Runtime();
|
||||
if (option.backend != Backend::OPENVINO &&
|
||||
|
Reference in New Issue
Block a user