tiny_dnn 1.0.0
A header only, dependency-free deep learning framework in C++11
Loading...
Searching...
No Matches
conv2d_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#include "tiny_dnn/core/params/conv_params.h"
30
31#ifdef CNN_USE_NNPACK
32#include "nnpack.h"
33
34inline nnp_convolution_algorithm nnp_algorithm() {
35 return nnp_convolution_algorithm_auto;
36}
37
38inline nnp_convolution_transform_strategy nnp_kts() {
39 return nnp_convolution_transform_strategy_tuple_based;//some algorithm accept tuple based only
40}
41#endif
42
43namespace tiny_dnn {
44namespace kernels {
45
46inline void
47conv2d_op_nnpack(const tensor_t& in_data,
48 const vec_t& W,
49 const vec_t& bias,
50 tensor_t& out_data,
51 const core::conv_params& params) {
52#ifdef CNN_USE_NNPACK
53 nnp_status init_status = nnp_initialize();
54 if (init_status != nnp_status_success) {
55 throw nn_error("Cannot initialize NNPACK.");
56 }
57
58 // TOOD: use input config
59 const auto algorithm = nnp_algorithm();
60 const auto kernel_transform_strategy = nnp_kts();
61
62 const serial_size_t input_channels = params.in.depth_;
63 const serial_size_t output_channels = params.out.depth_;
64
65 //input data passed by convolution layer has been padded already
66 //set input_size to padded size
67 const nnp_size input_size = {
68 static_cast<size_t>(params.in_padded.width_),
69 static_cast<size_t>(params.in_padded.height_)
70 };
71
72 const nnp_size kernel_size = {
73 static_cast<size_t>(params.weight.width_),
74 static_cast<size_t>(params.weight.height_)
75 };
76
77 // input padded ,so no need to do padding
78 const float_t dx =0;// params.in_padded.width_ - params.in.width_;
79 const float_t dy =0;// params.in_padded.height_ - params.in.height_;
80
81 // we'll assume that padding is symmetric
82
83 const nnp_padding padding = {
84 static_cast<size_t>(dy/2), // top
85 static_cast<size_t>(dx/2), // right
86 static_cast<size_t>(dy/2), // bottom
87 static_cast<size_t>(dx/2) // left
88 };
89
90 const float* input_ptr = reinterpret_cast<const float*>(in_data[0].data());
91 const float* kernel_ptr = reinterpret_cast<const float*>(W.data());
92 const float* bias_ptr = reinterpret_cast<const float*>(bias.data());
93 const nnp_size stride= {
94 static_cast<size_t>(params.w_stride),
95 static_cast<size_t>(params.h_stride)
96 };
97
98 float* output_ptr = out_data[0].data();
99
100 // TODO: embed it into a class
101 const size_t num_mkl_threads = 1;
102 pthreadpool_t threadpool = pthreadpool_create(num_mkl_threads);
103
104 nnp_profile* profile = nullptr;
105
106 nnp_status status =
107 nnp_convolution_inference(
108 algorithm,
109 kernel_transform_strategy,
110 input_channels,
111 output_channels,
112 input_size,
113 padding,
114 kernel_size,
115 stride,
116 input_ptr,
117 kernel_ptr,
118 bias_ptr,
119 output_ptr,
120 threadpool,
121 profile);
122
123 if (status != nnp_status_success) {
124 throw nn_error("Could not succeed with nnp_convolution_inference");
125 }
126
127 // TODO: embed it into a class
128 pthreadpool_destroy(threadpool);
129#else
130 throw nn_error("TinyDNN has not been compiled with NNPACK support.");
131#endif
132}
133
134} // namespace kernels
135} // namespace tiny_dnn