!23198 context support to set Ascend310 config

Merge pull request !23198 from zhengyuanhua/code_review
This commit is contained in:
i-robot 2021-09-13 01:41:54 +00:00 committed by Gitee
commit 901124c9bd
12 changed files with 79 additions and 36 deletions

View File

@ -1,15 +1,11 @@
# build mindspore_shared_lib
include_directories(${CMAKE_SOURCE_DIR}/mindspore/ccsrc)
include_directories(${CMAKE_SOURCE_DIR}/mindspore/ccsrc/minddata/dataset)
if(NOT(BUILD_LITE))
if(NOT BUILD_LITE)
set(LOAD_MINDIR_SRC
${CMAKE_SOURCE_DIR}/mindspore/core/load_mindir/load_model.cc
${CMAKE_SOURCE_DIR}/mindspore/core/load_mindir/anf_model_parser.cc
)
else()
set(MS_UTILS_SRC
${CMAKE_CURRENT_SOURCE_DIR}/../../../mindspore/ccsrc/utils/config_manager.cc
)
endif()
file(GLOB_RECURSE API_OPS_SRC ${CMAKE_CURRENT_SOURCE_DIR} "ops/*.cc")
@ -21,18 +17,13 @@ if(ENABLE_D OR ENABLE_ACL)
include_directories(${CMAKE_BINARY_DIR}/proto/ge)
file(GLOB_RECURSE API_ACL_SRC ${CMAKE_CURRENT_SOURCE_DIR}
"model/acl/acl_model_options.cc"
"akg_kernel_register.cc"
"model/acl/*.cc"
"model/acl/model_converter.cc"
"model/model_converter_utils/*.cc"
"graph/acl/*.cc"
)
if(NOT(BUILD_LITE))
list(APPEND API_ACL_SRC "${CMAKE_CURRENT_SOURCE_DIR}/akg_kernel_register.cc"
"${CMAKE_CURRENT_SOURCE_DIR}/model/acl/acl_model_multi.cc"
"${CMAKE_CURRENT_SOURCE_DIR}/model/acl/acl_model.cc")
endif()
if(NOT ENABLE_D)
list(APPEND API_ACL_SRC $<TARGET_OBJECTS:_mindspore_transform_graph_ir_obj>)
list(APPEND API_ACL_SRC $<TARGET_OBJECTS:_mindspore_vm_obj>)
@ -53,22 +44,29 @@ endif()
set(MSLIB_SRC ${CMAKE_CURRENT_SOURCE_DIR}/types.cc
${CMAKE_CURRENT_SOURCE_DIR}/context.cc
${CMAKE_CURRENT_SOURCE_DIR}/cell.cc
${CMAKE_CURRENT_SOURCE_DIR}/serialization.cc
${CMAKE_CURRENT_SOURCE_DIR}/graph/graph.cc
${CMAKE_CURRENT_SOURCE_DIR}/graph/graph_data.cc
${CMAKE_CURRENT_SOURCE_DIR}/model/model.cc
${CMAKE_CURRENT_SOURCE_DIR}/model/model_impl.cc
${API_MS_INFER_SRC}
${API_ACL_SRC}
${API_OPS_SRC}
${LOAD_MINDIR_SRC}
${MS_UTILS_SRC})
${LOAD_MINDIR_SRC})
if(NOT(BUILD_LITE))
list(APPEND MSLIB_SRC "${CMAKE_CURRENT_SOURCE_DIR}/serialization.cc"
"${CMAKE_CURRENT_SOURCE_DIR}/model/model.cc"
"${CMAKE_CURRENT_SOURCE_DIR}/model/model_impl.cc")
if(BUILD_LITE)
list(APPEND MSLIB_SRC "${CMAKE_CURRENT_SOURCE_DIR}/../../../mindspore/ccsrc/utils/config_manager.cc")
list(REMOVE_ITEM MSLIB_SRC "${CMAKE_CURRENT_SOURCE_DIR}/akg_kernel_register.cc"
"${CMAKE_CURRENT_SOURCE_DIR}/model/acl/acl_model_multi.cc"
"${CMAKE_CURRENT_SOURCE_DIR}/model/acl/acl_model.cc"
"${CMAKE_CURRENT_SOURCE_DIR}/serialization.cc"
"${CMAKE_CURRENT_SOURCE_DIR}/model/model.cc"
"${CMAKE_CURRENT_SOURCE_DIR}/model/model_impl.cc"
$<TARGET_OBJECTS:_mindspore_vm_obj>)
endif()
add_library(mindspore_shared_lib SHARED ${MSLIB_SRC})
if(NOT(BUILD_LITE))
if(NOT BUILD_LITE)
set_target_properties(mindspore_shared_lib PROPERTIES OUTPUT_NAME mindspore)
endif()
@ -81,9 +79,9 @@ else()
-Wl,--whole-archive mindspore -Wl,--no-whole-archive mindspore_core proto_input mindspore_gvar
mindspore::protobuf)
else()
if(NOT(BUILD_LITE))
if(NOT BUILD_LITE)
target_link_libraries(mindspore_shared_lib PRIVATE ${PYTHON_LIBRARIES} ${SECUREC_LIBRARY}
mindspore mindspore_core proto_input mindspore_gvar mindspore::protobuf)
mindspore mindspore_core proto_input mindspore_gvar mindspore::protobuf)
else()
target_link_libraries(mindspore_shared_lib PRIVATE ${SECUREC_LIBRARY})
endif()

