ONE - On-device Neural Engine
Loading...
Searching...
No Matches
TransposeConv.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "OMStatus.h"
18
19#include "core/OMUtils.h"
20#include "core/OMKernelData.h"
21
23#include "execute/OMUtils.h"
25
26#include "PALTransposeConv.h"
27
28using namespace onert_micro;
29using namespace onert_micro::core;
30using namespace onert_micro::execute;
31
32namespace
33{
34
35// For the TfLite transpose_conv implementation, input tensor 0 corresponds to
36// the OutputShapeTensor. However, since TFLM does not support dynamic tensors,
37// the TFLM implementation ignores input tensor 0 and the only inputs we care
38// about are kFilterTensor, kInputTensor and kBiasTensor.
39
40constexpr int kWeightTensorIdx = 1;
41constexpr int kInputTensorIdx = 2;
42constexpr int kBiasTensorIdx = 3;
43constexpr int kOutputTensorIdx = 0;
44
45} // namespace
46
47// NOTE: doesn't currently support dynamic shapes
48namespace onert_micro
49{
50namespace execute
51{
52
54{
55 core::OMRuntimeContext &runtime_context = execute_args.runtime_context;
56 core::OMRuntimeStorage &runtime_storage = execute_args.runtime_storage;
57 uint16_t op_index = execute_args.kernel_index;
58
59 const circle::Tensor *input;
60 const circle::Tensor *weight;
61 const circle::Tensor *output;
62
63 uint8_t *input_data;
64 uint8_t *weight_data;
65 uint8_t *bias_data;
66 uint8_t *output_data;
67
68 const circle::TransposeConvOptions *options;
69 // Read kernel
70 {
71 execute::OMRuntimeKernel runtime_kernel;
72 OMStatus status = runtime_kernel.readKernel(op_index, runtime_context);
73 if (status != Ok)
74 return status;
75
76 input = runtime_kernel.inputs[kInputTensorIdx];
77 weight = runtime_kernel.inputs[kWeightTensorIdx];
78 output = runtime_kernel.outputs[kOutputTensorIdx];
79 assert(input != nullptr);
80 assert(weight != nullptr);
81 // Bias can be nullptr
82 assert(output != nullptr);
83
84 status = runtime_kernel.getDataFromStorage(op_index, runtime_storage, runtime_context);
85 if (status != Ok)
86 return status;
87
88 input_data = runtime_kernel.inputs_data[kInputTensorIdx];
89 weight_data = runtime_kernel.inputs_data[kWeightTensorIdx];
90 bias_data = runtime_kernel.inputs_data[kBiasTensorIdx];
91 output_data = runtime_kernel.outputs_data[kOutputTensorIdx];
92 assert(input_data != nullptr);
93 assert(weight_data != nullptr);
94 // Bias can be nullptr
95 assert(output_data != nullptr);
96
97 options = runtime_kernel.first_operator->builtin_options_as_TransposeConvOptions();
98 }
99
100 OMStatus status;
101
102 int32_t padding_h = 0;
103 int32_t padding_w = 0;
104
105 OMRuntimeShape weight_shape(weight);
106 OMRuntimeShape input_shape(input);
107
108 const int input_width = input_shape.dims(2);
109 const int input_height = input_shape.dims(1);
110 const int weight_width = weight_shape.dims(2);
111 const int weight_height = weight_shape.dims(1);
112
113 // Note: Dilation height and width are always 1 for transpose_conv
114 execute::computePaddingHeightWidth(options->stride_h(), options->stride_w(), 1, 1, input_height,
115 input_width, weight_height, weight_width, options->padding(),
116 &padding_h, &padding_w);
117
118 switch (input->type())
119 {
120#ifndef DIS_FLOAT
121 case circle::TensorType_FLOAT32:
122 {
123
124 FloatConv2D params{};
125 status = calculateActivationRange(options->fused_activation_function(),
126 &params.activation_min, &params.activation_max);
127 params.stride_w = options->stride_w();
128 params.stride_h = options->stride_h();
129 params.dilation_width_factor = 1;
130 params.dilation_height_factor = 1;
131 params.pad_h = padding_h;
132 params.pad_w = padding_w;
133
134 if (status != Ok)
135 return status;
136
138 &params, input_shape, core::utils::castInputData<float>(input_data), weight_shape,
139 core::utils::castInputData<float>(weight_data),
140 core::utils::castInputData<float>(bias_data), OMRuntimeShape(output),
141 core::utils::castOutputData<float>(output_data));
142 assert(status == Ok);
143 }
144 break;
145#endif // DIS_FLOAT
146 default:
147 {
148 status = UnsupportedActivation;
149 assert(false && "Unsupported type.");
150 }
151 }
152
153 return status;
154}
155
156} // namespace execute
157} // namespace onert_micro
uint8_t * outputs_data[maxOutputSize]
const circle::Operator * first_operator
OMStatus getDataFromStorage(uint16_t op_index, core::OMRuntimeStorage &storage, core::OMRuntimeContext &context)
OMStatus readKernel(uint16_t op_index, core::OMRuntimeContext &runtime_context)
const circle::Tensor * outputs[maxOutputSize]
const circle::Tensor * inputs[maxInputSize]
OMStatus TransposeConv< float >(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)
OMStatus execute_kernel_CircleTransposeConv(const OMExecuteArgs &execute_args)
OMStatus calculateActivationRange(circle::ActivationFunctionType activation, T *activation_min, T *activation_max)
Definition OMUtils.h:36
void computePaddingHeightWidth(int32_t stride_height, int32_t stride_width, int32_t dilation_rate_height, int32_t dilation_rate_width, int32_t in_height, int32_t in_width, int32_t filter_height, int32_t filter_width, circle::Padding padding, int32_t *padding_h, int32_t *padding_w)
Definition OMUtils.h:141
@ UnsupportedActivation
Definition OMStatus.h:28
core::OMRuntimeContext & runtime_context
core::OMRuntimeStorage & runtime_storage