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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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_ = TRAINING_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::PrintCostModel() {
  65. MS_LOG(INFO) << "device_memory_capacity: " << device_memory_capacity_ << ".";
  66. MS_LOG(INFO) << "costmodel_alpha: " << costmodel_alpha_ << ".";
  67. MS_LOG(INFO) << "costmodel_beta: " << costmodel_beta_ << ".";
  68. MS_LOG(INFO) << "costmodel_gamma: " << costmodel_gamma_ << ".";
  69. MS_LOG(INFO) << "costmodel_simplify_cal: " << costmodel_simplify_cal_ << ".";
  70. MS_LOG(INFO) << "costmodel_communi_threshold: " << costmodel_communi_threshold_ << ".";
  71. MS_LOG(INFO) << "costmodel_communi_const: " << costmodel_communi_const_ << ".";
  72. MS_LOG(INFO) << "costmodel_communi_bias: " << costmodel_communi_bias_ << ".";
  73. MS_LOG(INFO) << "is_multi_subgraphs: " << is_multi_subgraphs_ << ".";
  74. MS_LOG(INFO) << "triangle_star_strategy_overwrite: " << triangle_star_strategy_overwrite_ << ".";
  75. MS_LOG(INFO) << "dp_algo_enable_approxi: " << dp_algo_enable_approxi_ << ".";
  76. MS_LOG(INFO) << "dp_algo_approxi_epsilon: " << dp_algo_approxi_epsilon_ << ".";
  77. MS_LOG(INFO) << "dp_algo_single_loop: " << dp_algo_single_loop_ << ".";
  78. MS_LOG(INFO) << "run_phase: " << run_phase_ << ".";
  79. MS_LOG(INFO) << "tensor_slice_alignment_enable: " << tensor_slice_alignment_enable_ << ".";
  80. MS_LOG(INFO) << "tensor_slice_align_size: " << tensor_slice_alignment_size_ << ".";
  81. MS_LOG(INFO) << "fully_use_device: " << fully_use_device_ << ".";
  82. MS_LOG(INFO) << "elementwise_stra_follow: " << elementwise_stra_follow_ << ".";
  83. }
  84. void CostModelContext::set_costmodel_context_for_device(const std::string &device_target) {
  85. if (device_target == kGPUDevice) {
  86. costmodel_beta_ = DEFAULT_COST_MODEL_BETA_GPU;
  87. }
  88. }
  89. void CostModelContext::set_dp_algo_approxi_epsilon(double epsilon) {
  90. if (epsilon <= 0 || epsilon > 1) {
  91. MS_LOG(EXCEPTION) << "'epsilon' must be in (0, 1]";
  92. }
  93. dp_algo_approxi_epsilon_ = epsilon;
  94. }
  95. void CostModelContext::set_dp_algo_enable_approxi(bool approxi) {
  96. if (approxi) {
  97. MS_LOG(INFO) << "dp_algo_enable_approx: true.";
  98. } else {
  99. MS_LOG(INFO) << "dp_algo_enable_approx: false.";
  100. }
  101. dp_algo_enable_approxi_ = approxi;
  102. }
  103. void CostModelContext::set_device_memory_capacity(double dm_capacity) {
  104. if (dm_capacity <= 0) {
  105. MS_LOG(EXCEPTION) << "'device_memory_capacity' must be positive.";
  106. }
  107. device_memory_capacity_ = dm_capacity;
  108. }
  109. void CostModelContext::set_costmodel_alpha(double cm_alpha) {
  110. if (cm_alpha <= 0) {
  111. MS_LOG(EXCEPTION) << "'costmodel_alpha' must be positive.";
  112. }
  113. costmodel_alpha_ = cm_alpha;
  114. }
  115. void CostModelContext::set_costmodel_beta(double cm_beta) {
  116. if (cm_beta <= 0) {
  117. MS_LOG(EXCEPTION) << "'costmodel_beta' must be positive.";
  118. }
  119. costmodel_beta_ = cm_beta;
  120. }
  121. void CostModelContext::set_costmodel_gamma(double cm_gamma) {
  122. if ((cm_gamma < 0) || (cm_gamma > 1)) {
  123. MS_LOG(EXCEPTION) << "'costmodel_gamma' must in [0, 1].";
  124. }
  125. costmodel_gamma_ = cm_gamma;
  126. }
  127. void CostModelContext::set_costmodel_simplify_cal(bool cm_simplify) {
  128. if (cm_simplify) {
  129. MS_LOG(INFO) << "costmodel_simplify_cal: true.";
  130. } else {
  131. MS_LOG(INFO) << "costmodel_simplify_cal: false.";
  132. }
  133. costmodel_simplify_cal_ = cm_simplify;
  134. }
  135. void CostModelContext::set_costmodel_communi_threshold(double cm_communi_th) {
  136. if (cm_communi_th < 0) {
  137. MS_LOG(EXCEPTION) << "'costmodel_communi_threshold' must be non-zero.";
  138. }
  139. costmodel_communi_threshold_ = cm_communi_th;
  140. }
  141. void CostModelContext::set_costmodel_communi_const(double cm_communi_const) {
  142. if (cm_communi_const < 0) {
  143. MS_LOG(EXCEPTION) << "'costmodel_communi_const' must be non-zero.";
  144. }
  145. costmodel_communi_const_ = cm_communi_const;
  146. }
  147. void CostModelContext::set_costmodel_communi_bias(double cm_communi_bias) {
  148. if (cm_communi_bias < 0) {
  149. MS_LOG(EXCEPTION) << "'costmodel_communi_bias' must be non-zero.";
  150. }
  151. costmodel_communi_bias_ = cm_communi_bias;
  152. }
  153. void CostModelContext::set_multi_subgraphs(bool multi_graphs) {
  154. if (multi_graphs) {
  155. MS_LOG(INFO) << "multi_subgraphs: true.";
  156. } else {
  157. MS_LOG(INFO) << "multi_subgraphs: false.";
  158. }
  159. is_multi_subgraphs_ = multi_graphs;
  160. }
  161. void CostModelContext::set_costmodel_allreduce_fusion_algorithm(int64_t algorithm) {
  162. costmodel_allreduce_fusion_algorithm_ = algorithm;
  163. }
  164. void CostModelContext::set_costmodel_allreduce_fusion_times(int64_t allreduce_fusion_times) {
  165. costmodel_allreduce_fusion_times_ = allreduce_fusion_times;
  166. }
  167. void CostModelContext::set_costmodel_allreduce_fusion_tail_percent(double tail_percent) {
  168. costmodel_allreduce_fusion_tail_percent_ = tail_percent;
  169. }
  170. void CostModelContext::set_costmodel_allreduce_fusion_tail_time(double tail_time) {
  171. costmodel_allreduce_fusion_tail_time_ = tail_time;
  172. }
  173. void CostModelContext::set_costmodel_allreduce_fusion_allreduce_inherent_time(double allreduce_inherent_time) {
  174. costmodel_allreduce_fusion_allreduce_inherent_time_ = allreduce_inherent_time;
  175. }
  176. void CostModelContext::set_costmodel_allreduce_fusion_allreduce_bandwidth(double allreduce_bandwidth) {
  177. costmodel_allreduce_fusion_allreduce_bandwidth_ = allreduce_bandwidth;
  178. }
  179. void CostModelContext::set_costmodel_allreduce_fusion_computation_time_parameter(double computation_time_parameter) {
  180. costmodel_allreduce_fusion_computation_time_parameter_ = computation_time_parameter;
  181. }
  182. void CostModelContext::set_tensor_slice_alignment_enable(bool ts_align) {
  183. if (ts_align) {
  184. MS_LOG(INFO) << "tensor_slice_align_enable: true.";
  185. } else {
  186. MS_LOG(INFO) << "tensor_slice_align_enable: false.";
  187. }
  188. tensor_slice_alignment_enable_ = ts_align;
  189. }
  190. void CostModelContext::set_tensor_slice_alignment_size(size_t ts_align_size) {
  191. if (ts_align_size == 0) {
  192. MS_LOG(EXCEPTION) << "'tensor_slice_align_size' must be positive.";
  193. }
  194. tensor_slice_alignment_size_ = ts_align_size;
  195. }
  196. void CostModelContext::set_fully_use_device(bool fully_use) {
  197. if (fully_use) {
  198. MS_LOG(INFO) << "fully_use_devices: true.";
  199. } else {
  200. MS_LOG(INFO) << "fully_use_devices: false.";
  201. }
  202. fully_use_device_ = fully_use;
  203. }
  204. void CostModelContext::set_elementwise_stra_follow(bool elementwise_follow) {
  205. if (elementwise_follow) {
  206. MS_LOG(INFO) << "elementwise_op_strategy_follow: true.";
  207. } else {
  208. MS_LOG(INFO) << "elementwise_op_strategy_follow: false.";
  209. }
  210. elementwise_stra_follow_ = elementwise_follow;
  211. }
  212. void CostModelContext::set_triangle_star_strategy_overwrite(bool overwrite) {
  213. if (overwrite) {
  214. MS_LOG(INFO) << "triangle_star_strategy_overwrite: true.";
  215. } else {
  216. MS_LOG(INFO) << "triangle_star_strategy_overwrite: false.";
  217. }
  218. triangle_star_strategy_overwrite_ = overwrite;
  219. }
  220. void CostModelContext::set_run_phase(int64_t phase) {
  221. if (phase != 0 && phase != 1) {
  222. MS_LOG(EXCEPTION) << "'run_phase' must be in {0, 1}";
  223. }
  224. run_phase_ = phase;
  225. }
  226. void CostModelContext::set_dp_algo_single_loop(bool single_loop) {
  227. if (single_loop) {
  228. MS_LOG(INFO) << "dp_algo_single_loop: true.";
  229. } else {
  230. MS_LOG(INFO) << "dp_algo_single_loop: false.";
  231. }
  232. dp_algo_single_loop_ = single_loop;
  233. }
  234. struct CostRegister {
  235. CostRegister() {
  236. MsContext::device_seter([](const std::string &device_target) {
  237. CostModelContext::GetInstance()->set_costmodel_context_for_device(device_target);
  238. });
  239. }
  240. ~CostRegister() = default;
  241. } cost_regsiter;
  242. } // namespace parallel
  243. } // namespace mindspore