tvm
Loading...
Searching...
No Matches
pooling.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
24#ifndef TVM_TOPI_NN_POOLING_H_
25#define TVM_TOPI_NN_POOLING_H_
26
27#include <tvm/s_tir/stmt.h>
28#include <tvm/sym/analyzer.h>
30#include <tvm/topi/nn.h>
31#include <tvm/topi/reduction.h>
32#include <tvm/topi/tags.h>
33
34#include <algorithm>
35#include <string>
36#include <vector>
37
38namespace tvm {
39namespace topi {
40namespace nn {
41
42using namespace tvm::te;
43
45enum PoolType : int {
48};
49
50inline Tensor pool_grad_impl(const Tensor& out_grad, const Tensor& x,
51 const ffi::Array<PrimExpr>& kernel_size,
52 const ffi::Array<PrimExpr>& stride_size,
53 const ffi::Array<PrimExpr>& padding_size, PoolType pool_type,
54 bool ceil_mode, const size_t height_axis, const size_t width_axis,
55 bool count_include_pad) {
56 using namespace tvm::prim;
57 TVM_FFI_ICHECK(out_grad->shape.size() >= 2) << "Pooling grad output must >= 2-D (H, W)";
58 TVM_FFI_ICHECK(x->shape.size() >= 2) << "Pooling input must >= 2-D (H, W)";
59 TVM_FFI_ICHECK_EQ(kernel_size.size(), 2) << "Pooling kernel_size must have 2 elements";
60 TVM_FFI_ICHECK_EQ(stride_size.size(), 2) << "Pooling stride_size must have 2 elements";
61 TVM_FFI_ICHECK_EQ(padding_size.size(), 4) << "Pooling padding_size must have 4 elements";
62
63 auto kernel_height = kernel_size[0];
64 auto kernel_width = kernel_size[1];
65 auto stride_height = stride_size[0];
66 auto stride_width = stride_size[1];
67
68 auto height = x->shape[height_axis];
69 auto width = x->shape[width_axis];
70
71 auto pad_top = padding_size[0];
72 auto pad_left = padding_size[1];
73 auto pad_bottom = padding_size[2];
74 auto pad_right = padding_size[3];
75
76 if (ceil_mode) {
77 // Additional padding to ensure we do ceil instead of floor when
78 // dividing by stride.
79 pad_bottom += stride_height - 1;
80 pad_right += stride_width - 1;
81 }
82
83 ffi::Array<PrimExpr> pad_before(std::vector<PrimExpr>(x->shape.size(), 0));
84 pad_before.Set(height_axis, pad_top);
85 pad_before.Set(width_axis, pad_left);
86
87 ffi::Array<PrimExpr> pad_after(std::vector<PrimExpr>(x->shape.size(), 0));
88 pad_after.Set(height_axis, pad_bottom);
89 pad_after.Set(width_axis, pad_right);
90 sym::Analyzer analyzer;
91 auto out_height =
92 analyzer->Simplify((height - kernel_height + pad_top + pad_bottom) / stride_height + 1);
93 auto out_width =
94 analyzer->Simplify((width - kernel_width + pad_left + pad_right) / stride_width + 1);
95
96 auto dheight = tvm::te::reduce_axis(Range(0, kernel_height), "dh");
97 auto dwidth = tvm::te::reduce_axis(Range(0, kernel_width), "dw");
98
99 ffi::Array<PrimExpr> data_shape = x->shape;
100 ffi::Array<PrimExpr> out_shape = data_shape;
101 out_shape.Set(height_axis, out_height);
102 out_shape.Set(width_axis, out_width);
103
104 const auto* padding_h0 = pad_top.as<IntImmObj>();
105 const auto* padding_w0 = pad_left.as<IntImmObj>();
106 const auto* padding_h1 = pad_bottom.as<IntImmObj>();
107 const auto* padding_w1 = pad_right.as<IntImmObj>();
108 const bool do_pad =
109 ((padding_h0 && padding_h0->value != 0) || (padding_w0 && padding_w0->value != 0)) ||
110 ((padding_h1 && padding_h1->value != 0) || (padding_w1 && padding_w1->value != 0));
111
112 if (pool_type == kMaxPool) {
113 ffi::Array<PrimExpr> ravel_shape{data_shape.begin(), data_shape.end()};
114 ravel_shape.Set(height_axis, ravel_shape[height_axis] + pad_top + pad_bottom);
115 ravel_shape.Set(width_axis, ravel_shape[width_axis] + pad_left + pad_right);
116
117 auto windowh =
118 tvm::te::reduce_axis(Range(0, (kernel_height + stride_height - 1) / stride_height), "wh");
119 auto windoww =
120 tvm::te::reduce_axis(Range(0, (kernel_width + stride_width - 1) / stride_width), "ww");
121
122 auto argmax = MakeArgmaxReducer();
123 auto pad_x =
124 do_pad ? pad(x, pad_before, pad_after, tvm::prim::min_value(PrimType(x->dtype)), "pad_temp")
125 : x;
126
127 auto mp_argmax = tvm::te::compute(
128 out_shape,
129 [&](const ffi::Array<PrimVar>& inds) {
130 ffi::Array<PrimExpr> window_inds =
131 inds.Map([](const PrimVar& var) { return var.as_or_throw<PrimExpr>(); });
132 window_inds.Set(height_axis, inds[height_axis] * stride_height + dheight);
133 window_inds.Set(width_axis, inds[width_axis] * stride_width + dwidth);
134 auto idx = detail::RavelIndex(window_inds, ravel_shape);
135 return argmax({idx, pad_x(window_inds)}, {dheight, dwidth}, nullptr);
136 },
137 "maxpool_grad_argmax", kCommReduceIdx);
138
139 auto mp_inds = mp_argmax[0];
140
141 return tvm::te::compute(
142 data_shape,
143 [&](const ffi::Array<PrimVar>& inds) {
144 ffi::Array<PrimExpr> pad_inds =
145 inds.Map([](const PrimVar& var) { return var.as_or_throw<PrimExpr>(); });
146 pad_inds.Set(height_axis, pad_inds[height_axis] + pad_top);
147 pad_inds.Set(width_axis, pad_inds[width_axis] + pad_left);
148 auto idx = detail::RavelIndex(pad_inds, ravel_shape);
149
150 ffi::Array<PrimExpr> out_idx =
151 inds.Map([](const PrimVar& var) { return var.as_or_throw<PrimExpr>(); });
152 out_idx.Set(height_axis, (inds[height_axis] + pad_top) / stride_height - windowh);
153 out_idx.Set(width_axis, (inds[width_axis] + pad_left) / stride_width - windoww);
154
155 PrimExpr out_idx_lower_h = prim::Select(
156 pad_inds[height_axis] < kernel_height, IntImm(pad_inds[height_axis].ty(), 0),
157 (pad_inds[height_axis] - kernel_height) / stride_height + 1);
158 PrimExpr out_idx_lower_w = prim::Select(
159 pad_inds[width_axis] < kernel_width, IntImm(pad_inds[width_axis].ty(), 0),
160 (pad_inds[width_axis] - kernel_width) / stride_width + 1);
161
162 return tvm::prim::sum(
163 tvm::if_then_else(prim::And(prim::And(out_idx[height_axis] >= out_idx_lower_h,
164 out_idx[width_axis] >= out_idx_lower_w),
165 mp_inds(out_idx) == idx),
166 out_grad(out_idx), MakeConst(PrimType(x->dtype), 0)),
167 {windowh, windoww});
168 },
169 "T_pool_grad", "pool_grad_max");
170 } else if (pool_type == kAvgPool) {
171 auto windowh =
172 tvm::te::reduce_axis(Range(0, (kernel_height + stride_height - 1) / stride_height), "wh");
173 auto windoww =
174 tvm::te::reduce_axis(Range(0, (kernel_width + stride_width - 1) / stride_width), "ww");
175 return tvm::te::compute(
176 data_shape,
177 [&](const ffi::Array<PrimVar>& inds) {
178 PrimExpr pad_h_idx = inds[height_axis] + pad_top;
179 PrimExpr pad_w_idx = inds[width_axis] + pad_left;
180
181 // output indices whose pooling windows cover current input element (can be out-of-bound)
182 ffi::Array<PrimExpr> out_idx =
183 inds.Map([](const PrimVar& var) { return var.as_or_throw<PrimExpr>(); });
184 out_idx.Set(height_axis, (pad_h_idx / stride_height - windowh));
185 out_idx.Set(width_axis, (pad_w_idx / stride_width - windoww));
186
187 PrimExpr out_idx_lower_h =
188 prim::Select(pad_h_idx < kernel_height, IntImm(pad_h_idx.ty(), 0),
189 (pad_h_idx - kernel_height) / stride_height + 1);
190 PrimExpr out_idx_lower_w =
191 prim::Select(pad_w_idx < kernel_width, IntImm(pad_w_idx.ty(), 0),
192 (pad_w_idx - kernel_width) / stride_width + 1);
193
194 PrimExpr divide_factor = kernel_height * kernel_width; // number of pooled elements
195 if (!count_include_pad) {
196 PrimExpr h_start = out_idx[height_axis] * stride_height - pad_top;
197 PrimExpr w_start = out_idx[width_axis] * stride_width - pad_left;
198
199 PrimExpr h_end = min(h_start + kernel_height, height);
200 PrimExpr w_end = min(w_start + kernel_width, width);
201 h_start = max(h_start, IntImm(h_start.ty(), 0));
202 w_start = max(w_start, IntImm(w_start.ty(), 0));
203 divide_factor = max((h_end - h_start) * (w_end - w_start), IntImm(h_end.ty(), 1));
204 }
205 return tvm::prim::sum(
206 tvm::if_then_else(prim::And(prim::And(out_idx[height_axis] >= out_idx_lower_h,
207 out_idx[height_axis] < out_height),
208 prim::And(out_idx[width_axis] >= out_idx_lower_w,
209 out_idx[width_axis] < out_width)),
210 out_grad(out_idx) / divide_factor,
211 MakeConst(PrimType(out_grad->dtype), 0)),
212 {windowh, windoww});
213 },
214 "T_pool_grad", "pool_grad_avg");
215 } else {
216 TVM_FFI_THROW(ValueError) << "Unrecognized pool_type: " << pool_type;
217 }
218}
219
231inline bool find_depth_height_width(const std::string& layout, int* depth_axis, int* height_axis,
232 int* width_axis) {
233 if (depth_axis) *depth_axis = -1;
234 if (height_axis) *height_axis = -1;
235 if (width_axis) *width_axis = -1;
236 int curr_idx = 0;
237 for (size_t i = 0; i < layout.size(); ++i) {
238 if ((layout[i] >= 'A' && layout[i] <= 'Z') || (layout[i] >= 'a' && layout[i] <= 'z')) {
239 if (layout[i] == 'D' && depth_axis) {
240 if (*depth_axis != -1) return false;
241 *depth_axis = curr_idx;
242 } else if (layout[i] == 'H' && height_axis) {
243 if (*height_axis != -1) return false;
244 *height_axis = curr_idx;
245 } else if (layout[i] == 'W' && width_axis) {
246 if (*width_axis != -1) return false;
247 *width_axis = curr_idx;
248 } else if (layout[i] == 'd' || layout[i] == 'h' || layout[i] == 'w') {
249 // do not support split on height, width or depth, e.g., NCHW16w
250 return false;
251 }
252 ++curr_idx;
253 }
254 }
255 if ((depth_axis && *depth_axis == -1) || (height_axis && *height_axis == -1) ||
256 (width_axis && *width_axis == -1))
257 return false;
258 return true;
259}
260
261inline bool find_height_width(const std::string& layout, int* height_axis, int* width_axis) {
262 return find_depth_height_width(layout, /*depth_axis=*/nullptr, height_axis, width_axis);
263}
264
265inline bool find_width(const std::string& layout, int* width_axis) {
266 return find_depth_height_width(layout, /*depth_axis=*/nullptr, /*height_axis=*/nullptr,
267 width_axis);
268}
269
300inline Tensor pool_grad(const Tensor& out_grad, const Tensor& x,
301 const ffi::Array<PrimExpr>& kernel_size,
302 const ffi::Array<PrimExpr>& stride_size,
303 const ffi::Array<PrimExpr>& padding_size, PoolType pool_type,
304 bool ceil_mode, const std::string& layout = "NCHW",
305 bool count_include_pad = true) {
306 int height_axis = -1, width_axis = -1;
307 TVM_FFI_ICHECK(find_height_width(layout, &height_axis, &width_axis))
308 << "Unsupported layout " << layout;
309 return pool_grad_impl(out_grad, x, kernel_size, stride_size, padding_size, pool_type, ceil_mode,
310 height_axis, width_axis, count_include_pad);
311}
312
313inline PrimExpr start_index(const PrimVar& out_index, const PrimExpr& odim, const PrimExpr& idim) {
314 return indexdiv(out_index * idim, odim);
315}
316
317inline PrimExpr end_index(const PrimVar& out_index, const PrimExpr& odim, const PrimExpr& idim) {
318 PrimExpr tmp = indexdiv((out_index + 1) * idim, odim);
319 return tvm::prim::Select(indexmod((out_index + 1) * idim, odim) == 0, tmp, tmp + 1);
320}
321
332inline Tensor adaptive_pool_impl(const Tensor& x, const ffi::Array<PrimExpr>& output_size,
333 PoolType pool_type, const std::vector<int>& axes) {
334 const auto n_dim = output_size.size();
335 TVM_FFI_ICHECK_EQ(axes.size(), n_dim) << "The number of axes not equal to the in/out dimension";
336
337 ffi::Array<PrimExpr> data_shape = x->shape;
338 ffi::Array<PrimExpr> out_shape = data_shape;
339 ffi::Array<PrimExpr> in_size, out_size;
340 for (size_t i = 0; i < n_dim; ++i) {
341 in_size.push_back(data_shape[axes[i]]);
342 out_size.push_back(output_size[i]);
343 out_shape.Set(axes[i], out_size[i]);
344 }
345
346 auto get_iter_vars = [=](const ffi::Array<PrimVar>& output, bool reduce_indices) {
347 ffi::Array<PrimExpr> indices;
348 for (size_t i = 0; i < output.size(); ++i) indices.push_back(output[i]);
349 ffi::Array<s_tir::IterVar> reduce_axes;
350 for (size_t i = 0; i < n_dim; ++i) {
351 auto i_start = start_index(output[axes[i]], out_size[i], in_size[i]);
352 auto i_end = end_index(output[axes[i]], out_size[i], in_size[i]);
353 auto rv_name = "rv" + std::to_string(i);
354 auto rv_axis = tvm::te::reduce_axis(Range(0, i_end - i_start), rv_name);
355 reduce_axes.push_back(rv_axis);
356 if (reduce_indices) {
357 indices.Set(axes[i], i_start + rv_axis);
358 }
359 }
360 return std::make_tuple(indices, reduce_axes);
361 };
362
363 ffi::Map<ffi::String, ffi::Any> attrs;
364 if (pool_type == kMaxPool) {
365 attrs.Set(tvm::s_tir::attr::kScheduleRule.name,
366 tvm::ffi::String("meta_schedule.adaptive_pool_max"));
367 return tvm::te::compute(
368 out_shape,
369 [&](const ffi::Array<PrimVar>& output) {
370 ffi::Array<PrimExpr> indices;
371 ffi::Array<s_tir::IterVar> reduce_axes;
372 std::tie(indices, reduce_axes) = get_iter_vars(output, true);
373 return tvm::max(x(indices), reduce_axes); // NOLINT(*)
374 },
375 "adaptive_pool_max", "adaptive_pool_max", attrs);
376 } else if (pool_type == kAvgPool) {
377 attrs.Set(tvm::s_tir::attr::kScheduleRule.name,
378 tvm::ffi::String("meta_schedule.adaptive_pool_avg"));
379 auto pool_sum = tvm::te::compute(
380 out_shape,
381 [&](const ffi::Array<PrimVar>& output) {
382 ffi::Array<PrimExpr> indices;
383 ffi::Array<s_tir::IterVar> reduce_axes;
384 std::tie(indices, reduce_axes) = get_iter_vars(output, true);
385 return tvm::prim::sum(x(indices), reduce_axes);
386 },
387 "adaptive_pool_sum", "adaptive_pool_sum");
388
389 return tvm::te::compute(
390 out_shape,
391 [&](const ffi::Array<PrimVar>& output) {
392 ffi::Array<PrimExpr> indices;
393 ffi::Array<s_tir::IterVar> reduce_axes;
394 std::tie(indices, reduce_axes) = get_iter_vars(output, false);
395
396 PrimExpr divide_factor = tvm::prim::cast(1, PrimType(x->dtype));
397 for (size_t i = 0; i < n_dim; ++i) {
398 divide_factor *=
399 tvm::prim::cast(reduce_axes[i]->dom.value()->extent, PrimType::Int(32));
400 }
401
402 return div(pool_sum(indices), divide_factor);
403 },
404 "adaptive_pool_avg", kElementWise, attrs);
405 } else {
406 LOG(ERROR) << "Unrecognized pool_type: " << pool_type;
407 return x;
408 }
409}
410
437inline Tensor adaptive_pool(const Tensor& x, const ffi::Array<PrimExpr>& output_size,
438 PoolType pool_type, const std::string& layout = "NCHW") {
439 int height_axis = -1, width_axis = -1;
440 TVM_FFI_ICHECK(find_height_width(layout, &height_axis, &width_axis))
441 << "Unsupported layout " << layout;
442 return adaptive_pool_impl(x, output_size, pool_type, {height_axis, width_axis});
443}
444
453inline Tensor adaptive_pool3d(const Tensor& x, const ffi::Array<PrimExpr>& output_size,
454 PoolType pool_type, const std::string& layout = "NCDHW") {
455 int depth_axis = -1, height_axis = -1, width_axis = -1;
456 TVM_FFI_ICHECK(find_depth_height_width(layout, &depth_axis, &height_axis, &width_axis))
457 << "Unsupported layout " << layout;
458 return adaptive_pool_impl(x, output_size, pool_type, {depth_axis, height_axis, width_axis});
459}
460
469inline Tensor adaptive_pool1d(const Tensor& x, const ffi::Array<PrimExpr>& output_size,
470 PoolType pool_type, const std::string& layout = "NCW") {
471 int width_axis = -1;
472 TVM_FFI_ICHECK(find_width(layout, &width_axis)) << "Unsupported layout " << layout;
473 return adaptive_pool_impl(x, output_size, pool_type, {width_axis});
474}
475
501inline Tensor global_pool(const Tensor& x, PoolType pool_type, const std::string& layout = "NCHW") {
502 return adaptive_pool(x, ffi::Array<PrimExpr>{1, 1}, pool_type, layout);
503}
504
521inline Tensor pool_impl_nd(const Tensor& x, const ffi::Array<PrimExpr>& kernel_size,
522 const ffi::Array<PrimExpr>& stride_size,
523 const ffi::Array<PrimExpr>& dilation_size,
524 const ffi::Array<PrimExpr>& padding_size, PoolType pool_type,
525 bool ceil_mode, const std::vector<int>& axis, bool count_include_pad) {
526 using namespace tvm::prim;
527 int k_size = kernel_size.size();
528 int x_size = x->shape.size();
529 TVM_FFI_ICHECK_EQ(stride_size.size(), k_size)
530 << "Pooling stride_size must have same elements as kernel";
531 TVM_FFI_ICHECK_EQ(padding_size.size(), k_size * 2)
532 << "Pooling padding_size must has double elements of"
533 " kernel";
534 TVM_FFI_ICHECK_EQ(axis.size(), k_size) << "axis must have same elements as kernel";
535
536 ffi::Array<IterVar> daxis;
537 std::vector<PrimExpr> kernel(kernel_size.begin(), kernel_size.end());
538 std::vector<PrimExpr> stride(stride_size.begin(), stride_size.end());
539 std::vector<PrimExpr> dilation(dilation_size.begin(), dilation_size.end());
540 std::vector<PrimExpr> pad_head(padding_size.begin(), padding_size.begin() + k_size);
541 std::vector<PrimExpr> pad_tail(padding_size.begin() + k_size, padding_size.end());
542 std::vector<PrimExpr> offset(k_size, 0);
543 ffi::Array<PrimExpr> pad_before(std::vector<PrimExpr>(x_size, 0));
544 ffi::Array<PrimExpr> pad_after(std::vector<PrimExpr>(x_size, 0));
545 ffi::Array<PrimExpr> data_shape = x->shape;
546 ffi::Array<PrimExpr> out_shape = data_shape;
547
548 bool do_pad = false;
549 for (int i = 0; i < k_size; i++) {
550 int ii = axis[i];
551
552 if (ceil_mode) {
553 // The offset[i] is an additional padding to ensure we do ceil instead of floor when
554 // dividing by stride.
555 // In the case of ceil_mode=True and count_include_pad=True,
556 // in order to obtain the correct boundary,
557 // we also need to use the offset[i] to eliminate this extra padding.
558 offset[i] = stride[i] - 1;
559 pad_tail[i] += offset[i];
560 }
561
562 const auto* padding0 = pad_head[i].as<IntImmObj>();
563 const auto* padding1 = pad_tail[i].as<IntImmObj>();
564 do_pad = do_pad || (padding0 && padding0->value != 0) || (padding1 && padding1->value != 0);
565
566 daxis.push_back(tvm::te::reduce_axis(Range(0, kernel[i]), "rv" + std::to_string(i)));
567
568 pad_before.Set(ii, pad_head[i]);
569 pad_after.Set(ii, pad_tail[i]);
570
571 sym::Analyzer analyzer;
572
573 PrimExpr numerator =
574 data_shape[ii] - (kernel[i] - 1) * dilation[i] - 1 + pad_head[i] + pad_tail[i];
575 auto raw_out = indexdiv(numerator, stride[i]) + 1;
576 if (ceil_mode) {
577 // In the case of ceil_mode=True, we need to check if the last pooling window is valid.
578 // If not, we skip the last window as it would start in the bottom padded region,
579 // we need to minus 1 to get the correct output shape.
580 auto invalid_last = (raw_out - 1) * stride[i] >= data_shape[ii] + pad_head[i];
581 auto out_dim = analyzer->Simplify(if_then_else(invalid_last, raw_out - 1, raw_out));
582 out_shape.Set(ii, out_dim);
583 } else {
584 auto out_dim = analyzer->Simplify(raw_out);
585 out_shape.Set(ii, out_dim);
586 }
587 }
588
589 ffi::Map<ffi::String, ffi::Any> attrs;
590 if (pool_type == kMaxPool) {
591 auto temp =
592 do_pad ? pad(x, pad_before, pad_after, tvm::prim::min_value(PrimType(x->dtype)), "pad_temp")
593 : x;
594 attrs.Set(tvm::s_tir::attr::kScheduleRule.name, tvm::ffi::String("meta_schedule.pool_max"));
595 return tvm::te::compute(
596 out_shape,
597 [&](const ffi::Array<PrimVar>& output) {
598 ffi::Array<PrimExpr> indices;
599 for (const PrimVar& var : output) indices.push_back(var);
600
601 for (int i = 0; i < k_size; i++) {
602 int ii = axis[i];
603 indices.Set(ii, output[ii] * stride[i] + daxis[i] * dilation[i]);
604 }
605 return tvm::max(temp(indices), daxis);
606 },
607 "pool_max", "pool_max", attrs);
608 } else if (pool_type == kAvgPool) {
609 attrs.Set(tvm::s_tir::attr::kScheduleRule.name, tvm::ffi::String("meta_schedule.pool_avg"));
610 // Pad the inputs
611 auto temp = do_pad ? pad(x, pad_before, pad_after, 0, "pad_temp") : x;
612
613 // TVM compute for summing the pooling window.
614 auto pool_sum = tvm::te::compute(
615 out_shape,
616 [&](const ffi::Array<PrimVar>& output) {
617 ffi::Array<PrimExpr> indices;
618 for (const PrimVar& var : output) indices.push_back(var);
619
620 for (int i = 0; i < k_size; i++) {
621 int ii = axis[i];
622 indices.Set(ii, output[ii] * stride[i] + daxis[i] * dilation[i]);
623 }
624 return tvm::prim::sum(temp(indices), daxis);
625 },
626 "pool_sum", "pool_sum");
627
628 // TVM compute for dividing the reduced window sum by kernel size.
629 return tvm::te::compute(
630 out_shape,
631 [&](const ffi::Array<PrimVar>& output) {
632 ffi::Array<PrimExpr> indices;
633 for (const PrimVar& var : output) indices.push_back(var);
634 if (count_include_pad) {
635 auto num_el = IntImm::Int32(1);
636 for (int i = 0; i < k_size; i++) {
637 int ii = axis[i];
638 PrimExpr start = output[ii] * stride[i] - pad_head[i];
639 // When computing the output shape in ceil_mode,
640 // we have added the extra padding of offset[i],
641 // so now in order to calculate the correct boundary ,
642 // we need to substract the offset[i].
643 PrimExpr end = start + (kernel[i] - 1) * dilation[i];
644 end = min(end, data_shape[ii] + pad_tail[i] - 1 - offset[i]);
645 num_el *= (end - start) / dilation[i] + 1;
646 }
647 return div(pool_sum(indices), num_el);
648 } else {
649 auto num_el = IntImm::Int32(1);
650 for (int i = 0; i < k_size; i++) {
651 int ii = axis[i];
652
653 // Let start and end contain the first and last index of our Tensor
654 // along the relevant dimension we use in our calculation.
655 // Assume indices -1, -2 represent the padding before (tail) and
656 // len(arr), len(arr) + 1 represent the padding after (head).
657 PrimExpr start = output[ii] * stride[i] - pad_head[i];
658 PrimExpr end = start + (kernel[i] - 1) * dilation[i];
659
660 // if start < 0, e.g. we start on a tail padded number this will be a positive
661 // number that represents the number of steps along the dilated kernel to reach a
662 // non-padded value. Otherwise this should be 0.
663 PrimExpr jumps_to_non_pad = (dilation[i] - 1 - start) / dilation[i];
664 jumps_to_non_pad = max(jumps_to_non_pad, IntImm(jumps_to_non_pad.ty(), 0));
665
666 end = min(end, data_shape[ii] - 1);
667 num_el *= (end - (start + dilation[i] * jumps_to_non_pad)) / dilation[i] + 1;
668 }
669
670 PrimExpr divide_factor = max(num_el, IntImm::Int32(1));
671 return div(pool_sum(indices), divide_factor);
672 }
673 },
674 "pool_avg", kElementWise, attrs);
675 } else {
676 LOG(ERROR) << "Unrecognized pool_type: " << pool_type;
677 return x;
678 }
679}
680
711inline Tensor pool1d(const Tensor& x, const ffi::Array<PrimExpr>& kernel_size,
712 const ffi::Array<PrimExpr>& stride_size,
713 const ffi::Array<PrimExpr>& dilation_size,
714 const ffi::Array<PrimExpr>& padding_size, PoolType pool_type, bool ceil_mode,
715 const std::string& layout = "NCW", bool count_include_pad = true) {
716 int width_axis = -1;
717 TVM_FFI_ICHECK(find_width(layout, &width_axis)) << "Unsupported layout " << layout;
718 std::vector<int> axis = {width_axis};
719 return pool_impl_nd(x, kernel_size, stride_size, dilation_size, padding_size, pool_type,
720 ceil_mode, axis, count_include_pad);
721}
722
753inline Tensor pool2d(const Tensor& x, const ffi::Array<PrimExpr>& kernel_size,
754 const ffi::Array<PrimExpr>& stride_size,
755 const ffi::Array<PrimExpr>& dilation_size,
756 const ffi::Array<PrimExpr>& padding_size, PoolType pool_type, bool ceil_mode,
757 const std::string& layout = "NCHW", bool count_include_pad = true) {
758 int height_axis = -1, width_axis = -1;
759 TVM_FFI_ICHECK(find_height_width(layout, &height_axis, &width_axis))
760 << "Unsupported layout " << layout;
761 std::vector<int> axis = {height_axis, width_axis};
762 return pool_impl_nd(x, kernel_size, stride_size, dilation_size, padding_size, pool_type,
763 ceil_mode, axis, count_include_pad);
764}
765
797inline Tensor pool3d(const Tensor& x, const ffi::Array<PrimExpr>& kernel_size,
798 const ffi::Array<PrimExpr>& stride_size,
799 const ffi::Array<PrimExpr>& dilation_size,
800 const ffi::Array<PrimExpr>& padding_size, PoolType pool_type, bool ceil_mode,
801 const std::string& layout = "NCDHW", bool count_include_pad = true) {
802 int depth_axis = -1, height_axis = -1, width_axis = -1;
803 TVM_FFI_ICHECK(find_depth_height_width(layout, &depth_axis, &height_axis, &width_axis))
804 << "Unsupported layout " << layout;
805 std::vector<int> axis = {depth_axis, height_axis, width_axis};
806 return pool_impl_nd(x, kernel_size, stride_size, dilation_size, padding_size, pool_type,
807 ceil_mode, axis, count_include_pad);
808}
809
810} // namespace nn
811} // namespace topi
812} // namespace tvm
813#endif // TVM_TOPI_NN_POOLING_H_
Algebra expression simplifications.
Constant integer literals in the program.
Definition expr.h:314
ffi::BigInt value
the Internal value.
Definition expr.h:317
Managed reference class to IntImmObj.
Definition expr.h:331
static IntImm Int32(ffi::BigInt value, Location loc=UnknownLoc())
Construct a scalar int32 constant.
Definition expr.h:360
Typed reference/view over any Expr whose ExprObj::ty is PrimType.
Definition expr.h:187
Definition type.h:182
static TVM_TIRX_DLL PrimType Int(int bits, int lanes=1)
Construct a signed integer type with fixed lanes.
Checked scalar view over a VarObj.
Definition expr.h:598
Range container
Definition expr.h:883
ExpectedType ty() const
Definition expr.h:164
Managed reference to AndObj.
Definition expr.h:578
Managed reference to SelectObj.
Definition expr.h:712
Managed reference to AnalyzerObj.
Definition analyzer.h:750
Tensor structure representing a possible input, or intermediate computation result.
Definition tensor.h:112
S-TIR (Schedulable TIR) statements and attributes.
Definition reduction.h:29
TVM_TIRX_DLL PrimExpr min_value(PrimType dtype, Location loc=UnknownLoc())
PrimExpr sum(PrimExpr source, ffi::Array< s_tir::IterVar > axis, ffi::Array< PrimExpr > init={}, Location loc=UnknownLoc())
sum of source expression over axis
TVM_TIRX_DLL PrimExpr cast(PrimExpr value, PrimType t, Location loc=UnknownLoc())
cast value to type.
constexpr ffi::reflection::AttrKey< ffi::String > kScheduleRule
Definition stmt.h:408
Tensor expression language DSL.
Definition extracted_task.h:33
PrimVar var(std::string name_hint, PrimType t=PrimType::Int(32))
Construct a new Var expression.
IterVar reduce_axis(Range dom, std::string name="rv")
Create a new IterVar for reduction operations.
Tensor compute(ffi::Array< PrimExpr > shape, FCompute fcompute, std::string name="tensor", std::string tag="", ffi::Map< ffi::String, ffi::Any > attrs={})
Construct a new tensor by computing over shape, using the computation rule: result_tensor[axis] = fco...
Tensor adaptive_pool3d(const Tensor &x, const ffi::Array< PrimExpr > &output_size, PoolType pool_type, const std::string &layout="NCDHW")
Adaptively perform pooling on three dimensional data. See the two dimensional version above for detai...
Definition pooling.h:453
Tensor adaptive_pool(const Tensor &x, const ffi::Array< PrimExpr > &output_size, PoolType pool_type, const std::string &layout="NCHW")
Adaptively perform pooling on height and width dimension of data. The pooling kernel and stride sizes...
Definition pooling.h:437
PoolType
Pooling type.
Definition pooling.h:45
@ kAvgPool
Definition pooling.h:46
@ kMaxPool
Definition pooling.h:47
PrimExpr start_index(const PrimVar &out_index, const PrimExpr &odim, const PrimExpr &idim)
Definition pooling.h:313
Tensor adaptive_pool1d(const Tensor &x, const ffi::Array< PrimExpr > &output_size, PoolType pool_type, const std::string &layout="NCW")
Adaptively perform pooling on one dimensional data. See the two dimensional version above for details...
Definition pooling.h:469
Tensor pool3d(const Tensor &x, const ffi::Array< PrimExpr > &kernel_size, const ffi::Array< PrimExpr > &stride_size, const ffi::Array< PrimExpr > &dilation_size, const ffi::Array< PrimExpr > &padding_size, PoolType pool_type, bool ceil_mode, const std::string &layout="NCDHW", bool count_include_pad=true)
Perform pooling on depth, height and width dimension of data. It decides the depth,...
Definition pooling.h:797
Tensor pool2d(const Tensor &x, const ffi::Array< PrimExpr > &kernel_size, const ffi::Array< PrimExpr > &stride_size, const ffi::Array< PrimExpr > &dilation_size, const ffi::Array< PrimExpr > &padding_size, PoolType pool_type, bool ceil_mode, const std::string &layout="NCHW", bool count_include_pad=true)
Perform pooling on height and width dimension of data. It decides the height and width dimension acco...
Definition pooling.h:753
Tensor pool_grad_impl(const Tensor &out_grad, const Tensor &x, const ffi::Array< PrimExpr > &kernel_size, const ffi::Array< PrimExpr > &stride_size, const ffi::Array< PrimExpr > &padding_size, PoolType pool_type, bool ceil_mode, const size_t height_axis, const size_t width_axis, bool count_include_pad)
Definition pooling.h:50
Tensor pool_grad(const Tensor &out_grad, const Tensor &x, const ffi::Array< PrimExpr > &kernel_size, const ffi::Array< PrimExpr > &stride_size, const ffi::Array< PrimExpr > &padding_size, PoolType pool_type, bool ceil_mode, const std::string &layout="NCHW", bool count_include_pad=true)
Calculate gradient of pooling on height and width dimension of data. It decides the height and width ...
Definition pooling.h:300
bool find_depth_height_width(const std::string &layout, int *depth_axis, int *height_axis, int *width_axis)
Find index of Depth, Height or Width dimension in a layout string.
Definition pooling.h:231
bool find_width(const std::string &layout, int *width_axis)
Definition pooling.h:265
Tensor pool_impl_nd(const Tensor &x, const ffi::Array< PrimExpr > &kernel_size, const ffi::Array< PrimExpr > &stride_size, const ffi::Array< PrimExpr > &dilation_size, const ffi::Array< PrimExpr > &padding_size, PoolType pool_type, bool ceil_mode, const std::vector< int > &axis, bool count_include_pad)
Perform pooling on N-dimension of data.
Definition pooling.h:521
PrimExpr end_index(const PrimVar &out_index, const PrimExpr &odim, const PrimExpr &idim)
Definition pooling.h:317
bool find_height_width(const std::string &layout, int *height_axis, int *width_axis)
Definition pooling.h:261
Tensor global_pool(const Tensor &x, PoolType pool_type, const std::string &layout="NCHW")
Perform global pooling on height and width dimension of data. It decides the height and width dimensi...
Definition pooling.h:501
Tensor adaptive_pool_impl(const Tensor &x, const ffi::Array< PrimExpr > &output_size, PoolType pool_type, const std::vector< int > &axes)
Perform adaptive pooling on N dimensional data.
Definition pooling.h:332
Tensor pool1d(const Tensor &x, const ffi::Array< PrimExpr > &kernel_size, const ffi::Array< PrimExpr > &stride_size, const ffi::Array< PrimExpr > &dilation_size, const ffi::Array< PrimExpr > &padding_size, PoolType pool_type, bool ceil_mode, const std::string &layout="NCW", bool count_include_pad=true)
Perform pooling on the width dimension of data. Width axis is determined by the layout string in whic...
Definition pooling.h:711
constexpr auto kElementWise
Definition tags.h:32
tvm::te::Tensor pad(const tvm::te::Tensor &t, const tvm::ffi::Array< tvm::PrimExpr > &pad_before, tvm::ffi::Array< tvm::PrimExpr > pad_after=tvm::ffi::Array< tvm::PrimExpr >(), ffi::Optional< PrimExpr > pad_value=std::nullopt, std::string name="T_pad", std::string tag=kElementWise, std::string pad_mode="constant", const ffi::Array< PrimExpr > *dyn_output_shape=nullptr)
Creates an operation that performs padding.
Definition nn.h:155
Tensor min(const Tensor &data, const ffi::Optional< ffi::Array< int64_t > > &axis, bool keepdims=false, bool atleast1d=false)
Creates an operation that finds the minimum of elements over a given axis.
Definition reduction.h:432
FCommReduce MakeArgmaxReducer(bool select_last_index=false)
Definition reduction.h:513
Tensor max(const Tensor &data, const ffi::Optional< ffi::Array< int64_t > > &axis, bool keepdims=false, bool atleast1d=false)
Creates an operation that finds the maximum of elements over a given axis.
Definition reduction.h:451
constexpr auto kCommReduceIdx
Definition tags.h:35
Tensor argmax(const Tensor &data, const ffi::Optional< ffi::Array< int64_t > > &axis, bool keepdims=false, bool atleast1d=false, bool select_last_index=false)
Creates an operation that finds the indices of the maximum values over a given axis.
Definition reduction.h:562
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41
TVM_TIRX_DLL PrimExpr if_then_else(PrimExpr cond, PrimExpr true_value, PrimExpr false_value, Location loc=UnknownLoc())
Conditional expression.
TVM_TIRX_DLL PrimExpr indexdiv(PrimExpr a, PrimExpr b, Location loc=UnknownLoc())
compute floor(a / b) where a and b are non-negative.
TVM_TIRX_DLL PrimExpr div(PrimExpr a, PrimExpr b, Location loc=UnknownLoc())
compute division in C semantics.
PrimExpr max(PrimExpr source, ffi::Array< s_tir::IterVar > axis, ffi::Array< PrimExpr > init={}, Location loc=UnknownLoc())
max of source expression over axis
TVM_TIRX_DLL PrimExpr indexmod(PrimExpr a, PrimExpr b, Location loc=UnknownLoc())
compute the remainder floor(a / b) where a and b are non-negative.
Padding helpers.
Tag definitions.
NN op constructions.
Reduction op constructors.