ONE - On-device Neural Engine
Loading...
Searching...
No Matches
PALQuantize.h
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#ifndef ONERT_MICRO_EXECUTE_PAL_QUANTIZE_COMMON_H
18#define ONERT_MICRO_EXECUTE_PAL_QUANTIZE_COMMON_H
19
20#include "core/OMRuntimeShape.h"
21#include "OMStatus.h"
22#include "core/OMKernelData.h"
23#include "PALUtils.h"
24
25#include <cmath>
26
27namespace onert_micro
28{
29namespace execute
30{
31namespace pal
32{
33
34template <typename InputT, typename OutputT>
35OMStatus Quantize(const core::QuantizationParams op_params, const uint32_t flat_size,
36 const InputT *input_data, OutputT *output_data)
37{
38 const int32_t zero_point = op_params.zero_point;
39 const double scale = op_params.scale;
40 static constexpr int32_t min_val = std::numeric_limits<OutputT>::min();
41 static constexpr int32_t max_val = std::numeric_limits<OutputT>::max();
42
43 for (int i = 0; i < flat_size; i++)
44 {
45 const InputT val = input_data[i];
46 int32_t unclamped =
47 static_cast<int32_t>(std::round(val / static_cast<float>(scale))) + zero_point;
48 int32_t clamped = std::min(std::max(unclamped, min_val), max_val);
49 output_data[i] = clamped;
50 }
51
52 return Ok;
53}
54} // namespace pal
55} // namespace execute
56} // namespace onert_micro
57
58#endif // ONERT_MICRO_EXECUTE_PAL_DEQUANTIZE_COMMON_H
OMStatus Quantize(const core::QuantizationParams op_params, const uint32_t flat_size, const InputT *input_data, OutputT *output_data)
Definition PALQuantize.h:35