ONE - On-device Neural Engine
Loading...
Searching...
No Matches
ConvolutionCommon.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
18#include "execute/OMUtils.h"
19
20using namespace onert_micro;
21using namespace onert_micro::core;
22
24 const circle::Tensor *input,
25 const circle::Tensor *filter,
26 const circle::Tensor *output,
27 circle::ActivationFunctionType act_type)
28{
29 assert(input->quantization() != nullptr); // Fix caller
30 assert(filter->quantization() != nullptr); // Fix caller
31 assert(output->quantization() != nullptr); // Fix caller
32
33 const auto *input_scales = input->quantization()->scale();
34 const auto *filter_scales = filter->quantization()->scale();
35 const auto *output_scales = output->quantization()->scale();
36
37 assert(input_scales != nullptr); // Fix caller
38 assert(filter_scales != nullptr); // Fix caller
39 assert(output_scales != nullptr); // Fix caller
40
41 assert(input_scales->size() != 0); // Fix caller
42 assert(filter_scales->size() != 0); // Fix caller
43 assert(output_scales->size() != 0); // Fix caller
44
45 const auto input_zero_points = input->quantization()->zero_point();
46 const auto filter_zero_points = filter->quantization()->zero_point();
47 const auto output_zero_points = output->quantization()->zero_point();
48
49 assert(input_zero_points != nullptr); // Fix caller
50 assert(filter_zero_points != nullptr); // Fix caller
51 assert(output_zero_points != nullptr); // Fix caller
52
53 assert(input_zero_points->size() != 0); // Fix caller
54 assert(filter_zero_points->size() != 0); // Fix caller
55 assert(output_zero_points->size() != 0); // Fix caller
56
57 const auto input_zp = input_zero_points->operator[](0);
58 const auto filter_zp = filter_zero_points->operator[](0);
59 const auto output_zp = output_zero_points->operator[](0);
60
61 const auto output_scale = output_scales->operator[](0);
62
63 int32_t activation_min{};
64 int32_t activation_max{};
66 act_type, static_cast<int32_t>(output_zp), output_scale, output->type(), &activation_min,
67 &activation_max);
68 assert(status == Ok);
69 if (status != Ok)
70 return status;
71
72 // The kernel expects input and filter zero points to be negated.
73 params.input_offset = -static_cast<int32_t>(input_zp); // Note the '-'.
74 params.weights_offset = -static_cast<int32_t>(filter_zp); // Note the '-'.
75 params.output_offset = static_cast<int32_t>(output_zp);
76 params.quantized_activation_min = activation_min;
77 params.quantized_activation_max = activation_max;
78
79 assert(filter_scales->size() > 1); // Support only channel-wise quantization
80 // Channel-wise quantization
81 const auto input_scale = input_scales->operator[](0);
82 const std::vector<double> effective_output_scale =
83 execute::getQuantizedConvolutionMultiplers(input_scale, filter_scales, output_scale);
84
85 size_t n = effective_output_scale.size();
86 params.per_channel_output_shift.resize(n);
87 params.per_channel_output_multiplier.resize(n);
88 for (size_t i = 0; i < n; ++i)
89 {
90 execute::quantizeMultiplier(effective_output_scale[i], &params.per_channel_output_multiplier[i],
91 &params.per_channel_output_shift[i]);
92 }
93
94 return Ok;
95}
void quantizeMultiplier(double double_multiplier, int32_t *quantized_multiplier, int *shift)
Definition OMUtils.cpp:23
std::vector< double > getQuantizedConvolutionMultiplers(float input_scale, const flatbuffers::Vector< float > *filter_scale, float output_scale)
Definition OMUtils.h:95
OMStatus calculateActivationRangeQuantized(circle::ActivationFunctionType activation, int32_t output_zero_point, float output_scale, circle::TensorType data_type, int32_t *activation_min, int32_t *activation_max)
Definition OMUtils.cpp:112
OMStatus createConvParams(core::ConvQuant &params, const circle::Tensor *input, const circle::Tensor *filter, const circle::Tensor *output, circle::ActivationFunctionType act_type)
std::vector< int > per_channel_output_shift
std::vector< int32_t > per_channel_output_multiplier