1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "quant_param.h"
17 
18 #include "common/log.h"
19 
20 namespace OHOS {
21 namespace NeuralNetworkRuntime {
SetScales(const std::vector<double> & scales)22 void QuantParams::SetScales(const std::vector<double>& scales)
23 {
24     m_scales = scales;
25 }
26 
SetZeroPoints(const std::vector<int32_t> & zeroPoints)27 void QuantParams::SetZeroPoints(const std::vector<int32_t>& zeroPoints)
28 {
29     m_zeroPoints = zeroPoints;
30 }
31 
SetNumBits(const std::vector<uint32_t> & numBits)32 void QuantParams::SetNumBits(const std::vector<uint32_t>& numBits)
33 {
34     m_numBits = numBits;
35 }
36 
GetScales() const37 std::vector<double> QuantParams::GetScales() const
38 {
39     return m_scales;
40 }
41 
GetZeroPoints() const42 std::vector<int32_t> QuantParams::GetZeroPoints() const
43 {
44     return m_zeroPoints;
45 }
46 
GetNumBits() const47 std::vector<uint32_t> QuantParams::GetNumBits() const
48 {
49     return m_numBits;
50 }
51 
CopyToCompat(std::vector<OHOS::NeuralNetworkRuntime::QuantParam> & compatQuantParams) const52 OH_NN_ReturnCode QuantParams::CopyToCompat(std::vector<OHOS::NeuralNetworkRuntime::QuantParam>& compatQuantParams) const
53 {
54     if ((m_scales.size() != m_zeroPoints.size()) || (m_zeroPoints.size() != m_numBits.size())) {
55         LOGE("CopyToCompat failed, the size of scales(%zu), zeroPoints(%zu) and numBits(%zu) are not equal.",
56             m_scales.size(), m_zeroPoints.size(), m_numBits.size());
57         return OH_NN_INVALID_PARAMETER;
58     }
59 
60     size_t quantCount = m_scales.size();
61     for (size_t i = 0; i < quantCount; i++) {
62         compatQuantParams.push_back({
63             .numBits = m_numBits[i],
64             .scale = m_scales[i],
65             .zeroPoint = m_zeroPoints[i],
66         });
67     }
68 
69     return OH_NN_SUCCESS;
70 }
71 } // namespace NeuralNetworkRuntime
72 } // namespace OHOS