Merged
【fix】: fix GetAllIrAttrs logic #4794
kantao1Add on Sep 8
【fix】: fix GetAllIrAttrs logic #4794
Merged
Merged fromkantao1/gerefactoringintocann/gedevelop
Total: 6 files changed+57-7
| @@ -94,13 +94,20 @@ bool GetAllIrAttrs(const ge::NodePtr &node, std::vector<std::vector<uint8_t>> &r | |||
| 94 | const auto &ir_attr_names = op_desc->GetIrAttrNames(); | 94 | const auto &ir_attr_names = op_desc->GetIrAttrNames(); |
| 95 | for (const auto &attr_name : ir_attr_names) { | 95 | for (const auto &attr_name : ir_attr_names) { |
| 96 | const std::map<std::string, ge::AnyValue>::const_iterator &iter = all_attrs.find(attr_name); | 96 | const std::map<std::string, ge::AnyValue>::const_iterator &iter = all_attrs.find(attr_name); |
| 97 | - if (iter == all_attrs.cend()) { | 97 | + if (iter != all_attrs.cend()) { |
| 98 | - runtime_attrs.clear(); | 98 | + GE_ASSERT_TRUE(AppendAttrTensor(iter->second, runtime_attrs)); |
| 99 | - GELOGI("Cannot find the IR attr %s from node %s(%s), clear all attrs", attr_name.c_str(), node->GetNamePtr(), | 99 | + continue; |
| 100 | - node->GetTypePtr()); | ||
| 101 | - return true; | ||
| 102 | } | 100 | } |
| 103 | - GE_ASSERT_TRUE(AppendAttrTensor(iter->second, runtime_attrs)); | 101 | + const auto &required_attrs = op_desc->GetRequiredAttrWithType(); |
| 102 | + if (required_attrs.find(attr_name) != required_attrs.cend()) { | ||
| 103 | + GELOGE(ge::FAILED, "Cannot find the required IR attr %s from node %s(%s), clear all attrs", attr_name.c_str(), | ||
| 104 | + node->GetNamePtr(), node->GetTypePtr()); | ||
| 105 | + runtime_attrs.clear(); | ||
| 106 | + return false; | ||
| 107 | + } | ||
| 108 | + GELOGD("Node %s(%s) ir attr %s has no value and is not marked as required, skip the remaining ir attrs", | ||
| 109 | + node->GetNamePtr(), node->GetTypePtr(), attr_name.c_str()); | ||
| 110 | + return true; | ||
| 104 | } | 111 | } |
| 105 | return true; | 112 | return true; |
| 106 | } | 113 | } |
| @@ -52,7 +52,7 @@ class KernelContextHolder { | |||
| 52 | std::vector<Chain> value_holder_; | 52 | std::vector<Chain> value_holder_; |
| 53 | std::unique_ptr<uint8_t[]> compute_node_extend_holder_; | 53 | std::unique_ptr<uint8_t[]> compute_node_extend_holder_; |
| 54 | bg::BufferPool buffer_pool_; | 54 | bg::BufferPool buffer_pool_; |
| 55 | - KernelContext *context_; | 55 | + KernelContext *context_{nullptr}; |
| 56 | }; | 56 | }; |
| 57 | class KernelRunContextBuilder { | 57 | class KernelRunContextBuilder { |
| 58 | public: | 58 | public: |
| @@ -246,6 +246,7 @@ ge::graphStatus ExeGraphSerializer::SerializeComputeNodeInfo(const std::vector<g | |||
| 246 | } else { | 246 | } else { |
| 247 | compute_node_info = CreateComputeNodeInfo(compute_node, buffer_pool, private_attrs, total_size); | 247 | compute_node_info = CreateComputeNodeInfo(compute_node, buffer_pool, private_attrs, total_size); |
| 248 | } | 248 | } |
| 249 | + GE_ASSERT_NOTNULL(compute_node_info, "Create compute node info for node %s failed.", compute_node->GetNamePtr()); | ||
| 249 | auto buf_id = compute_node_infos.AddBuf(compute_node_info.get(), total_size); | 250 | auto buf_id = compute_node_infos.AddBuf(compute_node_info.get(), total_size); |
| 250 | index_2_unique_index.emplace_back(buf_id); | 251 | index_2_unique_index.emplace_back(buf_id); |
| 251 | } | 252 | } |
| @@ -97,6 +97,7 @@ ComputeGraphPtr ShapeRuleOpGraph(const std::string &rule, const bool &with_binar | |||
| 97 | std::vector<uint8_t> binary; | 97 | std::vector<uint8_t> binary; |
| 98 | ShapeInferenceRule::CompileJsonString(rule, binary); | 98 | ShapeInferenceRule::CompileJsonString(rule, binary); |
| 99 | AttrUtils::SetStr(rule_op->GetOpDesc(), "_inference_rule", rule); | 99 | AttrUtils::SetStr(rule_op->GetOpDesc(), "_inference_rule", rule); |
| 100 | + AttrUtils::SetInt(rule_op->GetOpDesc(), "N", static_cast<int64_t>(num_outputs)); | ||
| 100 | if (with_binary) { | 101 | if (with_binary) { |
| 101 | AttrUtils::SetBytes(rule_op->GetOpDesc(), "_inference_rule_binary", Buffer::CopyFrom(binary.data(), binary.size())); | 102 | AttrUtils::SetBytes(rule_op->GetOpDesc(), "_inference_rule_binary", Buffer::CopyFrom(binary.data(), binary.size())); |
| 102 | } | 103 | } |
| @@ -86,6 +86,7 @@ ComputeGraphPtr ShapeRuleOpGraph(const std::string &rule, const bool &with_binar | |||
| 86 | std::vector<uint8_t> binary; | 86 | std::vector<uint8_t> binary; |
| 87 | ShapeInferenceRule::CompileJsonString(rule, binary); | 87 | ShapeInferenceRule::CompileJsonString(rule, binary); |
| 88 | AttrUtils::SetStr(rule_op->GetOpDesc(), "_inference_rule", rule); | 88 | AttrUtils::SetStr(rule_op->GetOpDesc(), "_inference_rule", rule); |
| 89 | + AttrUtils::SetInt(rule_op->GetOpDesc(), "N", static_cast<int64_t>(num_outputs)); | ||
| 89 | if (with_binary) { | 90 | if (with_binary) { |
| 90 | AttrUtils::SetBytes(rule_op->GetOpDesc(), "_inference_rule_binary", Buffer::CopyFrom(binary.data(), binary.size())); | 91 | AttrUtils::SetBytes(rule_op->GetOpDesc(), "_inference_rule_binary", Buffer::CopyFrom(binary.data(), binary.size())); |
| 91 | } | 92 | } |
| @@ -102,6 +102,46 @@ TEST_F(BgIrAttrsUT, CreateAttrBufferSuccessOpLossAttr) { | |||
| 102 | EXPECT_EQ(base[1], 0U); // todo 原始用例,没加预留字段之前,base[1]为啥能取到值 | 102 | EXPECT_EQ(base[1], 0U); // todo 原始用例,没加预留字段之前,base[1]为啥能取到值 |
| 103 | } | 103 | } |
| 104 | 104 | ||
| 105 | +TEST_F(BgIrAttrsUT, CreateAttrBufferFailedWhenRequiredIrAttrLoss) { | ||
| 106 | + auto op_desc = std::make_shared<ge::OpDesc>("foo", "Foo"); | ||
| 107 | + op_desc->AppendIrAttrName("n"); | ||
| 108 | + EXPECT_EQ(op_desc->AddRequiredAttr("n"), ge::GRAPH_SUCCESS); | ||
| 109 | + auto node = ge::NodeUtils::CreatNodeWithoutGraph(op_desc); | ||
| 110 | + size_t attr_size; | ||
| 111 | + auto attr_buffer = bg::CreateAttrBuffer(node, attr_size); | ||
| 112 | + EXPECT_EQ(attr_buffer, nullptr); | ||
| 113 | +} | ||
| 114 | + | ||
| 115 | +TEST_F(BgIrAttrsUT, CreateAttrBufferSuccessWhenRequiredIrAttrHasValue) { | ||
| 116 | + auto op_desc = std::make_shared<ge::OpDesc>("foo", "Foo"); | ||
| 117 | + op_desc->AppendIrAttrName("n"); | ||
| 118 | + EXPECT_EQ(op_desc->AddRequiredAttr("n"), ge::GRAPH_SUCCESS); | ||
| 119 | + ge::AttrUtils::SetInt(op_desc, "n", 5); | ||
| 120 | + auto node = ge::NodeUtils::CreatNodeWithoutGraph(op_desc); | ||
| 121 | + size_t attr_size; | ||
| 122 | + auto attr_buffer = bg::CreateAttrBuffer(node, attr_size); | ||
| 123 | + auto rt_attr_def = reinterpret_cast<RuntimeAttrsDef *>(attr_buffer.get()); | ||
| 124 | + ASSERT_NE(rt_attr_def, nullptr); | ||
| 125 | + EXPECT_EQ(rt_attr_def->attr_num, 1U); | ||
| 126 | + auto base = reinterpret_cast<uint8_t *>(rt_attr_def); | ||
| 127 | + EXPECT_EQ(*reinterpret_cast<int64_t *>(base + rt_attr_def->offset[0]), 5); | ||
| 128 | +} | ||
| 129 | + | ||
| 130 | +TEST_F(BgIrAttrsUT, CreateAttrBufferSkipLossOptionalIrAttr) { | ||
| 131 | + auto op_desc = std::make_shared<ge::OpDesc>("foo", "Foo"); | ||
| 132 | + op_desc->AppendIrAttrName("a"); | ||
| 133 | + ge::AttrUtils::SetInt(op_desc, "a", 1); | ||
| 134 | + op_desc->AppendIrAttrName("b"); | ||
| 135 | + auto node = ge::NodeUtils::CreatNodeWithoutGraph(op_desc); | ||
| 136 | + size_t attr_size; | ||
| 137 | + auto attr_buffer = bg::CreateAttrBuffer(node, attr_size); | ||
| 138 | + auto rt_attr_def = reinterpret_cast<RuntimeAttrsDef *>(attr_buffer.get()); | ||
| 139 | + ASSERT_NE(rt_attr_def, nullptr); | ||
| 140 | + EXPECT_EQ(rt_attr_def->attr_num, 1U); | ||
| 141 | + auto base = reinterpret_cast<uint8_t *>(rt_attr_def); | ||
| 142 | + EXPECT_EQ(*reinterpret_cast<int64_t *>(base + rt_attr_def->offset[0]), 1); | ||
| 143 | +} | ||
| 144 | + | ||
| 105 | TEST_F(BgIrAttrsUT, CreateListListIntAttrBuffer_Int64Ok) { | 145 | TEST_F(BgIrAttrsUT, CreateListListIntAttrBuffer_Int64Ok) { |
| 106 | auto op_desc = std::make_shared<ge::OpDesc>("foo", "Foo"); | 146 | auto op_desc = std::make_shared<ge::OpDesc>("foo", "Foo"); |
| 107 | op_desc->AppendIrAttrName("axes"); | 147 | op_desc->AppendIrAttrName("axes"); |