summaryrefslogtreecommitdiff
path: root/compiler/exo/src/Dialect/IR/TFLNodes.h
blob: 5f521a0a6dc17d3ef447f069c5b3fafe48bd4456 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/*
 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __LOCOEX_IR_TFLNODES_H__
#define __LOCOEX_IR_TFLNODES_H__

#include "TFLNodeDecl.h"
#include "TFLOpcode.h"

#include "FusedActFunc.h"
#include "NodeMixins.h"

#include <loco/IR/Node.h>
#include <loco/IR/NodeMixins.h>
#include <loco/IR/DataTypeTraits.h>

#include <locoex/VariadicArityNode.h>

#include <array>

namespace locoex
{

enum class Padding
{
  UNDEFINED, // This is not defined by TFLite. This was added to prevent programming error.
  SAME,
  VALID,
};

class Filter final
{
public:
  Filter() : _w(1), _h(1) {}

  int32_t w() const { return _w; }
  void w(int32_t w) { _w = w; }

  int32_t h() const { return _h; }
  void h(int32_t h) { _h = h; }

private:
  int32_t _w;
  int32_t _h;
};

class Stride final
{
public:
  Stride() : _w(1), _h(1) {}

  int32_t w() const { return _w; }
  void w(int32_t w) { _w = w; }

  int32_t h() const { return _h; }
  void h(int32_t h) { _h = h; }

private:
  int32_t _w;
  int32_t _h;
};

/// @brief enumeration of mixin class
enum class TFLNodeTrait
{
  FusedActFunc,
  Bias
};

template <TFLNodeTrait T> class TFLNodeMixin;

template <> class TFLNodeMixin<TFLNodeTrait::FusedActFunc>
{
public:
  TFLNodeMixin() = default;

public:
  FusedActFunc fusedActivationFunction() const { return _fused_act_fun; }
  void fusedActivationFunction(FusedActFunc fused_act_fun) { _fused_act_fun = fused_act_fun; }

private:
  FusedActFunc _fused_act_fun = FusedActFunc::UNDEFINED;
};

/**
 * @brief Mixin class for nodes that has a bias input
 */
template <> class TFLNodeMixin<TFLNodeTrait::Bias>
{
public:
  TFLNodeMixin() = default;

public:
  virtual loco::Node *bias(void) const = 0; /// @brief get the input for bias.
  virtual void bias(loco::Node *node) = 0;  /// @brief set the input for bias.
};

/**
 * @brief ADD in TensorFlow Lite
 */
class TFLAdd final : public FixedArityNode<2, TFLNodeImpl<TFLOpcode::ADD>>,
                     public TFLNodeMixin<TFLNodeTrait::FusedActFunc>
{
public:
  loco::Node *x(void) const { return at(0)->node(); }
  void x(loco::Node *node) { at(0)->node(node); }

  loco::Node *y(void) const { return at(1)->node(); }
  void y(loco::Node *node) { at(1)->node(node); }
};

/**
 * @brief AVERAGE_POOL_2D in TensorFlow Lite
 */
class TFLAveragePool2D final : public FixedArityNode<1, TFLNodeImpl<TFLOpcode::AVERAGE_POOL_2D>>,
                               public TFLNodeMixin<TFLNodeTrait::FusedActFunc>
{
public:
  TFLAveragePool2D() : _padding(Padding::UNDEFINED) { /* empty */}

public:
  loco::Node *value(void) const { return at(0)->node(); }
  void value(loco::Node *node) { at(0)->node(node); }

  Padding padding() const { return _padding; }
  void padding(Padding padding) { _padding = padding; }

  const Filter *filter(void) const { return &_filter; }
  Filter *filter(void) { return &_filter; }

  const Stride *stride(void) const { return &_stride; }
  Stride *stride(void) { return &_stride; }

private:
  Padding _padding;
  Stride _stride;
  Filter _filter;
};

/**
 * @brief CONCATENATION in TensorFlow Lite
 */
