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.

unpack_kernel.cc 3.5 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/unpack_kernel.h"
  17. #include "common/debug/ge_log.h"
  18. #include "common/op/ge_op_utils.h"
  19. #include "common/op/ge_op_utils.h"
  20. #include "common/types.h"
  21. #include "graph/debug/ge_attr_define.h"
  22. #include "inc/kernel_factory.h"
  23. namespace ge {
  24. namespace {
  25. const size_t kUnpackInputNum = 1;
  26. } // namespace
  27. template <typename T>
  28. Status CalcUpack(const int32_t num, const DataType data_type, const T *value, std::vector<GeTensorPtr> &v_output) {
  29. GE_CHECK_NOTNULL(value);
  30. // not support num=0
  31. if (num > 0) {
  32. unique_ptr<T[]> buf(new (std::nothrow) T[num]());
  33. GE_CHECK_NOTNULL(buf);
  34. for (int32_t i = 0; i < num; ++i) {
  35. GeTensorPtr output_ptr = ge::MakeShared<ge::GeTensor>();
  36. GE_CHECK_NOTNULL(output_ptr);
  37. buf[i] = *value;
  38. ++value;
  39. GE_CHK_STATUS_RET(output_ptr->SetData(reinterpret_cast<uint8_t *>(&buf[i]), sizeof(T)),
  40. "unpack set data failed!");
  41. output_ptr->MutableTensorDesc().SetDataType(data_type);
  42. v_output.push_back(output_ptr);
  43. }
  44. } else {
  45. GELOGW("num <= 0 is not support.");
  46. return NOT_CHANGED;
  47. }
  48. return SUCCESS;
  49. }
  50. Status UnpackKernel::Compute(const OpDescPtr attr, const std::vector<ge::ConstGeTensorPtr> &input,
  51. std::vector<ge::GeTensorPtr> &v_output) {
  52. GE_CHECK_NOTNULL(attr);
  53. // check input num
  54. GE_RT_PARAM_INVALID_WITH_LOG_IF_FALSE(input.size() == kUnpackInputNum,
  55. "The number of input for unpack must be %zu, real is %zu.", kUnpackInputNum,
  56. input.size());
  57. ConstGeTensorPtr dims = input[0];
  58. GE_CHECK_NOTNULL(dims);
  59. if (dims->GetTensorDesc().GetShape().GetDimNum() != 1) {
  60. GELOGW("input tensor not 1 dim");
  61. return NOT_CHANGED;
  62. }
  63. ge::DataType data_type;
  64. GE_CHK_BOOL_RET_STATUS(AttrUtils::GetDataType(attr, ATTR_NAME_T, data_type), PARAM_INVALID, "get T attr failed.");
  65. // data_type must be FLOAT or INT32
  66. GE_CHK_BOOL_RET_STATUS((data_type == DT_FLOAT || data_type == DT_INT32), PARAM_INVALID, "T must be float or int32.");
  67. int64_t num = 0;
  68. GE_CHK_BOOL_RET_STATUS(AttrUtils::GetInt(attr, UNPACK_ATTR_NAME_NUM, num), PARAM_INVALID, "get num attr failed.");
  69. size_t data_count = dims->GetData().size() / sizeof(float);
  70. // num must equal to input_data size
  71. GE_RT_PARAM_INVALID_WITH_LOG_IF_FALSE(data_count == static_cast<size_t>(num),
  72. "input tensor size not equal num, data_count:%zu, num:%ld.", data_count, num);
  73. // calculate result
  74. if (data_type == DT_FLOAT) {
  75. GE_RETURN_IF_ERROR(CalcUpack(num, data_type, reinterpret_cast<const float *>(dims->GetData().data()), v_output));
  76. } else {
  77. GE_RETURN_IF_ERROR(CalcUpack(num, data_type, reinterpret_cast<const int32_t *>(dims->GetData().data()), v_output));
  78. }
  79. return SUCCESS;
  80. }
  81. REGISTER_KERNEL(UNPACK, UnpackKernel);
  82. } // namespace ge

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