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

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 TRAINING[] = "training";
  41. constexpr char ACCUMULATION[] = "accumulation";
  42. constexpr char ALL_GROUP_PARALLEL[] = "all_group_parallel";
  43. constexpr char SAME_SERVER_GROUP_PARALLEL[] = "same_server_group_parallel";
  44. constexpr char NO_GROUP_PARALLEL[] = "no_group_parallel";
  45. constexpr char IS_FIRST_ITERATION[] = "is_first_iteration";
  46. class ParallelContext {
  47. public:
  48. ~ParallelContext() = default;
  49. ParallelContext(const ParallelContext &) = delete;
  50. ParallelContext &operator=(const ParallelContext &) = delete;
  51. static std::shared_ptr<ParallelContext> GetInstance();
  52. void set_gradients_mean(bool gradients_mean);
  53. bool gradients_mean() const { return gradients_mean_; }
  54. void set_full_batch(bool full_batch);
  55. bool full_batch() const { return full_batch_; }
  56. void set_dataset_strategy(const std::vector<std::vector<int64_t>> &dataset_strategy);
  57. std::vector<std::vector<int64_t>> dataset_strategy() const { return dataset_strategy_; }
  58. void set_gradient_fp32_sync(bool gradient_fp32_sync);
  59. bool gradient_fp32_sync() const { return gradient_fp32_sync_; }
  60. void set_loss_repeated_mean(bool loss_repeated_mean);
  61. bool loss_repeated_mean() const { return loss_repeated_mean_; }
  62. void set_device_num(int64_t device_num);
  63. int64_t device_num() const { return device_num_; }
  64. void set_pipeline_stage_split_num(const int64_t stages);
  65. int64_t pipeline_stage_split_num() const { return pipeline_stage_split_num_; }
  66. void set_global_rank(int64_t global_rank);
  67. int64_t global_rank() const { return global_rank_; }
  68. void set_grad_accumulation_step(int64_t grad_accumulation_step);
  69. int64_t grad_accumulation_step() const { return grad_accumulation_step_; }
  70. bool set_parallel_mode(const std::string &parallel_mode);
  71. std::string parallel_mode() const { return parallel_mode_; }
  72. bool set_strategy_search_mode(const std::string &strategy_search_mode);
  73. std::string strategy_search_mode() const { return strategy_search_mode_; }
  74. void set_parameter_broadcast(bool parameter_broadcast);
  75. bool parameter_broadcast() const { return parameter_broadcast_; }
  76. bool device_num_is_set() const { return device_num_is_set_; }
  77. bool global_rank_is_set() const { return global_rank_is_set_; }
  78. bool parameter_broadcast_is_set() const { return parameter_broadcast_is_set_; }
  79. void set_optimizer_weight_shard_size(int64_t optimizer_weight_shard_size);
  80. int64_t optimizer_weight_shard_size() const { return optimizer_weight_shard_size_; }
  81. void set_optimizer_weight_shard_aggregated_save(bool optimizer_weight_shard_aggregated_save);
  82. bool optimizer_weight_shard_aggregated_save() const { return optimizer_weight_shard_aggregated_save_; }
  83. void SetAllReduceFusionSplitIndices(const std::vector<uint32_t> &indices, const std::string &group);
  84. std::vector<uint32_t> GetAllReduceFusionSplitIndices(const std::string &group) const;
  85. void SetAllReduceFusionSplitSizes(const std::vector<uint32_t> &sizes, const std::string &group);
  86. std::vector<uint32_t> GetAllReduceFusionSplitSizes(const std::string &group) const;
  87. void set_enable_all_reduce_fusion(bool enable_all_reduce_fusion) {
  88. enable_all_reduce_fusion_ = enable_all_reduce_fusion;
  89. }
  90. bool enable_all_reduce_fusion() const { return enable_all_reduce_fusion_; }
  91. void set_strategy_ckpt_load_file(const std::string &strategy_ckpt_load_file);
  92. std::string strategy_ckpt_load_file() const { return strategy_ckpt_load_file_; }
  93. void set_strategy_ckpt_save_file(const std::string &strategy_ckpt_save_file);
  94. std::string strategy_ckpt_save_file() const { return strategy_ckpt_save_file_; }
  95. void set_group_ckpt_save_file(const std::string &group_ckpt_save_file);
  96. std::string group_ckpt_save_file() const { return group_ckpt_save_file_; }
  97. void set_enable_parallel_optimizer(bool enable_parallel_optimizer) {
  98. enable_parallel_optimizer_ = enable_parallel_optimizer;
  99. }
  100. bool enable_parallel_optimizer() const { return enable_parallel_optimizer_; }
  101. bool set_communi_parallel_mode(const std::string &communi_parallel_mode);
  102. std::string communi_parallel_mode() const { return communi_parallel_mode_; }
  103. void set_sharding_propagation(const bool);
  104. bool sharding_propagation() const { return sharding_propagation_; }
  105. void set_enable_all2all(const bool);
  106. bool enable_all2all() const { return enable_all2all_; }
  107. void Reset();
  108. void ParallelParameterContextInitShape(const FuncGraphPtr &func_graph);
  109. void ParallelParameterContextRestoreShape(const FuncGraphPtr &func_graph, const ParameterPtr &param_node,
  110. AbstractBasePtr ptr);
  111. void ParallelParameterContextCkptShape(const FuncGraphPtr &func_graph, const ParameterPtr &param_node,
  112. const AbstractBasePtr &ptr);
  113. private:
  114. ParallelContext();
  115. static std::shared_ptr<ParallelContext> inst_context_;
  116. bool gradients_mean_;
  117. bool full_batch_;
  118. bool gradient_fp32_sync_;
  119. bool loss_repeated_mean_;
  120. int64_t device_num_;
  121. int64_t global_rank_;
  122. int64_t grad_accumulation_step_;
  123. std::string parallel_mode_;
  124. std::string strategy_search_mode_;
  125. int64_t pipeline_stage_split_num_;
  126. bool parameter_broadcast_;
  127. bool device_num_is_set_;
  128. bool global_rank_is_set_;
  129. bool parameter_broadcast_is_set_;
  130. bool enable_all_reduce_fusion_;
  131. std::map<std::string, std::vector<uint32_t>> all_reduce_fusion_split_indices_;
  132. std::map<std::string, std::vector<uint32_t>> all_reduce_fusion_split_sizes_;
  133. std::string strategy_ckpt_load_file_;
  134. std::string strategy_ckpt_save_file_;
  135. std::string group_ckpt_save_file_;
  136. bool enable_parallel_optimizer_;
  137. bool init_param_shape_;
  138. std::string communi_parallel_mode_;
  139. int64_t optimizer_weight_shard_size_;
  140. bool optimizer_weight_shard_aggregated_save_;
  141. // In AUTO_PARALLEL mode, 'sharding_propagation_' = True indicates that sharding-configured operators
  142. // will propagate the sharding strategies to other operators with minimum redistribution cost.
  143. bool sharding_propagation_;
  144. // Enable AllToAll or not. If false, use AllGather and Split.
  145. bool enable_all2all_;
  146. std::vector<std::vector<int64_t>> dataset_strategy_;
  147. };
  148. } // namespace parallel
  149. } // namespace mindspore
  150. #endif // MINDSPORE_CCSRC_FRONTEND_PARALLEL_CONTEXT_H_