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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 <map>
  22. #include <string>
  23. #include <vector>
  24. #include "kernel/kernel.h"
  25. #include "kernel/oplib/opinfo.h"
  26. #include "kernel/kernel_build_info.h"
  27. namespace mindspore {
  28. namespace kernel {
  29. constexpr auto kCceKernelMeta = "./kernel_meta/";
  30. constexpr auto kGpuKernelMeta = "./cuda_meta";
  31. constexpr auto kProcessorAiCore = "aicore";
  32. constexpr auto kProcessorAiCpu = "aicpu";
  33. constexpr auto kProcessorCuda = "cuda";
  34. constexpr auto kJsonSuffix = ".json";
  35. constexpr auto kInfoSuffix = ".info";
  36. constexpr unsigned int AUTODIFF_COMPILE_OVERTIME = 600;
  37. constexpr auto kAkgModule = "_akg";
  38. constexpr auto kArgDataformat = "data_format";
  39. const std::vector<std::string> support_devices = {"aicore", "aicpu", "cuda"};
  40. struct KernelMetaInfo {
  41. uintptr_t func_stub_;
  42. uint32_t block_dim_;
  43. };
  44. using KernelMetaPtr = std::shared_ptr<KernelMetaInfo>;
  45. class KernelMeta {
  46. public:
  47. KernelMeta() = default;
  48. void Initialize();
  49. void RemoveKernelCache();
  50. std::string Search(const std::string &kernel_name) const;
  51. bool Insert(const std::string &kernel_name, const std::string &kernel_json);
  52. std::string GetKernelMetaPath() { return kernel_meta_path_; }
  53. static KernelMeta *GetInstance() {
  54. static KernelMeta kernel_meta;
  55. return &kernel_meta;
  56. }
  57. ~KernelMeta() = default;
  58. private:
  59. bool initialized_ = false;
  60. std::string kernel_meta_path_;
  61. std::unordered_map<std::string, std::string> kernel_meta_map_;
  62. };
  63. struct SparseGradient {
  64. float *value_;
  65. int *indices_;
  66. size_t indices_size_;
  67. };
  68. bool CheckCache(const std::string &kernel_name);
  69. KernelPackPtr SearchCache(const std::string &kernel_name, const std::string &processor);
  70. KernelPackPtr InsertCache(const std::string &kernel_name, const std::string &processor);
  71. TypeId DtypeToTypeId(const std::string &dtypes);
  72. std::string Dtype2String(const std::string &dtypes);
  73. std::string Dtype2ShortType(const std::string &dtypes);
  74. std::string TypeId2String(TypeId type_id);
  75. size_t GetDtypeNbyte(const std::string &dtypes);
  76. bool ParseMetadata(const CNodePtr &kernel_node, const std::shared_ptr<const OpInfo> &op_info_ptr, Processor processor,
  77. std::vector<std::shared_ptr<KernelBuildInfo>> *const kernel_info_list);
  78. bool IsAtomicNode(const CNodePtr &kernel_node);
  79. void SaveJsonInfo(const std::string &json_name, const std::string &info);
  80. std::string GetProcessor(const AnfNodePtr &anf_node);
  81. bool IsSameShape(const std::vector<size_t> &shape_a, const std::vector<size_t> &shape_b);
  82. int Sign(float x);
  83. void DeduplicateIndexedSlices(const SparseGradient &origin_sparse_grad, SparseGradient *unique_grad, size_t first_dim,
  84. size_t outer_dim);
  85. void ReduceSparseGradient(const SparseGradient &origin_sparse_grad, SparseGradient *unique_grad, size_t first_dim,
  86. size_t outer_dim);
  87. } // namespace kernel
  88. } // namespace mindspore
  89. #endif // MINDSPORE_CCSRC_KERNEL_COMMON_UTILS_H_