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.

graph_util.h 2.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #ifndef PREDICT_COMMON_GRAPH_UTIL_H_
  17. #define PREDICT_COMMON_GRAPH_UTIL_H_
  18. #include <string>
  19. #include <unordered_map>
  20. #include <unordered_set>
  21. #include <utility>
  22. #include <vector>
  23. #include <memory>
  24. #include "common/utils.h"
  25. #include "schema/inner/ms_generated.h"
  26. namespace mindspore {
  27. namespace predict {
  28. using NODE_ID = std::string;
  29. class OpNode {
  30. public:
  31. explicit OpNode(NODE_ID nodeId) : id(std::move(nodeId)) {}
  32. NODE_ID ID();
  33. void AddInEdge(const NODE_ID &nodeId);
  34. void AddOutEdge(const NODE_ID &nodeId);
  35. std::unordered_set<NODE_ID> GetAllInEdge();
  36. std::unordered_set<NODE_ID> GetAllOutEdge();
  37. protected:
  38. NODE_ID id;
  39. std::unordered_set<NODE_ID> inEdges;
  40. std::unordered_set<NODE_ID> outEdges;
  41. };
  42. class OpGraph {
  43. public:
  44. OpGraph() = default;
  45. ~OpGraph();
  46. static OpGraph *Build(const SubGraphDef &subGraphDef);
  47. OpNode *GetNode(const NODE_ID &nodeId);
  48. OpNode *AddNode(const NODE_ID &nodeId);
  49. std::unordered_set<NODE_ID> GetInputNode();
  50. std::unordered_set<NODE_ID> GetOutputNode();
  51. private:
  52. int AddEdge(const NODE_ID &srcId, const NODE_ID &dstId);
  53. int AddEdge(const NodeDef &srcNodeDef, const flatbuffers::Vector<flatbuffers::Offset<NodeDef>> &nodeDefs);
  54. protected:
  55. std::unordered_map<NODE_ID, OpNode *> nodes;
  56. };
  57. } // namespace predict
  58. } // namespace mindspore
  59. #endif // PREDICT_COMMON_GRAPH_UTIL_H_