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.

gnn_graph_test.cc 14 kB

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. #include <algorithm>
  17. #include <string>
  18. #include <map>
  19. #include <memory>
  20. #include <unordered_set>
  21. #include "common/common.h"
  22. #include "gtest/gtest.h"
  23. #include "minddata/dataset/util/status.h"
  24. #include "minddata/dataset/engine/gnn/node.h"
  25. #include "minddata/dataset/engine/gnn/graph_data_impl.h"
  26. #include "minddata/dataset/engine/gnn/graph_loader.h"
  27. using namespace mindspore::dataset;
  28. using namespace mindspore::dataset::gnn;
  29. #define print_int_vec(_i, _str) \
  30. do { \
  31. std::stringstream ss; \
  32. std::copy(_i.begin(), _i.end(), std::ostream_iterator<int>(ss, " ")); \
  33. MS_LOG(INFO) << _str << " " << ss.str(); \
  34. } while (false)
  35. class MindDataTestGNNGraph : public UT::Common {
  36. protected:
  37. MindDataTestGNNGraph() = default;
  38. using NumNeighborsMap = std::map<NodeIdType, uint32_t>;
  39. using NodeNeighborsMap = std::map<NodeIdType, NumNeighborsMap>;
  40. void ParsingNeighbors(const std::shared_ptr<Tensor> &neighbors, NodeNeighborsMap &node_neighbors) {
  41. auto shape_vec = neighbors->shape().AsVector();
  42. uint32_t num_members = 1;
  43. for (size_t i = 1; i < shape_vec.size(); ++i) {
  44. num_members *= shape_vec[i];
  45. }
  46. uint32_t index = 0;
  47. NodeIdType src_node = 0;
  48. for (auto node_itr = neighbors->begin<NodeIdType>(); node_itr != neighbors->end<NodeIdType>();
  49. ++node_itr, ++index) {
  50. if (index % num_members == 0) {
  51. src_node = *node_itr;
  52. continue;
  53. }
  54. auto src_node_itr = node_neighbors.find(src_node);
  55. if (src_node_itr == node_neighbors.end()) {
  56. node_neighbors[src_node] = {{*node_itr, 1}};
  57. } else {
  58. auto nei_itr = src_node_itr->second.find(*node_itr);
  59. if (nei_itr == src_node_itr->second.end()) {
  60. src_node_itr->second[*node_itr] = 1;
  61. } else {
  62. src_node_itr->second[*node_itr] += 1;
  63. }
  64. }
  65. }
  66. }
  67. void CheckNeighborsRatio(const NumNeighborsMap &number_neighbors, const std::vector<WeightType> &weights,
  68. float deviation_ratio = 0.2) {
  69. EXPECT_EQ(number_neighbors.size(), weights.size());
  70. int index = 0;
  71. uint32_t pre_num = 0;
  72. WeightType pre_weight = 1;
  73. for (auto neighbor : number_neighbors) {
  74. if (pre_num != 0) {
  75. float target_ratio = static_cast<float>(pre_weight) / static_cast<float>(weights[index]);
  76. float current_ratio = static_cast<float>(pre_num) / static_cast<float>(neighbor.second);
  77. float target_upper = target_ratio * (1 + deviation_ratio);
  78. float target_lower = target_ratio * (1 - deviation_ratio);
  79. MS_LOG(INFO) << "current_ratio:" << std::to_string(current_ratio)
  80. << " target_upper:" << std::to_string(target_upper)
  81. << " target_lower:" << std::to_string(target_lower);
  82. EXPECT_LE(current_ratio, target_upper);
  83. EXPECT_GE(current_ratio, target_lower);
  84. }
  85. pre_num = neighbor.second;
  86. pre_weight = weights[index];
  87. ++index;
  88. }
  89. }
  90. };
  91. TEST_F(MindDataTestGNNGraph, TestGetAllNeighbors) {
  92. std::string path = "data/mindrecord/testGraphData/testdata";
  93. GraphDataImpl graph(path, 1);
  94. Status s = graph.Init();
  95. EXPECT_TRUE(s.IsOk());
  96. MetaInfo meta_info;
  97. s = graph.GetMetaInfo(&meta_info);
  98. EXPECT_TRUE(s.IsOk());
  99. EXPECT_TRUE(meta_info.node_type.size() == 2);
  100. std::shared_ptr<Tensor> nodes;
  101. s = graph.GetAllNodes(meta_info.node_type[0], &nodes);
  102. EXPECT_TRUE(s.IsOk());
  103. std::vector<NodeIdType> node_list;
  104. for (auto itr = nodes->begin<NodeIdType>(); itr != nodes->end<NodeIdType>(); ++itr) {
  105. node_list.push_back(*itr);
  106. if (node_list.size() >= 10) {
  107. break;
  108. }
  109. }
  110. std::shared_ptr<Tensor> neighbors;
  111. s = graph.GetAllNeighbors(node_list, meta_info.node_type[1], &neighbors);
  112. EXPECT_TRUE(s.IsOk());
  113. EXPECT_TRUE(neighbors->shape().ToString() == "<10,6>");
  114. TensorRow features;
  115. s = graph.GetNodeFeature(nodes, meta_info.node_feature_type, &features);
  116. EXPECT_TRUE(s.IsOk());
  117. EXPECT_TRUE(features.size() == 4);
  118. EXPECT_TRUE(features[0]->shape().ToString() == "<10,5>");
  119. EXPECT_TRUE(features[0]->ToString() ==
  120. "Tensor (shape: <10,5>, Type: int32)\n"
  121. "[[0,1,0,0,0],[1,0,0,0,1],[0,0,1,1,0],[0,0,0,0,0],[1,1,0,1,0],[0,0,0,0,1],[0,1,0,0,0],[0,0,0,1,1],[0,1,1,"
  122. "0,0],[0,1,0,1,0]]");
  123. EXPECT_TRUE(features[1]->shape().ToString() == "<10>");
  124. EXPECT_TRUE(features[1]->ToString() ==
  125. "Tensor (shape: <10>, Type: float32)\n[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1]");
  126. EXPECT_TRUE(features[2]->shape().ToString() == "<10>");
  127. EXPECT_TRUE(features[2]->ToString() == "Tensor (shape: <10>, Type: int32)\n[1,2,3,1,4,3,5,3,5,4]");
  128. }
  129. TEST_F(MindDataTestGNNGraph, TestGetSampledNeighbors) {
  130. std::string path = "data/mindrecord/testGraphData/testdata";
  131. GraphDataImpl graph(path, 1);
  132. Status s = graph.Init();
  133. EXPECT_TRUE(s.IsOk());
  134. MetaInfo meta_info;
  135. s = graph.GetMetaInfo(&meta_info);
  136. EXPECT_TRUE(s.IsOk());
  137. EXPECT_TRUE(meta_info.node_type.size() == 2);
  138. std::shared_ptr<Tensor> edges;
  139. s = graph.GetAllEdges(meta_info.edge_type[0], &edges);
  140. EXPECT_TRUE(s.IsOk());
  141. std::vector<EdgeIdType> edge_list;
  142. edge_list.resize(edges->Size());
  143. std::transform(edges->begin<EdgeIdType>(), edges->end<EdgeIdType>(), edge_list.begin(),
  144. [](const EdgeIdType edge) { return edge; });
  145. TensorRow edge_features;
  146. s = graph.GetEdgeFeature(edges, meta_info.edge_feature_type, &edge_features);
  147. EXPECT_TRUE(s.IsOk());
  148. EXPECT_TRUE(edge_features[0]->ToString() ==
  149. "Tensor (shape: <40>, Type: int32)\n"
  150. "[0,1,0,0,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0]");
  151. EXPECT_TRUE(edge_features[1]->ToString() ==
  152. "Tensor (shape: <40>, Type: float32)\n"
  153. "[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2,2.1,2.2,2.3,2.4,2.5,2.6,2."
  154. "7,2.8,2.9,3,3.1,3.2,3.3,3.4,3.5,3.6,3.7,3.8,3.9,4]");
  155. std::shared_ptr<Tensor> nodes;
  156. s = graph.GetNodesFromEdges(edge_list, &nodes);
  157. EXPECT_TRUE(s.IsOk());
  158. std::unordered_set<NodeIdType> node_set;
  159. std::vector<NodeIdType> node_list;
  160. int index = 0;
  161. for (auto itr = nodes->begin<NodeIdType>(); itr != nodes->end<NodeIdType>(); ++itr) {
  162. index++;
  163. if (index % 2 == 0) {
  164. continue;
  165. }
  166. node_set.emplace(*itr);
  167. if (node_set.size() >= 5) {
  168. break;
  169. }
  170. }
  171. node_list.resize(node_set.size());
  172. std::transform(node_set.begin(), node_set.end(), node_list.begin(), [](const NodeIdType node) { return node; });
  173. std::shared_ptr<Tensor> neighbors;
  174. {
  175. MS_LOG(INFO) << "Test random sampling.";
  176. NodeNeighborsMap number_neighbors;
  177. int count = 0;
  178. while (count < 1000) {
  179. neighbors.reset();
  180. s = graph.GetSampledNeighbors(node_list, {10}, {meta_info.node_type[1]}, SamplingStrategy::kRandom, &neighbors);
  181. EXPECT_TRUE(s.IsOk());
  182. EXPECT_TRUE(neighbors->shape().ToString() == "<5,11>");
  183. ParsingNeighbors(neighbors, number_neighbors);
  184. ++count;
  185. }
  186. CheckNeighborsRatio(number_neighbors[103], {1, 1, 1, 1, 1});
  187. }
  188. {
  189. MS_LOG(INFO) << "Test edge weight sampling.";
  190. NodeNeighborsMap number_neighbors;
  191. int count = 0;
  192. while (count < 1000) {
  193. neighbors.reset();
  194. s =
  195. graph.GetSampledNeighbors(node_list, {10}, {meta_info.node_type[1]}, SamplingStrategy::kEdgeWeight, &neighbors);
  196. EXPECT_TRUE(s.IsOk());
  197. EXPECT_TRUE(neighbors->shape().ToString() == "<5,11>");
  198. ParsingNeighbors(neighbors, number_neighbors);
  199. ++count;
  200. }
  201. CheckNeighborsRatio(number_neighbors[103], {3, 5, 6, 7, 8});
  202. }
  203. neighbors.reset();
  204. s = graph.GetSampledNeighbors(node_list, {2, 3}, {meta_info.node_type[1], meta_info.node_type[0]},
  205. SamplingStrategy::kRandom, &neighbors);
  206. EXPECT_TRUE(s.IsOk());
  207. EXPECT_TRUE(neighbors->shape().ToString() == "<5,9>");
  208. neighbors.reset();
  209. s = graph.GetSampledNeighbors(node_list, {2, 3, 4},
  210. {meta_info.node_type[1], meta_info.node_type[0], meta_info.node_type[1]},
  211. SamplingStrategy::kRandom, &neighbors);
  212. EXPECT_TRUE(s.IsOk());
  213. EXPECT_TRUE(neighbors->shape().ToString() == "<5,33>");
  214. neighbors.reset();
  215. s = graph.GetSampledNeighbors({}, {10}, {meta_info.node_type[1]}, SamplingStrategy::kRandom, &neighbors);
  216. EXPECT_TRUE(s.ToString().find("Input node_list is empty.") != std::string::npos);
  217. neighbors.reset();
  218. s = graph.GetSampledNeighbors({-1, 1}, {10}, {meta_info.node_type[1]}, SamplingStrategy::kRandom, &neighbors);
  219. EXPECT_TRUE(s.ToString().find("Invalid node id") != std::string::npos);
  220. neighbors.reset();
  221. s = graph.GetSampledNeighbors(node_list, {2, 50}, {meta_info.node_type[0], meta_info.node_type[1]},
  222. SamplingStrategy::kRandom, &neighbors);
  223. EXPECT_TRUE(s.ToString().find("Wrong samples number") != std::string::npos);
  224. neighbors.reset();
  225. s = graph.GetSampledNeighbors(node_list, {2}, {5}, SamplingStrategy::kRandom, &neighbors);
  226. EXPECT_TRUE(s.ToString().find("Invalid neighbor type") != std::string::npos);
  227. neighbors.reset();
  228. s = graph.GetSampledNeighbors(node_list, {2, 3, 4}, {meta_info.node_type[1], meta_info.node_type[0]},
  229. SamplingStrategy::kRandom, &neighbors);
  230. EXPECT_TRUE(s.ToString().find("The sizes of neighbor_nums and neighbor_types are inconsistent.") !=
  231. std::string::npos);
  232. neighbors.reset();
  233. s = graph.GetSampledNeighbors({301}, {10}, {meta_info.node_type[1]}, SamplingStrategy::kRandom, &neighbors);
  234. EXPECT_TRUE(s.ToString().find("Invalid node id:301") != std::string::npos);
  235. }
  236. TEST_F(MindDataTestGNNGraph, TestGetNegSampledNeighbors) {
  237. std::string path = "data/mindrecord/testGraphData/testdata";
  238. GraphDataImpl graph(path, 1);
  239. Status s = graph.Init();
  240. EXPECT_TRUE(s.IsOk());
  241. MetaInfo meta_info;
  242. s = graph.GetMetaInfo(&meta_info);
  243. EXPECT_TRUE(s.IsOk());
  244. EXPECT_TRUE(meta_info.node_type.size() == 2);
  245. std::shared_ptr<Tensor> nodes;
  246. s = graph.GetAllNodes(meta_info.node_type[0], &nodes);
  247. EXPECT_TRUE(s.IsOk());
  248. std::vector<NodeIdType> node_list;
  249. for (auto itr = nodes->begin<NodeIdType>(); itr != nodes->end<NodeIdType>(); ++itr) {
  250. node_list.push_back(*itr);
  251. if (node_list.size() >= 10) {
  252. break;
  253. }
  254. }
  255. std::shared_ptr<Tensor> neg_neighbors;
  256. s = graph.GetNegSampledNeighbors(node_list, 3, meta_info.node_type[1], &neg_neighbors);
  257. EXPECT_TRUE(s.IsOk());
  258. EXPECT_TRUE(neg_neighbors->shape().ToString() == "<10,4>");
  259. neg_neighbors.reset();
  260. s = graph.GetNegSampledNeighbors({}, 3, meta_info.node_type[1], &neg_neighbors);
  261. EXPECT_TRUE(s.ToString().find("Input node_list is empty.") != std::string::npos);
  262. neg_neighbors.reset();
  263. s = graph.GetNegSampledNeighbors({-1, 1}, 3, meta_info.node_type[1], &neg_neighbors);
  264. EXPECT_TRUE(s.ToString().find("Invalid node id") != std::string::npos);
  265. neg_neighbors.reset();
  266. s = graph.GetNegSampledNeighbors(node_list, 50, meta_info.node_type[1], &neg_neighbors);
  267. EXPECT_TRUE(s.ToString().find("Wrong samples number") != std::string::npos);
  268. neg_neighbors.reset();
  269. s = graph.GetNegSampledNeighbors(node_list, 3, 3, &neg_neighbors);
  270. EXPECT_TRUE(s.ToString().find("Invalid neighbor type") != std::string::npos);
  271. }
  272. TEST_F(MindDataTestGNNGraph, TestRandomWalk) {
  273. std::string path = "data/mindrecord/testGraphData/sns";
  274. GraphDataImpl graph(path, 1);
  275. Status s = graph.Init();
  276. EXPECT_TRUE(s.IsOk());
  277. MetaInfo meta_info;
  278. s = graph.GetMetaInfo(&meta_info);
  279. EXPECT_TRUE(s.IsOk());
  280. std::shared_ptr<Tensor> nodes;
  281. s = graph.GetAllNodes(meta_info.node_type[0], &nodes);
  282. EXPECT_TRUE(s.IsOk());
  283. std::vector<NodeIdType> node_list;
  284. for (auto itr = nodes->begin<NodeIdType>(); itr != nodes->end<NodeIdType>(); ++itr) {
  285. node_list.push_back(*itr);
  286. }
  287. print_int_vec(node_list, "node list ");
  288. std::vector<NodeType> meta_path(59, 1);
  289. std::shared_ptr<Tensor> walk_path;
  290. s = graph.RandomWalk(node_list, meta_path, 2.0, 0.5, -1, &walk_path);
  291. EXPECT_TRUE(s.IsOk());
  292. EXPECT_TRUE(walk_path->shape().ToString() == "<33,60>");
  293. }
  294. TEST_F(MindDataTestGNNGraph, TestRandomWalkDefaults) {
  295. std::string path = "data/mindrecord/testGraphData/sns";
  296. GraphDataImpl graph(path, 1);
  297. Status s = graph.Init();
  298. EXPECT_TRUE(s.IsOk());
  299. MetaInfo meta_info;
  300. s = graph.GetMetaInfo(&meta_info);
  301. EXPECT_TRUE(s.IsOk());
  302. std::shared_ptr<Tensor> nodes;
  303. s = graph.GetAllNodes(meta_info.node_type[0], &nodes);
  304. EXPECT_TRUE(s.IsOk());
  305. std::vector<NodeIdType> node_list;
  306. for (auto itr = nodes->begin<NodeIdType>(); itr != nodes->end<NodeIdType>(); ++itr) {
  307. node_list.push_back(*itr);
  308. }
  309. print_int_vec(node_list, "node list ");
  310. std::vector<NodeType> meta_path(59, 1);
  311. std::shared_ptr<Tensor> walk_path;
  312. s = graph.RandomWalk(node_list, meta_path, 1.0, 1.0, -1, &walk_path);
  313. EXPECT_TRUE(s.IsOk());
  314. EXPECT_TRUE(walk_path->shape().ToString() == "<33,60>");
  315. }