Elffuss BraidHub

kikocis/ncnn public

master · 2.9 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
4//
5// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6// in compliance with the License. You may obtain a copy of the License at
7//
8// https://opensource.org/licenses/BSD-3-Clause
9//
10// Unless required by applicable law or agreed to in writing, software distributed
11// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13// specific language governing permissions and limitations under the License.
14
15#include "convert_attribute.h"
16
17namespace pnnx {
18
19namespace ncnn {
20
21void convert_attribute(Graph& graph)
22{
23 for (Operator* op : graph.ops)
24 {
25 if (op->type != "pnnx.Attribute")
26 continue;
27
28 op->type = "MemoryData";
29
30 const std::string& key = op->attrs.begin()->first;
31 const Attribute& data = op->attrs.begin()->second;
32
33 const int batch_index = op->outputs[0]->params["__batch_index"].i;
34
35 if ((int)data.shape.size() > 5)
36 {
37 fprintf(stderr, "pnnx attribute %d-rank tensor is not supported yet!\n", (int)data.shape.size());
38 return;
39 }
40
41 // drop batch index
42 std::vector<int> new_shape;
43 for (int i = 0; i < (int)data.shape.size(); i++)
44 {
45 if (i == batch_index && data.shape[i] == 1)
46 continue;
47
48 new_shape.push_back(data.shape[i]);
49 }
50
51 if (new_shape.size() == 5 && batch_index == 233)
52 {
53 if (new_shape[0] == 1)
54 {
55 fprintf(stderr, "assume pnnx attribute 5-rank tensor has batch_index 0\n");
56 new_shape.erase(new_shape.begin());
57 }
58 else
59 {
60 fprintf(stderr, "pnnx attribute 5-rank tensor is not supported yet!\n");
61 }
62 }
63
64 if (new_shape.size() == 0)
65 {
66 // scalar
67 op->params["0"] = 1;
68 }
69 if (new_shape.size() == 1)
70 {
71 op->params["0"] = new_shape[0];
72 }
73 if (new_shape.size() == 2)
74 {
75 op->params["0"] = new_shape[1];
76 op->params["1"] = new_shape[0];
77 }
78 if (new_shape.size() == 3)
79 {
80 op->params["0"] = new_shape[2];
81 op->params["1"] = new_shape[1];
82 op->params["2"] = new_shape[0];
83 }
84 if (new_shape.size() == 4)
85 {
86 op->params["0"] = new_shape[3];
87 op->params["1"] = new_shape[2];
88 op->params["11"] = new_shape[1];
89 op->params["2"] = new_shape[0];
90 }
91
92 if (key != "0")
93 {
94 op->attrs["0"] = data;
95 op->attrs.erase(key);
96 }
97 }
98}
99
100} // namespace ncnn
101
102} // namespace pnnx