[Other] Optimize sophgo backend (#1273)

optimize sophgo backend
This commit is contained in:
Jason
2023-02-08 19:50:37 +08:00
committed by GitHub
parent 79d0ee07c9
commit 0fb2f26352
3 changed files with 27 additions and 35 deletions

View File

@@ -27,19 +27,7 @@ SophgoBackend::~SophgoBackend() { bm_dev_free(handle_); }
bool SophgoBackend::GetSDKAndDeviceVersion() { return true; } bool SophgoBackend::GetSDKAndDeviceVersion() { return true; }
/*************************************************************** /***************************************************************
* @name BuildOption * @name Init
* @brief save option
* @param SOPHGOTPU2BackendOption
* @note None
***************************************************************/
void SophgoBackend::BuildOption(const SophgoBackendOption& option) {
// this->option_ = option;
// save cpu_name
// this->option_.cpu_name = option.cpu_name;
}
/***************************************************************
* @name InitFromSophgo
* @brief Initialize Sophgo model * @brief Initialize Sophgo model
* @param model_file: Binary data for the Sophgo model. * @param model_file: Binary data for the Sophgo model.
* params_file: None * params_file: None
@@ -47,8 +35,26 @@ void SophgoBackend::BuildOption(const SophgoBackendOption& option) {
* @return bool * @return bool
* @note None * @note None
***************************************************************/ ***************************************************************/
bool SophgoBackend::InitFromSophgo(const std::string& model_file, bool SophgoBackend::Init(const RuntimeOption& option) {
const SophgoBackendOption& option) { if (option.model_from_memory_) {
FDERROR << "SophgoBackend doesn't support load model from memory, please "
"load model from disk."
<< std::endl;
return false;
}
if (option.model_format != ModelFormat::SOPHGO) {
FDERROR << "SophgoBackend only supports model format SOPHGO, but now it's "
<< option.model_format << "." << std::endl;
return false;
}
if (option.device != Device::SOPHGOTPUD) {
FDERROR << "SophgoBackend only supports device::SOPHGOTPUD, but now it's "
<< option.device << "." << std::endl;
return false;
}
std::string model_file = option.model_file;
// LoadModel // LoadModel
if (!this->LoadModel((char*)model_file.data())) { if (!this->LoadModel((char*)model_file.data())) {
FDERROR << "load model failed" << std::endl; FDERROR << "load model failed" << std::endl;
@@ -61,9 +67,6 @@ bool SophgoBackend::InitFromSophgo(const std::string& model_file,
return false; return false;
} }
// BuildOption
this->BuildOption(option);
// GetModelInputOutputInfos // GetModelInputOutputInfos
if (!this->GetModelInputOutputInfos()) { if (!this->GetModelInputOutputInfos()) {
FDERROR << "get model input output infos failed" << std::endl; FDERROR << "get model input output infos failed" << std::endl;

View File

@@ -30,12 +30,7 @@ class SophgoBackend : public BaseBackend {
public: public:
SophgoBackend() = default; SophgoBackend() = default;
virtual ~SophgoBackend(); virtual ~SophgoBackend();
bool LoadModel(void* model); bool Init(const RuntimeOption& option);
bool GetSDKAndDeviceVersion();
bool GetModelInputOutputInfos();
void BuildOption(const SophgoBackendOption& option);
bool InitFromSophgo(const std::string& model_file,
const SophgoBackendOption& option = SophgoBackendOption());
int NumInputs() const override { int NumInputs() const override {
return static_cast<int>(inputs_desc_.size()); return static_cast<int>(inputs_desc_.size());
@@ -54,6 +49,10 @@ class SophgoBackend : public BaseBackend {
bool copy_to_fd = true) override; bool copy_to_fd = true) override;
private: private:
bool LoadModel(void* model);
bool GetSDKAndDeviceVersion();
bool GetModelInputOutputInfos();
std::vector<TensorInfo> inputs_desc_; std::vector<TensorInfo> inputs_desc_;
std::vector<TensorInfo> outputs_desc_; std::vector<TensorInfo> outputs_desc_;
std::string net_name_; std::string net_name_;

View File

@@ -356,18 +356,8 @@ void Runtime::CreateRKNPU2Backend() {
void Runtime::CreateSophgoNPUBackend() { void Runtime::CreateSophgoNPUBackend() {
#ifdef ENABLE_SOPHGO_BACKEND #ifdef ENABLE_SOPHGO_BACKEND
auto sophgo_option = SophgoBackendOption();
FDASSERT(option.model_from_memory_ == false,
"SophgoBackend don't support to load model from memory");
FDASSERT(option.device == Device::SOPHGOTPUD,
"Backend::SOPHGO only supports Device::SOPHGO");
FDASSERT(option.model_format == ModelFormat::SOPHGO,
"SophgoBackend only support model format of ModelFormat::SOPHGO");
auto sophgo_option = SophgoBackendOption();
backend_ = utils::make_unique<SophgoBackend>(); backend_ = utils::make_unique<SophgoBackend>();
auto casted_backend = dynamic_cast<SophgoBackend*>(backend_.get()); FDASSERT(backend_->Init(option), "Failed to initialize Sophgo backend.");
FDASSERT(casted_backend->InitFromSophgo(option.model_file, sophgo_option),
"Load model from nb file failed while initializing LiteBackend.");
#else #else
FDASSERT(false, FDASSERT(false,
"SophgoBackend is not available, please compiled with " "SophgoBackend is not available, please compiled with "