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

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