summaryrefslogtreecommitdiff
path: root/compiler/enco/core/src/Transforms/ConstantFolding.cpp
blob: cd6f2235106abcfb93702afa0319d4fbe5f2afb8 (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
/*
 * Copyright (c) 2018 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.
 */

#include "ConstantFolding.h"
#include "Session.h"

#include <queue>
#include <cmath>
#include <cassert>

namespace
{

/**
 * @brief is_constant_bag(b) returns true if the bag "b" has corresponding weight
 */
bool is_constant_bag(coco::Bag *b)
{
  auto m = b->module();
  auto d = enco::data(m);
  return d->allocated(b);
}

class ConstantBagEnumerator
{
public:
  ConstantBagEnumerator(enco::Code *code) : _code{code}
  {
    // DO NOTHING
  }

public:
  template <typename Callable> void enumerate(Callable cb) const
  {
    auto m = _code->module();

    for (uint32_t n = 0; n < m->entity()->bag()->size(); ++n)
    {
      auto b = m->entity()->bag()->at(n);

      if (is_constant_bag(b))
      {
        cb(b);
      }
    }
  }

private:
  enco::Code *_code;
};

template <typename Callable> void operator<<(const ConstantBagEnumerator &e, Callable &&cb)
{
  e.enumerate(std::forward<Callable>(cb));
}

ConstantBagEnumerator constant_bag_enumerator(enco::Code *code)
{
  return ConstantBagEnumerator{code};
}

} // namespace

namespace
{

/**
 * @brief Take the first element from the queue
 * @note The queue SHOULD have at least one element.
 */
template <typename T> T take(std::queue<T> &q)
{
  assert(q.size() > 0);
  auto res = q.front();
  q.pop();
  return res;
}

} // namespace

namespace
{

void fold_constant(std::queue<coco::Bag *> &q, coco::Copy *copy)
{
  auto m = copy->module();
  auto d = enco::data(m);

  auto src_obj = copy->from();
  auto src_bag = src_obj->bag();

  auto dst_obj = copy->into();
  auto dst_bag = dst_obj->bag();

  // Output calculation should not be folded
  // TODO Reduce code duplication of this kind
  if (dst_bag->isOutput())
  {
    return;
  }

  // NOTE d->allocated(bag) returns true if bag has corresponding initial
  //      values (e.g. convolution kernel)
  assert(d->allocated(src_bag));
  assert(!d->allocated(dst_bag));

  // TODO Support other data type
  auto src_span = d->f32()->weight(src_bag);

  assert(src_span.data() != nullptr);

  auto src_feature = src_obj->asFeature();
  auto dst_feature = dst_obj->asFeature();

  // TODO Support other object type
  if (src_feature == nullptr || dst_feature == nullptr)
  {
    return;
  }

  assert(src_feature != nullptr);
  assert(dst_feature != nullptr);

  // Allocate weight for destination
  d->f32()->allocate(dst_bag);

  auto dst_span = d->f32()->weight(dst_bag);

  assert(src_feature->layout()->batch() == dst_feature->layout()->batch());
  assert(src_feature->layout()->depth() == dst_feature->layout()->depth());
  assert(src_feature->layout()->height() == dst_feature->layout()->height());
  assert(src_feature->layout()->width() == dst_feature->layout()->width());

  uint32_t const B = src_feature->layout()->batch();
  uint32_t const C = src_feature->layout()->depth();
  uint32_t const H = src_feature->layout()->height();
  uint32_t const W = src_feature->layout()->width();

  for (uint32_t b = 0; b < B; ++b)
  {
    for (uint32_t ch = 0; ch < C; ++ch)
    {
      for (uint32_t row = 0; row < H; ++row)
      {
        for (uint32_t col = 0; col < W; ++col)
        {
          auto src_ind = src_feature->layout()->at(b, ch, row, col);
          auto dst_ind = dst_feature->layout()->at(b, ch, row, col);

          dst_span[dst_ind.value()] = src_span[src_ind.value()];
        }
      }
    }
  }

  // Let's detach copy
  copy->from(nullptr);
  copy->into(nullptr);
  copy->detach();

  // Let's visit destination bag!
  q.push(dst_bag);
}

template <typename Callable>
void fold_constant_op(std::queue<coco::Bag *> &q, coco::UnaryOp *op, Callable evaluate)
{
  auto m = op->module();
  auto d = enco::data(m);

  auto ins = op->parent();
  auto eval = ins->asEval();

  // UnaryOp has only one arg
  auto src_obj = *(op->uses().begin());
  auto src_bag = src_obj->bag();

  auto dst_obj = eval->out();
  auto dst_bag = dst_obj->bag();

  // Output calculation should not be folded
  // TODO Reduce code duplication of this kind
  if (dst_bag->isOutput())
  {
    return;
  }

  assert(d->allocated(src_bag));
  assert(!d->allocated(dst_bag));

  // TODO Support other data type
  auto src_span = d->f32()->weight(src_bag);
  assert(src_span.data() != nullptr);

  auto src_feature = src_obj->asFeature();
  auto dst_feature = dst_obj->asFeature();

  // TODO Support other object type
  if (src_feature == nullptr || dst_feature == nullptr)
  {
    return;
  }

  assert(src_feature != nullptr);
  assert(dst_feature != nullptr);

  // Allocate weight for destination
  d->f32()->allocate(dst_bag);
  auto dst_span = d->f32()->weight(dst_bag);

  assert(src_feature->layout()->batch() == dst_feature->layout()->batch());
  assert(src_feature->layout()->depth() == dst_feature->layout()->depth());
  assert(src_feature->layout()->height() == dst_feature->layout()->height());
  assert(src_feature->layout()->width() == dst_feature->layout()->width());

  uint32_t const B = src_feature->layout()->batch();
  uint32_t const C = src_feature->layout()->depth();
  uint32_t const H = src_feature->layout()->height();
  uint32_t const W = src_feature->layout()->width();

  for (uint32_t b = 0; b < B; ++b)
  {
    for (uint32_t ch = 0; ch < C; ++ch)
    {
      for (uint32_t row = 0; row < H; ++row)
      {
        for (uint32_t col = 0; col < W; ++col)
        {
          auto src_ind = src_feature->layout()->at(b, ch, row, col);
          auto dst_ind = dst_feature->layout()->at(b, ch, row, col);

          evaluate(&dst_span[dst_ind.value()], src_span[src_ind.value()]);
        }
      }
    }
  }

  // Let's detach eval
  eval->out(nullptr);
  eval->detach();

  // Let's visit destination bag!
  q.push(dst_bag);
}

template <typename Callable>
void fold_constant_op(std::queue<coco::Bag *> &q, coco::BinaryOp *op, Callable evaluate)
{
  auto m = op->module();
  auto d = enco::data(m);

  auto ins = op->parent();
  auto eval = ins->asEval();

  // Already folded by the other bag
  if (!eval->out())
  {
    return;
  }

  auto lhs_load = op->left()->asLoad();
  auto lhs_obj = lhs_load->object();
  auto lhs_bag = lhs_obj->bag();

  auto rhs_load = op->right()->asLoad();
  auto rhs_obj = rhs_load->object();
  auto rhs_bag = rhs_obj->bag();

  auto dst_obj = eval->out();
  auto dst_bag = dst_obj->bag();

  // Output calculation should not be folded
  // TODO Reduce code duplication of this kind
  if (dst_bag->isOutput())
  {
    return;
  }

  // The other bag is non-constant
  if (!d->allocated(lhs_bag) || !d->allocated(rhs_bag))
  {
    return;
  }

  assert(d->allocated(lhs_bag));
  assert(d->allocated(rhs_bag));
  assert(!d->allocated(dst_bag));

  // TODO Support other data type
  auto lhs_span = d->f32()->weight(lhs_bag);
  auto rhs_span = d->f32()->weight(rhs_bag);
  assert(lhs_span.data() != nullptr);
  assert(rhs_span.data() != nullptr);

  auto lhs_feature = lhs_obj->asFeature();
  auto rhs_feature = rhs_obj->asFeature();
  auto dst_feature = dst_obj->asFeature();

  // TODO Support other object type
  if (lhs_feature == nullptr || rhs_feature == nullptr || dst_feature == nullptr)
  {
    return;
  }

  assert(lhs_feature != nullptr);
  assert(rhs_feature != nullptr);
  assert(dst_feature != nullptr);

  // Allocate weight for destination
  d->f32()->allocate(dst_bag);
  auto dst_span = d->f32()->weight(dst_bag);

  assert(lhs_feature->layout()->batch() == rhs_feature->layout()->batch());
  assert(lhs_feature->layout()->depth() == rhs_feature->layout()->depth());
  assert(lhs_feature->layout()->height() == rhs_feature->layout()->height());
  assert(lhs_feature->layout()->width() == rhs_feature->layout()->width());

  assert(lhs_feature->layout()->batch() == dst_feature->layout()->batch());
  assert(lhs_feature->layout()->depth() == dst_feature->layout()->depth());
  assert(lhs_feature->layout()->height() == dst_feature->layout()->height());
  assert(lhs_feature->layout()->width() == dst_feature->layout()->width());

  uint32_t const B = lhs_feature->layout()->batch();
  uint32_t const C = lhs_feature->layout()->depth();
  uint32_t const H = lhs_feature->layout()->height();
  uint32_t const W = lhs_feature->layout()->width();

  for (uint32_t b = 0; b < B; ++b)
  {
    for (uint32_t ch = 0; ch < C; ++ch)
    {
      for (uint32_t row = 0; row < H; ++row)
      {
        for (uint32_t col = 0; col < W; ++col)
        {
          auto lhs_ind = lhs_feature->layout()->at(b, ch, row, col);
          auto rhs_ind = rhs_feature->layout()->at(b, ch, row, col);
          auto dst_ind = dst_feature->layout()->at(b, ch, row, col);

          evaluate(&dst_span[dst_ind.value()], lhs_span[lhs_ind.value()],
                   rhs_span[rhs_ind.value()]);
        }
      }
    }
  }

  // Let's detach eval
  eval->out(nullptr);
  eval->detach();

  // Let's visit destination bag!
  q.push(dst_bag);
}

void fold_constant(std::queue<coco::Bag *> &q, coco::Eval *eval)
{
  // TODO Support other data types
  if (auto op = eval->op()->asSqrt())
  {
    fold_constant_op(q, op, [](float *dst, float value) { *dst = std::sqrt(value); });
  }
  else if (auto op = eval->op()->asAdd())
  {
    fold_constant_op(q, op, [](float *dst, float lhs, float rhs) { *dst = lhs + rhs; });
  }
  else if (auto op = eval->op()->asSub())
  {
    fold_constant_op(q, op, [](float *dst, float lhs, float rhs) { *dst = lhs - rhs; });
  }
  else if (auto op = eval->op()->asMul())
  {
    fold_constant_op(q, op, [](float *dst, float lhs, float rhs) { *dst = lhs * rhs; });
  }
  else if (auto op = eval->op()->asDiv())
  {
    fold_constant_op(q, op, [](float *dst, float lhs, float rhs) { *dst = lhs / rhs; });
  }
  else
  {
    // Not supported opteration, do nothing
    // TODO Support other operations
  }
}

void fold_constant(std::queue<coco::Bag *> &q, coco::Instr *ins)
{
  if (auto copy = coco::safe_cast<coco::Copy>(ins))
  {
    fold_constant(q, copy);
    return;
  }
  if (auto eval = coco::safe_cast<coco::Eval>(ins))
  {
    fold_constant(q, eval);
    return;
  }

  // TODO Add more cases for constant folding
}

} // namespace

namespace enco
{

void fold_constants(enco::Code *code)
{
  std::queue<coco::Bag *> q;

  // Collect the initial set of "constant" bag
  constant_bag_enumerator(code) << [&q](coco::Bag *bag) { q.push(bag); };

  while (!q.empty())
  {
    auto candidate_bag = take(q);

    // Scan the readers of each candidate bag
    for (auto reader : coco::readers(candidate_bag))
    {
      // TODO Decide how to handle the reader with unknown instruction
      if (auto ins = reader->loc())
      {
        fold_constant(q, ins);
      }
    }
  }
}

} // namespace enco