|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- /**
- * Copyright 2021 Huawei Technologies Co., Ltd
- *
- * 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.
- */
-
- #include "command_handle.h"
- #include "runtime/base.h"
- #include "common/profiling/profiling_manager.h"
- #include "framework/common/debug/ge_log.h"
- #include "framework/common/debug/log.h"
- #include "framework/common/ge_inner_error_codes.h"
- #include "framework/omg/omg_inner_types.h"
- #include "graph/load/graph_loader.h"
-
- namespace {
- const uint32_t kDeviceListIndex = 3;
- const uint32_t kCommandNum = 6;
- const int kMaxDevNum = 64;
- const std::string kDeviceNums = "devNums";
- const std::string kDeviceIdList = "devIdList";
- const std::string kProfilingInit = "prof_init";
- const std::string kProfilingFinalize = "prof_finalize";
- const std::string kProfilingStart = "prof_start";
- const std::string kProfilingStop = "prof_stop";
- const std::string kProfilingModelSubscribe = "prof_model_subscribe";
- const std::string kProfilingModelUnsubscribe = "prof_model_cancel_subscribe";
- const std::string kProfilingModelId = "modelId";
-
- enum ProfCommandHandleType {
- kProfCommandhandleInit = 0,
- kProfCommandhandleStart,
- kProfCommandhandleStop,
- kProfCommandhandleFinalize,
- kProfCommandhandleModelSubscribe,
- kProfCommandhandleModelUnsubscribe
- };
-
- const std::map<ProfCommandHandleType, std::string> kProfCommandTypeMap = {
- {kProfCommandhandleInit, kProfilingInit},
- {kProfCommandhandleStart, kProfilingStart},
- {kProfCommandhandleStop, kProfilingStop},
- {kProfCommandhandleFinalize, kProfilingFinalize},
- {kProfCommandhandleModelSubscribe, kProfilingModelSubscribe},
- {kProfCommandhandleModelUnsubscribe, kProfilingModelUnsubscribe}};
-
- bool IsProfTypeValid(uint32_t type) {
- if (type < 0 || type >= kCommandNum) {
- GELOGE(ge::PARAM_INVALID, "[Check][Type]Type %u is invalid", type);
- return false;
- }
- GELOGD("Type is %u", type);
- return true;
- }
-
- bool IsProfConfigValid(const uint32_t *deviceid_list, uint32_t device_nums) {
- if (deviceid_list == nullptr) {
- GELOGE(ge::PARAM_INVALID, "[Check][DeviceIDList]Invalid, it is nullptr");
- REPORT_INNER_ERROR("E19999", "Device id list is nullptr");
- return false;
- }
- if (device_nums == 0 || device_nums > kMaxDevNum) {
- GELOGE(ge::PARAM_INVALID, "[Check][DeviceNums]Invalid, device nums: %u", device_nums);
- REPORT_INNER_ERROR("E19999", "DeviceNums %u check invalid", device_nums);
- return false;
- }
-
- // real device num
- int32_t dev_count = 0;
- rtError_t rt_err = rtGetDeviceCount(&dev_count);
- if (rt_err != RT_ERROR_NONE) {
- GELOGE(ge::INTERNAL_ERROR, "[Get][DeviceCount]Failed, error_code %d", rt_err);
- REPORT_CALL_ERROR("E19999", "Get device count failed, error_code %d", rt_err);
- return false;
- }
-
- if (device_nums > static_cast<uint32_t>(dev_count)) {
- GELOGE(ge::PARAM_INVALID, "[Check][Param]Device num %u is not in range [1,%d]", device_nums, dev_count);
- REPORT_INNER_ERROR("E19999", "Device num %u check invalid, it is not in range [1,%d]", device_nums, dev_count);
- return false;
- }
-
- std::set<uint32_t> record;
- for (size_t i = 0; i < device_nums; ++i) {
- uint32_t dev_id = deviceid_list[i];
- if (dev_id >= static_cast<uint32_t>(dev_count)) {
- GELOGE(ge::PARAM_INVALID, "[Check][DeviceId]Device id %u is not in range [0,%d)", dev_id, dev_count);
- REPORT_CALL_ERROR("E19999", "Device id %u is not in range [0,%d)", dev_id, dev_count);
- return false;
- }
- if (record.count(dev_id) > 0) {
- GELOGE(ge::PARAM_INVALID, "[Check][DeviceId]Device id %u is duplicatedly set", dev_id);
- REPORT_CALL_ERROR("E19999", "Device id %u is not unique, duplicatedly set", dev_id);
- return false;
- }
- record.insert(dev_id);
- }
- return true;
- }
-
- bool TransProfConfigToParam(const rtProfCommandHandle &profCommand, vector<string> &prof_config_params) {
- prof_config_params.clear();
- prof_config_params.emplace_back(kDeviceNums);
- prof_config_params.emplace_back(std::to_string(profCommand.devNums));
- prof_config_params.emplace_back(kDeviceIdList);
- std::string devID = "";
- if (profCommand.devNums == 0) {
- GELOGE(ge::FAILED, "[Check][Param]The device num is invalid.");
- return false;
- }
- for (uint32_t i = 0; i < profCommand.devNums; i++) {
- devID.append(std::to_string(profCommand.devIdList[i]));
- if (i != profCommand.devNums - 1) {
- devID.append(",");
- }
- }
-
- prof_config_params.push_back(devID);
- return true;
- }
-
- ge::Status NeedUnsubscribe(ProfCommandHandleType type, bool is_subscribe, uint32_t graph_id,
- vector<string> &prof_params) {
- if (type == kProfCommandhandleModelUnsubscribe && is_subscribe) {
- prof_params.clear();
- prof_params.emplace_back(kProfilingModelId);
- uint32_t model_id = graph_id;
- if (is_subscribe) {
- auto &profiling_manager = ge::ProfilingManager::Instance();
- auto ret = profiling_manager.GetModelIdFromGraph(graph_id, model_id);
- if (ret != ge::SUCCESS) {
- GELOGE(ret, "[Get][GraphId]graph_id:%u not not found", graph_id);
- return ret;
- }
- }
- prof_params.emplace_back(std::to_string(model_id));
- }
- return ge::SUCCESS;
- }
-
- rtError_t NeedHandleStartEnd(ProfCommandHandleType type, rtProfCommandHandle_t *prof_config_param,
- std::vector<string> &prof_params) {
- if (type == kProfCommandhandleStart || type == kProfCommandhandleStop) {
- if (!IsProfConfigValid(prof_config_param->devIdList, prof_config_param->devNums)) {
- return ge::FAILED;
- }
- if (!TransProfConfigToParam(*prof_config_param, prof_params)) {
- GELOGE(ge::PARAM_INVALID, "[Check][Param]Transfer profilerConfig to string vector failed");
- REPORT_CALL_ERROR("E19999", "Transfer profilerConfig to string vector failed");
- return ge::PARAM_INVALID;
- }
- }
- return ge::SUCCESS;
- }
-
- rtError_t NeedHandleModelSubscribe(ProfCommandHandleType type, rtProfCommandHandle_t *prof_config_param,
- std::vector<string> &prof_params) {
- if (type == kProfCommandhandleModelSubscribe) {
- auto &profiling_manager = ge::ProfilingManager::Instance();
- auto is_train = domi::GetContext().train_flag;
- if (is_train) {
- profiling_manager.SetSubscribeInfo(prof_config_param->profSwitch, prof_config_param->modelId, true);
- return ge::SUCCESS;
- }
- prof_params.clear();
- prof_params.push_back(kProfilingModelId);
- prof_params.push_back(std::to_string(prof_config_param->modelId));
- }
- return ge::SUCCESS;
- }
-
- rtError_t ExecuteCommand(ProfCommandHandleType type,
- std::map<ProfCommandHandleType, std::string>::const_iterator iter,
- rtProfCommandHandle_t *prof_config_param, std::vector<string> &prof_params) {
- ge::GraphLoader graph_loader;
- ge::Command command;
- command.cmd_params.clear();
- command.cmd_type = iter->second;
- command.cmd_params = prof_params;
- if (type != kProfCommandhandleFinalize) {
- command.module_index = prof_config_param->profSwitch;
- }
- GELOGI("GE commandhandle execute, Command Type: %s, data type config: 0x%lx", iter->second.c_str(),
- command.module_index);
- if (type == kProfCommandhandleStart || type == kProfCommandhandleStop) {
- GELOGI("Profiling device nums:%s , deviceID:[%s]", prof_params[0].c_str(), prof_params[kDeviceListIndex].c_str());
- }
- ge::Status ret = graph_loader.CommandHandle(command);
- if (ret != ge::SUCCESS) {
- GELOGE(ret, "[Handle][Command]Handle profiling command failed, command type %s, error_code %u",
- iter->second.c_str(), ret);
- REPORT_CALL_ERROR("E19999", "Handle profiling command failed, command type %s, error_code %u",
- iter->second.c_str(), ret);
- return ge::FAILED;
- }
-
- GELOGI("Successfully execute profiling command type: %d, command 0x%lx.", type, command.module_index);
- return ge::SUCCESS;
- }
-
- rtError_t HandleCtrlSwitch(void *data) {
- auto &profiling_manager = ge::ProfilingManager::Instance();
- rtProfCommandHandle_t *prof_config_param = reinterpret_cast<rtProfCommandHandle_t *>(data);
- if (!IsProfTypeValid(prof_config_param->type)) {
- GELOGE(ge::PARAM_INVALID, "[Check][Param]The prof comand is invalid.");
- return ge::FAILED;
- }
- auto type = static_cast<ProfCommandHandleType>(prof_config_param->type);
- if (type != kProfCommandhandleFinalize) {
- GE_CHECK_NOTNULL(data);
- }
- auto iter = kProfCommandTypeMap.find(type);
- if (iter == kProfCommandTypeMap.end()) {
- GELOGE(ge::PARAM_INVALID, "[Check][Param]The prof comand type is invalid.");
- return ge::PARAM_INVALID;
- }
- std::vector<string> prof_params;
- ge::Status ret = NeedHandleStartEnd(type, prof_config_param, prof_params);
- if (ret != ge::SUCCESS) {
- return ret;
- }
- ret = NeedHandleModelSubscribe(type, prof_config_param, prof_params);
- if (ret != ge::SUCCESS) {
- return ret;
- }
-
- auto is_subscribe = profiling_manager.GetSubscribeInfo().is_subscribe;
- // GraphId is actually stored in prof_config_param
- auto graph_id = prof_config_param->modelId;
- ret = NeedUnsubscribe(type, is_subscribe, graph_id, prof_params);
- if (ret != ge::SUCCESS) {
- GELOGE(ret, "[Check][Param]graph_id:%u not not found", graph_id);
- REPORT_INPUT_ERROR(
- "E10001", std::vector<std::string>({"value", "parameter", "reason"}),
- std::vector<std::string>({std::to_string(graph_id), "GraphToModelMap", "graph_id does not exist!"}));
- return ge::FAILED;
- }
- return ExecuteCommand(type, iter, prof_config_param, prof_params);
- }
- } // namespace
-
- namespace ge {
- rtError_t CommandHandle(uint32_t rt_type, void *data, uint32_t len) {
- if (data == nullptr) {
- GELOGE(ge::PARAM_INVALID, "[Check][Param]The prof comand is invalid.");
- return ge::FAILED;
- }
- auto &profiling_manager = ge::ProfilingManager::Instance();
- if (rt_type == RT_PROF_CTRL_REPORTER) {
- profiling_manager.SetMsprofReporterCallback(reinterpret_cast<MsprofReporterCallback>(data));
- GELOGD("return with MsprofReporterCallback");
- return ge::SUCCESS;
- } else if (rt_type == RT_PROF_CTRL_SWITCH) {
- return HandleCtrlSwitch(data);
- }
- return ge::FAILED;
- }
- } // namespace ge
|