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.

cast_kernel.cc 5.5 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
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * Copyright 2019-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/cast_kernel.h"
  17. #include <memory>
  18. #include <vector>
  19. #include "common/debug/log.h"
  20. #include "common/formats/formats.h"
  21. #include "common/formats/utils/formats_trans_utils.h"
  22. #include "common/fp16_t.h"
  23. #include "common/op/ge_op_utils.h"
  24. #include "common/types.h"
  25. #include "common/util.h"
  26. #include "framework/common/debug/ge_log.h"
  27. #include "framework/common/ge_inner_error_codes.h"
  28. #include "graph/common/bcast.h"
  29. #include "host_kernels/kernel_utils.h"
  30. #include "graph/utils/type_utils.h"
  31. #include "inc/kernel_factory.h"
  32. namespace ge {
  33. namespace {
  34. const size_t kCastInputSize = 1;
  35. }
  36. Status CastKernel::Compute(const OpDescPtr op_desc_ptr, const std::vector<ConstGeTensorPtr> &input,
  37. std::vector<GeTensorPtr> &v_output) {
  38. GELOGD("CastKernel begin.");
  39. if (input.size() != kCastInputSize) {
  40. GELOGE(PARAM_INVALID, "The number of input for cast must be %zu.", kCastInputSize);
  41. return PARAM_INVALID;
  42. }
  43. ConstGeTensorPtr const_weight_ptr = input[0];
  44. if (const_weight_ptr == nullptr) {
  45. GELOGE(PARAM_INVALID, "Input const_weight_ptr is nullptr.");
  46. return PARAM_INVALID;
  47. }
  48. const uint8_t *src_data = const_weight_ptr->GetData().data();
  49. // src_data == nullptr is supported
  50. if (op_desc_ptr == nullptr) {
  51. GELOGE(PARAM_INVALID, "Parameter's invalid, Input opDescPtr is nullptr.");
  52. return PARAM_INVALID;
  53. }
  54. GeTensorDesc op_desc = op_desc_ptr->GetOutputDesc(0);
  55. GeTensorDesc op_desc_in = op_desc_ptr->GetInputDesc(0);
  56. auto src_data_type = op_desc_in.GetDataType();
  57. auto src_shape = op_desc_in.GetShape();
  58. auto src_format = op_desc_in.GetFormat();
  59. auto data_type = op_desc.GetDataType();
  60. auto data_shape = op_desc.GetShape();
  61. auto data_format = op_desc.GetFormat();
  62. GELOGD(
  63. "Current node %s, format %s, input shape %s, data type %s, weight format %s, shape %s, data type %s. "
  64. "output format %s, shape %s, data type %s",
  65. op_desc_ptr->GetName().c_str(), TypeUtils::FormatToSerialString(src_format).c_str(),
  66. formats::ShapeToString(src_shape).c_str(), TypeUtils::DataTypeToSerialString(src_data_type).c_str(),
  67. TypeUtils::FormatToSerialString(const_weight_ptr->GetTensorDesc().GetFormat()).c_str(),
  68. formats::ShapeToString(const_weight_ptr->GetTensorDesc().GetShape()).c_str(),
  69. TypeUtils::DataTypeToSerialString(const_weight_ptr->GetTensorDesc().GetDataType()).c_str(),
  70. TypeUtils::FormatToSerialString(data_format).c_str(), formats::ShapeToString(data_shape).c_str(),
  71. TypeUtils::DataTypeToSerialString(data_type).c_str());
  72. // const_weight_ptr->GetData().GetSize() == 0 is supported
  73. auto src_data_size = src_shape.GetShapeSize();
  74. if (src_data_size == 0 &&
  75. static_cast<int>(const_weight_ptr->GetData().GetSize()) == GetSizeByDataType(src_data_type)) {
  76. src_data_size = 1;
  77. GELOGD("Weight of the current const node is scalar");
  78. }
  79. const formats::CastArgs cast_args{src_data, static_cast<size_t>(src_data_size), src_data_type, data_type};
  80. formats::TransResult trans_result;
  81. GELOGD("Trans data type from %s to %s, shape %s, data size %ld",
  82. TypeUtils::DataTypeToSerialString(src_data_type).c_str(), TypeUtils::DataTypeToSerialString(data_type).c_str(),
  83. formats::ShapeToString(src_shape).c_str(), src_data_size);
  84. if ((src_format != data_format) || (src_shape.GetDims() != data_shape.GetDims()) ||
  85. (!formats::IsTransDataTypeSupport(cast_args))) {
  86. GELOGW("Transfer from data type %s to %s, format %s to %s, shape %s to %s is not supported",
  87. TypeUtils::DataTypeToSerialString(src_data_type).c_str(),
  88. TypeUtils::DataTypeToSerialString(data_type).c_str(), TypeUtils::FormatToSerialString(src_format).c_str(),
  89. TypeUtils::FormatToSerialString(data_format).c_str(), formats::ShapeToString(src_shape).c_str(),
  90. formats::ShapeToString(data_shape).c_str());
  91. return NOT_CHANGED;
  92. }
  93. if (!KernelUtils::CheckSizeForTransOp(const_weight_ptr, op_desc_ptr)) {
  94. GELOGE(FAILED, "CheckSize failed, input size is not equal to weight size");
  95. return NOT_CHANGED;
  96. }
  97. if (formats::TransDataType(cast_args, trans_result) != SUCCESS) {
  98. GELOGE(INTERNAL_ERROR, "Failed to trans data type from %s to %s, shape %s, data size %ld.",
  99. TypeUtils::DataTypeToSerialString(src_data_type).c_str(),
  100. TypeUtils::DataTypeToSerialString(data_type).c_str(), formats::ShapeToString(src_shape).c_str(),
  101. src_data_size);
  102. return NOT_CHANGED;
  103. }
  104. GeTensorPtr output_ptr = MakeShared<GeTensor>(op_desc_ptr->GetOutputDesc(0));
  105. if (output_ptr == nullptr) {
  106. return FAILED;
  107. }
  108. if (output_ptr->SetData(trans_result.data.get(), trans_result.length) != SUCCESS) {
  109. GELOGW("Compute: SetData failed");
  110. }
  111. v_output.push_back(output_ptr);
  112. return SUCCESS;
  113. }
  114. REGISTER_KERNEL(CAST, CastKernel);
  115. } // namespace ge

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