summaryrefslogtreecommitdiff
path: root/compiler/luci/pass/src/FoldDensifyPass.cpp
blob: 5ddc743e52e2698d10a3b220e294b6505f919fca (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
/*
 * Copyright (c) 2022 Samsung Electronics Co., Ltd. All Rights Reserved
 * Copyright 2020 The TensorFlow Authors. 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 "luci/Pass/FoldDensifyPass.h"
#include "helpers/SparsityFormatConverter.h"

#include <luci/IR/CircleNodes.h>
#include <luci/Profile/CircleNodeOrigin.h>

#include <cassert>
#include <vector>

namespace
{

bool is_foldable_const(luci::CircleConst *node)
{
  if (node->sparsityparam() == nullptr)
    return false;

  if (node->dtype() == loco::DataType::FLOAT32)
    return true;
  if (node->dtype() == loco::DataType::FLOAT16)
    return true;

  return false;
}

luci::CircleConst *densified_const_node(luci::CircleConst *const_node)
{
  assert(const_node->sparsityparam());

  auto name = const_node->name();
  assert(name.length() > 0);
  auto g = const_node->graph();
  auto new_const_node = g->nodes()->create<luci::CircleConst>();

  new_const_node->dtype(const_node->dtype());
  new_const_node->rank(const_node->rank());

  uint32_t dim_size = 1;
  std::vector<int> dense_shape;
  for (uint32_t i = 0; i < new_const_node->rank(); ++i)
  {
    assert(const_node->dim(i).known());
    new_const_node->dim(i) = const_node->dim(i);

    uint32_t value = const_node->dim(i).value();
    dim_size *= value;
    dense_shape.emplace_back(static_cast<int32_t>(value));
  }

  if (const_node->dtype() == loco::DataType::FLOAT32)
    new_const_node->size<loco::DataType::FLOAT32>(dim_size);
  else
  {
    assert(const_node->dtype() == loco::DataType::FLOAT16);
    new_const_node->size<loco::DataType::FLOAT16>(dim_size);
  }

  new_const_node->shape_status(luci::ShapeStatus::VALID);
  new_const_node->name(name + "_DS");

  if (const_node->dtype() == loco::DataType::FLOAT32)
  {
    auto const_items = const_node->size<loco::DataType::FLOAT32>();
    auto f_data = std::make_unique<float[]>(const_items);
    for (size_t i = 0; i < const_items; ++i)
      f_data[i] = const_node->at<loco::DataType::FLOAT32>(i);

    sparsity::TfLiteSparsity sp = to_tflite_sparsity(const_node->sparsityparam());
    sparsity::FormatConverter<float> converter(dense_shape, sp);
    converter.SparseToDense(f_data.get());
    const auto &data_dense = converter.GetData();
    assert(data_dense.size() == dim_size);

    for (uint32_t i = 0; i < dim_size; ++i)
      new_const_node->at<loco::DataType::FLOAT32>(i) = data_dense[i];

    luci::freeTfLiteSparsity(sp);
  }
  else
  {
    assert(const_node->dtype() == loco::DataType::FLOAT16);

    auto const_items = const_node->size<loco::DataType::FLOAT16>();
    auto f_data = std::make_unique<uint16_t[]>(const_items);
    for (size_t i = 0; i < const_items; ++i)
      f_data[i] = const_node->at<loco::DataType::FLOAT16>(i);

    // Primitive type for FLOAT16 is UINT16
    sparsity::TfLiteSparsity sp = to_tflite_sparsity(const_node->sparsityparam());
    sparsity::FormatConverter<uint16_t> converter(dense_shape, sp);
    converter.SparseToDense(f_data.get());
    const auto &data_dense = converter.GetData();
    assert(data_dense.size() == dim_size);
    for (uint32_t i = 0; i < dim_size; ++i)
      new_const_node->at<loco::DataType::FLOAT16>(i) = data_dense[i];

    luci::freeTfLiteSparsity(sp);
  }

  return new_const_node;
}

/**
 * @brief Fold Densify if input is Sparse Constant
 */
bool fold_densify(luci::CircleDensify *densify)
{
  auto const_input = dynamic_cast<luci::CircleConst *>(densify->input());
  if (not const_input)
    return false;

  if (not is_foldable_const(const_input))
    return false;

  auto dense_const = densified_const_node(const_input);
  assert(dense_const);

  loco::replace(densify).with(dense_const);
  luci::add_origin(dense_const, luci::composite_origin(
                                  {luci::get_origin(densify), luci::get_origin(const_input)}));

  return true;
}

} // namespace

namespace luci
{

/**
 * BEFORE
 *
 *    [CircleConst](sparse)
 *         |
 *   [CircleDensify]
 *         |
 *    [CircleNode]
 *         |
 *
 * AFTER
 *
 *    [CircleConst](dense)  [CircleConst](sparse)
 *         |                     |
 *    [CircleNode]          [CircleDensify]
 *         |
 */
bool FoldDensifyPass::run(loco::Graph *g)
{
  bool changed = false;

  for (auto node : loco::active_nodes(loco::output_nodes(g)))
  {
    if (auto densify = dynamic_cast<luci::CircleDensify *>(node))
    {
      if (fold_densify(densify))
        changed = true;
    }
  }

  return changed;
}

} // namespace luci