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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. } else {
  48. MS_LOG(INFO) << "PS mode is disabled.";
  49. is_worker_ = false;
  50. is_pserver_ = false;
  51. is_sched_ = false;
  52. }
  53. }
  54. bool PSContext::is_ps_enabled() const { return ps_enabled_; }
  55. void PSContext::Reset() {
  56. ps_enabled_ = false;
  57. is_worker_ = false;
  58. is_pserver_ = false;
  59. is_sched_ = false;
  60. #if (ENABLE_CPU && (ENABLE_D || ENABLE_GPU))
  61. if (ps::PsDataPrefetch::GetInstance().cache_enable()) {
  62. ps_cache_instance.Finalize();
  63. set_cache_enable(false);
  64. }
  65. #endif
  66. }
  67. std::string PSContext::ms_role() const {
  68. if (is_worker_) {
  69. return kEnvRoleOfWorker;
  70. } else if (is_pserver_) {
  71. return kEnvRoleOfPServer;
  72. } else if (is_sched_) {
  73. return kEnvRoleOfScheduler;
  74. } else {
  75. return kEnvRoleOfNotPS;
  76. }
  77. }
  78. bool PSContext::is_role_worker() const { return is_worker_; }
  79. bool PSContext::is_role_pserver() const { return is_pserver_; }
  80. bool PSContext::is_role_sched() const { return is_sched_; }
  81. void PSContext::SetPSRankId(int rank_id) { rank_id_ = rank_id; }
  82. int PSContext::ps_rank_id() const { return rank_id_; }
  83. void PSContext::InsertHashTableSize(const std::string &param_name, size_t cache_vocab_size, size_t embedding_size,
  84. size_t vocab_size) const {
  85. #if (ENABLE_CPU && (ENABLE_D || ENABLE_GPU))
  86. ps_cache_instance.InsertHashTableSize(param_name, cache_vocab_size, embedding_size, vocab_size);
  87. #endif
  88. }
  89. void PSContext::ReInsertHashTableSize(const std::string &new_param_name, const std::string &cur_param_name,
  90. size_t cache_vocab_size, size_t embedding_size) const {
  91. #if (ENABLE_CPU && (ENABLE_D || ENABLE_GPU))
  92. ps_cache_instance.ReInsertHashTableSize(new_param_name, cur_param_name, cache_vocab_size, embedding_size);
  93. #endif
  94. }
  95. void PSContext::InsertWeightInitInfo(const std::string &param_name, size_t global_seed, size_t op_seed) const {
  96. #if (ENABLE_CPU && (ENABLE_D || ENABLE_GPU))
  97. ps_cache_instance.InsertWeightInitInfo(param_name, global_seed, op_seed);
  98. #endif
  99. }
  100. void PSContext::InsertAccumuInitInfo(const std::string &param_name, float init_val) const {
  101. #if (ENABLE_CPU && (ENABLE_D || ENABLE_GPU))
  102. ps_cache_instance.InsertAccumuInitInfo(param_name, init_val);
  103. #endif
  104. }
  105. void PSContext::CloneHashTable(const std::string &dest_param_name, const std::string &src_param_name) const {
  106. #if (ENABLE_CPU && (ENABLE_D || ENABLE_GPU))
  107. ps_cache_instance.CloneHashTable(dest_param_name, src_param_name);
  108. #endif
  109. }
  110. void PSContext::set_cache_enable(bool cache_enable) const {
  111. #if (ENABLE_CPU && (ENABLE_D || ENABLE_GPU))
  112. PsDataPrefetch::GetInstance().set_cache_enable(cache_enable);
  113. #endif
  114. }
  115. void PSContext::set_rank_id(int rank_id) const {
  116. #if (ENABLE_CPU && (ENABLE_D || ENABLE_GPU))
  117. ps_cache_instance.set_rank_id(rank_id);
  118. #endif
  119. }
  120. } // namespace ps
  121. } // namespace mindspore