ONE - On-device Neural Engine
Loading...
Searching...
No Matches
AvgPool2D.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2018 The TensorFlow Authors. All Rights Reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include "NodeExecution.h"
19
20#include "NodeDataImpl.h"
21#include "NodeDomain.h"
22#include "Validation.h"
23
28
29#include <cassert>
30#include <stdexcept>
31
32namespace
33{
34
40
46inline uint32_t compute_out_size(uint32_t image_size, uint32_t whole_pad, uint32_t filter_size,
47 uint32_t stride)
48{
49 assert((image_size + whole_pad - filter_size) % stride == 0);
50 return (image_size + whole_pad - filter_size) / stride + 1;
51}
52
53template <typename T>
55 const Buffer<T> *ifm_buf)
56{
57 assert(avgpool2d->convention() == loco::AvgPool2D::Convention::Valid ||
59
60 auto ifm_shape = ifm_buf->shape();
61
62 const uint32_t batches = ifm_shape.dim(0);
63 const uint32_t depth = ifm_shape.dim(3);
64
65 const uint32_t ifm_height = ifm_shape.dim(1);
66 const uint32_t ifm_width = ifm_shape.dim(2);
67
68 const uint32_t window_height = avgpool2d->window()->vertical();
69 const uint32_t window_width = avgpool2d->window()->horizontal();
70
71 const uint32_t stride_height = avgpool2d->stride()->vertical();
72 const uint32_t stride_width = avgpool2d->stride()->horizontal();
73
74 const uint32_t pad_top = avgpool2d->pad()->top();
75 const uint32_t pad_bottom = avgpool2d->pad()->bottom();
76
77 const uint32_t pad_left = avgpool2d->pad()->left();
78 const uint32_t pad_right = avgpool2d->pad()->right();
79
80 const uint32_t output_height =
81 compute_out_size(ifm_height, pad_top + pad_bottom, window_height, stride_height);
82 const uint32_t output_width =
83 compute_out_size(ifm_width, pad_left + pad_right, window_width, stride_width);
84
85 // prepare output buffer
86 Shape output_shape{batches, output_height, output_width, depth};
87 auto output_buf = make_buffer<T, LexicalLayout>(output_shape);
88
89 for (uint32_t batch = 0; batch < batches; ++batch)
90 {
91 for (uint32_t out_y = 0; out_y < output_height; ++out_y)
92 {
93 for (uint32_t out_x = 0; out_x < output_width; ++out_x)
94 {
95 for (uint32_t channel = 0; channel < depth; ++channel)
96 {
97 const int in_x_origin = (out_x * stride_width) - pad_left;
98 const int in_y_origin = (out_y * stride_height) - pad_top;
99
100 uint32_t f_x0, f_x1, f_y0, f_y1;
102 {
103 f_x0 = std::max(0, -in_x_origin);
104 f_x1 = std::min(window_width, ifm_width - in_x_origin);
105 f_y0 = std::max(0, -in_y_origin);
106 f_y1 = std::min(window_height, ifm_height - in_y_origin);
107 }
108 else
109 {
110 throw std::runtime_error("TODO support AvgPool2D::Convention::Full");
111 }
112 const uint32_t filter_x_start = f_x0;
113 const uint32_t filter_x_end = f_x1;
114
115 const uint32_t filter_y_start = f_y0;
116 const uint32_t filter_y_end = f_y1;
117
118 T total = 0;
119 uint32_t filter_ele_count = 0;
120
121 for (uint32_t filter_y = filter_y_start; filter_y < filter_y_end; ++filter_y)
122 {
123 for (uint32_t filter_x = filter_x_start; filter_x < filter_x_end; ++filter_x)
124 {
125 const uint32_t in_x = in_x_origin + filter_x;
126 const uint32_t in_y = in_y_origin + filter_y;
127 total += ifm_buf->at(Index({batch, in_y, in_x, channel}));
128 filter_ele_count++;
129 }
130 }
131
132 if (filter_ele_count <= 0)
133 throw std::runtime_error("The number of filter element must be greater than zero.");
134 output_buf.at(Index({batch, out_y, out_x, channel})) = total / filter_ele_count;
135 }
136 }
137 }
138 }
139
140 return output_buf;
141}
142
143} // namespace
144
145namespace
146{
147
148using namespace locomotiv;
149
150void exectute_node(loco::AvgPool2D *avgpool2d)
151{
152 auto ifm_data = annot_data(avgpool2d->ifm());
153
154 validate(ifm_data, "Can't find input data of AvgPool2D");
155 validate(ifm_data->shape()->rank() == 4, "IFM rank should be 4");
157 "ifm of AvgPool2D is not Feature");
158
159 std::unique_ptr<NodeData> avgpool2d_data = nullptr;
160
161 switch (ifm_data->dtype())
162 {
163 case loco::DataType::FLOAT32:
164 {
165 auto ifm_buf = ifm_data->as_f32_bufptr();
166
167 auto avgpool2d_buf = avgPool2D<float>(avgpool2d, ifm_buf);
168
169 avgpool2d_data = make_data(avgpool2d_buf);
170 break;
171 }
172 default:
173 throw std::runtime_error("NYI for this DataType");
174 }
175
176 assert(avgpool2d_data != nullptr);
177
178 annot_data(avgpool2d, std::move(avgpool2d_data));
180}
181
182} // namespace
183
184namespace locomotiv
185{
186
187void NodeExecution::execute(loco::AvgPool2D *avgpool2d) { exectute_node(avgpool2d); }
188
189} // namespace locomotiv
2D Average Pooling
Definition Nodes.h:337
Convention convention(void) const
Definition Nodes.h:353
Node * ifm(void) const
Definition Nodes.h:349
const Stride< 2 > * stride(void) const
Definition Nodes.h:365
const Padding2D * pad(void) const
Definition Nodes.h:357
const Window< 2 > * window(void) const
Definition Nodes.h:361
uint32_t left(void) const
Definition Padding2D.h:49
uint32_t top(void) const
Definition Padding2D.h:41
uint32_t bottom(void) const
Definition Padding2D.h:45
uint32_t right(void) const
Definition Padding2D.h:53
uint32_t horizontal(void) const
Definition Stride.h:40
uint32_t vertical(void) const
Definition Stride.h:36
uint32_t horizontal(void) const
Definition Window.h:42
uint32_t vertical(void) const
Definition Window.h:38
const luci_interpreter::RuntimeShape output_shape
bool validate(Code *code)
void annot_domain(loco::Node *node, const loco::Domain &domain)
Wrapper to annotate domain to node. Cannot annotate unknown domain.
std::unique_ptr< NodeData > make_data(const NodeData::Buffer< DT > &buffer)
Copy buffer to make NodeData.
Buffer< T > make_buffer(const Shape &shape)
Definition Buffer.h:47
Definition Shape.h:28