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 {
TransferReduceToAttr(LiteGraph::Node * node,std::vector<schema::Tensor * > * dst_tensors,std::vector<char * > * const tensor_bufs)22 int TransferReduceToAttr(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) << "fill don't need to convert attr to tensor.";
30 return RET_OK;
31 }
32 dst_tensors->clear();
33 auto prim = reinterpret_cast<const schema::v0::Primitive *>(node->primitive_);
34 MS_ASSERT(prim != nullptr);
35 auto param = prim->value_as_Reduce();
36 if (param == nullptr) {
37 MS_LOG(ERROR) << "param is nullptr";
38 return RET_ERROR;
39 }
40 auto axes_attr = param->axes();
41 if (axes_attr == nullptr) {
42 MS_LOG(ERROR) << "axes_attr is nullptr";
43 return RET_ERROR;
44 }
45 std::vector<int> axes = std::vector<int>(axes_attr->begin(), axes_attr->end());
46 auto axes_tensor = AttrToTensor(axes.data(), axes.size(), true, kNumberTypeInt32, tensor_bufs);
47 if (axes_tensor == nullptr) {
48 MS_LOG(ERROR) << "attr tensor is nullptr, transform is failed.";
49 return RET_NULL_PTR;
50 }
51 dst_tensors->push_back(axes_tensor);
52 return RET_OK;
53 }
54
55 Register ReduceTransferRegistry(SCHEMA_VERSION::SCHEMA_V0, schema::v0::PrimitiveType_Reduce, TransferReduceToAttr);
56 } // namespace lite
57 } // namespace mindspore
58