forked from microsoft/onnxruntime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonnxruntime_unittests.cmake
783 lines (679 loc) · 31.4 KB
/
onnxruntime_unittests.cmake
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
set(TEST_SRC_DIR ${ONNXRUNTIME_ROOT}/test)
set(TEST_INC_DIR ${ONNXRUNTIME_ROOT})
if (onnxruntime_USE_TVM)
list(APPEND TEST_INC_DIR ${TVM_INCLUDES})
endif()
if (onnxruntime_USE_OPENVINO)
list(APPEND TEST_INC_DIR ${OPENVINO_INCLUDE_DIR})
endif()
set(disabled_warnings)
set(extra_includes)
function(AddTest)
cmake_parse_arguments(_UT "DYN" "TARGET" "LIBS;SOURCES;DEPENDS" ${ARGN})
if(_UT_LIBS)
list(REMOVE_DUPLICATES _UT_LIBS)
endif()
list(REMOVE_DUPLICATES _UT_SOURCES)
if (_UT_DEPENDS)
list(REMOVE_DUPLICATES _UT_DEPENDS)
endif(_UT_DEPENDS)
add_executable(${_UT_TARGET} ${_UT_SOURCES})
if (MSVC AND NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
#TODO: fix the warnings, they are dangerous
target_compile_options(${_UT_TARGET} PRIVATE "/wd4244")
endif()
source_group(TREE ${TEST_SRC_DIR} FILES ${_UT_SOURCES})
set_target_properties(${_UT_TARGET} PROPERTIES FOLDER "ONNXRuntimeTest")
if (_UT_DEPENDS)
add_dependencies(${_UT_TARGET} ${_UT_DEPENDS})
endif(_UT_DEPENDS)
if(_UT_DYN)
target_link_libraries(${_UT_TARGET} PRIVATE ${_UT_LIBS} gtest gmock onnxruntime ${CMAKE_DL_LIBS} Threads::Threads)
else()
target_link_libraries(${_UT_TARGET} PRIVATE ${_UT_LIBS} gtest gmock ${onnxruntime_EXTERNAL_LIBRARIES})
endif()
onnxruntime_add_include_to_target(${_UT_TARGET} date_interface)
target_include_directories(${_UT_TARGET} PRIVATE ${TEST_INC_DIR})
if (onnxruntime_USE_CUDA)
target_include_directories(${_UT_TARGET} PRIVATE ${CUDA_INCLUDE_DIRS} ${onnxruntime_CUDNN_HOME}/include)
endif()
if (onnxruntime_ENABLE_LANGUAGE_INTEROP_OPS AND onnxruntime_ENABLE_PYTHON)
target_compile_definitions(${_UT_TARGET} PRIVATE ENABLE_LANGUAGE_INTEROP_OPS)
endif()
if (WIN32)
if (onnxruntime_USE_CUDA)
# disable a warning from the CUDA headers about unreferenced local functions
if (MSVC)
target_compile_options(${_UT_TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:-Xcompiler /wd4505>"
"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/wd4505>")
endif()
endif()
target_compile_options(${_UT_TARGET} PRIVATE ${disabled_warnings})
else()
target_compile_options(${_UT_TARGET} PRIVATE ${DISABLED_WARNINGS_FOR_TVM})
target_compile_options(${_UT_TARGET} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:-Xcompiler -Wno-error=sign-compare>"
"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:-Wno-error=sign-compare>")
endif()
set(TEST_ARGS)
if (onnxruntime_GENERATE_TEST_REPORTS)
# generate a report file next to the test program
list(APPEND TEST_ARGS
"--gtest_output=xml:$<SHELL_PATH:$<TARGET_FILE:${_UT_TARGET}>.$<CONFIG>.results.xml>")
endif(onnxruntime_GENERATE_TEST_REPORTS)
add_test(NAME ${_UT_TARGET}
COMMAND ${_UT_TARGET} ${TEST_ARGS}
WORKING_DIRECTORY $<TARGET_FILE_DIR:${_UT_TARGET}>
)
endfunction(AddTest)
#Do not add '${TEST_SRC_DIR}/util/include' to your include directories directly
#Use onnxruntime_add_include_to_target or target_link_libraries, so that compile definitions
#can propagate correctly.
file(GLOB onnxruntime_test_utils_src CONFIGURE_DEPENDS
"${TEST_SRC_DIR}/util/include/*.h"
"${TEST_SRC_DIR}/util/*.cc"
)
file(GLOB onnxruntime_test_common_src CONFIGURE_DEPENDS
"${TEST_SRC_DIR}/common/*.cc"
"${TEST_SRC_DIR}/common/*.h"
"${TEST_SRC_DIR}/common/logging/*.cc"
"${TEST_SRC_DIR}/common/logging/*.h"
)
file(GLOB onnxruntime_test_ir_src CONFIGURE_DEPENDS
"${TEST_SRC_DIR}/ir/*.cc"
"${TEST_SRC_DIR}/ir/*.h"
)
file(GLOB onnxruntime_test_optimizer_src CONFIGURE_DEPENDS
"${TEST_SRC_DIR}/optimizer/*.cc"
"${TEST_SRC_DIR}/optimizer/*.h"
)
set(onnxruntime_test_framework_src_patterns
"${TEST_SRC_DIR}/framework/*.cc"
"${TEST_SRC_DIR}/framework/*.h"
"${TEST_SRC_DIR}/platform/*.cc"
)
if(WIN32)
list(APPEND onnxruntime_test_framework_src_patterns
"${TEST_SRC_DIR}/platform/windows/*.cc"
"${TEST_SRC_DIR}/platform/windows/logging/*.cc" )
endif()
if(onnxruntime_USE_CUDA)
list(APPEND onnxruntime_test_framework_src_patterns ${TEST_SRC_DIR}/framework/cuda/*)
endif()
set(onnxruntime_test_providers_src_patterns
"${TEST_SRC_DIR}/providers/*.h"
"${TEST_SRC_DIR}/providers/*.cc"
"${TEST_SRC_DIR}/framework/TestAllocatorManager.cc"
"${TEST_SRC_DIR}/framework/TestAllocatorManager.h"
"${TEST_SRC_DIR}/framework/test_utils.cc"
"${TEST_SRC_DIR}/framework/test_utils.h"
)
if(NOT onnxruntime_DISABLE_CONTRIB_OPS)
list(APPEND onnxruntime_test_providers_src_patterns
"${TEST_SRC_DIR}/contrib_ops/*.h"
"${TEST_SRC_DIR}/contrib_ops/*.cc")
endif()
if(onnxruntime_USE_FEATURIZERS)
list(APPEND onnxruntime_test_providers_src_patterns
"${TEST_SRC_DIR}/featurizers_ops/*.h"
"${TEST_SRC_DIR}/featurizers_ops/*.cc")
endif()
file(GLOB onnxruntime_test_providers_src CONFIGURE_DEPENDS
${onnxruntime_test_providers_src_patterns})
file(GLOB_RECURSE onnxruntime_test_providers_cpu_src CONFIGURE_DEPENDS
"${TEST_SRC_DIR}/providers/cpu/*"
)
list(APPEND onnxruntime_test_providers_src ${onnxruntime_test_providers_cpu_src})
if (onnxruntime_USE_NGRAPH)
file(GLOB_RECURSE onnxruntime_test_providers_ngraph_src CONFIGURE_DEPENDS
"${TEST_SRC_DIR}/providers/ngraph/*"
)
list(APPEND onnxruntime_test_providers_src ${onnxruntime_test_providers_ngraph_src})
endif()
if (onnxruntime_USE_NNAPI)
file(GLOB_RECURSE onnxruntime_test_providers_nnapi_src CONFIGURE_DEPENDS
"${TEST_SRC_DIR}/providers/nnapi/*"
)
list(APPEND onnxruntime_test_providers_src ${onnxruntime_test_providers_nnapi_src})
endif()
# tests from lowest level library up.
# the order of libraries should be maintained, with higher libraries being added first in the list
set(onnxruntime_test_common_libs
onnxruntime_test_utils
onnxruntime_common
)
set(onnxruntime_test_ir_libs
onnxruntime_test_utils
onnxruntime_graph
onnxruntime_common
)
set(onnxruntime_test_optimizer_libs
onnxruntime_test_utils
onnxruntime_framework
onnxruntime_util
onnxruntime_graph
onnxruntime_common
)
set(onnxruntime_test_framework_libs
onnxruntime_test_utils_for_framework
onnxruntime_framework
onnxruntime_util
onnxruntime_graph
onnxruntime_common
onnxruntime_mlas
)
set(onnxruntime_test_server_libs
onnxruntime_test_utils_for_framework
onnxruntime_test_utils_for_server
)
if(WIN32)
list(APPEND onnxruntime_test_framework_libs Advapi32)
endif()
set (onnxruntime_test_providers_dependencies ${onnxruntime_EXTERNAL_DEPENDENCIES})
if(onnxruntime_USE_CUDA)
list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_cuda)
endif()
if(onnxruntime_USE_DNNL)
list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_dnnl)
endif()
if(onnxruntime_USE_NGRAPH)
list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_ngraph)
endif()
if(onnxruntime_USE_OPENVINO)
list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_openvino)
endif()
if(onnxruntime_USE_NNAPI)
list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_nnapi)
endif()
if(onnxruntime_USE_FEATURIZERS)
list(APPEND onnxruntime_test_providers_dependencies onnxruntime_featurizers)
endif()
if(onnxruntime_USE_DML)
list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_dml)
endif()
file(GLOB_RECURSE onnxruntime_test_tvm_src CONFIGURE_DEPENDS
"${ONNXRUNTIME_ROOT}/test/tvm/*.h"
"${ONNXRUNTIME_ROOT}/test/tvm/*.cc"
)
file(GLOB_RECURSE onnxruntime_test_openvino_src
"${ONNXRUNTIME_ROOT}/test/openvino/*.h"
"${ONNXRUNTIME_ROOT}/test/openvino/*.cc"
)
if(onnxruntime_USE_NUPHAR)
list(APPEND onnxruntime_test_framework_src_patterns ${TEST_SRC_DIR}/framework/nuphar/*)
list(APPEND onnxruntime_test_framework_libs onnxruntime_providers_nuphar)
list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_nuphar)
list(APPEND onnxruntime_test_providers_libs onnxruntime_providers_nuphar)
endif()
if(onnxruntime_USE_ACL)
list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_acl)
endif()
if (onnxruntime_ENABLE_MICROSOFT_INTERNAL)
include(onnxruntime_unittests_internal.cmake)
endif()
if (onnxruntime_ENABLE_LANGUAGE_INTEROP_OPS)
set(ONNXRUNTIME_INTEROP_TEST_LIBS PRIVATE onnxruntime_language_interop onnxruntime_pyop)
endif()
set(ONNXRUNTIME_TEST_LIBS
onnxruntime_session
${ONNXRUNTIME_INTEROP_TEST_LIBS}
${onnxruntime_libs}
${PROVIDERS_CUDA}
${PROVIDERS_DNNL}
${PROVIDERS_TENSORRT}
${PROVIDERS_NGRAPH}
${PROVIDERS_OPENVINO}
${PROVIDERS_NUPHAR}
${PROVIDERS_NNAPI}
${PROVIDERS_DML}
${PROVIDERS_ACL}
onnxruntime_optimizer
onnxruntime_providers
onnxruntime_util
${onnxruntime_tvm_libs}
onnxruntime_framework
onnxruntime_util
onnxruntime_graph
onnxruntime_common
onnxruntime_mlas
)
set(onnxruntime_test_providers_libs
onnxruntime_test_utils_for_framework
${ONNXRUNTIME_TEST_LIBS}
)
if(onnxruntime_USE_TENSORRT)
list(APPEND onnxruntime_test_framework_src_patterns ${TEST_SRC_DIR}/providers/tensorrt/*)
list(APPEND onnxruntime_test_framework_libs onnxruntime_providers_tensorrt)
list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_tensorrt)
list(APPEND onnxruntime_test_providers_libs onnxruntime_providers_tensorrt)
endif()
if(onnxruntime_USE_NNAPI)
list(APPEND onnxruntime_test_framework_src_patterns ${TEST_SRC_DIR}/providers/nnapi/*)
list(APPEND onnxruntime_test_framework_libs onnxruntime_providers_nnapi)
list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_nnapi)
list(APPEND onnxruntime_test_providers_libs onnxruntime_providers_nnapi)
endif()
if(WIN32)
if (onnxruntime_USE_TVM)
list(APPEND disabled_warnings ${DISABLED_WARNINGS_FOR_TVM})
endif()
endif()
file(GLOB onnxruntime_test_framework_src CONFIGURE_DEPENDS
${onnxruntime_test_framework_src_patterns}
)
#with auto initialize onnxruntime
add_library(onnxruntime_test_utils_for_framework ${onnxruntime_test_utils_src})
onnxruntime_add_include_to_target(onnxruntime_test_utils_for_framework onnxruntime_framework gtest onnx onnx_proto)
if (onnxruntime_USE_FULL_PROTOBUF)
target_compile_definitions(onnxruntime_test_utils_for_framework PRIVATE USE_FULL_PROTOBUF=1)
endif()
if (onnxruntime_USE_DNNL)
target_compile_definitions(onnxruntime_test_utils_for_framework PUBLIC USE_DNNL=1)
endif()
add_dependencies(onnxruntime_test_utils_for_framework ${onnxruntime_EXTERNAL_DEPENDENCIES})
target_include_directories(onnxruntime_test_utils_for_framework PUBLIC "${TEST_SRC_DIR}/util/include" PRIVATE ${eigen_INCLUDE_DIRS} ${ONNXRUNTIME_ROOT})
# Add the define for conditionally using the framework Environment class in TestEnvironment
target_compile_definitions(onnxruntime_test_utils_for_framework PUBLIC "HAVE_FRAMEWORK_LIB")
set_target_properties(onnxruntime_test_utils_for_framework PROPERTIES FOLDER "ONNXRuntimeTest")
#without auto initialize onnxruntime
add_library(onnxruntime_test_utils ${onnxruntime_test_utils_src})
onnxruntime_add_include_to_target(onnxruntime_test_utils onnxruntime_framework gtest onnx onnx_proto)
if (onnxruntime_USE_FULL_PROTOBUF)
target_compile_definitions(onnxruntime_test_utils PRIVATE USE_FULL_PROTOBUF=1)
endif()
if (onnxruntime_USE_DNNL)
target_compile_definitions(onnxruntime_test_utils PUBLIC USE_DNNL=1)
endif()
add_dependencies(onnxruntime_test_utils ${onnxruntime_EXTERNAL_DEPENDENCIES})
target_include_directories(onnxruntime_test_utils PUBLIC "${TEST_SRC_DIR}/util/include" PRIVATE ${eigen_INCLUDE_DIRS} ${ONNXRUNTIME_ROOT})
set_target_properties(onnxruntime_test_utils PROPERTIES FOLDER "ONNXRuntimeTest")
if (SingleUnitTestProject)
set(all_tests ${onnxruntime_test_common_src} ${onnxruntime_test_ir_src} ${onnxruntime_test_optimizer_src} ${onnxruntime_test_framework_src} ${onnxruntime_test_providers_src})
set(all_dependencies ${onnxruntime_test_providers_dependencies} )
if (onnxruntime_USE_TVM)
list(APPEND all_tests ${onnxruntime_test_tvm_src})
endif()
if (onnxruntime_USE_OPENVINO)
list(APPEND all_tests ${onnxruntime_test_openvino_src})
endif()
# we can only have one 'main', so remove them all and add back the providers test_main as it sets
# up everything we need for all tests
file(GLOB_RECURSE test_mains CONFIGURE_DEPENDS
"${TEST_SRC_DIR}/*/test_main.cc"
)
list(REMOVE_ITEM all_tests ${test_mains})
list(APPEND all_tests "${TEST_SRC_DIR}/providers/test_main.cc")
# this is only added to onnxruntime_test_framework_libs above, but we use onnxruntime_test_providers_libs for the onnxruntime_test_all target.
# for now, add it here. better is probably to have onnxruntime_test_providers_libs use the full onnxruntime_test_framework_libs
# list given it's built on top of that library and needs all the same dependencies.
if(WIN32)
list(APPEND onnxruntime_test_providers_libs Advapi32)
endif()
AddTest(
TARGET onnxruntime_test_all
SOURCES ${all_tests}
LIBS ${onnxruntime_test_providers_libs} ${onnxruntime_test_common_libs}
DEPENDS ${all_dependencies}
)
# the default logger tests conflict with the need to have an overall default logger
# so skip in this type of
target_compile_definitions(onnxruntime_test_all PUBLIC -DSKIP_DEFAULT_LOGGER_TESTS)
if (onnxruntime_ENABLE_LANGUAGE_INTEROP_OPS)
target_link_libraries(onnxruntime_test_all PRIVATE onnxruntime_language_interop onnxruntime_pyop)
endif()
set(test_data_target onnxruntime_test_all)
else()
AddTest(
TARGET onnxruntime_test_common
SOURCES ${onnxruntime_test_common_src}
LIBS ${onnxruntime_test_common_libs}
DEPENDS ${onnxruntime_EXTERNAL_DEPENDENCIES}
)
AddTest(
TARGET onnxruntime_test_ir
SOURCES ${onnxruntime_test_ir_src}
LIBS ${onnxruntime_test_ir_libs}
DEPENDS ${onnxruntime_EXTERNAL_DEPENDENCIES}
)
AddTest(
TARGET onnxruntime_test_optimizer
SOURCES ${onnxruntime_test_optimizer_src}
LIBS ${onnxruntime_test_optimizer_libs}
DEPENDS ${onnxruntime_EXTERNAL_DEPENDENCIES}
)
AddTest(
TARGET onnxruntime_test_framework
SOURCES ${onnxruntime_test_framework_src}
LIBS ${onnxruntime_test_framework_libs}
# code smell! see if CPUExecutionProvider should move to framework so onnxruntime_providers isn't needed.
DEPENDS ${onnxruntime_test_providers_dependencies}
)
AddTest(
TARGET onnxruntime_test_providers
SOURCES ${onnxruntime_test_providers_src}
LIBS ${onnxruntime_test_providers_libs}
DEPENDS ${onnxruntime_test_providers_dependencies}
)
set(test_data_target onnxruntime_test_ir)
endif() # SingleUnitTestProject
# standalone test for inference session without environment
# the normal test executables set up a default runtime environment, which we don't want here
AddTest(
TARGET onnxruntime_test_framework_session_without_environment_standalone
SOURCES "${TEST_SRC_DIR}/framework/inference_session_without_environment/inference_session_without_environment_standalone_test.cc" "${TEST_SRC_DIR}/framework/test_main.cc"
LIBS onnxruntime_test_utils ${ONNXRUNTIME_TEST_LIBS}
DEPENDS ${onnxruntime_EXTERNAL_DEPENDENCIES}
)
#
# onnxruntime_ir_graph test data
#
set(TEST_DATA_SRC ${TEST_SRC_DIR}/testdata)
set(TEST_DATA_DES $<TARGET_FILE_DIR:${test_data_target}>/testdata)
# Copy test data from source to destination.
add_custom_command(
TARGET ${test_data_target} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${TEST_DATA_SRC}
${TEST_DATA_DES})
if(WIN32)
if (onnxruntime_USE_DNNL)
list(APPEND onnx_test_libs dnnl)
add_custom_command(
TARGET ${test_data_target} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${DNNL_DLL_PATH} $<TARGET_FILE_DIR:${test_data_target}>
)
endif()
if (onnxruntime_USE_MKLML)
add_custom_command(
TARGET ${test_data_target} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${MKLML_LIB_DIR}/${MKLML_SHARED_LIB} ${MKLML_LIB_DIR}/${IOMP5MD_SHARED_LIB}
$<TARGET_FILE_DIR:${test_data_target}>
)
endif()
if (onnxruntime_USE_OPENVINO)
add_custom_command(
TARGET ${test_data_target} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${OPENVINO_CPU_EXTENSION_DIR}/${OPENVINO_CPU_EXTENSION_LIB}
$<TARGET_FILE_DIR:${test_data_target}>
)
endif()
if (onnxruntime_USE_NGRAPH)
add_custom_command(
TARGET ${test_data_target} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${ngraph_LIBRARIES}/
$<TARGET_FILE_DIR:${test_data_target}>
)
endif()
if (onnxruntime_USE_TVM)
add_custom_command(
TARGET ${test_data_target} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:tvm> $<TARGET_FILE_DIR:${test_data_target}>
)
endif()
endif()
add_library(onnx_test_data_proto ${TEST_SRC_DIR}/proto/tml.proto)
add_dependencies(onnx_test_data_proto onnx_proto ${onnxruntime_EXTERNAL_DEPENDENCIES})
if(WIN32)
target_compile_options(onnx_test_data_proto PRIVATE "/wd4125" "/wd4456" "/wd4100" "/wd4267")
else()
if(HAS_UNUSED_PARAMETER)
target_compile_options(onnx_test_data_proto PRIVATE "-Wno-unused-parameter")
endif()
if(HAS_UNUSED_VARIABLE)
target_compile_options(onnx_test_data_proto PRIVATE "-Wno-unused-variable")
endif()
if(HAS_UNUSED_BUT_SET_VARIABLE)
target_compile_options(onnx_test_data_proto PRIVATE "-Wno-unused-but-set-variable")
endif()
endif()
add_dependencies(onnx_test_data_proto onnx_proto ${onnxruntime_EXTERNAL_DEPENDENCIES})
onnxruntime_add_include_to_target(onnx_test_data_proto onnx_proto)
target_include_directories(onnx_test_data_proto PRIVATE ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/onnx)
set_target_properties(onnx_test_data_proto PROPERTIES FOLDER "ONNXRuntimeTest")
onnxruntime_protobuf_generate(APPEND_PATH IMPORT_DIRS ${ONNXRUNTIME_ROOT}/core/protobuf TARGET onnx_test_data_proto)
set(onnx_test_runner_src_dir ${TEST_SRC_DIR}/onnx)
set(onnx_test_runner_common_srcs
${onnx_test_runner_src_dir}/TestResultStat.cc
${onnx_test_runner_src_dir}/TestResultStat.h
${onnx_test_runner_src_dir}/testenv.h
${onnx_test_runner_src_dir}/FixedCountFinishCallback.h
${onnx_test_runner_src_dir}/TestCaseResult.cc
${onnx_test_runner_src_dir}/TestCaseResult.h
${onnx_test_runner_src_dir}/testenv.cc
${onnx_test_runner_src_dir}/heap_buffer.h
${onnx_test_runner_src_dir}/heap_buffer.cc
${onnx_test_runner_src_dir}/OrtValueList.h
${onnx_test_runner_src_dir}/runner.h
${onnx_test_runner_src_dir}/runner.cc
${onnx_test_runner_src_dir}/TestCase.cc
${onnx_test_runner_src_dir}/TestCase.h
${onnx_test_runner_src_dir}/onnxruntime_event.h
${onnx_test_runner_src_dir}/sync_api.h
${onnx_test_runner_src_dir}/sync_api.cc
${onnx_test_runner_src_dir}/callback.h
${onnx_test_runner_src_dir}/callback.cc
${onnx_test_runner_src_dir}/pb_helper.h
${onnx_test_runner_src_dir}/pb_helper.cc
${onnx_test_runner_src_dir}/mem_buffer.h
${onnx_test_runner_src_dir}/tensorprotoutils.h
${onnx_test_runner_src_dir}/tensorprotoutils.cc)
if(WIN32)
set(wide_get_opt_src_dir ${TEST_SRC_DIR}/win_getopt/wide)
add_library(win_getopt_wide ${wide_get_opt_src_dir}/getopt.cc ${wide_get_opt_src_dir}/include/getopt.h)
target_include_directories(win_getopt_wide INTERFACE ${wide_get_opt_src_dir}/include)
set_target_properties(win_getopt_wide PROPERTIES FOLDER "ONNXRuntimeTest")
set(onnx_test_runner_common_srcs ${onnx_test_runner_common_srcs})
set(GETOPT_LIB_WIDE win_getopt_wide)
endif()
add_library(onnx_test_runner_common ${onnx_test_runner_common_srcs})
if (MSVC AND NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
#TODO: fix the warnings, they are dangerous
target_compile_options(onnx_test_runner_common PRIVATE "/wd4244")
endif()
onnxruntime_add_include_to_target(onnx_test_runner_common onnxruntime_common onnxruntime_framework onnxruntime_test_utils onnx onnx_proto)
add_dependencies(onnx_test_runner_common onnx_test_data_proto ${onnxruntime_EXTERNAL_DEPENDENCIES})
target_include_directories(onnx_test_runner_common PRIVATE ${eigen_INCLUDE_DIRS} ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/onnx ${ONNXRUNTIME_ROOT} ${REPO_ROOT}/cmake/external/re2)
set_target_properties(onnx_test_runner_common PROPERTIES FOLDER "ONNXRuntimeTest")
set(onnx_test_libs
onnxruntime_test_utils
${ONNXRUNTIME_TEST_LIBS}
onnx_test_data_proto
re2 ${onnxruntime_EXTERNAL_LIBRARIES})
if (onnxruntime_ENABLE_LANGUAGE_INTEROP_OPS)
list(APPEND onnx_test_libs onnxruntime_language_interop onnxruntime_pyop)
endif()
add_executable(onnx_test_runner ${onnx_test_runner_src_dir}/main.cc)
target_link_libraries(onnx_test_runner PRIVATE onnx_test_runner_common ${GETOPT_LIB_WIDE} ${onnx_test_libs})
target_include_directories(onnx_test_runner PRIVATE ${ONNXRUNTIME_ROOT})
set_target_properties(onnx_test_runner PROPERTIES FOLDER "ONNXRuntimeTest")
if (onnxruntime_USE_TVM)
if (WIN32)
target_link_options(onnx_test_runner PRIVATE "/STACK:4000000")
endif()
endif()
install(TARGETS onnx_test_runner
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
if(onnxruntime_BUILD_BENCHMARKS)
add_executable(onnxruntime_benchmark ${TEST_SRC_DIR}/onnx/microbenchmark/main.cc ${TEST_SRC_DIR}/onnx/microbenchmark/modeltest.cc)
target_include_directories(onnxruntime_benchmark PRIVATE ${ONNXRUNTIME_ROOT} ${onnxruntime_graph_header} benchmark)
if(WIN32)
target_compile_options(onnxruntime_benchmark PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:-Xcompiler /wd4141>"
"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/wd4141>")
endif()
target_link_libraries(onnxruntime_benchmark PRIVATE onnx_test_runner_common benchmark ${onnx_test_libs})
add_dependencies(onnxruntime_benchmark ${onnxruntime_EXTERNAL_DEPENDENCIES})
set_target_properties(onnxruntime_benchmark PROPERTIES FOLDER "ONNXRuntimeTest")
endif()
if(WIN32)
target_compile_options(onnx_test_runner_common PRIVATE -D_CRT_SECURE_NO_WARNINGS)
endif()
add_test(NAME onnx_test_pytorch_converted
COMMAND onnx_test_runner ${PROJECT_SOURCE_DIR}/external/onnx/onnx/backend/test/data/pytorch-converted)
add_test(NAME onnx_test_pytorch_operator
COMMAND onnx_test_runner ${PROJECT_SOURCE_DIR}/external/onnx/onnx/backend/test/data/pytorch-operator)
#perf test runner
set(onnxruntime_perf_test_src_dir ${TEST_SRC_DIR}/perftest)
set(onnxruntime_perf_test_src_patterns
"${onnxruntime_perf_test_src_dir}/*.cc"
"${onnxruntime_perf_test_src_dir}/*.h")
if(WIN32)
list(APPEND onnxruntime_perf_test_src_patterns
"${onnxruntime_perf_test_src_dir}/windows/*.cc"
"${onnxruntime_perf_test_src_dir}/windows/*.h" )
else ()
list(APPEND onnxruntime_perf_test_src_patterns
"${onnxruntime_perf_test_src_dir}/posix/*.cc"
"${onnxruntime_perf_test_src_dir}/posix/*.h" )
endif()
file(GLOB onnxruntime_perf_test_src CONFIGURE_DEPENDS
${onnxruntime_perf_test_src_patterns}
)
add_executable(onnxruntime_perf_test ${onnxruntime_perf_test_src} ${ONNXRUNTIME_ROOT}/core/framework/path_lib.cc)
target_include_directories(onnxruntime_perf_test PRIVATE ${onnx_test_runner_src_dir} ${ONNXRUNTIME_ROOT}
${eigen_INCLUDE_DIRS} ${extra_includes} ${onnxruntime_graph_header} ${onnxruntime_exec_src_dir}
${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/onnx)
if (WIN32)
target_compile_options(onnxruntime_perf_test PRIVATE ${disabled_warnings})
SET(SYS_PATH_LIB shlwapi)
endif()
if (onnxruntime_BUILD_SHARED_LIB)
set(onnxruntime_perf_test_libs onnxruntime_test_utils onnx_test_runner_common onnxruntime_common re2
onnx_test_data_proto onnx_proto protobuf::libprotobuf ${GETOPT_LIB_WIDE} onnxruntime ${onnxruntime_EXTERNAL_LIBRARIES}
${SYS_PATH_LIB} ${CMAKE_DL_LIBS})
if(onnxruntime_USE_NSYNC)
list(APPEND onnxruntime_perf_test_libs nsync_cpp)
endif()
target_link_libraries(onnxruntime_perf_test PRIVATE ${onnxruntime_perf_test_libs} Threads::Threads)
if(tensorflow_C_PACKAGE_PATH)
target_include_directories(onnxruntime_perf_test PRIVATE ${tensorflow_C_PACKAGE_PATH}/include)
target_link_directories(onnxruntime_perf_test PRIVATE ${tensorflow_C_PACKAGE_PATH}/lib)
target_link_libraries(onnxruntime_perf_test PRIVATE tensorflow)
target_compile_definitions(onnxruntime_perf_test PRIVATE HAVE_TENSORFLOW)
endif()
else()
target_link_libraries(onnxruntime_perf_test PRIVATE onnx_test_runner_common ${GETOPT_LIB_WIDE} ${onnx_test_libs})
endif()
set_target_properties(onnxruntime_perf_test PROPERTIES FOLDER "ONNXRuntimeTest")
if (onnxruntime_ENABLE_LANGUAGE_INTEROP_OPS AND NOT onnxruntime_BUILD_SHARED_LIB)
target_link_libraries(onnxruntime_perf_test PRIVATE onnxruntime_language_interop onnxruntime_pyop)
endif()
if (onnxruntime_USE_TVM)
if (WIN32)
target_link_options(onnxruntime_perf_test PRIVATE "/STACK:4000000")
endif()
endif()
# Opaque API test can not be a part of the shared lib tests since it is using
# C++ internals apis to register custom type, kernel and schema. It also can not
# a part of providers unit tests since it requires its own environment.
set(opaque_api_test_srcs ${ONNXRUNTIME_ROOT}/test/opaque_api/test_opaque_api.cc)
AddTest(
TARGET opaque_api_test
SOURCES ${opaque_api_test_srcs}
LIBS ${onnxruntime_test_providers_libs} ${onnxruntime_test_common_libs}
DEPENDS ${onnxruntime_test_providers_dependencies}
)
if (onnxruntime_ENABLE_LANGUAGE_INTEROP_OPS)
target_link_libraries(opaque_api_test PRIVATE onnxruntime_language_interop onnxruntime_pyop)
endif()
# shared lib
if (onnxruntime_BUILD_SHARED_LIB)
add_library(onnxruntime_mocked_allocator ${ONNXRUNTIME_ROOT}/test/util/test_allocator.cc)
target_include_directories(onnxruntime_mocked_allocator PUBLIC ${ONNXRUNTIME_ROOT}/test/util/include)
set_target_properties(onnxruntime_mocked_allocator PROPERTIES FOLDER "ONNXRuntimeTest")
#################################################################
# test inference using shared lib
set (ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR "${ONNXRUNTIME_ROOT}/test/shared_lib")
set (onnxruntime_shared_lib_test_SRC
${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_fixture.h
${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_inference.cc
${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_session_options.cc
${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_run_options.cc
${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_allocator.cc
${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_nontensor_types.cc)
if(onnxruntime_RUN_ONNX_TESTS)
list(APPEND onnxruntime_shared_lib_test_SRC ${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_io_types.cc)
endif()
if (NOT(${CMAKE_SYSTEM_NAME} MATCHES "Darwin"))
#for some reason, these tests are failing. Need investigation.
if (onnxruntime_USE_FULL_PROTOBUF)
list(APPEND onnxruntime_shared_lib_test_SRC ${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_model_loading.cc)
endif()
endif()
set(onnxruntime_shared_lib_test_LIBS onnxruntime_mocked_allocator onnxruntime_test_utils onnxruntime_common
onnx_proto)
if(onnxruntime_USE_NSYNC)
list(APPEND onnxruntime_shared_lib_test_LIBS nsync_cpp)
endif()
AddTest(DYN
TARGET onnxruntime_shared_lib_test
SOURCES ${onnxruntime_shared_lib_test_SRC}
LIBS ${onnxruntime_shared_lib_test_LIBS}
protobuf::libprotobuf
DEPENDS ${all_dependencies}
)
endif()
#some ETW tools
if(WIN32 AND onnxruntime_ENABLE_INSTRUMENT)
add_executable(generate_perf_report_from_etl ${ONNXRUNTIME_ROOT}/tool/etw/main.cc ${ONNXRUNTIME_ROOT}/tool/etw/eparser.h ${ONNXRUNTIME_ROOT}/tool/etw/eparser.cc ${ONNXRUNTIME_ROOT}/tool/etw/TraceSession.h ${ONNXRUNTIME_ROOT}/tool/etw/TraceSession.cc)
target_compile_definitions(generate_perf_report_from_etl PRIVATE "_CONSOLE" "_UNICODE" "UNICODE")
target_link_libraries(generate_perf_report_from_etl PRIVATE tdh Advapi32)
add_executable(compare_two_sessions ${ONNXRUNTIME_ROOT}/tool/etw/compare_two_sessions.cc ${ONNXRUNTIME_ROOT}/tool/etw/eparser.h ${ONNXRUNTIME_ROOT}/tool/etw/eparser.cc ${ONNXRUNTIME_ROOT}/tool/etw/TraceSession.h ${ONNXRUNTIME_ROOT}/tool/etw/TraceSession.cc)
target_compile_definitions(compare_two_sessions PRIVATE "_CONSOLE" "_UNICODE" "UNICODE")
target_link_libraries(compare_two_sessions PRIVATE ${GETOPT_LIB_WIDE} tdh Advapi32)
endif()
add_executable(onnxruntime_mlas_test ${TEST_SRC_DIR}/mlas/unittest.cpp)
target_include_directories(onnxruntime_mlas_test PRIVATE ${ONNXRUNTIME_ROOT}/core/mlas/inc ${ONNXRUNTIME_ROOT})
set(onnxruntime_mlas_test_libs onnxruntime_mlas onnxruntime_common)
if(onnxruntime_USE_NSYNC)
list(APPEND onnxruntime_mlas_test_libs nsync_cpp)
endif()
list(APPEND onnxruntime_mlas_test_libs Threads::Threads)
target_link_libraries(onnxruntime_mlas_test PRIVATE ${onnxruntime_mlas_test_libs})
set_target_properties(onnxruntime_mlas_test PROPERTIES FOLDER "ONNXRuntimeTest")
add_library(custom_op_library SHARED ${REPO_ROOT}/onnxruntime/test/testdata/custom_op_library/custom_op_library.cc)
target_include_directories(custom_op_library PRIVATE ${REPO_ROOT}/include)
if(UNIX)
if (APPLE)
set(ONNXRUNTIME_CUSTOM_OP_LIB_LINK_FLAG "-Xlinker -dead_strip")
else()
set(ONNXRUNTIME_CUSTOM_OP_LIB_LINK_FLAG "-Xlinker --no-undefined -Xlinker --gc-sections")
endif()
else()
set(ONNXRUNTIME_CUSTOM_OP_LIB_LINK_FLAG "-DEF:${REPO_ROOT}/onnxruntime/test/testdata/custom_op_library/custom_op_library.def /IGNORE:4199")
# need to ignore the linker warning 4199, due to some global linker flags failing here
endif()
set_property(TARGET custom_op_library APPEND_STRING PROPERTY LINK_FLAGS ${ONNXRUNTIME_CUSTOM_OP_LIB_LINK_FLAG})
if (onnxruntime_BUILD_JAVA)
message(STATUS "Running Java tests")
# Build and run tests
set(onnxruntime4j_test_src
${REPO_ROOT}/java/src/test/java/ai/onnxruntime/InferenceTest.java
${REPO_ROOT}/java/src/test/java/ai/onnxruntime/TestHelpers.java
${REPO_ROOT}/java/src/test/java/ai/onnxruntime/OnnxMl.java
${REPO_ROOT}/java/src/test/java/ai/onnxruntime/UtilTest.java
)
# Create test directories
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/java-tests/")
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/java-tests/results")
# Download test dependencies
if (NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/java-tests/junit-platform-console-standalone-1.5.2.jar)
message("Downloading JUnit 5")
file(DOWNLOAD https://repo1.maven.org/maven2/org/junit/platform/junit-platform-console-standalone/1.5.2/junit-platform-console-standalone-1.5.2.jar ${CMAKE_CURRENT_BINARY_DIR}/java-tests/junit-platform-console-standalone-1.5.2.jar EXPECTED_HASH SHA1=8d937d2b461018a876836362b256629f4da5feb1)
endif()
if (NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/java-tests/protobuf-java-3.10.0.jar)
message("Downloading protobuf-java 3.10.0")
file(DOWNLOAD https://repo1.maven.org/maven2/com/google/protobuf/protobuf-java/3.10.0/protobuf-java-3.10.0.jar ${CMAKE_CURRENT_BINARY_DIR}/java-tests/protobuf-java-3.10.0.jar EXPECTED_HASH SHA1=410b61dd0088aab4caa05739558d43df248958c9)
endif()
# Build the test jar
add_jar(onnxruntime4j_test SOURCES ${onnxruntime4j_test_src} VERSION ${ORT_VERSION} INCLUDE_JARS ${onnxruntime_jar_name} ${CMAKE_CURRENT_BINARY_DIR}/java-tests/junit-platform-console-standalone-1.5.2.jar ${CMAKE_CURRENT_BINARY_DIR}/java-tests/protobuf-java-3.10.0.jar)
add_dependencies(onnxruntime4j_test onnxruntime4j_jni onnxruntime4j)
get_property(onnxruntime_test_jar_name TARGET onnxruntime4j_test PROPERTY JAR_FILE)
# Run the tests with JUnit's console launcher
add_test(NAME java-api COMMAND ${Java_JAVA_EXECUTABLE} -jar ${CMAKE_CURRENT_BINARY_DIR}/java-tests/junit-platform-console-standalone-1.5.2.jar -cp ${CMAKE_CURRENT_BINARY_DIR}/java-tests/protobuf-java-3.10.0.jar -cp ${onnxruntime_test_jar_name} -cp ${onnxruntime_jar_binaries_platform} --scan-class-path --fail-if-no-tests --reports-dir=${CMAKE_CURRENT_BINARY_DIR}/java-tests/results --disable-banner WORKING_DIRECTORY ${REPO_ROOT})
endif()