-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCMakeLists.txt
394 lines (358 loc) · 14.9 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
cmake_minimum_required(VERSION 3.2)
cmake_policy(SET CMP0069 NEW)
set(CMAKE_POLICY_DEFAULT_CMP0069 NEW)
set(CMAKE_CXX_STANDARD 11)
if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.15")
cmake_policy(SET CMP0092 NEW)
endif()
project(mlas)
include(CheckCXXCompilerFlag)
include(CheckLanguage)
# OpenMP
find_package(OpenMP)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
include_directories(${OpenMP_CXX_INCLUDE_DIR})
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
set(ONNXRUNTIME_ROOT ${CMAKE_CURRENT_SOURCE_DIR})
set(mlas_common_srcs
${ONNXRUNTIME_ROOT}/lib/platform.cpp
${ONNXRUNTIME_ROOT}/lib/threading.cpp
${ONNXRUNTIME_ROOT}/lib/sgemm.cpp
${ONNXRUNTIME_ROOT}/lib/qgemm.cpp
${ONNXRUNTIME_ROOT}/lib/qdwconv.cpp
${ONNXRUNTIME_ROOT}/lib/convolve.cpp
${ONNXRUNTIME_ROOT}/lib/pooling.cpp
${ONNXRUNTIME_ROOT}/lib/transpose.cpp
${ONNXRUNTIME_ROOT}/lib/reorder.cpp
${ONNXRUNTIME_ROOT}/lib/snchwc.cpp
${ONNXRUNTIME_ROOT}/lib/activate.cpp
${ONNXRUNTIME_ROOT}/lib/logistic.cpp
${ONNXRUNTIME_ROOT}/lib/tanh.cpp
${ONNXRUNTIME_ROOT}/lib/erf.cpp
${ONNXRUNTIME_ROOT}/lib/compute.cpp
${ONNXRUNTIME_ROOT}/lib/quantize.cpp
${ONNXRUNTIME_ROOT}/lib/qladd.cpp
${ONNXRUNTIME_ROOT}/lib/qlmul.cpp
${ONNXRUNTIME_ROOT}/lib/qpostprocessor.cpp
${ONNXRUNTIME_ROOT}/lib/qlgavgpool.cpp
)
if (onnxruntime_BUILD_WEBASSEMBLY)
if (onnxruntime_ENABLE_WEBASSEMBLY_SIMD)
file(GLOB_RECURSE mlas_platform_srcs
"${ONNXRUNTIME_ROOT}/lib/wasm_simd/*.cpp"
)
else()
file(GLOB_RECURSE mlas_platform_srcs
"${ONNXRUNTIME_ROOT}/lib/wasm/*.cpp"
)
endif()
elseif(MSVC)
if(onnxruntime_target_platform STREQUAL "ARM64")
set(mlas_platform_preprocess_srcs
${ONNXRUNTIME_ROOT}/lib/arm64/QgemmU8X8KernelNeon.asm
${ONNXRUNTIME_ROOT}/lib/arm64/QgemmU8X8KernelUdot.asm
${ONNXRUNTIME_ROOT}/lib/arm64/SgemmKernelNeon.asm
)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(ARMASM_FLAGS "-g")
else()
set(ARMASM_FLAGS "")
endif()
# Run the C precompiler on each input before the assembler.
foreach(asm_filename ${mlas_platform_preprocess_srcs})
get_filename_component(asm_filename_base ${asm_filename} NAME_WLE)
set(preprocess_filename ${CMAKE_CURRENT_BINARY_DIR}/${asm_filename_base}.i)
set(obj_filename ${CMAKE_CURRENT_BINARY_DIR}/${asm_filename_base}.obj)
add_custom_command(
OUTPUT ${obj_filename}
COMMAND
cl.exe /P ${asm_filename} /Fi${preprocess_filename}
COMMAND
armasm64.exe ${ARMASM_FLAGS} ${preprocess_filename} ${obj_filename}
DEPENDS ${asm_filename}
BYPRODUCTS ${preprocess_filename}
)
list(APPEND mlas_platform_srcs ${obj_filename})
endforeach()
elseif((onnxruntime_target_platform STREQUAL "ARM") OR (onnxruntime_target_platform STREQUAL "ARM64EC"))
set(mlas_platform_srcs
${ONNXRUNTIME_ROOT}/lib/arm/sgemmc.cpp
)
elseif(onnxruntime_target_platform STREQUAL "x64")
enable_language(ASM_MASM)
file(GLOB_RECURSE mlas_platform_srcs_avx CONFIGURE_DEPENDS
"${ONNXRUNTIME_ROOT}/lib/intrinsics/avx/*.cpp"
)
set_source_files_properties(${mlas_platform_srcs_avx} PROPERTIES COMPILE_FLAGS "/arch:AVX")
file(GLOB_RECURSE mlas_platform_srcs_avx2 CONFIGURE_DEPENDS
"${ONNXRUNTIME_ROOT}/lib/intrinsics/avx2/*.cpp"
)
set_source_files_properties(${mlas_platform_srcs_avx2} PROPERTIES COMPILE_FLAGS "/arch:AVX2")
if (onnxruntime_MINIMAL_BUILD)
# exclude AVX512 in minimal build
set_source_files_properties(${mlas_common_srcs} PROPERTIES COMPILE_FLAGS "-DMLAS_AVX512F_UNSUPPORTED")
endif()
set(mlas_platform_srcs
${ONNXRUNTIME_ROOT}/lib/dgemm.cpp
${mlas_platform_srcs_avx}
${mlas_platform_srcs_avx2}
${ONNXRUNTIME_ROOT}/lib/intrinsics/avx512/quantize_avx512f.cpp
${ONNXRUNTIME_ROOT}/lib/amd64/QgemmU8S8KernelAvx2.asm
${ONNXRUNTIME_ROOT}/lib/amd64/QgemmU8U8KernelAvx2.asm
${ONNXRUNTIME_ROOT}/lib/amd64/QgemmU8X8KernelAvx2.asm
${ONNXRUNTIME_ROOT}/lib/amd64/QgemmU8X8KernelAvx512Core.asm
${ONNXRUNTIME_ROOT}/lib/amd64/QgemvU8S8KernelAvx2.asm
${ONNXRUNTIME_ROOT}/lib/amd64/QgemvU8S8KernelAvx512Core.asm
${ONNXRUNTIME_ROOT}/lib/amd64/QgemvU8S8KernelAvx512Vnni.asm
${ONNXRUNTIME_ROOT}/lib/amd64/QgemvU8S8KernelAvxVnni.asm
${ONNXRUNTIME_ROOT}/lib/amd64/DgemmKernelSse2.asm
${ONNXRUNTIME_ROOT}/lib/amd64/DgemmKernelAvx.asm
${ONNXRUNTIME_ROOT}/lib/amd64/DgemmKernelFma3.asm
${ONNXRUNTIME_ROOT}/lib/amd64/DgemmKernelAvx512F.asm
${ONNXRUNTIME_ROOT}/lib/amd64/SgemmKernelSse2.asm
${ONNXRUNTIME_ROOT}/lib/amd64/SgemmKernelAvx.asm
${ONNXRUNTIME_ROOT}/lib/amd64/SgemmKernelM1Avx.asm
${ONNXRUNTIME_ROOT}/lib/amd64/SgemmKernelFma3.asm
${ONNXRUNTIME_ROOT}/lib/amd64/SgemmKernelAvx512F.asm
${ONNXRUNTIME_ROOT}/lib/amd64/SconvKernelSse2.asm
${ONNXRUNTIME_ROOT}/lib/amd64/SconvKernelAvx.asm
${ONNXRUNTIME_ROOT}/lib/amd64/SconvKernelFma3.asm
${ONNXRUNTIME_ROOT}/lib/amd64/SconvKernelAvx512F.asm
${ONNXRUNTIME_ROOT}/lib/amd64/SpoolKernelSse2.asm
${ONNXRUNTIME_ROOT}/lib/amd64/SpoolKernelAvx.asm
${ONNXRUNTIME_ROOT}/lib/amd64/SpoolKernelAvx512F.asm
${ONNXRUNTIME_ROOT}/lib/amd64/sgemma.asm
${ONNXRUNTIME_ROOT}/lib/amd64/cvtfp16a.asm
${ONNXRUNTIME_ROOT}/lib/amd64/SoftmaxKernelAvx.asm
${ONNXRUNTIME_ROOT}/lib/amd64/TransKernelFma3.asm
${ONNXRUNTIME_ROOT}/lib/amd64/TransKernelAvx512F.asm
${ONNXRUNTIME_ROOT}/lib/amd64/LogisticKernelFma3.asm
${ONNXRUNTIME_ROOT}/lib/amd64/TanhKernelFma3.asm
${ONNXRUNTIME_ROOT}/lib/amd64/ErfKernelFma3.asm
)
else()
enable_language(ASM_MASM)
set(CMAKE_ASM_MASM_FLAGS "${CMAKE_ASM_MASM_FLAGS} /safeseh")
set(mlas_platform_srcs
${ONNXRUNTIME_ROOT}/lib/i386/SgemmKernelSse2.asm
${ONNXRUNTIME_ROOT}/lib/i386/SgemmKernelAvx.asm
)
endif()
else()
if (CMAKE_OSX_ARCHITECTURES STREQUAL "arm64")
set(ARM64 TRUE)
elseif (CMAKE_OSX_ARCHITECTURES STREQUAL "arm64e")
set(ARM64 TRUE)
elseif (CMAKE_OSX_ARCHITECTURES STREQUAL "arm")
set(ARM TRUE)
elseif (CMAKE_OSX_ARCHITECTURES STREQUAL "x86_64")
set(X86_64 TRUE)
elseif (CMAKE_OSX_ARCHITECTURES STREQUAL "i386")
set(X86 TRUE)
endif()
if (CMAKE_SYSTEM_NAME STREQUAL "Android")
if (CMAKE_ANDROID_ARCH_ABI STREQUAL "armeabi-v7a")
set(ARM TRUE)
elseif (CMAKE_ANDROID_ARCH_ABI STREQUAL "arm64-v8a")
set(ARM64 TRUE)
elseif (CMAKE_ANDROID_ARCH_ABI STREQUAL "x86_64")
set(X86_64 TRUE)
elseif (CMAKE_ANDROID_ARCH_ABI STREQUAL "x86")
set(X86 TRUE)
endif()
elseif(CMAKE_SYSTEM_NAME STREQUAL "iOS" OR CMAKE_SYSTEM_NAME STREQUAL "iOSCross")
set(IOS TRUE)
else()
execute_process(
COMMAND ${CMAKE_C_COMPILER} -dumpmachine
OUTPUT_VARIABLE dumpmachine_output
ERROR_QUIET
)
if(dumpmachine_output MATCHES "^arm64.*")
set(ARM64 TRUE)
elseif(dumpmachine_output MATCHES "^arm.*")
set(ARM TRUE)
elseif(dumpmachine_output MATCHES "^aarch64.*")
set(ARM64 TRUE)
elseif(dumpmachine_output MATCHES "^(powerpc.*|ppc.*)")
set(POWER TRUE)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(i.86|x86?)$")
set(X86 TRUE)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|amd64)$")
set(X86_64 TRUE)
endif()
endif()
if(ARM)
enable_language(ASM)
set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -mfpu=neon")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfpu=neon")
set(mlas_platform_srcs
${ONNXRUNTIME_ROOT}/lib/aarch32/QgemmU8X8KernelNeon.S
${ONNXRUNTIME_ROOT}/lib/arm/sgemmc.cpp
)
elseif(ARM64)
enable_language(ASM)
set(mlas_platform_srcs
${ONNXRUNTIME_ROOT}/lib/aarch64/QgemmU8X8KernelNeon.S
${ONNXRUNTIME_ROOT}/lib/aarch64/QgemmU8X8KernelUdot.S
${ONNXRUNTIME_ROOT}/lib/aarch64/SgemmKernelNeon.S
${ONNXRUNTIME_ROOT}/lib/aarch64/SgemvKernelNeon.S
)
elseif(POWER)
set(mlas_platform_srcs
${ONNXRUNTIME_ROOT}/lib/power/SgemmKernelPower.cpp
)
elseif(X86)
enable_language(ASM)
set(mlas_platform_srcs_sse2
${ONNXRUNTIME_ROOT}/lib/x86/SgemmKernelSse2.S
)
set_source_files_properties(${mlas_platform_srcs_sse2} PROPERTIES COMPILE_FLAGS "-msse2")
set(mlas_platform_srcs_avx
${ONNXRUNTIME_ROOT}/lib/x86/SgemmKernelAvx.S
)
set_source_files_properties(${mlas_platform_srcs_avx} PROPERTIES COMPILE_FLAGS "-mavx")
set(mlas_platform_srcs
${mlas_platform_srcs_sse2}
${mlas_platform_srcs_avx}
)
elseif(X86_64)
enable_language(ASM)
# Forward the flags for the minimum target platform version from the C
# compiler to the assembler. This works around CMakeASMCompiler.cmake.in
# not including the logic to set this flag for the assembler.
set(CMAKE_ASM${ASM_DIALECT}_OSX_DEPLOYMENT_TARGET_FLAG "${CMAKE_C_OSX_DEPLOYMENT_TARGET_FLAG}")
# The LLVM assembler does not support the .arch directive to enable instruction
# set extensions and also doesn't support AVX-512F instructions without
# turning on support via command-line option. Group the sources by the
# instruction set extension and explicitly set the compiler flag as appropriate.
set(mlas_platform_srcs_sse2
${ONNXRUNTIME_ROOT}/lib/x86_64/DgemmKernelSse2.S
${ONNXRUNTIME_ROOT}/lib/x86_64/SgemmKernelSse2.S
${ONNXRUNTIME_ROOT}/lib/x86_64/SgemmTransposePackB16x4Sse2.S
${ONNXRUNTIME_ROOT}/lib/x86_64/SconvKernelSse2.S
${ONNXRUNTIME_ROOT}/lib/x86_64/SpoolKernelSse2.S
)
set_source_files_properties(${mlas_platform_srcs_sse2} PROPERTIES COMPILE_FLAGS "-msse2")
set(mlas_platform_srcs_avx
${ONNXRUNTIME_ROOT}/lib/x86_64/DgemmKernelAvx.S
${ONNXRUNTIME_ROOT}/lib/x86_64/SgemmKernelAvx.S
${ONNXRUNTIME_ROOT}/lib/x86_64/SgemmKernelM1Avx.S
${ONNXRUNTIME_ROOT}/lib/x86_64/SgemmKernelM1TransposeBAvx.S
${ONNXRUNTIME_ROOT}/lib/x86_64/SgemmTransposePackB16x4Avx.S
${ONNXRUNTIME_ROOT}/lib/x86_64/SconvKernelAvx.S
${ONNXRUNTIME_ROOT}/lib/x86_64/SpoolKernelAvx.S
${ONNXRUNTIME_ROOT}/lib/x86_64/SoftmaxKernelAvx.S
${ONNXRUNTIME_ROOT}/lib/intrinsics/avx/min_max_elements.cpp
)
set_source_files_properties(${mlas_platform_srcs_avx} PROPERTIES COMPILE_FLAGS "-mavx")
set(mlas_platform_srcs_avx2
${ONNXRUNTIME_ROOT}/lib/x86_64/QgemmU8S8KernelAvx2.S
${ONNXRUNTIME_ROOT}/lib/x86_64/QgemvU8S8KernelAvx2.S
${ONNXRUNTIME_ROOT}/lib/x86_64/QgemmU8U8KernelAvx2.S
${ONNXRUNTIME_ROOT}/lib/x86_64/QgemvU8S8KernelAvxVnni.S
${ONNXRUNTIME_ROOT}/lib/x86_64/QgemmU8X8KernelAvx2.S
${ONNXRUNTIME_ROOT}/lib/x86_64/DgemmKernelFma3.S
${ONNXRUNTIME_ROOT}/lib/x86_64/SgemmKernelFma3.S
${ONNXRUNTIME_ROOT}/lib/x86_64/SconvKernelFma3.S
${ONNXRUNTIME_ROOT}/lib/x86_64/TransKernelFma3.S
${ONNXRUNTIME_ROOT}/lib/x86_64/LogisticKernelFma3.S
${ONNXRUNTIME_ROOT}/lib/x86_64/TanhKernelFma3.S
${ONNXRUNTIME_ROOT}/lib/x86_64/ErfKernelFma3.S
${ONNXRUNTIME_ROOT}/lib/intrinsics/avx2/qladd_avx2.cpp
${ONNXRUNTIME_ROOT}/lib/intrinsics/avx2/qdwconv_avx2.cpp
)
set_source_files_properties(${mlas_platform_srcs_avx2} PROPERTIES COMPILE_FLAGS "-mavx2 -mfma")
# Some toolchains do not support AVX512 compiler flags but are still able
# to build the sources. Other toolchains require the AVX512 compiler flags
# to be specified.
check_cxx_compiler_flag("-mavx512f" HAS_AVX512F)
if(HAS_AVX512F)
set(CMAKE_REQUIRED_FLAGS "-mavx512f")
else()
set(CMAKE_REQUIRED_FLAGS "")
endif()
check_cxx_source_compiles("
int main() {
asm(\"vpxord %zmm0,%zmm0,%zmm0\");
return 0;
}"
COMPILES_AVX512F
)
if(COMPILES_AVX512F AND NOT onnxruntime_MINIMAL_BUILD)
set(mlas_platform_srcs_avx512f
${ONNXRUNTIME_ROOT}/lib/x86_64/DgemmKernelAvx512F.S
${ONNXRUNTIME_ROOT}/lib/x86_64/SgemmKernelAvx512F.S
${ONNXRUNTIME_ROOT}/lib/x86_64/SconvKernelAvx512F.S
${ONNXRUNTIME_ROOT}/lib/x86_64/SpoolKernelAvx512F.S
${ONNXRUNTIME_ROOT}/lib/x86_64/TransKernelAvx512F.S
)
check_cxx_source_compiles("
#include <immintrin.h>
int main() {
__m512 zeros = _mm512_set1_ps(0.f);
(void)zeros;
return 0;
}"
COMPILES_AVX512F_INTRINSICS
)
if(COMPILES_AVX512F_INTRINSICS)
set(mlas_platform_srcs_avx512f
${ONNXRUNTIME_ROOT}/lib/intrinsics/avx512/quantize_avx512f.cpp
${mlas_platform_srcs_avx512f}
)
else()
set_source_files_properties(${mlas_common_srcs} PROPERTIES COMPILE_FLAGS "-DMLAS_AVX512F_INTRINSICS_UNSUPPORTED")
endif()
if(HAS_AVX512F)
set_source_files_properties(${mlas_platform_srcs_avx512f} PROPERTIES COMPILE_FLAGS "-mavx512f")
endif()
check_cxx_compiler_flag("-mavx512bw -mavx512dq -mavx512vl" HAS_AVX512CORE)
if(HAS_AVX512CORE)
set(CMAKE_REQUIRED_FLAGS "-mavx512bw -mavx512dq -mavx512vl")
endif()
check_cxx_source_compiles("
int main() {
asm(\"vpmaddwd %zmm0,%zmm0,%zmm0\"); // AVX512BW feature
asm(\"vandnps %xmm31,%xmm31,%xmm31\"); // AVX512DQ/AVX512VL feature
return 0;
}"
COMPILES_AVX512CORE
)
if(COMPILES_AVX512CORE)
set(mlas_platform_srcs_avx512core
${ONNXRUNTIME_ROOT}/lib/x86_64/QgemvU8S8KernelAvx512Core.S
${ONNXRUNTIME_ROOT}/lib/x86_64/QgemvU8S8KernelAvx512Vnni.S
${ONNXRUNTIME_ROOT}/lib/x86_64/QgemmU8X8KernelAvx512Core.S
)
if(HAS_AVX512CORE)
set_source_files_properties(${mlas_platform_srcs_avx512core} PROPERTIES COMPILE_FLAGS "-mavx512bw -mavx512dq -mavx512vl")
endif()
else()
set_source_files_properties(${mlas_common_srcs} PROPERTIES COMPILE_FLAGS "-DMLAS_AVX512CORE_UNSUPPORTED")
endif()
else()
set_source_files_properties(${mlas_common_srcs} PROPERTIES COMPILE_FLAGS "-DMLAS_AVX512F_UNSUPPORTED")
endif()
set(mlas_platform_srcs
${ONNXRUNTIME_ROOT}/lib/dgemm.cpp
${mlas_platform_srcs_sse2}
${mlas_platform_srcs_avx}
${mlas_platform_srcs_avx2}
${mlas_platform_srcs_avx512f}
${mlas_platform_srcs_avx512core}
)
endif()
endif()
add_library(onnxruntime_mlas SHARED ${mlas_common_srcs} ${mlas_platform_srcs})
target_include_directories(onnxruntime_mlas PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/inc ${CMAKE_CURRENT_SOURCE_DIR}/lib)
add_library(onnxruntime_mlas_static STATIC ${mlas_common_srcs} ${mlas_platform_srcs})
target_include_directories(onnxruntime_mlas_static PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/inc ${CMAKE_CURRENT_SOURCE_DIR}/lib)
# Does not support ONNXRUNTIME threadpool
target_compile_definitions(onnxruntime_mlas PUBLIC MLAS_NO_ONNXRUNTIME_THREADPOOL)
target_compile_definitions(onnxruntime_mlas_static PUBLIC MLAS_NO_ONNXRUNTIME_THREADPOOL)