2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2017 The TensorFlow Authors. All Rights Reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #ifndef __NNFW_CKER_DEPTHWISE_CONV_H__
19 #define __NNFW_CKER_DEPTHWISE_CONV_H__
21 #include "cker/Shape.h"
22 #include "cker/Types.h"
23 #include "cker/Utils.h"
24 #include "cker/neon/neon_check.h"
25 #include "cker/operation/optimized/DepthwiseConvFloat.h"
26 #include "cker/operation/optimized/DepthwiseConvUint8.h"
27 #include "cker/CpuBackendThreadpool.h"
34 // TODO(luwa): add multithread to per-channel depthwise_conv
35 // DepthwiseConv can run with multi threads on the dim specified by thread_dim.
36 // Each thread processes output elements on dim, thread_dim, in the range of
37 // [thread_start, thread_end).
38 // For example, assume thread_start = 2, thread_end = 6, and thread_dim = 1, it
39 // means that it will calculate DepthwiseConv for output_data[:, 2:5, :, :].
40 template <typename T, typename TS> struct DepthwiseConvWorkerTask : cpu_backend_threadpool::Task
42 DepthwiseConvWorkerTask(const DepthwiseConvParams ¶ms, const Shape &input_shape,
43 const T *input_data, const Shape &filter_shape, const T *filter_data,
44 const Shape &bias_shape, const TS *bias_data, const Shape &output_shape,
45 T *output_data, int thread_start, int thread_end, int thread_dim)
46 : params_(params), input_shape_(input_shape), input_data_(input_data),
47 filter_shape_(filter_shape), filter_data_(filter_data), bias_shape_(bias_shape),
48 bias_data_(bias_data), output_shape_(output_shape), output_data_(output_data),
49 thread_start_(thread_start), thread_end_(thread_end), thread_dim_(thread_dim)
55 optimized::DepthwiseConvImpl(params_, input_shape_, input_data_, filter_shape_, filter_data_,
56 bias_shape_, bias_data_, output_shape_, output_data_,
57 thread_start_, thread_end_, thread_dim_);
61 const DepthwiseConvParams ¶ms_;
62 const Shape &input_shape_;
64 const Shape &filter_shape_;
65 const T *filter_data_;
66 const Shape &bias_shape_;
68 const Shape &output_shape_;
70 // const CpuFlags& cpu_flags_;
76 inline int HowManyConvThreads(const Shape &output_shape, const Shape &filter_shape)
78 // How many scalar multiplications are needed to make it worth using one
80 static constexpr int kMinMulPerThread = 1 << 13; // 8k
81 const int filter_height = filter_shape.Dims(1);
82 const int filter_width = filter_shape.Dims(2);
83 const int num_muls = output_shape.FlatSize() * filter_height * filter_width;
84 // Try to avoid real runtime divisions if possible by dividing by a
85 // compile-time constant.
86 int thread_count = std::max(1, num_muls / kMinMulPerThread);
90 inline bool MultithreadAlongBatches(int thread_count, int batches)
92 assert(thread_count >= 2);
93 // If there are fewer batch entries than the number of threads we want to use,
94 // then better do intra-batch-entry multithreading.
95 if (batches < thread_count)
99 // If there are at least 2 batch entries to be handed to each thread, then
100 // it's safe to proceed with batch-wise multithreading: each thread will have
101 // approximately equal number of batch entries to handle, so the load
102 // balancing will be reasonable, and the amount to which the load is not
103 // perfectly balanced will be offset by the inherent advantages of
104 // batch-wise multithreading (each thread is more efficient thanks to working
105 // on larger buffers with less boundary-handling overhead).
106 if (batches >= 2 * thread_count)
110 // In the limit case were there are at least 1 but not much more than 1
111 // batch entries per thread, it may be a good idea to do per-batch
112 // multithreading if the number of batch entries is a multiple of the number
113 // of threads, so that each thread will have the same number of batch entries
115 return ((batches % thread_count) == 0);
118 template <typename T, typename TS>
119 inline void DepthwiseConv(const DepthwiseConvParams ¶ms, const Shape &input_shape,
120 const T *input_data, const Shape &filter_shape, const T *filter_data,
121 const Shape &bias_shape, const TS *bias_data, const Shape &output_shape,
122 T *output_data, ruy::Context *ruy_context)
124 assert(input_shape.DimensionsCount() == 4);
125 assert(filter_shape.DimensionsCount() == 4);
126 assert(output_shape.DimensionsCount() == 4);
128 int thread_count = HowManyConvThreads(output_shape, filter_shape);
130 // NOTE Borrow RuyContext to get max_num_threads setting
131 // TODO Define and use max_num_threads for CPU backend
132 const auto max_threads = (ruy_context == nullptr) ? 1 : ruy_context->max_num_threads();
134 thread_count = std::max(1, std::min(thread_count, max_threads));
135 // Cap the number of threads to 2 for float path to avoid regression in
136 // performance (b/132294857).
137 if (std::is_floating_point<T>::value)
139 thread_count = std::min(thread_count, 2);
142 const int output_batches = output_shape.Dims(0);
143 const int output_height = output_shape.Dims(1);
145 if (thread_count == 1)
147 optimized::DepthwiseConvImpl(params, input_shape, input_data, filter_shape, filter_data,
148 bias_shape, bias_data, output_shape, output_data, 0, output_height,
153 int thread_dim, thread_dim_size;
154 if (MultithreadAlongBatches(thread_count, output_batches))
157 thread_dim_size = output_batches;
162 thread_dim_size = output_height;
165 std::vector<DepthwiseConvWorkerTask<T, TS>> tasks;
166 // TODO(b/131746020) don't create new heap allocations every time.
167 // At least we make it a single heap allocation by using reserve().
168 tasks.reserve(thread_count);
169 int thread_start = 0;
170 for (int i = 0; i < thread_count; ++i)
172 int thread_end = thread_start + (thread_dim_size - thread_start) / (thread_count - i);
173 tasks.emplace_back(params, input_shape, input_data, filter_shape, filter_data, bias_shape,
174 bias_data, output_shape, output_data, thread_start, thread_end, thread_dim);
175 thread_start = thread_end;
177 cpu_backend_threadpool::Execute(tasks.size(), tasks.data(), ruy_context);
183 #endif // __NNFW_CKER_DEPTHWISE_CONV_H__