blob: f399bd186cee2d5462b787199839698eff83a640 [file] [log] [blame]
Eric Kunzee5e26762020-10-13 16:11:07 -07001
James Ward8b390432022-08-12 20:48:56 +01002// Copyright (c) 2020-2022, ARM Limited.
Eric Kunzee5e26762020-10-13 16:11:07 -07003//
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#ifndef OPS_OP_FACTORY_H
17#define OPS_OP_FACTORY_H
18
19#include "attribute.h"
20#include "graph_node.h"
Eric Kunzee5e26762020-10-13 16:11:07 -070021#include "template_types.h"
22#include "tosa_serialization_handler.h"
23
24#define DEF_FACTORY_ONE_RANK_ONE_TYPE(OP, RANK, DTYPE) \
25 case RANK: \
Eric Kunzeb5fabec2022-06-07 05:20:44 +000026 return new OP<RANK, DType_##DTYPE>(sgt, attribute, id);
Eric Kunzee5e26762020-10-13 16:11:07 -070027
28#define DEF_FACTORY_ONE_RANK_TWO_TYPE(OP, RANK, DTYPE1, DTYPE2) \
29 case RANK: \
Eric Kunzeb5fabec2022-06-07 05:20:44 +000030 return new OP<RANK, DType_##DTYPE1, DType_##DTYPE2>(sgt, attribute, id);
Eric Kunzee5e26762020-10-13 16:11:07 -070031
32#define DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, RANK1, RANK2, DTYPE) \
33 case RANK2: \
Eric Kunzeb5fabec2022-06-07 05:20:44 +000034 return new OP<RANK1, RANK2, DType_##DTYPE>(sgt, attribute, id);
Eric Kunzee5e26762020-10-13 16:11:07 -070035
36#define DEF_FACTORY_TWO_RANK_TWO_TYPE(OP, RANK1, RANK2, DTYPE1, DTYPE2) \
37 case RANK2: \
Eric Kunzeb5fabec2022-06-07 05:20:44 +000038 return new OP<RANK1, RANK2, DType_##DTYPE1, DType_##DTYPE2>(sgt, attribute, id);
Eric Kunzee5e26762020-10-13 16:11:07 -070039
40#define DEF_FACTORY_ONE_RANK_0_6(OP) \
41 switch (inputRank) \
42 { \
43 case 0: \
TatWai Chongf7326092022-06-08 12:17:14 -070044 return new OP<0>(sgt, attribute, id); \
Eric Kunzee5e26762020-10-13 16:11:07 -070045 case 1: \
TatWai Chongf7326092022-06-08 12:17:14 -070046 return new OP<1>(sgt, attribute, id); \
Eric Kunzee5e26762020-10-13 16:11:07 -070047 case 2: \
TatWai Chongf7326092022-06-08 12:17:14 -070048 return new OP<2>(sgt, attribute, id); \
Eric Kunzee5e26762020-10-13 16:11:07 -070049 case 3: \
TatWai Chongf7326092022-06-08 12:17:14 -070050 return new OP<3>(sgt, attribute, id); \
Eric Kunzee5e26762020-10-13 16:11:07 -070051 case 4: \
TatWai Chongf7326092022-06-08 12:17:14 -070052 return new OP<4>(sgt, attribute, id); \
Eric Kunzee5e26762020-10-13 16:11:07 -070053 case 5: \
TatWai Chongf7326092022-06-08 12:17:14 -070054 return new OP<5>(sgt, attribute, id); \
Eric Kunzee5e26762020-10-13 16:11:07 -070055 case 6: \
TatWai Chongf7326092022-06-08 12:17:14 -070056 return new OP<6>(sgt, attribute, id); \
Eric Kunzee5e26762020-10-13 16:11:07 -070057 }
58
59#define DEF_FACTORY_ONE_TYPE(OP, DTYPE) \
60 if (inputDType == DType_##DTYPE) \
61 { \
TatWai Chongf7326092022-06-08 12:17:14 -070062 return new OP<DType_##DTYPE>(sgt, attribute, id); \
Eric Kunzee5e26762020-10-13 16:11:07 -070063 }
64
James Ward8b390432022-08-12 20:48:56 +010065#define DEF_FACTORY_ONE_TYPE_ONE_ACCUM(OP, ATTR_NAME, DTYPE, ACCUM_DTYPE) \
66 if (inputDType == DType_##DTYPE && ACCUM_FROM_ATTRIBUTE(ATTR_NAME) == DType_##ACCUM_DTYPE) \
67 { \
68 return new OP<DType_##DTYPE, DType_##ACCUM_DTYPE>(sgt, attribute, id); \
69 }
70
Eric Kunzee5e26762020-10-13 16:11:07 -070071#define DEF_FACTORY_TWO_TYPE(OP, DTYPE1, DTYPE2) \
72 if (inputDType == DType_##DTYPE1 && weightDType == DType_##DTYPE2) \
73 { \
TatWai Chongf7326092022-06-08 12:17:14 -070074 return new OP<DType_##DTYPE1, DType_##DTYPE2>(sgt, attribute, id); \
Eric Kunzee5e26762020-10-13 16:11:07 -070075 }
76
James Ward8b390432022-08-12 20:48:56 +010077#define DEF_FACTORY_TWO_TYPE_ONE_ACCUM(OP, ATTR_NAME, DTYPE1, DTYPE2, ACCUM_DTYPE) \
78 if (inputDType == DType_##DTYPE1 && weightDType == DType_##DTYPE2 \
79 && ACCUM_FROM_ATTRIBUTE(ATTR_NAME) == DType_##ACCUM_DTYPE) \
80 { \
81 return new OP<DType_##DTYPE1, DType_##DTYPE2, DType_##ACCUM_DTYPE>(sgt, attribute, id); \
82 } \
83
84// Statement-expression to evaluate accumulate attribute in-place
85#define ACCUM_FROM_ATTRIBUTE(ATTRIBUTE_NAME) \
86 ({ \
87 tosa::DType accumDType = tosa::DType_UNKNOWN; \
88 if (auto p = dynamic_cast<tosa::Tosa##ATTRIBUTE_NAME##Attribute*>(attribute)) \
89 { \
90 auto attr = new tosa::Tosa##ATTRIBUTE_NAME##Attribute(p); \
91 ASSERT_MEM(attr); \
92 accumDType = tosa::EnumValuesDType()[attr->accum_dtype()]; \
93 } \
94 else \
95 { \
96 FATAL_ERROR("Can't initialize Tosa" #ATTRIBUTE_NAME "Attribute.\nPre-initialization " \
97 "of this attribute is required in order to determine the accumulate type."); \
98 } \
99 accumDType; \
100 }) \
101
TatWai Chongf7326092022-06-08 12:17:14 -0700102#define DEF_FACTORY_TWO_TYPE_RESIZE_INT16(OP, DTYPE1, DTYPE2) \
Eric Kunzee5e26762020-10-13 16:11:07 -0700103 if (inputDType == DType_##DTYPE1 && outputDType == DType_##DTYPE2) \
104 { \
TatWai Chongf7326092022-06-08 12:17:14 -0700105 return new OP<DType_##DTYPE1, DType_##DTYPE2, int16_t>(sgt, attribute, id); \
106 }
107
James Ward8b390432022-08-12 20:48:56 +0100108#define DEF_FACTORY_TWO_TYPE_RESIZE_FP16(OP, DTYPE1, DTYPE2) \
109 if (inputDType == DType_##DTYPE1 && outputDType == DType_##DTYPE2) \
110 { \
111 return new OP<DType_##DTYPE1, DType_##DTYPE2, float>(sgt, attribute, id); \
112 }
113
James Ward24dbc422022-10-19 12:20:31 +0100114#define DEF_FACTORY_TWO_TYPE_RESIZE_BF16(OP, DTYPE1, DTYPE2) \
115 if (inputDType == DType_##DTYPE1 && outputDType == DType_##DTYPE2) \
116 { \
117 return new OP<DType_##DTYPE1, DType_##DTYPE2, Eigen::bfloat16>(sgt, attribute, id); \
118 }
119
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +0100120#define DEF_FACTORY_TWO_TYPE_RESIZE_FP32(OP, DTYPE1, DTYPE2) \
TatWai Chongf7326092022-06-08 12:17:14 -0700121 if (inputDType == DType_##DTYPE1 && outputDType == DType_##DTYPE2) \
122 { \
123 return new OP<DType_##DTYPE1, DType_##DTYPE2, float>(sgt, attribute, id); \
Eric Kunzee5e26762020-10-13 16:11:07 -0700124 }
125
126#define DEF_FACTORY_RANK0_6_ONE_RANK_ONE_TYPE(OP, DTYPE) \
127 if (inputDType == DType_##DTYPE) \
128 { \
129 switch (inputRank) \
130 { \
131 DEF_FACTORY_ONE_RANK_ONE_TYPE(OP, 0, DTYPE) \
132 DEF_FACTORY_ONE_RANK_ONE_TYPE(OP, 1, DTYPE) \
133 DEF_FACTORY_ONE_RANK_ONE_TYPE(OP, 2, DTYPE) \
134 DEF_FACTORY_ONE_RANK_ONE_TYPE(OP, 3, DTYPE) \
135 DEF_FACTORY_ONE_RANK_ONE_TYPE(OP, 4, DTYPE) \
136 DEF_FACTORY_ONE_RANK_ONE_TYPE(OP, 5, DTYPE) \
137 DEF_FACTORY_ONE_RANK_ONE_TYPE(OP, 6, DTYPE) \
138 } \
139 }
140
141#define DEF_FACTORY_RANK1_6_ONE_RANK_ONE_TYPE(OP, DTYPE) \
142 if (inputDType == DType_##DTYPE) \
143 { \
144 switch (inputRank) \
145 { \
146 DEF_FACTORY_ONE_RANK_ONE_TYPE(OP, 1, DTYPE) \
147 DEF_FACTORY_ONE_RANK_ONE_TYPE(OP, 2, DTYPE) \
148 DEF_FACTORY_ONE_RANK_ONE_TYPE(OP, 3, DTYPE) \
149 DEF_FACTORY_ONE_RANK_ONE_TYPE(OP, 4, DTYPE) \
150 DEF_FACTORY_ONE_RANK_ONE_TYPE(OP, 5, DTYPE) \
151 DEF_FACTORY_ONE_RANK_ONE_TYPE(OP, 6, DTYPE) \
152 } \
153 }
154
155#define DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OP, DTYPE1, DTYPE2) \
156 if (inputDType == DType_##DTYPE1 && outputDType == DType_##DTYPE2) \
157 { \
158 switch (inputRank) \
159 { \
160 DEF_FACTORY_ONE_RANK_TWO_TYPE(OP, 0, DTYPE1, DTYPE2) \
161 DEF_FACTORY_ONE_RANK_TWO_TYPE(OP, 1, DTYPE1, DTYPE2) \
162 DEF_FACTORY_ONE_RANK_TWO_TYPE(OP, 2, DTYPE1, DTYPE2) \
163 DEF_FACTORY_ONE_RANK_TWO_TYPE(OP, 3, DTYPE1, DTYPE2) \
164 DEF_FACTORY_ONE_RANK_TWO_TYPE(OP, 4, DTYPE1, DTYPE2) \
165 DEF_FACTORY_ONE_RANK_TWO_TYPE(OP, 5, DTYPE1, DTYPE2) \
166 DEF_FACTORY_ONE_RANK_TWO_TYPE(OP, 6, DTYPE1, DTYPE2) \
167 } \
168 }
169
170#define DEF_FACTORY_RESHAPE(OP, DTYPE) \
171 if (inputDType == DType_##DTYPE && outputDType == DType_##DTYPE) \
172 { \
173 switch (inputRank) \
174 { \
175 case 0: \
176 { \
177 switch (outputRank) \
178 { \
179 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 0, 0, DTYPE) \
180 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 0, 1, DTYPE) \
181 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 0, 2, DTYPE) \
182 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 0, 3, DTYPE) \
183 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 0, 4, DTYPE) \
184 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 0, 5, DTYPE) \
185 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 0, 6, DTYPE) \
186 } \
187 } \
188 case 1: \
189 { \
190 switch (outputRank) \
191 { \
192 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 1, 0, DTYPE) \
193 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 1, 1, DTYPE) \
194 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 1, 2, DTYPE) \
195 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 1, 3, DTYPE) \
196 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 1, 4, DTYPE) \
197 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 1, 5, DTYPE) \
198 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 1, 6, DTYPE) \
199 } \
200 } \
201 case 2: \
202 { \
203 switch (outputRank) \
204 { \
205 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 2, 0, DTYPE) \
206 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 2, 1, DTYPE) \
207 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 2, 2, DTYPE) \
208 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 2, 3, DTYPE) \
209 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 2, 4, DTYPE) \
210 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 2, 5, DTYPE) \
211 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 2, 6, DTYPE) \
212 } \
213 } \
214 case 3: \
215 { \
216 switch (outputRank) \
217 { \
218 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 3, 0, DTYPE) \
219 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 3, 1, DTYPE) \
220 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 3, 2, DTYPE) \
221 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 3, 3, DTYPE) \
222 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 3, 4, DTYPE) \
223 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 3, 5, DTYPE) \
224 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 3, 6, DTYPE) \
225 } \
226 } \
227 case 4: \
228 { \
229 switch (outputRank) \
230 { \
231 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 4, 0, DTYPE) \
232 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 4, 1, DTYPE) \
233 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 4, 2, DTYPE) \
234 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 4, 3, DTYPE) \
235 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 4, 4, DTYPE) \
236 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 4, 5, DTYPE) \
237 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 4, 6, DTYPE) \
238 } \
239 } \
240 case 5: \
241 { \
242 switch (outputRank) \
243 { \
244 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 5, 0, DTYPE) \
245 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 5, 1, DTYPE) \
246 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 5, 2, DTYPE) \
247 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 5, 3, DTYPE) \
248 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 5, 4, DTYPE) \
249 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 5, 5, DTYPE) \
250 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 5, 6, DTYPE) \
251 } \
252 } \
253 case 6: \
254 { \
255 switch (outputRank) \
256 { \
257 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 6, 0, DTYPE) \
258 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 6, 1, DTYPE) \
259 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 6, 2, DTYPE) \
260 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 6, 3, DTYPE) \
261 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 6, 4, DTYPE) \
262 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 6, 5, DTYPE) \
263 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 6, 6, DTYPE) \
264 } \
265 } \
266 } \
267 }
268
Eric Kunzee5e26762020-10-13 16:11:07 -0700269namespace TosaReference
270{
271
Kevin Chengacb550f2021-06-29 15:32:19 -0700272class SubgraphTraverser;
273class GraphNode;
274
Eric Kunzee5e26762020-10-13 16:11:07 -0700275class OpFactory
276{
277public:
Kevin Chengacb550f2021-06-29 15:32:19 -0700278 static GraphNode* newOp(SubgraphTraverser* sgt,
279 tosa::TosaSerializationHandler* tsh,
Eric Kunzee5e26762020-10-13 16:11:07 -0700280 tosa::Op opType,
281 tosa::TosaAttributeBase* attribute,
Eric Kunzee5e26762020-10-13 16:11:07 -0700282 uint64_t id,
283 tosa::DType inputDType,
284 int inputRank,
285 tosa::DType outputDType,
286 int outputRank,
287 tosa::DType weightDType,
288 int weightRank);
289};
290}; // namespace TosaReference
291
292#endif