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.h 2.3 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. #ifndef MINDSPORE_CCSRC_PS_UTIL_H_
  17. #define MINDSPORE_CCSRC_PS_UTIL_H_
  18. #include <map>
  19. #include <string>
  20. #include <unordered_map>
  21. #include "backend/session/anf_runtime_algorithm.h"
  22. #include "backend/kernel_compiler/common_utils.h"
  23. #include "backend/kernel_compiler/cpu/sparse_optimizer_cpu_kernel.h"
  24. namespace mindspore {
  25. namespace ps {
  26. enum ParamType { kUnKnown = 0, kWeight = 1, kAccumulation = 2 };
  27. struct ParamInitInfo {
  28. ParamType param_type_{kUnKnown};
  29. size_t global_seed_{0};
  30. size_t op_seed_{0};
  31. float init_val_{0};
  32. };
  33. class Util {
  34. public:
  35. static bool IsRoleOfPServer();
  36. static bool IsRoleOfScheduler();
  37. static int64_t optimizer_id(std::string name);
  38. static std::string optimizer_name(int64_t id);
  39. static std::string optimizer_node_name(int64_t id);
  40. static bool is_optimizer(std::string name);
  41. static int64_t LocalShard(int64_t first_dim, int64_t rank_id, int64_t server_num);
  42. static std::map<int64_t, int64_t> AllRankLocalShard(int64_t first_dim, int64_t rank_id, int64_t server_num);
  43. static void ReduceSparseGradient(float *gradients, int *indices, const size_t indices_size, size_t segment_size,
  44. const size_t first_dim_size, const size_t outer_dim_size,
  45. mindspore::kernel::SparseGradient<int> *unique_sparse_grad);
  46. private:
  47. static std::unordered_map<std::string, int64_t> optimizer_to_ids;
  48. static std::unordered_map<int64_t, std::string> id_to_optimizers;
  49. static std::unordered_map<int64_t, std::string> id_to_optimizer_nodes;
  50. static int64_t rank_id_;
  51. };
  52. } // namespace ps
  53. } // namespace mindspore
  54. #endif // MINDSPORE_CCSRC_PS_UTIL_H_