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.

util.cc 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * Copyright 2020 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/ps/util.h"
  17. #include <unordered_map>
  18. #include "frontend/parallel/ps/common.h"
  19. #include "utils/ms_utils.h"
  20. namespace mindspore {
  21. namespace parallel {
  22. namespace ps {
  23. int Util::rank_id_ = -1;
  24. std::unordered_map<std::string, int> Util::optimizer_to_ids{
  25. {kApplyMomentum, 0},
  26. {kSparseAdam, 1},
  27. {kSparseLazyAdam, 2},
  28. {kSparseFtrl, 3},
  29. };
  30. std::unordered_map<int, std::string> Util::id_to_optimizers{
  31. {0, kApplyMomentum},
  32. {1, kSparseAdam},
  33. {2, kSparseLazyAdam},
  34. {3, kSparseFtrl},
  35. };
  36. std::unordered_map<int, std::string> Util::id_to_optimizer_nodes{
  37. {0, kApplyMomentumOp},
  38. {1, kSparseAdamOp},
  39. {2, kSparseLazyAdamOp},
  40. {3, kSparseFtrlOp},
  41. };
  42. bool Util::IsParamServerMode() { return IsRoleOfWorker() || IsRoleOfPServer() || IsRoleOfScheduler(); }
  43. bool Util::IsRoleOfWorker() {
  44. auto role = common::GetEnv(kEnvRole);
  45. if (strcmp(role.c_str(), kEnvRoleOfWorker) == 0) {
  46. return true;
  47. } else {
  48. return false;
  49. }
  50. }
  51. bool Util::IsRoleOfPServer() {
  52. auto role = common::GetEnv(kEnvRole);
  53. if (strcmp(role.c_str(), kEnvRoleOfPServer) == 0) {
  54. return true;
  55. } else {
  56. return false;
  57. }
  58. }
  59. bool Util::IsRoleOfScheduler() {
  60. auto role = common::GetEnv(kEnvRole);
  61. if (strcmp(role.c_str(), kEnvRoleOfScheduler) == 0) {
  62. return true;
  63. } else {
  64. return false;
  65. }
  66. }
  67. void Util::SetInternalEnvVar() {
  68. if (IsParamServerMode()) {
  69. auto comm_type = common::GetEnv(kEnvCommType);
  70. if (comm_type.size() > 0) {
  71. (void)common::SetEnv(kDmlcCommType, comm_type.c_str());
  72. }
  73. auto interface = common::GetEnv(kEnvInterface);
  74. if (interface.size() > 0) {
  75. (void)common::SetEnv(kDmlcInterface, interface.c_str());
  76. }
  77. auto server_num = common::GetEnv(kEnvPServerNum);
  78. if (server_num.size() > 0) {
  79. (void)common::SetEnv(kDmlcPServerNum, server_num.c_str());
  80. }
  81. auto worker_num = common::GetEnv(kEnvWorkerNum);
  82. if (worker_num.size() > 0) {
  83. (void)common::SetEnv(kDmlcWorkerNum, worker_num.c_str());
  84. }
  85. if (IsRoleOfScheduler()) {
  86. (void)common::SetEnv(kDmlcRole, kRoleOfScheduler);
  87. } else if (IsRoleOfPServer()) {
  88. (void)common::SetEnv(kDmlcRole, kRoleOfPServer);
  89. } else if (IsRoleOfWorker()) {
  90. (void)common::SetEnv(kDmlcRole, kRoleOfWorker);
  91. }
  92. auto scheduler_host = common::GetEnv(kEnvSchedulerHost);
  93. if (scheduler_host.size() > 0) {
  94. (void)common::SetEnv(kDmlcSchedulerHost, scheduler_host.c_str());
  95. }
  96. auto scheduler_port = common::GetEnv(kEnvSchedulerPort);
  97. if (scheduler_port.size() > 0) {
  98. (void)common::SetEnv(kDmlcSchedulerPort, scheduler_port.c_str());
  99. }
  100. }
  101. }
  102. int Util::optimizer_id(std::string name) {
  103. if (optimizer_to_ids.count(name) > 0) {
  104. return optimizer_to_ids[name];
  105. }
  106. return -1;
  107. }
  108. std::string Util::optimizer_name(int id) {
  109. if (id_to_optimizers.count(id) > 0) {
  110. return id_to_optimizers[id];
  111. }
  112. return "";
  113. }
  114. std::string Util::optimizer_node_name(int id) {
  115. if (id_to_optimizer_nodes.count(id) > 0) {
  116. return id_to_optimizer_nodes[id];
  117. }
  118. return "";
  119. }
  120. bool Util::is_optimizer(std::string name) { return optimizer_to_ids.count(name) > 0; }
  121. int Util::LocalShard(int first_dim, int rank_id, int server_num) {
  122. int shard_size = std::round((static_cast<float>(first_dim)) / server_num);
  123. int remain_size = first_dim % server_num;
  124. if (remain_size == 0 || rank_id < server_num - 1) {
  125. return shard_size;
  126. } else {
  127. return first_dim - (shard_size * (server_num - 1));
  128. }
  129. }
  130. void Util::SetRankId(int rank_id) { rank_id_ = rank_id; }
  131. int Util::GetRankId() { return rank_id_; }
  132. } // namespace ps
  133. } // namespace parallel
  134. } // namespace mindspore