Elffuss BraidHub

kikocis/ncnn public

gemm int8 quantization (#5706)

* quantize gemm

* write gemm quantize scales

* update doc

* less openmp args

* x86 riscv fallback

* skip gemm vulkan int8

* fix noint8 test, fix arm bf16 test

* enable vfpv4 on neon build only

* fix gemm vulkan without C

* fp16 pack8 output

* enable elempack=8 only for asimdhp+

* tiled gemm int8 test

* opt arm64 tiles, fix asimdhp dispatch
Changesetd7a5f98c0f60adc2c73cea73a6d7d2dd1de57f2a4124736c379e64588697bfb2
Authornihui <nihuini@tencent.com> · 2024-10-12 19:02 +08:00
CommitterGitHub <noreply@github.com> · 2024-10-12 19:02 +08:00
Parentsc796fe082fe6

Browse the files at this changeset

24 files changed

modified CMakeLists.txt +18 −14
@@ -162,21 +162,25 @@
162162 endif()
163163
164164 if(CMAKE_SIZEOF_VOID_P EQUAL 4 AND NOT NCNN_TARGET_ILP32)
165- if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC" OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_SIMULATE_ID MATCHES "MSVC" AND CMAKE_CXX_COMPILER_FRONTEND_VARIANT MATCHES "MSVC"))
166- set(CMAKE_REQUIRED_FLAGS "/arch:VFPv4")
167- check_cxx_source_compiles("#include <arm_neon.h>\nint main() { float32x4_t _a; float16x4_t _s = vcvt_f16_f32(_a); return 0; }" NCNN_COMPILER_SUPPORT_ARM_VFPV4)
168-
169- unset(CMAKE_REQUIRED_FLAGS)
170- else()
171- set(CMAKE_REQUIRED_FLAGS "-mfpu=neon-vfpv4")
172- check_cxx_source_compiles("#include <arm_neon.h>\nint main() { float32x4_t _a; float16x4_t _s = vcvt_f16_f32(_a); return 0; }" NCNN_COMPILER_SUPPORT_ARM_VFPV4)
173-
174- if(NOT NCNN_COMPILER_SUPPORT_ARM_VFPV4)
175- set(CMAKE_REQUIRED_FLAGS "-mfpu=neon-vfpv4 -mfp16-format=ieee")
176- check_cxx_source_compiles("#include <arm_neon.h>\nint main() { float32x4_t _a; float16x4_t _s = vcvt_f16_f32(_a); return 0; }" NCNN_COMPILER_SUPPORT_ARM_VFPV4_FP16)
177- endif()
165+ check_cxx_source_compiles("#include <arm_neon.h>\nint main() { float32x4_t _s, _a, _b; _s = vmlaq_f32(_s, _a, _b); return 0; }" NCNN_COMPILER_SUPPORT_ARM_NEON)
166+
167+ if(NCNN_COMPILER_SUPPORT_ARM_NEON)
168+ if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC" OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_SIMULATE_ID MATCHES "MSVC" AND CMAKE_CXX_COMPILER_FRONTEND_VARIANT MATCHES "MSVC"))
169+ set(CMAKE_REQUIRED_FLAGS "/arch:VFPv4")
170+ check_cxx_source_compiles("#include <arm_neon.h>\nint main() { float32x4_t _a; float16x4_t _s = vcvt_f16_f32(_a); return 0; }" NCNN_COMPILER_SUPPORT_ARM_VFPV4)
178171
179- unset(CMAKE_REQUIRED_FLAGS)
172+ unset(CMAKE_REQUIRED_FLAGS)
173+ else()
174+ set(CMAKE_REQUIRED_FLAGS "-mfpu=neon-vfpv4")
175+ check_cxx_source_compiles("#include <arm_neon.h>\nint main() { float32x4_t _a; float16x4_t _s = vcvt_f16_f32(_a); return 0; }" NCNN_COMPILER_SUPPORT_ARM_VFPV4)
176+
177+ if(NOT NCNN_COMPILER_SUPPORT_ARM_VFPV4)
178+ set(CMAKE_REQUIRED_FLAGS "-mfpu=neon-vfpv4 -mfp16-format=ieee")
179+ check_cxx_source_compiles("#include <arm_neon.h>\nint main() { float32x4_t _a; float16x4_t _s = vcvt_f16_f32(_a); return 0; }" NCNN_COMPILER_SUPPORT_ARM_VFPV4_FP16)
180+ endif()
181+
182+ unset(CMAKE_REQUIRED_FLAGS)
183+ endif()
180184 endif()
181185
182186 if(NCNN_COMPILER_SUPPORT_ARM_VFPV4 OR NCNN_COMPILER_SUPPORT_ARM_VFPV4_FP16)
modified cmake/ncnn_add_layer.cmake +48 −48
@@ -144,25 +144,25 @@
144144 if(NCNN_RUNTIME_CPU AND NCNN_AVX)
145145 ncnn_add_arch_opt_layer(${class} avx "/arch:AVX /D__SSSE3__ /D__SSE4_1__")
146146 endif()
147- if(NCNN_AVX512VNNI)
147+ if(NCNN_RUNTIME_CPU AND NCNN_AVX512VNNI)
148148 ncnn_add_arch_opt_source(${class} avx512vnni "/arch:AVX512 /D__SSSE3__ /D__SSE4_1__ /D__FMA__ /D__F16C__ /D__AVX512VNNI__")
149149 endif()
150- if(NCNN_AVX512BF16)
150+ if(NCNN_RUNTIME_CPU AND NCNN_AVX512BF16)
151151 ncnn_add_arch_opt_source(${class} avx512bf16 "/arch:AVX512 /D__SSSE3__ /D__SSE4_1__ /D__FMA__ /D__F16C__ /D__AVX512BF16__")
152152 endif()
153- if(NCNN_AVX512FP16)
153+ if(NCNN_RUNTIME_CPU AND NCNN_AVX512FP16)
154154 ncnn_add_arch_opt_source(${class} avx512fp16 "/arch:AVX512 /D__SSSE3__ /D__SSE4_1__ /D__FMA__ /D__F16C__ /D__AVX512FP16__")
155155 endif()
156- if(NCNN_AVXVNNI)
156+ if(NCNN_RUNTIME_CPU AND NCNN_AVXVNNI)
157157 ncnn_add_arch_opt_source(${class} avxvnni "/arch:AVX2 /D__SSSE3__ /D__SSE4_1__ /D__FMA__ /D__F16C__ /D__AVXVNNI__")
158158 endif()
159- if(NCNN_AVX2)
159+ if(NCNN_RUNTIME_CPU AND NCNN_AVX2)
160160 ncnn_add_arch_opt_source(${class} avx2 "/arch:AVX2 /D__SSSE3__ /D__SSE4_1__ /D__FMA__ /D__F16C__")
161161 endif()
162- if(NCNN_XOP)
162+ if(NCNN_RUNTIME_CPU AND NCNN_XOP)
163163 ncnn_add_arch_opt_source(${class} xop "/arch:AVX /D__SSSE3__ /D__SSE4_1__ /D__XOP__")
164164 endif()
165- if(NCNN_F16C)
165+ if(NCNN_RUNTIME_CPU AND NCNN_F16C)
166166 ncnn_add_arch_opt_source(${class} f16c "/arch:AVX /D__SSSE3__ /D__SSE4_1__ /D__F16C__")
167167 endif()
168168 elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_SIMULATE_ID MATCHES "MSVC" AND CMAKE_CXX_COMPILER_FRONTEND_VARIANT MATCHES "MSVC")
@@ -175,25 +175,25 @@
175175 if(NCNN_RUNTIME_CPU AND NCNN_AVX)
176176 ncnn_add_arch_opt_layer(${class} avx "/arch:AVX /D__SSSE3__ /D__SSE4_1__")
177177 endif()
178- if(NCNN_AVX512VNNI)
178+ if(NCNN_RUNTIME_CPU AND NCNN_AVX512VNNI)
179179 ncnn_add_arch_opt_source(${class} avx512vnni "/arch:AVX512 -mavx512cd -mavx512bw -mavx512dq -mavx512vl -mfma -mf16c -mavx512vnni /D__SSSE3__ /D__SSE4_1__ /D__FMA__ /D__F16C__ /D__AVX512VNNI__")
180180 endif()
181- if(NCNN_AVX512BF16)
181+ if(NCNN_RUNTIME_CPU AND NCNN_AVX512BF16)
182182 ncnn_add_arch_opt_source(${class} avx512bf16 "/arch:AVX512 -mavx512cd -mavx512bw -mavx512dq -mavx512vl -mfma -mf16c -mavx512bf16 /D__SSSE3__ /D__SSE4_1__ /D__FMA__ /D__F16C__ /D__AVX512BF16__")
183183 endif()
184- if(NCNN_AVX512FP16)
184+ if(NCNN_RUNTIME_CPU AND NCNN_AVX512FP16)
185185 ncnn_add_arch_opt_source(${class} avx512fp16 "/arch:AVX512 -mavx512cd -mavx512bw -mavx512dq -mavx512vl -mfma -mf16c -mavx512fp16 /D__SSSE3__ /D__SSE4_1__ /D__FMA__ /D__F16C__ /D__AVX512FP16__")
186186 endif()
187- if(NCNN_AVXVNNI)
187+ if(NCNN_RUNTIME_CPU AND NCNN_AVXVNNI)
188188 ncnn_add_arch_opt_source(${class} avxvnni "/arch:AVX2 -mfma -mf16c -mavxvnni /D__SSSE3__ /D__SSE4_1__ /D__FMA__ /D__F16C__ /D__AVXVNNI__")
189189 endif()
190- if(NCNN_AVX2)
190+ if(NCNN_RUNTIME_CPU AND NCNN_AVX2)
191191 ncnn_add_arch_opt_source(${class} avx2 "/arch:AVX2 -mfma -mf16c /D__SSSE3__ /D__SSE4_1__ /D__FMA__ /D__F16C__")
192192 endif()
193- if(NCNN_XOP)
193+ if(NCNN_RUNTIME_CPU AND NCNN_XOP)
194194 ncnn_add_arch_opt_source(${class} xop "/arch:AVX -mxop /D__SSSE3__ /D__SSE4_1__ /D__XOP__")
195195 endif()
196- if(NCNN_F16C)
196+ if(NCNN_RUNTIME_CPU AND NCNN_F16C)
197197 ncnn_add_arch_opt_source(${class} f16c "/arch:AVX -mf16c /D__SSSE3__ /D__SSE4_1__ /D__F16C__")
198198 endif()
199199 else()
@@ -206,25 +206,25 @@
206206 if(NCNN_RUNTIME_CPU AND NCNN_AVX)
207207 ncnn_add_arch_opt_layer(${class} avx "-mavx")
208208 endif()
209- if(NCNN_AVX512VNNI)
209+ if(NCNN_RUNTIME_CPU AND NCNN_AVX512VNNI)
210210 ncnn_add_arch_opt_source(${class} avx512vnni "-mavx512f -mavx512cd -mavx512bw -mavx512dq -mavx512vl -mfma -mf16c -mavx512vnni")
211211 endif()
212- if(NCNN_AVX512BF16)
212+ if(NCNN_RUNTIME_CPU AND NCNN_AVX512BF16)
213213 ncnn_add_arch_opt_source(${class} avx512bf16 "-mavx512f -mavx512cd -mavx512bw -mavx512dq -mavx512vl -mfma -mf16c -mavx512bf16")
214214 endif()
215- if(NCNN_AVX512FP16)
215+ if(NCNN_RUNTIME_CPU AND NCNN_AVX512FP16)
216216 ncnn_add_arch_opt_source(${class} avx512fp16 "-mavx512f -mavx512cd -mavx512bw -mavx512dq -mavx512vl -mfma -mf16c -mavx512fp16")
217217 endif()
218- if(NCNN_AVXVNNI)
218+ if(NCNN_RUNTIME_CPU AND NCNN_AVXVNNI)
219219 ncnn_add_arch_opt_source(${class} avxvnni "-mavx2 -mfma -mf16c -mavxvnni")
220220 endif()
221- if(NCNN_AVX2)
221+ if(NCNN_RUNTIME_CPU AND NCNN_AVX2)
222222 ncnn_add_arch_opt_source(${class} avx2 "-mavx2 -mfma -mf16c")
223223 endif()
224- if(NCNN_XOP)
224+ if(NCNN_RUNTIME_CPU AND NCNN_XOP)
225225 ncnn_add_arch_opt_source(${class} xop "-mavx -mxop")
226226 endif()
227- if(NCNN_F16C)
227+ if(NCNN_RUNTIME_CPU AND NCNN_F16C)
228228 ncnn_add_arch_opt_source(${class} f16c "-mavx -mf16c")
229229 endif()
230230 endif()
@@ -254,28 +254,28 @@
254254 if(NCNN_ARM82)
255255 ncnn_add_arch_opt_source(${class} asimdhp "/arch:armv8.2 /D__ARM_FEATURE_FP16_VECTOR_ARITHMETIC")
256256 endif()
257- if(NCNN_ARM82DOT)
257+ if(NCNN_RUNTIME_CPU AND NCNN_ARM82DOT)
258258 ncnn_add_arch_opt_source(${class} asimddp "/arch:armv8.2 /D__ARM_FEATURE_FP16_VECTOR_ARITHMETIC /D__ARM_FEATURE_DOTPROD")
259259 endif()
260- if(NCNN_ARM82FP16FML)
260+ if(NCNN_RUNTIME_CPU AND NCNN_ARM82FP16FML)
261261 ncnn_add_arch_opt_source(${class} asimdfhm "/arch:armv8.2 /D__ARM_FEATURE_FP16_VECTOR_ARITHMETIC /D__ARM_FEATURE_FP16_FML")
262262 endif()
263- if(NCNN_ARM84BF16)
263+ if(NCNN_RUNTIME_CPU AND NCNN_ARM84BF16)
264264 ncnn_add_arch_opt_source(${class} bf16 "/arch:armv8.4 /D__ARM_FEATURE_FP16_VECTOR_ARITHMETIC /D__ARM_FEATURE_DOTPROD /D__ARM_FEATURE_FP16_FML /D__ARM_FEATURE_BF16_VECTOR_ARITHMETIC")
265265 endif()
266- if(NCNN_ARM84I8MM)
266+ if(NCNN_RUNTIME_CPU AND NCNN_ARM84I8MM)
267267 ncnn_add_arch_opt_source(${class} i8mm "/arch:armv8.4 /D__ARM_FEATURE_FP16_VECTOR_ARITHMETIC /D__ARM_FEATURE_DOTPROD /D__ARM_FEATURE_FP16_FML /D__ARM_FEATURE_MATMUL_INT8")
268268 endif()
269269 # TODO add support for sve family
270- if(NCNN_ARM86SVE)
270+ if(NCNN_RUNTIME_CPU AND NCNN_ARM86SVE)
271271 endif()
272- if(NCNN_ARM86SVE2)
272+ if(NCNN_RUNTIME_CPU AND NCNN_ARM86SVE2)
273273 endif()
274- if(NCNN_ARM86SVEBF16)
274+ if(NCNN_RUNTIME_CPU AND NCNN_ARM86SVEBF16)
275275 endif()
276- if(NCNN_ARM86SVEI8MM)
276+ if(NCNN_RUNTIME_CPU AND NCNN_ARM86SVEI8MM)
277277 endif()
278- if(NCNN_ARM86SVEF32MM)
278+ if(NCNN_RUNTIME_CPU AND NCNN_ARM86SVEF32MM)
279279 endif()
280280 elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_SIMULATE_ID MATCHES "MSVC" AND CMAKE_CXX_COMPILER_FRONTEND_VARIANT MATCHES "MSVC")
281281 if(NCNN_VFPV4)
@@ -284,28 +284,28 @@
284284 if(NCNN_ARM82)
285285 ncnn_add_arch_opt_source(${class} asimdhp "/arch:armv8.2 -march=armv8.2-a+fp16 /D__ARM_FEATURE_FP16_VECTOR_ARITHMETIC")
286286 endif()
287- if(NCNN_ARM82DOT)
287+ if(NCNN_RUNTIME_CPU AND NCNN_ARM82DOT)
288288 ncnn_add_arch_opt_source(${class} asimddp "/arch:armv8.2 -march=armv8.2-a+fp16+dotprod /D__ARM_FEATURE_FP16_VECTOR_ARITHMETIC /D__ARM_FEATURE_DOTPROD")
289289 endif()
290- if(NCNN_ARM82FP16FML)
290+ if(NCNN_RUNTIME_CPU AND NCNN_ARM82FP16FML)
291291 ncnn_add_arch_opt_source(${class} asimdfhm "/arch:armv8.2 -march=armv8.2-a+fp16+fp16fml /D__ARM_FEATURE_FP16_VECTOR_ARITHMETIC /D__ARM_FEATURE_FP16_FML")
292292 endif()
293- if(NCNN_ARM84BF16)
293+ if(NCNN_RUNTIME_CPU AND NCNN_ARM84BF16)
294294 ncnn_add_arch_opt_source(${class} bf16 "/arch:armv8.4 -march=armv8.4-a+fp16+dotprod+bf16 /D__ARM_FEATURE_FP16_VECTOR_ARITHMETIC /D__ARM_FEATURE_DOTPROD /D__ARM_FEATURE_FP16_FML /D__ARM_FEATURE_BF16_VECTOR_ARITHMETIC")
295295 endif()
296- if(NCNN_ARM84I8MM)
296+ if(NCNN_RUNTIME_CPU AND NCNN_ARM84I8MM)
297297 ncnn_add_arch_opt_source(${class} i8mm "/arch:armv8.4 -march=armv8.4-a+fp16+dotprod+i8mm /D__ARM_FEATURE_FP16_VECTOR_ARITHMETIC /D__ARM_FEATURE_DOTPROD /D__ARM_FEATURE_FP16_FML /D__ARM_FEATURE_MATMUL_INT8")
298298 endif()
299299 # TODO add support for sve family
300- if(NCNN_ARM86SVE)
300+ if(NCNN_RUNTIME_CPU AND NCNN_ARM86SVE)
301301 endif()
302- if(NCNN_ARM86SVE2)
302+ if(NCNN_RUNTIME_CPU AND NCNN_ARM86SVE2)
303303 endif()
304- if(NCNN_ARM86SVEBF16)
304+ if(NCNN_RUNTIME_CPU AND NCNN_ARM86SVEBF16)
305305 endif()
306- if(NCNN_ARM86SVEI8MM)
306+ if(NCNN_RUNTIME_CPU AND NCNN_ARM86SVEI8MM)
307307 endif()
308- if(NCNN_ARM86SVEF32MM)
308+ if(NCNN_RUNTIME_CPU AND NCNN_ARM86SVEF32MM)
309309 endif()
310310 else()
311311 if(NCNN_VFPV4)
@@ -314,31 +314,31 @@
314314 if(NCNN_ARM82)
315315 ncnn_add_arch_opt_source(${class} asimdhp "-march=armv8.2-a+fp16")
316316 endif()
317- if(NCNN_ARM82DOT)
317+ if(NCNN_RUNTIME_CPU AND NCNN_ARM82DOT)
318318 ncnn_add_arch_opt_source(${class} asimddp "-march=armv8.2-a+fp16+dotprod")
319319 endif()
320- if(NCNN_ARM82FP16FML)
320+ if(NCNN_RUNTIME_CPU AND NCNN_ARM82FP16FML)
321321 ncnn_add_arch_opt_source(${class} asimdfhm "-march=armv8.2-a+fp16+fp16fml")
322322 endif()
323- if(NCNN_ARM84BF16)
323+ if(NCNN_RUNTIME_CPU AND NCNN_ARM84BF16)
324324 ncnn_add_arch_opt_source(${class} bf16 "-march=armv8.4-a+fp16+dotprod+bf16")
325325 endif()
326- if(NCNN_ARM84I8MM)
326+ if(NCNN_RUNTIME_CPU AND NCNN_ARM84I8MM)
327327 ncnn_add_arch_opt_source(${class} i8mm "-march=armv8.4-a+fp16+dotprod+i8mm")
328328 endif()
329- if(NCNN_ARM86SVE)
329+ if(NCNN_RUNTIME_CPU AND NCNN_ARM86SVE)
330330 ncnn_add_arch_opt_source(${class} sve "-march=armv8.6-a+fp16+dotprod+sve")
331331 endif()
332- if(NCNN_ARM86SVE2)
332+ if(NCNN_RUNTIME_CPU AND NCNN_ARM86SVE2)
333333 ncnn_add_arch_opt_source(${class} sve2 "-march=armv8.6-a+fp16+dotprod+sve2")
334334 endif()
335- if(NCNN_ARM86SVEBF16)
335+ if(NCNN_RUNTIME_CPU AND NCNN_ARM86SVEBF16)
336336 ncnn_add_arch_opt_source(${class} svebf16 "-march=armv8.6-a+fp16+dotprod+sve+bf16")
337337 endif()
338- if(NCNN_ARM86SVEI8MM)
338+ if(NCNN_RUNTIME_CPU AND NCNN_ARM86SVEI8MM)
339339 ncnn_add_arch_opt_source(${class} svei8mm "-march=armv8.6-a+fp16+dotprod+sve+i8mm")
340340 endif()
341- if(NCNN_ARM86SVEF32MM)
341+ if(NCNN_RUNTIME_CPU AND NCNN_ARM86SVEF32MM)
342342 ncnn_add_arch_opt_source(${class} svef32mm "-march=armv8.6-a+fp16+dotprod+sve+f32mm")
343343 endif()
344344 endif()
modified docs/developer-guide/operators.md +5 −2
@@ -942,15 +942,18 @@
942942 | 12 | output_elempack | int | 0 | |
943943 | 13 | output_elemtype | int | 0 | |
944944 | 14 | output_transpose | int| 0 | |
945+| 18 | int8_scale_term | int | 0 | |
945946 | 20 | constant_TILE_M | int | 0 | |
946947 | 21 | constant_TILE_N | int | 0 | |
947948 | 22 | constant_TILE_K | int | 0 | |
948949
949950 | weight | type | shape |
950951 | ------------- | ----- | --------------------- |
951-| A_data | float | [M, K] or [K, M] |
952-| B_data | float | [N, K] or [K, N] |
952+| A_data | float/fp16/int8 | [M, K] or [K, M] |
953+| B_data | float/fp16/int8 | [N, K] or [K, N] |
953954 | C_data | float | [1], [M] or [N] or [1, M] or [N,1] or [N, M] |
955+| A_data_int8_scales| float | [M] |
956+| B_data_int8_scales| float | [1] |
954957
955958 # GridSample
956959 ```
modified src/layer/arm/gemm_arm.cpp +1087 −0
@@ -16,6 +16,7 @@
1616
1717 #if __ARM_NEON
1818 #include <arm_neon.h>
19+#include "neon_mathfun.h"
1920 #endif // __ARM_NEON
2021
2122 #include "arm_usability.h"
@@ -29,6 +30,13 @@
2930 #include "gemm_bf16s.h"
3031 #endif
3132
33+#if NCNN_INT8
34+#include "gemm_int8.h"
35+#if NCNN_BF16
36+#include "gemm_int8_bf16s.h"
37+#endif
38+#endif
39+
3240 Gemm_arm::Gemm_arm()
3341 {
3442 #if __ARM_NEON
@@ -2461,6 +2469,79 @@
24612469 int kk = 0;
24622470 for (; kk < max_kk; kk += 1)
24632471 {
2472+#if NCNN_GNU_INLINE_ASM
2473+#if __aarch64__
2474+ asm volatile(
2475+ "prfm pldl1keep, [%0, #128] \n"
2476+ "ld1 {v2.4s}, [%0], #16 \n"
2477+ "prfm pldl1keep, [%1, #256] \n"
2478+ "ld1 {v0.4s, v1.4s}, [%1], #32 \n"
2479+ "fmla %2.4s, v2.4s, v0.s[0] \n"
2480+ "fmla %3.4s, v2.4s, v0.s[1] \n"
2481+ "fmla %4.4s, v2.4s, v0.s[2] \n"
2482+ "fmla %5.4s, v2.4s, v0.s[3] \n"
2483+ "fmla %6.4s, v2.4s, v1.s[0] \n"
2484+ "fmla %7.4s, v2.4s, v1.s[1] \n"
2485+ "fmla %8.4s, v2.4s, v1.s[2] \n"
2486+ "fmla %9.4s, v2.4s, v1.s[3] \n"
2487+ : "=r"(pA),
2488+ "=r"(pB),
2489+ "=w"(_sum0),
2490+ "=w"(_sum1),
2491+ "=w"(_sum2),
2492+ "=w"(_sum3),
2493+ "=w"(_sum4),
2494+ "=w"(_sum5),
2495+ "=w"(_sum6),
2496+ "=w"(_sum7)
2497+ : "0"(pA),
2498+ "1"(pB),
2499+ "2"(_sum0),
2500+ "3"(_sum1),
2501+ "4"(_sum2),
2502+ "5"(_sum3),
2503+ "6"(_sum4),
2504+ "7"(_sum5),
2505+ "8"(_sum6),
2506+ "9"(_sum7)
2507+ : "memory", "v0", "v1", "v2", "v3");
2508+#else
2509+ asm volatile(
2510+ "pld [%0, #128] \n"
2511+ "vld1.f32 {d4-d5}, [%0]! \n"
2512+ "pld [%1, #256] \n"
2513+ "vld1.f32 {d0-d3}, [%1]! \n"
2514+ "vmla.f32 %q2, q2, d0[0] \n"
2515+ "vmla.f32 %q3, q2, d0[1] \n"
2516+ "vmla.f32 %q4, q2, d1[0] \n"
2517+ "vmla.f32 %q5, q2, d1[1] \n"
2518+ "vmla.f32 %q6, q2, d2[0] \n"
2519+ "vmla.f32 %q7, q2, d2[1] \n"
2520+ "vmla.f32 %q8, q2, d3[0] \n"
2521+ "vmla.f32 %q9, q2, d3[1] \n"
2522+ : "=r"(pA),
2523+ "=r"(pB),
2524+ "=w"(_sum0),
2525+ "=w"(_sum1),
2526+ "=w"(_sum2),
2527+ "=w"(_sum3),
2528+ "=w"(_sum4),
2529+ "=w"(_sum5),
2530+ "=w"(_sum6),
2531+ "=w"(_sum7)
2532+ : "0"(pA),
2533+ "1"(pB),
2534+ "2"(_sum0),
2535+ "3"(_sum1),
2536+ "4"(_sum2),
2537+ "5"(_sum3),
2538+ "6"(_sum4),
2539+ "7"(_sum5),
2540+ "8"(_sum6),
2541+ "9"(_sum7)
2542+ : "memory", "q0", "q1", "q2");
2543+#endif
2544+#else // NCNN_GNU_INLINE_ASM
24642545 float32x4_t _pA = vld1q_f32(pA);
24652546 float32x4_t _pB0 = vld1q_f32(pB);
24662547 float32x4_t _pB1 = vld1q_f32(pB + 4);
@@ -2487,6 +2568,7 @@
24872568
24882569 pA += 4;
24892570 pB += 8;
2571+#endif // NCNN_GNU_INLINE_ASM
24902572 }
24912573
24922574 if (k_end)
@@ -4164,6 +4246,13 @@
41644246
41654247 int Gemm_arm::create_pipeline(const Option& opt)
41664248 {
4249+#if NCNN_INT8
4250+ if (int8_scale_term)
4251+ {
4252+ return create_pipeline_int8(opt);
4253+ }
4254+#endif
4255+
41674256 #if NCNN_ARM82
41684257 if (cpu_support_arm_asimdhp() && opt.use_fp16_storage)
41694258 {
@@ -4311,6 +4400,14 @@
43114400
43124401 int Gemm_arm::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
43134402 {
4403+#if NCNN_INT8
4404+ if (int8_scale_term)
4405+ {
4406+ return forward_int8(bottom_blobs, top_blobs, opt);
4407+ // return Gemm::forward_int8(bottom_blobs, top_blobs, opt);
4408+ }
4409+#endif
4410+
43144411 const Mat& bottom_blob = constantA ? AT_data : bottom_blobs[0];
43154412 int elembits = bottom_blob.elembits();
43164413
@@ -5199,4 +5296,994 @@
51995296 }
52005297 #endif // NCNN_BF16
52015298
5299+#if NCNN_INT8
5300+
5301+#if NCNN_VFPV4
5302+extern void compute_A_tile_fp16_int8_scales_vfpv4(const Mat& A, Mat& scales, float B_scale, Mat& out_descales, int i, int max_ii);
5303+extern void pack_A_tile_fp16_to_int8_vfpv4(const Mat& A, Mat& AT, int i, int max_ii, int k, int max_kk, const Mat& scales);
5304+extern void transpose_compute_A_tile_fp16_int8_scales_vfpv4(const Mat& A, Mat& scales, float B_scale, Mat& out_descales, int i, int max_ii);
5305+extern void transpose_pack_A_tile_fp16_to_int8_vfpv4(const Mat& A, Mat& AT, int i, int max_ii, int k, int max_kk, const Mat& scales);
5306+extern void compute_B_fp16_int8_scale_vfpv4(const Mat& B, float& scale);
5307+extern void pack_B_tile_fp16_to_int8_vfpv4(const Mat& B, Mat& BT, int j, int max_jj, int k, int max_kk, float scale);
5308+extern void transpose_pack_B_tile_fp16_to_int8_vfpv4(const Mat& B, Mat& BT, int j, int max_jj, int k, int max_kk, float scale);
5309+extern void unpack_output_tile_int32_to_fp16_vfpv4(const Mat& topT, const Mat& C, Mat& top_blob, int broadcast_type_C, int i, int max_ii, int j, int max_jj, const Mat& descales, float alpha, float beta);
5310+extern void transpose_unpack_output_tile_int32_to_fp16_vfpv4(const Mat& topT, const Mat& C, Mat& top_blob, int broadcast_type_C, int i, int max_ii, int j, int max_jj, const Mat& descales, float alpha, float beta);
5311+#endif
5312+
5313+static void compute_A_tile_int8_scales(const Mat& A, Mat& scales, float B_scale, Mat& out_descales, int i, int max_ii, int input_elemtype)
5314+{
5315+#if NCNN_VFPV4
5316+ if (A.elembits() == 16 && input_elemtype == 2)
5317+ {
5318+ compute_A_tile_fp16_int8_scales_vfpv4(A, scales, B_scale, out_descales, i, max_ii);
5319+ return;
5320+ }
5321+#endif
5322+
5323+#if NCNN_BF16
5324+ if (A.elembits() == 16 && input_elemtype == 3)
5325+ {
5326+ compute_A_tile_bf16_int8_scales(A, scales, B_scale, out_descales, i, max_ii);
5327+ return;
5328+ }
5329+#endif
5330+
5331+ compute_A_tile_fp32_int8_scales(A, scales, B_scale, out_descales, i, max_ii);
5332+}
5333+
5334+static void transpose_compute_A_tile_int8_scales(const Mat& A, Mat& scales, float B_scale, Mat& out_descales, int i, int max_ii, int input_elemtype)
5335+{
5336+#if NCNN_VFPV4
5337+ if (A.elembits() == 16 && input_elemtype == 2)
5338+ {
5339+ transpose_compute_A_tile_fp16_int8_scales_vfpv4(A, scales, B_scale, out_descales, i, max_ii);
5340+ return;
5341+ }
5342+#endif
5343+
5344+#if NCNN_BF16
5345+ if (A.elembits() == 16 && input_elemtype == 3)
5346+ {
5347+ transpose_compute_A_tile_bf16_int8_scales(A, scales, B_scale, out_descales, i, max_ii);
5348+ return;
5349+ }
5350+#endif
5351+
5352+ transpose_compute_A_tile_fp32_int8_scales(A, scales, B_scale, out_descales, i, max_ii);
5353+}
5354+
5355+static void pack_A_tile_quantize(const Mat& A, Mat& AT, int i, int max_ii, int k, int max_kk, const Mat& scales, int input_elemtype)
5356+{
5357+#if NCNN_VFPV4
5358+ if (A.elembits() == 16 && input_elemtype == 2)
5359+ {
5360+ pack_A_tile_fp16_to_int8_vfpv4(A, AT, i, max_ii, k, max_kk, scales);
5361+ return;
5362+ }
5363+#endif
5364+
5365+#if NCNN_BF16
5366+ if (A.elembits() == 16 && input_elemtype == 3)
5367+ {
5368+ pack_A_tile_bf16_to_int8(A, AT, i, max_ii, k, max_kk, scales);
5369+ return;
5370+ }
5371+#endif
5372+
5373+ pack_A_tile_fp32_to_int8(A, AT, i, max_ii, k, max_kk, scales);
5374+}
5375+
5376+static void transpose_pack_A_tile_quantize(const Mat& A, Mat& AT, int i, int max_ii, int k, int max_kk, const Mat& scales, int input_elemtype)
5377+{
5378+#if NCNN_VFPV4
5379+ if (A.elembits() == 16 && input_elemtype == 2)
5380+ {
5381+ transpose_pack_A_tile_fp16_to_int8_vfpv4(A, AT, i, max_ii, k, max_kk, scales);
5382+ return;
5383+ }
5384+#endif
5385+
5386+#if NCNN_BF16
5387+ if (A.elembits() == 16 && input_elemtype == 3)
5388+ {
5389+ transpose_pack_A_tile_bf16_to_int8(A, AT, i, max_ii, k, max_kk, scales);
5390+ return;
5391+ }
5392+#endif
5393+
5394+ transpose_pack_A_tile_fp32_to_int8(A, AT, i, max_ii, k, max_kk, scales);
5395+}
5396+
5397+static void compute_B_int8_scale(const Mat& B, float& scale, int input_elemtype)
5398+{
5399+#if NCNN_VFPV4
5400+ if (B.elembits() == 16 && input_elemtype == 2)
5401+ {
5402+ compute_B_fp16_int8_scale_vfpv4(B, scale);
5403+ return;
5404+ }
5405+#endif
5406+
5407+#if NCNN_BF16
5408+ if (B.elembits() == 16 && input_elemtype == 3)
5409+ {
5410+ compute_B_bf16_int8_scale(B, scale);
5411+ return;
5412+ }
5413+#endif
5414+
5415+ compute_B_fp32_int8_scale(B, scale);
5416+}
5417+
5418+static void pack_B_tile_quantize(const Mat& B, Mat& BT, int j, int max_jj, int k, int max_kk, float scale, int input_elemtype)
5419+{
5420+#if NCNN_VFPV4
5421+ if (B.elembits() == 16 && input_elemtype == 2)
5422+ {
5423+ pack_B_tile_fp16_to_int8_vfpv4(B, BT, j, max_jj, k, max_kk, scale);
5424+ return;
5425+ }
5426+#endif
5427+
5428+#if NCNN_BF16
5429+ if (B.elembits() == 16 && input_elemtype == 3)
5430+ {
5431+ pack_B_tile_bf16_to_int8(B, BT, j, max_jj, k, max_kk, scale);
5432+ return;
5433+ }
5434+#endif
5435+
5436+ pack_B_tile_fp32_to_int8(B, BT, j, max_jj, k, max_kk, scale);
5437+}
5438+
5439+static void transpose_pack_B_tile_quantize(const Mat& B, Mat& BT, int j, int max_jj, int k, int max_kk, float scale, int input_elemtype)
5440+{
5441+#if NCNN_VFPV4
5442+ if (B.elembits() == 16 && input_elemtype == 2)
5443+ {
5444+ transpose_pack_B_tile_fp16_to_int8_vfpv4(B, BT, j, max_jj, k, max_kk, scale);
5445+ return;
5446+ }
5447+#endif
5448+
5449+#if NCNN_BF16
5450+ if (B.elembits() == 16 && input_elemtype == 3)
5451+ {
5452+ transpose_pack_B_tile_bf16_to_int8(B, BT, j, max_jj, k, max_kk, scale);
5453+ return;
5454+ }
5455+#endif
5456+
5457+ transpose_pack_B_tile_fp32_to_int8(B, BT, j, max_jj, k, max_kk, scale);
5458+}
5459+
5460+static void unpack_output_tile_dequantize(const Mat& topT, const Mat& C, Mat& top_blob, int broadcast_type_C, int i, int max_ii, int j, int max_jj, const Mat& descales, float alpha, float beta, int output_elemtype)
5461+{
5462+#if NCNN_VFPV4
5463+ if (top_blob.elembits() == 16 && output_elemtype == 2)
5464+ {
5465+ unpack_output_tile_int32_to_fp16_vfpv4(topT, C, top_blob, broadcast_type_C, i, max_ii, j, max_jj, descales, alpha, beta);
5466+ return;
5467+ }
5468+#endif
5469+
5470+#if NCNN_BF16
5471+ if (top_blob.elembits() == 16 && output_elemtype == 3)
5472+ {
5473+ unpack_output_tile_int32_to_bf16(topT, C, top_blob, broadcast_type_C, i, max_ii, j, max_jj, descales, alpha, beta);
5474+ return;
5475+ }
5476+#endif
5477+
5478+ unpack_output_tile_int32_to_fp32(topT, C, top_blob, broadcast_type_C, i, max_ii, j, max_jj, descales, alpha, beta);
5479+}
5480+
5481+static void transpose_unpack_output_tile_dequantize(const Mat& topT, const Mat& C, Mat& top_blob, int broadcast_type_C, int i, int max_ii, int j, int max_jj, const Mat& descales, float alpha, float beta, int output_elemtype)
5482+{
5483+#if NCNN_VFPV4
5484+ if (top_blob.elembits() == 16 && output_elemtype == 2)
5485+ {
5486+ transpose_unpack_output_tile_int32_to_fp16_vfpv4(topT, C, top_blob, broadcast_type_C, i, max_ii, j, max_jj, descales, alpha, beta);
5487+ return;
5488+ }
5489+#endif
5490+
5491+#if NCNN_BF16
5492+ if (top_blob.elembits() == 16 && output_elemtype == 3)
5493+ {
5494+ transpose_unpack_output_tile_int32_to_bf16(topT, C, top_blob, broadcast_type_C, i, max_ii, j, max_jj, descales, alpha, beta);
5495+ return;
5496+ }
5497+#endif
5498+
5499+ transpose_unpack_output_tile_int32_to_fp32(topT, C, top_blob, broadcast_type_C, i, max_ii, j, max_jj, descales, alpha, beta);
5500+}
5501+
5502+struct gemm_arm_int8_omp_args
5503+{
5504+ int TILE_M;
5505+ int TILE_N;
5506+ int TILE_K;
5507+ int broadcast_type_C;
5508+ int transA;
5509+ int output_transpose;
5510+ float alpha;
5511+ float beta;
5512+ int input_elemtype;
5513+ int output_elemtype;
5514+};
5515+
5516+static int gemm_arm_int8(const Mat& A, const Mat& B, const Mat& C, Mat& top_blob, int broadcast_type_C, int transA, int transB, int output_transpose, float alpha, float beta, int input_elemtype, int output_elemtype, int constant_TILE_M, int constant_TILE_N, int constant_TILE_K, int nT, const Option& opt)
5517+{
5518+ // NCNN_LOGE("gemm_arm_int8");
5519+
5520+ const int M = transA ? A.w : (A.dims == 3 ? A.c : A.h) * A.elempack;
5521+ const int K = transA ? (A.dims == 3 ? A.c : A.h) * A.elempack : A.w;
5522+ const int N = transB ? (B.dims == 3 ? B.c : B.h) * B.elempack : B.w;
5523+
5524+ // NCNN_LOGE("M/N/K = %d %d %d", M, N, K);
5525+
5526+ int TILE_M, TILE_N, TILE_K;
5527+ get_optimal_tile_mnk_int8(M, N, K, constant_TILE_M, constant_TILE_N, constant_TILE_K, TILE_M, TILE_N, TILE_K, nT);
5528+
5529+ // NCNN_LOGE("TILE M/N/K = %d %d %d", TILE_M, TILE_N, TILE_K);
5530+
5531+ int nn_M = (M + TILE_M - 1) / TILE_M;
5532+ int nn_N = (N + TILE_N - 1) / TILE_N;
5533+ int nn_K = (K + TILE_K - 1) / TILE_K;
5534+
5535+ Mat ATX(TILE_K * TILE_M, (K + TILE_K - 1) / TILE_K, nT, 1u, opt.workspace_allocator);
5536+ if (ATX.empty())
5537+ return -100;
5538+ Mat BT(TILE_K * TILE_N, (K + TILE_K - 1) / TILE_K, (N + TILE_N - 1) / TILE_N, 1u, opt.workspace_allocator);
5539+ if (BT.empty())
5540+ return -100;
5541+
5542+ const int nn_NK = nn_N * nn_K;
5543+
5544+ Mat A_int8_scales(M, 4u, opt.workspace_allocator);
5545+ if (A_int8_scales.empty())
5546+ return -100;
5547+
5548+ // dynamic quantize B
5549+ float B_int8_scale;
5550+ compute_B_int8_scale(B, B_int8_scale, input_elemtype);
5551+
5552+ // const float output_descale = 1.f / (A_int8_scale * B_int8_scale);
5553+ Mat output_descales(M, 4u, opt.workspace_allocator);
5554+ if (output_descales.empty())
5555+ return -100;
5556+
5557+ // NCNN_LOGE("arm ds %f %f", 1/A_int8_scale, 1/B_int8_scale);
5558+
5559+ // pack B
5560+ #pragma omp parallel for num_threads(nT)
5561+ for (int ppjk = 0; ppjk < nn_NK; ppjk++)
5562+ {
5563+ const int ppj = ppjk / nn_K;
5564+ const int ppk = ppjk % nn_K;
5565+
5566+ const int j = ppj * TILE_N;
5567+ const int k = ppk * TILE_K;
5568+
5569+ const int max_jj = std::min((N - j), TILE_N);
5570+ const int max_kk = std::min((K - k), TILE_K);
5571+
5572+ Mat BT_tile = BT.channel(j / TILE_N).row_range(k / TILE_K, 1);
5573+
5574+ if (transB)
5575+ pack_B_tile_quantize(B, BT_tile, j, max_jj, k, max_kk, B_int8_scale, input_elemtype);
5576+ else
5577+ transpose_pack_B_tile_quantize(B, BT_tile, j, max_jj, k, max_kk, B_int8_scale, input_elemtype);
5578+ }
5579+
5580+ Mat topT(TILE_N * TILE_M, 1, nT, 4u, opt.workspace_allocator);
5581+ if (topT.empty())
5582+ return -100;
5583+
5584+ const struct gemm_arm_int8_omp_args args = {TILE_M, TILE_N, TILE_K, broadcast_type_C, transA, output_transpose, alpha, beta, input_elemtype, output_elemtype};
5585+
5586+ #pragma omp parallel for num_threads(nT)
5587+ for (int ppi = 0; ppi < nn_M; ppi++)
5588+ {
5589+ // shadowed variable for less openmp task args
5590+ const int TILE_M = args.TILE_M;
5591+ const int TILE_N = args.TILE_N;
5592+ const int TILE_K = args.TILE_K;
5593+ const int broadcast_type_C = args.broadcast_type_C;
5594+ const int transA = args.transA;
5595+ const int output_transpose = args.output_transpose;
5596+ const float alpha = args.alpha;
5597+ const float beta = args.beta;
5598+ const int input_elemtype = args.input_elemtype;
5599+ const int output_elemtype = args.output_elemtype;
5600+
5601+ const int M = transA ? A.w : (A.dims == 3 ? A.c : A.h) * A.elempack;
5602+ const int K = transA ? (A.dims == 3 ? A.c : A.h) * A.elempack : A.w;
5603+
5604+ const int i = ppi * TILE_M;
5605+
5606+ const int max_ii = std::min((M - i), TILE_M);
5607+
5608+ Mat topT_tile = topT.channel(get_omp_thread_num());
5609+
5610+ for (int j = 0; j < N; j += TILE_N)
5611+ {
5612+ const int max_jj = std::min((N - j), TILE_N);
5613+
5614+ for (int k = 0; k < K; k += TILE_K)
5615+ {
5616+ const int max_kk = std::min((K - k), TILE_K);
5617+
5618+ // NCNN_LOGE("max_ii/jj/kk = %d %d %d", max_ii, max_jj, max_kk);
5619+
5620+ Mat AT_tile = ATX.channel(get_omp_thread_num()).row_range(k / TILE_K, 1);
5621+
5622+ Mat BT_tile = BT.channel(j / TILE_N).row_range(k / TILE_K, 1);
5623+
5624+ if (j == 0)
5625+ {
5626+ if (k == 0)
5627+ {
5628+ if (transA)
5629+ transpose_compute_A_tile_int8_scales(A, A_int8_scales, B_int8_scale, output_descales, i, max_ii, input_elemtype);
5630+ else
5631+ compute_A_tile_int8_scales(A, A_int8_scales, B_int8_scale, output_descales, i, max_ii, input_elemtype);
5632+
5633+ // NCNN_LOGE("A_int8_scales %f B_int8_scale %f", A_int8_scales[0], B_int8_scale);
5634+ }
5635+
5636+ if (transA)
5637+ transpose_pack_A_tile_quantize(A, AT_tile, i, max_ii, k, max_kk, A_int8_scales, input_elemtype);
5638+ else
5639+ pack_A_tile_quantize(A, AT_tile, i, max_ii, k, max_kk, A_int8_scales, input_elemtype);
5640+ }
5641+
5642+ gemm_transB_packed_tile_int8(AT_tile, BT_tile, topT_tile, i, max_ii, j, max_jj, k, max_kk);
5643+ }
5644+
5645+ if (output_transpose)
5646+ transpose_unpack_output_tile_dequantize(topT_tile, C, top_blob, broadcast_type_C, i, max_ii, j, max_jj, output_descales, alpha, beta, output_elemtype);
5647+ else
5648+ unpack_output_tile_dequantize(topT_tile, C, top_blob, broadcast_type_C, i, max_ii, j, max_jj, output_descales, alpha, beta, output_elemtype);
5649+ }
5650+ }
5651+
5652+ return 0;
5653+}
5654+
5655+static int gemm_AT_arm_int8(const Mat& AT, const Mat& A_int8_scales, const Mat& B, const Mat& C, Mat& top_blob, int broadcast_type_C, int M, int K, int transB, int output_transpose, float alpha, float beta, int input_elemtype, int output_elemtype, int constant_TILE_M, int constant_TILE_N, int constant_TILE_K, int nT, const Option& opt)
5656+{
5657+ // NCNN_LOGE("gemm_AT_arm_int8");
5658+
5659+ const int N = transB ? (B.dims == 3 ? B.c : B.h) * B.elempack : B.w;
5660+
5661+ // NCNN_LOGE("M/N/K = %d %d %d", M, N, K);
5662+
5663+ int TILE_M, TILE_N, TILE_K;
5664+ get_optimal_tile_mnk_int8(M, N, K, constant_TILE_M, constant_TILE_N, constant_TILE_K, TILE_M, TILE_N, TILE_K, nT);
5665+
5666+ // NCNN_LOGE("TILE M/N/K = %d %d %d", TILE_M, TILE_N, TILE_K);
5667+
5668+ int nn_M = (M + TILE_M - 1) / TILE_M;
5669+ int nn_N = (N + TILE_N - 1) / TILE_N;
5670+ int nn_K = (K + TILE_K - 1) / TILE_K;
5671+
5672+ Mat BT(TILE_K * TILE_N, (K + TILE_K - 1) / TILE_K, (N + TILE_N - 1) / TILE_N, 1u, opt.workspace_allocator);
5673+ if (BT.empty())
5674+ return -100;
5675+
5676+ const int nn_NK = nn_N * nn_K;
5677+
5678+ // dynamic quantize B
5679+ float B_int8_scale;
5680+ compute_B_int8_scale(B, B_int8_scale, input_elemtype);
5681+
5682+ // NCNN_LOGE("%.4f %.4f", A_int8_scale, B_int8_scale);
5683+
5684+ // const float output_descale = 1.f / (A_int8_scale * B_int8_scale);
5685+ Mat output_descales(M, 4u, opt.workspace_allocator);
5686+ if (output_descales.empty())
5687+ return -100;
5688+
5689+ for (int i = 0; i < M; i++)
5690+ {
5691+ output_descales[i] = 1.f / (A_int8_scales[i] * B_int8_scale);
5692+ }
5693+
5694+ // pack B
5695+ #pragma omp parallel for num_threads(nT)
5696+ for (int ppjk = 0; ppjk < nn_NK; ppjk++)
5697+ {
5698+ const int ppj = ppjk / nn_K;
5699+ const int ppk = ppjk % nn_K;
5700+
5701+ const int j = ppj * TILE_N;
5702+ const int k = ppk * TILE_K;
5703+
5704+ const int max_jj = std::min((N - j), TILE_N);
5705+ const int max_kk = std::min((K - k), TILE_K);
5706+
5707+ Mat BT_tile = BT.channel(j / TILE_N).row_range(k / TILE_K, 1);
5708+
5709+ if (transB)
5710+ pack_B_tile_quantize(B, BT_tile, j, max_jj, k, max_kk, B_int8_scale, input_elemtype);
5711+ else
5712+ transpose_pack_B_tile_quantize(B, BT_tile, j, max_jj, k, max_kk, B_int8_scale, input_elemtype);
5713+ }
5714+
5715+ Mat topT(TILE_N * TILE_M, 1, nT, 4u, opt.workspace_allocator);
5716+ if (topT.empty())
5717+ return -100;
5718+
5719+ const struct gemm_arm_int8_omp_args args = {TILE_M, TILE_N, TILE_K, broadcast_type_C, 0, output_transpose, alpha, beta, input_elemtype, output_elemtype};
5720+
5721+ #pragma omp parallel for num_threads(nT)
5722+ for (int ppi = 0; ppi < nn_M; ppi++)
5723+ {
5724+ // shadowed variable for less openmp task args
5725+ const int TILE_M = args.TILE_M;
5726+ const int TILE_N = args.TILE_N;
5727+ const int TILE_K = args.TILE_K;
5728+ const int broadcast_type_C = args.broadcast_type_C;
5729+ const int output_transpose = args.output_transpose;
5730+ const float alpha = args.alpha;
5731+ const float beta = args.beta;
5732+ const int output_elemtype = args.output_elemtype;
5733+
5734+ const int i = ppi * TILE_M;
5735+
5736+ const int max_ii = std::min((M - i), TILE_M);
5737+
5738+ Mat topT_tile = topT.channel(get_omp_thread_num());
5739+
5740+ for (int j = 0; j < N; j += TILE_N)
5741+ {
5742+ const int max_jj = std::min((N - j), TILE_N);
5743+
5744+ for (int k = 0; k < K; k += TILE_K)
5745+ {
5746+ const int max_kk = std::min((K - k), TILE_K);
5747+
5748+ // NCNN_LOGE("max_ii/jj/kk = %d %d %d", max_ii, max_jj, max_kk);
5749+
5750+ Mat AT_tile = AT.channel(i / TILE_M).row_range(k / TILE_K, 1);
5751+
5752+ Mat BT_tile = BT.channel(j / TILE_N).row_range(k / TILE_K, 1);
5753+
5754+ gemm_transB_packed_tile_int8(AT_tile, BT_tile, topT_tile, i, max_ii, j, max_jj, k, max_kk);
5755+ }
5756+
5757+ if (output_transpose)
5758+ transpose_unpack_output_tile_dequantize(topT_tile, C, top_blob, broadcast_type_C, i, max_ii, j, max_jj, output_descales, alpha, beta, output_elemtype);
5759+ else
5760+ unpack_output_tile_dequantize(topT_tile, C, top_blob, broadcast_type_C, i, max_ii, j, max_jj, output_descales, alpha, beta, output_elemtype);
5761+ }
5762+ }
5763+
5764+ return 0;
5765+}
5766+
5767+static int gemm_BT_arm_int8(const Mat& A, const Mat& BT, float B_int8_scale, const Mat& C, Mat& top_blob, int broadcast_type_C, int N, int K, int transA, int output_transpose, float alpha, float beta, int input_elemtype, int output_elemtype, int constant_TILE_M, int constant_TILE_N, int constant_TILE_K, int nT, const Option& opt)
5768+{
5769+ // NCNN_LOGE("gemm_BT_arm_int8");
5770+
5771+ const int M = transA ? A.w : (A.dims == 3 ? A.c : A.h) * A.elempack;
5772+
5773+ // NCNN_LOGE("M/N/K = %d %d %d", M, N, K);
5774+
5775+ int TILE_M, TILE_N, TILE_K;
5776+ get_optimal_tile_mnk_int8(M, N, K, constant_TILE_M, constant_TILE_N, constant_TILE_K, TILE_M, TILE_N, TILE_K, nT);
5777+
5778+ // NCNN_LOGE("TILE M/N/K = %d %d %d", TILE_M, TILE_N, TILE_K);
5779+
5780+ int nn_M = (M + TILE_M - 1) / TILE_M;
5781+ // int nn_N = (N + TILE_N - 1) / TILE_N;
5782+
5783+ Mat A_int8_scales(M, 4u, opt.workspace_allocator);
5784+ if (A_int8_scales.empty())
5785+ return -100;
5786+
5787+ // const float output_descale = 1.f / (A_int8_scale * B_int8_scale);
5788+ Mat output_descales(M, 4u, opt.workspace_allocator);
5789+ if (output_descales.empty())
5790+ return -100;
5791+
5792+ // NCNN_LOGE("scale %.4f %.4f", A_int8_scale, B_int8_scale);
5793+
5794+ Mat ATX(TILE_K * TILE_M, (K + TILE_K - 1) / TILE_K, nT, 1u, opt.workspace_allocator);
5795+ if (ATX.empty())
5796+ return -100;
5797+
5798+ Mat topT(TILE_N * TILE_M, 1, nT, 4u, opt.workspace_allocator);
5799+ if (topT.empty())
5800+ return -100;
5801+
5802+ const struct gemm_arm_int8_omp_args args = {TILE_M, TILE_N, TILE_K, broadcast_type_C, transA, output_transpose, alpha, beta, input_elemtype, output_elemtype};
5803+
5804+ #pragma omp parallel for num_threads(nT)
5805+ for (int ppi = 0; ppi < nn_M; ppi++)
5806+ {
5807+ // shadowed variable for less openmp task args
5808+ const int TILE_M = args.TILE_M;
5809+ const int TILE_N = args.TILE_N;
5810+ const int TILE_K = args.TILE_K;
5811+ const int broadcast_type_C = args.broadcast_type_C;
5812+ const int transA = args.transA;
5813+ const int output_transpose = args.output_transpose;
5814+ const float alpha = args.alpha;
5815+ const float beta = args.beta;
5816+ const int input_elemtype = args.input_elemtype;
5817+ const int output_elemtype = args.output_elemtype;
5818+
5819+ const int i = ppi * TILE_M;
5820+
5821+ // shadowed variable for less openmp task args
5822+ const int M = transA ? A.w : (A.dims == 3 ? A.c : A.h) * A.elempack;
5823+ const int K = transA ? (A.dims == 3 ? A.c : A.h) * A.elempack : A.w;
5824+
5825+ const int max_ii = std::min((M - i), TILE_M);
5826+
5827+ Mat topT_tile = topT.channel(get_omp_thread_num());
5828+
5829+ for (int j = 0; j < N; j += TILE_N)
5830+ {
5831+ const int max_jj = std::min((N - j), TILE_N);
5832+
5833+ for (int k = 0; k < K; k += TILE_K)
5834+ {
5835+ const int max_kk = std::min((K - k), TILE_K);
5836+
5837+ // NCNN_LOGE("max_ii/jj/kk = %d %d %d", max_ii, max_jj, max_kk);
5838+
5839+ Mat AT_tile = ATX.channel(get_omp_thread_num()).row_range(k / TILE_K, 1);
5840+
5841+ Mat BT_tile = BT.channel(j / TILE_N).row_range(k / TILE_K, 1);
5842+
5843+ if (j == 0)
5844+ {
5845+ if (k == 0)
5846+ {
5847+ if (transA)
5848+ transpose_compute_A_tile_int8_scales(A, A_int8_scales, B_int8_scale, output_descales, i, max_ii, input_elemtype);
5849+ else
5850+ compute_A_tile_int8_scales(A, A_int8_scales, B_int8_scale, output_descales, i, max_ii, input_elemtype);
5851+
5852+ // NCNN_LOGE("A_int8_scales %f B_int8_scale %f", A_int8_scales[0], B_int8_scale);
5853+ }
5854+
5855+ if (transA)
5856+ transpose_pack_A_tile_quantize(A, AT_tile, i, max_ii, k, max_kk, A_int8_scales, input_elemtype);
5857+ else
5858+ pack_A_tile_quantize(A, AT_tile, i, max_ii, k, max_kk, A_int8_scales, input_elemtype);
5859+ }
5860+
5861+ gemm_transB_packed_tile_int8(AT_tile, BT_tile, topT_tile, i, max_ii, j, max_jj, k, max_kk);
5862+ }
5863+
5864+ if (output_transpose)
5865+ transpose_unpack_output_tile_dequantize(topT_tile, C, top_blob, broadcast_type_C, i, max_ii, j, max_jj, output_descales, alpha, beta, output_elemtype);
5866+ else
5867+ unpack_output_tile_dequantize(topT_tile, C, top_blob, broadcast_type_C, i, max_ii, j, max_jj, output_descales, alpha, beta, output_elemtype);
5868+ }
5869+ }
5870+
5871+ return 0;
5872+}
5873+
5874+static int gemm_AT_BT_arm_int8(const Mat& AT, const Mat& A_int8_scales, const Mat& BT, float B_int8_scale, const Mat& C, Mat& top_blob, int broadcast_type_C, int M, int N, int K, int output_transpose, float alpha, float beta, int input_elemtype, int output_elemtype, int constant_TILE_M, int constant_TILE_N, int constant_TILE_K, int nT, const Option& opt)
5875+{
5876+ // NCNN_LOGE("gemm_AT_BT_arm_int8");
5877+
5878+ // NCNN_LOGE("M/N/K = %d %d %d", M, N, K);
5879+
5880+ int TILE_M, TILE_N, TILE_K;
5881+ get_optimal_tile_mnk_int8(M, N, K, constant_TILE_M, constant_TILE_N, constant_TILE_K, TILE_M, TILE_N, TILE_K, nT);
5882+
5883+ // NCNN_LOGE("TILE M/N/K = %d %d %d", TILE_M, TILE_N, TILE_K);
5884+
5885+ int nn_M = (M + TILE_M - 1) / TILE_M;
5886+ // int nn_N = (N + TILE_N - 1) / TILE_N;
5887+
5888+ // const float output_descale = 1.f / (A_int8_scale * B_int8_scale);
5889+ Mat output_descales(M, 4u, opt.workspace_allocator);
5890+ if (output_descales.empty())
5891+ return -100;
5892+
5893+ for (int i = 0; i < M; i++)
5894+ {
5895+ output_descales[i] = 1.f / (A_int8_scales[i] * B_int8_scale);
5896+ }
5897+
5898+ Mat topT(TILE_N * TILE_M, 1, nT, 4u, opt.workspace_allocator);
5899+ if (topT.empty())
5900+ return -100;
5901+
5902+ const struct gemm_arm_int8_omp_args args = {TILE_M, TILE_N, TILE_K, broadcast_type_C, 0, output_transpose, alpha, beta, input_elemtype, output_elemtype};
5903+
5904+ #pragma omp parallel for num_threads(nT)
5905+ for (int ppi = 0; ppi < nn_M; ppi++)
5906+ {
5907+ // shadowed variable for less openmp task args
5908+ const int TILE_M = args.TILE_M;
5909+ const int TILE_N = args.TILE_N;
5910+ const int TILE_K = args.TILE_K;
5911+ const int broadcast_type_C = args.broadcast_type_C;
5912+ const int output_transpose = args.output_transpose;
5913+ const float alpha = args.alpha;
5914+ const float beta = args.beta;
5915+ const int output_elemtype = args.output_elemtype;
5916+
5917+ const int i = ppi * TILE_M;
5918+
5919+ const int max_ii = std::min((M - i), TILE_M);
5920+
5921+ Mat topT_tile = topT.channel(get_omp_thread_num());
5922+
5923+ for (int j = 0; j < N; j += TILE_N)
5924+ {
5925+ const int max_jj = std::min((N - j), TILE_N);
5926+
5927+ for (int k = 0; k < K; k += TILE_K)
5928+ {
5929+ const int max_kk = std::min((K - k), TILE_K);
5930+
5931+ // NCNN_LOGE("max_ii/jj/kk = %d %d %d", max_ii, max_jj, max_kk);
5932+
5933+ Mat AT_tile = AT.channel(i / TILE_M).row_range(k / TILE_K, 1);
5934+
5935+ Mat BT_tile = BT.channel(j / TILE_N).row_range(k / TILE_K, 1);
5936+
5937+ gemm_transB_packed_tile_int8(AT_tile, BT_tile, topT_tile, i, max_ii, j, max_jj, k, max_kk);
5938+ }
5939+
5940+ if (output_transpose)
5941+ transpose_unpack_output_tile_dequantize(topT_tile, C, top_blob, broadcast_type_C, i, max_ii, j, max_jj, output_descales, alpha, beta, output_elemtype);
5942+ else
5943+ unpack_output_tile_dequantize(topT_tile, C, top_blob, broadcast_type_C, i, max_ii, j, max_jj, output_descales, alpha, beta, output_elemtype);
5944+ }
5945+ }
5946+
5947+ return 0;
5948+}
5949+
5950+int Gemm_arm::create_pipeline_int8(const Option& opt)
5951+{
5952+ // finalize input_elemtype from cpu capability and opt
5953+ {
5954+ // armv8.2 + use-fp16 = fp16
5955+ // armv8.2 + no-fp16 + use-bf16 = bf16
5956+ // armv8.2 + no-fp16 + no-bf16 = fp32
5957+ // armv8.0/armv7-vfpv4 + use-bf16 = bf16
5958+ // armv8.0/armv7-vfpv4 + no-bf16 + use-fp16 = fp16
5959+ // armv8.0/armv7-vfpv4 + no-fp16 + no-bf16 = fp32
5960+ // armv7 + use-bf16 = bf16
5961+ // armv7 + no-bf16 = fp32
5962+
5963+ bool use_fp16 = false;
5964+ bool use_bf16 = false;
5965+
5966+#if NCNN_ARM82
5967+ if (ncnn::cpu_support_arm_asimdhp())
5968+ {
5969+ use_fp16 = opt.use_fp16_storage;
5970+ use_bf16 = opt.use_bf16_storage && !opt.use_fp16_storage;
5971+ }
5972+ else
5973+#endif
5974+#if NCNN_VFPV4
5975+ if (ncnn::cpu_support_arm_vfpv4())
5976+ {
5977+ use_bf16 = opt.use_bf16_storage;
5978+ use_fp16 = opt.use_fp16_storage && !opt.use_bf16_storage;
5979+ }
5980+ else
5981+#endif
5982+ {
5983+ use_bf16 = opt.use_bf16_storage;
5984+ }
5985+
5986+ input_elemtype = 1; // fp32
5987+ if (use_fp16) input_elemtype = 2;
5988+ if (use_bf16) input_elemtype = 3;
5989+
5990+ // NCNN_LOGE("input_elemtype = %d", input_elemtype);
5991+ }
5992+
5993+ if (constantA)
5994+ {
5995+ const int M = constantM;
5996+ const int K = constantK;
5997+
5998+ int TILE_M, TILE_N, TILE_K;
5999+ get_optimal_tile_mnk_int8(M, 0, K, constant_TILE_M, constant_TILE_N, constant_TILE_K, TILE_M, TILE_N, TILE_K, opt.num_threads);
6000+
6001+ const int nn_M = (M + TILE_M - 1) / TILE_M;
6002+
6003+ AT_data.create(TILE_K * TILE_M, (K + TILE_K - 1) / TILE_K, (M + TILE_M - 1) / TILE_M, 1u, (Allocator*)0);
6004+ if (AT_data.empty())
6005+ return -100;
6006+
6007+ #pragma omp parallel for num_threads(opt.num_threads)
6008+ for (int ppj = 0; ppj < nn_M; ppj++)
6009+ {
6010+ const int i = ppj * TILE_M;
6011+
6012+ for (int k = 0; k < K; k += TILE_K)
6013+ {
6014+ const int max_ii = std::min((M - i), TILE_M);
6015+ const int max_kk = std::min((K - k), TILE_K);
6016+
6017+ Mat AT_tile = AT_data.channel(i / TILE_M).row_range(k / TILE_K, 1);
6018+
6019+ if (transA)
6020+ {
6021+ transpose_pack_A_tile_int8(A_data, AT_tile, i, max_ii, k, max_kk);
6022+ }
6023+ else
6024+ {
6025+ pack_A_tile_int8(A_data, AT_tile, i, max_ii, k, max_kk);
6026+ }
6027+ }
6028+ }
6029+
6030+ if (opt.lightmode)
6031+ A_data.release();
6032+ }
6033+
6034+ if (constantB)
6035+ {
6036+ const int N = constantN;
6037+ const int K = constantK;
6038+
6039+ int TILE_M, TILE_N, TILE_K;
6040+ get_optimal_tile_mnk_int8(0, N, K, constant_TILE_M, constant_TILE_N, constant_TILE_K, TILE_M, TILE_N, TILE_K, opt.num_threads);
6041+
6042+ const int nn_N = (N + TILE_N - 1) / TILE_N;
6043+
6044+ BT_data.create(TILE_K * TILE_N, (K + TILE_K - 1) / TILE_K, (N + TILE_N - 1) / TILE_N, 1u, (Allocator*)0);
6045+ if (BT_data.empty())
6046+ return -100;
6047+
6048+ #pragma omp parallel for num_threads(opt.num_threads)
6049+ for (int ppj = 0; ppj < nn_N; ppj++)
6050+ {
6051+ const int j = ppj * TILE_N;
6052+
6053+ for (int k = 0; k < K; k += TILE_K)
6054+ {
6055+ const int max_jj = std::min((N - j), TILE_N);
6056+ const int max_kk = std::min((K - k), TILE_K);
6057+
6058+ Mat BT_tile = BT_data.channel(j / TILE_N).row_range(k / TILE_K, 1);
6059+
6060+ if (transB)
6061+ {
6062+ pack_B_tile_int8(B_data, BT_tile, j, max_jj, k, max_kk);
6063+ }
6064+ else
6065+ {
6066+ transpose_pack_B_tile_int8(B_data, BT_tile, j, max_jj, k, max_kk);
6067+ }
6068+ }
6069+ }
6070+
6071+ if (opt.lightmode)
6072+ B_data.release();
6073+ }
6074+
6075+ if (constantC && constant_broadcast_type_C != -1)
6076+ {
6077+ CT_data = C_data;
6078+
6079+#if NCNN_VFPV4
6080+ if (input_elemtype == 2)
6081+ {
6082+ Mat C2;
6083+ ncnn::cast_float32_to_float16(CT_data, C2);
6084+ CT_data = C2;
6085+ }
6086+#endif
6087+#if NCNN_BF16
6088+ if (input_elemtype == 3)
6089+ {
6090+ Mat C2;
6091+ ncnn::cast_float32_to_bfloat16(CT_data, C2);
6092+ CT_data = C2;
6093+ }
6094+#endif
6095+
6096+ if (opt.lightmode)
6097+ C_data.release();
6098+ }
6099+
6100+ if (constantA || constantB || constantC)
6101+ {
6102+ nT = opt.num_threads;
6103+ }
6104+
6105+ return 0;
6106+}
6107+
6108+int Gemm_arm::forward_int8(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
6109+{
6110+ int M;
6111+ int N;
6112+ if (constantA && constantB)
6113+ {
6114+ M = constantM;
6115+ N = constantN;
6116+ }
6117+ else if (constantA)
6118+ {
6119+ const Mat& B = bottom_blobs[0];
6120+ M = constantM;
6121+ N = transB ? (B.dims == 3 ? B.c : B.h) * B.elempack : B.w;
6122+ }
6123+ else if (constantB)
6124+ {
6125+ const Mat& A = bottom_blobs[0];
6126+ M = transA ? A.w : (A.dims == 3 ? A.c : A.h) * A.elempack;
6127+ N = constantN;
6128+ }
6129+ else
6130+ {
6131+ const Mat& A = bottom_blobs[0];
6132+ const Mat& B = bottom_blobs[1];
6133+ M = transA ? A.w : (A.dims == 3 ? A.c : A.h) * A.elempack;
6134+ N = transB ? (B.dims == 3 ? B.c : B.h) * B.elempack : B.w;
6135+ }
6136+
6137+ Mat C;
6138+ int broadcast_type_C = 0;
6139+ if (constantC)
6140+ {
6141+ C = CT_data;
6142+ broadcast_type_C = constant_broadcast_type_C;
6143+ }
6144+ else
6145+ {
6146+ if (constantA && constantB)
6147+ {
6148+ C = bottom_blobs.size() == 1 ? bottom_blobs[0] : Mat();
6149+ }
6150+ else if (constantA)
6151+ {
6152+ C = bottom_blobs.size() == 2 ? bottom_blobs[1] : Mat();
6153+ }
6154+ else if (constantB)
6155+ {
6156+ C = bottom_blobs.size() == 2 ? bottom_blobs[1] : Mat();
6157+ }
6158+ else
6159+ {
6160+ C = bottom_blobs.size() == 3 ? bottom_blobs[2] : Mat();
6161+ }
6162+
6163+ if (!C.empty())
6164+ {
6165+ if (C.dims == 1 && C.w == 1)
6166+ {
6167+ // scalar
6168+ broadcast_type_C = 0;
6169+ }
6170+ if (C.dims == 1 && C.w * C.elempack == M)
6171+ {
6172+ // M
6173+ // auto broadcast from h to w is the ncnn-style convention
6174+ broadcast_type_C = 1;
6175+ }
6176+ if (C.dims == 1 && C.w * C.elempack == N)
6177+ {
6178+ // N
6179+ broadcast_type_C = 4;
6180+ }
6181+ if (C.dims == 2 && C.w == 1 && C.h * C.elempack == M)
6182+ {
6183+ // Mx1
6184+ broadcast_type_C = 2;
6185+ }
6186+ if (C.dims == 2 && C.w == N && C.h * C.elempack == M)
6187+ {
6188+ // MxN
6189+ broadcast_type_C = 3;
6190+ }
6191+ if (C.dims == 2 && C.w == N && C.h * C.elempack == 1)
6192+ {
6193+ // 1xN
6194+ broadcast_type_C = 4;
6195+ }
6196+ }
6197+ }
6198+
6199+ int out_elempack = 1;
6200+#if __ARM_NEON
6201+ if (opt.use_packing_layout)
6202+ {
6203+ int outh = output_transpose ? N : M;
6204+ out_elempack = outh % 4 == 0 ? 4 : 1;
6205+#if NCNN_ARM82
6206+ if (cpu_support_arm_asimdhp() && opt.use_fp16_arithmetic)
6207+ {
6208+ // TODO use output_elemtype
6209+ out_elempack = outh % 8 == 0 ? 8 : outh % 4 == 0 ? 4 : 1;
6210+ }
6211+#endif
6212+ }
6213+#endif // __ARM_NEON
6214+
6215+ // FIXME use output_elempack
6216+ // int output_elempack = out_elempack > 4 ? 4 : out_elempack;
6217+
6218+ if (output_elempack)
6219+ out_elempack = output_elempack;
6220+ size_t out_elemsize = 4u * out_elempack;
6221+
6222+ // FIXME use output_elemtype instead of input_elemtype
6223+ int output_elemtype = input_elemtype;
6224+
6225+ // TODO use output_elemtype
6226+ if (opt.use_bf16_storage)
6227+ {
6228+ out_elemsize = 2u * out_elempack;
6229+ }
6230+#if NCNN_VFPV4
6231+ else if (support_fp16_storage && opt.use_fp16_storage)
6232+ {
6233+ out_elemsize = 2u * out_elempack;
6234+ }
6235+#endif
6236+
6237+ Mat& top_blob = top_blobs[0];
6238+ if (output_transpose)
6239+ {
6240+ if (output_N1M)
6241+ top_blob.create(M, 1, N / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
6242+ else
6243+ top_blob.create(M, N / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
6244+ }
6245+ else
6246+ {
6247+ if (output_N1M)
6248+ top_blob.create(N, 1, M / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
6249+ else
6250+ top_blob.create(N, M / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
6251+ }
6252+ if (top_blob.empty())
6253+ return -100;
6254+
6255+ int _nT = nT ? nT : opt.num_threads;
6256+ if (nT != 0 && opt.num_threads != nT)
6257+ {
6258+ // force num_threads the same as in create_pipeline
6259+ // so we could use pre-packed A/B from the same tile config
6260+ NCNN_LOGE("opt.num_threads %d changed, gemm will use load-time value %d", opt.num_threads, nT);
6261+ }
6262+
6263+ int ret = 0;
6264+ if (constantA && constantB)
6265+ {
6266+ ret = gemm_AT_BT_arm_int8(AT_data, A_data_int8_scales, BT_data, B_data_int8_scale, C, top_blob, broadcast_type_C, constantM, constantN, constantK, output_transpose, alpha, beta, input_elemtype, output_elemtype, constant_TILE_M, constant_TILE_N, constant_TILE_K, _nT, opt);
6267+ }
6268+ else if (constantA)
6269+ {
6270+ const Mat& B = bottom_blobs[0];
6271+ ret = gemm_AT_arm_int8(AT_data, A_data_int8_scales, B, C, top_blob, broadcast_type_C, constantM, constantK, transB, output_transpose, alpha, beta, input_elemtype, output_elemtype, constant_TILE_M, constant_TILE_N, constant_TILE_K, _nT, opt);
6272+ }
6273+ else if (constantB)
6274+ {
6275+ const Mat& A = bottom_blobs[0];
6276+ ret = gemm_BT_arm_int8(A, BT_data, B_data_int8_scale, C, top_blob, broadcast_type_C, constantN, constantK, transA, output_transpose, alpha, beta, input_elemtype, output_elemtype, constant_TILE_M, constant_TILE_N, constant_TILE_K, _nT, opt);
6277+ }
6278+ else
6279+ {
6280+ const Mat& A = bottom_blobs[0];
6281+ const Mat& B = bottom_blobs[1];
6282+ ret = gemm_arm_int8(A, B, C, top_blob, broadcast_type_C, transA, transB, output_transpose, alpha, beta, input_elemtype, output_elemtype, constant_TILE_M, constant_TILE_N, constant_TILE_K, _nT, opt);
6283+ }
6284+
6285+ return ret;
6286+}
6287+#endif
6288+
52026289 } // namespace ncnn
modified src/layer/arm/gemm_arm.h +6 −0
@@ -41,12 +41,18 @@
4141 int create_pipeline_bf16s(const Option& opt);
4242 int forward_bf16s(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const;
4343 #endif
44+#if NCNN_INT8
45+ int create_pipeline_int8(const Option& opt);
46+ int forward_int8(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const;
47+#endif
4448
4549 public:
4650 int nT;
4751 Mat AT_data;
4852 Mat BT_data;
4953 Mat CT_data;
54+
55+ int input_elemtype; // 0=auto 1=fp32 2=fp16 3=bf16
5056 };
5157
5258 } // namespace ncnn
added src/layer/arm/gemm_arm_asimddp.cpp +145 −0
@@ -0,0 +1,145 @@
1+// Tencent is pleased to support the open source community by making ncnn available.
2+//
3+// Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
4+//
5+// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6+// in compliance with the License. You may obtain a copy of the License at
7+//
8+// https://opensource.org/licenses/BSD-3-Clause
9+//
10+// Unless required by applicable law or agreed to in writing, software distributed
11+// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12+// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13+// specific language governing permissions and limitations under the License.
14+
15+#include "cpu.h"
16+#include "mat.h"
17+#include "arm_usability.h"
18+
19+namespace ncnn {
20+
21+#include "gemm_int8.h"
22+#include "gemm_int8_fp16s.h"
23+
24+#if NCNN_BF16
25+#include "gemm_int8_bf16s.h"
26+#endif
27+
28+void pack_A_tile_int8_asimddp(const Mat& A, Mat& AT, int i, int max_ii, int k, int max_kk)
29+{
30+ pack_A_tile_int8(A, AT, i, max_ii, k, max_kk);
31+}
32+
33+void transpose_pack_A_tile_int8_asimddp(const Mat& A, Mat& AT, int i, int max_ii, int k, int max_kk)
34+{
35+ transpose_pack_A_tile_int8(A, AT, i, max_ii, k, max_kk);
36+}
37+
38+void pack_B_tile_int8_asimddp(const Mat& B, Mat& BT, int j, int max_jj, int k, int max_kk)
39+{
40+ pack_B_tile_int8(B, BT, j, max_jj, k, max_kk);
41+}
42+
43+void transpose_pack_B_tile_int8_asimddp(const Mat& B, Mat& BT, int j, int max_jj, int k, int max_kk)
44+{
45+ transpose_pack_B_tile_int8(B, BT, j, max_jj, k, max_kk);
46+}
47+
48+void pack_A_tile_fp32_to_int8_asimddp(const Mat& A, Mat& AT, int i, int max_ii, int k, int max_kk, const Mat& scales)
49+{
50+ pack_A_tile_fp32_to_int8(A, AT, i, max_ii, k, max_kk, scales);
51+}
52+
53+void transpose_pack_A_tile_fp32_to_int8_asimddp(const Mat& A, Mat& AT, int i, int max_ii, int k, int max_kk, const Mat& scales)
54+{
55+ transpose_pack_A_tile_fp32_to_int8(A, AT, i, max_ii, k, max_kk, scales);
56+}
57+
58+void pack_B_tile_fp32_to_int8_asimddp(const Mat& B, Mat& BT, int j, int max_jj, int k, int max_kk, float scale)
59+{
60+ pack_B_tile_fp32_to_int8(B, BT, j, max_jj, k, max_kk, scale);
61+}
62+
63+void transpose_pack_B_tile_fp32_to_int8_asimddp(const Mat& B, Mat& BT, int j, int max_jj, int k, int max_kk, float scale)
64+{
65+ transpose_pack_B_tile_fp32_to_int8(B, BT, j, max_jj, k, max_kk, scale);
66+}
67+
68+void unpack_output_tile_int32_to_fp32_asimddp(const Mat& topT, const Mat& C, Mat& top_blob, int broadcast_type_C, int i, int max_ii, int j, int max_jj, const Mat& descales, float alpha, float beta)
69+{
70+ unpack_output_tile_int32_to_fp32(topT, C, top_blob, broadcast_type_C, i, max_ii, j, max_jj, descales, alpha, beta);
71+}
72+
73+void transpose_unpack_output_tile_int32_to_fp32_asimddp(const Mat& topT, const Mat& C, Mat& top_blob, int broadcast_type_C, int i, int max_ii, int j, int max_jj, const Mat& descales, float alpha, float beta)
74+{
75+ transpose_unpack_output_tile_int32_to_fp32(topT, C, top_blob, broadcast_type_C, i, max_ii, j, max_jj, descales, alpha, beta);
76+}
77+
78+void gemm_transB_packed_tile_int8_asimddp(const Mat& AT_tile, const Mat& BT_tile, Mat& topT_tile, int i, int max_ii, int j, int max_jj, int k, int max_kk)
79+{
80+ gemm_transB_packed_tile_int8(AT_tile, BT_tile, topT_tile, i, max_ii, j, max_jj, k, max_kk);
81+}
82+
83+void pack_A_tile_fp16_to_int8_asimddp(const Mat& A, Mat& AT, int i, int max_ii, int k, int max_kk, const Mat& scales)
84+{
85+ pack_A_tile_fp16_to_int8(A, AT, i, max_ii, k, max_kk, scales);
86+}
87+
88+void transpose_pack_A_tile_fp16_to_int8_asimddp(const Mat& A, Mat& AT, int i, int max_ii, int k, int max_kk, const Mat& scales)
89+{
90+ transpose_pack_A_tile_fp16_to_int8(A, AT, i, max_ii, k, max_kk, scales);
91+}
92+
93+void pack_B_tile_fp16_to_int8_asimddp(const Mat& B, Mat& BT, int j, int max_jj, int k, int max_kk, float scale)
94+{
95+ pack_B_tile_fp16_to_int8(B, BT, j, max_jj, k, max_kk, scale);
96+}
97+
98+void transpose_pack_B_tile_fp16_to_int8_asimddp(const Mat& B, Mat& BT, int j, int max_jj, int k, int max_kk, float scale)
99+{
100+ transpose_pack_B_tile_fp16_to_int8(B, BT, j, max_jj, k, max_kk, scale);
101+}
102+
103+void unpack_output_tile_int32_to_fp16_asimddp(const Mat& topT, const Mat& C, Mat& top_blob, int broadcast_type_C, int i, int max_ii, int j, int max_jj, const Mat& descales, float alpha, float beta)
104+{
105+ unpack_output_tile_int32_to_fp16(topT, C, top_blob, broadcast_type_C, i, max_ii, j, max_jj, descales, alpha, beta);
106+}
107+
108+void transpose_unpack_output_tile_int32_to_fp16_asimddp(const Mat& topT, const Mat& C, Mat& top_blob, int broadcast_type_C, int i, int max_ii, int j, int max_jj, const Mat& descales, float alpha, float beta)
109+{
110+ transpose_unpack_output_tile_int32_to_fp16(topT, C, top_blob, broadcast_type_C, i, max_ii, j, max_jj, descales, alpha, beta);
111+}
112+
113+#if NCNN_BF16
114+void pack_A_tile_bf16_to_int8_asimddp(const Mat& A, Mat& AT, int i, int max_ii, int k, int max_kk, const Mat& scales)
115+{
116+ pack_A_tile_bf16_to_int8(A, AT, i, max_ii, k, max_kk, scales);
117+}
118+
119+void transpose_pack_A_tile_bf16_to_int8_asimddp(const Mat& A, Mat& AT, int i, int max_ii, int k, int max_kk, const Mat& scales)
120+{
121+ transpose_pack_A_tile_bf16_to_int8(A, AT, i, max_ii, k, max_kk, scales);
122+}
123+
124+void pack_B_tile_bf16_to_int8_asimddp(const Mat& B, Mat& BT, int j, int max_jj, int k, int max_kk, float scale)
125+{
126+ pack_B_tile_bf16_to_int8(B, BT, j, max_jj, k, max_kk, scale);
127+}
128+
129+void transpose_pack_B_tile_bf16_to_int8_asimddp(const Mat& B, Mat& BT, int j, int max_jj, int k, int max_kk, float scale)
130+{
131+ transpose_pack_B_tile_bf16_to_int8(B, BT, j, max_jj, k, max_kk, scale);
132+}
133+
134+void unpack_output_tile_int32_to_bf16_asimddp(const Mat& topT, const Mat& C, Mat& top_blob, int broadcast_type_C, int i, int max_ii, int j, int max_jj, const Mat& descales, float alpha, float beta)
135+{
136+ unpack_output_tile_int32_to_bf16(topT, C, top_blob, broadcast_type_C, i, max_ii, j, max_jj, descales, alpha, beta);
137+}
138+
139+void transpose_unpack_output_tile_int32_to_bf16_asimddp(const Mat& topT, const Mat& C, Mat& top_blob, int broadcast_type_C, int i, int max_ii, int j, int max_jj, const Mat& descales, float alpha, float beta)
140+{
141+ transpose_unpack_output_tile_int32_to_bf16(topT, C, top_blob, broadcast_type_C, i, max_ii, j, max_jj, descales, alpha, beta);
142+}
143+#endif // NCNN_BF16
144+
145+} // namespace ncnn
modified src/layer/arm/gemm_arm_asimdhp.cpp +21 −0
@@ -27,6 +27,10 @@
2727 #include "gemm_bf16s_fp16s.h"
2828 #include "gemm_fp16s.h"
2929
30+#if NCNN_INT8
31+#include "gemm_int8_fp16s.h"
32+#endif
33+
3034 static void gemm_transB_packed_tile_fp16sa(const Mat& AT_tile, const Mat& BT_tile, const Mat& CT_tile, Mat& topT_tile, Mat& top_blob, int broadcast_type_C, int i, int max_ii, int j, int max_jj, int k, int max_kk, bool k_end)
3135 {
3236 const int out_elempack = top_blob.elempack;
@@ -3026,4 +3030,21 @@
30263030 return 0;
30273031 }
30283032
3033+#if NCNN_INT8
3034+void compute_A_tile_fp16_int8_scales_asimdhp(const Mat& A, Mat& scales, float B_scale, Mat& out_descales, int i, int max_ii)
3035+{
3036+ compute_A_tile_fp16_int8_scales(A, scales, B_scale, out_descales, i, max_ii);
3037+}
3038+
3039+void transpose_compute_A_tile_fp16_int8_scales_asimdhp(const Mat& A, Mat& scales, float B_scale, Mat& out_descales, int i, int max_ii)
3040+{
3041+ transpose_compute_A_tile_fp16_int8_scales(A, scales, B_scale, out_descales, i, max_ii);
3042+}
3043+
3044+void compute_B_fp16_int8_scale_asimdhp(const Mat& B, float& scale)
3045+{
3046+ compute_B_fp16_int8_scale(B, scale);
3047+}
3048+#endif // NCNN_INT8
3049+
30293050 } // namespace ncnn
added src/layer/arm/gemm_arm_i8mm.cpp +115 −0
@@ -0,0 +1,115 @@
1+// Tencent is pleased to support the open source community by making ncnn available.
2+//
3+// Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
4+//
5+// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6+// in compliance with the License. You may obtain a copy of the License at
7+//
8+// https://opensource.org/licenses/BSD-3-Clause
9+//
10+// Unless required by applicable law or agreed to in writing, software distributed
11+// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12+// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13+// specific language governing permissions and limitations under the License.
14+
15+#include "cpu.h"
16+#include "mat.h"
17+#include "arm_usability.h"
18+
19+namespace ncnn {
20+
21+#include "gemm_int8.h"
22+#include "gemm_int8_fp16s.h"
23+
24+#if NCNN_BF16
25+#include "gemm_int8_bf16s.h"
26+#endif
27+
28+void pack_A_tile_int8_i8mm(const Mat& A, Mat& AT, int i, int max_ii, int k, int max_kk)
29+{
30+ pack_A_tile_int8(A, AT, i, max_ii, k, max_kk);
31+}
32+
33+void transpose_pack_A_tile_int8_i8mm(const Mat& A, Mat& AT, int i, int max_ii, int k, int max_kk)
34+{
35+ transpose_pack_A_tile_int8(A, AT, i, max_ii, k, max_kk);
36+}
37+
38+void pack_B_tile_int8_i8mm(const Mat& B, Mat& BT, int j, int max_jj, int k, int max_kk)
39+{
40+ pack_B_tile_int8(B, BT, j, max_jj, k, max_kk);
41+}
42+
43+void transpose_pack_B_tile_int8_i8mm(const Mat& B, Mat& BT, int j, int max_jj, int k, int max_kk)
44+{
45+ transpose_pack_B_tile_int8(B, BT, j, max_jj, k, max_kk);
46+}
47+
48+void pack_A_tile_fp32_to_int8_i8mm(const Mat& A, Mat& AT, int i, int max_ii, int k, int max_kk, const Mat& scales)
49+{
50+ pack_A_tile_fp32_to_int8(A, AT, i, max_ii, k, max_kk, scales);
51+}
52+
53+void transpose_pack_A_tile_fp32_to_int8_i8mm(const Mat& A, Mat& AT, int i, int max_ii, int k, int max_kk, const Mat& scales)
54+{
55+ transpose_pack_A_tile_fp32_to_int8(A, AT, i, max_ii, k, max_kk, scales);
56+}
57+
58+void pack_B_tile_fp32_to_int8_i8mm(const Mat& B, Mat& BT, int j, int max_jj, int k, int max_kk, float scale)
59+{
60+ pack_B_tile_fp32_to_int8(B, BT, j, max_jj, k, max_kk, scale);
61+}
62+
63+void transpose_pack_B_tile_fp32_to_int8_i8mm(const Mat& B, Mat& BT, int j, int max_jj, int k, int max_kk, float scale)
64+{
65+ transpose_pack_B_tile_fp32_to_int8(B, BT, j, max_jj, k, max_kk, scale);
66+}
67+
68+void gemm_transB_packed_tile_int8_i8mm(const Mat& AT_tile, const Mat& BT_tile, Mat& topT_tile, int i, int max_ii, int j, int max_jj, int k, int max_kk)
69+{
70+ gemm_transB_packed_tile_int8(AT_tile, BT_tile, topT_tile, i, max_ii, j, max_jj, k, max_kk);
71+}
72+
73+void pack_A_tile_fp16_to_int8_i8mm(const Mat& A, Mat& AT, int i, int max_ii, int k, int max_kk, const Mat& scales)
74+{
75+ pack_A_tile_fp16_to_int8(A, AT, i, max_ii, k, max_kk, scales);
76+}
77+
78+void transpose_pack_A_tile_fp16_to_int8_i8mm(const Mat& A, Mat& AT, int i, int max_ii, int k, int max_kk, const Mat& scales)
79+{
80+ transpose_pack_A_tile_fp16_to_int8(A, AT, i, max_ii, k, max_kk, scales);
81+}
82+
83+void pack_B_tile_fp16_to_int8_i8mm(const Mat& B, Mat& BT, int j, int max_jj, int k, int max_kk, float scale)
84+{
85+ pack_B_tile_fp16_to_int8(B, BT, j, max_jj, k, max_kk, scale);
86+}
87+
88+void transpose_pack_B_tile_fp16_to_int8_i8mm(const Mat& B, Mat& BT, int j, int max_jj, int k, int max_kk, float scale)
89+{
90+ transpose_pack_B_tile_fp16_to_int8(B, BT, j, max_jj, k, max_kk, scale);
91+}
92+
93+#if NCNN_BF16
94+void pack_A_tile_bf16_to_int8_i8mm(const Mat& A, Mat& AT, int i, int max_ii, int k, int max_kk, const Mat& scales)
95+{
96+ pack_A_tile_bf16_to_int8(A, AT, i, max_ii, k, max_kk, scales);
97+}
98+
99+void transpose_pack_A_tile_bf16_to_int8_i8mm(const Mat& A, Mat& AT, int i, int max_ii, int k, int max_kk, const Mat& scales)
100+{
101+ transpose_pack_A_tile_bf16_to_int8(A, AT, i, max_ii, k, max_kk, scales);
102+}
103+
104+void pack_B_tile_bf16_to_int8_i8mm(const Mat& B, Mat& BT, int j, int max_jj, int k, int max_kk, float scale)
105+{
106+ pack_B_tile_bf16_to_int8(B, BT, j, max_jj, k, max_kk, scale);
107+}
108+
109+void transpose_pack_B_tile_bf16_to_int8_i8mm(const Mat& B, Mat& BT, int j, int max_jj, int k, int max_kk, float scale)
110+{
111+ transpose_pack_B_tile_bf16_to_int8(B, BT, j, max_jj, k, max_kk, scale);
112+}
113+#endif // NCNN_BF16
114+
115+} // namespace ncnn
modified src/layer/arm/gemm_arm_vfpv4.cpp +51 −0
@@ -27,6 +27,10 @@
2727 #include "gemm_bf16s_fp16s.h"
2828 #include "gemm_fp16s.h"
2929
30+#if NCNN_INT8
31+#include "gemm_int8_fp16s.h"
32+#endif
33+
3034 extern void pack_A_tile(const Mat& A, Mat& AT, int i, int max_ii, int k, int max_kk);
3135
3236 static int gemm_arm_fp16s(const Mat& A, const Mat& B, const Mat& C, Mat& top_blob, int broadcast_type_C, int transA, int transB, int output_transpose, float alpha, int constant_TILE_M, int constant_TILE_N, int constant_TILE_K, int nT, const Option& opt)
@@ -709,4 +713,51 @@
709713 return ret;
710714 }
711715
716+#if NCNN_INT8
717+void compute_A_tile_fp16_int8_scales_vfpv4(const Mat& A, Mat& scales, float B_scale, Mat& out_descales, int i, int max_ii)
718+{
719+ compute_A_tile_fp16_int8_scales(A, scales, B_scale, out_descales, i, max_ii);
720+}
721+
722+void transpose_compute_A_tile_fp16_int8_scales_vfpv4(const Mat& A, Mat& scales, float B_scale, Mat& out_descales, int i, int max_ii)
723+{
724+ transpose_compute_A_tile_fp16_int8_scales(A, scales, B_scale, out_descales, i, max_ii);
725+}
726+
727+void pack_A_tile_fp16_to_int8_vfpv4(const Mat& A, Mat& AT, int i, int max_ii, int k, int max_kk, const Mat& scales)
728+{
729+ pack_A_tile_fp16_to_int8(A, AT, i, max_ii, k, max_kk, scales);
730+}
731+
732+void transpose_pack_A_tile_fp16_to_int8_vfpv4(const Mat& A, Mat& AT, int i, int max_ii, int k, int max_kk, const Mat& scales)
733+{
734+ transpose_pack_A_tile_fp16_to_int8(A, AT, i, max_ii, k, max_kk, scales);
735+}
736+
737+void compute_B_fp16_int8_scale_vfpv4(const Mat& B, float& scale)
738+{
739+ compute_B_fp16_int8_scale(B, scale);
740+}
741+
742+void pack_B_tile_fp16_to_int8_vfpv4(const Mat& B, Mat& BT, int j, int max_jj, int k, int max_kk, float scale)
743+{
744+ pack_B_tile_fp16_to_int8(B, BT, j, max_jj, k, max_kk, scale);
745+}
746+
747+void transpose_pack_B_tile_fp16_to_int8_vfpv4(const Mat& B, Mat& BT, int j, int max_jj, int k, int max_kk, float scale)
748+{
749+ transpose_pack_B_tile_fp16_to_int8(B, BT, j, max_jj, k, max_kk, scale);
750+}
751+
752+void unpack_output_tile_int32_to_fp16_vfpv4(const Mat& topT, const Mat& C, Mat& top_blob, int broadcast_type_C, int i, int max_ii, int j, int max_jj, const Mat& descales, float alpha, float beta)
753+{
754+ unpack_output_tile_int32_to_fp16(topT, C, top_blob, broadcast_type_C, i, max_ii, j, max_jj, descales, alpha, beta);
755+}
756+
757+void transpose_unpack_output_tile_int32_to_fp16_vfpv4(const Mat& topT, const Mat& C, Mat& top_blob, int broadcast_type_C, int i, int max_ii, int j, int max_jj, const Mat& descales, float alpha, float beta)
758+{
759+ transpose_unpack_output_tile_int32_to_fp16(topT, C, top_blob, broadcast_type_C, i, max_ii, j, max_jj, descales, alpha, beta);
760+}
761+#endif // NCNN_INT8
762+
712763 } // namespace ncnn
added src/layer/arm/gemm_int8.h +0 −0

Not shown: the file is larger than the hub shows, or this page holds as much as it shows. View the file.

added src/layer/arm/gemm_int8_bf16s.h +0 −0

Not shown: the file is larger than the hub shows, or this page holds as much as it shows. View the file.

added src/layer/arm/gemm_int8_fp16s.h +0 −0

Not shown: the file is larger than the hub shows, or this page holds as much as it shows. View the file.

modified src/layer/gemm.cpp +393 −58
@@ -39,10 +39,19 @@
3939 output_elempack = pd.get(12, 0);
4040 output_elemtype = pd.get(13, 0);
4141 output_transpose = pd.get(14, 0);
42+ int8_scale_term = pd.get(18, 0);
4243 constant_TILE_M = pd.get(20, 0);
4344 constant_TILE_N = pd.get(21, 0);
4445 constant_TILE_K = pd.get(22, 0);
4546
47+ if (int8_scale_term)
48+ {
49+#if !NCNN_INT8
50+ NCNN_LOGE("please build ncnn with NCNN_INT8 enabled for int8 inference");
51+ return -1;
52+#endif
53+ }
54+
4655 if (constantA == 1 && (constantM == 0 || constantK == 0))
4756 {
4857 NCNN_LOGE("constantM and constantK must be non-zero when constantA enabled");
@@ -111,9 +120,175 @@
111120 return -100;
112121 }
113122
123+#if NCNN_INT8
124+ if (int8_scale_term)
125+ {
126+ if (constantA == 1)
127+ {
128+ A_data_int8_scales = mb.load(constantM, 1);
129+ }
130+
131+ if (constantB == 1)
132+ {
133+ B_data_int8_scale = mb.load(1, 1)[0];
134+ }
135+ }
136+#endif // NCNN_INT8
137+
114138 return 0;
115139 }
116140
141+static void gemm_transB(const Mat& A, const Mat& BT, const Mat& C, Mat& top_blob, float alpha, float beta, int broadcast_type_C, int output_transpose, const Option& opt)
142+{
143+ const int M = A.dims == 3 ? A.c : A.h;
144+ const int N = BT.dims == 3 ? BT.c : BT.h;
145+ const int K = A.w; // assert A.w == BT.w
146+
147+ #pragma omp parallel for num_threads(opt.num_threads)
148+ for (int i = 0; i < M; i++)
149+ {
150+ const int out_hstep = top_blob.dims == 3 ? (int)top_blob.cstep : top_blob.w;
151+
152+ const int A_hstep = A.dims == 3 ? (int)A.cstep : A.w;
153+ const int BT_hstep = BT.dims == 3 ? (int)BT.cstep : BT.w;
154+
155+ const float* ptrA = (const float*)A + i * A_hstep;
156+ const float* ptrC = C;
157+
158+ for (int j = 0; j < N; j++)
159+ {
160+ const float* ptrBT = (const float*)BT + j * BT_hstep;
161+
162+ float sum = 0.f;
163+ if (ptrC)
164+ {
165+ if (broadcast_type_C == 0)
166+ {
167+ sum = ptrC[0];
168+ }
169+ if (broadcast_type_C == 1)
170+ {
171+ sum = ptrC[i];
172+ }
173+ if (broadcast_type_C == 2)
174+ {
175+ sum = ptrC[i];
176+ }
177+ if (broadcast_type_C == 3)
178+ {
179+ sum = ptrC[i * N + j];
180+ }
181+ if (broadcast_type_C == 4)
182+ {
183+ sum = ptrC[j];
184+ }
185+
186+ sum *= beta;
187+ }
188+
189+ for (int k = 0; k < K; k++)
190+ {
191+ sum += ptrA[k] * ptrBT[k];
192+ }
193+
194+ sum *= alpha;
195+
196+ if (output_transpose)
197+ {
198+ top_blob[j * out_hstep + i] = sum;
199+ }
200+ else
201+ {
202+ top_blob[i * out_hstep + j] = sum;
203+ }
204+ }
205+ }
206+}
207+
208+#if NCNN_INT8
209+static inline signed char float2int8(float v)
210+{
211+ int int32 = static_cast<int>(round(v));
212+ if (int32 > 127) return 127;
213+ if (int32 < -127) return -127;
214+ return (signed char)int32;
215+}
216+
217+static void gemm_transB_int8(const Mat& A_int8, const Mat& BT_int8, const Mat& A_int8_scales, float BT_int8_scale, const Mat& C, Mat& top_blob, float alpha, float beta, int broadcast_type_C, int output_transpose, const Option& opt)
218+{
219+ const int M = A_int8.h;
220+ const int N = BT_int8.h;
221+ const int K = A_int8.w; // assert A_int8.w == BT_int8.w
222+
223+ // NCNN_LOGE("naive ds %f %f", A_int8_scales[0], BT_int8_scale);
224+
225+ // #pragma omp parallel for num_threads(opt.num_threads)
226+ for (int i = 0; i < M; i++)
227+ {
228+ const int out_hstep = top_blob.dims == 3 ? (int)top_blob.cstep : top_blob.w;
229+
230+ const signed char* ptrA = A_int8.row<const signed char>(i);
231+ const float* ptrC = C;
232+
233+ const float descale = 1.f / (A_int8_scales[i] * BT_int8_scale);
234+
235+ // NCNN_LOGE("descale %f", descale);
236+
237+ for (int j = 0; j < N; j++)
238+ {
239+ const signed char* ptrBT = BT_int8.row<const signed char>(j);
240+
241+ int sum = 0;
242+ for (int k = 0; k < K; k++)
243+ {
244+ // NCNN_LOGE("ptrA[%d] %d", k, ptrA[k]);
245+ sum += ptrA[k] * ptrBT[k];
246+ }
247+
248+ float sum_fp32 = sum * descale;
249+
250+ if (ptrC)
251+ {
252+ float c = 0.f;
253+ if (broadcast_type_C == 0)
254+ {
255+ c = ptrC[0];
256+ }
257+ if (broadcast_type_C == 1)
258+ {
259+ c = ptrC[i];
260+ }
261+ if (broadcast_type_C == 2)
262+ {
263+ c = ptrC[i];
264+ }
265+ if (broadcast_type_C == 3)
266+ {
267+ c = ptrC[i * N + j];
268+ }
269+ if (broadcast_type_C == 4)
270+ {
271+ c = ptrC[j];
272+ }
273+
274+ sum_fp32 += c * beta;
275+ }
276+
277+ sum_fp32 *= alpha;
278+
279+ if (output_transpose)
280+ {
281+ top_blob[j * out_hstep + i] = sum_fp32;
282+ }
283+ else
284+ {
285+ top_blob[i * out_hstep + j] = sum_fp32;
286+ }
287+ }
288+ }
289+}
290+#endif // NCNN_INT8
291+
117292 int Gemm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
118293 {
119294 std::vector<Mat> bottom_blobs(1, bottom_blob);
@@ -125,6 +300,13 @@
125300
126301 int Gemm::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
127302 {
303+#if NCNN_INT8
304+ if (int8_scale_term)
305+ {
306+ return forward_int8(bottom_blobs, top_blobs, opt);
307+ }
308+#endif // NCNN_INT8
309+
128310 const Mat& A0 = constantA ? A_data : bottom_blobs[0];
129311 const Mat& B0 = constantB ? B_data : constantA ? bottom_blobs[0] : bottom_blobs[1];
130312
@@ -152,18 +334,18 @@
152334 }
153335 }
154336
155- Mat B;
337+ Mat BT;
156338 if (transB == 0)
157339 {
158340 // transpose B to col-major
159- B.create((B0.dims == 3 ? B0.c : B0.h), B0.w, elemsize, opt.workspace_allocator);
341+ BT.create((B0.dims == 3 ? B0.c : B0.h), B0.w, elemsize, opt.workspace_allocator);
160342
161343 const int B0_hstep = B0.dims == 3 ? (int)B0.cstep : B0.w;
162344
163- for (int i = 0; i < B.h; i++)
345+ for (int i = 0; i < BT.h; i++)
164346 {
165- float* ptr = B.row(i);
166- for (int j = 0; j < B.w; j++)
347+ float* ptr = BT.row(i);
348+ for (int j = 0; j < BT.w; j++)
167349 {
168350 ptr[j] = B0[j * B0_hstep + i];
169351 }
@@ -171,43 +353,36 @@
171353 }
172354 else
173355 {
174- B = B0;
356+ BT = B0;
175357 }
176358
177359 const int M = A.dims == 3 ? A.c : A.h;
178- const int K = A.w; // assert A.w == B.w
179- const int N = B.dims == 3 ? B.c : B.h;
360+ const int N = BT.dims == 3 ? BT.c : BT.h;
180361
181- const float* ptrC = 0;
362+ Mat C;
182363 int broadcast_type_C = 0;
183364 if (constantC)
184365 {
185- ptrC = C_data;
366+ C = C_data;
186367 broadcast_type_C = constant_broadcast_type_C;
187368 }
188369 else
189370 {
190- if (constantA && constantB)
371+ if (constantA && constantB && bottom_blobs.size() == 1)
191372 {
192- ptrC = bottom_blobs.size() == 1 ? bottom_blobs[0] : 0;
373+ C = bottom_blobs[0];
193374 }
194- else if (constantA)
375+ else if ((constantA || constantB) && bottom_blobs.size() == 2)
195376 {
196- ptrC = bottom_blobs.size() == 2 ? bottom_blobs[1] : 0;
377+ C = bottom_blobs[1];
197378 }
198- else if (constantB)
379+ else if (bottom_blobs.size() == 3)
199380 {
200- ptrC = bottom_blobs.size() == 2 ? bottom_blobs[1] : 0;
201- }
202- else
203- {
204- ptrC = bottom_blobs.size() == 3 ? bottom_blobs[2] : 0;
381+ C = bottom_blobs[2];
205382 }
206383
207- if (ptrC)
384+ if (!C.empty())
208385 {
209- const Mat& C = bottom_blobs[bottom_blobs.size() - 1];
210-
211386 if (C.dims == 1 && C.w == 1)
212387 {
213388 // scalar
@@ -260,66 +435,226 @@
260435 if (top_blob.empty())
261436 return -100;
262437
263- #pragma omp parallel for num_threads(opt.num_threads)
264- for (int i = 0; i < M; i++)
265- {
266- const int out_hstep = top_blob.dims == 3 ? (int)top_blob.cstep : top_blob.w;
438+ gemm_transB(A, BT, C, top_blob, alpha, beta, broadcast_type_C, output_transpose, opt);
267439
268- const int A_hstep = A.dims == 3 ? (int)A.cstep : A.w;
269- const int B_hstep = B.dims == 3 ? (int)B.cstep : B.w;
440+ return 0;
441+}
270442
271- const float* ptrA = (const float*)A + i * A_hstep;
443+#if NCNN_INT8
444+int Gemm::forward_int8(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
445+{
446+ const Mat& A0 = constantA ? A_data : bottom_blobs[0];
447+ const Mat& B0 = constantB ? B_data : constantA ? bottom_blobs[0] : bottom_blobs[1];
272448
273- for (int j = 0; j < N; j++)
449+ Mat A;
450+ if (transA == 0)
451+ {
452+ A = A0;
453+ }
454+ else
455+ {
456+ // transpose A to row-major
457+ if (A0.elemsize == 1)
274458 {
275- const float* ptrB = (const float*)B + j * B_hstep;
459+ A.create(A0.h, A0.w, (size_t)1u, 1, opt.workspace_allocator);
276460
277- float sum = 0.f;
278- if (ptrC)
461+ for (int i = 0; i < A.h; i++)
279462 {
280- if (broadcast_type_C == 0)
281- {
282- sum = ptrC[0];
283- }
284- if (broadcast_type_C == 1)
463+ signed char* ptr = A.row<signed char>(i);
464+ for (int j = 0; j < A.w; j++)
285465 {
286- sum = ptrC[i];
287- }
288- if (broadcast_type_C == 2)
289- {
290- sum = ptrC[i];
291- }
292- if (broadcast_type_C == 3)
293- {
294- sum = ptrC[i * N + j];
466+ ptr[j] = A0.row<const signed char>(j)[i];
295467 }
296- if (broadcast_type_C == 4)
468+ }
469+ }
470+ else
471+ {
472+ A.create(A0.dims == 3 ? A0.c : A0.h, A0.w, (size_t)4u, 1, opt.workspace_allocator);
473+
474+ for (int i = 0; i < A.h; i++)
475+ {
476+ float* ptr = A.row(i);
477+ for (int j = 0; j < A.w; j++)
297478 {
298- sum = ptrC[j];
479+ ptr[j] = A0.dims == 3 ? A0.channel(j)[i] : A0.row(j)[i];
299480 }
481+ }
482+ }
483+ }
300484
301- sum *= beta;
485+ // dynamic quantize A
486+ Mat A_int8 = A;
487+ Mat A_int8_scales = A_data_int8_scales;
488+ if (A_int8.elemsize != 1)
489+ {
490+ A_int8.create(A.w, A.dims == 3 ? A.c : A.h, (size_t)1u, 1, opt.workspace_allocator);
491+ A_int8_scales.create(A_int8.h, (size_t)4u, 1, opt.workspace_allocator);
492+
493+ for (int i = 0; i < A_int8.h; i++)
494+ {
495+ const int A_hstep = A.dims == 3 ? (int)A.cstep : A.w;
496+ const float* ptr = (const float*)A + i * A_hstep;
497+
498+ float absmax = 0.f;
499+ for (int k = 0; k < A_int8.w; k++)
500+ {
501+ absmax = std::max(absmax, (float)fabs(ptr[k]));
302502 }
303503
304- for (int k = 0; k < K; k++)
504+ // NCNN_LOGE("A[%d] absmax %f", i, absmax);
505+
506+ float A_int8_scale = absmax == 0.f ? 1.f : 127.f / absmax;
507+ A_int8_scales[i] = A_int8_scale;
508+
509+ signed char* ptrAi = A_int8.row<signed char>(i);
510+
511+ for (int k = 0; k < A_int8.w; k++)
305512 {
306- sum += ptrA[k] * ptrB[k];
513+ ptrAi[k] = float2int8(ptr[k] * A_int8_scale);
307514 }
515+ }
516+ }
308517
309- sum *= alpha;
518+ // dynamic quantize B
519+ Mat B0_int8 = B0;
520+ float B_int8_scale = B_data_int8_scale;
521+ if (B0_int8.elemsize != 1)
522+ {
523+ B0_int8.create(B0.w, B0.dims == 3 ? B0.c : B0.h, (size_t)1u, 1, opt.workspace_allocator);
310524
311- if (output_transpose)
525+ float absmax = 0.f;
526+ for (int i = 0; i < B0_int8.h; i++)
527+ {
528+ const int B_hstep = B0.dims == 3 ? (int)B0.cstep : B0.w;
529+ const float* ptr = (const float*)B0 + i * B_hstep;
530+
531+ for (int k = 0; k < B0_int8.w; k++)
312532 {
313- top_blob[j * out_hstep + i] = sum;
533+ absmax = std::max(absmax, (float)fabs(ptr[k]));
314534 }
315- else
535+ }
536+
537+ // NCNN_LOGE("B0 absmax %f", absmax);
538+
539+ B_int8_scale = absmax == 0.f ? 1.f : 127.f / absmax;
540+
541+ for (int i = 0; i < B0_int8.h; i++)
542+ {
543+ const int B_hstep = B0.dims == 3 ? (int)B0.cstep : B0.w;
544+ const float* ptr = (const float*)B0 + i * B_hstep;
545+
546+ signed char* ptrBi = B0_int8.row<signed char>(i);
547+
548+ for (int k = 0; k < B0_int8.w; k++)
316549 {
317- top_blob[i * out_hstep + j] = sum;
550+ ptrBi[k] = float2int8(ptr[k] * B_int8_scale);
551+ }
552+ }
553+ }
554+
555+ Mat BT_int8;
556+ if (transB == 0)
557+ {
558+ // transpose B to col-major
559+ BT_int8.create(B0_int8.h, B0_int8.w, (size_t)1u, 1, opt.workspace_allocator);
560+
561+ for (int i = 0; i < BT_int8.h; i++)
562+ {
563+ signed char* ptr = BT_int8.row<signed char>(i);
564+ for (int j = 0; j < BT_int8.w; j++)
565+ {
566+ ptr[j] = B0_int8.row<const signed char>(j)[i];
567+ }
568+ }
569+ }
570+ else
571+ {
572+ BT_int8 = B0_int8;
573+ }
574+
575+ const int M = A_int8.h;
576+ const int N = BT_int8.h;
577+
578+ Mat C;
579+ int broadcast_type_C = 0;
580+ if (constantC)
581+ {
582+ C = C_data;
583+ broadcast_type_C = constant_broadcast_type_C;
584+ }
585+ else
586+ {
587+ if (constantA && constantB && bottom_blobs.size() == 1)
588+ {
589+ C = bottom_blobs[0];
590+ }
591+ else if ((constantA || constantB) && bottom_blobs.size() == 2)
592+ {
593+ C = bottom_blobs[1];
594+ }
595+ else if (bottom_blobs.size() == 3)
596+ {
597+ C = bottom_blobs[2];
598+ }
599+
600+ if (!C.empty())
601+ {
602+ if (C.dims == 1 && C.w == 1)
603+ {
604+ // scalar
605+ broadcast_type_C = 0;
606+ }
607+ if (C.dims == 1 && C.w == M)
608+ {
609+ // M
610+ // auto broadcast from h to w is the ncnn-style convention
611+ broadcast_type_C = 1;
612+ }
613+ if (C.dims == 1 && C.w == N)
614+ {
615+ // N
616+ broadcast_type_C = 4;
617+ }
618+ if (C.dims == 2 && C.w == 1 && C.h == M)
619+ {
620+ // Mx1
621+ broadcast_type_C = 2;
622+ }
623+ if (C.dims == 2 && C.w == N && C.h == M)
624+ {
625+ // MxN
626+ broadcast_type_C = 3;
627+ }
628+ if (C.dims == 2 && C.w == N && C.h == 1)
629+ {
630+ // 1xN
631+ broadcast_type_C = 4;
318632 }
319633 }
320634 }
321635
636+ Mat& top_blob = top_blobs[0];
637+ if (output_transpose)
638+ {
639+ if (output_N1M)
640+ top_blob.create(M, 1, N, 4u, opt.blob_allocator);
641+ else
642+ top_blob.create(M, N, 4u, opt.blob_allocator);
643+ }
644+ else
645+ {
646+ if (output_N1M)
647+ top_blob.create(N, 1, M, 4u, opt.blob_allocator);
648+ else
649+ top_blob.create(N, M, 4u, opt.blob_allocator);
650+ }
651+ if (top_blob.empty())
652+ return -100;
653+
654+ gemm_transB_int8(A_int8, BT_int8, A_int8_scales, B_int8_scale, C, top_blob, alpha, beta, broadcast_type_C, output_transpose, opt);
655+
322656 return 0;
323657 }
658+#endif // NCNN_INT8
324659
325660 } // namespace ncnn
modified src/layer/gemm.h +12 −0
@@ -32,6 +32,11 @@
3232
3333 virtual int forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const;
3434
35+protected:
36+#if NCNN_INT8
37+ int forward_int8(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const;
38+#endif
39+
3540 public:
3641 float alpha;
3742 float beta;
@@ -50,6 +55,8 @@
5055 int output_elemtype; // 0=auto 1=fp32
5156 int output_transpose;
5257
58+ int int8_scale_term;
59+
5360 int constant_TILE_M;
5461 int constant_TILE_N;
5562 int constant_TILE_K;
@@ -58,6 +65,11 @@
5865 Mat A_data;
5966 Mat B_data;
6067 Mat C_data;
68+
69+#if NCNN_INT8
70+ Mat A_data_int8_scales;
71+ float B_data_int8_scale;
72+#endif
6173 };
6274
6375 } // namespace ncnn
modified src/layer/riscv/gemm_riscv.cpp +15 −0
@@ -3947,6 +3947,14 @@
39473947
39483948 int Gemm_riscv::create_pipeline(const Option& opt)
39493949 {
3950+#if NCNN_INT8
3951+ if (int8_scale_term)
3952+ {
3953+ support_packing = false;
3954+ return 0;
3955+ }
3956+#endif
3957+
39503958 if (constantA)
39513959 {
39523960 const int M = constantM;
@@ -4070,6 +4078,13 @@
40704078
40714079 int Gemm_riscv::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
40724080 {
4081+#if NCNN_INT8
4082+ if (int8_scale_term)
4083+ {
4084+ return Gemm::forward_int8(bottom_blobs, top_blobs, opt);
4085+ }
4086+#endif
4087+
40734088 int M;
40744089 int N;
40754090 if (constantA && constantB)
modified src/layer/vulkan/gemm_vulkan.cpp +111 −54
@@ -26,6 +26,19 @@
2626 pipeline_gemm = 0;
2727 }
2828
29+int Gemm_vulkan::load_param(const ParamDict& pd)
30+{
31+ int ret = Gemm::load_param(pd);
32+
33+ if (int8_scale_term)
34+ {
35+ support_vulkan = false;
36+ support_image_storage = false;
37+ }
38+
39+ return ret;
40+}
41+
2942 int Gemm_vulkan::create_pipeline(const Option& opt)
3043 {
3144 // const Mat& shape = top_shapes.empty() ? Mat() : top_shapes[0];
@@ -169,56 +182,78 @@
169182 {
170183 const VkMat& A0 = constantA ? A_data_gpu : bottom_blobs[0];
171184 const VkMat& B0 = constantB ? B_data_gpu : constantA ? bottom_blobs[0] : bottom_blobs[1];
172- const VkMat& C0 = constantC ? C_data_gpu : bottom_blobs[bottom_blobs.size() - 1];
173185
174186 VkMat A;
175187 VkMat B;
176- VkMat C;
177188 vkdev->convert_packing(A0, A, 1, cmd, opt);
178189 vkdev->convert_packing(B0, B, 1, cmd, opt);
179- vkdev->convert_packing(C0, C, 1, cmd, opt);
180190
181191 const int M = constantM ? constantM : transA ? A.w : (A.dims == 3 ? A.c : A.h);
182192 const int K = constantK ? constantK : transA ? (A.dims == 3 ? A.c : A.h) : A.w;
183193 const int N = constantN ? constantN : transB ? (B.dims == 3 ? B.c : B.h) : B.w;
184194
185- int broadcast_type_C;
195+ VkMat C;
196+ int broadcast_type_C = -1;
186197 if (constantC)
187198 {
199+ vkdev->convert_packing(C_data_gpu, C, 1, cmd, opt);
188200 broadcast_type_C = constant_broadcast_type_C;
189201 }
190202 else
191203 {
192- if (C.dims == 1 && C.w == 1)
193- {
194- // scalar
195- broadcast_type_C = 0;
196- }
197- if (C.dims == 1 && C.w == M)
198- {
199- // M
200- // auto broadcast from h to w is the ncnn-style convention
201- broadcast_type_C = 1;
202- }
203- if (C.dims == 1 && C.w == N)
204+ VkMat C0;
205+ if (constantA && constantB)
204206 {
205- // N
206- broadcast_type_C = 4;
207+ C0 = bottom_blobs.size() == 1 ? bottom_blobs[0] : VkMat();
207208 }
208- if (C.dims == 2 && C.w == 1 && C.h == M)
209+ else if (constantA)
209210 {
210- // Mx1
211- broadcast_type_C = 2;
211+ C0 = bottom_blobs.size() == 2 ? bottom_blobs[1] : VkMat();
212212 }
213- if (C.dims == 2 && C.w == N && C.h == M)
213+ else if (constantB)
214214 {
215- // MxN
216- broadcast_type_C = 3;
215+ C0 = bottom_blobs.size() == 2 ? bottom_blobs[1] : VkMat();
217216 }
218- if (C.dims == 2 && C.w == N && C.h == 1)
217+ else
219218 {
220- // 1xN
221- broadcast_type_C = 4;
219+ C0 = bottom_blobs.size() == 3 ? bottom_blobs[2] : VkMat();
220+ }
221+
222+ if (!C0.empty())
223+ {
224+ vkdev->convert_packing(C0, C, 1, cmd, opt);
225+
226+ if (C.dims == 1 && C.w == 1)
227+ {
228+ // scalar
229+ broadcast_type_C = 0;
230+ }
231+ if (C.dims == 1 && C.w == M)
232+ {
233+ // M
234+ // auto broadcast from h to w is the ncnn-style convention
235+ broadcast_type_C = 1;
236+ }
237+ if (C.dims == 1 && C.w == N)
238+ {
239+ // N
240+ broadcast_type_C = 4;
241+ }
242+ if (C.dims == 2 && C.w == 1 && C.h == M)
243+ {
244+ // Mx1
245+ broadcast_type_C = 2;
246+ }
247+ if (C.dims == 2 && C.w == N && C.h == M)
248+ {
249+ // MxN
250+ broadcast_type_C = 3;
251+ }
252+ if (C.dims == 2 && C.w == N && C.h == 1)
253+ {
254+ // 1xN
255+ broadcast_type_C = 4;
256+ }
222257 }
223258 }
224259
@@ -301,56 +336,78 @@
301336 {
302337 const VkImageMat& A0 = constantA ? A_data_gpu_image : bottom_blobs[0];
303338 const VkImageMat& B0 = constantB ? B_data_gpu_image : constantA ? bottom_blobs[0] : bottom_blobs[1];
304- const VkImageMat& C0 = constantC ? C_data_gpu_image : bottom_blobs[bottom_blobs.size() - 1];
305339
306340 VkImageMat A;
307341 VkImageMat B;
308- VkImageMat C;
309342 vkdev->convert_packing(A0, A, 1, cmd, opt);
310343 vkdev->convert_packing(B0, B, 1, cmd, opt);
311- vkdev->convert_packing(C0, C, 1, cmd, opt);
312344
313345 const int M = constantM ? constantM : transA ? A.w : (A.dims == 3 ? A.c : A.h);
314346 const int K = constantK ? constantK : transA ? (A.dims == 3 ? A.c : A.h) : A.w;
315347 const int N = constantN ? constantN : transB ? (B.dims == 3 ? B.c : B.h) : B.w;
316348
317- int broadcast_type_C;
349+ VkImageMat C;
350+ int broadcast_type_C = -1;
318351 if (constantC)
319352 {
353+ vkdev->convert_packing(C_data_gpu_image, C, 1, cmd, opt);
320354 broadcast_type_C = constant_broadcast_type_C;
321355 }
322356 else
323357 {
324- if (C.dims == 1 && C.w == 1)
325- {
326- // scalar
327- broadcast_type_C = 0;
328- }
329- if (C.dims == 1 && C.w == M)
358+ VkImageMat C0;
359+ if (constantA && constantB)
330360 {
331- // M
332- // auto broadcast from h to w is the ncnn-style convention
333- broadcast_type_C = 1;
361+ C0 = bottom_blobs.size() == 1 ? bottom_blobs[0] : VkImageMat();
334362 }
335- if (C.dims == 1 && C.w == N)
363+ else if (constantA)
336364 {
337- // N
338- broadcast_type_C = 4;
365+ C0 = bottom_blobs.size() == 2 ? bottom_blobs[1] : VkImageMat();
339366 }
340- if (C.dims == 2 && C.w == 1 && C.h == M)
367+ else if (constantB)
341368 {
342- // Mx1
343- broadcast_type_C = 2;
369+ C0 = bottom_blobs.size() == 2 ? bottom_blobs[1] : VkImageMat();
344370 }
345- if (C.dims == 2 && C.w == N && C.h == M)
346- {
347- // MxN
348- broadcast_type_C = 3;
349- }
350- if (C.dims == 2 && C.w == N && C.h == 1)
371+ else
351372 {
352- // 1xN
353- broadcast_type_C = 4;
373+ C0 = bottom_blobs.size() == 3 ? bottom_blobs[2] : VkImageMat();
374+ }
375+
376+ if (!C0.empty())
377+ {
378+ vkdev->convert_packing(C0, C, 1, cmd, opt);
379+
380+ if (C.dims == 1 && C.w == 1)
381+ {
382+ // scalar
383+ broadcast_type_C = 0;
384+ }
385+ if (C.dims == 1 && C.w == M)
386+ {
387+ // M
388+ // auto broadcast from h to w is the ncnn-style convention
389+ broadcast_type_C = 1;
390+ }
391+ if (C.dims == 1 && C.w == N)
392+ {
393+ // N
394+ broadcast_type_C = 4;
395+ }
396+ if (C.dims == 2 && C.w == 1 && C.h == M)
397+ {
398+ // Mx1
399+ broadcast_type_C = 2;
400+ }
401+ if (C.dims == 2 && C.w == N && C.h == M)
402+ {
403+ // MxN
404+ broadcast_type_C = 3;
405+ }
406+ if (C.dims == 2 && C.w == N && C.h == 1)
407+ {
408+ // 1xN
409+ broadcast_type_C = 4;
410+ }
354411 }
355412 }
356413
modified src/layer/vulkan/gemm_vulkan.h +2 −0
@@ -24,6 +24,8 @@
2424 public:
2525 Gemm_vulkan();
2626
27+ virtual int load_param(const ParamDict& pd);
28+
2729 virtual int create_pipeline(const Option& opt);
2830 virtual int destroy_pipeline(const Option& opt);
2931
modified src/layer/x86/gemm_x86.cpp +0 −0

Not shown: the file is larger than the hub shows, or this page holds as much as it shows. View the file.

modified tests/test_gemm.cpp +1 −1
@@ -300,7 +300,7 @@
300300 || test_gemm_1(M, N, K);
301301
302302 if (ret != 0)
303- return 0;
303+ return ret;
304304 }
305305
306306 return 0;
modified tests/test_gemm_1.cpp +2 −2
@@ -120,13 +120,13 @@
120120
121121 int ret = test_gemm_0(M, N, K, TILE_M, TILE_N, TILE_K);
122122 if (ret != 0)
123- return 0;
123+ return ret;
124124 }
125125
126126 // test no tiling
127127 int ret = test_gemm_0(M, N, K, 100, 100, 100);
128128 if (ret != 0)
129- return 0;
129+ return ret;
130130 }
131131
132132 return 0;
added tests/test_gemm_3.cpp +336 −0
@@ -0,0 +1,336 @@
1+// Tencent is pleased to support the open source community by making ncnn available.
2+//
3+// Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
4+//
5+// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6+// in compliance with the License. You may obtain a copy of the License at
7+//
8+// https://opensource.org/licenses/BSD-3-Clause
9+//
10+// Unless required by applicable law or agreed to in writing, software distributed
11+// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12+// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13+// specific language governing permissions and limitations under the License.
14+
15+#include "testutil.h"
16+
17+#if NCNN_INT8
18+static int test_gemm_int8(int M, int N, int K, float alpha, int transA, int transB, int output_elemtype, int output_transpose, int constantA, int constantB, int output_N1M)
19+{
20+ ncnn::ParamDict pd;
21+ pd.set(0, alpha);
22+ pd.set(1, 1.f); // beta
23+ pd.set(2, transA);
24+ pd.set(3, transB);
25+ pd.set(4, constantA);
26+ pd.set(5, constantB);
27+ pd.set(6, 1);
28+ pd.set(7, M);
29+ pd.set(8, N);
30+ pd.set(9, K);
31+ pd.set(10, -1);
32+ pd.set(11, output_N1M);
33+ pd.set(13, output_elemtype);
34+ pd.set(14, output_transpose);
35+ pd.set(18, 2); // int8_scale_term
36+
37+ std::vector<ncnn::Mat> weights;
38+ if (constantA) weights.push_back(transA ? (output_N1M ? RandomS8Mat(M, 1, K) : RandomS8Mat(M, K)) : (output_N1M ? RandomS8Mat(K, 1, M) : RandomS8Mat(K, M)));
39+ if (constantB) weights.push_back(transB ? (output_N1M ? RandomS8Mat(K, 1, N) : RandomS8Mat(K, N)) : (output_N1M ? RandomS8Mat(N, 1, K) : RandomS8Mat(N, K)));
40+ if (constantA) weights.push_back(RandomMat(M, 10.f, 20.f));
41+ if (constantB) weights.push_back(RandomMat(1, 10.f, 20.f));
42+
43+ std::vector<ncnn::Mat> a;
44+ if (!constantA) a.push_back(transA ? (output_N1M ? ncnn::Mat(M, 1, K) : ncnn::Mat(M, K)) : (output_N1M ? ncnn::Mat(K, 1, M) : ncnn::Mat(K, M)));
45+ if (!constantB) a.push_back(transB ? (output_N1M ? ncnn::Mat(K, 1, N) : ncnn::Mat(K, N)) : (output_N1M ? ncnn::Mat(N, 1, K) : ncnn::Mat(N, K)));
46+
47+ for (size_t i = 0; i < a.size(); i++)
48+ {
49+ Randomize(a[i], -10.f, 10.f);
50+ }
51+
52+ int ret = test_layer("Gemm", pd, weights, a);
53+ if (ret != 0)
54+ {
55+ fprintf(stderr, "test_gemm_int8 failed M=%d N=%d K=%d alpha=%f transA=%d transB=%d output_elemtype=%d output_transpose=%d constantA=%d constantB=%d output_N1M=%d\n", M, N, K, alpha, transA, transB, output_elemtype, output_transpose, constantA, constantB, output_N1M);
56+ }
57+
58+ return ret;
59+}
60+
61+static int test_gemm_int8_bias(int M, int N, int K, const ncnn::Mat& C, float alpha, float beta, int transA, int transB, int output_elemtype, int output_transpose, int constantA, int constantB, int constantC)
62+{
63+ int broadcast_type_C = 0;
64+ if (C.dims == 1 && C.w == 1)
65+ {
66+ // scalar
67+ broadcast_type_C = 0;
68+ }
69+ if (C.dims == 1 && C.w == M)
70+ {
71+ // M
72+ // auto broadcast from h to w is the ncnn-style convention
73+ broadcast_type_C = 1;
74+ }
75+ if (C.dims == 1 && C.w == N)
76+ {
77+ // N
78+ broadcast_type_C = 4;
79+ }
80+ if (C.dims == 2 && C.w == 1 && C.h == M)
81+ {
82+ // Mx1
83+ broadcast_type_C = 2;
84+ }
85+ if (C.dims == 2 && C.w == N && C.h == M)
86+ {
87+ // MxN
88+ broadcast_type_C = 3;
89+ }
90+ if (C.dims == 2 && C.w == N && C.h == 1)
91+ {
92+ // 1xN
93+ broadcast_type_C = 4;
94+ }
95+
96+ ncnn::ParamDict pd;
97+ pd.set(0, alpha);
98+ pd.set(1, beta);
99+ pd.set(2, transA);
100+ pd.set(3, transB);
101+ pd.set(4, constantA);
102+ pd.set(5, constantB);
103+ pd.set(6, constantC);
104+ pd.set(7, M);
105+ pd.set(8, N);
106+ pd.set(9, K);
107+ pd.set(10, broadcast_type_C);
108+ // pd.set(12, 1); // output_elempack
109+ pd.set(13, output_elemtype);
110+ pd.set(14, output_transpose);
111+ pd.set(18, 2); // int8_scale_term
112+
113+ std::vector<ncnn::Mat> weights;
114+ if (constantA) weights.push_back(transA ? RandomS8Mat(M, K) : RandomS8Mat(K, M));
115+ if (constantB) weights.push_back(transB ? RandomS8Mat(K, N) : RandomS8Mat(N, K));
116+ if (constantC) weights.push_back(C);
117+ if (constantA) weights.push_back(RandomMat(M, 10.f, 20.f));
118+ if (constantB) weights.push_back(RandomMat(1, 10.f, 20.f));
119+
120+ std::vector<ncnn::Mat> a;
121+ if (!constantA) a.push_back(transA ? ncnn::Mat(M, K) : ncnn::Mat(K, M));
122+ if (!constantB) a.push_back(transB ? ncnn::Mat(K, N) : ncnn::Mat(N, K));
123+ if (!constantC) a.push_back(C);
124+
125+ for (size_t i = 0; i < a.size(); i++)
126+ {
127+ Randomize(a[i], -10.f, 10.f);
128+ }
129+
130+ int ret = test_layer("Gemm", pd, weights, a);
131+ if (ret != 0)
132+ {
133+ fprintf(stderr, "test_gemm_int8_bias failed M=%d N=%d K=%d C.dims=%d C=(%d %d %d) alpha=%f beta=%f transA=%d transB=%d output_elemtype=%d output_transpose=%d constantA=%d constantB=%d constantC=%d\n", M, N, K, C.dims, C.w, C.h, C.c, alpha, beta, transA, transB, output_elemtype, output_transpose, constantA, constantB, constantC);
134+ }
135+
136+ return ret;
137+}
138+
139+static int test_gemm_int8_fp16s(int M, int N, int K, float alpha, int transA, int transB, int output_elemtype, int output_transpose, int constantA, int constantB, int output_N1M)
140+{
141+ ncnn::ParamDict pd;
142+ pd.set(0, alpha);
143+ pd.set(1, 1.f); // beta
144+ pd.set(2, transA);
145+ pd.set(3, transB);
146+ pd.set(4, constantA);
147+ pd.set(5, constantB);
148+ pd.set(6, 1);
149+ pd.set(7, M);
150+ pd.set(8, N);
151+ pd.set(9, K);
152+ pd.set(10, -1);
153+ pd.set(11, output_N1M);
154+ pd.set(13, output_elemtype);
155+ pd.set(14, output_transpose);
156+ pd.set(18, 2); // int8_scale_term
157+
158+ std::vector<ncnn::Mat> weights;
159+ if (constantA) weights.push_back(transA ? (output_N1M ? RandomS8Mat(M, 1, K) : RandomS8Mat(M, K)) : (output_N1M ? RandomS8Mat(K, 1, M) : RandomS8Mat(K, M)));
160+ if (constantB) weights.push_back(transB ? (output_N1M ? RandomS8Mat(K, 1, N) : RandomS8Mat(K, N)) : (output_N1M ? RandomS8Mat(N, 1, K) : RandomS8Mat(N, K)));
161+ if (constantA) weights.push_back(RandomMat(M, 10.f, 20.f));
162+ if (constantB) weights.push_back(RandomMat(1, 10.f, 20.f));
163+
164+ std::vector<ncnn::Mat> a;
165+ if (!constantA) a.push_back(transA ? (output_N1M ? ncnn::Mat(M, 1, K) : ncnn::Mat(M, K)) : (output_N1M ? ncnn::Mat(K, 1, M) : ncnn::Mat(K, M)));
166+ if (!constantB) a.push_back(transB ? (output_N1M ? ncnn::Mat(K, 1, N) : ncnn::Mat(K, N)) : (output_N1M ? ncnn::Mat(N, 1, K) : ncnn::Mat(N, K)));
167+
168+ for (size_t i = 0; i < a.size(); i++)
169+ {
170+ Randomize(a[i], -10.f, 10.f);
171+ }
172+
173+ ncnn::Option opt;
174+ opt.num_threads = 1;
175+ opt.use_packing_layout = true;
176+ opt.use_fp16_packed = false;
177+ opt.use_fp16_storage = true;
178+ opt.use_fp16_arithmetic = false;
179+ opt.use_bf16_storage = false;
180+
181+ float epsilon = 0.001;
182+
183+ int ret = test_layer_opt("Gemm", pd, weights, opt, a, 1, epsilon);
184+ if (ret != 0)
185+ {
186+ fprintf(stderr, "test_gemm_int8_fp16s failed M=%d N=%d K=%d alpha=%f transA=%d transB=%d output_elemtype=%d output_transpose=%d constantA=%d constantB=%d output_N1M=%d\n", M, N, K, alpha, transA, transB, output_elemtype, output_transpose, constantA, constantB, output_N1M);
187+ return ret;
188+ }
189+
190+ return 0;
191+}
192+
193+static int test_gemm_0(int M, int N, int K)
194+{
195+ return 0
196+ || test_gemm_int8(M, N, K, 2.1f, 0, 1, 0, 0, 0, 0, 0)
197+ || test_gemm_int8(M, N, K, 3.1f, 1, 1, 0, 0, 0, 0, 0)
198+ || test_gemm_int8(M, N, K, 4.1f, 0, 0, 0, 0, 0, 0, 1)
199+ || test_gemm_int8(M, N, K, 5.1f, 1, 0, 0, 0, 0, 0, 1)
200+
201+ || test_gemm_int8(M, N, K, 0.2f, 0, 1, 0, 0, 1, 0, 1)
202+ || test_gemm_int8(M, N, K, 0.3f, 1, 1, 0, 0, 1, 0, 1)
203+ || test_gemm_int8(M, N, K, 0.4f, 0, 0, 0, 0, 0, 1, 0)
204+ || test_gemm_int8(M, N, K, 0.5f, 0, 1, 0, 0, 0, 1, 0)
205+
206+ || test_gemm_int8(M, N, K, 1.2f, 0, 1, 0, 0, 1, 1, 0)
207+ || test_gemm_int8(M, N, K, 1.3f, 1, 1, 0, 0, 1, 1, 1)
208+ || test_gemm_int8(M, N, K, 1.4f, 0, 0, 0, 0, 1, 1, 0)
209+ || test_gemm_int8(M, N, K, 1.5f, 1, 0, 0, 0, 1, 1, 1)
210+
211+ || test_gemm_int8(M, N, K, -1.2f, 0, 1, 0, 1, 0, 0, 0)
212+ || test_gemm_int8(M, N, K, -1.3f, 1, 1, 0, 1, 0, 0, 0)
213+ || test_gemm_int8(M, N, K, -1.4f, 0, 0, 0, 1, 0, 0, 1)
214+ || test_gemm_int8(M, N, K, -1.5f, 1, 0, 0, 1, 0, 0, 1)
215+
216+ || test_gemm_int8(M, N, K, -2.0f, 0, 1, 0, 1, 1, 0, 1)
217+ || test_gemm_int8(M, N, K, -3.0f, 1, 1, 0, 1, 1, 0, 1)
218+ || test_gemm_int8(M, N, K, -4.0f, 0, 0, 0, 1, 0, 1, 0)
219+ || test_gemm_int8(M, N, K, -5.0f, 0, 1, 0, 1, 0, 1, 0)
220+
221+ || test_gemm_int8(M, N, K, -2.1f, 0, 1, 0, 1, 1, 1, 0)
222+ || test_gemm_int8(M, N, K, -3.1f, 1, 1, 0, 1, 1, 1, 1)
223+ || test_gemm_int8(M, N, K, -4.1f, 0, 0, 0, 1, 1, 1, 0)
224+ || test_gemm_int8(M, N, K, -5.1f, 1, 0, 0, 1, 1, 1, 1)
225+
226+ || test_gemm_int8_fp16s(M, N, K, 1.f, 0, 1, 0, 0, 0, 0, 0)
227+ || test_gemm_int8_fp16s(M, N, K, 1.f, 1, 0, 0, 1, 0, 0, 0);
228+}
229+
230+static int test_gemm_1(int M, int N, int K)
231+{
232+ return 0
233+ || test_gemm_int8_bias(M, N, K, RandomMat(1), 2.1f, 0.5f, 0, 0, 0, 0, 0, 0, 0)
234+ || test_gemm_int8_bias(M, N, K, RandomMat(1), 2.1f, 0.5f, 0, 0, 1, 1, 0, 0, 0)
235+ || test_gemm_int8_bias(M, N, K, RandomMat(M), 3.1f, 0.6f, 0, 1, 2, 0, 0, 0, 0)
236+ || test_gemm_int8_bias(M, N, K, RandomMat(M), 3.1f, 0.6f, 0, 1, 3, 1, 0, 0, 0)
237+ || test_gemm_int8_bias(M, N, K, RandomMat(1, M), 4.1f, 0.7f, 1, 0, 0, 0, 0, 0, 0)
238+ || test_gemm_int8_bias(M, N, K, RandomMat(1, M), 4.1f, 0.7f, 1, 0, 1, 1, 0, 0, 0)
239+ || test_gemm_int8_bias(M, N, K, RandomMat(N, M), 5.1f, -0.8f, 1, 1, 2, 0, 0, 0, 0)
240+ || test_gemm_int8_bias(M, N, K, RandomMat(N, M), 5.1f, -0.8f, 1, 1, 3, 1, 0, 0, 0)
241+ || test_gemm_int8_bias(M, N, K, RandomMat(N, M), 1.f, 1.f, 1, 1, 0, 0, 0, 0, 0)
242+ || test_gemm_int8_bias(M, N, K, RandomMat(N, M), 1.f, 1.f, 1, 1, 1, 1, 0, 0, 0)
243+ || test_gemm_int8_bias(M, N, K, RandomMat(N, 1), 2.1f, -0.5f, 0, 0, 2, 0, 0, 0, 0)
244+ || test_gemm_int8_bias(M, N, K, RandomMat(N, 1), 2.1f, -0.5f, 0, 0, 3, 1, 0, 0, 0)
245+ || test_gemm_int8_bias(M, N, K, RandomMat(N, 1), 0.8f, 1.f, 0, 0, 0, 0, 0, 0, 0)
246+ || test_gemm_int8_bias(M, N, K, RandomMat(N), 0.8f, 1.f, 0, 0, 1, 1, 0, 0, 0)
247+ || test_gemm_int8_bias(M, N, K, RandomMat(N), 3.1f, -0.6f, 0, 1, 2, 0, 0, 0, 0)
248+ || test_gemm_int8_bias(M, N, K, RandomMat(N), 3.1f, -0.6f, 0, 1, 3, 1, 0, 0, 0)
249+
250+ || test_gemm_int8_bias(M, N, K, RandomMat(1), -2.1f, 0.5f, 0, 0, 0, 0, 1, 1, 1)
251+ || test_gemm_int8_bias(M, N, K, RandomMat(1), -2.1f, 0.5f, 0, 0, 1, 1, 1, 1, 1)
252+ || test_gemm_int8_bias(M, N, K, RandomMat(M), -3.1f, 0.6f, 0, 1, 2, 0, 1, 1, 1)
253+ || test_gemm_int8_bias(M, N, K, RandomMat(M), -3.1f, 0.6f, 0, 1, 3, 1, 1, 1, 1)
254+ || test_gemm_int8_bias(M, N, K, RandomMat(1, M), -4.1f, 0.7f, 1, 0, 0, 0, 1, 1, 1)
255+ || test_gemm_int8_bias(M, N, K, RandomMat(1, M), -4.1f, 0.7f, 1, 0, 1, 1, 1, 1, 1)
256+ || test_gemm_int8_bias(M, N, K, RandomMat(N, M), -5.1f, -0.8f, 1, 1, 2, 0, 1, 1, 1)
257+ || test_gemm_int8_bias(M, N, K, RandomMat(N, M), -5.1f, -0.8f, 1, 1, 3, 1, 1, 1, 1)
258+ || test_gemm_int8_bias(M, N, K, RandomMat(N, M), 1.f, 1.f, 1, 1, 0, 0, 1, 1, 1)
259+ || test_gemm_int8_bias(M, N, K, RandomMat(N, M), 1.f, 1.f, 1, 1, 1, 1, 1, 1, 1)
260+ || test_gemm_int8_bias(M, N, K, RandomMat(N, 1), -2.1f, -0.5f, 0, 0, 2, 0, 1, 1, 1)
261+ || test_gemm_int8_bias(M, N, K, RandomMat(N, 1), -2.1f, -0.5f, 0, 0, 3, 1, 1, 1, 1)
262+ || test_gemm_int8_bias(M, N, K, RandomMat(N, 1), 0.8f, 1.f, 0, 0, 0, 0, 1, 1, 1)
263+ || test_gemm_int8_bias(M, N, K, RandomMat(N), 0.8f, 1.f, 0, 0, 1, 1, 1, 1, 1)
264+ || test_gemm_int8_bias(M, N, K, RandomMat(N), -3.1f, -0.6f, 0, 1, 2, 0, 1, 1, 1)
265+ || test_gemm_int8_bias(M, N, K, RandomMat(N), -3.1f, -0.6f, 0, 1, 3, 1, 1, 1, 1);
266+}
267+#endif // NCNN_INT8
268+
269+int main()
270+{
271+ SRAND(7767517);
272+
273+#if NCNN_INT8
274+ int mnk[][3] = {
275+ {1, 1, 1},
276+ {1, 1, 23},
277+ {1, 1, 47},
278+ {1, 23, 1},
279+ {1, 23, 23},
280+ {1, 31, 1},
281+ {1, 35, 1},
282+ {1, 35, 47},
283+ {1, 47, 1},
284+ {2, 2, 2},
285+ {3, 3, 3},
286+ {4, 4, 4},
287+ {5, 5, 5},
288+ {6, 6, 6},
289+ {7, 7, 7},
290+ {7, 31, 3},
291+ {8, 8, 8},
292+ {12, 12, 23},
293+ {12, 23, 12},
294+ {12, 31, 12},
295+ {15, 15, 15},
296+ {16, 16, 16},
297+ {19, 44, 7},
298+ {20, 28, 7},
299+ {23, 31, 1},
300+ {23, 31, 23},
301+ {24, 24, 47},
302+ {24, 35, 24},
303+ {24, 47, 24},
304+ {31, 31, 31},
305+ {32, 32, 9},
306+ {35, 47, 48},
307+ {35, 48, 47},
308+ {40, 40, 40},
309+ {47, 48, 47}
310+ };
311+
312+ int mnk_count = sizeof(mnk) / sizeof(int) / 3;
313+
314+ for (int i = 0; i < mnk_count; i++)
315+ {
316+ int M = mnk[i][0];
317+ int N = mnk[i][1];
318+ int K = mnk[i][2];
319+
320+ int ret = test_gemm_0(M, N, K) || test_gemm_1(M, N, K);
321+ if (ret != 0)
322+ return ret;
323+
324+ if (M != N)
325+ {
326+ int ret = test_gemm_0(N, M, K) || test_gemm_1(N, M, K);
327+ if (ret != 0)
328+ return ret;
329+ }
330+ }
331+#else
332+ // test nothing for non-int8 build
333+#endif
334+
335+ return 0;
336+}
added tests/test_gemm_4.cpp +140 −0
@@ -0,0 +1,140 @@
1+// Tencent is pleased to support the open source community by making ncnn available.
2+//
3+// Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
4+//
5+// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6+// in compliance with the License. You may obtain a copy of the License at
7+//
8+// https://opensource.org/licenses/BSD-3-Clause
9+//
10+// Unless required by applicable law or agreed to in writing, software distributed
11+// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12+// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13+// specific language governing permissions and limitations under the License.
14+
15+#include "testutil.h"
16+
17+#if NCNN_INT8
18+static int test_gemm_int8(int M, int N, int K, int TILE_M, int TILE_N, int TILE_K, float alpha, int transA, int transB, int output_transpose)
19+{
20+ ncnn::ParamDict pd;
21+ pd.set(0, alpha);
22+ pd.set(1, 1.f); // beta
23+ pd.set(2, transA);
24+ pd.set(3, transB);
25+ pd.set(14, output_transpose);
26+ pd.set(18, 2); // int8_scale_term
27+
28+ pd.set(20, TILE_M);
29+ pd.set(21, TILE_N);
30+ pd.set(22, TILE_K);
31+
32+ std::vector<ncnn::Mat> weights(0);
33+
34+ std::vector<ncnn::Mat> a(2);
35+ a[0] = transA ? ncnn::Mat(M, K) : ncnn::Mat(K, M);
36+ a[1] = transB ? ncnn::Mat(K, N) : ncnn::Mat(N, K);
37+
38+ Randomize(a[0], -10.f, 10.f);
39+ Randomize(a[1], -10.f, 10.f);
40+
41+ int ret = test_layer("Gemm", pd, weights, a);
42+ if (ret != 0)
43+ {
44+ fprintf(stderr, "test_gemm_int8 failed M=%d N=%d K=%d TILE_M=%d TILE_N=%d TILE_K=%d alpha=%f transA=%d transB=%d output_transpose=%d\n", M, N, K, TILE_M, TILE_N, TILE_K, alpha, transA, transB, output_transpose);
45+ }
46+
47+ return ret;
48+}
49+
50+static int test_gemm_0(int M, int N, int K, int TILE_M, int TILE_N, int TILE_K)
51+{
52+ return 0
53+ || test_gemm_int8(M, N, K, TILE_M, TILE_N, TILE_K, 2.1f, 0, 0, 0)
54+ || test_gemm_int8(M, N, K, TILE_M, TILE_N, TILE_K, 3.1f, 0, 1, 0)
55+ || test_gemm_int8(M, N, K, TILE_M, TILE_N, TILE_K, 4.1f, 1, 0, 0)
56+ || test_gemm_int8(M, N, K, TILE_M, TILE_N, TILE_K, 5.1f, 1, 1, 0)
57+ || test_gemm_int8(M, N, K, TILE_M, TILE_N, TILE_K, 2.1f, 0, 0, 1)
58+ || test_gemm_int8(M, N, K, TILE_M, TILE_N, TILE_K, 3.1f, 0, 1, 1)
59+ || test_gemm_int8(M, N, K, TILE_M, TILE_N, TILE_K, 4.1f, 1, 0, 1)
60+ || test_gemm_int8(M, N, K, TILE_M, TILE_N, TILE_K, 5.1f, 1, 1, 1);
61+}
62+#endif // NCNN_INT8
63+
64+int main()
65+{
66+ SRAND(7767517);
67+
68+#if NCNN_INT8
69+ int mnk[][3] = {
70+ {1, 1, 1},
71+ {2, 2, 2},
72+ {3, 3, 3},
73+ {4, 4, 4},
74+ {5, 5, 5},
75+ {6, 6, 6},
76+ {7, 7, 7},
77+ {8, 8, 8},
78+ {15, 15, 15},
79+ {16, 16, 16},
80+ {24, 24, 24},
81+ {31, 31, 31},
82+ {31, 32, 31},
83+ {32, 31, 32},
84+ {32, 32, 32},
85+ {20, 32, 20},
86+ {40, 40, 40},
87+ {47, 47, 47},
88+ {48, 48, 48},
89+ {52, 52, 52},
90+ {63, 64, 63},
91+ {64, 63, 64},
92+ {64, 64, 64}
93+ };
94+
95+ int tile_mnk[][3] = {
96+ {1, 1, 1},
97+ {2, 2, 2},
98+ {4, 4, 4},
99+ {8, 8, 8},
100+ {12, 12, 12},
101+ {16, 16, 16},
102+ {20, 20, 20},
103+ {24, 24, 24},
104+ {28, 28, 28}
105+ };
106+
107+ int mnk_count = sizeof(mnk) / sizeof(int) / 3;
108+ int tile_mnk_count = sizeof(tile_mnk) / sizeof(int) / 3;
109+
110+ for (int i = 0; i < mnk_count; i++)
111+ {
112+ int M = mnk[i][0];
113+ int N = mnk[i][1];
114+ int K = mnk[i][2];
115+
116+ for (int j = 0; j < tile_mnk_count; j++)
117+ {
118+ int TILE_M = tile_mnk[j][0];
119+ int TILE_N = tile_mnk[j][1];
120+ int TILE_K = tile_mnk[j][2];
121+
122+ if (TILE_M >= M && TILE_N >= N && TILE_K >= K)
123+ continue;
124+
125+ int ret = test_gemm_0(M, N, K, TILE_M, TILE_N, TILE_K);
126+ if (ret != 0)
127+ return ret;
128+ }
129+
130+ // test no tiling
131+ int ret = test_gemm_0(M, N, K, 100, 100, 100);
132+ if (ret != 0)
133+ return ret;
134+ }
135+#else
136+ // test nothing for non-int8 build
137+#endif
138+
139+ return 0;
140+}
modified tools/modelwriter.h +18 −0
@@ -1773,6 +1773,7 @@
17731773 fprintf_param_value(" 12=%d", output_elempack)
17741774 fprintf_param_value(" 13=%d", output_elemtype)
17751775 fprintf_param_value(" 14=%d", output_transpose)
1776+ fprintf_param_value(" 18=%d", int8_scale_term)
17761777 fprintf_param_value(" 20=%d", constant_TILE_M)
17771778 fprintf_param_value(" 21=%d", constant_TILE_N)
17781779 fprintf_param_value(" 22=%d", constant_TILE_K)
@@ -1789,6 +1790,23 @@
17891790 {
17901791 fwrite_weight_tag_data(op->C_data, bp);
17911792 }
1793+
1794+#if NCNN_INT8
1795+ // write int8_scale data
1796+ if (op->int8_scale_term)
1797+ {
1798+ if (op->constantA == 1)
1799+ {
1800+ fwrite_weight_data(op->A_data_int8_scales, bp, 90, 100);
1801+ }
1802+ if (op->constantB == 1)
1803+ {
1804+ ncnn::Mat B_data_int8_scales(1);
1805+ B_data_int8_scales[0] = op->B_data_int8_scale;
1806+ fwrite_weight_data(B_data_int8_scales, bp, 90, 100);
1807+ }
1808+ }
1809+#endif // NCNN_INT8
17921810 }
17931811 else if (layer->type == "GLU")
17941812 {
modified tools/quantize/ncnn2int8.cpp +109 −0
@@ -134,6 +134,7 @@
134134 int quantize_gru();
135135
136136 int quantize_embed();
137+ int quantize_gemm();
137138
138139 int fuse_requantize();
139140 };
@@ -613,6 +614,113 @@
613614 return 0;
614615 }
615616
617+int NetQuantize::quantize_gemm()
618+{
619+ for (size_t i = 0; i < layers.size(); i++)
620+ {
621+ if (layers[i]->type != "Gemm")
622+ continue;
623+
624+ // Gemm - quantize weight from fp32 to int8
625+ ncnn::Gemm* gemm = (ncnn::Gemm*)layers[i];
626+
627+ fprintf(stderr, "quantize_gemm %s\n", gemm->name.c_str());
628+
629+ // TODO move to ncnn2table
630+
631+ if (gemm->constantA)
632+ {
633+ if (gemm->transA == 1)
634+ {
635+ // transpose for easier quantization
636+ ncnn::Mat A_data_transposed(gemm->constantK * gemm->constantM);
637+ for (int i = 0; i < gemm->constantM; i++)
638+ {
639+ float* ptr = (float*)A_data_transposed + i * gemm->constantK;
640+ for (int j = 0; j < gemm->constantK; j++)
641+ {
642+ ptr[j] = gemm->A_data[j * gemm->constantM + i];
643+ }
644+ }
645+ gemm->A_data = A_data_transposed;
646+ gemm->transA = 0;
647+ }
648+
649+ gemm->A_data_int8_scales.create(gemm->constantM);
650+ for (int i = 0; i < gemm->constantM; i++)
651+ {
652+ float absmax = 0.f;
653+
654+ const float* ptr = (const float*)gemm->A_data + i * gemm->constantK;
655+ for (int j = 0; j < gemm->constantK; j++)
656+ {
657+ absmax = std::max(absmax, (float)fabs(ptr[j]));
658+ }
659+
660+ gemm->A_data_int8_scales[i] = absmax == 0.f ? 1.f : 127 / absmax;
661+ }
662+
663+ ncnn::Mat A_data = gemm->A_data.reshape(gemm->constantK, gemm->constantM);
664+ ncnn::Mat A_data_int8;
665+
666+ ncnn::Option opt_q = opt;
667+ opt_q.blob_allocator = A_data.allocator;
668+ opt_q.use_packing_layout = false;
669+ ncnn::quantize_to_int8(A_data, A_data_int8, gemm->A_data_int8_scales, opt_q);
670+ if (A_data_int8.empty())
671+ return -100;
672+
673+ gemm->A_data = A_data_int8.reshape(gemm->constantK * gemm->constantM);
674+ }
675+
676+ if (gemm->constantB)
677+ {
678+ if (gemm->transB == 0)
679+ {
680+ // transpose for easier quantization
681+ ncnn::Mat B_data_transposed(gemm->constantK * gemm->constantN);
682+ for (int i = 0; i < gemm->constantN; i++)
683+ {
684+ float* ptr = (float*)B_data_transposed + i * gemm->constantK;
685+ for (int j = 0; j < gemm->constantK; j++)
686+ {
687+ ptr[j] = gemm->B_data[j * gemm->constantN + i];
688+ }
689+ }
690+ gemm->B_data = B_data_transposed;
691+ gemm->transB = 1;
692+ }
693+
694+ const float* ptr = gemm->B_data;
695+ float absmax = 0.f;
696+ for (int j = 0; j < gemm->B_data.w; j++)
697+ {
698+ absmax = std::max(absmax, (float)fabs(ptr[j]));
699+ }
700+
701+ gemm->B_data_int8_scale = absmax == 0.f ? 1.f : 127 / absmax;
702+
703+ ncnn::Mat B_data_int8_scales(1);
704+ B_data_int8_scales[0] = gemm->B_data_int8_scale;
705+
706+ ncnn::Mat B_data_int8;
707+
708+ ncnn::Option opt_q = opt;
709+ opt_q.blob_allocator = gemm->B_data.allocator;
710+ opt_q.use_packing_layout = false;
711+ ncnn::quantize_to_int8(gemm->B_data, B_data_int8, B_data_int8_scales, opt_q);
712+ if (B_data_int8.empty())
713+ return -100;
714+
715+ gemm->B_data = B_data_int8;
716+ }
717+
718+ gemm->int8_scale_term = 2;
719+ }
720+
721+ return 0;
722+}
723+
616724 int NetQuantize::fuse_requantize()
617725 {
618726 const size_t layer_count = layers.size();
@@ -861,6 +969,7 @@
861969 quantizer.quantize_lstm();
862970 quantizer.quantize_gru();
863971 quantizer.quantize_embed();
972+ quantizer.quantize_gemm();
864973
865974 quantizer.fuse_requantize();
866975