/src/Simd/src/Simd/SimdSynetPermute.h
Line | Count | Source |
1 | | /* |
2 | | * Simd Library (http://ermig1979.github.io/Simd). |
3 | | * |
4 | | * Copyright (c) 2011-2026 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 __SimdSynetPermute_h__ |
25 | | #define __SimdSynetPermute_h__ |
26 | | |
27 | | #include "Simd/SimdMath.h" |
28 | | #include "Simd/SimdMemory.h" |
29 | | #include "Simd/SimdArray.h" |
30 | | |
31 | | #include <vector> |
32 | | |
33 | | namespace Simd |
34 | | { |
35 | | namespace Base |
36 | | { |
37 | | typedef std::vector<size_t> Shape; |
38 | | |
39 | | struct PermuteParam |
40 | | { |
41 | | static const size_t CountMax = 5; |
42 | | Shape shape, order; |
43 | | size_t count, align; |
44 | | SimdTensorDataType type; |
45 | | |
46 | | SIMD_INLINE PermuteParam(const size_t* s, const size_t* o, size_t c, SimdTensorDataType t, size_t a) |
47 | 0 | : shape(s, s + Simd::Min(c, CountMax)) |
48 | 0 | , order(o, o + Simd::Min(c, CountMax)) |
49 | 0 | , count(c) |
50 | 0 | , type(t) |
51 | 0 | , align(a) |
52 | 0 | { |
53 | 0 | } |
54 | | |
55 | | SIMD_INLINE bool Valid() const |
56 | 0 | { |
57 | 0 | if (count < 2 || count > CountMax) |
58 | 0 | return false; |
59 | 0 | int permute = 0; |
60 | 0 | for (size_t i = 0; i < count; ++i) |
61 | 0 | { |
62 | 0 | if (order[i] >= count || shape[i] == 0) |
63 | 0 | return false; |
64 | 0 | if (order[i] != i && shape[order[i]] != 1) |
65 | 0 | permute++; |
66 | 0 | } |
67 | 0 | return permute > 1; |
68 | 0 | } |
69 | | |
70 | | SIMD_INLINE size_t PixelSize() const |
71 | 0 | { |
72 | 0 | switch (type) |
73 | 0 | { |
74 | 0 | case SimdTensorDataUnknown: return 0; |
75 | 0 | case SimdTensorData32f: return 4; |
76 | 0 | case SimdTensorData32i: return 4; |
77 | 0 | case SimdTensorData8i: return 1; |
78 | 0 | case SimdTensorData8u: return 1; |
79 | 0 | case SimdTensorData16b: return 2; |
80 | 0 | case SimdTensorData16f: return 2; |
81 | 0 | default: assert(0); return 0; |
82 | 0 | } |
83 | 0 | } |
84 | | }; |
85 | | |
86 | | //------------------------------------------------------------------------------------------------- |
87 | | |
88 | | class SynetPermute : public Deletable |
89 | | { |
90 | | public: |
91 | | SynetPermute(const PermuteParam & param); |
92 | | |
93 | | size_t InternalBufferSize() const |
94 | 0 | { |
95 | 0 | return _index.RawSize(); |
96 | 0 | } |
97 | | |
98 | | void Forward(const uint8_t* src, uint8_t* dst); |
99 | | |
100 | | typedef void (*PermutePtr)(const uint8_t* src, const Shape& shape, const Shape& stride, uint8_t* dst); |
101 | | |
102 | | protected: |
103 | | |
104 | | PermuteParam _param; |
105 | | Array32i _index; |
106 | | size_t _batch, _stride, _count; |
107 | | Shape _srcShape, _srcOrder, _srcStride; |
108 | | Shape _dstShape, _dstOrder, _dstStride; |
109 | | PermutePtr _permute; |
110 | | }; |
111 | | |
112 | | //------------------------------------------------------------------------------------------------- |
113 | | |
114 | | void * SynetPermuteInit(const size_t* shape, const size_t* order, size_t count, SimdTensorDataType type); |
115 | | } |
116 | | |
117 | | #ifdef SIMD_SSE41_ENABLE |
118 | | namespace Sse41 |
119 | | { |
120 | | class SynetPermute : public Base::SynetPermute |
121 | | { |
122 | | public: |
123 | | SynetPermute(const Base::PermuteParam& param); |
124 | | }; |
125 | | |
126 | | //------------------------------------------------------------------------------------------------- |
127 | | |
128 | | void* SynetPermuteInit(const size_t* shape, const size_t* order, size_t count, SimdTensorDataType type); |
129 | | } |
130 | | #endif |
131 | | |
132 | | #ifdef SIMD_AVX2_ENABLE |
133 | | namespace Avx2 |
134 | | { |
135 | | class SynetPermute : public Sse41::SynetPermute |
136 | | { |
137 | | public: |
138 | | SynetPermute(const Base::PermuteParam& param); |
139 | | }; |
140 | | |
141 | | //------------------------------------------------------------------------------------------------- |
142 | | |
143 | | void* SynetPermuteInit(const size_t* shape, const size_t* order, size_t count, SimdTensorDataType type); |
144 | | } |
145 | | #endif |
146 | | |
147 | | #ifdef SIMD_AVX512BW_ENABLE |
148 | | namespace Avx512bw |
149 | | { |
150 | | class SynetPermute : public Avx2::SynetPermute |
151 | | { |
152 | | public: |
153 | | SynetPermute(const Base::PermuteParam& param); |
154 | | }; |
155 | | |
156 | | //------------------------------------------------------------------------------------------------- |
157 | | |
158 | | void* SynetPermuteInit(const size_t* shape, const size_t* order, size_t count, SimdTensorDataType type); |
159 | | } |
160 | | #endif |
161 | | |
162 | | #ifdef SIMD_NEON_ENABLE |
163 | | namespace Neon |
164 | | { |
165 | | class SynetPermute : public Base::SynetPermute |
166 | | { |
167 | | public: |
168 | | SynetPermute(const Base::PermuteParam& param); |
169 | | }; |
170 | | |
171 | | //------------------------------------------------------------------------------------------------- |
172 | | |
173 | | void* SynetPermuteInit(const size_t* shape, const size_t* order, size_t count, SimdTensorDataType type); |
174 | | } |
175 | | #endif |
176 | | |
177 | | #ifdef SIMD_SVE2_ENABLE |
178 | | namespace Sve2 |
179 | | { |
180 | | class SynetPermute : |
181 | | #ifdef SIMD_NEON_ENABLE |
182 | | public Neon::SynetPermute |
183 | | #else |
184 | | public Base::SynetPermute |
185 | | #endif |
186 | | { |
187 | | public: |
188 | | SynetPermute(const Base::PermuteParam& param); |
189 | | }; |
190 | | |
191 | | //------------------------------------------------------------------------------------------------- |
192 | | |
193 | | void* SynetPermuteInit(const size_t* shape, const size_t* order, size_t count, SimdTensorDataType type); |
194 | | } |
195 | | #endif |
196 | | } |
197 | | |
198 | | #endif |