class TFLConcatenation final : public VariadicArityNode<TFLNodeImpl<TFLOpcode::CONCATENATION>>,
                               public TFLNodeMixin<TFLNodeTrait::FusedActFunc>
{
public:
  TFLConcatenation(uint32_t arity) : VariadicArityNode<TFLNodeImpl<TFLOpcode::CONCATENATION>>(arity)
  {
    // TODO Support when arity is 0
    assert(arity >= 1);
  }

public:
  uint32_t numValues(void) const { return arity(); }

public:
  Node *values(uint32_t index) const
  {
    assert(index < numValues());
    return at(index)->node();
  }
  void values(uint32_t index, Node *node)
  {
    assert(index < numValues());
    at(index)->node(node);
  }

public:
  uint32_t axis(void) const { return _axis; }
  void axis(uint32_t axis) { _axis = axis; }

private:
  uint32_t _axis;
};

/**
 * @brief Class to build tensor data
 * @note  This will not be exported as a specific op
 */
class TFLConst final : public FixedArityNode<0, TFLNodeImpl<TFLOpcode::CONST>>,
                       public loco::NodeMixin<loco::NodeTrait::DataType>,
                       public loco::NodeMixin<loco::NodeTrait::TensorShape>
{
public:
  TFLConst() = default;

public:
  template <loco::DataType DT> uint32_t size(void) const;
  template <loco::DataType DT> void size(uint32_t size);
  template <loco::DataType DT> const typename loco::DataTypeImpl<DT>::Type &at(uint32_t n) const;
  template <loco::DataType DT> typename loco::DataTypeImpl<DT>::Type &at(uint32_t n);

private:
  std::vector<uint8_t> _data;
};

/**
 * @brief CONV_2D in TensorFlow Lite
 */
class TFLConv2D final : public FixedArityNode<3, TFLNodeImpl<TFLOpcode::CONV_2D>>,
                        public TFLNodeMixin<TFLNodeTrait::FusedActFunc>,
                        public TFLNodeMixin<TFLNodeTrait::Bias>
{
public:
  loco::Node *input(void) const { return at(0)->node(); }
  void input(loco::Node *node) { at(0)->node(node); }

  loco::Node *filter(void) const { return at(1)->node(); }
  void filter(loco::Node *node) { at(1)->node(node); }

  loco::Node *bias(void) const override { return at(2)->node(); }
  void bias(loco::Node *node) override { at(2)->node(node); }

public:
  Padding padding() const { return _padding; }
  void padding(Padding padding) { _padding = padding; }

  const Stride *stride(void) const { return &_stride; }
  Stride *stride(void) { return &_stride; }

private:
  Padding _padding = Padding::UNDEFINED;
  Stride _stride;
};

/**
 * @brief DEPTHWISE_CONV_2D in TensorFlow Lite
 */
class TFLDepthwiseConv2D final
    : public FixedArityNode<3, TFLNodeImpl<TFLOpcode::DEPTHWISE_CONV_2D>>,
      public TFLNodeMixin<TFLNodeTrait::FusedActFunc>,
      public TFLNodeMixin<TFLNodeTrait::Bias>
{
public:
  loco::Node *input(void) const { return at(0)->node(); }
  void input(loco::Node *node) { at(0)->node(node); }

  loco::Node *filter(void) const { return at(1)->node(); }
  void filter(loco::Node *node) { at(1)->node(node); }

  loco::Node *bias(void) const override { return at(2)->node(); }
  void bias(loco::Node *node) override { at(2)->node(node); }

public:
  Padding padding() const { return _padding; }
  void padding(Padding padding) { _padding = padding; }

  const Stride *stride(void) const { return &_stride; }
  Stride *stride(void) { return &_stride; }

  int32_t depthMultiplier(void) const { return _depth_multiplier; }
  void depthMultiplier(int32_t arg) { _depth_multiplier = arg; }

private:
  Padding _padding = Padding::UNDEFINED;
  Stride _stride;
  int32_t _depth_multiplier = 0;
};

/**
 * @brief DIV in TensorFlow Lite
 */
class TFLDiv final : public FixedArityNode<2, TFLNodeImpl<TFLOpcode::DIV>>,
                     public TFLNodeMixin<TFLNodeTrait::FusedActFunc>
{
public:
  TFLDiv() = default;

public:
  loco::Node *x(void) const { return at(0)->node(); }
  void x(loco::Node *node) { at(0)->node(node); }

  loco::Node *y(void) const { return at(1)->node(); }
  void y(loco::Node *node) { at(1)->node(node); }
};

