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.

context.cc 7.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "frontend/parallel/context.h"
  17. #include <algorithm>
  18. #include <cstdint>
  19. #include <functional>
  20. #include <memory>
  21. #include <numeric>
  22. #include <utility>
  23. #include <map>
  24. #include "utils/ms_utils.h"
  25. #include "frontend/parallel/device_manager.h"
  26. namespace mindspore {
  27. namespace parallel {
  28. static std::map<std::string, Shape> param_shapes;
  29. std::vector<std::string> PARALLEL_MODE_LIST = {STAND_ALONE, DATA_PARALLEL, HYBRID_PARALLEL, SEMI_AUTO_PARALLEL,
  30. AUTO_PARALLEL};
  31. std::vector<std::string> STRATEGY_SEARCH_MODE_LIST = {DYNAMIC_PROGRAMMING, RECURSIVE_PROGRAMMING};
  32. std::shared_ptr<ParallelContext> ParallelContext::inst_context_ = nullptr;
  33. std::shared_ptr<ParallelContext> ParallelContext::GetInstance() {
  34. if (inst_context_ == nullptr) {
  35. inst_context_.reset(new (std::nothrow) ParallelContext());
  36. }
  37. return inst_context_;
  38. }
  39. ParallelContext::ParallelContext() {
  40. communication_backend_ = HCCL_BACKEND;
  41. Reset();
  42. }
  43. void ParallelContext::Reset() {
  44. mirror_mean_ = false;
  45. full_batch_ = false;
  46. cast_before_mirror_ = true;
  47. loss_repeated_mean_ = true;
  48. device_num_ = 1;
  49. global_rank_ = 0;
  50. device_num_is_set_ = false;
  51. global_rank_is_set_ = false;
  52. parallel_mode_ = STAND_ALONE;
  53. parameter_broadcast_ = false;
  54. parameter_broadcast_is_set_ = false;
  55. enable_all_reduce_fusion_ = false;
  56. strategy_ckpt_load_file_ = "";
  57. strategy_ckpt_save_file_ = "";
  58. enable_parallel_optimizer_ = false;
  59. all_reduce_fusion_split_indices_.clear();
  60. all_reduce_fusion_split_sizes_.clear();
  61. }
  62. void ParallelContext::set_device_num(int32_t device_num) {
  63. device_num_ = device_num;
  64. device_num_is_set_ = true;
  65. }
  66. void ParallelContext::set_global_rank(int32_t global_rank) {
  67. global_rank_ = global_rank;
  68. global_rank_is_set_ = true;
  69. }
  70. void ParallelContext::set_mirror_mean(bool mirror_mean) { mirror_mean_ = mirror_mean; }
  71. void ParallelContext::set_full_batch(bool full_batch) { full_batch_ = full_batch; }
  72. void ParallelContext::set_cast_before_mirror(bool cast_before_mirror) { cast_before_mirror_ = cast_before_mirror; }
  73. void ParallelContext::set_loss_repeated_mean(bool loss_repeated_mean) { loss_repeated_mean_ = loss_repeated_mean; }
  74. void ParallelContext::set_communication_backend(const std::string &communication_backend) {
  75. communication_backend_ = communication_backend;
  76. }
  77. bool ParallelContext::set_parallel_mode(const std::string &parallel_mode) {
  78. auto iter = std::find(PARALLEL_MODE_LIST.begin(), PARALLEL_MODE_LIST.end(), parallel_mode);
  79. if (iter == PARALLEL_MODE_LIST.end()) {
  80. MS_LOG(INFO) << "Invalid parallel mode:" << parallel_mode;
  81. return false;
  82. }
  83. parallel_mode_ = parallel_mode;
  84. return true;
  85. }
  86. bool ParallelContext::set_strategy_search_mode(const std::string &strategy_search_mode) {
  87. auto iter = std::find(STRATEGY_SEARCH_MODE_LIST.begin(), STRATEGY_SEARCH_MODE_LIST.end(), strategy_search_mode);
  88. if (iter == STRATEGY_SEARCH_MODE_LIST.end()) {
  89. MS_LOG(INFO) << "Invalid strategy search mode mode: " << strategy_search_mode;
  90. return false;
  91. }
  92. strategy_search_mode_ = strategy_search_mode;
  93. return true;
  94. }
  95. void ParallelContext::set_parameter_broadcast(bool parameter_broadcast) {
  96. parameter_broadcast_ = parameter_broadcast;
  97. parameter_broadcast_is_set_ = true;
  98. }
  99. void ParallelContext::set_strategy_ckpt_load_file(const std::string &strategy_ckpt_load_file) {
  100. strategy_ckpt_load_file_ = strategy_ckpt_load_file;
  101. }
  102. void ParallelContext::set_strategy_ckpt_save_file(const std::string &strategy_ckpt_save_file) {
  103. strategy_ckpt_save_file_ = strategy_ckpt_save_file;
  104. }
  105. void ParallelContext::SetAllReduceFusionSplitIndices(const std::vector<uint32_t> indices, const std::string &group) {
  106. all_reduce_fusion_split_indices_[group] = indices;
  107. }
  108. const std::vector<uint32_t> ParallelContext::GetAllReduceFusionSplitIndices(const std::string &group) const {
  109. auto iter = all_reduce_fusion_split_indices_.find(group);
  110. if (iter != all_reduce_fusion_split_indices_.end()) {
  111. return iter->second;
  112. }
  113. return {};
  114. }
  115. void ParallelContext::SetAllReduceFusionSplitSizes(const std::vector<uint32_t> sizes, const std::string &group) {
  116. all_reduce_fusion_split_sizes_[group] = sizes;
  117. }
  118. const std::vector<uint32_t> ParallelContext::GetAllReduceFusionSplitSizes(const std::string &group) const {
  119. auto iter = all_reduce_fusion_split_sizes_.find(group);
  120. if (iter != all_reduce_fusion_split_sizes_.end()) {
  121. return iter->second;
  122. }
  123. return {};
  124. }
  125. // Clear param_shapes before training in auto-parallel or semi-auto-parallel mode
  126. void ParallelParameterContextInit(const FuncGraphPtr &func_graph) {
  127. MS_EXCEPTION_IF_NULL(func_graph);
  128. if (!func_graph->has_flag(AUTO_PARALLEL) || !func_graph->has_flag(TRAINING)) {
  129. return;
  130. }
  131. param_shapes.clear();
  132. }
  133. // Restore the parameters' shape for evaluation/prediction in auto-parallel or semi-auto-parallel mode
  134. void ParallelParameterContextRestoreInNoTraining(const FuncGraphPtr &func_graph, const ParameterPtr &param_node,
  135. AbstractBasePtr ptr) {
  136. MS_EXCEPTION_IF_NULL(func_graph);
  137. MS_EXCEPTION_IF_NULL(param_node);
  138. MS_EXCEPTION_IF_NULL(ptr);
  139. if (!func_graph->has_flag(AUTO_PARALLEL) || (func_graph->attrs().count(TRAINING) == 0) ||
  140. func_graph->has_flag(TRAINING)) {
  141. return;
  142. }
  143. auto iter = param_shapes.find(param_node->name());
  144. if (iter == param_shapes.end()) {
  145. MS_LOG(WARNING) << "Can not found the shape for parameter " << param_node->name();
  146. return;
  147. }
  148. Shape shape = iter->second;
  149. std::shared_ptr<abstract::BaseShape> base_shape = std::make_shared<abstract::Shape>(shape);
  150. ptr->set_shape(base_shape);
  151. MS_LOG(DEBUG) << "The parameter name is " << param_node->name() << ", the shape is " << shape;
  152. }
  153. // Checkpoint the parameters' shape for training in auto-parallel or semi-auto-parallel mode
  154. void ParallelParameterContextCkptInTraining(const FuncGraphPtr &func_graph, const ParameterPtr &param_node,
  155. const AbstractBasePtr &ptr) {
  156. MS_EXCEPTION_IF_NULL(func_graph);
  157. MS_EXCEPTION_IF_NULL(param_node);
  158. MS_EXCEPTION_IF_NULL(ptr);
  159. if (!func_graph->has_flag(AUTO_PARALLEL) || !func_graph->has_flag(TRAINING)) {
  160. return;
  161. }
  162. std::vector<int> shape_int = dyn_cast<abstract::Shape>(ptr->GetShapeTrack())->shape();
  163. Shape shape;
  164. (void)std::transform(shape_int.begin(), shape_int.end(), std::back_inserter(shape),
  165. [](const int &value) { return static_cast<int64_t>(value); });
  166. auto ret = param_shapes.try_emplace(param_node->name(), shape);
  167. if (!ret.second) {
  168. MS_LOG(EXCEPTION) << "The shape for parameter name " << param_node->name() << " is existed";
  169. return;
  170. }
  171. MS_LOG(DEBUG) << "The parameter name is " << param_node->name() << ", the shape is " << shape;
  172. }
  173. } // namespace parallel
  174. } // namespace mindspore