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

4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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_BACKEND_KERNEL_COMPILER_COMMON_UTILS_H_
  17. #define MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_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 <algorithm>
  25. #include <vector>
  26. #include <utility>
  27. #include <nlohmann/json.hpp>
  28. #include "backend/kernel_compiler/kernel.h"
  29. #include "backend/kernel_compiler/oplib/opinfo.h"
  30. #include "backend/kernel_compiler/kernel_build_info.h"
  31. namespace mindspore {
  32. namespace kernel {
  33. constexpr auto kAkgKernelMeta = "kernel_meta/";
  34. constexpr auto kProcessorAiCore = "aicore";
  35. constexpr auto kProcessorAiCpu = "aicpu";
  36. constexpr auto kProcessorCuda = "cuda";
  37. constexpr auto kProcessorCpu = "cpu";
  38. constexpr auto kProcessorUnknown = "unknown";
  39. constexpr auto kJsonSuffix = ".json";
  40. constexpr auto kInfoSuffix = ".info";
  41. constexpr unsigned int AUTODIFF_COMPILE_OVERTIME = 600;
  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. std::string Search(const std::string &kernel_name) const;
  53. bool Insert(const std::string &kernel_name, const std::string &kernel_json);
  54. std::string kernel_meta_path() const { return kernel_meta_path_; }
  55. bool initialized() const { return initialized_; }
  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. std::string GetCompilerCachePath();
  67. bool CheckCache(const std::string &kernel_name);
  68. KernelPackPtr SearchCache(const std::string &kernel_name, const std::string &processor);
  69. KernelPackPtr InsertCache(const std::string &kernel_name, const std::string &processor);
  70. TypeId DtypeToTypeId(const std::string &dtypes);
  71. std::string Dtype2ShortType(const std::string &dtypes);
  72. size_t GetDtypeNbyte(const std::string &dtypes);
  73. bool GetShapeSize(const std::vector<size_t> &shape, const TypePtr &type_ptr, int64_t *size_i);
  74. bool ParseMetadata(const CNodePtr &kernel_node, const std::shared_ptr<const OpInfo> &op_info_ptr, Processor processor,
  75. std::vector<std::shared_ptr<KernelBuildInfo>> *const kernel_info_list);
  76. void SaveJsonInfo(const std::string &json_name, const std::string &info, const std::string &base_path);
  77. std::string GetProcessor(const AnfNodePtr &anf_node);
  78. Processor GetProcessor(const string &processor);
  79. bool IsSameShape(const std::vector<size_t> &shape_a, const std::vector<size_t> &shape_b);
  80. int Sign(float x);
  81. int GetReductionInt(const std::string &reduction);
  82. std::vector<std::pair<AnfNodePtr, size_t>> GetOutputIndex(const std::vector<AnfNodePtr> &node_list,
  83. const std::vector<AnfNodePtr> &input_list,
  84. const std::vector<AnfNodePtr> &output_list);
  85. void GetValidKernelNodes(const FuncGraphPtr &func_graph, std::vector<AnfNodePtr> *node_list);
  86. void GetValidKernelNodes(const FuncGraphPtr &func_graph, std::vector<AnfNodePtr> *node_list,
  87. std::vector<AnfNodePtr> *input_list, std::vector<AnfNodePtr> *output_list);
  88. void GetFuncGraphOutputNodes(const FuncGraphPtr &func_graph, std::vector<AnfNodePtr> *output_list);
  89. void GetGraphRealOutput(const FuncGraphPtr &func_graph, std::vector<std::pair<AnfNodePtr, size_t>> *node_list);
  90. bool IsWeightBoundary(const AnfNodePtr &node);
  91. std::vector<int64_t> GetReduceAttrAxis(const CNodePtr &cnode);
  92. std::string GetProcessorStr(const AnfNodePtr &anf_node);
  93. Processor GetProcessorFromContext();
  94. std::string GetStrProcessorFromContext();
  95. float Scaling(size_t in_size, size_t out_size, bool align_corners);
  96. float ScaleGrid(const int x, const float scale);
  97. FusionType GetFusionTypeByName(const std::string &name);
  98. std::string GetFusionNameByType(const kernel::FusionType &type);
  99. struct CachedInterpolation {
  100. size_t lower;
  101. size_t upper;
  102. float lerp;
  103. };
  104. void ComputeInterpolationWeights(const size_t out_size, const size_t in_size, const float scale,
  105. CachedInterpolation *interpolation);
  106. template <typename T>
  107. inline std::string Vector2Str(const std::vector<T> &inputs) {
  108. if (!inputs.empty()) {
  109. std::ostringstream oss;
  110. (void)std::copy(inputs.begin(), inputs.end() - 1, std::ostream_iterator<T>(oss, ", "));
  111. oss << inputs.back();
  112. return oss.str();
  113. }
  114. return "";
  115. }
  116. template <typename T>
  117. inline T ComputeLerp(T top_left, T top_right, T bottom_left, T bottom_right, T x_lerp, T y_lerp) {
  118. T top = top_left + (top_right - top_left) * x_lerp;
  119. T bottom = bottom_left + (bottom_right - bottom_left) * x_lerp;
  120. return top + (bottom - top) * y_lerp;
  121. }
  122. void CastShapeSizeToLong(const std::vector<size_t> &shape, std::vector<int64_t> *long_shape);
  123. void CheckSliceValid(const std::vector<int64_t> &start, const std::vector<int64_t> &stop,
  124. const std::vector<int64_t> &step, const std::vector<int64_t> &input_shape);
  125. size_t CalOffset(const std::vector<int64_t> &start, const std::vector<int64_t> &stop,
  126. const std::vector<int64_t> &dim_offset);
  127. std::vector<int64_t> CalDimOffset(const std::vector<int64_t> &input_shape);
  128. size_t GetCopySize(const std::vector<int64_t> &dim_offset, const std::vector<int64_t> &start,
  129. const std::vector<int64_t> &stop);
  130. size_t UnitSizeInBytes(const mindspore::TypeId &t);
  131. #define CHECK_KERNEL_INPUTS_NUM(actual_inputs_num, expect_inputs_num, kernel_name) \
  132. do { \
  133. if ((actual_inputs_num) != (expect_inputs_num)) { \
  134. MS_LOG(EXCEPTION) << (kernel_name) << " requires " << (expect_inputs_num) << " inputs, but got " \
  135. << (actual_inputs_num) << "."; \
  136. } \
  137. } while (0)
  138. #define CHECK_KERNEL_OUTPUTS_NUM(actual_outputs_num, expect_outputs_num, kernel_name) \
  139. do { \
  140. if ((actual_outputs_num) != (expect_outputs_num)) { \
  141. MS_LOG(EXCEPTION) << (kernel_name) << " should have " << (expect_outputs_num) << " outputs, but got " \
  142. << (actual_outputs_num) << "."; \
  143. } \
  144. } while (0)
  145. #define CHECK_KERNEL_WORKSPACE_SIZE(actual_size, expect_size, kernel_name) \
  146. do { \
  147. if ((actual_size) != (expect_size)) { \
  148. MS_LOG(EXCEPTION) << (kernel_name) << " requires " << (expect_size) << " workspace, but got " << (actual_size) \
  149. << "."; \
  150. } \
  151. } while (0)
  152. } // namespace kernel
  153. } // namespace mindspore
  154. #endif // MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_COMMON_UTILS_H_