blob: b525e6942694a456bade3f62ac03118969dd9617 [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
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +0100114#define DEF_FACTORY_TWO_TYPE_RESIZE_FP32(OP, DTYPE1, DTYPE2) \
TatWai Chongf7326092022-06-08 12:17:14 -0700115 if (inputDType == DType_##DTYPE1 && outputDType == DType_##DTYPE2) \
116 { \
117 return new OP<DType_##DTYPE1, DType_##DTYPE2, float>(sgt, attribute, id); \
Eric Kunzee5e26762020-10-13 16:11:07 -0700118 }
119
120#define DEF_FACTORY_RANK0_6_ONE_RANK_ONE_TYPE(OP, DTYPE) \
121 if (inputDType == DType_##DTYPE) \
122 { \
123 switch (inputRank) \
124 { \
125 DEF_FACTORY_ONE_RANK_ONE_TYPE(OP, 0, DTYPE) \
126 DEF_FACTORY_ONE_RANK_ONE_TYPE(OP, 1, DTYPE) \
127 DEF_FACTORY_ONE_RANK_ONE_TYPE(OP, 2, DTYPE) \
128 DEF_FACTORY_ONE_RANK_ONE_TYPE(OP, 3, DTYPE) \
129 DEF_FACTORY_ONE_RANK_ONE_TYPE(OP, 4, DTYPE) \
130 DEF_FACTORY_ONE_RANK_ONE_TYPE(OP, 5, DTYPE) \
131 DEF_FACTORY_ONE_RANK_ONE_TYPE(OP, 6, DTYPE) \
132 } \
133 }
134
135#define DEF_FACTORY_RANK1_6_ONE_RANK_ONE_TYPE(OP, DTYPE) \
136 if (inputDType == DType_##DTYPE) \
137 { \
138 switch (inputRank) \
139 { \
140 DEF_FACTORY_ONE_RANK_ONE_TYPE(OP, 1, DTYPE) \
141 DEF_FACTORY_ONE_RANK_ONE_TYPE(OP, 2, DTYPE) \
142 DEF_FACTORY_ONE_RANK_ONE_TYPE(OP, 3, DTYPE) \
143 DEF_FACTORY_ONE_RANK_ONE_TYPE(OP, 4, DTYPE) \
144 DEF_FACTORY_ONE_RANK_ONE_TYPE(OP, 5, DTYPE) \
145 DEF_FACTORY_ONE_RANK_ONE_TYPE(OP, 6, DTYPE) \
146 } \
147 }
148
149#define DEF_FACTORY_RANK0_6_ONE_RANK_TWO_TYPE(OP, DTYPE1, DTYPE2) \
150 if (inputDType == DType_##DTYPE1 && outputDType == DType_##DTYPE2) \
151 { \
152 switch (inputRank) \
153 { \
154 DEF_FACTORY_ONE_RANK_TWO_TYPE(OP, 0, DTYPE1, DTYPE2) \
155 DEF_FACTORY_ONE_RANK_TWO_TYPE(OP, 1, DTYPE1, DTYPE2) \
156 DEF_FACTORY_ONE_RANK_TWO_TYPE(OP, 2, DTYPE1, DTYPE2) \
157 DEF_FACTORY_ONE_RANK_TWO_TYPE(OP, 3, DTYPE1, DTYPE2) \
158 DEF_FACTORY_ONE_RANK_TWO_TYPE(OP, 4, DTYPE1, DTYPE2) \
159 DEF_FACTORY_ONE_RANK_TWO_TYPE(OP, 5, DTYPE1, DTYPE2) \
160 DEF_FACTORY_ONE_RANK_TWO_TYPE(OP, 6, DTYPE1, DTYPE2) \
161 } \
162 }
163
164#define DEF_FACTORY_RESHAPE(OP, DTYPE) \
165 if (inputDType == DType_##DTYPE && outputDType == DType_##DTYPE) \
166 { \
167 switch (inputRank) \
168 { \
169 case 0: \
170 { \
171 switch (outputRank) \
172 { \
173 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 0, 0, DTYPE) \
174 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 0, 1, DTYPE) \
175 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 0, 2, DTYPE) \
176 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 0, 3, DTYPE) \
177 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 0, 4, DTYPE) \
178 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 0, 5, DTYPE) \
179 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 0, 6, DTYPE) \
180 } \
181 } \
182 case 1: \
183 { \
184 switch (outputRank) \
185 { \
186 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 1, 0, DTYPE) \
187 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 1, 1, DTYPE) \
188 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 1, 2, DTYPE) \
189 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 1, 3, DTYPE) \
190 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 1, 4, DTYPE) \
191 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 1, 5, DTYPE) \
192 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 1, 6, DTYPE) \
193 } \
194 } \
195 case 2: \
196 { \
197 switch (outputRank) \
198 { \
199 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 2, 0, DTYPE) \
200 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 2, 1, DTYPE) \
201 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 2, 2, DTYPE) \
202 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 2, 3, DTYPE) \
203 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 2, 4, DTYPE) \
204 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 2, 5, DTYPE) \
205 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 2, 6, DTYPE) \
206 } \
207 } \
208 case 3: \
209 { \
210 switch (outputRank) \
211 { \
212 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 3, 0, DTYPE) \
213 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 3, 1, DTYPE) \
214 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 3, 2, DTYPE) \
215 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 3, 3, DTYPE) \
216 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 3, 4, DTYPE) \
217 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 3, 5, DTYPE) \
218 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 3, 6, DTYPE) \
219 } \
220 } \
221 case 4: \
222 { \
223 switch (outputRank) \
224 { \
225 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 4, 0, DTYPE) \
226 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 4, 1, DTYPE) \
227 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 4, 2, DTYPE) \
228 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 4, 3, DTYPE) \
229 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 4, 4, DTYPE) \
230 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 4, 5, DTYPE) \
231 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 4, 6, DTYPE) \
232 } \
233 } \
234 case 5: \
235 { \
236 switch (outputRank) \
237 { \
238 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 5, 0, DTYPE) \
239 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 5, 1, DTYPE) \
240 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 5, 2, DTYPE) \
241 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 5, 3, DTYPE) \
242 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 5, 4, DTYPE) \
243 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 5, 5, DTYPE) \
244 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 5, 6, DTYPE) \
245 } \
246 } \
247 case 6: \
248 { \
249 switch (outputRank) \
250 { \
251 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 6, 0, DTYPE) \
252 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 6, 1, DTYPE) \
253 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 6, 2, DTYPE) \
254 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 6, 3, DTYPE) \
255 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 6, 4, DTYPE) \
256 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 6, 5, DTYPE) \
257 DEF_FACTORY_TWO_RANK_ONE_TYPE(OP, 6, 6, DTYPE) \
258 } \
259 } \
260 } \
261 }
262
Eric Kunzee5e26762020-10-13 16:11:07 -0700263namespace TosaReference
264{
265
Kevin Chengacb550f2021-06-29 15:32:19 -0700266class SubgraphTraverser;
267class GraphNode;
268
Eric Kunzee5e26762020-10-13 16:11:07 -0700269class OpFactory
270{
271public:
Kevin Chengacb550f2021-06-29 15:32:19 -0700272 static GraphNode* newOp(SubgraphTraverser* sgt,
273 tosa::TosaSerializationHandler* tsh,
Eric Kunzee5e26762020-10-13 16:11:07 -0700274 tosa::Op opType,
275 tosa::TosaAttributeBase* attribute,
Eric Kunzee5e26762020-10-13 16:11:07 -0700276 uint64_t id,
277 tosa::DType inputDType,
278 int inputRank,
279 tosa::DType outputDType,
280 int outputRank,
281 tosa::DType weightDType,
282 int weightRank);
283};
284}; // namespace TosaReference
285
286#endif