ONE - On-device Neural Engine
Loading...
Searching...
No Matches
PALDepthwiseConv2DCommon.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2017 The TensorFlow Authors. All Rights Reserved.
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17
18#ifndef LUCI_INTERPRETER_PAL_DEPTHWISE_CONV2D_COMMON_H
19#define LUCI_INTERPRETER_PAL_DEPTHWISE_CONV2D_COMMON_H
20#include "Params.h"
21#include "PALUtils.h"
22
24{
25static inline void DepthwiseConv2D(const ConvParams &params, const int32_t *input_shape,
26 const float *input_data, const int32_t *filter_shape,
27 const float *filter_data, const float *bias_data,
28 const int32_t *output_shape, float *output_data)
29{
30 const int stride_width = params.stride_width;
31 const int stride_height = params.stride_height;
32 const int dilation_width_factor = params.dilation_width_factor;
33 const int dilation_height_factor = params.dilation_height_factor;
34 const int pad_width = params.padding_values.width;
35 const int pad_height = params.padding_values.height;
36 const int depth_multiplier = params.depth_multiplier;
37 const float output_activation_min = params.float_activation_min;
38 const float output_activation_max = params.float_activation_max;
39
40 const int batches = input_shape[0];
41 const int input_height = input_shape[1];
42 const int input_width = input_shape[2];
43 const int input_depth = input_shape[3];
44 const int filter_height = filter_shape[1];
45 const int filter_width = filter_shape[2];
46 const int output_height = output_shape[1];
47 const int output_width = output_shape[2];
48
49 for (int b = 0; b < batches; ++b)
50 {
51 for (int out_y = 0; out_y < output_height; ++out_y)
52 {
53 for (int out_x = 0; out_x < output_width; ++out_x)
54 {
55 for (int ic = 0; ic < input_depth; ++ic)
56 {
57 for (int m = 0; m < depth_multiplier; m++)
58 {
59 const int oc = m + ic * depth_multiplier;
60 const int in_x_origin = (out_x * stride_width) - pad_width;
61 const int in_y_origin = (out_y * stride_height) - pad_height;
62 float total = 0.f;
63 for (int filter_y = 0; filter_y < filter_height; ++filter_y)
64 {
65 for (int filter_x = 0; filter_x < filter_width; ++filter_x)
66 {
67 const int in_x = in_x_origin + dilation_width_factor * filter_x;
68 const int in_y = in_y_origin + dilation_height_factor * filter_y;
69 // If the location is outside the bounds of the input image,
70 // use zero as a default value.
71 if ((in_x >= 0) && (in_x < input_width) && (in_y >= 0) && (in_y < input_height))
72 {
73 float input_value = input_data[offset(input_shape, b, in_y, in_x, ic)];
74 float filter_value = filter_data[offset(filter_shape, 0, filter_y, filter_x, oc)];
75 total += (input_value * filter_value);
76 }
77 }
78 }
79 float bias_value = 0.0f;
80 if (bias_data)
81 {
82 bias_value = bias_data[oc];
83 }
85 total + bias_value, output_activation_min, output_activation_max);
86 }
87 }
88 }
89 }
90 }
91}
92
93} // namespace luci_interpreter_pal
94
95#endif // LUCI_INTERPRETER_PAL_DEPTHWISE_CONV2D_COMMON_H
const luci_interpreter::RuntimeShape output_shape
list input_data
Definition infer.py:29
int offset(const int32_t *dims_data, int i0, int i1, int i2, int i3)
Definition PALUtils.h:193
T activationFunctionWithMinMax(T x, T output_activation_min, T output_activation_max)
Definition PALUtils.h:204