You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

node.cc 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "src/node.h"
  17. #include <fstream>
  18. #include <sstream>
  19. #include <string>
  20. #include <vector>
  21. #include <algorithm>
  22. #include "schema/inner/ms_generated.h"
  23. #include "common/mslog.h"
  24. #include "common/op_utils.h"
  25. #include "include/errorcode.h"
  26. #include "src/op_factory.h"
  27. namespace mindspore {
  28. namespace predict {
  29. Node::Node(const NodeDef *nodeDef)
  30. : id(std::string(nodeDef->opDef()->name()->c_str())), type(GetOpTypeName(*nodeDef)) {}
  31. Node::~Node() {
  32. if (op != nullptr) {
  33. delete op;
  34. }
  35. }
  36. NODE_ID Node::ID() { return id; }
  37. std::string Node::Type() { return type; }
  38. void Node::SetTensors(const NodeDef &nodeDef, const std::vector<Tensor *> &allTensors) {
  39. if (nodeDef.opDef() == nullptr) {
  40. MS_LOGE("nodeDef is null");
  41. return;
  42. }
  43. auto inputIndex = nodeDef.opDef()->inputIndex();
  44. MS_ASSERT(inputIndex != nullptr);
  45. inputs.resize(inputIndex->size());
  46. std::transform(inputIndex->begin(), inputIndex->end(), inputs.begin(), [allTensors](int i) { return allTensors[i]; });
  47. auto outputIndex = nodeDef.opDef()->outputIndex();
  48. MS_ASSERT(outputIndex != nullptr);
  49. outputs.resize(outputIndex->size());
  50. std::transform(outputIndex->begin(), outputIndex->end(), outputs.begin(),
  51. [allTensors](int i) { return allTensors[i]; });
  52. }
  53. void Node::SetDepends(const std::unordered_set<NODE_ID> &deps) { depends = deps; }
  54. std::unordered_set<NODE_ID> Node::GetDepends() { return depends; }
  55. void Node::AddInEdge(Node *node) {
  56. if (node == nullptr) {
  57. MS_LOGE("node is null");
  58. return;
  59. }
  60. inEdges.insert(node);
  61. }
  62. void Node::AddOutEdge(Node *node) {
  63. if (node == nullptr) {
  64. MS_LOGE("node is null");
  65. return;
  66. }
  67. outEdges.insert(node);
  68. }
  69. std::unordered_set<Node *> &Node::GetAllInEdges() { return inEdges; }
  70. std::unordered_set<Node *> &Node::GetAllOutEdges() { return outEdges; }
  71. std::vector<Tensor *> &Node::GetOutputTensors() { return outputs; }
  72. std::vector<Tensor *> &Node::GetInputTensors() { return inputs; }
  73. int Node::InitOp(const OpDef &opDef, const Context &ctx) {
  74. OpDesc dst;
  75. dst.type = GetOpType(opDef);
  76. dst.arch = X86_FP32;
  77. MS_ASSERT(OpFactory::GetInstance() != nullptr);
  78. op = OpFactory::GetInstance()->GetOp(inputs, outputs, opDef, ctx, dst);
  79. if (op == nullptr) {
  80. MS_LOGE("Can't find opName: %s, type: %s ", id.c_str(), type.c_str());
  81. return RET_ERROR;
  82. }
  83. return RET_OK;
  84. }
  85. int Node::Run(const Context &ctx) {
  86. MS_LOGD("%s run start", id.c_str());
  87. auto ret = MallocOutput(ctx);
  88. if (ret != RET_OK) {
  89. MS_LOGE("MallocOutput failed: %d", ret);
  90. return ret;
  91. }
  92. if (op == nullptr) {
  93. MS_LOGE("op is nullptr.");
  94. return RET_ERROR;
  95. }
  96. ret = op->Execute(inputs, outputs);
  97. if (ret != RET_OK) {
  98. return ret;
  99. }
  100. FreeInput();
  101. return RET_OK;
  102. }
  103. int Node::MallocOutput(const Context &ctx) {
  104. size_t refCount = outEdges.size();
  105. for (auto tensor : outputs) {
  106. if (tensor == nullptr) {
  107. MS_LOGE("tensor in outputs is nullptr");
  108. return RET_ERROR;
  109. }
  110. auto ret = tensor->MallocData(ctx.allocator, refCount);
  111. if (ret != RET_OK) {
  112. return ret;
  113. }
  114. }
  115. return RET_OK;
  116. }
  117. void Node::FreeInput() {
  118. for (auto tensor : inputs) {
  119. if (tensor == nullptr) {
  120. MS_LOGW("tensor in inputs is nullptr");
  121. return;
  122. }
  123. if (tensor->RefCount() != MSConst_WEIGHT_REFCOUNT) {
  124. tensor->FreeData();
  125. }
  126. }
  127. }
  128. } // namespace predict
  129. } // namespace mindspore