* Copyright (c) 2025 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
#include "common/omg_util/omg_util.h"
#include "graph/debug/ge_attr_define.h"
#include "graph/utils/graph_utils.h"
#include "graph/utils/tensor_utils.h"
#include "common/math/math_util.h"
#include "base/err_msg.h"
namespace ge {
Status GetOriginalType(const ge::NodePtr &node, std::string &type) {
GE_CHECK_NOTNULL(node);
type = node->GetType();
GE_IF_BOOL_EXEC(type != FRAMEWORKOP, return SUCCESS);
GE_CHECK_NOTNULL(node->GetOpDesc());
const std::string* type_ptr = ge::AttrUtils::GetStr(node->GetOpDesc(), ATTR_NAME_FRAMEWORK_ORIGINAL_TYPE);
if (type_ptr == nullptr) {
REPORT_INNER_ERR_MSG("E19999", "Get Attr:%s fail from op:%s(%s)", ATTR_NAME_FRAMEWORK_ORIGINAL_TYPE.c_str(),
node->GetName().c_str(), node->GetType().c_str());
GELOGE(INTERNAL_ERROR, "[Get][Attr] %s fail from op:%s(%s)", ATTR_NAME_FRAMEWORK_ORIGINAL_TYPE.c_str(),
node->GetName().c_str(), node->GetType().c_str());
return INTERNAL_ERROR;
}
type = *type_ptr;
GELOGD("Get FrameWorkOp original type [%s]", type.c_str());
return SUCCESS;
}
Status SetStreamLabel(const ge::NodePtr &node, const std::string &label) {
GE_CHECK_NOTNULL(node);
const OpDescPtr tmp_desc = node->GetOpDesc();
GE_CHECK_NOTNULL(tmp_desc);
if (!AttrUtils::SetStr(tmp_desc, ge::ATTR_NAME_STREAM_LABEL, label)) {
REPORT_INNER_ERR_MSG("E19999", "Set Attr:%s fail for op:%s(%s)", ATTR_NAME_STREAM_LABEL.c_str(),
node->GetName().c_str(), node->GetType().c_str());
GELOGE(FAILED, "[Set][Attr] %s fail for op:%s(%s)", ATTR_NAME_STREAM_LABEL.c_str(), node->GetName().c_str(),
node->GetType().c_str());
return FAILED;
}
return SUCCESS;
}
Status SetCycleEvent(const ge::NodePtr &node) {
GE_CHECK_NOTNULL(node);
const OpDescPtr tmp_desc = node->GetOpDesc();
GE_CHECK_NOTNULL(tmp_desc);
if (!AttrUtils::SetBool(tmp_desc, ge::ATTR_NAME_STREAM_CYCLE_EVENT_FLAG, true)) {
REPORT_INNER_ERR_MSG("E19999", "Set Attr:%s fail for op:%s(%s)", ATTR_NAME_STREAM_CYCLE_EVENT_FLAG.c_str(),
node->GetName().c_str(), node->GetType().c_str());
GELOGE(FAILED, "[Set][Attr] %s fail for op:%s(%s)", ATTR_NAME_STREAM_CYCLE_EVENT_FLAG.c_str(),
node->GetName().c_str(), node->GetType().c_str());
return FAILED;
}
return SUCCESS;
}
Status SetActiveLabelList(const ge::NodePtr &node, const std::vector<std::string> &active_label_list) {
GE_CHECK_NOTNULL(node);
const OpDescPtr tmp_desc = node->GetOpDesc();
GE_CHECK_NOTNULL(tmp_desc);
if (!AttrUtils::SetListStr(tmp_desc, ge::ATTR_NAME_ACTIVE_LABEL_LIST, active_label_list)) {
REPORT_INNER_ERR_MSG("E19999", "Set Attr:%s fail for op:%s(%s)", ATTR_NAME_ACTIVE_LABEL_LIST.c_str(),
node->GetName().c_str(), node->GetType().c_str());
GELOGE(FAILED, "[Set][Attr] %s fail for op:%s(%s)", ATTR_NAME_ACTIVE_LABEL_LIST.c_str(), node->GetName().c_str(),
node->GetType().c_str());
return FAILED;
}
return SUCCESS;
}
Status SetSwitchBranchNodeLabel(const ge::NodePtr &node, const std::string &branch_label) {
GE_CHECK_NOTNULL(node);
const OpDescPtr tmp_desc = node->GetOpDesc();
GE_CHECK_NOTNULL(tmp_desc);
if (!AttrUtils::SetStr(tmp_desc, ge::ATTR_NAME_SWITCH_BRANCH_NODE_LABEL, branch_label)) {
REPORT_INNER_ERR_MSG("E19999", "Set Attr:%s fail for op:%s(%s)", ATTR_NAME_SWITCH_BRANCH_NODE_LABEL.c_str(),
node->GetName().c_str(), node->GetType().c_str());
GELOGE(FAILED, "[Set][Attr] %s fail for op:%s(%s)", ATTR_NAME_SWITCH_BRANCH_NODE_LABEL.c_str(),
node->GetName().c_str(), node->GetType().c_str());
return FAILED;
}
return SUCCESS;
}
Status SetSwitchTrueBranchFlag(const ge::NodePtr &node, const bool value) {
GE_CHECK_NOTNULL(node);
const OpDescPtr tmp_desc = node->GetOpDesc();
GE_CHECK_NOTNULL(tmp_desc);
if (!AttrUtils::SetBool(tmp_desc, ge::ATTR_NAME_SWITCH_TRUE_BRANCH_FLAG, value)) {
REPORT_INNER_ERR_MSG("E19999", "Set Attr:%s fail for op:%s(%s)", ATTR_NAME_SWITCH_TRUE_BRANCH_FLAG.c_str(),
node->GetName().c_str(), node->GetType().c_str());
GELOGE(FAILED, "[Set][Attr] %s fail for op:%s(%s)", ATTR_NAME_SWITCH_TRUE_BRANCH_FLAG.c_str(),
node->GetName().c_str(), node->GetType().c_str());
return FAILED;
}
return SUCCESS;
}
Status SetOriginalNodeName(const ge::NodePtr &node, const std::string &orig_name) {
GE_CHECK_NOTNULL(node);
const OpDescPtr tmp_desc = node->GetOpDesc();
GE_CHECK_NOTNULL(tmp_desc);
if (!AttrUtils::SetStr(tmp_desc, ge::ATTR_NAME_ORIG_NODE_NAME, orig_name)) {
REPORT_INNER_ERR_MSG("E19999", "Set Attr:%s fail for op:%s(%s)", ATTR_NAME_ORIG_NODE_NAME.c_str(),
node->GetName().c_str(), node->GetType().c_str());
GELOGE(FAILED, "[Set][Attr] %s fail for op:%s(%s)", ATTR_NAME_ORIG_NODE_NAME.c_str(), node->GetName().c_str(),
node->GetType().c_str());
return FAILED;
}
return SUCCESS;
}
Status SetCyclicDependenceFlag(const ge::NodePtr &node) {
GE_CHECK_NOTNULL(node);
const OpDescPtr tmp_desc = node->GetOpDesc();
GE_CHECK_NOTNULL(tmp_desc);
if (!AttrUtils::SetBool(tmp_desc, ge::ATTR_NAME_CYCLIC_DEPENDENCE_FLAG, true)) {
REPORT_INNER_ERR_MSG("E19999", "Set Attr:%s fail for op:%s(%s)", ATTR_NAME_CYCLIC_DEPENDENCE_FLAG.c_str(),
node->GetName().c_str(), node->GetType().c_str());
GELOGE(FAILED, "[Set][Attr] %s fail for op:%s(%s)", ATTR_NAME_CYCLIC_DEPENDENCE_FLAG.c_str(),
node->GetName().c_str(), node->GetType().c_str());
return FAILED;
}
return SUCCESS;
}
Status SetNextIteration(const NodePtr &node, const NodePtr &next_node) {
GE_CHECK_NOTNULL(node);
GE_CHECK_NOTNULL(next_node);
GE_CHECK_NOTNULL(next_node->GetOpDesc());
GE_CHECK_NOTNULL(next_node->GetOpDesc());
const auto SetIterationName = [](const OpDescPtr &op_desc, const std::string &name) -> Status {
if (!AttrUtils::SetStr(op_desc, ATTR_NAME_NEXT_ITERATION, name)) {
REPORT_INNER_ERR_MSG("E19999", "Set Attr:%s fail for op:%s(%s)", ATTR_NAME_NEXT_ITERATION.c_str(),
op_desc->GetName().c_str(), op_desc->GetType().c_str());
GELOGE(FAILED, "[Set][Attr] %s fail for op:%s(%s)", ATTR_NAME_NEXT_ITERATION.c_str(), op_desc->GetName().c_str(),
op_desc->GetType().c_str());
return FAILED;
}
return SUCCESS;
};
GE_CHK_STATUS_RET_NOLOG(SetIterationName(node->GetOpDesc(), next_node->GetName()));
GE_CHK_STATUS_RET_NOLOG(SetIterationName(next_node->GetOpDesc(), node->GetName()));
return SUCCESS;
}
void AlignMemSize(int64_t &mem_size, const int64_t align_size) {
if (mem_size <= 0L) {
return;
}
mem_size = (mem_size + align_size - 1L) / align_size * align_size;
}
Status GetMemorySize(const NodePtr &node, int64_t &output_size) {
GE_CHECK_NOTNULL(node->GetOpDesc());
const auto output_op_desc = node->GetOpDesc()->GetOutputDescPtr(kBufferPoolNodeOutIndex);
GE_CHECK_NOTNULL(output_op_desc);
int64_t size = 0;
const auto ret = ge::TensorUtils::GetSize(*output_op_desc, size);
if (ret != ge::GRAPH_SUCCESS) {
GELOGE(INTERNAL_ERROR, "[Get][Size]Node:%s.", node->GetName().c_str());
REPORT_INNER_ERR_MSG("E19999", "Failed to get output size, node:%s.", node->GetName().c_str());
return INTERNAL_ERROR;
}
FMK_INT64_ADDCHECK(size, kBufferPoolMemAlignSize);
AlignMemSize(size, kBufferPoolMemAlignSize);
FMK_INT64_ADDCHECK(size, (kBufferPoolMemAlignSize + kBufferPoolMemAlignSize));
output_size = kBufferPoolMemAlignSize + size + kBufferPoolMemAlignSize;
return SUCCESS;
}
void SetControlFlowGroup(const NodePtr &node, const int64_t group) {
GE_RT_VOID_CHECK_NOTNULL(node);
const auto &op_desc = node->GetOpDesc();
GE_RT_VOID_CHECK_NOTNULL(op_desc);
GELOGD("[%s] Set control flow group index: %ld", node->GetName().c_str(), group);
if (!AttrUtils::SetInt(op_desc, ATTR_NAME_CONTROL_FLOW_GROUP, group)) {
REPORT_INNER_ERR_MSG("E19999", "Set Attr:%s fail for op:%s(%s)", ATTR_NAME_CONTROL_FLOW_GROUP.c_str(),
node->GetName().c_str(), node->GetType().c_str());
GELOGE(FAILED, "[Set][Attr] %s fail for op:%s(%s)", ATTR_NAME_CONTROL_FLOW_GROUP.c_str(), node->GetName().c_str(),
node->GetType().c_str());
}
}
}