Changesets · Branches · Tags · Compare
| Changeset | d7a5f98c0f60adc2c73cea73a6d7d2dd1de57f2a4124736c379e64588697bfb2 |
|---|---|
| Author | nihui <nihuini@tencent.com> · 2024-10-12 19:02 +08:00 |
| Committer | GitHub <noreply@github.com> · 2024-10-12 19:02 +08:00 |
| Parents | c796fe082fe6 |
24 files changed
modified CMakeLists.txt +18 −14
| @@ -162,21 +162,25 @@ | ||
| 162 | 162 | endif() |
| 163 | 163 | |
| 164 | 164 | 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) | |
| 178 | 171 | |
| 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() | |
| 180 | 184 | endif() |
| 181 | 185 | |
| 182 | 186 | 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 @@ | ||
| 144 | 144 | if(NCNN_RUNTIME_CPU AND NCNN_AVX) |
| 145 | 145 | ncnn_add_arch_opt_layer(${class} avx "/arch:AVX /D__SSSE3__ /D__SSE4_1__") |
| 146 | 146 | endif() |
| 147 | - if(NCNN_AVX512VNNI) | |
| 147 | + if(NCNN_RUNTIME_CPU AND NCNN_AVX512VNNI) | |
| 148 | 148 | ncnn_add_arch_opt_source(${class} avx512vnni "/arch:AVX512 /D__SSSE3__ /D__SSE4_1__ /D__FMA__ /D__F16C__ /D__AVX512VNNI__") |
| 149 | 149 | endif() |
| 150 | - if(NCNN_AVX512BF16) | |
| 150 | + if(NCNN_RUNTIME_CPU AND NCNN_AVX512BF16) | |
| 151 | 151 | ncnn_add_arch_opt_source(${class} avx512bf16 "/arch:AVX512 /D__SSSE3__ /D__SSE4_1__ /D__FMA__ /D__F16C__ /D__AVX512BF16__") |
| 152 | 152 | endif() |
| 153 | - if(NCNN_AVX512FP16) | |
| 153 | + if(NCNN_RUNTIME_CPU AND NCNN_AVX512FP16) | |
| 154 | 154 | ncnn_add_arch_opt_source(${class} avx512fp16 "/arch:AVX512 /D__SSSE3__ /D__SSE4_1__ /D__FMA__ /D__F16C__ /D__AVX512FP16__") |
| 155 | 155 | endif() |
| 156 | - if(NCNN_AVXVNNI) | |
| 156 | + if(NCNN_RUNTIME_CPU AND NCNN_AVXVNNI) | |
| 157 | 157 | ncnn_add_arch_opt_source(${class} avxvnni "/arch:AVX2 /D__SSSE3__ /D__SSE4_1__ /D__FMA__ /D__F16C__ /D__AVXVNNI__") |
| 158 | 158 | endif() |
| 159 | - if(NCNN_AVX2) | |
| 159 | + if(NCNN_RUNTIME_CPU AND NCNN_AVX2) | |
| 160 | 160 | ncnn_add_arch_opt_source(${class} avx2 "/arch:AVX2 /D__SSSE3__ /D__SSE4_1__ /D__FMA__ /D__F16C__") |
| 161 | 161 | endif() |
| 162 | - if(NCNN_XOP) | |
| 162 | + if(NCNN_RUNTIME_CPU AND NCNN_XOP) | |
| 163 | 163 | ncnn_add_arch_opt_source(${class} xop "/arch:AVX /D__SSSE3__ /D__SSE4_1__ /D__XOP__") |
| 164 | 164 | endif() |
| 165 | - if(NCNN_F16C) | |
| 165 | + if(NCNN_RUNTIME_CPU AND NCNN_F16C) | |
| 166 | 166 | ncnn_add_arch_opt_source(${class} f16c "/arch:AVX /D__SSSE3__ /D__SSE4_1__ /D__F16C__") |
| 167 | 167 | endif() |
| 168 | 168 | 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 @@ | ||
| 175 | 175 | if(NCNN_RUNTIME_CPU AND NCNN_AVX) |
| 176 | 176 | ncnn_add_arch_opt_layer(${class} avx "/arch:AVX /D__SSSE3__ /D__SSE4_1__") |
| 177 | 177 | endif() |
| 178 | - if(NCNN_AVX512VNNI) | |
| 178 | + if(NCNN_RUNTIME_CPU AND NCNN_AVX512VNNI) | |
| 179 | 179 | 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__") |
| 180 | 180 | endif() |
| 181 | - if(NCNN_AVX512BF16) | |
| 181 | + if(NCNN_RUNTIME_CPU AND NCNN_AVX512BF16) | |
| 182 | 182 | 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__") |
| 183 | 183 | endif() |
| 184 | - if(NCNN_AVX512FP16) | |
| 184 | + if(NCNN_RUNTIME_CPU AND NCNN_AVX512FP16) | |
| 185 | 185 | 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__") |
| 186 | 186 | endif() |
| 187 | - if(NCNN_AVXVNNI) | |
| 187 | + if(NCNN_RUNTIME_CPU AND NCNN_AVXVNNI) | |
| 188 | 188 | ncnn_add_arch_opt_source(${class} avxvnni "/arch:AVX2 -mfma -mf16c -mavxvnni /D__SSSE3__ /D__SSE4_1__ /D__FMA__ /D__F16C__ /D__AVXVNNI__") |
| 189 | 189 | endif() |
| 190 | - if(NCNN_AVX2) | |
| 190 | + if(NCNN_RUNTIME_CPU AND NCNN_AVX2) | |
| 191 | 191 | ncnn_add_arch_opt_source(${class} avx2 "/arch:AVX2 -mfma -mf16c /D__SSSE3__ /D__SSE4_1__ /D__FMA__ /D__F16C__") |
| 192 | 192 | endif() |
| 193 | - if(NCNN_XOP) | |
| 193 | + if(NCNN_RUNTIME_CPU AND NCNN_XOP) | |
| 194 | 194 | ncnn_add_arch_opt_source(${class} xop "/arch:AVX -mxop /D__SSSE3__ /D__SSE4_1__ /D__XOP__") |
| 195 | 195 | endif() |
| 196 | - if(NCNN_F16C) | |
| 196 | + if(NCNN_RUNTIME_CPU AND NCNN_F16C) | |
| 197 | 197 | ncnn_add_arch_opt_source(${class} f16c "/arch:AVX -mf16c /D__SSSE3__ /D__SSE4_1__ /D__F16C__") |
| 198 | 198 | endif() |
| 199 | 199 | else() |
| @@ -206,25 +206,25 @@ | ||
| 206 | 206 | if(NCNN_RUNTIME_CPU AND NCNN_AVX) |
| 207 | 207 | ncnn_add_arch_opt_layer(${class} avx "-mavx") |
| 208 | 208 | endif() |
| 209 | - if(NCNN_AVX512VNNI) | |
| 209 | + if(NCNN_RUNTIME_CPU AND NCNN_AVX512VNNI) | |
| 210 | 210 | ncnn_add_arch_opt_source(${class} avx512vnni "-mavx512f -mavx512cd -mavx512bw -mavx512dq -mavx512vl -mfma -mf16c -mavx512vnni") |
| 211 | 211 | endif() |
| 212 | - if(NCNN_AVX512BF16) | |
| 212 | + if(NCNN_RUNTIME_CPU AND NCNN_AVX512BF16) | |
| 213 | 213 | ncnn_add_arch_opt_source(${class} avx512bf16 "-mavx512f -mavx512cd -mavx512bw -mavx512dq -mavx512vl -mfma -mf16c -mavx512bf16") |
| 214 | 214 | endif() |
| 215 | - if(NCNN_AVX512FP16) | |
| 215 | + if(NCNN_RUNTIME_CPU AND NCNN_AVX512FP16) | |
| 216 | 216 | ncnn_add_arch_opt_source(${class} avx512fp16 "-mavx512f -mavx512cd -mavx512bw -mavx512dq -mavx512vl -mfma -mf16c -mavx512fp16") |
| 217 | 217 | endif() |
| 218 | - if(NCNN_AVXVNNI) | |
| 218 | + if(NCNN_RUNTIME_CPU AND NCNN_AVXVNNI) | |
| 219 | 219 | ncnn_add_arch_opt_source(${class} avxvnni "-mavx2 -mfma -mf16c -mavxvnni") |
| 220 | 220 | endif() |
| 221 | - if(NCNN_AVX2) | |
| 221 | + if(NCNN_RUNTIME_CPU AND NCNN_AVX2) | |
| 222 | 222 | ncnn_add_arch_opt_source(${class} avx2 "-mavx2 -mfma -mf16c") |
| 223 | 223 | endif() |
| 224 | - if(NCNN_XOP) | |
| 224 | + if(NCNN_RUNTIME_CPU AND NCNN_XOP) | |
| 225 | 225 | ncnn_add_arch_opt_source(${class} xop "-mavx -mxop") |
| 226 | 226 | endif() |
| 227 | - if(NCNN_F16C) | |
| 227 | + if(NCNN_RUNTIME_CPU AND NCNN_F16C) | |
| 228 | 228 | ncnn_add_arch_opt_source(${class} f16c "-mavx -mf16c") |
| 229 | 229 | endif() |
| 230 | 230 | endif() |
| @@ -254,28 +254,28 @@ | ||
| 254 | 254 | if(NCNN_ARM82) |
| 255 | 255 | ncnn_add_arch_opt_source(${class} asimdhp "/arch:armv8.2 /D__ARM_FEATURE_FP16_VECTOR_ARITHMETIC") |
| 256 | 256 | endif() |
| 257 | - if(NCNN_ARM82DOT) | |
| 257 | + if(NCNN_RUNTIME_CPU AND NCNN_ARM82DOT) | |
| 258 | 258 | ncnn_add_arch_opt_source(${class} asimddp "/arch:armv8.2 /D__ARM_FEATURE_FP16_VECTOR_ARITHMETIC /D__ARM_FEATURE_DOTPROD") |
| 259 | 259 | endif() |
| 260 | - if(NCNN_ARM82FP16FML) | |
| 260 | + if(NCNN_RUNTIME_CPU AND NCNN_ARM82FP16FML) | |
| 261 | 261 | ncnn_add_arch_opt_source(${class} asimdfhm "/arch:armv8.2 /D__ARM_FEATURE_FP16_VECTOR_ARITHMETIC /D__ARM_FEATURE_FP16_FML") |
| 262 | 262 | endif() |
| 263 | - if(NCNN_ARM84BF16) | |
| 263 | + if(NCNN_RUNTIME_CPU AND NCNN_ARM84BF16) | |
| 264 | 264 | 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") |
| 265 | 265 | endif() |
| 266 | - if(NCNN_ARM84I8MM) | |
| 266 | + if(NCNN_RUNTIME_CPU AND NCNN_ARM84I8MM) | |
| 267 | 267 | 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") |
| 268 | 268 | endif() |
| 269 | 269 | # TODO add support for sve family |
| 270 | - if(NCNN_ARM86SVE) | |
| 270 | + if(NCNN_RUNTIME_CPU AND NCNN_ARM86SVE) | |
| 271 | 271 | endif() |
| 272 | - if(NCNN_ARM86SVE2) | |
| 272 | + if(NCNN_RUNTIME_CPU AND NCNN_ARM86SVE2) | |
| 273 | 273 | endif() |
| 274 | - if(NCNN_ARM86SVEBF16) | |
| 274 | + if(NCNN_RUNTIME_CPU AND NCNN_ARM86SVEBF16) | |
| 275 | 275 | endif() |
| 276 | - if(NCNN_ARM86SVEI8MM) | |
| 276 | + if(NCNN_RUNTIME_CPU AND NCNN_ARM86SVEI8MM) | |
| 277 | 277 | endif() |
| 278 | - if(NCNN_ARM86SVEF32MM) | |
| 278 | + if(NCNN_RUNTIME_CPU AND NCNN_ARM86SVEF32MM) | |
| 279 | 279 | endif() |
| 280 | 280 | elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_SIMULATE_ID MATCHES "MSVC" AND CMAKE_CXX_COMPILER_FRONTEND_VARIANT MATCHES "MSVC") |
| 281 | 281 | if(NCNN_VFPV4) |
| @@ -284,28 +284,28 @@ | ||
| 284 | 284 | if(NCNN_ARM82) |
| 285 | 285 | ncnn_add_arch_opt_source(${class} asimdhp "/arch:armv8.2 -march=armv8.2-a+fp16 /D__ARM_FEATURE_FP16_VECTOR_ARITHMETIC") |
| 286 | 286 | endif() |
| 287 | - if(NCNN_ARM82DOT) | |
| 287 | + if(NCNN_RUNTIME_CPU AND NCNN_ARM82DOT) | |
| 288 | 288 | 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") |
| 289 | 289 | endif() |
| 290 | - if(NCNN_ARM82FP16FML) | |
| 290 | + if(NCNN_RUNTIME_CPU AND NCNN_ARM82FP16FML) | |
| 291 | 291 | 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") |
| 292 | 292 | endif() |
| 293 | - if(NCNN_ARM84BF16) | |
| 293 | + if(NCNN_RUNTIME_CPU AND NCNN_ARM84BF16) | |
| 294 | 294 | 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") |
| 295 | 295 | endif() |
| 296 | - if(NCNN_ARM84I8MM) | |
| 296 | + if(NCNN_RUNTIME_CPU AND NCNN_ARM84I8MM) | |
| 297 | 297 | 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") |
| 298 | 298 | endif() |
| 299 | 299 | # TODO add support for sve family |
| 300 | - if(NCNN_ARM86SVE) | |
| 300 | + if(NCNN_RUNTIME_CPU AND NCNN_ARM86SVE) | |
| 301 | 301 | endif() |
| 302 | - if(NCNN_ARM86SVE2) | |
| 302 | + if(NCNN_RUNTIME_CPU AND NCNN_ARM86SVE2) | |
| 303 | 303 | endif() |
| 304 | - if(NCNN_ARM86SVEBF16) | |
| 304 | + if(NCNN_RUNTIME_CPU AND NCNN_ARM86SVEBF16) | |
| 305 | 305 | endif() |
| 306 | - if(NCNN_ARM86SVEI8MM) | |
| 306 | + if(NCNN_RUNTIME_CPU AND NCNN_ARM86SVEI8MM) | |
| 307 | 307 | endif() |
| 308 | - if(NCNN_ARM86SVEF32MM) | |
| 308 | + if(NCNN_RUNTIME_CPU AND NCNN_ARM86SVEF32MM) | |
| 309 | 309 | endif() |
| 310 | 310 | else() |
| 311 | 311 | if(NCNN_VFPV4) |
| @@ -314,31 +314,31 @@ | ||
| 314 | 314 | if(NCNN_ARM82) |
| 315 | 315 | ncnn_add_arch_opt_source(${class} asimdhp "-march=armv8.2-a+fp16") |
| 316 | 316 | endif() |
| 317 | - if(NCNN_ARM82DOT) | |
| 317 | + if(NCNN_RUNTIME_CPU AND NCNN_ARM82DOT) | |
| 318 | 318 | ncnn_add_arch_opt_source(${class} asimddp "-march=armv8.2-a+fp16+dotprod") |
| 319 | 319 | endif() |
| 320 | - if(NCNN_ARM82FP16FML) | |
| 320 | + if(NCNN_RUNTIME_CPU AND NCNN_ARM82FP16FML) | |
| 321 | 321 | ncnn_add_arch_opt_source(${class} asimdfhm "-march=armv8.2-a+fp16+fp16fml") |
| 322 | 322 | endif() |
| 323 | - if(NCNN_ARM84BF16) | |
| 323 | + if(NCNN_RUNTIME_CPU AND NCNN_ARM84BF16) | |
| 324 | 324 | ncnn_add_arch_opt_source(${class} bf16 "-march=armv8.4-a+fp16+dotprod+bf16") |
| 325 | 325 | endif() |
| 326 | - if(NCNN_ARM84I8MM) | |
| 326 | + if(NCNN_RUNTIME_CPU AND NCNN_ARM84I8MM) | |
| 327 | 327 | ncnn_add_arch_opt_source(${class} i8mm "-march=armv8.4-a+fp16+dotprod+i8mm") |
| 328 | 328 | endif() |
| 329 | - if(NCNN_ARM86SVE) | |
| 329 | + if(NCNN_RUNTIME_CPU AND NCNN_ARM86SVE) | |
| 330 | 330 | ncnn_add_arch_opt_source(${class} sve "-march=armv8.6-a+fp16+dotprod+sve") |
| 331 | 331 | endif() |
| 332 | - if(NCNN_ARM86SVE2) | |
| 332 | + if(NCNN_RUNTIME_CPU AND NCNN_ARM86SVE2) | |
| 333 | 333 | ncnn_add_arch_opt_source(${class} sve2 "-march=armv8.6-a+fp16+dotprod+sve2") |
| 334 | 334 | endif() |
| 335 | - if(NCNN_ARM86SVEBF16) | |
| 335 | + if(NCNN_RUNTIME_CPU AND NCNN_ARM86SVEBF16) | |
| 336 | 336 | ncnn_add_arch_opt_source(${class} svebf16 "-march=armv8.6-a+fp16+dotprod+sve+bf16") |
| 337 | 337 | endif() |
| 338 | - if(NCNN_ARM86SVEI8MM) | |
| 338 | + if(NCNN_RUNTIME_CPU AND NCNN_ARM86SVEI8MM) | |
| 339 | 339 | ncnn_add_arch_opt_source(${class} svei8mm "-march=armv8.6-a+fp16+dotprod+sve+i8mm") |
| 340 | 340 | endif() |
| 341 | - if(NCNN_ARM86SVEF32MM) | |
| 341 | + if(NCNN_RUNTIME_CPU AND NCNN_ARM86SVEF32MM) | |
| 342 | 342 | ncnn_add_arch_opt_source(${class} svef32mm "-march=armv8.6-a+fp16+dotprod+sve+f32mm") |
| 343 | 343 | endif() |
| 344 | 344 | endif() |
modified docs/developer-guide/operators.md +5 −2
| @@ -942,15 +942,18 @@ | ||
| 942 | 942 | | 12 | output_elempack | int | 0 | | |
| 943 | 943 | | 13 | output_elemtype | int | 0 | | |
| 944 | 944 | | 14 | output_transpose | int| 0 | | |
| 945 | +| 18 | int8_scale_term | int | 0 | | | |
| 945 | 946 | | 20 | constant_TILE_M | int | 0 | | |
| 946 | 947 | | 21 | constant_TILE_N | int | 0 | | |
| 947 | 948 | | 22 | constant_TILE_K | int | 0 | | |
| 948 | 949 | |
| 949 | 950 | | weight | type | shape | |
| 950 | 951 | | ------------- | ----- | --------------------- | |
| 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] | | |
| 953 | 954 | | 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] | | |
| 954 | 957 | |
| 955 | 958 | # GridSample |
| 956 | 959 | ``` |
modified src/layer/arm/gemm_arm.cpp +1087 −0
| @@ -16,6 +16,7 @@ | ||
| 16 | 16 | |
| 17 | 17 | #if __ARM_NEON |
| 18 | 18 | #include <arm_neon.h> |
| 19 | +#include "neon_mathfun.h" | |
| 19 | 20 | #endif // __ARM_NEON |
| 20 | 21 | |
| 21 | 22 | #include "arm_usability.h" |
| @@ -29,6 +30,13 @@ | ||
| 29 | 30 | #include "gemm_bf16s.h" |
| 30 | 31 | #endif |
| 31 | 32 | |
| 33 | +#if NCNN_INT8 | |
| 34 | +#include "gemm_int8.h" | |
| 35 | +#if NCNN_BF16 | |
| 36 | +#include "gemm_int8_bf16s.h" | |
| 37 | +#endif | |
| 38 | +#endif | |
| 39 | + | |
| 32 | 40 | Gemm_arm::Gemm_arm() |
| 33 | 41 | { |
| 34 | 42 | #if __ARM_NEON |
| @@ -2461,6 +2469,79 @@ | ||
| 2461 | 2469 | int kk = 0; |
| 2462 | 2470 | for (; kk < max_kk; kk += 1) |
| 2463 | 2471 | { |
| 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 | |
| 2464 | 2545 | float32x4_t _pA = vld1q_f32(pA); |
| 2465 | 2546 | float32x4_t _pB0 = vld1q_f32(pB); |
| 2466 | 2547 | float32x4_t _pB1 = vld1q_f32(pB + 4); |
| @@ -2487,6 +2568,7 @@ | ||
| 2487 | 2568 | |
| 2488 | 2569 | pA += 4; |
| 2489 | 2570 | pB += 8; |
| 2571 | +#endif // NCNN_GNU_INLINE_ASM | |
| 2490 | 2572 | } |
| 2491 | 2573 | |
| 2492 | 2574 | if (k_end) |
| @@ -4164,6 +4246,13 @@ | ||
| 4164 | 4246 | |
| 4165 | 4247 | int Gemm_arm::create_pipeline(const Option& opt) |
| 4166 | 4248 | { |
| 4249 | +#if NCNN_INT8 | |
| 4250 | + if (int8_scale_term) | |
| 4251 | + { | |
| 4252 | + return create_pipeline_int8(opt); | |
| 4253 | + } | |
| 4254 | +#endif | |
| 4255 | + | |
| 4167 | 4256 | #if NCNN_ARM82 |
| 4168 | 4257 | if (cpu_support_arm_asimdhp() && opt.use_fp16_storage) |
| 4169 | 4258 | { |
| @@ -4311,6 +4400,14 @@ | ||
| 4311 | 4400 | |
| 4312 | 4401 | int Gemm_arm::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 4313 | 4402 | { |
| 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 | + | |
| 4314 | 4411 | const Mat& bottom_blob = constantA ? AT_data : bottom_blobs[0]; |
| 4315 | 4412 | int elembits = bottom_blob.elembits(); |
| 4316 | 4413 | |
| @@ -5199,4 +5296,994 @@ | ||
| 5199 | 5296 | } |
| 5200 | 5297 | #endif // NCNN_BF16 |
| 5201 | 5298 | |
| 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 | + | |
| 5202 | 6289 | } // namespace ncnn |
modified src/layer/arm/gemm_arm.h +6 −0
| @@ -41,12 +41,18 @@ | ||
| 41 | 41 | int create_pipeline_bf16s(const Option& opt); |
| 42 | 42 | int forward_bf16s(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const; |
| 43 | 43 | #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 | |
| 44 | 48 | |
| 45 | 49 | public: |
| 46 | 50 | int nT; |
| 47 | 51 | Mat AT_data; |
| 48 | 52 | Mat BT_data; |
| 49 | 53 | Mat CT_data; |
| 54 | + | |
| 55 | + int input_elemtype; // 0=auto 1=fp32 2=fp16 3=bf16 | |
| 50 | 56 | }; |
| 51 | 57 | |
| 52 | 58 | } // 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 @@ | ||
| 27 | 27 | #include "gemm_bf16s_fp16s.h" |
| 28 | 28 | #include "gemm_fp16s.h" |
| 29 | 29 | |
| 30 | +#if NCNN_INT8 | |
| 31 | +#include "gemm_int8_fp16s.h" | |
| 32 | +#endif | |
| 33 | + | |
| 30 | 34 | 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) |
| 31 | 35 | { |
| 32 | 36 | const int out_elempack = top_blob.elempack; |
| @@ -3026,4 +3030,21 @@ | ||
| 3026 | 3030 | return 0; |
| 3027 | 3031 | } |
| 3028 | 3032 | |
| 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 | + | |
| 3029 | 3050 | } // 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 @@ | ||
| 27 | 27 | #include "gemm_bf16s_fp16s.h" |
| 28 | 28 | #include "gemm_fp16s.h" |
| 29 | 29 | |
| 30 | +#if NCNN_INT8 | |
| 31 | +#include "gemm_int8_fp16s.h" | |
| 32 | +#endif | |
| 33 | + | |
| 30 | 34 | extern void pack_A_tile(const Mat& A, Mat& AT, int i, int max_ii, int k, int max_kk); |
| 31 | 35 | |
| 32 | 36 | 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 @@ | ||
| 709 | 713 | return ret; |
| 710 | 714 | } |
| 711 | 715 | |
| 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 | + | |
| 712 | 763 | } // 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 @@ | ||
| 39 | 39 | output_elempack = pd.get(12, 0); |
| 40 | 40 | output_elemtype = pd.get(13, 0); |
| 41 | 41 | output_transpose = pd.get(14, 0); |
| 42 | + int8_scale_term = pd.get(18, 0); | |
| 42 | 43 | constant_TILE_M = pd.get(20, 0); |
| 43 | 44 | constant_TILE_N = pd.get(21, 0); |
| 44 | 45 | constant_TILE_K = pd.get(22, 0); |
| 45 | 46 | |
| 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 | + | |
| 46 | 55 | if (constantA == 1 && (constantM == 0 || constantK == 0)) |
| 47 | 56 | { |
| 48 | 57 | NCNN_LOGE("constantM and constantK must be non-zero when constantA enabled"); |
| @@ -111,9 +120,175 @@ | ||
| 111 | 120 | return -100; |
| 112 | 121 | } |
| 113 | 122 | |
| 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 | + | |
| 114 | 138 | return 0; |
| 115 | 139 | } |
| 116 | 140 | |
| 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 | + | |
| 117 | 292 | int Gemm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 118 | 293 | { |
| 119 | 294 | std::vector<Mat> bottom_blobs(1, bottom_blob); |
| @@ -125,6 +300,13 @@ | ||
| 125 | 300 | |
| 126 | 301 | int Gemm::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 127 | 302 | { |
| 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 | + | |
| 128 | 310 | const Mat& A0 = constantA ? A_data : bottom_blobs[0]; |
| 129 | 311 | const Mat& B0 = constantB ? B_data : constantA ? bottom_blobs[0] : bottom_blobs[1]; |
| 130 | 312 | |
| @@ -152,18 +334,18 @@ | ||
| 152 | 334 | } |
| 153 | 335 | } |
| 154 | 336 | |
| 155 | - Mat B; | |
| 337 | + Mat BT; | |
| 156 | 338 | if (transB == 0) |
| 157 | 339 | { |
| 158 | 340 | // 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); | |
| 160 | 342 | |
| 161 | 343 | const int B0_hstep = B0.dims == 3 ? (int)B0.cstep : B0.w; |
| 162 | 344 | |
| 163 | - for (int i = 0; i < B.h; i++) | |
| 345 | + for (int i = 0; i < BT.h; i++) | |
| 164 | 346 | { |
| 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++) | |
| 167 | 349 | { |
| 168 | 350 | ptr[j] = B0[j * B0_hstep + i]; |
| 169 | 351 | } |
| @@ -171,43 +353,36 @@ | ||
| 171 | 353 | } |
| 172 | 354 | else |
| 173 | 355 | { |
| 174 | - B = B0; | |
| 356 | + BT = B0; | |
| 175 | 357 | } |
| 176 | 358 | |
| 177 | 359 | 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; | |
| 180 | 361 | |
| 181 | - const float* ptrC = 0; | |
| 362 | + Mat C; | |
| 182 | 363 | int broadcast_type_C = 0; |
| 183 | 364 | if (constantC) |
| 184 | 365 | { |
| 185 | - ptrC = C_data; | |
| 366 | + C = C_data; | |
| 186 | 367 | broadcast_type_C = constant_broadcast_type_C; |
| 187 | 368 | } |
| 188 | 369 | else |
| 189 | 370 | { |
| 190 | - if (constantA && constantB) | |
| 371 | + if (constantA && constantB && bottom_blobs.size() == 1) | |
| 191 | 372 | { |
| 192 | - ptrC = bottom_blobs.size() == 1 ? bottom_blobs[0] : 0; | |
| 373 | + C = bottom_blobs[0]; | |
| 193 | 374 | } |
| 194 | - else if (constantA) | |
| 375 | + else if ((constantA || constantB) && bottom_blobs.size() == 2) | |
| 195 | 376 | { |
| 196 | - ptrC = bottom_blobs.size() == 2 ? bottom_blobs[1] : 0; | |
| 377 | + C = bottom_blobs[1]; | |
| 197 | 378 | } |
| 198 | - else if (constantB) | |
| 379 | + else if (bottom_blobs.size() == 3) | |
| 199 | 380 | { |
| 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]; | |
| 205 | 382 | } |
| 206 | 383 | |
| 207 | - if (ptrC) | |
| 384 | + if (!C.empty()) | |
| 208 | 385 | { |
| 209 | - const Mat& C = bottom_blobs[bottom_blobs.size() - 1]; | |
| 210 | - | |
| 211 | 386 | if (C.dims == 1 && C.w == 1) |
| 212 | 387 | { |
| 213 | 388 | // scalar |
| @@ -260,66 +435,226 @@ | ||
| 260 | 435 | if (top_blob.empty()) |
| 261 | 436 | return -100; |
| 262 | 437 | |
| 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); | |
| 267 | 439 | |
| 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 | +} | |
| 270 | 442 | |
| 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]; | |
| 272 | 448 | |
| 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) | |
| 274 | 458 | { |
| 275 | - const float* ptrB = (const float*)B + j * B_hstep; | |
| 459 | + A.create(A0.h, A0.w, (size_t)1u, 1, opt.workspace_allocator); | |
| 276 | 460 | |
| 277 | - float sum = 0.f; | |
| 278 | - if (ptrC) | |
| 461 | + for (int i = 0; i < A.h; i++) | |
| 279 | 462 | { |
| 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++) | |
| 285 | 465 | { |
| 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]; | |
| 295 | 467 | } |
| 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++) | |
| 297 | 478 | { |
| 298 | - sum = ptrC[j]; | |
| 479 | + ptr[j] = A0.dims == 3 ? A0.channel(j)[i] : A0.row(j)[i]; | |
| 299 | 480 | } |
| 481 | + } | |
| 482 | + } | |
| 483 | + } | |
| 300 | 484 | |
| 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])); | |
| 302 | 502 | } |
| 303 | 503 | |
| 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++) | |
| 305 | 512 | { |
| 306 | - sum += ptrA[k] * ptrB[k]; | |
| 513 | + ptrAi[k] = float2int8(ptr[k] * A_int8_scale); | |
| 307 | 514 | } |
| 515 | + } | |
| 516 | + } | |
| 308 | 517 | |
| 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); | |
| 310 | 524 | |
| 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++) | |
| 312 | 532 | { |
| 313 | - top_blob[j * out_hstep + i] = sum; | |
| 533 | + absmax = std::max(absmax, (float)fabs(ptr[k])); | |
| 314 | 534 | } |
| 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++) | |
| 316 | 549 | { |
| 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; | |
| 318 | 632 | } |
| 319 | 633 | } |
| 320 | 634 | } |
| 321 | 635 | |
| 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 | + | |
| 322 | 656 | return 0; |
| 323 | 657 | } |
| 658 | +#endif // NCNN_INT8 | |
| 324 | 659 | |
| 325 | 660 | } // namespace ncnn |
modified src/layer/gemm.h +12 −0
| @@ -32,6 +32,11 @@ | ||
| 32 | 32 | |
| 33 | 33 | virtual int forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const; |
| 34 | 34 | |
| 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 | + | |
| 35 | 40 | public: |
| 36 | 41 | float alpha; |
| 37 | 42 | float beta; |
| @@ -50,6 +55,8 @@ | ||
| 50 | 55 | int output_elemtype; // 0=auto 1=fp32 |
| 51 | 56 | int output_transpose; |
| 52 | 57 | |
| 58 | + int int8_scale_term; | |
| 59 | + | |
| 53 | 60 | int constant_TILE_M; |
| 54 | 61 | int constant_TILE_N; |
| 55 | 62 | int constant_TILE_K; |
| @@ -58,6 +65,11 @@ | ||
| 58 | 65 | Mat A_data; |
| 59 | 66 | Mat B_data; |
| 60 | 67 | Mat C_data; |
| 68 | + | |
| 69 | +#if NCNN_INT8 | |
| 70 | + Mat A_data_int8_scales; | |
| 71 | + float B_data_int8_scale; | |
| 72 | +#endif | |
| 61 | 73 | }; |
| 62 | 74 | |
| 63 | 75 | } // namespace ncnn |
modified src/layer/riscv/gemm_riscv.cpp +15 −0
| @@ -3947,6 +3947,14 @@ | ||
| 3947 | 3947 | |
| 3948 | 3948 | int Gemm_riscv::create_pipeline(const Option& opt) |
| 3949 | 3949 | { |
| 3950 | +#if NCNN_INT8 | |
| 3951 | + if (int8_scale_term) | |
| 3952 | + { | |
| 3953 | + support_packing = false; | |
| 3954 | + return 0; | |
| 3955 | + } | |
| 3956 | +#endif | |
| 3957 | + | |
| 3950 | 3958 | if (constantA) |
| 3951 | 3959 | { |
| 3952 | 3960 | const int M = constantM; |
| @@ -4070,6 +4078,13 @@ | ||
| 4070 | 4078 | |
| 4071 | 4079 | int Gemm_riscv::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 4072 | 4080 | { |
| 4081 | +#if NCNN_INT8 | |
| 4082 | + if (int8_scale_term) | |
| 4083 | + { | |
| 4084 | + return Gemm::forward_int8(bottom_blobs, top_blobs, opt); | |
| 4085 | + } | |
| 4086 | +#endif | |
| 4087 | + | |
| 4073 | 4088 | int M; |
| 4074 | 4089 | int N; |
| 4075 | 4090 | if (constantA && constantB) |
modified src/layer/vulkan/gemm_vulkan.cpp +111 −54
| @@ -26,6 +26,19 @@ | ||
| 26 | 26 | pipeline_gemm = 0; |
| 27 | 27 | } |
| 28 | 28 | |
| 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 | + | |
| 29 | 42 | int Gemm_vulkan::create_pipeline(const Option& opt) |
| 30 | 43 | { |
| 31 | 44 | // const Mat& shape = top_shapes.empty() ? Mat() : top_shapes[0]; |
| @@ -169,56 +182,78 @@ | ||
| 169 | 182 | { |
| 170 | 183 | const VkMat& A0 = constantA ? A_data_gpu : bottom_blobs[0]; |
| 171 | 184 | 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]; | |
| 173 | 185 | |
| 174 | 186 | VkMat A; |
| 175 | 187 | VkMat B; |
| 176 | - VkMat C; | |
| 177 | 188 | vkdev->convert_packing(A0, A, 1, cmd, opt); |
| 178 | 189 | vkdev->convert_packing(B0, B, 1, cmd, opt); |
| 179 | - vkdev->convert_packing(C0, C, 1, cmd, opt); | |
| 180 | 190 | |
| 181 | 191 | const int M = constantM ? constantM : transA ? A.w : (A.dims == 3 ? A.c : A.h); |
| 182 | 192 | const int K = constantK ? constantK : transA ? (A.dims == 3 ? A.c : A.h) : A.w; |
| 183 | 193 | const int N = constantN ? constantN : transB ? (B.dims == 3 ? B.c : B.h) : B.w; |
| 184 | 194 | |
| 185 | - int broadcast_type_C; | |
| 195 | + VkMat C; | |
| 196 | + int broadcast_type_C = -1; | |
| 186 | 197 | if (constantC) |
| 187 | 198 | { |
| 199 | + vkdev->convert_packing(C_data_gpu, C, 1, cmd, opt); | |
| 188 | 200 | broadcast_type_C = constant_broadcast_type_C; |
| 189 | 201 | } |
| 190 | 202 | else |
| 191 | 203 | { |
| 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) | |
| 204 | 206 | { |
| 205 | - // N | |
| 206 | - broadcast_type_C = 4; | |
| 207 | + C0 = bottom_blobs.size() == 1 ? bottom_blobs[0] : VkMat(); | |
| 207 | 208 | } |
| 208 | - if (C.dims == 2 && C.w == 1 && C.h == M) | |
| 209 | + else if (constantA) | |
| 209 | 210 | { |
| 210 | - // Mx1 | |
| 211 | - broadcast_type_C = 2; | |
| 211 | + C0 = bottom_blobs.size() == 2 ? bottom_blobs[1] : VkMat(); | |
| 212 | 212 | } |
| 213 | - if (C.dims == 2 && C.w == N && C.h == M) | |
| 213 | + else if (constantB) | |
| 214 | 214 | { |
| 215 | - // MxN | |
| 216 | - broadcast_type_C = 3; | |
| 215 | + C0 = bottom_blobs.size() == 2 ? bottom_blobs[1] : VkMat(); | |
| 217 | 216 | } |
| 218 | - if (C.dims == 2 && C.w == N && C.h == 1) | |
| 217 | + else | |
| 219 | 218 | { |
| 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 | + } | |
| 222 | 257 | } |
| 223 | 258 | } |
| 224 | 259 | |
| @@ -301,56 +336,78 @@ | ||
| 301 | 336 | { |
| 302 | 337 | const VkImageMat& A0 = constantA ? A_data_gpu_image : bottom_blobs[0]; |
| 303 | 338 | 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]; | |
| 305 | 339 | |
| 306 | 340 | VkImageMat A; |
| 307 | 341 | VkImageMat B; |
| 308 | - VkImageMat C; | |
| 309 | 342 | vkdev->convert_packing(A0, A, 1, cmd, opt); |
| 310 | 343 | vkdev->convert_packing(B0, B, 1, cmd, opt); |
| 311 | - vkdev->convert_packing(C0, C, 1, cmd, opt); | |
| 312 | 344 | |
| 313 | 345 | const int M = constantM ? constantM : transA ? A.w : (A.dims == 3 ? A.c : A.h); |
| 314 | 346 | const int K = constantK ? constantK : transA ? (A.dims == 3 ? A.c : A.h) : A.w; |
| 315 | 347 | const int N = constantN ? constantN : transB ? (B.dims == 3 ? B.c : B.h) : B.w; |
| 316 | 348 | |
| 317 | - int broadcast_type_C; | |
| 349 | + VkImageMat C; | |
| 350 | + int broadcast_type_C = -1; | |
| 318 | 351 | if (constantC) |
| 319 | 352 | { |
| 353 | + vkdev->convert_packing(C_data_gpu_image, C, 1, cmd, opt); | |
| 320 | 354 | broadcast_type_C = constant_broadcast_type_C; |
| 321 | 355 | } |
| 322 | 356 | else |
| 323 | 357 | { |
| 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) | |
| 330 | 360 | { |
| 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(); | |
| 334 | 362 | } |
| 335 | - if (C.dims == 1 && C.w == N) | |
| 363 | + else if (constantA) | |
| 336 | 364 | { |
| 337 | - // N | |
| 338 | - broadcast_type_C = 4; | |
| 365 | + C0 = bottom_blobs.size() == 2 ? bottom_blobs[1] : VkImageMat(); | |
| 339 | 366 | } |
| 340 | - if (C.dims == 2 && C.w == 1 && C.h == M) | |
| 367 | + else if (constantB) | |
| 341 | 368 | { |
| 342 | - // Mx1 | |
| 343 | - broadcast_type_C = 2; | |
| 369 | + C0 = bottom_blobs.size() == 2 ? bottom_blobs[1] : VkImageMat(); | |
| 344 | 370 | } |
| 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 | |
| 351 | 372 | { |
| 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 | + } | |
| 354 | 411 | } |
| 355 | 412 | } |
| 356 | 413 |
modified src/layer/vulkan/gemm_vulkan.h +2 −0
| @@ -24,6 +24,8 @@ | ||
| 24 | 24 | public: |
| 25 | 25 | Gemm_vulkan(); |
| 26 | 26 | |
| 27 | + virtual int load_param(const ParamDict& pd); | |
| 28 | + | |
| 27 | 29 | virtual int create_pipeline(const Option& opt); |
| 28 | 30 | virtual int destroy_pipeline(const Option& opt); |
| 29 | 31 |
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 @@ | ||
| 300 | 300 | || test_gemm_1(M, N, K); |
| 301 | 301 | |
| 302 | 302 | if (ret != 0) |
| 303 | - return 0; | |
| 303 | + return ret; | |
| 304 | 304 | } |
| 305 | 305 | |
| 306 | 306 | return 0; |
modified tests/test_gemm_1.cpp +2 −2
| @@ -120,13 +120,13 @@ | ||
| 120 | 120 | |
| 121 | 121 | int ret = test_gemm_0(M, N, K, TILE_M, TILE_N, TILE_K); |
| 122 | 122 | if (ret != 0) |
| 123 | - return 0; | |
| 123 | + return ret; | |
| 124 | 124 | } |
| 125 | 125 | |
| 126 | 126 | // test no tiling |
| 127 | 127 | int ret = test_gemm_0(M, N, K, 100, 100, 100); |
| 128 | 128 | if (ret != 0) |
| 129 | - return 0; | |
| 129 | + return ret; | |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | 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 @@ | ||
| 1773 | 1773 | fprintf_param_value(" 12=%d", output_elempack) |
| 1774 | 1774 | fprintf_param_value(" 13=%d", output_elemtype) |
| 1775 | 1775 | fprintf_param_value(" 14=%d", output_transpose) |
| 1776 | + fprintf_param_value(" 18=%d", int8_scale_term) | |
| 1776 | 1777 | fprintf_param_value(" 20=%d", constant_TILE_M) |
| 1777 | 1778 | fprintf_param_value(" 21=%d", constant_TILE_N) |
| 1778 | 1779 | fprintf_param_value(" 22=%d", constant_TILE_K) |
| @@ -1789,6 +1790,23 @@ | ||
| 1789 | 1790 | { |
| 1790 | 1791 | fwrite_weight_tag_data(op->C_data, bp); |
| 1791 | 1792 | } |
| 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 | |
| 1792 | 1810 | } |
| 1793 | 1811 | else if (layer->type == "GLU") |
| 1794 | 1812 | { |
modified tools/quantize/ncnn2int8.cpp +109 −0
| @@ -134,6 +134,7 @@ | ||
| 134 | 134 | int quantize_gru(); |
| 135 | 135 | |
| 136 | 136 | int quantize_embed(); |
| 137 | + int quantize_gemm(); | |
| 137 | 138 | |
| 138 | 139 | int fuse_requantize(); |
| 139 | 140 | }; |
| @@ -613,6 +614,113 @@ | ||
| 613 | 614 | return 0; |
| 614 | 615 | } |
| 615 | 616 | |
| 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 | + | |
| 616 | 724 | int NetQuantize::fuse_requantize() |
| 617 | 725 | { |
| 618 | 726 | const size_t layer_count = layers.size(); |
| @@ -861,6 +969,7 @@ | ||
| 861 | 969 | quantizer.quantize_lstm(); |
| 862 | 970 | quantizer.quantize_gru(); |
| 863 | 971 | quantizer.quantize_embed(); |
| 972 | + quantizer.quantize_gemm(); | |
| 864 | 973 | |
| 865 | 974 | quantizer.fuse_requantize(); |
| 866 | 975 |