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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/util.h"
  17. #include <unordered_map>
  18. #include <vector>
  19. #include "ps/constants.h"
  20. #include "ps/ps_context.h"
  21. #include "utils/ms_utils.h"
  22. namespace mindspore {
  23. namespace ps {
  24. int64_t Util::rank_id_ = -1;
  25. std::unordered_map<std::string, int64_t> Util::optimizer_to_ids{
  26. {kApplyMomentum, 0},
  27. {kSparseAdam, 1},
  28. {kSparseLazyAdam, 2},
  29. {kSparseFtrl, 3},
  30. };
  31. std::unordered_map<int64_t, std::string> Util::id_to_optimizers{
  32. {0, kApplyMomentum},
  33. {1, kSparseAdam},
  34. {2, kSparseLazyAdam},
  35. {3, kSparseFtrl},
  36. };
  37. std::unordered_map<int64_t, std::string> Util::id_to_optimizer_nodes{
  38. {0, kApplyMomentumOp},
  39. {1, kSparseAdamOp},
  40. {2, kSparseLazyAdamOp},
  41. {3, kSparseFtrlOp},
  42. };
  43. bool Util::IsRoleOfPServer() { return PSContext::instance()->is_server(); }
  44. bool Util::IsRoleOfScheduler() { return PSContext::instance()->is_scheduler(); }
  45. int64_t Util::optimizer_id(std::string name) {
  46. if (optimizer_to_ids.count(name) > 0) {
  47. return optimizer_to_ids[name];
  48. }
  49. return -1;
  50. }
  51. std::string Util::optimizer_name(int64_t id) {
  52. if (id_to_optimizers.count(id) > 0) {
  53. return id_to_optimizers[id];
  54. }
  55. return "";
  56. }
  57. std::string Util::optimizer_node_name(int64_t id) {
  58. if (id_to_optimizer_nodes.count(id) > 0) {
  59. return id_to_optimizer_nodes[id];
  60. }
  61. return "";
  62. }
  63. bool Util::is_optimizer(std::string name) { return optimizer_to_ids.count(name) > 0; }
  64. int64_t Util::LocalShard(int64_t first_dim, int64_t rank_id, int64_t server_num) {
  65. std::map<int64_t, int64_t> shard_dims = AllRankLocalShard(first_dim, rank_id, server_num);
  66. if (shard_dims.count(rank_id) == 0) {
  67. MS_LOG(EXCEPTION) << "Invalid rank id " << rank_id;
  68. }
  69. return shard_dims[rank_id];
  70. }
  71. std::map<int64_t, int64_t> Util::AllRankLocalShard(int64_t first_dim, int64_t rank_id, int64_t server_num) {
  72. if (first_dim <= 0 || server_num <= 0 || rank_id < 0) {
  73. MS_LOG(EXCEPTION) << "Input values are invalid.";
  74. }
  75. if (rank_id >= server_num) {
  76. MS_LOG(EXCEPTION) << "The rank ID " << rank_id << " should be less than the number of servers " << server_num;
  77. }
  78. std::map<int64_t, int64_t> shard_dims;
  79. for (int64_t i = 0; i < server_num; i++) {
  80. shard_dims[i] = 0;
  81. }
  82. if (server_num != static_cast<int64_t>(shard_dims.size())) {
  83. MS_LOG(EXCEPTION) << "Inconsistent server num " << server_num << " shard dims counter size " << shard_dims.size();
  84. }
  85. int64_t server_index = -1;
  86. for (int64_t i = 0; i < first_dim; i++) {
  87. server_index = (server_index + 1) % server_num;
  88. shard_dims[server_index] = shard_dims[server_index] + 1;
  89. }
  90. if (shard_dims.count(rank_id) == 0) {
  91. MS_LOG(EXCEPTION) << "Invalid rank id " << rank_id << ", total server num " << server_num;
  92. }
  93. return shard_dims;
  94. }
  95. void Util::ReduceSparseGradient(float *gradients, int *indices, const size_t indices_size, size_t segment_size,
  96. const size_t first_dim_size, const size_t outer_dim_size,
  97. mindspore::kernel::SparseGradient<int> *unique_sparse_grad) {
  98. size_t slice_segment_size = indices_size * segment_size;
  99. std::vector<float> workspace_grad(slice_segment_size);
  100. std::vector<int> workspace_indices(indices_size);
  101. MS_EXCEPTION_IF_NULL(gradients);
  102. MS_EXCEPTION_IF_NULL(indices);
  103. mindspore::kernel::SparseGradient<int> workspace_sparse_grad(
  104. {workspace_grad.data(), workspace_indices.data(), indices_size});
  105. mindspore::kernel::SparseGradient<int> input_sparse_grad({gradients, indices, indices_size});
  106. mindspore::kernel::ReduceSparseGradientParam<int> param;
  107. param.input_grad_ = &input_sparse_grad;
  108. param.workspace_grad_ = &workspace_sparse_grad;
  109. param.output_grad_ = unique_sparse_grad;
  110. param.max_index_ = first_dim_size;
  111. param.value_stride_ = outer_dim_size;
  112. mindspore::kernel::SparseOptimizerCPUKernel::BucketReduceSparseGradient(param);
  113. }
  114. } // namespace ps
  115. } // namespace mindspore