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.

transpose_info.cc 8.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 "parallel/ops_info/transpose_info.h"
  17. #include <memory>
  18. #include <vector>
  19. #include "parallel/device_manager.h"
  20. #include "parallel/device_matrix.h"
  21. #include "parallel/step_parallel.h"
  22. #include "utils/convert_utils.h"
  23. #include "utils/log_adapter.h"
  24. namespace mindspore {
  25. namespace parallel {
  26. Status TransposeInfo::CheckStrategy(const StrategyPtr &strategy) {
  27. if (CheckStrategyValue(strategy, inputs_shape_, is_auto_parallel_) != SUCCESS) {
  28. if (is_auto_parallel_) {
  29. MS_LOG(DEBUG) << name_ << ": Invalid strategy.";
  30. } else {
  31. MS_LOG(ERROR) << name_ << ": Invalid strategy.";
  32. }
  33. return FAILED;
  34. }
  35. return SUCCESS;
  36. }
  37. Status TransposeInfo::InferDevMatrixShape() {
  38. std::vector<Dimensions> stra = strategy_->GetInputDim();
  39. input_strategy_ = stra.at(0);
  40. for (auto &iter : input_strategy_) {
  41. dev_matrix_shape_.push_back(iter);
  42. }
  43. return SUCCESS;
  44. }
  45. // there is no Parameter for Transpose Primitive, so no need to do all reduce
  46. Status TransposeInfo::InferMirrorOps() { return SUCCESS; }
  47. // there is no reduction dimension for forward computation of Transpose Primitive, so no need to do all reduce
  48. Status TransposeInfo::InferForwardCommunication() { return SUCCESS; }
  49. /*
  50. * get perm input of Transpose Primitive
  51. * perm is a permutation of the dimensions of input
  52. * the result is saved in axis_v_
  53. */
  54. Status TransposeInfo::ComputeAxis() {
  55. if (input_value_[1] == nullptr) {
  56. MS_LOG(ERROR) << name_ << ": input_value_[1] is nullptr.";
  57. return FAILED;
  58. }
  59. std::vector<ValuePtr> elements;
  60. ValueTuplePtr dim_tuple = input_value_[1]->cast<ValueTuplePtr>();
  61. if (dim_tuple == nullptr) {
  62. MS_LOG(ERROR) << name_ << ": input_value_[1] must be ValueTuplePtr.";
  63. return FAILED;
  64. }
  65. elements = dim_tuple->value();
  66. if (elements.size() != inputs_shape_[0].size()) {
  67. MS_LOG(ERROR) << name_ << ": elements size must equal to inputs shape 0 size.";
  68. return FAILED;
  69. }
  70. axis_v_.clear();
  71. for (auto &element : elements) {
  72. MS_EXCEPTION_IF_NULL(element);
  73. if (element->isa<Int32Imm>()) {
  74. int32_t axis = element->cast<Int32ImmPtr>()->value();
  75. axis_v_.push_back(axis);
  76. } else {
  77. MS_LOG(ERROR) << name_ << ": The value of axis must be int32.";
  78. return FAILED;
  79. }
  80. }
  81. for (int32_t i = 0; i < SizeToInt(axis_v_.size()); i++) {
  82. auto iter = std::find(axis_v_.begin(), axis_v_.end(), i);
  83. if (iter == axis_v_.end()) {
  84. MS_LOG(ERROR) << name_ << ": axis_v_ must be a permutation.";
  85. }
  86. }
  87. return SUCCESS;
  88. }
  89. // the output tensor map is the permutation of input tensor map, the permutation is axis_v
  90. Status TransposeInfo::InferTensorMap() {
  91. if ((inputs_shape_.size() != 1) || (outputs_shape_.size() != 1)) {
  92. MS_LOG(ERROR) << name_ << ": inputs_shape_ and outputs_shape_ size must be 1, inputs shape and outputs shape is "
  93. << inputs_shape_.size() << ", " << outputs_shape_.size();
  94. return FAILED;
  95. }
  96. std::vector<int32_t> tensor_map_index_input;
  97. for (size_t j = 0; j < inputs_shape_[0].size(); ++j) {
  98. tensor_map_index_input.push_back(SizeToInt(inputs_shape_[0].size() - j - 1));
  99. }
  100. inputs_tensor_map_.push_back(tensor_map_index_input);
  101. std::vector<int32_t> tensor_map_index_output = tensor_map_index_input;
  102. for (uint32_t i = 0; i < tensor_map_index_output.size(); i++) {
  103. tensor_map_index_output[i] = tensor_map_index_input[IntToUint(axis_v_[i])];
  104. }
  105. outputs_tensor_map_.push_back(tensor_map_index_output);
  106. return SUCCESS;
  107. }
  108. // the output tensor strategy is the permutation of input tensor strategy, the permutation is axis_v
  109. Strategys TransposeInfo::GetOutputsStrategy() {
  110. Strategys outputs_strategy;
  111. std::vector<int32_t> strategy = input_strategy_;
  112. for (uint32_t i = 0; i < strategy.size(); i++) {
  113. strategy[i] = input_strategy_[IntToUint(axis_v_[i])];
  114. }
  115. outputs_strategy.push_back(strategy);
  116. return outputs_strategy;
  117. }
  118. Status TransposeInfo::InferTensorLayout(TensorLayouts *inputs_layout, TensorLayouts *outputs_layout) {
  119. if ((inputs_layout == nullptr) || (outputs_layout == nullptr)) {
  120. MS_LOG(ERROR) << name_ << ": InferTensorLayout: the layout is null.";
  121. return FAILED;
  122. }
  123. Shape shape_in = inputs_shape_.at(0);
  124. TensorMap tensor_map_in = inputs_tensor_map_.at(0);
  125. Shape shape_out = outputs_shape_.at(0);
  126. TensorMap tensor_map_out = outputs_tensor_map_.at(0);
  127. TensorLayout tensor_layout_in, tensor_layout_out;
  128. if ((tensor_layout_in.InitFromVector(dev_matrix_shape_, tensor_map_in, shape_in) != SUCCESS) ||
  129. (tensor_layout_out.InitFromVector(dev_matrix_shape_, tensor_map_out, shape_out) != SUCCESS)) {
  130. return FAILED;
  131. }
  132. inputs_layout->push_back(tensor_layout_in);
  133. outputs_layout->push_back(tensor_layout_out);
  134. return SUCCESS;
  135. }
  136. Status TransposeInfo::InferTensorInfo() {
  137. Shapes inputs_slice_shape, outputs_slice_shape;
  138. Strategys inputs_strategy = strategy_->GetInputDim();
  139. Strategys outputs_strategy = GetOutputsStrategy();
  140. if (InferSliceShape(inputs_strategy, outputs_strategy, &inputs_slice_shape, &outputs_slice_shape) != SUCCESS) {
  141. return FAILED;
  142. }
  143. TensorLayouts inputs_layout, outputs_layout;
  144. if (InferTensorLayout(&inputs_layout, &outputs_layout) != SUCCESS) {
  145. return FAILED;
  146. }
  147. TensorLayout tensor_layout_in = inputs_layout.at(0);
  148. TensorLayout tensor_layout_out = outputs_layout.at(0);
  149. Shape shape_array_in = inputs_shape_.at(0);
  150. Shape slice_shape_in = inputs_slice_shape.at(0);
  151. Shape shape_array_out = outputs_shape_.at(0);
  152. Shape slice_shape_out = outputs_slice_shape.at(0);
  153. TensorInfo tensor_info_in(tensor_layout_in, shape_array_in, slice_shape_in);
  154. TensorInfo tensor_info_out(tensor_layout_out, shape_array_out, slice_shape_out);
  155. inputs_tensor_info_.push_back(tensor_info_in);
  156. outputs_tensor_info_.push_back(tensor_info_out);
  157. return SUCCESS;
  158. }
  159. // compute axis_v_ during this method
  160. Status TransposeInfo::GetAttrs() { return ComputeAxis(); }
  161. Status TransposeInfo::Init(const StrategyPtr &strategy) {
  162. if (InitWithAutoRepeatCalc(strategy) != SUCCESS) {
  163. MS_LOG(ERROR) << name_ << ": Init failed.";
  164. return FAILED;
  165. }
  166. MS_LOG(INFO) << name_ << ": Init success.";
  167. return SUCCESS;
  168. }
  169. Status TransposeInfo::InitForCostModel(const StrategyPtr &strategy) {
  170. if (InitForCostModelWithAutoRepeatCalc(strategy) != SUCCESS) {
  171. if (is_auto_parallel_) {
  172. MS_LOG(DEBUG) << name_ << ": Init for cost model failed.";
  173. } else {
  174. MS_LOG(ERROR) << name_ << ": Init for cost model failed.";
  175. }
  176. return FAILED;
  177. }
  178. MS_LOG(INFO) << name_ << ": Init for cost model success.";
  179. return SUCCESS;
  180. }
  181. Status TransposeInfo::SetCostUnderStrategy(const mindspore::parallel::StrategyPtr &strategy) {
  182. if (SetCostUnderStrategyBase(strategy) != SUCCESS) {
  183. if (is_auto_parallel_) {
  184. MS_LOG(ERROR) << name_ << ": Set cost under strategy failed.";
  185. } else {
  186. MS_LOG(ERROR) << name_ << ": Set cost under strategy failed.";
  187. }
  188. return FAILED;
  189. }
  190. return SUCCESS;
  191. }
  192. Status TransposeInfo::GenerateStrategies(int32_t stage_id) {
  193. if (GetAttrs() != SUCCESS) {
  194. MS_LOG(ERROR) << name_ << ": GetAttrs failed.";
  195. return FAILED;
  196. }
  197. if ((inputs_shape_.size() != 1) || (outputs_shape_.size() != 1)) {
  198. MS_LOG(ERROR) << name_ << ": inputs shape size or outputs shape size is wrong, " << inputs_shape_.size() << ", "
  199. << outputs_shape_.size();
  200. return FAILED;
  201. }
  202. is_auto_parallel_ = true;
  203. Shape input0_split(inputs_shape_[0].size(), 1);
  204. Shapes splittable_inputs = {input0_split};
  205. std::vector<StrategyPtr> sp_vector;
  206. if (GenerateStrategiesForIndependentInputs(stage_id, inputs_shape_, splittable_inputs, &sp_vector) != SUCCESS) {
  207. MS_LOG(ERROR) << name_ << ": GenerateStrategiesForIndependentInputs failed";
  208. return FAILED;
  209. }
  210. size_t success = 0;
  211. for (auto &sp : sp_vector) {
  212. if (SetCostUnderStrategy(sp) == SUCCESS) {
  213. success++;
  214. MS_LOG(INFO) << name_ << ": Successfully generated " << success << "strategy.";
  215. PrintStrategy(sp);
  216. }
  217. }
  218. return SUCCESS;
  219. }
  220. } // namespace parallel
  221. } // namespace mindspore