Elffuss BraidHub

kikocis/ncnn public

master · 5.0 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2022 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_torch_einsum.h"
16
17namespace pnnx {
18
19namespace ncnn {
20
21void convert_torch_einsum(Graph& graph)
22{
23 int op_index = 0;
24
25 for (Operator* op : graph.ops)
26 {
27 if (op->type != "torch.einsum")
28 continue;
29
30 std::string equation = op->params.at("equation").s;
31
32 const int batch_index = op->inputs[0]->params["__batch_index"].i;
33
34 if (batch_index != 233)
35 {
36 // drop batch index in equation
37 char batch_x = 'i' + batch_index;
38
39 std::string new_equation;
40 for (size_t i = 0; i < equation.size(); i++)
41 {
42 if (equation[i] == batch_x)
43 continue;
44
45 char x = equation[i];
46 if (x > batch_x)
47 x -= 1;
48
49 new_equation.push_back(x);
50 }
51
52 equation = new_equation;
53 }
54
55 op->name = std::string("einsum_") + std::to_string(op_index++);
56
57 if (equation == "ij->" || equation == "ijk->" || equation == "ijkl->")
58 {
59 // reduce sum
60 op->type = "Reduction";
61
62 op->params["0"] = 0;
63 op->params["1"] = 1;
64 op->params["4"] = 0;
65 }
66 else if (equation == "ii")
67 {
68 // trace
69 op->type = "Einsum";
70
71 std::vector<int> equation_int(equation.size());
72 for (size_t i = 0; i < equation.size(); i++)
73 {
74 equation_int[i] = equation[i];
75 }
76
77 op->params["0"] = equation_int;
78 }
79 else if (equation.find("->") == std::string::npos)
80 {
81 // permute
82 op->type = "Permute";
83
84 if (equation == "ji")
85 op->params["0"] = 1;
86
87 if (equation == "ikj")
88 op->params["0"] = 1;
89 else if (equation == "jik")
90 op->params["0"] = 2;
91 else if (equation == "kij")
92 op->params["0"] = 3;
93 else if (equation == "jki")
94 op->params["0"] = 4;
95 else if (equation == "kji")
96 op->params["0"] = 5;
97
98 if (equation == "ijlk")
99 op->params["0"] = 1;
100 else if (equation == "ikjl")
101 op->params["0"] = 2;
102 else if (equation == "iljk")
103 op->params["0"] = 3;
104 else if (equation == "iklj")
105 op->params["0"] = 4;
106 else if (equation == "ilkj")
107 op->params["0"] = 5;
108 else if (equation == "jikl")
109 op->params["0"] = 6;
110 else if (equation == "jilk")
111 op->params["0"] = 7;
112 else if (equation == "kijl")
113 op->params["0"] = 8;
114 else if (equation == "lijk")
115 op->params["0"] = 9;
116 else if (equation == "kilj")
117 op->params["0"] = 10;
118 else if (equation == "likj")
119 op->params["0"] = 11;
120 else if (equation == "jkil")
121 op->params["0"] = 12;
122 else if (equation == "jlik")
123 op->params["0"] = 13;
124 else if (equation == "kjil")
125 op->params["0"] = 14;
126 else if (equation == "ljik")
127 op->params["0"] = 15;
128 else if (equation == "klij")
129 op->params["0"] = 16;
130 else if (equation == "lkij")
131 op->params["0"] = 17;
132 else if (equation == "jkli")
133 op->params["0"] = 18;
134 else if (equation == "jlki")
135 op->params["0"] = 19;
136 else if (equation == "kjli")
137 op->params["0"] = 20;
138 else if (equation == "ljki")
139 op->params["0"] = 21;
140 else if (equation == "klji")
141 op->params["0"] = 22;
142 else if (equation == "lkji")
143 op->params["0"] = 23;
144 }
145 else
146 {
147 op->type = "Einsum";
148
149 std::vector<int> equation_int(equation.size());
150 for (size_t i = 0; i < equation.size(); i++)
151 {
152 equation_int[i] = equation[i];
153 }
154
155 op->params["0"] = equation_int;
156 }
157
158 op->params.erase("equation");
159 }
160}
161
162} // namespace ncnn
163
164} // namespace pnnx