Elffuss BraidHub

kikocis/ncnn public

master · 5.2 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 "normalize_einsum_equation.h"
16
17#include <ctype.h>
18#include <algorithm>
19#include <map>
20#include <vector>
21#include "pass_level2.h"
22
23namespace pnnx {
24
25static void replaceAll(std::string& str, const std::string& from, const std::string& to)
26{
27 size_t start_pos = 0;
28 while ((start_pos = str.find(from, start_pos)) != std::string::npos)
29 {
30 str.replace(start_pos, from.length(), to);
31 start_pos += to.length();
32 }
33}
34
35void normalize_einsum_equation(Graph& graph)
36{
37 for (size_t i = 0; i < graph.ops.size(); i++)
38 {
39 Operator* op = graph.ops[i];
40
41 if (op->type != "torch.einsum")
42 continue;
43
44 std::string equation = op->params.at("equation").s;
45
46 // remove all spaces
47 equation.erase(std::remove_if(equation.begin(), equation.end(), isspace), equation.end());
48
49 size_t equation_len = equation.size();
50
51 std::map<char, char> xset;
52
53 // find arrow ->
54 size_t arrow = equation.find("->");
55 if (arrow == std::string::npos)
56 {
57 // normalize to ijkl...
58 for (size_t i = 0; i < equation_len; i++)
59 {
60 char x = equation[i];
61
62 if ((x >= 'A' && x <= 'Z') || (x >= 'a' && x <= 'z'))
63 {
64 if (xset.find(x) == xset.end())
65 xset[x] = '\0';
66 }
67 }
68
69 char x = 'i';
70 for (auto& _xset : xset)
71 {
72 _xset.second = x++;
73 }
74 }
75 else
76 {
77 // find dot pair ...
78 size_t dot0 = equation.find("...");
79 if (dot0 != std::string::npos)
80 {
81 size_t dot1 = equation.find("...", dot0);
82 if (dot1 != std::string::npos)
83 {
84 // replace dot pair with alphabet
85 const int input_rank = op->inputs[0]->shape.size();
86 if (input_rank > 0)
87 {
88 int missing_bits = input_rank - (arrow - 3);
89
90 std::string missing_bits_word;
91 for (int i = 0; i < missing_bits; i++)
92 {
93 missing_bits_word += '0' + i;
94 }
95
96 replaceAll(equation, "...", missing_bits_word);
97 equation_len = equation.size();
98 arrow = equation.find("->");
99 }
100 }
101 }
102
103 std::string lhs = equation.substr(0, arrow);
104 std::string rhs = equation.substr(arrow + 2);
105
106 // normalize and sort to ijkl...
107 std::vector<char> xs;
108 for (size_t i = 0; i < rhs.size(); i++)
109 {
110 char x = rhs[i];
111
112 if ((x >= '0' && x <= '9') || (x >= 'A' && x <= 'Z') || (x >= 'a' && x <= 'z'))
113 {
114 if (xset.find(x) == xset.end())
115 {
116 xset[x] = '\0';
117 xs.push_back(x);
118 }
119 }
120 }
121
122 for (size_t i = 0; i < lhs.size(); i++)
123 {
124 char x = lhs[i];
125
126 if ((x >= '0' && x <= '9') || (x >= 'A' && x <= 'Z') || (x >= 'a' && x <= 'z'))
127 {
128 if (xset.find(x) == xset.end())
129 {
130 xset[x] = '\0';
131 xs.push_back(x);
132 }
133 }
134 }
135
136 for (auto& _xset : xset)
137 {
138 char x = _xset.first;
139 size_t xsi = 0;
140 for (size_t i = 0; i < xs.size(); i++)
141 {
142 if (xs[i] == x)
143 {
144 xsi = i;
145 break;
146 }
147 }
148
149 _xset.second = 'i' + xsi;
150 }
151 }
152
153 for (size_t i = 0; i < equation_len; i++)
154 {
155 char x = equation[i];
156
157 if ((x >= '0' && x <= '9') || (x >= 'A' && x <= 'Z') || (x >= 'a' && x <= 'z'))
158 equation[i] = xset[x];
159 }
160
161 // shorten jik->ijk to jik
162 if (arrow != std::string::npos && op->inputs.size() == 1)
163 {
164 std::string lhs = equation.substr(0, arrow);
165 std::string rhs = equation.substr(arrow + 2);
166
167 if (lhs.size() == rhs.size())
168 {
169 equation = lhs;
170 }
171 }
172
173 op->params["equation"] = equation;
174 }
175}
176
177} // namespace pnnx