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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 <memory>
  20. #include <map>
  21. #include <string>
  22. #include <vector>
  23. #include "frontend/parallel/ops_info/ops_utils.h"
  24. #include "frontend/parallel/status.h"
  25. #include "utils/convert_utils.h"
  26. #include "ir/anf.h"
  27. #include "ir/func_graph.h"
  28. #include "utils/info.h"
  29. #include "abstract/abstract_value.h"
  30. namespace mindspore {
  31. namespace parallel {
  32. constexpr char STAND_ALONE[] = "stand_alone";
  33. constexpr char DATA_PARALLEL[] = "data_parallel";
  34. constexpr char HYBRID_PARALLEL[] = "hybrid_parallel";
  35. constexpr char AUTO_PARALLEL[] = "auto_parallel";
  36. constexpr char SEMI_AUTO_PARALLEL[] = "semi_auto_parallel";
  37. constexpr char DYNAMIC_PROGRAMMING[] = "dynamic_programming";
  38. constexpr char RECURSIVE_PROGRAMMING[] = "recursive_programming";
  39. constexpr char TRAINING[] = "training";
  40. class ParallelContext {
  41. public:
  42. ~ParallelContext() = default;
  43. ParallelContext(const ParallelContext &) = delete;
  44. ParallelContext &operator=(const ParallelContext &) = delete;
  45. static std::shared_ptr<ParallelContext> GetInstance();
  46. void set_mirror_mean(bool mirror_mean);
  47. bool mirror_mean() const { return mirror_mean_; }
  48. void set_full_batch(bool full_batch);
  49. bool full_batch() const { return full_batch_; }
  50. void set_cast_before_mirror(bool cast_before_mirror);
  51. bool cast_before_mirror() const { return cast_before_mirror_; }
  52. void set_loss_repeated_mean(bool loss_repeated_mean);
  53. bool loss_repeated_mean() const { return loss_repeated_mean_; }
  54. void set_device_num(int32_t device_num);
  55. int32_t device_num() const { return device_num_; }
  56. void set_global_rank(int32_t global_rank);
  57. int32_t global_rank() const { return global_rank_; }
  58. void set_communication_backend(const std::string &communication_backend);
  59. std::string communication_backend() const { return communication_backend_; }
  60. bool set_parallel_mode(const std::string &parallel_mode);
  61. std::string parallel_mode() const { return parallel_mode_; }
  62. bool set_strategy_search_mode(const std::string &strategy_search_mode);
  63. std::string strategy_search_mode() const { return strategy_search_mode_; }
  64. void set_parameter_broadcast(bool parameter_broadcast);
  65. bool parameter_broadcast() const { return parameter_broadcast_; }
  66. bool device_num_is_set() const { return device_num_is_set_; }
  67. bool global_rank_is_set() const { return global_rank_is_set_; }
  68. bool parameter_broadcast_is_set() const { return parameter_broadcast_is_set_; }
  69. void SetAllReduceFusionSplitIndices(const std::vector<uint32_t> indices, const std::string &group);
  70. const std::vector<uint32_t> GetAllReduceFusionSplitIndices(const std::string &group) const;
  71. void SetAllReduceFusionSplitSizes(const std::vector<uint32_t> sizes, const std::string &group);
  72. const std::vector<uint32_t> GetAllReduceFusionSplitSizes(const std::string &group) const;
  73. void set_enable_all_reduce_fusion(bool enable_all_reduce_fusion) {
  74. enable_all_reduce_fusion_ = enable_all_reduce_fusion;
  75. }
  76. bool enable_all_reduce_fusion() const { return enable_all_reduce_fusion_; }
  77. void set_strategy_ckpt_load_file(const std::string &strategy_ckpt_load_file);
  78. std::string strategy_ckpt_load_file() const { return strategy_ckpt_load_file_; }
  79. void set_strategy_ckpt_save_file(const std::string &strategy_ckpt_save_file);
  80. std::string strategy_ckpt_save_file() const { return strategy_ckpt_save_file_; }
  81. void set_enable_parallel_optimizer(bool enable_parallel_optimizer) {
  82. enable_parallel_optimizer_ = enable_parallel_optimizer;
  83. }
  84. bool enable_parallel_optimizer() const { return enable_parallel_optimizer_; }
  85. void Reset();
  86. private:
  87. ParallelContext();
  88. static std::shared_ptr<ParallelContext> inst_context_;
  89. bool mirror_mean_;
  90. bool full_batch_;
  91. bool cast_before_mirror_;
  92. bool loss_repeated_mean_;
  93. int32_t device_num_;
  94. int32_t global_rank_;
  95. std::string communication_backend_;
  96. std::string parallel_mode_;
  97. std::string strategy_search_mode_;
  98. bool parameter_broadcast_;
  99. bool device_num_is_set_;
  100. bool global_rank_is_set_;
  101. bool parameter_broadcast_is_set_;
  102. bool enable_all_reduce_fusion_;
  103. std::map<std::string, std::vector<uint32_t>> all_reduce_fusion_split_indices_;
  104. std::map<std::string, std::vector<uint32_t>> all_reduce_fusion_split_sizes_;
  105. std::string strategy_ckpt_load_file_;
  106. std::string strategy_ckpt_save_file_;
  107. bool enable_parallel_optimizer_;
  108. };
  109. void ParallelParameterContextInit(const FuncGraphPtr &func_graph);
  110. void ParallelParameterContextRestoreInNoTraining(const FuncGraphPtr &func_graph, const ParameterPtr &param_node,
  111. AbstractBasePtr ptr);
  112. void ParallelParameterContextCkptInTraining(const FuncGraphPtr &func_graph, const ParameterPtr &param_node,
  113. const AbstractBasePtr &ptr);
  114. } // namespace parallel
  115. } // namespace mindspore
  116. #endif // MINDSPORE_CCSRC_FRONTEND_PARALLEL_CONTEXT_H_