summaryrefslogtreecommitdiff
path: root/compiler/moco-tf/src/BroadcastHelper.cpp
blob: fc058c1417b8df311bd53da331208fed83089c05 (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
/*
 * 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.
 */

#include "BroadcastHelper.h"

#include <loco/IR/Nodes.h>
#include <loco/Service/ShapeInference.h>

#include <cassert>

namespace
{

class NodeWithTensorShape
{
public:
  NodeWithTensorShape() = default;

public:
  NodeWithTensorShape(loco::Node *node, const loco::TensorShape &shape) : _node{node}, _shape{shape}
  {
    // DO NOTHING
  }

public:
  loco::Node *node(void) const { return _node; }
  const loco::TensorShape &shape(void) const { return _shape; }

private:
  loco::Node *_node = nullptr;
  loco::TensorShape _shape;
};

NodeWithTensorShape glue(loco::Node *node, const loco::TensorShape &shape)
{
  return NodeWithTensorShape(node, shape);
}

/**
 * @brief Create a higher-rank TensorShape following NumPy broadcasting semantics
 *
 * HOW TO USE:
 *
 *   auto expanded_tensor_shape = expand(tensor_shape).to(N);
 */
class TensorShapeExpander
{
public:
  TensorShapeExpander(const loco::TensorShape &shape) : _shape{shape}
  {
    // DO NOTHING
  }

public:
  loco::TensorShape to(uint32_t output_rank)
  {
    auto const &input_shape = _shape;
    uint32_t const input_rank = input_shape.rank();

    assert(input_rank <= output_rank && "Cannot shrink rank");
    uint32_t const axis_shift = output_rank - input_rank;

    loco::TensorShape output_shape;

    output_shape.rank(output_rank);
    for (uint32_t axis = 0; axis < output_rank; ++axis)
    {
      output_shape.dim(axis) = (axis < axis_shift) ? 1 : input_shape.dim(axis - axis_shift);
    }

    return output_shape;
  }

private:
  const loco::TensorShape _shape;
};

TensorShapeExpander expand(const loco::TensorShape &shape) { return TensorShapeExpander{shape}; }

/**
 * @brief Create a rank-expanded node (if required)
 */
class ExpandRankFunctor final
{
public:
  ExpandRankFunctor(uint32_t rank) : _rank{rank}
  {
    // DO NOTHING
  }

public:
  NodeWithTensorShape operator()(const NodeWithTensorShape &in) const
  {
    auto const input_node = in.node();
    auto const input_shape = in.shape();
    auto const input_rank = input_shape.rank();

    uint32_t const expected_rank = _rank;

    assert(input_rank <= expected_rank);
    if (input_rank == expected_rank)
    {
      // Nothing to expand
      return in;
    }

    auto g = input_node->graph();
    assert(g != nullptr);

    auto output_shape = expand(input_shape).to(expected_rank);
    auto output_node = g->nodes()->create<loco::FixedReshape>();

    output_node->input(input_node);
    output_node->rank(expected_rank);
    for (uint32_t axis = 0; axis < expected_rank; ++axis)
    {
      output_node->dim(axis) = output_shape.dim(axis);
    }

    return glue(output_node, output_shape);
  }

private:
  uint32_t _rank;
};

ExpandRankFunctor expand_rank_to(uint32_t rank) { return ExpandRankFunctor{rank}; }

/**
 * @brief Create a dimension-expanded node (if required)
 */
class ExpandDimsFunctor final
{
public:
  ExpandDimsFunctor(const loco::TensorShape &shape) : _shape{shape}
  {
    // DO NOTHING
  }

public:
  NodeWithTensorShape operator()(const NodeWithTensorShape &in) const
  {
    auto const input_node = in.node();
    auto const input_shape = in.shape();
    const auto &output_shape = _shape;

    assert(input_shape.rank() == output_shape.rank());

    if (input_shape == output_shape)
    {
      // Nothing to expand
      return in;
    }

    uint32_t const rank = output_shape.rank();

    auto g = input_node->graph();
    assert(g != nullptr);

    auto output_node = g->nodes()->create<loco::TensorBroadcast>();

    for (uint32_t axis = 0; axis < rank; ++axis)
    {
      auto input_dim = input_shape.dim(axis);
      auto output_dim = output_shape.dim(axis);

      assert(input_dim.known() and output_dim.known());

      if (!(input_dim == output_dim))
      {
        assert(input_dim == 1);
        output_node->mapping()->dim(axis) = output_dim;
      }
    }

    output_node->input(input_node);

    return glue(output_node, output_shape);
  }

private:
  loco::TensorShape _shape;
};

ExpandDimsFunctor expand_dims_as(const loco::TensorShape &shape)
{
  return ExpandDimsFunctor{shape};
}

} // namespace

namespace moco
{
namespace tf
{

loco::Node *BroadcastFunctor::build(loco::Node *node, const loco::TensorShape &shape) const
{
  // clang-format off
  return glue(node, shape)
       | expand_rank_to(_shape.rank())
       | expand_dims_as(_shape)
       | [] (const NodeWithTensorShape &in) { return in.node(); };
  // clang-format on
}

loco::Node *BroadcastFunctor::build(loco::Node *node) const
{
  return build(node, loco::shape_get(node).as<loco::TensorShape>());
}

} // namespace tf
} // namespace moco