/**
 * @brief FULLY_CONNECTED in TensorFlow Lite
 */
class TFLFullyConnected final : public FixedArityNode<3, TFLNodeImpl<TFLOpcode::FULLY_CONNECTED>>,
                                public TFLNodeMixin<TFLNodeTrait::FusedActFunc>,
                                public TFLNodeMixin<TFLNodeTrait::Bias>
{
public:
  loco::Node *input(void) const { return at(0)->node(); }
  void input(loco::Node *node) { at(0)->node(node); }

  loco::Node *weights(void) const { return at(1)->node(); }
  void weights(loco::Node *node) { at(1)->node(node); }

  loco::Node *bias(void) const override { return at(2)->node(); }
  void bias(loco::Node *node) override { at(2)->node(node); }
};

/**
 * @brief MAXIMUM in TensorFlow Lite
 */
class TFLMaximum final : public FixedArityNode<2, TFLNodeImpl<TFLOpcode::MAXIMUM>>
{
public:
  loco::Node *x(void) const { return at(0)->node(); }
  void x(loco::Node *node) { at(0)->node(node); }

  loco::Node *y(void) const { return at(1)->node(); }
  void y(loco::Node *node) { at(1)->node(node); }
};

/**
 * @brief MAX_POOL_2D in TensorFlow Lite
 */
class TFLMaxPool2D final : public FixedArityNode<1, TFLNodeImpl<TFLOpcode::MAX_POOL_2D>>,
                           public TFLNodeMixin<TFLNodeTrait::FusedActFunc>
{
public:
  TFLMaxPool2D() : _padding(Padding::UNDEFINED) { /* empty */}

public:
  loco::Node *value(void) const { return at(0)->node(); }
  void value(loco::Node *node) { at(0)->node(node); }

  Padding padding() const { return _padding; }
  void padding(Padding padding) { _padding = padding; }

  const Filter *filter(void) const { return &_filter; }
  Filter *filter(void) { return &_filter; }

  const Stride *stride(void) const { return &_stride; }
  Stride *stride(void) { return &_stride; }

private:
  Padding _padding;
  Stride _stride;
  Filter _filter;
};

class TFLMean final : public FixedArityNode<2, TFLNodeImpl<TFLOpcode::MEAN>>
{
public:
  loco::Node *input(void) const { return at(0)->node(); }
  void input(loco::Node *node) { at(0)->node(node); }

  loco::Node *reduction_indices(void) const { return at(1)->node(); }
  void reduction_indices(loco::Node *node) { at(1)->node(node); }

public:
  bool keep_dims(void) const { return _keep_dims; }
  void keep_dims(bool keep_dims) { _keep_dims = keep_dims; }

private:
  bool _keep_dims = false;
};

/**
 * @brief MUL in TensorFlow Lite
 */
class TFLMul final : public FixedArityNode<2, TFLNodeImpl<TFLOpcode::MUL>>,
                     public TFLNodeMixin<TFLNodeTrait::FusedActFunc>
{
public:
  loco::Node *x(void) const { return at(0)->node(); }
  void x(loco::Node *node) { at(0)->node(node); }

  loco::Node *y(void) const { return at(1)->node(); }
  void y(loco::Node *node) { at(1)->node(node); }
};

class TFLRelu final : public FixedArityNode<1, TFLNodeImpl<TFLOpcode::RELU>>
{
public:
  TFLRelu() = default;

public:
  loco::Node *features(void) const { return at(0)->node(); }
  void features(loco::Node *node) { at(0)->node(node); }
};

class TFLRelu6 final : public FixedArityNode<1, TFLNodeImpl<TFLOpcode::RELU6>>
{
public:
  TFLRelu6() = default;

public:
  loco::Node *features(void) const { return at(0)->node(); }
  void features(loco::Node *node) { at(0)->node(node); }
};

class TFLReshape final : public FixedArityNode<2, TFLNodeImpl<TFLOpcode::RESHAPE>>
{
public:
  TFLReshape() = default;

public:
  loco::Node *tensor(void) const { return at(0)->node(); }
  void tensor(loco::Node *node) { at(0)->node(node); }

