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.

tmpidentity_test.cc 6.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 "common/common_test.h"
  17. #include "frontend/parallel/strategy.h"
  18. #include "frontend/parallel/device_manager.h"
  19. #include "frontend/parallel/ops_info/operator_info.h"
  20. #include "frontend/parallel/ops_info/tmp_identity_info.h"
  21. #include "frontend/parallel/step_parallel.h"
  22. namespace mindspore {
  23. namespace parallel {
  24. class TmpIdentityInfo;
  25. using TmpIdentityInfoPtr = std::shared_ptr<TmpIdentityInfo>;
  26. TmpIdentityInfoPtr identity_ptr;
  27. class TestTmpIdentityInfo : public UT::Common {
  28. public:
  29. TestTmpIdentityInfo() { identity_ptr2 = nullptr; }
  30. void SetUp();
  31. void TearDown() {}
  32. TmpIdentityInfoPtr identity_ptr2;
  33. };
  34. void TestTmpIdentityInfo::SetUp() {
  35. RankList dev_list;
  36. for (int32_t i = 0; i < 1050; i++) {
  37. dev_list.push_back(i);
  38. }
  39. RankList stage_map;
  40. stage_map.push_back(1024);
  41. stage_map.push_back(26);
  42. int32_t local_dev = 0;
  43. // create a new g_device_manager
  44. g_device_manager = std::make_shared<DeviceManager>();
  45. g_device_manager->Init(dev_list, local_dev, stage_map, "hccl");
  46. std::unordered_map<std::string, ValuePtr> attr = {};
  47. Shapes inputs_shape = {{2, 4, 8, 16}};
  48. Shapes outputs_shape = {{2, 4, 8, 16}};
  49. identity_ptr = std::make_shared<TmpIdentityInfo>(inputs_shape, outputs_shape, attr);
  50. Shapes inputs_shape2 = {{4, 16, 8, 16}};
  51. Shapes outputs_shape2 = {{4, 16, 8, 16}};
  52. identity_ptr2 = std::make_shared<TmpIdentityInfo>(inputs_shape2, outputs_shape2, attr);
  53. }
  54. TEST_F(TestTmpIdentityInfo, InferDevMatrixShape1) {
  55. Strategys inputs = {{2, 4, 8, 16}};
  56. StrategyPtr strategy = NewStrategy(0, inputs);
  57. identity_ptr->Init(strategy);
  58. Shape dev_matrix_shape = identity_ptr->dev_matrix_shape();
  59. Shape expect = {2, 4, 8, 16};
  60. ASSERT_EQ(dev_matrix_shape, expect);
  61. }
  62. TEST_F(TestTmpIdentityInfo, InferSliceShape1) {
  63. Strategys str = {{2, 4, 8, 16}};
  64. StrategyPtr strategy = NewStrategy(0, str);
  65. identity_ptr->Init(strategy);
  66. std::vector<TensorInfo> inputs = identity_ptr->inputs_tensor_info();
  67. std::vector<TensorInfo> outputs = identity_ptr->outputs_tensor_info();
  68. Shape input_slice_shape_expect = {1, 1, 1, 1};
  69. Shape output_slice_shape_expect = {1, 1, 1, 1};
  70. TensorInfo input_tensor_info = inputs.at(0);
  71. TensorInfo output_tensor_info = outputs.at(0);
  72. Shape input_slice_shape = input_tensor_info.slice_shape();
  73. Shape output_slice_shape = output_tensor_info.slice_shape();
  74. ASSERT_EQ(input_slice_shape, input_slice_shape_expect);
  75. ASSERT_EQ(output_slice_shape, output_slice_shape_expect);
  76. }
  77. TEST_F(TestTmpIdentityInfo, GetTensorLayout1) {
  78. Strategys str = {{2, 4, 8, 16}};
  79. StrategyPtr strategy = NewStrategy(0, str);
  80. identity_ptr->Init(strategy);
  81. std::vector<TensorInfo> inputs = identity_ptr->inputs_tensor_info();
  82. std::vector<TensorInfo> outputs = identity_ptr->outputs_tensor_info();
  83. TensorMap input_expect = {3, 2, 1, 0};
  84. TensorMap output_expect = {3, 2, 1, 0};
  85. TensorInfo input_tensor_info = inputs.at(0);
  86. TensorInfo output_tensor_info = outputs.at(0);
  87. Map input_tensor_map = input_tensor_info.tensor_layout().origin_tensor_map();
  88. Map output_tensor_map = output_tensor_info.tensor_layout().origin_tensor_map();
  89. ASSERT_EQ(input_tensor_map.array(), input_expect);
  90. ASSERT_EQ(output_tensor_map.array(), output_expect);
  91. }
  92. TEST_F(TestTmpIdentityInfo, CheckStrategy1) {
  93. // Success: {{2,4,8,16}}
  94. Strategys inputs = {{2, 2, 8, 16}, {2, 4, 16, 1}};
  95. StrategyPtr strategy = NewStrategy(0, inputs);
  96. Status ret = identity_ptr->Init(strategy);
  97. ASSERT_EQ(ret, FAILED);
  98. }
  99. TEST_F(TestTmpIdentityInfo, CheckStrategy2) {
  100. // Success: {{2,4,8,16}}
  101. Strategys inputs = {{2, 4, 8}};
  102. StrategyPtr strategy = NewStrategy(0, inputs);
  103. Status ret = identity_ptr->Init(strategy);
  104. ASSERT_EQ(ret, FAILED);
  105. }
  106. TEST_F(TestTmpIdentityInfo, test_generate_strategies) {
  107. ASSERT_EQ(identity_ptr->GenerateStrategies(0), Status::SUCCESS);
  108. std::vector<std::shared_ptr<StrategyWithCost>> sc = identity_ptr->GetStrategyCost();
  109. for (const auto& swc : sc) {
  110. StrategyPtr sp = swc->strategy_ptr;
  111. Cost cost = *(swc->cost_list[0]);
  112. identity_ptr->Init(sp);
  113. std::vector<TensorInfo> inputs_info = identity_ptr->inputs_tensor_info();
  114. std::vector<TensorInfo> outputs_info = identity_ptr->outputs_tensor_info();
  115. ASSERT_DOUBLE_EQ(identity_ptr->operator_cost()->GetComputationCost(inputs_info, outputs_info, sp->GetInputStage()),
  116. cost.computation_cost_);
  117. ASSERT_DOUBLE_EQ(identity_ptr->operator_cost()->GetCommCost(inputs_info, outputs_info, sp->GetInputStage()),
  118. cost.communication_cost_);
  119. }
  120. }
  121. TEST_F(TestTmpIdentityInfo, test_generate_strategies_base) {
  122. ASSERT_EQ(identity_ptr->GenerateStrategies(0), Status::SUCCESS);
  123. std::vector<std::shared_ptr<StrategyWithCost>> sc = identity_ptr->GetStrategyCost();
  124. Shapes splittable_inputs = {{1, 1, 1, 1}};
  125. std::vector<StrategyPtr> sp_vector;
  126. Shapes inputs_shape = {{2, 4, 8, 16}};
  127. GenerateStrategiesForIndependentInputs(0, inputs_shape, splittable_inputs, &sp_vector);
  128. ASSERT_EQ(sc.size(), sp_vector.size());
  129. }
  130. TEST_F(TestTmpIdentityInfo, test_generate_strategies_base2) {
  131. ASSERT_EQ(identity_ptr2->GenerateStrategies(0), Status::SUCCESS);
  132. std::vector<std::shared_ptr<StrategyWithCost>> sc = identity_ptr2->GetStrategyCost();
  133. Shapes splittable_inputs = {{1, 1, 1, 1}};
  134. std::vector<StrategyPtr> sp_vector;
  135. Shapes inputs_shape2 = {{4, 16, 8, 16}};
  136. GenerateStrategiesForIndependentInputs(0, inputs_shape2, splittable_inputs, &sp_vector);
  137. ASSERT_EQ(sc.size(), sp_vector.size());
  138. }
  139. } // namespace parallel
  140. } // namespace mindspore