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.6 kB

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