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
48OMStatus onert_micro::execute::execute_kernel_CircleTransposeConv(const OMExecuteArgs &execute_args)
49{
50 core::OMRuntimeContext &runtime_context = execute_args.runtime_context;
51 core::OMRuntimeStorage &runtime_storage = execute_args.runtime_storage;
52 uint16_t op_index = execute_args.kernel_index;
53
54 const circle::Tensor *input;
55 const circle::Tensor *weight;
56 const circle::Tensor *output;
57
58 uint8_t *input_data;
59 uint8_t *weight_data;
60 uint8_t *bias_data;
61 uint8_t *output_data;
62
63 const circle::TransposeConvOptions *options;
64 // Read kernel
65 {
66 execute::OMRuntimeKernel runtime_kernel;
67 OMStatus status = runtime_kernel.readKernel(op_index, runtime_context);
68 if (status != Ok)
69 return status;
70
71 input = runtime_kernel.inputs[kInputTensorIdx];
72 weight = runtime_kernel.inputs[kWeightTensorIdx];
73 output = runtime_kernel.outputs[kOutputTensorIdx];
74 assert(input != nullptr);
75 assert(weight != nullptr);
76 // Bias can be nullptr
77 assert(output != nullptr);
78
79 status = runtime_kernel.getDataFromStorage(op_index, runtime_storage, runtime_context);
80 if (status != Ok)
81 return status;
82
83 input_data = runtime_kernel.inputs_data[kInputTensorIdx];
84 weight_data = runtime_kernel.inputs_data[kWeightTensorIdx];
85 bias_data = runtime_kernel.inputs_data[kBiasTensorIdx];
86 output_data = runtime_kernel.outputs_data[kOutputTensorIdx];
87 assert(input_data != nullptr);
88 assert(weight_data != nullptr);
89 // Bias can be nullptr
90 assert(output_data != nullptr);
91
92 options = runtime_kernel.first_operator->builtin_options_as_TransposeConvOptions();
93 }
94
95 OMStatus status;
96
97 int32_t padding_h = 0;
98 int32_t padding_w = 0;
99
100 OMRuntimeShape weight_shape(weight);
101 OMRuntimeShape input_shape(input);
102
103 const int input_width = input_shape.dims(2);
104 const int input_height = input_shape.dims(1);
105 const int weight_width = weight_shape.dims(2);
106 const int weight_height = weight_shape.dims(1);
107
108 // Note: Dilation height and width are always 1 for transpose_conv
109 execute::computePaddingHeightWidth(options->stride_h(), options->stride_w(), 1, 1, input_height,
110 input_width, weight_height, weight_width, options->padding(),
111 &padding_h, &padding_w);
112
113 switch (input->type())
114 {
115#ifndef DIS_FLOAT
116 case circle::TensorType_FLOAT32:
117 {
118
119 FloatConv2D params{};
120 status = calculateActivationRange(options->fused_activation_function(),
121 &params.activation_min, &params.activation_max);
122 params.stride_w = options->stride_w();
123 params.stride_h = options->stride_h();
124 params.dilation_width_factor = 1;
125 params.dilation_height_factor = 1;
126 params.pad_h = padding_h;
127 params.pad_w = padding_w;
128
129 if (status != Ok)
130 return status;
131
133 &params, input_shape, core::utils::castInputData<float>(input_data), weight_shape,
134 core::utils::castInputData<float>(weight_data),
135 core::utils::castInputData<float>(bias_data), OMRuntimeShape(output),
136 core::utils::castOutputData<float>(output_data));
137 assert(status == Ok);
138 }
139 break;
140#endif // DIS_FLOAT
141 default:
142 {
143 status = UnsupportedActivation;
144 assert(false && "Unsupported type.");
145 }
146 }
147
148 return status;
149}
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]
list input_data
Definition infer.py:29
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 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