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

5 years ago
4 years ago
4 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * Copyright 2020-2021 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 <vector>
  20. #include <string>
  21. #include "utils/hash_map.h"
  22. #include "frontend/optimizer/optimizer.h"
  23. #include "backend/session/anf_runtime_algorithm.h"
  24. #include "backend/kernel_compiler/common_utils.h"
  25. #include "backend/kernel_compiler/cpu/sparse_optimizer_cpu_kernel.h"
  26. namespace mindspore {
  27. namespace ps {
  28. enum ParamType { kUnKnown = 0, kWeight = 1, kAccumulation = 2 };
  29. struct ParamInitInfo {
  30. std::string param_name_;
  31. ParamType param_type_{kUnKnown};
  32. size_t global_seed_{0};
  33. size_t op_seed_{0};
  34. float init_val_{0};
  35. };
  36. constexpr size_t kNodeInputWeightNameOffset = 1;
  37. constexpr size_t kNodeInputWeightIndexOffset = 2;
  38. class Util {
  39. public:
  40. static bool IsRoleOfPServer();
  41. static bool IsRoleOfScheduler();
  42. static int64_t optimizer_id(const std::string &name);
  43. static std::string optimizer_name(int64_t id);
  44. static std::string optimizer_node_name(int64_t id);
  45. static bool is_optimizer(const std::string &name);
  46. static int64_t LocalShard(int64_t first_dim, int64_t rank_id, int64_t server_num);
  47. static std::map<int64_t, int64_t> AllRankLocalShard(int64_t first_dim, int64_t rank_id, int64_t server_num);
  48. static void ReduceSparseGradient(float *gradients, int *indices, const size_t indices_size, size_t segment_size,
  49. const size_t first_dim_size, const size_t outer_dim_size,
  50. mindspore::kernel::SparseGradient<int> *unique_sparse_grad);
  51. static bool FuseServerCommOps(const pipeline::ResourcePtr &res);
  52. private:
  53. static void DoFusion(const FuncGraphPtr &func_graph, const std::string &cnode_name,
  54. const std::string &fused_cnode_name);
  55. static kernel::KernelBuildInfoPtr GenerateKernelBuildInfo(const std::vector<AnfNodePtr> &node_list);
  56. static mindspore::HashMap<std::string, int64_t> optimizer_to_ids;
  57. static mindspore::HashMap<int64_t, std::string> id_to_optimizers;
  58. static mindspore::HashMap<int64_t, std::string> id_to_optimizer_nodes;
  59. static int64_t rank_id_;
  60. };
  61. } // namespace ps
  62. } // namespace mindspore
  63. #endif // MINDSPORE_CCSRC_PS_UTIL_H_