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.

common_utils.h 5.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * Copyright 2019 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_KERNEL_COMMON_UTILS_H_
  17. #define MINDSPORE_CCSRC_KERNEL_COMMON_UTILS_H_
  18. #include <dirent.h>
  19. #include <memory>
  20. #include <unordered_map>
  21. #include <unordered_set>
  22. #include <map>
  23. #include <string>
  24. #include <vector>
  25. #include <utility>
  26. #include <nlohmann/json.hpp>
  27. #include "kernel/kernel.h"
  28. #include "kernel/oplib/opinfo.h"
  29. #include "kernel/kernel_build_info.h"
  30. namespace mindspore {
  31. namespace kernel {
  32. constexpr auto kCceKernelMeta = "./kernel_meta/";
  33. constexpr auto kGpuKernelMeta = "./cuda_meta";
  34. constexpr auto kProcessorAiCore = "aicore";
  35. constexpr auto kProcessorAiCpu = "aicpu";
  36. constexpr auto kProcessorCuda = "cuda";
  37. constexpr auto kJsonSuffix = ".json";
  38. constexpr auto kInfoSuffix = ".info";
  39. constexpr unsigned int AUTODIFF_COMPILE_OVERTIME = 600;
  40. constexpr auto kAkgModule = "_akg";
  41. constexpr auto kArgDataformat = "data_format";
  42. const std::vector<std::string> support_devices = {"aicore", "aicpu", "cuda"};
  43. struct KernelMetaInfo {
  44. uintptr_t func_stub_;
  45. uint32_t block_dim_;
  46. };
  47. using KernelMetaPtr = std::shared_ptr<KernelMetaInfo>;
  48. class KernelMeta {
  49. public:
  50. KernelMeta() = default;
  51. void Initialize();
  52. void RemoveKernelCache();
  53. std::string Search(const std::string &kernel_name) const;
  54. bool Insert(const std::string &kernel_name, const std::string &kernel_json);
  55. std::string GetKernelMetaPath() { return kernel_meta_path_; }
  56. static KernelMeta *GetInstance() {
  57. static KernelMeta kernel_meta;
  58. return &kernel_meta;
  59. }
  60. ~KernelMeta() = default;
  61. private:
  62. bool initialized_ = false;
  63. std::string kernel_meta_path_;
  64. std::unordered_map<std::string, std::string> kernel_meta_map_;
  65. };
  66. struct SparseGradient {
  67. float *value_;
  68. int *indices_;
  69. size_t indices_size_;
  70. };
  71. struct MultiThreadComputeParams {
  72. float *var_;
  73. float *accum_;
  74. float *linear_;
  75. float *m_;
  76. float *m_t_;
  77. float *v_;
  78. float lr_;
  79. float l1_;
  80. float l2_;
  81. float lr_power_;
  82. float beta1_;
  83. float beta2_;
  84. float epsilon_;
  85. SparseGradient sparse_grad_;
  86. size_t var_first_dim_size_;
  87. size_t var_outer_dim_size_;
  88. bool use_nesterov_;
  89. };
  90. using MultiThreadComputeFunc = std::function<void(MultiThreadComputeParams *param, size_t start, size_t end)>;
  91. bool CheckCache(const std::string &kernel_name);
  92. KernelPackPtr SearchCache(const std::string &kernel_name, const std::string &processor);
  93. KernelPackPtr InsertCache(const std::string &kernel_name, const std::string &processor);
  94. TypeId DtypeToTypeId(const std::string &dtypes);
  95. std::string Dtype2ShortType(const std::string &dtypes);
  96. std::string TypeId2String(TypeId type_id);
  97. size_t GetDtypeNbyte(const std::string &dtypes);
  98. bool ParseMetadata(const CNodePtr &kernel_node, const std::shared_ptr<const OpInfo> &op_info_ptr, Processor processor,
  99. std::vector<std::shared_ptr<KernelBuildInfo>> *const kernel_info_list);
  100. void SaveJsonInfo(const std::string &json_name, const std::string &info);
  101. std::string GetProcessor(const AnfNodePtr &anf_node);
  102. bool IsSameShape(const std::vector<size_t> &shape_a, const std::vector<size_t> &shape_b);
  103. int Sign(float x);
  104. void DeduplicateIndexedSlices(const SparseGradient &origin_sparse_grad, SparseGradient *unique_grad, size_t first_dim,
  105. size_t outer_dim);
  106. void ReduceSparseGradient(const SparseGradient &origin_sparse_grad, SparseGradient *unique_grad, size_t first_dim,
  107. size_t outer_dim);
  108. std::pair<AnfNodePtr, size_t> GetKernelInput(const AnfNodePtr &anf_node, size_t index);
  109. std::vector<std::pair<AnfNodePtr, std::pair<size_t, size_t>>> GetInputIndex(const std::vector<AnfNodePtr> &node_list,
  110. const std::vector<AnfNodePtr> &input_list);
  111. std::vector<std::pair<AnfNodePtr, size_t>> GetOutputIndex(const std::vector<AnfNodePtr> &node_list,
  112. const std::vector<AnfNodePtr> &input_list,
  113. const std::vector<AnfNodePtr> &output_list);
  114. void GetValidKernelNodes(const FuncGraphPtr &func_graph, std::vector<AnfNodePtr> *node_list,
  115. std::vector<AnfNodePtr> *input_list, std::vector<AnfNodePtr> *output_list);
  116. void GetValidKernelNodes(const FuncGraphPtr &func_graph, std::vector<AnfNodePtr> *node_list);
  117. bool GetInputTensorValue(const AnfNodePtr &anf_node, size_t input_idx, nlohmann::json *const node_json);
  118. void GetGraphRealOutput(const FuncGraphPtr &func_graph, std::vector<std::pair<AnfNodePtr, size_t>> *node_list);
  119. bool IsWeightBoundary(const AnfNodePtr &node);
  120. void MultiThreadCompute(const MultiThreadComputeFunc &func, MultiThreadComputeParams *params,
  121. size_t total_compute_size);
  122. } // namespace kernel
  123. } // namespace mindspore
  124. #endif // MINDSPORE_CCSRC_KERNEL_COMMON_UTILS_H_