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_loader.h 5.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * Copyright 2020 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 DATASET_ENGINE_GNN_GRAPH_LOADER_H_
  17. #define DATASET_ENGINE_GNN_GRAPH_LOADER_H_
  18. #include <deque>
  19. #include <memory>
  20. #include <queue>
  21. #include <string>
  22. #include <vector>
  23. #include <unordered_map>
  24. #include <unordered_set>
  25. #include "dataset/core/data_type.h"
  26. #include "dataset/core/tensor.h"
  27. #include "dataset/engine/gnn/feature.h"
  28. #include "dataset/engine/gnn/graph.h"
  29. #include "dataset/engine/gnn/node.h"
  30. #include "dataset/engine/gnn/edge.h"
  31. #include "dataset/util/status.h"
  32. #include "mindrecord/include/shard_reader.h"
  33. namespace mindspore {
  34. namespace dataset {
  35. namespace gnn {
  36. using mindrecord::ShardReader;
  37. using NodeIdMap = std::unordered_map<NodeIdType, std::shared_ptr<Node>>;
  38. using EdgeIdMap = std::unordered_map<EdgeIdType, std::shared_ptr<Edge>>;
  39. using NodeTypeMap = std::unordered_map<NodeType, std::vector<NodeIdType>>;
  40. using EdgeTypeMap = std::unordered_map<EdgeType, std::vector<EdgeIdType>>;
  41. using NodeFeatureMap = std::unordered_map<NodeType, std::unordered_set<FeatureType>>;
  42. using EdgeFeatureMap = std::unordered_map<EdgeType, std::unordered_set<FeatureType>>;
  43. using DefaultFeatureMap = std::unordered_map<FeatureType, std::shared_ptr<Feature>>;
  44. // this class interfaces with the underlying storage format (mindrecord)
  45. // it returns raw nodes and edges via GetNodesAndEdges
  46. // it is then the responsibility of graph to construct itself based on the nodes and edges
  47. // if needed, this class could become a base where each derived class handles a specific storage format
  48. class GraphLoader {
  49. public:
  50. explicit GraphLoader(std::string mr_filepath, int32_t num_workers = 4);
  51. ~GraphLoader() = default;
  52. // Init mindrecord and load everything into memory multi-threaded
  53. // @return Status - the status code
  54. Status InitAndLoad();
  55. // this function will query mindrecord and construct all nodes and edges
  56. // nodes and edges are added to map without any connection. That's because there nodes and edges are read in
  57. // random order. src_node and dst_node in Edge are node_id only with -1 as type.
  58. // features attached to each node and edge are expected to be filled correctly
  59. Status GetNodesAndEdges(NodeIdMap *, EdgeIdMap *, NodeTypeMap *, EdgeTypeMap *, NodeFeatureMap *, EdgeFeatureMap *,
  60. DefaultFeatureMap *);
  61. private:
  62. //
  63. // worker thread that reads mindrecord file
  64. // @param int32_t worker_id - id of each worker
  65. // @return Status - the status code
  66. Status WorkerEntry(int32_t worker_id);
  67. // Load a node based on 1 row of mindrecord, returns a shared_ptr<Node>
  68. // @param std::vector<uint8_t> &blob - contains data in blob field in mindrecord
  69. // @param mindrecord::json &jsn - contains raw data
  70. // @param std::shared_ptr<Node> *node - return value
  71. // @param NodeFeatureMap *feature_map -
  72. // @param DefaultFeatureMap *default_feature -
  73. // @return Status - the status code
  74. Status LoadNode(const std::vector<uint8_t> &blob, const mindrecord::json &jsn, std::shared_ptr<Node> *node,
  75. NodeFeatureMap *feature_map, DefaultFeatureMap *default_feature);
  76. // @param std::vector<uint8_t> &blob - contains data in blob field in mindrecord
  77. // @param mindrecord::json &jsn - contains raw data
  78. // @param std::shared_ptr<Edge> *edge - return value, the edge ptr, edge is not yet connected
  79. // @param FeatureMap *feature_map
  80. // @param DefaultFeatureMap *default_feature -
  81. // @return Status - the status code
  82. Status LoadEdge(const std::vector<uint8_t> &blob, const mindrecord::json &jsn, std::shared_ptr<Edge> *edge,
  83. EdgeFeatureMap *feature_map, DefaultFeatureMap *default_feature);
  84. // @param std::string key - column name
  85. // @param std::vector<uint8_t> &blob - contains data in blob field in mindrecord
  86. // @param mindrecord::json &jsn - contains raw data
  87. // @param std::vector<int32_t> *ind - return value, list of feature index in int32_t
  88. // @return Status - the status code
  89. Status LoadFeatureIndex(const std::string &key, const std::vector<uint8_t> &blob, const mindrecord::json &jsn,
  90. std::vector<int32_t> *ind);
  91. // @param std::string &key - column name
  92. // @param std::vector<uint8_t> &blob - contains data in blob field in mindrecord
  93. // @param mindrecord::json &jsn - contains raw data
  94. // @param std::shared_ptr<Tensor> *tensor - return value feature tensor
  95. // @return Status - the status code
  96. Status LoadFeatureTensor(const std::string &key, const std::vector<uint8_t> &blob, const mindrecord::json &jsn,
  97. std::shared_ptr<Tensor> *tensor);
  98. // merge NodeFeatureMap and EdgeFeatureMap of each worker into 1
  99. void MergeFeatureMaps(NodeFeatureMap *, EdgeFeatureMap *, DefaultFeatureMap *);
  100. const int32_t num_workers_;
  101. std::atomic_int row_id_;
  102. std::string mr_path_;
  103. std::unique_ptr<ShardReader> shard_reader_;
  104. std::vector<std::deque<std::shared_ptr<Node>>> n_deques_;
  105. std::vector<std::deque<std::shared_ptr<Edge>>> e_deques_;
  106. std::vector<NodeFeatureMap> n_feature_maps_;
  107. std::vector<EdgeFeatureMap> e_feature_maps_;
  108. std::vector<DefaultFeatureMap> default_feature_maps_;
  109. const std::vector<std::string> keys_;
  110. };
  111. } // namespace gnn
  112. } // namespace dataset
  113. } // namespace mindspore
  114. #endif // DATASET_ENGINE_GNN_GRAPH_LOADER_H_