1 /**
2 * Copyright 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 "schema/model_v0_generated.h"
18 #include "src/ops/compat/attr_transfer_common.h"
19
20 namespace mindspore {
21 namespace lite {
TransferTopkAttr(LiteGraph::Node * node,std::vector<schema::Tensor * > * dst_tensors,std::vector<char * > * const tensor_bufs)22 int TransferTopkAttr(LiteGraph::Node *node, std::vector<schema::Tensor *> *dst_tensors,
23 std::vector<char *> *const tensor_bufs) {
24 if (node == nullptr || node->primitive_ == nullptr || dst_tensors == nullptr || tensor_bufs == nullptr) {
25 MS_LOG(ERROR) << "the parameter of this function is nullptr.";
26 return RET_ERROR;
27 }
28 if (node->input_indices_.size() != 1) {
29 MS_LOG(DEBUG) << "topK need to convert attr to tensor.";
30 return RET_OK;
31 }
32 MS_ASSERT(dst_tensors->size() == 0);
33 auto prim = reinterpret_cast<const schema::v0::Primitive *>(node->primitive_);
34 MS_ASSERT(prim != nullptr);
35 auto param = prim->value_as_TopK();
36 if (param == nullptr) {
37 MS_LOG(ERROR) << "param is nullptr";
38 return RET_ERROR;
39 }
40 int32_t topk_k = param->k();
41 auto k_tensor = AttrToTensor(&topk_k, 1, false, kNumberTypeInt32, tensor_bufs);
42 if (k_tensor == nullptr) {
43 MS_LOG(ERROR) << "attr tensor is nullptr, transform is failed.";
44 return RET_NULL_PTR;
45 }
46 dst_tensors->push_back(k_tensor);
47 return RET_OK;
48 }
49
50 Register TopkTransferRegistry(SCHEMA_VERSION::SCHEMA_V0, schema::v0::PrimitiveType_TopK, TransferTopkAttr);
51 } // namespace lite
52 } // namespace mindspore
53