ONE - On-device Neural Engine
Loading...
Searching...
No Matches
PALAveragePool2d.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2021 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#ifndef LUCI_INTERPRETER_PAL_AVERAGEPOOL2D_H
18#define LUCI_INTERPRETER_PAL_AVERAGEPOOL2D_H
19
20#include <tensorflow/lite/kernels/internal/reference/integer_ops/pooling.h>
21#include <tensorflow/lite/kernels/internal/reference/pooling.h>
22#include <arm_nn_types.h>
23#include <arm_nnfunctions.h>
24
26{
27template <typename T>
28static inline void AveragePool(const tflite::PoolParams &params,
29 const tflite::RuntimeShape &input_shape, const T *input_data,
30 const tflite::RuntimeShape &output_shape, T *output_data,
31 const tflite::RuntimeShape &scratchpad_shape, T *scratchpad_data)
32{
33 {
34 // MARK: At this moment this operation is not supported
35 assert(false && "AveragePool NYI");
36 (void)params;
37 (void)input_shape;
38 (void)input_data;
39 (void)output_shape;
40 (void)output_data;
41 (void)scratchpad_shape;
42 (void)scratchpad_data;
43 }
44}
45
46template <>
47inline void AveragePool<int8_t>(const tflite::PoolParams &params,
48 const tflite::RuntimeShape &input_shape, const int8_t *input_data,
49 const tflite::RuntimeShape &output_shape, int8_t *output_data,
50 const tflite::RuntimeShape &scratchpad_shape,
51 int8_t *scratchpad_data)
52{
53 assert(input_shape.DimensionsCount() == 4);
54 assert(output_shape.DimensionsCount() == 4);
55 assert(scratchpad_data != nullptr);
56
57 const int32_t batches = tflite::MatchingDim(input_shape, 0, output_shape, 0);
58 assert(batches == 1);
59
60 const int depth = tflite::MatchingDim(input_shape, 3, output_shape, 3);
61
62 cmsis_nn_dims input_dims;
63 input_dims.n = 1;
64 input_dims.h = input_shape.Dims(1);
65 input_dims.w = input_shape.Dims(2);
66 input_dims.c = depth;
67
68 cmsis_nn_dims output_dims;
69 output_dims.n = 1;
70 output_dims.h = output_shape.Dims(1);
71 output_dims.w = output_shape.Dims(2);
72 output_dims.c = depth;
73
74 cmsis_nn_pool_params pool_params;
75 pool_params.stride.h = params.stride_height;
76 pool_params.stride.w = params.stride_width;
77 pool_params.padding.h = params.padding_values.height;
78 pool_params.padding.w = params.padding_values.width;
79 pool_params.activation.min = params.quantized_activation_min;
80 pool_params.activation.max = params.quantized_activation_max;
81
82 cmsis_nn_dims filter_dims;
83 filter_dims.n = 1;
84 filter_dims.h = params.filter_height;
85 filter_dims.w = params.filter_width;
86 filter_dims.c = 1;
87
88 cmsis_nn_context ctx;
89 ctx.buf = scratchpad_data;
90 ctx.size = scratchpad_shape.Dims(0);
91 auto res = arm_avgpool_s8(&ctx, &pool_params, &input_dims, input_data, &filter_dims, &output_dims,
92 output_data);
93 assert(res == ARM_MATH_SUCCESS);
94}
95
96static inline void SetupScratchpadTensor(luci_interpreter::Tensor *scratchpad,
97 const luci_interpreter::DataType &input_data_type,
98 const tflite::RuntimeShape &input_shape,
99 const tflite::RuntimeShape &output_shape)
100
101{
102 if (input_data_type == luci_interpreter::DataType::S8)
103 {
104 assert(input_shape.DimensionsCount() == 4);
105 assert(output_shape.DimensionsCount() == 4);
106
107 const int32_t output_width = output_shape.Dims(2);
108 const int32_t depth = tflite::MatchingDim(input_shape, 3, output_shape, 3);
109
110 const int32_t buf_size = arm_avgpool_s8_get_buffer_size(output_width, depth);
111 auto data_type_size = static_cast<int32_t>(luci_interpreter::getDataTypeSize(input_data_type));
112
113 luci_interpreter::Shape scratchpad_shape{buf_size * data_type_size};
114 scratchpad->resize(scratchpad_shape);
115 }
116 else
117 {
118 scratchpad->set_allocatable(false);
119 }
120}
121
122} // namespace luci_interpreter_pal
123
124#endif // LUCI_INTERPRETER_PAL_AVERAGEPOOL2D_H
void set_allocatable(bool value)
Definition Tensor.h:168
void resize(const Shape &new_shape)
Definition Tensor.cpp:56
const luci_interpreter::RuntimeShape output_shape
void AveragePool< int8_t >(const tflite::PoolParams &params, const tflite::RuntimeShape &input_shape, const int8_t *input_data, const tflite::RuntimeShape &output_shape, int8_t *output_data, const tflite::RuntimeShape &scratchpad_shape, int8_t *scratchpad_data)
size_t getDataTypeSize(DataType data_type)
Definition DataType.h:33
DataType
"scalar" value type
Definition DataType.h:32
void AveragePool(const PoolParams &, const Shape &, const T *, const Shape &, T *)
Definition AveragePool.h:36