ONE - On-device Neural Engine
Loading...
Searching...
No Matches
MaxPool2D.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#include "core/OMUtils.h"
19#include "core/OMKernelData.h"
20#include "core/OMDataType.h"
23#include "execute/OMUtils.h"
26
27using namespace onert_micro;
28using namespace onert_micro::core;
29using namespace onert_micro::train;
30
31namespace
32{
33
34constexpr uint32_t inputTensorIdx = 0;
35constexpr uint32_t outputTensorIdx = 0;
36
37} // namespace
38
39/*
40 * - Calculate input gradient - Optional (not required if it is last op)
41 */
42OMStatus onert_micro::train::train_kernel_CircleMaxPool2D(const OMBackpropExecuteArgs &args)
43{
44 // Check is it last layer for training
45 if (args.is_last_layer)
46 {
47 return Ok;
48 }
49
50 core::OMRuntimeStorage &forward_storage = args.forward_storage;
51 core::OMRuntimeStorage &backward_storage = args.backward_storage;
52 core::OMRuntimeContext &context = args.backward_context;
53 uint16_t op_index = args.kernel_index;
54
55 const circle::Tensor *input;
56 const circle::Tensor *output;
57
58 uint8_t *input_data;
59 uint8_t *dloss_dinput_data;
60
61 uint8_t *dloss_doutput_data;
62
63 const circle::Pool2DOptions *options;
64 // Read kernel
65 {
66 execute::OMRuntimeKernel runtime_kernel;
67 runtime_kernel.readKernel(op_index, context);
68
69 input = runtime_kernel.inputs[inputTensorIdx];
70 output = runtime_kernel.outputs[outputTensorIdx];
71 assert(input != nullptr);
72 assert(output != nullptr);
73
74 // Read forward storage
75 {
76 runtime_kernel.getDataFromStorage(op_index, forward_storage, context);
77
78 input_data = runtime_kernel.inputs_data[inputTensorIdx];
79 assert(input_data != nullptr);
80 }
81
82 // Read backward storage
83 {
84 runtime_kernel.getDataFromStorage(op_index, backward_storage, context);
85
86 dloss_dinput_data = runtime_kernel.inputs_data[inputTensorIdx];
87 dloss_doutput_data = runtime_kernel.outputs_data[outputTensorIdx];
88
89 assert(dloss_dinput_data != nullptr);
90 assert(dloss_doutput_data != nullptr);
91 }
92
93 options = runtime_kernel.first_operator->builtin_options_as_Pool2DOptions();
94 }
95
96 assert(options->fused_activation_function() == circle::ActivationFunctionType_NONE);
97 if (options->fused_activation_function() != circle::ActivationFunctionType_NONE)
98 return UnsupportedType;
99
100 OMRuntimeShape input_shape(input);
102
103 int32_t padding_h = 0;
104 int32_t padding_w = 0;
105
106 const int input_width = input_shape.dims(2);
107 const int input_height = input_shape.dims(1);
109 options->stride_h(), options->stride_w(), 1 /* dilation_rate_height */,
110 1 /* dilation_rate_width */, input_height, input_width, options->filter_height(),
111 options->filter_width(), options->padding(), &padding_h, &padding_w);
112
113 core::Pool2DParams params{};
114 params.pad_h = padding_h;
115 params.pad_w = padding_w;
116 params.stride_h = options->stride_h();
117 params.stride_w = options->stride_w();
118 params.filter_h = options->filter_height();
119 params.filter_w = options->filter_width();
120
121 // Set input grad to zero
122 for (size_t i = 0; i < input_shape.flatSize() * sizeof(float); i += sizeof(float))
123 *static_cast<float *>(static_cast<void *>(dloss_dinput_data + i)) = 0;
124
125 // Calculate input grad
126 pal::MaxPool2D(params, input_shape, core::utils::castInputData<float>(input_data), output_shape,
127 core::utils::castInputData<float>(dloss_doutput_data),
128 core::utils::castOutputData<float>(dloss_dinput_data));
129
130 return Ok;
131}
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]
const luci_interpreter::RuntimeShape output_shape
constexpr uint32_t outputTensorIdx
args
Definition infer.py:21
list input_data
Definition infer.py:29
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
void MaxPool2D(const core::Pool2DParams &params, const core::OMRuntimeShape &input_shape, const float *input_data, const core::OMRuntimeShape &dloss_doutput_shape, const float *dloss_doutput_data, float *dloss_dinput_data)
@ UnsupportedType
Definition OMStatus.h:26