ONE - On-device Neural Engine
Loading...
Searching...
No Matches
PALConv2DCommon.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 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 ONERT_MICRO_PAL_CONV2D_COMMON_H
19#define ONERT_MICRO_PAL_CONV2D_COMMON_H
20
21#include "PALUtils.h"
22
23#include "OMStatus.h"
24
25namespace onert_micro
26{
27namespace execute
28{
29namespace pal
30{
31OMStatus ConvFloat(const core::FloatConv2D *params, const core::OMRuntimeShape &input_shape,
32 const float *input_data, const core::OMRuntimeShape &filter_shape,
33 const float *filter_data, const float *bias_data,
34 const core::OMRuntimeShape &output_shape, float *output_data)
35{
36 const int stride_width = params->stride_w;
37 const int stride_height = params->stride_h;
38 const int dilation_width_factor = params->dilation_width_factor;
39 const int dilation_height_factor = params->dilation_height_factor;
40 const int pad_width = params->pad_w;
41 const int pad_height = params->pad_h;
42 const float output_activation_min = params->activation_min;
43 const float output_activation_max = params->activation_max;
44
45 const auto batches = input_shape.dims(0);
46 const int input_height = input_shape.dims(1);
47 const int input_width = input_shape.dims(2);
48 const int input_depth = input_shape.dims(3);
49 const int output_depth = filter_shape.dims(0);
50 const int filter_height = filter_shape.dims(1);
51 const int filter_width = filter_shape.dims(2);
52 const int output_height = output_shape.dims(1);
53 const int output_width = output_shape.dims(2);
54 for (int batch = 0; batch < batches; ++batch)
55 {
56 for (int out_y = 0; out_y < output_height; ++out_y)
57 {
58 const int in_y_origin = (out_y * stride_height) - pad_height;
59 for (int out_x = 0; out_x < output_width; ++out_x)
60 {
61 const int in_x_origin = (out_x * stride_width) - pad_width;
62 for (int out_channel = 0; out_channel < output_depth; ++out_channel)
63 {
64 float total = 0.f;
65 for (int filter_y = 0; filter_y < filter_height; ++filter_y)
66 {
67 const int in_y = in_y_origin + dilation_height_factor * filter_y;
68 for (int filter_x = 0; filter_x < filter_width; ++filter_x)
69 {
70 const int in_x = in_x_origin + dilation_width_factor * filter_x;
71
72 // Zero padding by omitting the areas outside the image.
73 const bool is_point_inside_image =
74 (in_x >= 0) && (in_x < input_width) && (in_y >= 0) && (in_y < input_height);
75
76 if (!is_point_inside_image)
77 {
78 continue;
79 }
80
81 for (int in_channel = 0; in_channel < input_depth; ++in_channel)
82 {
83 const int input_data_offset =
84 ((batch * input_height + in_y) * input_width + in_x) * input_depth + in_channel;
85
86 const int filter_data_offset =
87 ((out_channel * filter_height + filter_y) * filter_width + filter_x) *
88 input_depth +
89 in_channel;
90
91 const float input_value = input_data[input_data_offset];
92 const float filter_value = filter_data[filter_data_offset];
93 total += (input_value * filter_value);
94 }
95 }
96 }
97 // float bias_value = 0.0f;
98 if (bias_data)
99 {
100 total += bias_data[out_channel];
101 }
102
103 const int output_data_offset =
104 ((batch * output_height + out_y) * output_width + out_x) * output_depth + out_channel;
105
106 output_data[output_data_offset] =
107 std::min(std::max(total, output_activation_min), output_activation_max);
108 }
109 }
110 }
111 }
112 return Ok;
113}
114
115} // namespace pal
116} // namespace execute
117} // namespace onert_micro
118
119#endif // ONERT_MICRO_PAL_CONV2D_COMMON_H
int32_t dims(int i) const
Definition Tensor.h:108
const luci_interpreter::RuntimeShape output_shape
OMStatus ConvFloat(const core::FloatConv2D *params, const core::OMRuntimeShape &input_shape, const float *input_data, const core::OMRuntimeShape &filter_shape, const float *filter_data, const float *bias_data, const core::OMRuntimeShape &output_shape, float *output_data)