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.

virtual_dataset_test.cc 5.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 <string>
  17. #include <list>
  18. #include <vector>
  19. #include "common/common_test.h"
  20. #include "frontend/parallel/strategy.h"
  21. #include "frontend/parallel/ops_info/virtual_dataset_info.h"
  22. #include "frontend/parallel/device_manager.h"
  23. #include "frontend/parallel/step_parallel.h"
  24. namespace mindspore {
  25. namespace parallel {
  26. class VirtualDatasetInfo;
  27. using VirtualDatasetInfoPtr = std::shared_ptr<VirtualDatasetInfo>;
  28. VirtualDatasetInfoPtr virtual_dataset;
  29. class TestVirtualDatasetInfo : public UT::Common {
  30. public:
  31. TestVirtualDatasetInfo() {}
  32. void SetUp();
  33. void TearDown() {}
  34. };
  35. void TestVirtualDatasetInfo::SetUp() {
  36. std::vector<int32_t> dev_list;
  37. for (int32_t i = 0; i < 130; i++) {
  38. dev_list.push_back(i);
  39. }
  40. std::vector<int32_t> stage_map;
  41. stage_map.push_back(16);
  42. stage_map.push_back(114);
  43. int32_t local_dev = 0;
  44. // create a new g_device_manager
  45. g_device_manager = std::make_shared<DeviceManager>();
  46. g_device_manager->Init(dev_list, local_dev, stage_map, "hccl");
  47. std::unordered_map<std::string, ValuePtr> attr;
  48. Shapes inputs_shape = {{128, 32}, {1280, 320}, {12800, 3200}};
  49. Shapes outputs_shape = {{128, 32}, {1280, 320}, {12800, 3200}};
  50. virtual_dataset = std::make_shared<VirtualDatasetInfo>("virtual_dataset_info", inputs_shape, outputs_shape, attr);
  51. }
  52. TEST_F(TestVirtualDatasetInfo, InferDevMatrixShape1) {
  53. std::vector<Dimensions> inputs = {{16, 1}, {16, 1}, {16, 1}};
  54. StrategyPtr strategy = NewStrategy(0, inputs);
  55. virtual_dataset->Init(strategy);
  56. std::vector<int32_t> dev_matrix_shape = virtual_dataset->dev_matrix_shape();
  57. std::vector<int32_t> expect = {16};
  58. ASSERT_EQ(dev_matrix_shape, expect);
  59. }
  60. TEST_F(TestVirtualDatasetInfo, InferDevMatrixShape2) {
  61. std::vector<Dimensions> inputs = {{8, 1}, {8, 1}, {8, 1}};
  62. StrategyPtr strategy = NewStrategy(0, inputs);
  63. virtual_dataset->Init(strategy);
  64. std::vector<int32_t> dev_matrix_shape = virtual_dataset->dev_matrix_shape();
  65. std::vector<int32_t> expect = {8, 2};
  66. ASSERT_EQ(dev_matrix_shape, expect);
  67. }
  68. TEST_F(TestVirtualDatasetInfo, InferSliceShape1) {
  69. std::vector<Dimensions> str = {{8, 1}, {8, 1}, {8, 1}};
  70. StrategyPtr strategy = NewStrategy(0, str);
  71. virtual_dataset->Init(strategy);
  72. std::vector<TensorInfo> inputs = virtual_dataset->inputs_tensor_info();
  73. std::vector<TensorInfo> outputs = virtual_dataset->outputs_tensor_info();
  74. Shape input_slice_shape_expect = {16, 32};
  75. Shape output_slice_shape_expect = {16, 32};
  76. TensorInfo input_tensor_info = inputs.at(0);
  77. TensorInfo output_tensor_info = outputs.at(0);
  78. Shape input_slice_shape = input_tensor_info.slice_shape();
  79. Shape output_slice_shape = output_tensor_info.slice_shape();
  80. ASSERT_EQ(input_slice_shape, input_slice_shape_expect);
  81. ASSERT_EQ(output_slice_shape, output_slice_shape_expect);
  82. Shape input_slice_shape_expect1 = {160, 320};
  83. Shape output_slice_shape_expect1 = {160, 320};
  84. TensorInfo input_tensor_info1 = inputs.at(1);
  85. TensorInfo output_tensor_info1 = outputs.at(1);
  86. Shape input_slice_shape1 = input_tensor_info1.slice_shape();
  87. Shape output_slice_shape1 = output_tensor_info1.slice_shape();
  88. ASSERT_EQ(input_slice_shape1, input_slice_shape_expect1);
  89. ASSERT_EQ(output_slice_shape1, output_slice_shape_expect1);
  90. Shape input_slice_shape_expect2 = {1600, 3200};
  91. Shape output_slice_shape_expect2 = {1600, 3200};
  92. TensorInfo input_tensor_info2 = inputs.at(2);
  93. TensorInfo output_tensor_info2 = outputs.at(2);
  94. Shape input_slice_shape2 = input_tensor_info2.slice_shape();
  95. Shape output_slice_shape2 = output_tensor_info2.slice_shape();
  96. ASSERT_EQ(input_slice_shape2, input_slice_shape_expect2);
  97. ASSERT_EQ(output_slice_shape2, output_slice_shape_expect2);
  98. }
  99. TEST_F(TestVirtualDatasetInfo, GetTensorLayout1) {
  100. std::vector<Dimensions> str = {{8, 1}, {8, 1}, {8, 1}};
  101. StrategyPtr strategy = NewStrategy(0, str);
  102. virtual_dataset->Init(strategy);
  103. std::vector<TensorInfo> inputs = virtual_dataset->inputs_tensor_info();
  104. std::vector<TensorInfo> outputs = virtual_dataset->outputs_tensor_info();
  105. TensorMap input_expect = {1, -1};
  106. TensorMap output_expect = {1, -1};
  107. TensorInfo input_tensor_info = inputs.at(0);
  108. TensorInfo output_tensor_info = outputs.at(0);
  109. Map input_tensor_map = input_tensor_info.tensor_layout().origin_tensor_map();
  110. Map output_tensor_map = output_tensor_info.tensor_layout().origin_tensor_map();
  111. ASSERT_EQ(input_tensor_map.array(), input_expect);
  112. ASSERT_EQ(output_tensor_map.array(), output_expect);
  113. }
  114. TEST_F(TestVirtualDatasetInfo, GetForwardOp1) {
  115. std::vector<Dimensions> inputs = {{8, 1}, {8, 1}, {8, 1}};
  116. StrategyPtr strategy = NewStrategy(0, inputs);
  117. virtual_dataset->Init(strategy);
  118. OperatorVector forward_op = virtual_dataset->forward_op();
  119. size_t size = forward_op.size();
  120. ASSERT_EQ(size, 0);
  121. }
  122. TEST_F(TestVirtualDatasetInfo, GetMirrorOPs1) {
  123. std::vector<Dimensions> inputs = {{8, 1}, {8, 1}, {8, 1}};
  124. StrategyPtr strategy = NewStrategy(0, inputs);
  125. virtual_dataset->Init(strategy);
  126. MirrorOps mirror_ops = virtual_dataset->mirror_ops();
  127. size_t size = mirror_ops.size();
  128. // no broadcast
  129. ASSERT_EQ(size, 0);
  130. // ASSERT_EQ(size, 3);
  131. }
  132. } // namespace parallel
  133. } // namespace mindspore