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.

reformat_kernel.cc 3.9 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #include "host_kernels/reformat_kernel.h"
  17. #include "common/formats/utils/formats_trans_utils.h"
  18. #include "common/ge/ge_util.h"
  19. #include "framework/common/ge_inner_error_codes.h"
  20. #include "framework/common/op/ge_op_utils.h"
  21. #include "framework/common/types.h"
  22. #include "framework/common/util.h"
  23. #include "framework/common/debug/ge_log.h"
  24. #include "host_kernels/kernel_utils.h"
  25. #include "graph/utils/type_utils.h"
  26. #include "inc/kernel_factory.h"
  27. namespace ge {
  28. namespace {
  29. const size_t kReFormatInputSize = 1;
  30. const size_t kReformatFirstInput = 0;
  31. const size_t kReformatFirstOutput = 0;
  32. } // namespace
  33. Status ReFormatKernel::ValidateInput(const OpDescPtr &op_desc_ptr, const std::vector<ConstGeTensorPtr> &input) const {
  34. GE_CHECK_NOTNULL(op_desc_ptr);
  35. if (op_desc_ptr->GetInputsSize() != kReFormatInputSize) {
  36. GELOGW("trans_op has more than 1 input_size.");
  37. return PARAM_INVALID;
  38. }
  39. if (input.empty()) {
  40. GELOGE(PARAM_INVALID, "Input tensor vector is empty");
  41. return PARAM_INVALID;
  42. }
  43. return SUCCESS;
  44. }
  45. Status ReFormatKernel::Compute(const OpDescPtr op_desc_ptr, const std::vector<ConstGeTensorPtr> &input,
  46. std::vector<GeTensorPtr> &v_output) {
  47. GELOGD("ReFormatKernel begin.");
  48. Status status = ValidateInput(op_desc_ptr, input);
  49. if (status != SUCCESS) {
  50. return status;
  51. }
  52. ConstGeTensorPtr const_weight_ptr = input[kReformatFirstInput];
  53. if (const_weight_ptr == nullptr) {
  54. GELOGW("Parameter's invalid, Input_0 is nullptr.");
  55. return NOT_CHANGED;
  56. }
  57. const auto &op_desc = op_desc_ptr->MutableOutputDesc(kReformatFirstOutput);
  58. const auto &op_desc_in = op_desc_ptr->MutableInputDesc(kReformatFirstInput);
  59. GE_CHECK_NOTNULL(op_desc);
  60. GE_CHECK_NOTNULL(op_desc_in);
  61. const auto &src_shape = op_desc_in->GetShape().GetDims();
  62. const auto &src_dtype = op_desc_in->GetDataType();
  63. const auto &dst_shape = op_desc->GetShape().GetDims();
  64. const auto &dst_dtype = op_desc->GetDataType();
  65. if (src_dtype != dst_dtype || src_shape != dst_shape) {
  66. GELOGW("Check params failed. src data type %s and shape %s should be equal to dst data type %s and shape %s",
  67. TypeUtils::DataTypeToSerialString(src_dtype).c_str(), formats::ShapeToString(src_shape).c_str(),
  68. TypeUtils::DataTypeToSerialString(dst_dtype).c_str(), formats::ShapeToString(dst_shape).c_str());
  69. return NOT_CHANGED;
  70. }
  71. if (!KernelUtils::CheckSizeForTransOp(const_weight_ptr, op_desc_ptr)) {
  72. GELOGW("CheckSize failed, input size(shape %s) is not equal to weight size(shape %s)",
  73. formats::ShapeToString(src_shape).c_str(),
  74. formats::ShapeToString(const_weight_ptr->GetTensorDesc().GetShape()).c_str());
  75. return NOT_CHANGED;
  76. }
  77. GeTensorPtr output_ptr = MakeShared<GeTensor>(op_desc_ptr->GetOutputDesc(kReformatFirstOutput));
  78. if (output_ptr == nullptr) {
  79. GELOGW("Create shared ptr for GeTensor failed");
  80. return NOT_CHANGED;
  81. }
  82. GE_IF_BOOL_EXEC(output_ptr->SetData(input.at(0)->GetData()) != GRAPH_SUCCESS,
  83. GELOGW("set data failed");
  84. return NOT_CHANGED);
  85. v_output.emplace_back(output_ptr);
  86. GELOGD("ReFormatKernel success.");
  87. return SUCCESS;
  88. }
  89. REGISTER_KERNEL(REFORMAT, ReFormatKernel);
  90. } // namespace ge

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