/src/Simd/src/Simd/SimdSynetQuantizedAdd.h
Line | Count | Source |
1 | | /* |
2 | | * Simd Library (http://ermig1979.github.io/Simd). |
3 | | * |
4 | | * Copyright (c) 2011-2025 Yermalayeu Ihar. |
5 | | * |
6 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | | * of this software and associated documentation files (the "Software"), to deal |
8 | | * in the Software without restriction, including without limitation the rights |
9 | | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
10 | | * copies of the Software, and to permit persons to whom the Software is |
11 | | * furnished to do so, subject to the following conditions: |
12 | | * |
13 | | * The above copyright notice and this permission notice shall be included in |
14 | | * all copies or substantial portions of the Software. |
15 | | * |
16 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
21 | | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
22 | | * SOFTWARE. |
23 | | */ |
24 | | #ifndef __SimdSynetQuantizedAdd_h__ |
25 | | #define __SimdSynetQuantizedAdd_h__ |
26 | | |
27 | | #include "Simd/SimdArray.h" |
28 | | #include "Simd/SimdPerformance.h" |
29 | | |
30 | | #include <vector> |
31 | | |
32 | | namespace Simd |
33 | | { |
34 | | typedef std::vector<size_t> Shape; |
35 | | |
36 | | struct QuantizedAddParam |
37 | | { |
38 | | Shape aShape, bShape; |
39 | | SimdTensorDataType aType, bType, dType; |
40 | | float aScale, bScale, dScale, actParams[2]; |
41 | | size_t aZero, bZero, dZero; |
42 | | SimdConvolutionActivationType actType; |
43 | | |
44 | | QuantizedAddParam( |
45 | | const size_t* as, size_t ac, SimdTensorDataType at, const float* aSc, int32_t aZr, |
46 | | const size_t* bs, size_t bc, SimdTensorDataType bt, const float* bSc, int32_t bZr, |
47 | | SimdConvolutionActivationType act, const float *ap, SimdTensorDataType dt, const float* dSc, int32_t dZr) |
48 | 0 | : aShape(as, as + ac) |
49 | 0 | , aType(at) |
50 | 0 | , aScale(aSc ? *aSc : 1.0f) |
51 | 0 | , aZero(aZr) |
52 | 0 | , bShape(bs, bs + bc) |
53 | 0 | , bType(bt) |
54 | 0 | , bScale(bSc ? *bSc : 1.0f) |
55 | 0 | , bZero(bZr) |
56 | 0 | , actType(act) |
57 | 0 | , dType(dt) |
58 | 0 | , dScale(dSc ? *dSc : 1.0f) |
59 | 0 | , dZero(dZr) |
60 | 0 | { |
61 | 0 | actParams[0] = ap ? ap[0] : 0.0f; |
62 | 0 | actParams[1] = ap ? ap[1] : 0.0f; |
63 | 0 | } |
64 | | |
65 | | bool Valid() |
66 | 0 | { |
67 | 0 | return |
68 | 0 | (aType == SimdTensorData32f || aType == SimdTensorData8u) && |
69 | 0 | (bType == SimdTensorData32f || bType == SimdTensorData8u) && |
70 | 0 | (dType == SimdTensorData32f || dType == SimdTensorData8u); |
71 | 0 | } |
72 | | }; |
73 | | |
74 | | class SynetQuantizedAdd : public Deletable |
75 | | { |
76 | | public: |
77 | | SynetQuantizedAdd(const QuantizedAddParam& p); |
78 | | |
79 | | virtual void Forward(const uint8_t* a, const uint8_t* b, uint8_t* dst) = 0; |
80 | | |
81 | | protected: |
82 | | QuantizedAddParam _param; |
83 | | }; |
84 | | |
85 | | //------------------------------------------------------------------------------------------------ |
86 | | |
87 | | namespace Base |
88 | | { |
89 | | //------------------------------------------------------------------------------------------------ |
90 | | |
91 | | class SynetQuantizedAddUniform : public SynetQuantizedAdd |
92 | | { |
93 | | public: |
94 | | SynetQuantizedAddUniform(const QuantizedAddParam& p); |
95 | | |
96 | | static bool Preferable(const QuantizedAddParam& p); |
97 | | |
98 | | virtual void Forward(const uint8_t* a, const uint8_t* b, uint8_t* dst); |
99 | | |
100 | | typedef void(*UniformPtr)(const uint8_t* a8, float aScale, int aZero, const uint8_t* b8, float bScale, int bZero, size_t size, const float* params, float dScale, int dZero, uint8_t* dst8); |
101 | | |
102 | | protected: |
103 | | size_t _size; |
104 | | UniformPtr _uniform; |
105 | | }; |
106 | | |
107 | | //------------------------------------------------------------------------------------------------ |
108 | | |
109 | | void* SynetQuantizedAddInit(const size_t* aShape, size_t aCount, SimdTensorDataType aType, const float* aScale, int32_t aZero, |
110 | | const size_t* bShape, size_t bCount, SimdTensorDataType bType, const float* bScale, int32_t bZero, |
111 | | SimdConvolutionActivationType actType, const float* actParams, SimdTensorDataType dstType, const float* dstScale, int32_t dstZero); |
112 | | } |
113 | | |
114 | | #ifdef SIMD_SSE41_ENABLE |
115 | | namespace Sse41 |
116 | | { |
117 | | class SynetQuantizedAddUniform : public Base::SynetQuantizedAddUniform |
118 | | { |
119 | | public: |
120 | | SynetQuantizedAddUniform(const QuantizedAddParam& p); |
121 | | }; |
122 | | |
123 | | //------------------------------------------------------------------------------------------------ |
124 | | |
125 | | void* SynetQuantizedAddInit(const size_t* aShape, size_t aCount, SimdTensorDataType aType, const float* aScale, int32_t aZero, |
126 | | const size_t* bShape, size_t bCount, SimdTensorDataType bType, const float* bScale, int32_t bZero, |
127 | | SimdConvolutionActivationType actType, const float* actParams, SimdTensorDataType dstType, const float* dstScale, int32_t dstZero); |
128 | | } |
129 | | #endif |
130 | | |
131 | | #ifdef SIMD_AVX2_ENABLE |
132 | | namespace Avx2 |
133 | | { |
134 | | class SynetQuantizedAddUniform : public Sse41::SynetQuantizedAddUniform |
135 | | { |
136 | | public: |
137 | | SynetQuantizedAddUniform(const QuantizedAddParam& p); |
138 | | }; |
139 | | |
140 | | //------------------------------------------------------------------------------------------------ |
141 | | |
142 | | void* SynetQuantizedAddInit(const size_t* aShape, size_t aCount, SimdTensorDataType aType, const float* aScale, int32_t aZero, |
143 | | const size_t* bShape, size_t bCount, SimdTensorDataType bType, const float* bScale, int32_t bZero, |
144 | | SimdConvolutionActivationType actType, const float* actParams, SimdTensorDataType dstType, const float* dstScale, int32_t dstZero); |
145 | | } |
146 | | #endif |
147 | | |
148 | | #ifdef SIMD_AVX512BW_ENABLE |
149 | | namespace Avx512bw |
150 | | { |
151 | | class SynetQuantizedAddUniform : public Avx2::SynetQuantizedAddUniform |
152 | | { |
153 | | public: |
154 | | SynetQuantizedAddUniform(const QuantizedAddParam& p); |
155 | | }; |
156 | | |
157 | | //------------------------------------------------------------------------------------------------ |
158 | | |
159 | | void* SynetQuantizedAddInit(const size_t* aShape, size_t aCount, SimdTensorDataType aType, const float* aScale, int32_t aZero, |
160 | | const size_t* bShape, size_t bCount, SimdTensorDataType bType, const float* bScale, int32_t bZero, |
161 | | SimdConvolutionActivationType actType, const float* actParams, SimdTensorDataType dstType, const float* dstScale, int32_t dstZero); |
162 | | } |
163 | | #endif |
164 | | } |
165 | | |
166 | | #endif |