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

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