• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020-2021 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 
17 #include "backend/kernel_compiler/aicpu/aicpu_kernel_metadata.h"
18 #include <memory>
19 #include <string>
20 #include "backend/kernel_compiler/oplib/oplib.h"
21 #include "backend/kernel_compiler/common_utils.h"
22 #include "backend/kernel_compiler/aicpu/aicpu_util.h"
23 #include "backend/session/anf_runtime_algorithm.h"
24 
25 namespace mindspore {
26 namespace kernel {
AicpuMetadataInfo(const CNodePtr & kernel_node,std::vector<std::shared_ptr<KernelBuildInfo>> * kernel_info_list)27 void AicpuMetadataInfo(const CNodePtr &kernel_node, std::vector<std::shared_ptr<KernelBuildInfo>> *kernel_info_list) {
28   MS_LOG(INFO) << "AicpuMetadataInfo.";
29   MS_EXCEPTION_IF_NULL(kernel_node);
30   MS_EXCEPTION_IF_NULL(kernel_info_list);
31   std::string op_name = AnfAlgo::GetCNodeName(kernel_node);
32   if (op_name == kInitDataSetQueue) {
33     op_name = kInitData;
34   }
35   auto op_info_ptr = mindspore::kernel::OpLib::FindOp(op_name, OpImplyType::kAICPU);
36   if (op_info_ptr == nullptr) {
37     MS_LOG(DEBUG) << "Aicpu does not have op [" << op_name << "]";
38     return;
39   }
40   // For compatibility with the current framework
41   if (op_name == kGetNext || kDynamicInputOps.find(op_name) != kDynamicInputOps.end()) {
42     AicpuMetadataInfoForSpecialNodes(kernel_node, kernel_info_list);
43     return;
44   }
45   if (!ParseMetadata(kernel_node, op_info_ptr, AICPU, kernel_info_list)) {
46     MS_LOG(WARNING) << "Aicpu parsed metadata op [" << op_name << "] failed";
47     return;
48   }
49 }
50 
AicpuMetadataInfoForSpecialNodes(const CNodePtr & kernel_node,std::vector<std::shared_ptr<KernelBuildInfo>> * kernel_info_list)51 void AicpuMetadataInfoForSpecialNodes(const CNodePtr &kernel_node,
52                                       std::vector<std::shared_ptr<KernelBuildInfo>> *kernel_info_list) {
53   MS_EXCEPTION_IF_NULL(kernel_info_list);
54   std::vector<std::string> inputs_format{};
55   std::vector<TypeId> inputs_type{};
56   auto op_name = AnfAlgo::GetCNodeName(kernel_node);
57   if (kDynamicInputOps.find(op_name) != kDynamicInputOps.end()) {
58     size_t input_num = AnfAlgo::GetInputTensorNum(kernel_node);
59     for (size_t input_index = 0; input_index < input_num; ++input_index) {
60       inputs_format.emplace_back(kOpFormat_DEFAULT);
61       (void)inputs_type.emplace_back(AnfAlgo::GetPrevNodeOutputInferDataType(kernel_node, input_index));
62     }
63   }
64   std::vector<std::string> outputs_format;
65   std::vector<TypeId> outputs_type;
66   size_t output_num = AnfAlgo::GetOutputTensorNum(kernel_node);
67   for (size_t output_index = 0; output_index < output_num; ++output_index) {
68     outputs_format.emplace_back(kOpFormat_DEFAULT);
69     (void)outputs_type.emplace_back(AnfAlgo::GetOutputInferDataType(kernel_node, output_index));
70   }
71   auto builder = KernelBuildInfo::KernelBuildInfoBuilder();
72   builder.SetInputsFormat(inputs_format);
73   builder.SetInputsDeviceType(inputs_type);
74   builder.SetOutputsFormat(outputs_format);
75   builder.SetOutputsDeviceType(outputs_type);
76   builder.SetProcessor(AICPU);
77   builder.SetKernelType(AICPU_KERNEL);
78   builder.SetFusionType(OPAQUE);
79   (void)kernel_info_list->emplace_back(builder.Build());
80   return;
81 }
82 }  // namespace kernel
83 }  // namespace mindspore
84