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 8.9 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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::vector<std::string> COMMUNI_PARALLEL_MODE_LIST = {ALL_GROUP_PARALLEL, SAME_SERVER_GROUP_PARALLEL,
  31. NO_GROUP_PARALLEL};
  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() { Reset(); }
  40. void ParallelContext::Reset() {
  41. gradients_mean_ = false;
  42. full_batch_ = false;
  43. gradient_fp32_sync_ = true;
  44. loss_repeated_mean_ = true;
  45. device_num_ = 1;
  46. global_rank_ = 0;
  47. device_num_is_set_ = false;
  48. global_rank_is_set_ = false;
  49. parallel_mode_ = STAND_ALONE;
  50. parameter_broadcast_ = false;
  51. parameter_broadcast_is_set_ = false;
  52. enable_all_reduce_fusion_ = false;
  53. strategy_ckpt_load_file_ = "";
  54. strategy_ckpt_save_file_ = "";
  55. enable_parallel_optimizer_ = false;
  56. all_reduce_fusion_split_indices_.clear();
  57. all_reduce_fusion_split_sizes_.clear();
  58. strategy_search_mode_ = DYNAMIC_PROGRAMMING;
  59. pipeline_stage_split_num_ = 1;
  60. grad_accumulation_step_ = 1;
  61. communi_parallel_mode_ = ALL_GROUP_PARALLEL;
  62. }
  63. void ParallelContext::set_device_num(int64_t device_num) {
  64. device_num_ = device_num;
  65. device_num_is_set_ = true;
  66. }
  67. void ParallelContext::set_global_rank(int64_t global_rank) {
  68. global_rank_ = global_rank;
  69. global_rank_is_set_ = true;
  70. }
  71. void ParallelContext::set_gradients_mean(bool gradients_mean) { gradients_mean_ = gradients_mean; }
  72. void ParallelContext::set_full_batch(bool full_batch) { full_batch_ = full_batch; }
  73. void ParallelContext::set_grad_accumulation_step(int64_t grad_accumulation_step) {
  74. grad_accumulation_step_ = grad_accumulation_step;
  75. }
  76. void ParallelContext::set_gradient_fp32_sync(bool gradient_fp32_sync) { gradient_fp32_sync_ = gradient_fp32_sync; }
  77. void ParallelContext::set_loss_repeated_mean(bool loss_repeated_mean) { loss_repeated_mean_ = loss_repeated_mean; }
  78. void ParallelContext::set_pipeline_stage_split_num(const int64_t stage_num) { pipeline_stage_split_num_ = stage_num; }
  79. bool ParallelContext::set_parallel_mode(const std::string &parallel_mode) {
  80. auto iter = std::find(PARALLEL_MODE_LIST.begin(), PARALLEL_MODE_LIST.end(), parallel_mode);
  81. if (iter == PARALLEL_MODE_LIST.end()) {
  82. MS_LOG(INFO) << "Invalid parallel mode:" << parallel_mode;
  83. return false;
  84. }
  85. parallel_mode_ = parallel_mode;
  86. return true;
  87. }
  88. bool ParallelContext::set_strategy_search_mode(const std::string &strategy_search_mode) {
  89. auto iter = std::find(STRATEGY_SEARCH_MODE_LIST.begin(), STRATEGY_SEARCH_MODE_LIST.end(), strategy_search_mode);
  90. if (iter == STRATEGY_SEARCH_MODE_LIST.end()) {
  91. MS_LOG(INFO) << "Invalid strategy search mode mode: " << strategy_search_mode;
  92. return false;
  93. }
  94. strategy_search_mode_ = strategy_search_mode;
  95. return true;
  96. }
  97. void ParallelContext::set_parameter_broadcast(bool parameter_broadcast) {
  98. parameter_broadcast_ = parameter_broadcast;
  99. parameter_broadcast_is_set_ = true;
  100. }
  101. void ParallelContext::set_strategy_ckpt_load_file(const std::string &strategy_ckpt_load_file) {
  102. strategy_ckpt_load_file_ = strategy_ckpt_load_file;
  103. }
  104. void ParallelContext::set_strategy_ckpt_save_file(const std::string &strategy_ckpt_save_file) {
  105. strategy_ckpt_save_file_ = strategy_ckpt_save_file;
  106. }
  107. void ParallelContext::set_group_ckpt_save_file(const std::string &group_ckpt_save_file) {
  108. group_ckpt_save_file_ = group_ckpt_save_file;
  109. }
  110. void ParallelContext::SetAllReduceFusionSplitIndices(const std::vector<uint32_t> indices, const std::string &group) {
  111. all_reduce_fusion_split_indices_[group] = indices;
  112. }
  113. const std::vector<uint32_t> ParallelContext::GetAllReduceFusionSplitIndices(const std::string &group) const {
  114. auto iter = all_reduce_fusion_split_indices_.find(group);
  115. if (iter != all_reduce_fusion_split_indices_.end()) {
  116. return iter->second;
  117. }
  118. return {};
  119. }
  120. void ParallelContext::SetAllReduceFusionSplitSizes(const std::vector<uint32_t> sizes, const std::string &group) {
  121. all_reduce_fusion_split_sizes_[group] = sizes;
  122. }
  123. const std::vector<uint32_t> ParallelContext::GetAllReduceFusionSplitSizes(const std::string &group) const {
  124. auto iter = all_reduce_fusion_split_sizes_.find(group);
  125. if (iter != all_reduce_fusion_split_sizes_.end()) {
  126. return iter->second;
  127. }
  128. return {};
  129. }
  130. bool ParallelContext::set_communi_parallel_mode(const std::string &communi_parallel_mode) {
  131. auto iter = std::find(COMMUNI_PARALLEL_MODE_LIST.begin(), COMMUNI_PARALLEL_MODE_LIST.end(), communi_parallel_mode);
  132. if (iter == COMMUNI_PARALLEL_MODE_LIST.end()) {
  133. MS_LOG(INFO) << "Invalid communication parallel mode:" << communi_parallel_mode;
  134. return false;
  135. }
  136. communi_parallel_mode_ = communi_parallel_mode;
  137. return true;
  138. }
  139. // Clear param_shapes before training in auto-parallel or semi-auto-parallel mode
  140. void ParallelContext::ParallelParameterContextInitShape(const FuncGraphPtr &func_graph) {
  141. MS_EXCEPTION_IF_NULL(func_graph);
  142. if (!func_graph->has_flag(AUTO_PARALLEL)) {
  143. return;
  144. }
  145. if (!func_graph->has_flag(TRAINING)) {
  146. init_param_shape_ = false;
  147. MS_LOG(INFO) << "In parallel evaluation or prediction, may be need to restore the parameter shape";
  148. return;
  149. }
  150. if ((ParallelContext::GetInstance()->grad_accumulation_step() > 1) && !func_graph->has_flag(ACCUMULATION)) {
  151. init_param_shape_ = false;
  152. MS_LOG(INFO) << "In parallel grad accumulation second graph, need to restore the parameter shape";
  153. } else {
  154. param_shapes.clear();
  155. init_param_shape_ = true;
  156. MS_LOG(INFO) << "Init the parameter shape dict";
  157. }
  158. }
  159. // Restore the parameters' shape for evaluation/prediction in auto-parallel or semi-auto-parallel mode
  160. void ParallelContext::ParallelParameterContextRestoreShape(const FuncGraphPtr &func_graph,
  161. const ParameterPtr &param_node, AbstractBasePtr ptr) {
  162. MS_EXCEPTION_IF_NULL(func_graph);
  163. MS_EXCEPTION_IF_NULL(param_node);
  164. MS_EXCEPTION_IF_NULL(ptr);
  165. if (!func_graph->has_flag(AUTO_PARALLEL)) {
  166. return;
  167. }
  168. if (init_param_shape_) {
  169. return;
  170. }
  171. auto iter = param_shapes.find(param_node->name());
  172. if (iter == param_shapes.end()) {
  173. MS_LOG(WARNING) << "Can not found the shape for parameter " << param_node->name();
  174. return;
  175. }
  176. Shape shape = iter->second;
  177. std::shared_ptr<abstract::BaseShape> base_shape = std::make_shared<abstract::Shape>(shape);
  178. ptr->set_shape(base_shape);
  179. MS_LOG(INFO) << "The parameter name is " << param_node->name() << ", the shape is " << shape;
  180. }
  181. // Clear param_shapes before training in auto-parallel or semi-auto-parallel mode
  182. // Checkpoint the parameters' shape for training in auto-parallel or semi-auto-parallel mode
  183. void ParallelContext::ParallelParameterContextCkptShape(const FuncGraphPtr &func_graph, const ParameterPtr &param_node,
  184. const AbstractBasePtr &ptr) {
  185. MS_EXCEPTION_IF_NULL(func_graph);
  186. MS_EXCEPTION_IF_NULL(param_node);
  187. MS_EXCEPTION_IF_NULL(ptr);
  188. if (!func_graph->has_flag(AUTO_PARALLEL)) {
  189. return;
  190. }
  191. if (!init_param_shape_) {
  192. return;
  193. }
  194. std::vector<int64_t> shape = dyn_cast<abstract::Shape>(ptr->GetShapeTrack())->shape();
  195. auto ret = param_shapes.try_emplace(param_node->name(), shape);
  196. if (!ret.second) {
  197. MS_LOG(EXCEPTION) << "The shape for parameter name " << param_node->name() << " is existed";
  198. return;
  199. }
  200. MS_LOG(DEBUG) << "The parameter name is " << param_node->name() << ", the shape is " << shape;
  201. }
  202. } // namespace parallel
  203. } // namespace mindspore