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.

kernel_utils.h 3.6 kB

5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * Copyright 2020 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 GE_GRAPH_PASSES_FOLDING_KERNEL_KERNEL_UTILS_H_
  17. #define GE_GRAPH_PASSES_FOLDING_KERNEL_KERNEL_UTILS_H_
  18. #include <memory>
  19. #include <vector>
  20. #include "common/ge_inner_error_codes.h"
  21. #include "common/util.h"
  22. #include "framework/common/debug/ge_log.h"
  23. #include "graph/compute_graph.h"
  24. namespace ge {
  25. class KernelUtils {
  26. public:
  27. KernelUtils() = delete;
  28. ~KernelUtils() = delete;
  29. static Status CheckDimensionNodeInfo(const NodePtr &node_ptr);
  30. static bool CheckFormatSupported(const NodePtr &node_ptr);
  31. static bool CheckSizeForTransOp(const ConstGeTensorPtr &const_weight_ptr, const OpDescPtr &op_desc_ptr);
  32. static bool IsUnknownShape(const GeShape &shape);
  33. /**
  34. * Generating a sequence of numbers
  35. * @param [in] data_num the num of generate
  36. * @param [in] value the value to write to buffer
  37. * @param [out] output the tensor for save sequence of numbers
  38. * @author
  39. */
  40. template<typename T>
  41. static Status GenData(const int64_t data_num, const T value, const GeTensorPtr &output) {
  42. if (data_num > 0) {
  43. if (!CheckInt64MulOverflow(data_num, static_cast<int64_t>(sizeof(T)))) {
  44. GELOGE(PARAM_INVALID, "Int64MulOverflow, data_num(%ld) type_len(%zu)", data_num, sizeof(T));
  45. return PARAM_INVALID;
  46. }
  47. std::unique_ptr<T[]> buf(new (std::nothrow) T[data_num]());
  48. if (buf == nullptr) {
  49. GELOGE(MEMALLOC_FAILED, "new sizeof(T) * data_num(%ld) memory failed", sizeof(T) * data_num);
  50. return MEMALLOC_FAILED;
  51. }
  52. for (int64_t i = 0; i < data_num; ++i) {
  53. buf[i] = value;
  54. }
  55. Status ret = output->SetData(reinterpret_cast<uint8_t *>(buf.get()), data_num * sizeof(T));
  56. if (ret != SUCCESS) {
  57. GELOGE(ret, " buf must not be null.");
  58. return ret;
  59. }
  60. }
  61. return SUCCESS;
  62. }
  63. /**
  64. * Calculate dimension
  65. * @param [in] dims save the tensor of the dimension
  66. * @param [in] vec_dim results of each dimension
  67. * @param [out] data_num total size of data
  68. * @author
  69. */
  70. template <typename T>
  71. static Status CalcDims(const ConstGeTensorPtr dims, std::vector<int64_t> &vec_dim, int64_t &data_num) {
  72. data_num = 1;
  73. int32_t size = dims->GetData().size() / sizeof(T);
  74. for (int32_t i = 0; i < size; i++) {
  75. T dim = *(reinterpret_cast<const T *>(dims->GetData().data()) + i);
  76. if (dim < 0) {
  77. GELOGE(PARAM_INVALID, "input dim(%d) is negative(%ld)", i, static_cast<int64_t>(dim));
  78. return PARAM_INVALID;
  79. }
  80. if (dim == 0) {
  81. GELOGI("input dim(%d) is zero", i);
  82. data_num = 0;
  83. vec_dim.clear();
  84. break;
  85. }
  86. if (!CheckInt64MulOverflow(data_num, dim)) {
  87. GELOGE(PARAM_INVALID, "Int64MulOverflow, data_num(%ld) dim(%ld)", data_num, static_cast<int64_t>(dim));
  88. return PARAM_INVALID;
  89. }
  90. data_num *= dim;
  91. vec_dim.push_back(dim);
  92. }
  93. return SUCCESS;
  94. }
  95. };
  96. } // namespace ge
  97. #endif // GE_GRAPH_PASSES_FOLDING_KERNEL_KERNEL_UTILS_H_

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示