tiny_dnn  1.0.0
A header only, dependency-free deep learning framework in C++11
maxpool_op.h
1 /*
2  COPYRIGHT
3 
4  All contributions by Taiga Nomi
5  Copyright (c) 2013, Taiga Nomi
6  All rights reserved.
7 
8  All other contributions:
9  Copyright (c) 2013-2016, the respective contributors.
10  All rights reserved.
11 
12  Each contributor holds copyright over their respective contributions.
13  The project versioning (Git) records all such contribution source information.
14 
15  LICENSE
16 
17  The BSD 3-Clause License
18 
19 
20  Redistribution and use in source and binary forms, with or without
21  modification, are permitted provided that the following conditions are met:
22 
23  * Redistributions of source code must retain the above copyright notice, this
24  list of conditions and the following disclaimer.
25 
26  * Redistributions in binary form must reproduce the above copyright notice,
27  this list of conditions and the following disclaimer in the documentation
28  and/or other materials provided with the distribution.
29 
30  * Neither the name of tiny-dnn nor the names of its
31  contributors may be used to endorse or promote products derived from
32  this software without specific prior written permission.
33 
34  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
35  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
38  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
40  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
41  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
43  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 */
45 #pragma once
46 
47 #include "tiny_dnn/core/framework/op_kernel.h"
48 
49 #include "tiny_dnn/core/kernels/maxpool_op_internal.h"
50 #include "tiny_dnn/core/kernels/maxpool_op_nnpack.h"
51 #include "tiny_dnn/core/kernels/maxpool_op_avx.h"
52 
53 namespace tiny_dnn {
54 
55 class MaxPoolOp : public core::OpKernel {
56  public:
57  explicit MaxPoolOp(const core::OpKernelConstruction& context)
58  : core::OpKernel(context) {}
59 
60  void compute(const core::OpKernelContext& context) override {
61  auto& params = OpKernel::params_->maxpool();
62 
63  // incomimg/outcoming data
64  const tensor_t& in_data = context.input(0);
65  tensor_t& out_data = context.output(1);
66 
67  // initialize outputs
68  fill_tensor(out_data, float_t(0));
69 
70  // call convolution algorithm depending
71  // on the selected engine type
72 
73  const core::backend_t engine = context.engine();
74 
75  if (engine == core::backend_t::internal) {
76  kernels::maxpool_op_internal(
77  in_data,
78  out_data,
79  params.out2inmax,
80  params.out2in,
81  context.parallelize());
82  } else if (engine == core::backend_t::nnpack) {
83  // NNPACK supports stride != 2 or pool_size !=2
84  // there's optimization over stride=2 and pool_size=2
85  /*
86  if (params.stride_x != 2 || params.stride_y != 2) {
87  throw nn_error("NNPACK Max-Pool requires a stride == 2.");
88  }
89 
90  if (params.pool_size_x != 2 || params.pool_size_y != 2) {
91  throw nn_error("NNPACK Max-Pool requires a pool size == 2.");
92  }
93 
94  */
95  kernels::maxpool_op_nnpack(
96  in_data,
97  out_data,
98  params);
99  } else if (engine == core::backend_t::avx) {
100  kernels::maxpool_op_avx(
101  in_data,
102  out_data,
103  params.out2inmax,
104  params.out2in,
105  context.parallelize());
106  } else {
107  throw nn_error("Not supported engine: " + to_string(engine));
108  }
109  }
110 };
111 
112 } // namespace tiny_dnn
Definition: maxpool_op.h:55
Definition: op_kernel.h:55
Definition: op_kernel.h:72
Definition: op_kernel.h:175
error exception class for tiny-dnn
Definition: nn_error.h:37