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.

ps_context.cc 5.0 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 "ps/ps_context.h"
  17. #include "utils/log_adapter.h"
  18. #include "utils/ms_utils.h"
  19. #include "backend/kernel_compiler/kernel.h"
  20. #if (ENABLE_CPU && (ENABLE_D || ENABLE_GPU))
  21. #include "ps/ps_cache/ps_cache_manager.h"
  22. #include "ps/ps_cache/ps_data/ps_data_prefetch.h"
  23. #endif
  24. namespace mindspore {
  25. namespace ps {
  26. std::shared_ptr<PSContext> PSContext::instance() {
  27. static std::shared_ptr<PSContext> ps_instance = nullptr;
  28. if (ps_instance == nullptr) {
  29. ps_instance.reset(new (std::nothrow) PSContext());
  30. }
  31. return ps_instance;
  32. }
  33. void PSContext::SetPSEnable(bool enabled) {
  34. ps_enabled_ = enabled;
  35. if (ps_enabled_) {
  36. std::string ms_role = common::GetEnv(kEnvRole);
  37. MS_LOG(INFO) << "PS mode is enabled. MS_ROLE is " << ms_role;
  38. if (ms_role == kEnvRoleOfWorker) {
  39. is_worker_ = true;
  40. } else if (ms_role == kEnvRoleOfPServer) {
  41. is_pserver_ = true;
  42. } else if (ms_role == kEnvRoleOfScheduler) {
  43. is_sched_ = true;
  44. } else {
  45. MS_LOG(WARNING) << "MS_ROLE is " << ms_role << ", which is invalid.";
  46. }
  47. worker_num_ = std::strtol(common::GetEnv(kEnvWorkerNum).c_str(), nullptr, 10);
  48. server_num_ = std::strtol(common::GetEnv(kEnvPServerNum).c_str(), nullptr, 10);
  49. scheduler_host_ = common::GetEnv(kEnvSchedulerHost);
  50. scheduler_port_ = std::strtol(common::GetEnv(kEnvSchedulerPort).c_str(), nullptr, 10);
  51. } else {
  52. MS_LOG(INFO) << "PS mode is disabled.";
  53. is_worker_ = false;
  54. is_pserver_ = false;
  55. is_sched_ = false;
  56. }
  57. }
  58. bool PSContext::is_ps_mode() const { return ps_enabled_; }
  59. void PSContext::Reset() {
  60. ps_enabled_ = false;
  61. is_worker_ = false;
  62. is_pserver_ = false;
  63. is_sched_ = false;
  64. #if (ENABLE_CPU && (ENABLE_D || ENABLE_GPU))
  65. if (ps::PsDataPrefetch::GetInstance().cache_enable()) {
  66. ps_cache_instance.Finalize();
  67. set_cache_enable(false);
  68. }
  69. #endif
  70. }
  71. std::string PSContext::ms_role() const {
  72. if (is_worker_) {
  73. return kEnvRoleOfWorker;
  74. } else if (is_pserver_) {
  75. return kEnvRoleOfPServer;
  76. } else if (is_sched_) {
  77. return kEnvRoleOfScheduler;
  78. } else {
  79. return kEnvRoleOfNotPS;
  80. }
  81. }
  82. bool PSContext::is_worker() const { return is_worker_; }
  83. bool PSContext::is_server() const { return is_pserver_; }
  84. bool PSContext::is_scheduler() const { return is_sched_; }
  85. uint32_t PSContext::initial_worker_num() { return worker_num_; }
  86. uint32_t PSContext::initial_server_num() { return server_num_; }
  87. std::string PSContext::scheduler_host() { return scheduler_host_; }
  88. uint16_t PSContext::scheduler_port() { return scheduler_port_; }
  89. void PSContext::SetPSRankId(int rank_id) { rank_id_ = rank_id; }
  90. int PSContext::ps_rank_id() const { return rank_id_; }
  91. void PSContext::InsertHashTableSize(const std::string &param_name, size_t cache_vocab_size, size_t embedding_size,
  92. size_t vocab_size) const {
  93. #if (ENABLE_CPU && (ENABLE_D || ENABLE_GPU))
  94. ps_cache_instance.InsertHashTableSize(param_name, cache_vocab_size, embedding_size, vocab_size);
  95. #endif
  96. }
  97. void PSContext::ReInsertHashTableSize(const std::string &new_param_name, const std::string &cur_param_name,
  98. size_t cache_vocab_size, size_t embedding_size) const {
  99. #if (ENABLE_CPU && (ENABLE_D || ENABLE_GPU))
  100. ps_cache_instance.ReInsertHashTableSize(new_param_name, cur_param_name, cache_vocab_size, embedding_size);
  101. #endif
  102. }
  103. void PSContext::InsertWeightInitInfo(const std::string &param_name, size_t global_seed, size_t op_seed) const {
  104. #if (ENABLE_CPU && (ENABLE_D || ENABLE_GPU))
  105. ps_cache_instance.InsertWeightInitInfo(param_name, global_seed, op_seed);
  106. #endif
  107. }
  108. void PSContext::InsertAccumuInitInfo(const std::string &param_name, float init_val) const {
  109. #if (ENABLE_CPU && (ENABLE_D || ENABLE_GPU))
  110. ps_cache_instance.InsertAccumuInitInfo(param_name, init_val);
  111. #endif
  112. }
  113. void PSContext::CloneHashTable(const std::string &dest_param_name, const std::string &src_param_name) const {
  114. #if (ENABLE_CPU && (ENABLE_D || ENABLE_GPU))
  115. ps_cache_instance.CloneHashTable(dest_param_name, src_param_name);
  116. #endif
  117. }
  118. void PSContext::set_cache_enable(bool cache_enable) const {
  119. #if (ENABLE_CPU && (ENABLE_D || ENABLE_GPU))
  120. PsDataPrefetch::GetInstance().set_cache_enable(cache_enable);
  121. #endif
  122. }
  123. void PSContext::set_rank_id(int rank_id) const {
  124. #if (ENABLE_CPU && (ENABLE_D || ENABLE_GPU))
  125. ps_cache_instance.set_rank_id(rank_id);
  126. #endif
  127. }
  128. } // namespace ps
  129. } // namespace mindspore