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.h 8.3 kB

5 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**
  2. * Copyright 2019-2020 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. #ifndef MINDSPORE_CCSRC_FRONTEND_PARALLEL_CONTEXT_H_
  17. #define MINDSPORE_CCSRC_FRONTEND_PARALLEL_CONTEXT_H_
  18. #include <cstdint>
  19. #include <map>
  20. #include <memory>
  21. #include <string>
  22. #include <vector>
  23. #include "abstract/abstract_value.h"
  24. #include "frontend/parallel/ops_info/ops_utils.h"
  25. #include "frontend/parallel/status.h"
  26. #include "ir/anf.h"
  27. #include "ir/func_graph.h"
  28. #include "utils/convert_utils.h"
  29. #include "utils/info.h"
  30. #include "pipeline/jit/pipeline.h"
  31. namespace mindspore {
  32. namespace parallel {
  33. constexpr char STAND_ALONE[] = "stand_alone";
  34. constexpr char DATA_PARALLEL[] = "data_parallel";
  35. constexpr char HYBRID_PARALLEL[] = "hybrid_parallel";
  36. constexpr char AUTO_PARALLEL[] = "auto_parallel";
  37. constexpr char SEMI_AUTO_PARALLEL[] = "semi_auto_parallel";
  38. constexpr char DYNAMIC_PROGRAMMING[] = "dynamic_programming";
  39. constexpr char RECURSIVE_PROGRAMMING[] = "recursive_programming";
  40. constexpr char SHARDING_PROPAGATION[] = "sharding_propagation";
  41. constexpr char TRAINING[] = "training";
  42. constexpr char ACCUMULATION[] = "accumulation";
  43. constexpr char ALL_GROUP_PARALLEL[] = "all_group_parallel";
  44. constexpr char SAME_SERVER_GROUP_PARALLEL[] = "same_server_group_parallel";
  45. constexpr char NO_GROUP_PARALLEL[] = "no_group_parallel";
  46. constexpr char IS_FIRST_ITERATION[] = "is_first_iteration";
  47. class ParallelContext {
  48. public:
  49. ~ParallelContext() = default;
  50. ParallelContext(const ParallelContext &) = delete;
  51. ParallelContext &operator=(const ParallelContext &) = delete;
  52. static std::shared_ptr<ParallelContext> GetInstance();
  53. void set_gradients_mean(bool gradients_mean);
  54. bool gradients_mean() const { return gradients_mean_; }
  55. void set_full_batch(bool full_batch);
  56. bool full_batch() const { return full_batch_; }
  57. void set_dataset_strategy(const std::vector<std::vector<int64_t>> &dataset_strategy);
  58. std::vector<std::vector<int64_t>> dataset_strategy() const { return dataset_strategy_; }
  59. void set_gradient_fp32_sync(bool gradient_fp32_sync);
  60. bool gradient_fp32_sync() const { return gradient_fp32_sync_; }
  61. void set_loss_repeated_mean(bool loss_repeated_mean);
  62. bool loss_repeated_mean() const { return loss_repeated_mean_; }
  63. void set_device_num(int64_t device_num);
  64. int64_t device_num() const { return device_num_; }
  65. void set_pipeline_stage_split_num(const int64_t stages);
  66. int64_t pipeline_stage_split_num() const { return pipeline_stage_split_num_; }
  67. void set_global_rank(int64_t global_rank);
  68. int64_t global_rank() const { return global_rank_; }
  69. void set_grad_accumulation_step(int64_t grad_accumulation_step);
  70. int64_t grad_accumulation_step() const { return grad_accumulation_step_; }
  71. bool set_parallel_mode(const std::string &parallel_mode);
  72. std::string parallel_mode() const { return parallel_mode_; }
  73. bool set_strategy_search_mode(const std::string &strategy_search_mode);
  74. std::string strategy_search_mode() const { return strategy_search_mode_; }
  75. void set_parameter_broadcast(bool parameter_broadcast);
  76. bool parameter_broadcast() const { return parameter_broadcast_; }
  77. bool device_num_is_set() const { return device_num_is_set_; }
  78. bool global_rank_is_set() const { return global_rank_is_set_; }
  79. bool parameter_broadcast_is_set() const { return parameter_broadcast_is_set_; }
  80. void set_optimizer_weight_shard_size(int64_t optimizer_weight_shard_size);
  81. int64_t optimizer_weight_shard_size() const { return optimizer_weight_shard_size_; }
  82. void set_optimizer_weight_shard_aggregated_save(bool optimizer_weight_shard_aggregated_save);
  83. bool optimizer_weight_shard_aggregated_save() const { return optimizer_weight_shard_aggregated_save_; }
  84. void SetAllReduceFusionSplitIndices(const std::vector<uint32_t> &indices, const std::string &group);
  85. std::vector<uint32_t> GetAllReduceFusionSplitIndices(const std::string &group) const;
  86. void SetAllReduceFusionSplitSizes(const std::vector<uint32_t> &sizes, const std::string &group);
  87. std::vector<uint32_t> GetAllReduceFusionSplitSizes(const std::string &group) const;
  88. void set_enable_all_reduce_fusion(bool enable_all_reduce_fusion) {
  89. enable_all_reduce_fusion_ = enable_all_reduce_fusion;
  90. }
  91. bool enable_all_reduce_fusion() const { return enable_all_reduce_fusion_; }
  92. void set_strategy_ckpt_load_file(const std::string &strategy_ckpt_load_file);
  93. std::string strategy_ckpt_load_file() const { return strategy_ckpt_load_file_; }
  94. void set_strategy_ckpt_save_file(const std::string &strategy_ckpt_save_file);
  95. std::string strategy_ckpt_save_file() const { return strategy_ckpt_save_file_; }
  96. void set_group_ckpt_save_file(const std::string &group_ckpt_save_file);
  97. std::string group_ckpt_save_file() const { return group_ckpt_save_file_; }
  98. void set_enable_parallel_optimizer(bool enable_parallel_optimizer) {
  99. enable_parallel_optimizer_ = enable_parallel_optimizer;
  100. }
  101. bool enable_parallel_optimizer() const { return enable_parallel_optimizer_; }
  102. void set_hccl_test_available(bool hccl_test_available) { hccl_test_available_ = hccl_test_available; }
  103. bool hccl_test_available() const { return hccl_test_available_; }
  104. void set_grad_accumulation_shard(const bool grad_accumulation_shard) {
  105. grad_accumulation_shard_ = grad_accumulation_shard;
  106. }
  107. bool grad_accumulation_shard() const { return grad_accumulation_shard_; }
  108. bool set_communi_parallel_mode(const std::string &communi_parallel_mode);
  109. std::string communi_parallel_mode() const { return communi_parallel_mode_; }
  110. void set_enable_all2all(const bool);
  111. bool enable_all2all() const { return enable_all2all_; }
  112. void set_dataset_repeat_dim_right(const bool dataset_repeat_dim_right) {
  113. dataset_repeat_dim_right_ = dataset_repeat_dim_right;
  114. }
  115. bool dataset_repeat_dim_right() const { return dataset_repeat_dim_right_; }
  116. void Reset();
  117. void ParallelParameterContextInitShape(const FuncGraphPtr &func_graph);
  118. void ParallelParameterContextRestoreShape(const FuncGraphPtr &func_graph, const ParameterPtr &param_node,
  119. const AbstractBasePtr &ptr);
  120. void ParallelParameterContextCkptShape(const FuncGraphPtr &func_graph, const ParameterPtr &param_node,
  121. const AbstractBasePtr &ptr);
  122. void set_sharding_propagation(const bool);
  123. bool sharding_propagation() const { return sharding_propagation_; }
  124. private:
  125. ParallelContext();
  126. static std::shared_ptr<ParallelContext> inst_context_;
  127. bool gradients_mean_;
  128. bool full_batch_;
  129. bool gradient_fp32_sync_;
  130. bool loss_repeated_mean_;
  131. int64_t device_num_;
  132. int64_t global_rank_;
  133. int64_t grad_accumulation_step_;
  134. std::string parallel_mode_;
  135. std::string strategy_search_mode_;
  136. int64_t pipeline_stage_split_num_;
  137. bool parameter_broadcast_;
  138. bool device_num_is_set_;
  139. bool global_rank_is_set_;
  140. bool parameter_broadcast_is_set_;
  141. bool enable_all_reduce_fusion_;
  142. std::map<std::string, std::vector<uint32_t>> all_reduce_fusion_split_indices_;
  143. std::map<std::string, std::vector<uint32_t>> all_reduce_fusion_split_sizes_;
  144. std::string strategy_ckpt_load_file_;
  145. std::string strategy_ckpt_save_file_;
  146. std::string group_ckpt_save_file_;
  147. bool enable_parallel_optimizer_;
  148. bool init_param_shape_;
  149. std::string communi_parallel_mode_;
  150. int64_t optimizer_weight_shard_size_;
  151. bool optimizer_weight_shard_aggregated_save_;
  152. bool grad_accumulation_shard_;
  153. // Enable AllToAll or not. If false, use AllGather and Split.
  154. bool enable_all2all_;
  155. std::vector<std::vector<int64_t>> dataset_strategy_;
  156. bool dataset_repeat_dim_right_ = false;
  157. bool hccl_test_available_ = false;
  158. bool sharding_propagation_;
  159. };
  160. } // namespace parallel
  161. } // namespace mindspore
  162. #endif // MINDSPORE_CCSRC_FRONTEND_PARALLEL_CONTEXT_H_