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.

concat_offset_kernel.cc 4.2 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
4 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/concat_offset_kernel.h"
  17. #include <memory>
  18. #include "framework/common/ge_inner_error_codes.h"
  19. #include "framework/common/op/ge_op_utils.h"
  20. #include "framework/common/types.h"
  21. #include "framework/common/debug/ge_log.h"
  22. #include "graph/utils/type_utils.h"
  23. #include "inc/kernel_factory.h"
  24. namespace ge {
  25. namespace {
  26. const size_t kConcatOffsetInputIndexZero = 0;
  27. const size_t kConcatOffsetInputIndexOne = 1;
  28. const int kNumOne = 1;
  29. } // namespace
  30. Status ConcatOffsetKernel::Compute(const OpDescPtr op_desc_ptr, const vector<ConstGeTensorPtr> &input,
  31. vector<GeTensorPtr> &v_output) {
  32. GELOGD("ConcatOffsetKernel in");
  33. if (op_desc_ptr == nullptr) {
  34. GELOGE(PARAM_INVALID, "input opdesc is nullptr.");
  35. return PARAM_INVALID;
  36. }
  37. // validate attrs
  38. int N = 0;
  39. if (!(AttrUtils::GetInt(op_desc_ptr, "N", N))) {
  40. GELOGW("Attr %s does not exist", "N");
  41. return NOT_CHANGED;
  42. }
  43. // follow IR def, the first input is concat_dim
  44. ConstGeTensorPtr input_0 = input[kConcatOffsetInputIndexZero];
  45. GE_CHECK_NOTNULL(input_0);
  46. int32_t concat_dim = *(const_cast<int32_t *>(reinterpret_cast<const int32_t *>(input_0->GetData().data())));
  47. // validate inputs
  48. if ((static_cast<int>(input.size()) != (N + kNumOne)) || (input.size() <= kConcatOffsetInputIndexOne)) {
  49. GELOGW("The number of input for concat offset must be equal to %d, and must be more than one", (N + kNumOne));
  50. return NOT_CHANGED;
  51. }
  52. // calculate ouput dim
  53. GeShape output_shape = input[kConcatOffsetInputIndexOne]->GetTensorDesc().GetShape();
  54. int64_t output_size = output_shape.GetShapeSize();
  55. if (concat_dim >= output_size) {
  56. GELOGW("Concat dim is bigger than the size of output_shape.");
  57. return NOT_CHANGED;
  58. }
  59. GELOGI("Output shape size is %ld.", output_size);
  60. int32_t offset = 0;
  61. if (output_size < 0) {
  62. GELOGE(FAILED, "Index is negative.");
  63. return FAILED;
  64. }
  65. unique_ptr<int32_t[]> buf(new (std::nothrow) int32_t[output_size]());
  66. if (buf == nullptr) {
  67. GELOGE(MEMALLOC_FAILED, "new buf failed");
  68. return INTERNAL_ERROR;
  69. }
  70. for (size_t i = 0; i < static_cast<size_t>(N); i++) {
  71. buf[concat_dim] = offset;
  72. // generate output, index 0 can always gets a GeTensorDesc object from any OpDescPtr.
  73. auto output_tensor_desc = op_desc_ptr->GetOutputDesc(0);
  74. GeTensorPtr output_ptr = MakeShared<GeTensor>(output_tensor_desc);
  75. if (output_ptr == nullptr) {
  76. GELOGW("Failed to fold node %s, out of memeory", op_desc_ptr->GetName().c_str());
  77. return NOT_CHANGED;
  78. }
  79. output_ptr->MutableTensorDesc().SetDataType(DT_INT32);
  80. output_ptr->MutableTensorDesc().SetShape(output_shape);
  81. GE_IF_BOOL_EXEC(output_ptr->SetData(reinterpret_cast<uint8_t *>(buf.get()),
  82. static_cast<size_t>(sizeof(DT_INT32) * output_size)) != GRAPH_SUCCESS,
  83. GELOGW("set data failed.");
  84. return NOT_CHANGED);
  85. v_output.push_back(output_ptr);
  86. // caculate offset
  87. const int32_t *input_shape =
  88. reinterpret_cast<const int32_t *>(input[i + kConcatOffsetInputIndexOne]->GetData().data());
  89. int64_t input_dim = input_shape[concat_dim]; // this index is valid, checked before
  90. if (input_dim > (INT64_MAX - offset)) {
  91. GELOGE(PARAM_INVALID, " %d and %ld addition can result in overflow!.", offset, input_dim);
  92. return INTERNAL_ERROR;
  93. }
  94. offset += input_dim;
  95. }
  96. GELOGD("ConcatOffsetKernel success");
  97. return SUCCESS;
  98. }
  99. REGISTER_KERNEL(CONCATOFFSET, ConcatOffsetKernel);
  100. } // namespace ge

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