[Other] Finetune sophgo module (#1054)

refine sophgo code
This commit is contained in:
Jason
2023-01-04 16:18:54 +08:00
committed by GitHub
parent d49160252b
commit 78a8c9afda
4 changed files with 78 additions and 55 deletions

View File

@@ -0,0 +1,25 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <cstring>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
namespace fastdeploy {
struct SophgoBackendOption{
};
} // namespace fastdeploy

View File

@@ -12,13 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastdeploy/backends/sophgo/sophgo_backend.h"
#include <assert.h>
namespace fastdeploy {
SophgoBackend::~SophgoBackend() {
bm_dev_free(handle_);
}
SophgoBackend::~SophgoBackend() { bm_dev_free(handle_); }
/***************************************************************
* @name GetSDKAndDeviceVersion
* @brief get Sophgo sdk and device version
@@ -26,10 +24,7 @@ namespace fastdeploy {
* @return bool
* @note None
***************************************************************/
bool SophgoBackend::GetSDKAndDeviceVersion() {
return true;
}
bool SophgoBackend::GetSDKAndDeviceVersion() { return true; }
/***************************************************************
* @name BuildOption
@@ -54,7 +49,6 @@ void SophgoBackend::BuildOption(const SophgoBackendOption& option) {
***************************************************************/
bool SophgoBackend::InitFromSophgo(const std::string& model_file,
const SophgoBackendOption& option) {
// LoadModel
if (!this->LoadModel((char*)model_file.data())) {
FDERROR << "load model failed" << std::endl;
@@ -167,11 +161,12 @@ TensorInfo SophgoBackend::GetOutputInfo(int index) {
return outputs_desc_[index];
}
std::vector<TensorInfo> SophgoBackend::GetOutputInfos() { return outputs_desc_; }
std::vector<TensorInfo> SophgoBackend::GetOutputInfos() {
return outputs_desc_;
}
bool SophgoBackend::Infer(std::vector<FDTensor>& inputs,
std::vector<FDTensor>* outputs,
bool copy_to_fd) {
std::vector<FDTensor>* outputs, bool copy_to_fd) {
int input_size = inputs.size();
assert(input_size != 0);
assert(input_size == NumInputs());
@@ -180,14 +175,15 @@ bool SophgoBackend::Infer(std::vector<FDTensor>& inputs,
bm_data_type_t* input_dtypes = net_info_->input_dtypes;
for (int i = 0; i < input_size; i++) {
status = bm_malloc_device_byte(handle_,
&input_tensors[i].device_mem,net_info_->max_input_bytes[i]);
status = bm_malloc_device_byte(handle_, &input_tensors[i].device_mem,
net_info_->max_input_bytes[i]);
assert(BM_SUCCESS == status);
input_tensors[i].dtype = input_dtypes[i];
input_tensors[i].st_mode = BM_STORE_1N;
input_tensors[i].shape = *(net_info_->stages[i].input_shapes);
unsigned int input_byte = bmrt_tensor_bytesize(&input_tensors[i]);
bm_memcpy_s2d_partial(handle_, input_tensors[i].device_mem, (void *)inputs[i].Data(),
bm_memcpy_s2d_partial(handle_, input_tensors[i].device_mem,
(void*)inputs[i].Data(),
bmrt_tensor_bytesize(&input_tensors[i]));
}
@@ -199,7 +195,8 @@ bool SophgoBackend::Infer(std::vector<FDTensor>& inputs,
assert(BM_SUCCESS == status);
}
bool launch_status = bmrt_launch_tensor_ex(p_bmrt_, net_name_.c_str(), input_tensors, net_info_->input_num,
bool launch_status = bmrt_launch_tensor_ex(
p_bmrt_, net_name_.c_str(), input_tensors, net_info_->input_num,
output_tensors, net_info_->output_num, true, false);
assert(launch_status);
status = bm_thread_sync(handle_);
@@ -210,14 +207,16 @@ bool SophgoBackend::Infer(std::vector<FDTensor>& inputs,
for (int i = 0; i < output_size; i++) {
int temp_bytesize = bmrt_tensor_bytesize(&output_tensors[i]); // Byte
float* temp_out = (float*)malloc(temp_bytesize);
bm_memcpy_d2s_partial(handle_, temp_out, output_tensors[i].device_mem, temp_bytesize);
bm_memcpy_d2s_partial(handle_, temp_out, output_tensors[i].device_mem,
temp_bytesize);
std::vector<int64_t> temp_shape;
temp_shape.resize(outputs_desc_[i].shape.size());
for (int j = 0; j < outputs_desc_[i].shape.size(); ++j) {
temp_shape[j] = outputs_desc_[i].shape[j];
}
(*outputs)[i].Resize(temp_shape, outputs_desc_[i].dtype, outputs_desc_[i].name);
(*outputs)[i].Resize(temp_shape, outputs_desc_[i].dtype,
outputs_desc_[i].name);
memcpy((*outputs)[i].MutableData(), temp_out, (*outputs)[i].Nbytes());
free(temp_out);
@@ -264,7 +263,8 @@ FDDataType SophgoBackend::SophgoTensorTypeToFDDataType(bm_data_type_t type) {
* @note None
***************************************************************/
// Sophgo_tensor_type
bm_data_type_t SophgoBackend::FDDataTypeToSophgoTensorType(fastdeploy::FDDataType type) {
bm_data_type_t SophgoBackend::FDDataTypeToSophgoTensorType(
fastdeploy::FDDataType type) {
if (type == FDDataType::FP16) {
return BM_FLOAT16;
}
@@ -287,4 +287,4 @@ bm_data_type_t SophgoBackend::FDDataTypeToSophgoTensorType(fastdeploy::FDDataTyp
return BM_FLOAT32;
}
}
} // namespace fastdeploy

View File

@@ -17,7 +17,7 @@
#include "fastdeploy/core/fd_tensor.h"
#include "bmruntime_interface.h" // NOLINT
#include "bmlib_runtime.h" // NOLINT
#include "fastdeploy/backends/sophgo/sophgo_config.h"
#include "fastdeploy/backends/sophgo/option.h"
#include <cstring>
#include <iostream>
#include <memory>
@@ -25,8 +25,6 @@
#include <vector>
namespace fastdeploy {
struct SophgoBackendOption{
};
class SophgoBackend : public BaseBackend {
public: