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 #include "src/common/log_util.h"
20
21 namespace mindspore {
22 namespace lite {
TransferReshapeAttr(LiteGraph::Node * node,std::vector<schema::Tensor * > * dst_tensors,std::vector<char * > * const tensor_bufs)23 int TransferReshapeAttr(LiteGraph::Node *node, std::vector<schema::Tensor *> *dst_tensors,
24 std::vector<char *> *const tensor_bufs) {
25 if (node == nullptr || node->primitive_ == nullptr || dst_tensors == nullptr || tensor_bufs == nullptr) {
26 MS_LOG(ERROR) << "the parameter of this function is nullptr.";
27 return RET_ERROR;
28 }
29 if (node->input_indices_.size() != 1) {
30 MS_LOG(DEBUG) << "reshape don't need to convert attr to tensor.";
31 return RET_OK;
32 }
33 dst_tensors->clear();
34 auto prim = reinterpret_cast<const schema::v0::Primitive *>(node->primitive_);
35 CHECK_NULL_RETURN(prim);
36 auto param = prim->value_as_Reshape();
37 if (param == nullptr) {
38 MS_LOG(ERROR) << "param is nullptr";
39 return RET_ERROR;
40 }
41 auto dst_shape_attr = param->shape();
42 if (dst_shape_attr == nullptr) {
43 MS_LOG(ERROR) << "dst_shape_attr is nullptr";
44 return RET_ERROR;
45 }
46 std::vector<int> dst_shape = std::vector<int>(dst_shape_attr->begin(), dst_shape_attr->end());
47 auto dst_shape_tensor = AttrToTensor(dst_shape.data(), dst_shape.size(), true, kNumberTypeInt32, tensor_bufs);
48 if (dst_shape_tensor == nullptr) {
49 MS_LOG(ERROR) << "attr tensor is nullptr, transform is failed.";
50 return RET_NULL_PTR;
51 }
52 dst_tensors->push_back(dst_shape_tensor);
53 return RET_OK;
54 }
55
56 Register ReshapeTransferRegistry(SCHEMA_VERSION::SCHEMA_V0, schema::v0::PrimitiveType_Reshape, TransferReshapeAttr);
57 } // namespace lite
58 } // namespace mindspore
59