  // TODO Make this input optional. That is, loco system does not emit error
  //      with this input being null
  loco::Node *shape(void) const { return at(1)->node(); }
  void shape(loco::Node *node) { at(1)->node(node); }

public:
  class Shape
  {
  public:
    uint32_t rank(void) const { return _shape.size(); }
    void rank(uint32_t rank) { _shape.resize(rank); }

    int32_t dim(uint32_t n) const { return _shape.at(n); }
    int32_t &dim(uint32_t n) { return _shape.at(n); }

  private:
    std::vector<int32_t> _shape;
  };

  const Shape *newShape(void) const { return &_new_shape; }
  Shape *newShape(void) { return &_new_shape; }

private:
  Shape _new_shape;
};

/**
 * @brief  Set both TFLReshape's 2nd input as TFLConst, and newShape attribute
 *         with same value
 * @note   Shape inference for TFLReshape forces them to be same
 * TODO find better place for this helper
 */
void set_new_shape(locoex::TFLReshape *node, int32_t *base, uint32_t size);

class TFLRsqrt final : public FixedArityNode<1, TFLNodeImpl<TFLOpcode::RSQRT>>
{
public:
  TFLRsqrt() = default;

public:
  loco::Node *x(void) const { return at(0)->node(); }
  void x(loco::Node *node) { at(0)->node(node); }
};

// TODO TFLSoftmax

class TFLSqrt final : public FixedArityNode<1, TFLNodeImpl<TFLOpcode::SQRT>>
{
public:
  TFLSqrt() = default;

public:
  loco::Node *x(void) const { return at(0)->node(); }
  void x(loco::Node *node) { at(0)->node(node); }
};

class TFLSquaredDifference final
    : public FixedArityNode<2, TFLNodeImpl<TFLOpcode::SQUARED_DIFFERENCE>>
{
public:
  TFLSquaredDifference() = default;

public:
  loco::Node *x(void) const { return at(0)->node(); }
  void x(loco::Node *node) { at(0)->node(node); }

  loco::Node *y(void) const { return at(1)->node(); }
  void y(loco::Node *node) { at(1)->node(node); }
};

/**
 * @brief SUB in TensorFlow Lite
 */
class TFLSub final : public FixedArityNode<2, TFLNodeImpl<TFLOpcode::SUB>>,
                     public TFLNodeMixin<TFLNodeTrait::FusedActFunc>
{
public:
  TFLSub() = default;

public:
  loco::Node *x(void) const { return at(0)->node(); }
  void x(loco::Node *node) { at(0)->node(node); }

  loco::Node *y(void) const { return at(1)->node(); }
  void y(loco::Node *node) { at(1)->node(node); }
};

// TODO TFLTanh

/**
 * @brief TRANSPOSE in TensorFlow Lite
 */
class TFLTranspose final : public FixedArityNode<2, TFLNodeImpl<TFLOpcode::TRANSPOSE>>
{
public:
  TFLTranspose() = default;

public:
  /// @brief Get the input node to transpose
  loco::Node *a(void) const { return at(0)->node(); }

  /// @brief Set the input node to transpose
  void a(loco::Node *node) { at(0)->node(node); }

  loco::Node *perm(void) const { return at(1)->node(); }
  void perm(loco::Node *node) { at(1)->node(node); }
};

/**
 * @brief TRANSPOSE_CONV in TensorFlow Lite
 *
 * @note  Argument node function names are from TensorFlow. So refering 'in' and
 *        'out' acutally means 'out' and 'in' of the this node.
 */
class TFLTransposeConv final : public FixedArityNode<3, TFLNodeImpl<TFLOpcode::TRANSPOSE_CONV>>
{
public:
  loco::Node *inputSizes(void) const { return at(0)->node(); }
  void inputSizes(Node *node) { at(0)->node(node); }

  loco::Node *filter(void) const { return at(1)->node(); }
  void filter(Node *node) { at(1)->node(node); }

  loco::Node *outBackprop(void) const { return at(2)->node(); }
  void outBackprop(Node *node) { at(2)->node(node); }

public:
  const Padding &padding(void) const { return _padding; }
  void padding(const Padding &padding) { _padding = padding; }

  const Stride *stride(void) const { return &_stride; }
  Stride *stride(void) { return &_stride; }

private:
  Padding _padding;
  Stride _stride;
};

// TODO define more children of TFLNode

} // namespace locoex

#endif // __LOCOEX_IR_TFLNODES_H__