• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
TransferTransposeAttr(LiteGraph::Node * node,std::vector<schema::Tensor * > * dst_tensors,std::vector<char * > * const tensor_bufs)22 int TransferTransposeAttr(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) << "transpose 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_Transpose();
36   if (param == nullptr) {
37     MS_LOG(ERROR) << "param is nullptr";
38     return RET_ERROR;
39   }
40   auto perm_attr = param->perm();
41   if (perm_attr == nullptr) {
42     MS_LOG(ERROR) << "perm_attr is nullptr";
43     return RET_ERROR;
44   }
45   std::vector<int> dst_shape = std::vector<int>(perm_attr->begin(), perm_attr->end());
46   auto dst_shape_tensor = AttrToTensor(dst_shape.data(), dst_shape.size(), true, kNumberTypeInt32, tensor_bufs);
47   if (dst_shape_tensor == nullptr) {
48     MS_LOG(ERROR) << "attr tensor is nullptr, transform is failed.";
49     return RET_NULL_PTR;
50   }
51   dst_tensors->push_back(dst_shape_tensor);
52   return RET_OK;
53 }
54 
55 Register TransposeTransferRegistry(SCHEMA_VERSION::SCHEMA_V0, schema::v0::PrimitiveType_Transpose,
56                                    TransferTransposeAttr);
57 }  // namespace lite
58 }  // namespace mindspore
59