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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. }
  53. void CostModelContext::ResetAlgoParameters() {
  54. costmodel_simplify_cal_ = DEFAULT_COST_MODEL_SIMPLIFY_CALCULATION;
  55. tensor_slice_alignment_enable_ = DEFAULT_TENSOR_SLICE_ALIGNMENT_ENABLE;
  56. tensor_slice_alignment_size_ = DEFAULT_TENSOR_SLICE_ALIGNMENT_SIZE;
  57. fully_use_device_ = DEFAULT_FULLY_USE_DEVICES;
  58. elementwise_stra_follow_ = DEFAULT_ELEMENTWISE_OP_STRA_FOLLOW;
  59. triangle_strategy_overwrite_ = DEFAULT_TRIANGLE_STRATEGY_OVERWRITE;
  60. }
  61. void CostModelContext::set_costmodel_context_for_device(const std::string &device_target) {
  62. if (device_target == kGPUDevice) {
  63. costmodel_beta_ = DEFAULT_COST_MODEL_BETA_GPU;
  64. }
  65. }
  66. void CostModelContext::set_device_memory_capacity(double dm_capacity) { device_memory_capacity_ = dm_capacity; }
  67. void CostModelContext::set_costmodel_alpha(double cm_alpha) { costmodel_alpha_ = cm_alpha; }
  68. void CostModelContext::set_costmodel_beta(double cm_beta) { costmodel_beta_ = cm_beta; }
  69. void CostModelContext::set_costmodel_gamma(double cm_gamma) { costmodel_gamma_ = cm_gamma; }
  70. void CostModelContext::set_costmodel_simplify_cal(bool cm_simplify) { costmodel_simplify_cal_ = cm_simplify; }
  71. void CostModelContext::set_costmodel_communi_threshold(double cm_communi_th) {
  72. costmodel_communi_threshold_ = cm_communi_th;
  73. }
  74. void CostModelContext::set_costmodel_communi_const(double cm_communi_const) {
  75. costmodel_communi_const_ = cm_communi_const;
  76. }
  77. void CostModelContext::set_costmodel_communi_bias(double cm_communi_bias) { costmodel_communi_bias_ = cm_communi_bias; }
  78. void CostModelContext::set_multi_subgraphs(bool multi_graphs) { is_multi_subgraphs_ = multi_graphs; }
  79. void CostModelContext::set_costmodel_allreduce_fusion_algorithm(int32_t algorithm) {
  80. costmodel_allreduce_fusion_algorithm_ = algorithm;
  81. }
  82. void CostModelContext::set_costmodel_allreduce_fusion_times(int32_t allreduce_fusion_times) {
  83. costmodel_allreduce_fusion_times_ = allreduce_fusion_times;
  84. }
  85. void CostModelContext::set_costmodel_allreduce_fusion_tail_percent(double tail_percent) {
  86. costmodel_allreduce_fusion_tail_percent_ = tail_percent;
  87. }
  88. void CostModelContext::set_costmodel_allreduce_fusion_tail_time(double tail_time) {
  89. costmodel_allreduce_fusion_tail_time_ = tail_time;
  90. }
  91. void CostModelContext::set_costmodel_allreduce_fusion_allreduce_inherent_time(double allreduce_inherent_time) {
  92. costmodel_allreduce_fusion_allreduce_inherent_time_ = allreduce_inherent_time;
  93. }
  94. void CostModelContext::set_costmodel_allreduce_fusion_allreduce_bandwidth(double allreduce_bandwidth) {
  95. costmodel_allreduce_fusion_allreduce_bandwidth_ = allreduce_bandwidth;
  96. }
  97. void CostModelContext::set_costmodel_allreduce_fusion_computation_time_parameter(double computation_time_parameter) {
  98. costmodel_allreduce_fusion_computation_time_parameter_ = computation_time_parameter;
  99. }
  100. void CostModelContext::set_tensor_slice_alignment_enable(bool ts_align) { tensor_slice_alignment_enable_ = ts_align; }
  101. void CostModelContext::set_tensor_slice_alignment_size(size_t ts_align_size) {
  102. tensor_slice_alignment_size_ = ts_align_size;
  103. }
  104. void CostModelContext::set_fully_use_device(bool fully_use) { fully_use_device_ = fully_use; }
  105. void CostModelContext::set_elementwise_stra_follow(bool elementwise_follow) {
  106. elementwise_stra_follow_ = elementwise_follow;
  107. }
  108. void CostModelContext::set_triangle_strategy_overwrite(bool overwrite) { triangle_strategy_overwrite_ = overwrite; }
  109. void CostModelContext::set_run_phase(int32_t phase) { run_phase_ = phase; }
  110. struct CostRegister {
  111. CostRegister() {
  112. MsContext::device_seter([](const std::string &device_target) {
  113. CostModelContext::GetInstance()->set_costmodel_context_for_device(device_target);
  114. });
  115. }
  116. ~CostRegister() = default;
  117. } cost_regsiter;
  118. } // namespace parallel
  119. } // namespace mindspore