• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2022-2023 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 "src/common/primitive_t_utils.h"
18 #ifdef PRIMITIVE_WRITEABLE
19 #include "src/common/ops/ops_utils.h"
20 #include "ops/primitive_c.h"
21 
22 namespace mindspore {
23 namespace lite {
24 constexpr size_t INITIAL_SIZE = 1024;
ConvertToPrimitive(const schema::PrimitiveT * primitive_t,flatbuffers::FlatBufferBuilder * fbb)25 const schema::Primitive *ConvertToPrimitive(const schema::PrimitiveT *primitive_t,
26                                             flatbuffers::FlatBufferBuilder *fbb) {
27   if (primitive_t == nullptr || fbb == nullptr) {
28     MS_LOG(ERROR) << "primitiveT or fbb is nullptr.";
29     return nullptr;
30   }
31   auto prim_offset = schema::CreatePrimitive(*fbb, primitive_t);
32   fbb->Finish(prim_offset);
33   auto prim_buf = fbb->GetBufferPointer();
34   return flatbuffers::GetRoot<schema::Primitive>(prim_buf);
35 }
36 
GetOpParameter(const schema::PrimitiveT * primitive_t)37 OpParameter *GetOpParameter(const schema::PrimitiveT *primitive_t) {
38   flatbuffers::FlatBufferBuilder fbb(INITIAL_SIZE);
39   auto primitive = ConvertToPrimitive(primitive_t, &fbb);
40   fbb.Clear();
41   auto prim_type = GetPrimitiveType(primitive, static_cast<int>(SCHEMA_VERSION::SCHEMA_CUR));
42   auto parame_gen =
43     PopulateRegistry::GetInstance()->GetParameterCreator(prim_type, static_cast<int>(SCHEMA_VERSION::SCHEMA_CUR));
44   if (parame_gen == nullptr) {
45     MS_LOG(ERROR) << "parameter generator is nullptr.";
46     return nullptr;
47   }
48   auto parameter = parame_gen(primitive);
49   if (parameter == nullptr) {
50     MS_LOG(ERROR) << "PopulateParameter return nullptr, type: "
51                   << GetPrimitiveTypeName(primitive, static_cast<int>(SCHEMA_VERSION::SCHEMA_CUR));
52   }
53   return parameter;
54 }
55 
GetPrimitiveT(const std::shared_ptr<mindspore::ops::BaseOperator> & op)56 std::unique_ptr<schema::PrimitiveT> GetPrimitiveT(const std::shared_ptr<mindspore::ops::BaseOperator> &op) {
57   if (op == nullptr) {
58     MS_LOG(ERROR) << "base operator is nullptr";
59     return nullptr;
60   }
61 
62   if (op->name().empty()) {
63     MS_LOG(ERROR) << "the name of operator is null";
64     return nullptr;
65   }
66 
67   auto creator = MSOpsRegistry::GetInstance()->GetPrimitiveCreator(op->name());
68   if (creator != nullptr) {
69     return creator(op->GetPrim());
70   } else {
71     MS_LOG(WARNING) << "can not find SingleOpRegistry for operator: " << op->name();
72     return nullptr;
73   }
74 }
75 
GetSchemaPrimType(const schema::PrimitiveT * primitive_t)76 schema::PrimitiveType GetSchemaPrimType(const schema::PrimitiveT *primitive_t) {
77   flatbuffers::FlatBufferBuilder fbb(INITIAL_SIZE);
78   auto primitive = ConvertToPrimitive(primitive_t, &fbb);
79   if (primitive == nullptr) {
80     MS_LOG(ERROR) << "Failed to convert Primitive.";
81     return schema::PrimitiveType::PrimitiveType_NONE;
82   }
83   fbb.Clear();
84   int prim_type = GetPrimitiveType(primitive, static_cast<int>(SCHEMA_VERSION::SCHEMA_CUR));
85   return static_cast<schema::PrimitiveType>(prim_type);
86 }
87 }  // namespace lite
88 }  // namespace mindspore
89 #endif
90