tiny_dnn  1.0.0
A header only, dependency-free deep learning framework in C++11
maxpool_op_nnpack.h
1 /*
2  Copyright (c) 2016, Taiga Nomi, Edgar Riba
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the <organization> nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
17  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 #pragma once
28 
29 #ifdef CNN_USE_NNPACK
30 #include "nnpack.h"
31 #endif
32 
33 namespace tiny_dnn {
34 namespace kernels {
35 
36 inline void maxpool_op_nnpack(const tensor_t& in_data,
37  tensor_t& out_data,
38  const maxpool_params& params) {
39 #ifdef CNN_USE_NNPACK
40  const serial_size_t input_channels = params.in.depth_;
41 
42  const nnp_size input_size = {
43  static_cast<size_t>(params.in.width_),
44  static_cast<size_t>(params.in.height_)
45  };
46 
47  const nnp_padding input_padding = {
48  static_cast<size_t>(0), // top
49  static_cast<size_t>(0), // right
50  static_cast<size_t>(0), // bottom
51  static_cast<size_t>(0) // left
52  };
53 
54  const nnp_size pooling_size = {
55  static_cast<size_t>(params.pool_size_x),
56  static_cast<size_t>(params.pool_size_y)
57  };
58 
59  const nnp_size pooling_stride = {
60  static_cast<size_t>(params.stride_x),
61  static_cast<size_t>(params.stride_y)
62  };
63 
64  const float* input_ptr = in_data[0].data();
65  float* output_ptr = out_data[0].data();
66 
67  // TODO: embed it into a class
68  const size_t num_mkl_threads = 1;
69  pthreadpool_t threadpool = pthreadpool_create(num_mkl_threads);
70 
71  const size_t batch_size = 1;
72 
73  const auto status =
74  nnp_max_pooling_output(
75  batch_size,
76  input_channels,
77  input_size,
78  input_padding,
79  pooling_size,
80  pooling_stride,
81  input_ptr,
82  output_ptr,
83  threadpool);
84 
85  if (status != nnp_status_success) {
86  throw nn_error("Could not succeed with nnp_max_pooling_output");
87  }
88 
89  // TODO: embed it into a class
90  pthreadpool_destroy(threadpool);
91 #else
92  throw nn_error("TinyDNN has not been compiled with NNPACK support.");
93 #endif
94 }
95 
96 } // namespace kernels
97 } // namespace tiny_dnn