View File

@ -39,11 +39,16 @@ typedef struct NpuDeviceInfo {
int frequency_ = 3; /**< npu frequency inference, low 1, medium 2, high 3, extreme 4, other values will be set to 3 */
} NpuDeviceInfo;
/// \brief Ascend310DeviceInfo defined for Ascend's configuration information.
typedef struct AscendDeviceInfo {
uint32_t device_id_;
} AscendDeviceInfo;
/// \brief DeviceInfo defined for backend's configuration information.
struct DeviceInfo {
CpuDeviceInfo cpu_device_info_;
GpuDeviceInfo gpu_device_info_;
NpuDeviceInfo npu_device_info_;
AscendDeviceInfo ascend310_device_info_;
};
/// \brief DeviceContext defined for holding backend's configuration information.

View File

@ -27,9 +27,10 @@ typedef enum {
/// \brief DeviceType defined for holding user's preferred backend.
typedef enum {
DT_CPU, /**< CPU device type */
DT_GPU, /**< GPU device type */
DT_NPU /**< NPU device type */
DT_CPU, /**< CPU device type */
DT_GPU, /**< GPU device type */
DT_NPU, /**< NPU device type */
DT_ASCEND310 /**< ASCEND310 device type */
} DeviceType;
typedef enum {

View File

@ -69,6 +69,18 @@ std::shared_ptr<mindspore::KirinNPUDeviceInfo> NPUDeviceInfoFromNPUDeviceContext
PassBasicProperties(npu_info, npu_context);
return npu_info;
}
std::shared_ptr<mindspore::Ascend310DeviceInfo> Ascend310DeviceInfoFromAscend310DeviceContext(
const lite::DeviceContext &ascend310_context) {
if (ascend310_context.device_type_ != DT_ASCEND310) {
MS_LOG(ERROR) << "Function input parameter is not ascend310 context.";
return nullptr;
}
auto ascend310_info = std::make_shared<mindspore::Ascend310DeviceInfo>();
MS_CHECK_TRUE_RET(ascend310_info != nullptr, nullptr);
ascend310_info->SetDeviceID(ascend310_context.device_info_.ascend310_device_info_.device_id_);
return ascend310_info;
}
} // namespace
mindspore::Context *MSContextFromContext(const lite::Context *context) {
@ -89,7 +101,8 @@ mindspore::Context *MSContextFromContext(const lite::Context *context) {
std::map<DeviceType, std::function<std::shared_ptr<mindspore::DeviceInfoContext>(const lite::DeviceContext &)>>
transfer_funcs = {{DT_CPU, CPUDeviceInfoFromCPUDeviceContext},
{DT_GPU, GPUDeviceInfoFromGPUDeviceContext},
{DT_NPU, NPUDeviceInfoFromNPUDeviceContext}};
{DT_NPU, NPUDeviceInfoFromNPUDeviceContext},
{DT_ASCEND310, Ascend310DeviceInfoFromAscend310DeviceContext}};
for (auto &device_context : context->device_list_) {
auto device_type = device_context.device_type_;
if (transfer_funcs.find(device_type) == transfer_funcs.end()) {

View File

@ -71,6 +71,14 @@ Status AddNpuDevice(Context *a_context, lite::InnerContext *l_context, DeviceInf
return kSuccess;
}
Status AddAscend310Device(Context *a_context, lite::InnerContext *l_context, DeviceInfoContext *device) {
lite::DeviceInfo device_info = {0};
auto ascend310_context = device->Cast<Ascend310DeviceInfo>();
device_info.ascend310_device_info_ = {ascend310_context->GetDeviceID()};
l_context->device_list_.push_back({lite::DT_ASCEND310, device_info});
return kSuccess;
}
Status A2L_ConvertContext(Context *a_context, lite::InnerContext *l_context) {
if ((a_context == nullptr) || (l_context == nullptr)) {
MS_LOG(ERROR) << "Invalid context pointers.";
@ -100,6 +108,8 @@ Status A2L_ConvertContext(Context *a_context, lite::InnerContext *l_context) {
error_code = AddGpuDevice(a_context, l_context, device.get());
} else if (device->GetDeviceType() == kKirinNPU) {
error_code = AddNpuDevice(a_context, l_context, device.get());
} else if (device->GetDeviceType() == kAscend310) {
error_code = AddAscend310Device(a_context, l_context, device.get());
} else {
MS_LOG(ERROR) << "Invalid device.";
return kLiteInputParamInvalid;

View File

@ -268,7 +268,7 @@ bool InnerContext::IsProviderEnabled() const {
bool InnerContext::IsAllDeviceTypeValid() const {
return std::all_of(this->device_list_.begin(), this->device_list_.end(), [](const DeviceContext &device) {
return device.device_type_ >= DT_CPU && device.device_type_ <= DT_NPU;
return device.device_type_ >= DT_CPU && device.device_type_ <= DT_ASCEND310;
});
}

View File

@ -47,8 +47,8 @@ AclModelOptions CustomAscend310Kernel::GetAclModelOptions(const mindspore::Conte
}
auto context = const_cast<mindspore::Context *>(ctx);
auto device_infos = context->MutableDeviceInfo();
if (device_infos.size() != 1) {
MS_LOG(WARNING) << "Size of device infos is not one.";
if (device_infos.size() < 1) {
MS_LOG(WARNING) << "Size of device infos is less than one.";
return options;
}
if (device_infos[0] == nullptr) {
@ -62,7 +62,6 @@ AclModelOptions CustomAscend310Kernel::GetAclModelOptions(const mindspore::Conte
}
options.device_id = static_cast<int32_t>(ascend31o_info->GetDeviceID());
options.dump_cfg_path = ascend31o_info->GetDumpConfigPath();
return options;
}

View File

@ -239,6 +239,16 @@ int BenchmarkBase::CheckThreadNumValid() {
return RET_OK;
}
int BenchmarkBase::CheckDeviceTypeValid() {
if (flags_->device_ != "CPU" && flags_->device_ != "GPU" && flags_->device_ != "NPU" &&
flags_->device_ != "Ascend310") {
MS_LOG(ERROR) << "Device type:" << flags_->device_ << " is not supported.";
std::cerr << "Device type:" << flags_->device_ << " is not supported." << std::endl;
return RET_ERROR;
}
return RET_OK;
}
int BenchmarkBase::InitDumpConfigFromJson(char *path) {
auto real_path = RealPath(path);
std::ifstream ifs(real_path);
@ -393,9 +403,8 @@ int BenchmarkBase::Init() {
return RET_ERROR;
}
if (flags_->device_ != "CPU" && flags_->device_ != "GPU" && flags_->device_ != "NPU") {
MS_LOG(ERROR) << "Device type:" << flags_->device_ << " is not supported.";
std::cerr << "Device type:" << flags_->device_ << " is not supported." << std::endl;
if (CheckDeviceTypeValid() != RET_OK) {
MS_LOG(ERROR) << "Device type is invalid.";
return RET_ERROR;
}

View File

@ -105,7 +105,7 @@ class MS_API BenchmarkFlags : public virtual FlagParser {
AddFlag(&BenchmarkFlags::model_file_, "modelFile", "Input model file", "");
AddFlag(&BenchmarkFlags::in_data_file_, "inDataFile", "Input data file, if not set, use random input", "");
AddFlag(&BenchmarkFlags::config_file_, "configFile", "Config file", "");
AddFlag(&BenchmarkFlags::device_, "device", "CPU | GPU | NPU", "CPU");
AddFlag(&BenchmarkFlags::device_, "device", "CPU | GPU | NPU | Ascend310", "CPU");
AddFlag(&BenchmarkFlags::cpu_bind_mode_, "cpuBindMode",
"Input 0 for NO_BIND, 1 for HIGHER_CPU, 2 for MID_CPU, default value: 1", 1);
// MarkPerformance
@ -298,6 +298,8 @@ class MS_API BenchmarkBase {
int CheckThreadNumValid();
int CheckDeviceTypeValid();
protected:
BenchmarkFlags *flags_;
std::vector<std::string> benchmark_tensor_names_;

View File

@ -177,6 +177,12 @@ void BenchmarkUnifiedApi::InitMSContext(const std::shared_ptr<mindspore::Context
device_list.push_back(npu_device_info);
}
if (flags_->device_ == "Ascend310") {
std::shared_ptr<Ascend310DeviceInfo> ascend310_device_info = std::make_shared<Ascend310DeviceInfo>();
ascend310_device_info->SetDeviceID(0);
device_list.push_back(ascend310_device_info);
}
// CPU priority is behind GPU and NPU
std::shared_ptr<CPUDeviceInfo> device_info = std::make_shared<CPUDeviceInfo>();
device_info->SetEnableFP16(flags_->enable_fp16_);

View File

@ -214,7 +214,7 @@ STATUS AclPass::ConvertGraphToOm(const FuncGraphPtr &func_graph, Buffer *om_data
SetAclModelOptions(func_graph);
// call interface of cloud
ModelConverter model_converter;
model_converter.set_options(options_.get());
model_converter.set_options(options_);
*om_data = model_converter.LoadMindIR(func_graph);
if (om_data->Data() == nullptr || om_data->DataSize() == 0) {
MS_LOG(ERROR) << "Model converter load mindir failed.";
@ -230,7 +230,7 @@ void AclPass::SetAclModelOptions(const FuncGraphPtr &func_graph) {
ascend310_info->SetDeviceID(0);
model_context->MutableDeviceInfo().emplace_back(ascend310_info);
// set options
options_ = std::make_unique<AclModelOptions>(model_context);
options_ = std::make_shared<AclModelOptions>(model_context);
if (options_ == nullptr) {
MS_LOG(ERROR) << "Acl option make shared failed.";
return;

View File

@ -58,7 +58,7 @@ class AclPass : public Pass {
FmkType fmk_type_;
ParameterPtr om_parameter_ = nullptr;
CNodePtr custom_node_ = nullptr;
std::unique_ptr<AclModelOptions> options_;
std::shared_ptr<AclModelOptions> options_;
AnfNodePtrList graph_outputs_;
std::vector<std::string> graph_output_names_;
std::vector<std::vector<int64_t>> graph_output_dims_;