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.

costmodel_context.cc 6.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 "frontend/parallel/costmodel_context.h"
  17. #include <memory>
  18. #include "frontend/parallel/allreduce_fusion/allreduce_fusion.h"
  19. #include "utils/ms_context.h"
  20. namespace mindspore {
  21. namespace parallel {
  22. std::shared_ptr<CostModelContext> CostModelContext::cm_context_inst_ = nullptr;
  23. std::shared_ptr<CostModelContext> CostModelContext::GetInstance() {
  24. if (cm_context_inst_ == nullptr) {
  25. MS_LOG(INFO) << "Create costmodel_context";
  26. cm_context_inst_.reset(new (std::nothrow) CostModelContext());
  27. }
  28. return cm_context_inst_;
  29. }
  30. CostModelContext::CostModelContext() {
  31. ResetCostModel();
  32. ResetAlgoParameters();
  33. }
  34. void CostModelContext::ResetCostModel() {
  35. device_memory_capacity_ = DEFAULT_DEVICE_MEMORY_CAPACITY;
  36. costmodel_alpha_ = DEFAULT_COST_MODEL_ALPHA;
  37. costmodel_beta_ = DEFAULT_COST_MODEL_BETA_ASCEND;
  38. costmodel_gamma_ = DEFAULT_COST_MODEL_GAMMA;
  39. costmodel_communi_threshold_ = DEFAULT_COST_MODEL_COMMUNI_THRESHOLD;
  40. costmodel_communi_const_ = DEFAULT_COST_MODEL_COMMUNI_CONST;
  41. costmodel_communi_bias_ = DEFAULT_COST_MODEL_COMMUNI_BIAS;
  42. is_multi_subgraphs_ = DEFAULT_IS_MULTI_SUBGRAPHS;
  43. run_phase_ = DEFAULT_RUN_PHASE;
  44. costmodel_allreduce_fusion_algorithm_ = DEFAULT_COST_MODEL_ALLREDUCE_FUSION_ALGORITHM;
  45. costmodel_allreduce_fusion_times_ = DEFAULT_COST_MODEL_ALLREDUCE_FUSION_TIMES;
  46. costmodel_allreduce_fusion_tail_percent_ = DEFAULT_COST_MODEL_ALLREDUCE_FUSION_TAIL_PERCENT;
  47. costmodel_allreduce_fusion_tail_time_ = DEFAULT_COST_MODEL_ALLREDUCE_FUSION_TAIL_TIME;
  48. costmodel_allreduce_fusion_allreduce_inherent_time_ = DEFAULT_COST_MODEL_ALLREDUCE_FUSION_ALLREDUCE_INHERENT_TIME;
  49. costmodel_allreduce_fusion_allreduce_bandwidth_ = DEFAULT_COST_MODEL_ALLREDUCE_FUSION_ALLREDUCE_BANDWIDTH;
  50. costmodel_allreduce_fusion_computation_time_parameter_ =
  51. DEFAULT_COST_MODEL_ALLREDUCE_FUSION_COMPUTATION_TIME_PARAMETER;
  52. dp_algo_single_loop_ = DEFAULT_DP_ALGO_SINGLE_LOOP;
  53. }
  54. void CostModelContext::ResetAlgoParameters() {
  55. costmodel_simplify_cal_ = DEFAULT_COST_MODEL_SIMPLIFY_CALCULATION;
  56. tensor_slice_alignment_enable_ = DEFAULT_TENSOR_SLICE_ALIGNMENT_ENABLE;
  57. tensor_slice_alignment_size_ = DEFAULT_TENSOR_SLICE_ALIGNMENT_SIZE;
  58. fully_use_device_ = DEFAULT_FULLY_USE_DEVICES;
  59. elementwise_stra_follow_ = DEFAULT_ELEMENTWISE_OP_STRA_FOLLOW;
  60. triangle_star_strategy_overwrite_ = DEFAULT_TRIANGLE_STAR_STRATEGY_OVERWRITE;
  61. dp_algo_enable_approxi_ = DEFAULT_DP_ALGO_ENABLE_APPROX;
  62. dp_algo_approxi_epsilon_ = DEFAULT_DP_ALGO_APPROX_EPSILON;
  63. }
  64. void CostModelContext::set_costmodel_context_for_device(const std::string &device_target) {
  65. if (device_target == kGPUDevice) {
  66. costmodel_beta_ = DEFAULT_COST_MODEL_BETA_GPU;
  67. }
  68. }
  69. void CostModelContext::set_dp_algo_approxi_epsilon(double epsilon) { dp_algo_approxi_epsilon_ = epsilon; }
  70. void CostModelContext::set_dp_algo_enable_approxi(bool approxi) { dp_algo_enable_approxi_ = approxi; }
  71. void CostModelContext::set_device_memory_capacity(double dm_capacity) { device_memory_capacity_ = dm_capacity; }
  72. void CostModelContext::set_costmodel_alpha(double cm_alpha) { costmodel_alpha_ = cm_alpha; }
  73. void CostModelContext::set_costmodel_beta(double cm_beta) { costmodel_beta_ = cm_beta; }
  74. void CostModelContext::set_costmodel_gamma(double cm_gamma) { costmodel_gamma_ = cm_gamma; }
  75. void CostModelContext::set_costmodel_simplify_cal(bool cm_simplify) { costmodel_simplify_cal_ = cm_simplify; }
  76. void CostModelContext::set_costmodel_communi_threshold(double cm_communi_th) {
  77. costmodel_communi_threshold_ = cm_communi_th;
  78. }
  79. void CostModelContext::set_costmodel_communi_const(double cm_communi_const) {
  80. costmodel_communi_const_ = cm_communi_const;
  81. }
  82. void CostModelContext::set_costmodel_communi_bias(double cm_communi_bias) { costmodel_communi_bias_ = cm_communi_bias; }
  83. void CostModelContext::set_multi_subgraphs(bool multi_graphs) { is_multi_subgraphs_ = multi_graphs; }
  84. void CostModelContext::set_costmodel_allreduce_fusion_algorithm(int64_t algorithm) {
  85. costmodel_allreduce_fusion_algorithm_ = algorithm;
  86. }
  87. void CostModelContext::set_costmodel_allreduce_fusion_times(int64_t allreduce_fusion_times) {
  88. costmodel_allreduce_fusion_times_ = allreduce_fusion_times;
  89. }
  90. void CostModelContext::set_costmodel_allreduce_fusion_tail_percent(double tail_percent) {
  91. costmodel_allreduce_fusion_tail_percent_ = tail_percent;
  92. }
  93. void CostModelContext::set_costmodel_allreduce_fusion_tail_time(double tail_time) {
  94. costmodel_allreduce_fusion_tail_time_ = tail_time;
  95. }
  96. void CostModelContext::set_costmodel_allreduce_fusion_allreduce_inherent_time(double allreduce_inherent_time) {
  97. costmodel_allreduce_fusion_allreduce_inherent_time_ = allreduce_inherent_time;
  98. }
  99. void CostModelContext::set_costmodel_allreduce_fusion_allreduce_bandwidth(double allreduce_bandwidth) {
  100. costmodel_allreduce_fusion_allreduce_bandwidth_ = allreduce_bandwidth;
  101. }
  102. void CostModelContext::set_costmodel_allreduce_fusion_computation_time_parameter(double computation_time_parameter) {
  103. costmodel_allreduce_fusion_computation_time_parameter_ = computation_time_parameter;
  104. }
  105. void CostModelContext::set_tensor_slice_alignment_enable(bool ts_align) { tensor_slice_alignment_enable_ = ts_align; }
  106. void CostModelContext::set_tensor_slice_alignment_size(size_t ts_align_size) {
  107. tensor_slice_alignment_size_ = ts_align_size;
  108. }
  109. void CostModelContext::set_fully_use_device(bool fully_use) { fully_use_device_ = fully_use; }
  110. void CostModelContext::set_elementwise_stra_follow(bool elementwise_follow) {
  111. elementwise_stra_follow_ = elementwise_follow;
  112. }
  113. void CostModelContext::set_triangle_star_strategy_overwrite(bool overwrite) {
  114. triangle_star_strategy_overwrite_ = overwrite;
  115. }
  116. void CostModelContext::set_run_phase(int64_t phase) { run_phase_ = phase; }
  117. void CostModelContext::set_dp_algo_single_loop(bool single_loop) { dp_algo_single_loop_ = single_loop; }
  118. struct CostRegister {
  119. CostRegister() {
  120. MsContext::device_seter([](const std::string &device_target) {
  121. CostModelContext::GetInstance()->set_costmodel_context_for_device(device_target);
  122. });
  123. }
  124. ~CostRegister() = default;
  125. } cost_regsiter;
  126. } // namespace parallel
  127. } // namespace mindspore