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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 "parallel/context.h"
  17. #include <algorithm>
  18. #include <cstdint>
  19. #include <functional>
  20. #include <memory>
  21. #include <numeric>
  22. #include <utility>
  23. #include "common/utils.h"
  24. #include "parallel/device_manager.h"
  25. namespace mindspore {
  26. namespace parallel {
  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::shared_ptr<ParallelContext> ParallelContext::inst_context_ = nullptr;
  31. std::shared_ptr<ParallelContext> ParallelContext::GetInstance() {
  32. if (inst_context_ == nullptr) {
  33. inst_context_.reset(new (std::nothrow) ParallelContext());
  34. }
  35. return inst_context_;
  36. }
  37. ParallelContext::ParallelContext() { Reset(); }
  38. void ParallelContext::Reset() {
  39. mirror_mean_ = false;
  40. cast_before_mirror_ = true;
  41. loss_repeated_mean_ = true;
  42. device_num_ = 1;
  43. global_rank_ = 0;
  44. communication_backend_ = HCCL_BACKEND;
  45. device_num_is_set_ = false;
  46. global_rank_is_set_ = false;
  47. parallel_mode_ = STAND_ALONE;
  48. parameter_broadcast_ = false;
  49. parameter_broadcast_is_set_ = false;
  50. }
  51. void ParallelContext::set_device_num(int32_t device_num) {
  52. device_num_ = device_num;
  53. device_num_is_set_ = true;
  54. }
  55. void ParallelContext::set_global_rank(int32_t global_rank) {
  56. global_rank_ = global_rank;
  57. global_rank_is_set_ = true;
  58. }
  59. void ParallelContext::set_mirror_mean(bool mirror_mean) { mirror_mean_ = mirror_mean; }
  60. void ParallelContext::set_cast_before_mirror(bool cast_before_mirror) { cast_before_mirror_ = cast_before_mirror; }
  61. void ParallelContext::set_loss_repeated_mean(bool loss_repeated_mean) { loss_repeated_mean_ = loss_repeated_mean; }
  62. void ParallelContext::set_communication_backend(const std::string &communication_backend) {
  63. communication_backend_ = communication_backend;
  64. }
  65. bool ParallelContext::set_parallel_mode(const std::string &parallel_mode) {
  66. auto iter = std::find(PARALLEL_MODE_LIST.begin(), PARALLEL_MODE_LIST.end(), parallel_mode);
  67. if (iter == PARALLEL_MODE_LIST.end()) {
  68. MS_LOG(INFO) << "Invalid parallel mode:" << parallel_mode;
  69. return false;
  70. }
  71. parallel_mode_ = parallel_mode;
  72. return true;
  73. }
  74. bool ParallelContext::set_strategy_search_mode(const std::string &strategy_search_mode) {
  75. auto iter = std::find(STRATEGY_SEARCH_MODE_LIST.begin(), STRATEGY_SEARCH_MODE_LIST.end(), strategy_search_mode);
  76. if (iter == STRATEGY_SEARCH_MODE_LIST.end()) {
  77. MS_LOG(INFO) << "Invalid strategy search mode mode: " << strategy_search_mode;
  78. return false;
  79. }
  80. strategy_search_mode_ = strategy_search_mode;
  81. return true;
  82. }
  83. void ParallelContext::set_parameter_broadcast(bool parameter_broadcast) {
  84. parameter_broadcast_ = parameter_broadcast;
  85. parameter_broadcast_is_set_ = true;
  86. }
  87. void ParallelContext::set_all_reduce_fusion_split_indices(const std::vector<uint32_t> indices) {
  88. all_reduce_fusion_split_indices_ = indices;
  89. }
  90. const std::vector<uint32_t> ParallelContext::all_reduce_fusion_split_indices() const {
  91. return all_reduce_fusion_split_indices_;
  92. }
  93. void ParallelContext::set_all_reduce_fusion_split_sizes(const std::vector<uint32_t> sizes) {
  94. all_reduce_fusion_split_sizes_ = sizes;
  95. }
  96. const std::vector<uint32_t> ParallelContext::all_reduce_fusion_split_sizes() const {
  97. return all_reduce_fusion_split_sizes_;
  98. }
  99. } // namespace parallel
  100. } // namespace mindspore