forked from Maratyszcza/NNPACK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure.py
executable file
·865 lines (767 loc) · 45.4 KB
/
configure.py
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
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
#!/usr/bin/env python
from __future__ import print_function
import os
import sys
import glob
import argparse
import ninja_syntax
class Configuration:
def __init__(self, options, ninja_build_file=os.path.join(os.path.dirname(os.path.abspath(__file__)), "build.ninja")):
self.output = open(ninja_build_file, "w")
self.writer = ninja_syntax.Writer(self.output)
self.source_dir = None
self.build_dir = None
self.artifact_dir = None
self.include_dirs = []
# Variables
self.build, self.host = Configuration.detect_system(options.host)
self.build_static = options.build_static
self.build_shared = options.build_shared and self.host in ["x86_64-linux-gnu", "x86_64-osx"]
if self.host == "pnacl-nacl-newlib":
self.object_ext = ".bc"
self.pic_object_ext = None
self.executable_ext = ".pexe"
self.static_library_ext = ".a"
self.dynamic_library_ext = None
elif self.host in ["x86_64-nacl-glibc", "x86_64-nacl-newlib"]:
self.object_ext = ".o"
self.pic_object_ext = None
self.executable_ext = ".nexe"
self.static_library_ext = ".a"
self.dynamic_library_ext = None
elif self.host == "x86_64-osx":
self.object_ext = ".o"
self.pic_object_ext = ".lo"
self.executable_ext = ""
self.static_library_ext = ".a"
self.dynamic_library_ext = ".dylib"
else:
self.object_ext = ".o"
self.pic_object_ext = ".lo"
self.executable_ext = ""
self.static_library_ext = ".a"
self.dynamic_library_ext = ".so"
cflags = ["-g", "-std=gnu99"]
if options.use_scalar:
cflags += ["-DNNP_ARCH_SCALAR"]
elif options.use_psimd or self.host == "pnacl-nacl-newlib":
cflags += ["-DNNP_ARCH_PSIMD"]
cxxflags = ["-g", "-std=gnu++0x"]
ldflags = ["-g"]
ldlibs = ["m"]
if self.host in ["x86_64-linux-gnu", "x86_64-nacl-glibc", "x86_64-nacl-newlib", "pnacl-nacl-newlib"]:
cflags.append("-pthread")
cxxflags.append("-pthread")
ldflags.append("-pthread")
if self.host == "x86_64-linux-gnu":
self.writer.variable("imageformat", "elf")
self.writer.variable("abi", "sysv")
self.writer.variable("ar", "ar")
if options.use_psimd:
self.writer.variable("cc", "clang")
self.writer.variable("cxx", "clang++")
else:
self.writer.variable("cc", "gcc")
self.writer.variable("cxx", "g++")
ldflags.append("-fuse-ld=gold")
ldlibs.append("rt")
elif self.host == "x86_64-windows-msvc":
import _winreg
with _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, r"SOFTWARE\Microsoft\VisualStudio\14.0_Config") as vs_key:
vs_tools_dir, _ = _winreg.QueryValueEx(vs_key, "InstallDir")
vs_dir = os.path.abspath(os.path.join(vs_tools_dir, "..", ".."))
self.writer.variable("imageformat", "ms-coff")
self.writer.variable("abi", "ms")
self.writer.variable("ar", "lib")
self.writer.variable("visual_studio_dir", vs_dir)
self.writer.variable("cc", "\"" + os.path.join("$visual_studio_dir", "VC", "Clang 3.7", "bin", "x86", "clang.exe") + "\"")
self.writer.variable("cxx", "\"" + os.path.join("$visual_studio_dir", "VC", "Clang 3.7", "bin", "x86", "clang.exe") + "\"")
elif self.host in ["x86_64-nacl-newlib", "x86_64-nacl-glibc", "pnacl-nacl-newlib"]:
if self.host.startswith("x86_64-"):
self.writer.variable("imageformat", "elf")
self.writer.variable("abi", "nacl")
self.writer.variable("nacl_sdk_dir", os.getenv("NACL_SDK_ROOT"))
self.writer.variable("pepper_include_dir", os.path.join("$nacl_sdk_dir", "include"))
toolchain_library_subdir_map = {
("x86_64-linux-gnu", "x86_64-nacl-glibc"): ("linux_x86_glibc", "glibc_x86_64"),
("x86_64-linux-gnu", "x86_64-nacl-newlib"): ("linux_pnacl", "clang-newlib_x86_64"),
("x86_64-linux-gnu", "pnacl-nacl-newlib"): ("linux_pnacl", "pnacl"),
("x86_64-osx", "x86_64-nacl-glibc"): ("mac_x86_glibc", "glibc_x86_64"),
("x86_64-osx", "x86_64-nacl-newlib"): ("mac_pnacl", "clang-newlib_x86_64"),
("x86_64-osx", "pnacl-nacl-newlib"): ("mac_pnacl", "pnacl"),
}
if (self.build, self.host) in toolchain_library_subdir_map:
toolchain_subdir, library_subdir = toolchain_library_subdir_map.get((self.build, self.host))
self.writer.variable("nacl_toolchain_dir", os.path.join("$nacl_sdk_dir", "toolchain", toolchain_subdir))
self.writer.variable("pepper_lib_dir", os.path.join("$nacl_sdk_dir", "lib", library_subdir, "Release"))
else:
print("Error: cross-compilation for %s is not supported on %s" % (self.host, self.build))
sys.exit(1)
toolchain_compiler_map = {
"x86_64-nacl-glibc": ("x86_64-nacl-gcc", "x86_64-nacl-g++", "x86_64-nacl-ar"),
"x86_64-nacl-newlib": ("x86_64-nacl-clang", "x86_64-nacl-clang++", "x86_64-nacl-ar"),
"pnacl-nacl-newlib": ("pnacl-clang", "pnacl-clang++", "pnacl-ar"),
}
toolchain_cc, toolchain_cxx, toolchain_ar = toolchain_compiler_map[self.host]
self.writer.variable("ar", os.path.join("$nacl_toolchain_dir", "bin", toolchain_ar))
self.writer.variable("cc", os.path.join("$nacl_toolchain_dir", "bin", toolchain_cc))
self.writer.variable("cxx", os.path.join("$nacl_toolchain_dir", "bin", toolchain_cxx))
if self.host == "pnacl-nacl-newlib":
self.writer.variable("finalize", os.path.join("$nacl_toolchain_dir", "bin", "pnacl-finalize"))
self.writer.variable("translate", os.path.join("$nacl_toolchain_dir", "bin", "pnacl-translate"))
if self.build in ["x86_64-linux-gnu", "x86_64-osx"]:
self.writer.variable("sel_ldr", os.path.join("$nacl_sdk_dir", "tools", "sel_ldr.py"))
elif self.host == "x86_64-osx":
self.writer.variable("imageformat", "mach-o")
self.writer.variable("abi", "sysv")
self.writer.variable("ar", "ar")
self.writer.variable("cc", "clang")
self.writer.variable("cxx", "clang++")
else:
print("Unsupported platform: %s" % sys.platform, file=sys.stdout)
sys.exit(1)
self.writer.variable("cflags", " ".join(cflags))
self.writer.variable("cxxflags", " ".join(cxxflags))
self.writer.variable("ldflags", " ".join(ldflags))
self.writer.variable("ldlibs", " ".join("-l" + lib for lib in ldlibs))
self.writer.variable("optflags", "-O3")
self.writer.variable("python2", "python")
# Rules
self.writer.rule("lib", "$ar rcs $out $in",
description="LIB $descpath")
self.writer.rule("cc", "$cc -o $out -c $in -MMD -MF $out.d $optflags $cflags $includes",
deps="gcc", depfile="$out.d",
description="CC $descpath")
self.writer.rule("cxx", "$cxx -o $out -c $in -MMD -MF $out.d $optflags $cxxflags $includes",
deps="gcc", depfile="$out.d",
description="CXX $descpath")
self.writer.rule("ccld", "$cc $ldflags $libdirs -o $out $in $ldlibs",
description="CCLD $descpath")
self.writer.rule("cxxld", "$cxx $ldflags $libdirs -o $out $in $ldlibs",
description="CXXLD $descpath")
if self.host.startswith("x86_64-"):
self.writer.rule("peachpy", "$python2 -m peachpy.x86_64 -mabi=$abi -g4 -mimage-format=$imageformat $peachpyincludes -MMD -MF $object.d -emit-c-header $header -o $object $in",
deps="gcc", depfile="$object.d",
description="PEACHPY[x86-64] $descpath")
if self.host == "pnacl-nacl-newlib":
self.writer.rule("finalize", "$finalize --compress -o $out $in",
description="FINALIZE $descpath")
self.writer.rule("translate", "$translate --allow-llvm-bitcode-input -O3 -threads=auto -arch x86-64 $in -o $out",
description="TRANSLATE $descpath")
if self.host in ["x86_64-nacl-newlib", "x86_64-nacl-glibc", "pnacl-nacl-newlib"]:
self.writer.rule("run", "$sel_ldr -- $in $args",
description="RUN $descpath", pool="console")
else:
self.writer.rule("run", "$in $args",
description="RUN $descpath", pool="console")
@staticmethod
def detect_system(host):
if sys.platform.startswith("linux"):
build = "x86_64-linux-gnu"
elif sys.platform == "darwin":
build = "x86_64-osx"
elif sys.platform == "win32":
build = "x86_64-windows-msvc"
else:
print("Error: failed to detect build platform: sys.platform = %s" % sys.platform, file=sys.stdout)
sys.exit(1)
if host is not None:
if os.getenv("NACL_SDK_ROOT") is None:
print("Error: failed to find NaCl SDK: NACL_SDK_ROOT environment variable is not set", file=sys.stdout)
sys.exit(1)
return build, host
else:
return build, build
def _compile(self, rule, source_file, object_file, pic=False, header_file=None, extra_flags=None):
if not os.path.isabs(source_file):
source_file = os.path.join(self.source_dir, source_file)
if object_file is None:
object_file = os.path.join(self.build_dir, os.path.relpath(source_file, self.source_dir)) + (self.object_ext if not pic else self.pic_object_ext)
elif not os.path.isabs(object_file):
object_file = os.path.join(self.build_dir, object_file) + (self.object_ext if not pic else self.pic_object_ext)
else:
object_file = object_file + (self.object_ext if not pic else self.pic_object_ext)
variables = {
"descpath": os.path.relpath(source_file, self.source_dir)
}
if extra_flags is None:
extra_flags = {}
if pic:
if "cflags" in extra_flags:
extra_flags["cflags"] = ["-fPIC"] + extra_flags["cflags"]
else:
extra_flags["cflags"] = ["-fPIC"]
for key, values in extra_flags.items():
if values:
if isinstance(values, str):
values = [values]
variables[key] = "$" + key + " " + " ".join(values)
if rule != "peachpy":
if self.include_dirs:
variables["includes"] = " ".join(["-I" + i for i in self.include_dirs])
else:
variables["object"] = object_file
if header_file is None:
header_file = os.path.join(self.build_dir, os.path.relpath(source_file, self.source_dir)) + ".h"
variables["header"] = header_file
self.writer.build(object_file, rule, source_file, variables=variables)
return object_file
def cc(self, source_file, object_file=None, extra_cflags=[]):
if self.build_static:
nonpic_object_file = self._compile("cc", source_file, object_file, extra_flags={"cflags": extra_cflags})
else:
nonpic_object_file = None
if self.build_shared:
pic_object_file = self._compile("cc", source_file, object_file, pic=True, extra_flags={"cflags": extra_cflags})
else:
pic_object_file = None
return nonpic_object_file, pic_object_file
def peachpy(self, source_file, object_file=None, header_file=None):
object_file = self._compile("peachpy", source_file, object_file)
return object_file, object_file
def cxx(self, source_file, object_file=None, extra_cxxflags=[]):
return self._compile("cxx", source_file, object_file, extra_flags={"cxxflags": extra_cxxflags}), None
def _link(self, rule, object_files, binary_file, binary_dir, lib_dirs, extra_ldlibs=None, extra_ldflags=None):
if not os.path.isabs(binary_file):
binary_file = os.path.join(binary_dir, binary_file)
variables = {
"descpath": os.path.relpath(binary_file, binary_dir)
}
if lib_dirs:
variables["libdirs"] = " ".join(["-L" + l for l in lib_dirs])
if extra_ldlibs:
variables["ldlibs"] = "$ldlibs " + " ".join("-l" + ldlib for ldlib in extra_ldlibs)
if extra_ldflags:
if isinstance(extra_ldflags, str):
extra_ldflags = [extra_ldflags]
variables["ldflags"] = "$ldflags " + " ".join(extra_ldflags)
self.writer.build(binary_file, rule, object_files, variables=variables)
return binary_file
def ccld_executable(self, object_files, binary_file, lib_dirs=[], extra_ldlibs=[], extra_ldflags=[]):
object_files = [nonpic_object for nonpic_object, pic_object in object_files]
if self.host == "pnacl-nacl-newlib":
bytecode_file = self._link("ccld", object_files, binary_file + ".bc", self.build_dir, lib_dirs, extra_ldlibs, extra_ldflags)
portable_file = self.finalize(bytecode_file, os.path.join(self.build_dir, binary_file + ".pexe"))
native_file = self.translate(portable_file, os.path.join(self.artifact_dir, binary_file + ".nexe"))
return native_file
else:
return self._link("ccld", object_files, binary_file, self.artifact_dir, lib_dirs, extra_ldlibs, extra_ldflags)
@staticmethod
def _prepare_library_path(library_file, library_dir, library_ext):
library_basename = os.path.basename(library_file)
if not library_basename.startswith("lib"):
library_basename = "lib" + library_basename
if not library_basename.endswith(library_ext):
library_basename = library_basename + library_ext
library_file = os.path.join(os.path.dirname(library_file), library_basename)
if not os.path.isabs(library_file):
library_file = os.path.join(library_dir, library_file)
return library_file
def ccld_library(self, object_files, library_file, lib_dirs=[], extra_ldlibs=[], extra_ldflags=[]):
assert self.dynamic_library_ext is not None
library_file = Configuration._prepare_library_path(library_file, self.artifact_dir, self.dynamic_library_ext)
if self.host == "x86_64-osx":
extra_ldflags.insert(0, "-dynamiclib")
else:
extra_ldflags = ["-shared", "-Wl,-soname," + os.path.basename(library_file)] + extra_ldflags
return self._link("ccld", object_files, library_file, self.artifact_dir, lib_dirs, extra_ldlibs, extra_ldflags)
def module(self, object_files, module_file, lib_dirs=[], extra_ldlibs=[], extra_ldflags=[]):
assert self.host in ["pnacl-nacl-newlib", "x86_64-nacl-newlib", "x86_64-nacl-glibc"]
object_files = [nonpic_object for nonpic_object, pic_object in object_files]
if self.host == "pnacl-nacl-newlib":
bytecode_file = self._link("ccld", object_files, module_file + ".bc", self.build_dir, lib_dirs, extra_ldlibs, extra_ldflags)
portable_file = self.finalize(bytecode_file, os.path.join(self.artifact_dir, module_file + ".pexe"))
return portable_file
else:
return self._link("ccld", object_files, module_file + ".x86_64.nexe", self.artifact_dir, lib_dirs, extra_ldlibs, extra_ldflags)
def unittest(self, object_files, test_name):
object_files = [nonpic_object for nonpic_object, pic_object in object_files]
if self.host == "pnacl-nacl-newlib":
bytecode_file = self._link("cxxld", object_files, test_name + ".bc", self.build_dir, lib_dirs=[], extra_ldflags=[])
native_file = self.translate(bytecode_file, os.path.join(self.artifact_dir, test_name + ".nexe"))
else:
native_file = self._link("cxxld", object_files, test_name + self.executable_ext, self.artifact_dir, lib_dirs=[], extra_ldflags=[])
self.run(native_file, test_name)
self.default(native_file)
return test_name
def translate(self, portable_file, native_file):
assert self.host == "pnacl-nacl-newlib"
assert os.path.isabs(portable_file)
if not os.path.isabs(native_file):
native_file = os.path.join(self.artifact_dir, native_file)
variables = {
"descpath": os.path.relpath(native_file, self.artifact_dir)
}
self.writer.build(native_file, "translate", portable_file, variables=variables)
return native_file
def finalize(self, bytecode_file, portable_file):
assert self.host == "pnacl-nacl-newlib"
if not os.path.isabs(portable_file):
portable_file = os.path.join(self.build_dir, portable_file)
if os.path.splitext(portable_file)[1] != ".pexe":
portable_file = portable_file + ".pexe"
variables = {
"descpath": os.path.relpath(portable_file, self.artifact_dir)
}
self.writer.build(portable_file, "finalize", bytecode_file, variables=variables)
return portable_file
def lib(self, object_files, library_file):
library_file = Configuration._prepare_library_path(library_file, self.artifact_dir, self.static_library_ext)
variables = {
"descpath": os.path.relpath(library_file, self.artifact_dir)
}
self.writer.build(library_file, "lib", object_files, variables=variables)
return library_file
def library(self, object_files, library_file):
if self.build_static:
static_library_file = self.lib([nonpic_object for nonpic_object, pic_object in object_files], library_file)
else:
static_library_file = None
if self.build_shared:
shared_library_file = self.ccld_library([pic_object for nonpic_object, pic_object in object_files], library_file)
else:
shared_library_file = None
return static_library_file, shared_library_file
def run(self, executable_file, target, args=""):
variables = {
"descpath": os.path.relpath(executable_file, self.artifact_dir)
}
if args:
variables["args"] = args
self.writer.build(target, "run", executable_file, variables=variables)
def default(self, targets):
if isinstance(targets, tuple):
targets = list(targets)
elif not isinstance(targets, list):
targets = [targets]
self.writer.default([ninja_syntax.escape(target).replace(":", "$:") for target in targets if target is not None])
def variable(self, name, value):
self.writer.variable(name, value)
def phony(self, target, deps):
self.writer.build(target, "phony", deps)
parser = argparse.ArgumentParser(description="NNPACK configuration script")
parser.add_argument("--host", dest="host", choices=("x86_64-linux-gnu", "x86_64-osx", "x86_64-nacl-newlib", "x86_64-nacl-glibc", "pnacl-nacl-newlib"))
parser.add_argument("--enable-mkl", dest="use_mkl", action="store_true")
parser.add_argument("--enable-openblas", dest="use_openblas", action="store_true")
parser.add_argument("--enable-blis", dest="use_blis", action="store_true")
parser.add_argument("--enable-psimd", dest="use_psimd", action="store_true")
parser.add_argument("--enable-scalar", dest="use_scalar", action="store_true")
parser.add_argument("--disable-static", dest="build_static", action="store_false", default=True)
parser.add_argument("--enable-shared", dest="build_shared", action="store_true", default=False)
def main():
options = parser.parse_args()
config = Configuration(options)
root_dir = os.path.dirname(os.path.abspath(__file__))
# Build gtest
gtest_dir = os.path.join(root_dir, "third-party", "gtest-1.7.0")
config.source_dir = os.path.join(gtest_dir, "src")
config.build_dir = os.path.join(gtest_dir, "lib")
config.include_dirs = [os.path.join(gtest_dir, "include"), gtest_dir]
gtest_objects = [config.cxx("gtest-all.cc")]
# Build pthreadpool
fxdiv_dir = os.path.join(root_dir, "third-party", "FXdiv")
pthreadpool_dir = os.path.join(root_dir, "third-party", "pthreadpool")
config.source_dir = os.path.join(pthreadpool_dir, "src")
config.build_dir = os.path.join(pthreadpool_dir, "lib")
config.include_dirs = [os.path.join(pthreadpool_dir, "include"), os.path.join(fxdiv_dir, "include")]
pthreadpool_objects = [config.cc("pthreadpool.c")]
# Build the library
config.source_dir = os.path.join(root_dir, "src")
config.build_dir = os.path.join(root_dir, "build")
config.include_dirs = [os.path.join(root_dir, "include"), os.path.join(root_dir, "src"), os.path.join(root_dir, "src", "ref"), os.path.join(pthreadpool_dir, "include"), os.path.join(fxdiv_dir, "include")]
nnpack_objects = [
config.cc("init.c"),
config.cc("convolution-output.c"),
config.cc("convolution-input-gradient.c"),
config.cc("convolution-kernel.c"),
config.cc("convolution-inference.c"),
config.cc("fully-connected-output.c"),
config.cc("fully-connected-inference.c"),
config.cc("pooling-output.c"),
config.cc("softmax-output.c"),
config.cc("relu-output.c"),
config.cc("relu-input-gradient.c"),
]
if config.host.startswith("x86_64-") and not options.use_psimd and not options.use_scalar:
arch_nnpack_objects = [
# Transformations
config.peachpy("x86_64-fma/2d-fft-8x8.py"),
config.peachpy("x86_64-fma/2d-fft-16x16.py"),
config.peachpy("x86_64-fma/2d-wt-8x8-3x3.py"),
# Pooling
config.peachpy("x86_64-fma/max-pooling.py"),
# ReLU and Softmax
config.peachpy("x86_64-fma/relu.py"),
config.peachpy("x86_64-fma/softmax.py"),
config.cc("x86_64-fma/softmax.c"),
# FFT block accumulation
config.peachpy("x86_64-fma/fft-block-mac.py"),
# Tuple GEMM
config.peachpy("x86_64-fma/blas/s8gemm.py"),
config.peachpy("x86_64-fma/blas/c8gemm.py"),
config.peachpy("x86_64-fma/blas/s4c6gemm.py"),
# BLAS microkernels
config.peachpy("x86_64-fma/blas/sgemm.py"),
config.peachpy("x86_64-fma/blas/sdotxf.py"),
]
elif options.use_scalar:
arch_nnpack_objects = [
# Transformations
config.cc("scalar/2d-fourier-8x8.c"),
config.cc("scalar/2d-fourier-16x16.c"),
config.cc("scalar/2d-winograd-8x8-3x3.c"),
# ReLU and Softmax
config.cc("scalar/relu.c"),
config.cc("scalar/softmax.c"),
# FFT block accumulation
# config.cc("scalar/fft-block-mac.c"),
# Tuple GEMM
config.cc("scalar/blas/s2gemm.c"),
config.cc("scalar/blas/s2gemm-transc.c"),
config.cc("scalar/blas/cgemm.c"),
config.cc("scalar/blas/cgemm-conjb.c"),
config.cc("scalar/blas/cgemm-conjb-transc.c"),
# BLAS microkernels
config.cc("scalar/blas/sgemm.c"),
config.cc("scalar/blas/sdotxf.c"),
]
elif options.use_psimd:
arch_nnpack_objects = [
# Transformations
config.cc("psimd/2d-fourier-8x8.c"),
config.cc("psimd/2d-fourier-16x16.c"),
config.cc("psimd/2d-wt-8x8-3x3.c"),
# ReLU and Softmax
config.cc("psimd/relu.c"),
config.cc("psimd/softmax.c"),
# FFT block accumulation
config.cc("psimd/fft-block-mac.c"),
# Tuple GEMM
config.cc("psimd/blas/s4gemm.c"),
config.cc("psimd/blas/c4gemm.c"),
config.cc("psimd/blas/s4c2gemm.c"),
config.cc("psimd/blas/c4gemm-conjb.c"),
config.cc("psimd/blas/s4c2gemm-conjb.c"),
config.cc("psimd/blas/c4gemm-conjb-transc.c"),
config.cc("psimd/blas/s4c2gemm-conjb-transc.c"),
# BLAS microkernels
config.cc("psimd/blas/sgemm.c"),
config.cc("psimd/blas/sdotxf.c"),
]
reference_layer_objects = [
config.cc("ref/convolution-output.c"),
config.cc("ref/convolution-input-gradient.c"),
config.cc("ref/convolution-kernel.c"),
config.cc("ref/fully-connected-output.c"),
config.cc("ref/max-pooling-output.c"),
config.cc("ref/softmax-output.c"),
config.cc("ref/relu-output.c"),
config.cc("ref/relu-input-gradient.c"),
]
reference_fft_objects = [
config.cc("ref/fft/aos.c"),
config.cc("ref/fft/soa.c"),
config.cc("ref/fft/forward-real.c"),
config.cc("ref/fft/forward-dualreal.c"),
config.cc("ref/fft/inverse-real.c"),
config.cc("ref/fft/inverse-dualreal.c"),
]
if config.host.startswith("x86_64-") and not options.use_psimd and not options.use_scalar:
arch_fft_stub_objects = [
config.peachpy("x86_64-fma/fft-soa.py"),
config.peachpy("x86_64-fma/fft-aos.py"),
config.peachpy("x86_64-fma/fft-dualreal.py"),
config.peachpy("x86_64-fma/ifft-dualreal.py"),
config.peachpy("x86_64-fma/fft-real.py"),
config.peachpy("x86_64-fma/ifft-real.py"),
]
arch_winograd_stub_objects = [
config.peachpy("x86_64-fma/winograd-f6k3.py"),
]
arch_math_stub_objects = [
]
elif options.use_scalar:
arch_fft_stub_objects = [
config.cc("scalar/fft-aos.c"),
config.cc("scalar/fft-soa.c"),
config.cc("scalar/fft-real.c"),
config.cc("scalar/fft-dualreal.c"),
]
elif options.use_psimd:
arch_fft_stub_objects = [
config.cc("psimd/fft-aos.c"),
config.cc("psimd/fft-soa.c"),
config.cc("psimd/fft-real.c"),
config.cc("psimd/fft-dualreal.c"),
]
arch_winograd_stub_objects = [
config.cc("psimd/winograd-f6k3.c"),
]
arch_math_stub_objects = [
config.cc("psimd/exp.c"),
]
fft_objects = reference_fft_objects + arch_fft_stub_objects
reference_blockmac_objects = [
config.cc("ref/fft/block-mac.c"),
]
nnpack_objects = nnpack_objects + arch_nnpack_objects + pthreadpool_objects
# Build the library
config.variable("peachpyincludes", "-I" + os.path.join(config.source_dir, "x86_64-fma"))
config.artifact_dir = os.path.join(root_dir, "lib")
config.default(config.library(nnpack_objects, "nnpack"))
# Build Native Client module
if config.host in ["x86_64-nacl-glibc", "x86_64-nacl-newlib", "pnacl-nacl-newlib"]:
config.source_dir = os.path.join(root_dir, "src")
config.build_dir = os.path.join(root_dir, "build")
config.include_dirs = [os.path.join(root_dir, "include"), os.path.join(root_dir, "src"), os.path.join(pthreadpool_dir, "include")]
config.artifact_dir = os.path.join(root_dir, "web")
module_source_files = ["nacl/entry.c", "nacl/instance.c", "nacl/interfaces.c", "nacl/messaging.c", "nacl/stringvars.c", "nacl/benchmark.c"]
nacl_module_objects = [config.cc(source_file, extra_cflags="-I$pepper_include_dir") for source_file in module_source_files]
nacl_module_binary = \
config.module(nnpack_objects + nacl_module_objects, "nnpack", lib_dirs=["$pepper_lib_dir"], libs=["ppapi"])
config.default(nacl_module_binary)
# Build unit tests
if config.host not in ["x86_64-nacl-glibc"]:
config.source_dir = os.path.join(root_dir, "test")
config.build_dir = os.path.join(root_dir, "build", "test")
config.artifact_dir = os.path.join(root_dir, "bin")
config.include_dirs = [os.path.join(root_dir, "include"), os.path.join(pthreadpool_dir, "include"), os.path.join(root_dir, "test"), os.path.join(gtest_dir, "include")]
fourier_reference_test = \
config.unittest(reference_fft_objects + [config.cxx("fourier/reference.cc")] + gtest_objects,
"fourier-reference-test")
if config.host.startswith("x86_64-") and not options.use_psimd and not options.use_scalar:
fourier_x86_64_avx2_test = \
config.unittest(reference_fft_objects + arch_fft_stub_objects + [config.cxx("fourier/x86_64-avx2.cc")] + gtest_objects,
"fourier-x86_64-avx2-test")
winograd_x86_64_fma3_test = \
config.unittest(arch_winograd_stub_objects + nnpack_objects + [config.cxx("winograd/x86_64-fma3.cc")] + gtest_objects,
"winograd-x86_64-fma3-test")
sgemm_x86_64_fma3_test = \
config.unittest(nnpack_objects + [config.cxx("sgemm/x86_64-fma3.cc")] + gtest_objects,
"sgemm-x86_64-fma3-test")
if config.host.startswith("pnacl-") and not options.use_scalar or options.use_psimd:
fourier_psimd_test = \
config.unittest(reference_fft_objects + arch_fft_stub_objects + [config.cxx("fourier/psimd.cc")] + gtest_objects,
"fourier-psimd-test")
winograd_psimd_test = \
config.unittest(arch_winograd_stub_objects + nnpack_objects + [config.cxx("winograd/psimd.cc")] + gtest_objects,
"winograd-psimd-test")
sgemm_psimd_test = \
config.unittest(nnpack_objects + [config.cxx("sgemm/psimd.cc")] + gtest_objects,
"sgemm-psimd-test")
if options.use_scalar:
fourier_scalar_test = \
config.unittest(reference_fft_objects + arch_fft_stub_objects + [config.cxx("fourier/scalar.cc")] + gtest_objects,
"fourier-scalar-test")
sgemm_scalar_test = \
config.unittest(nnpack_objects + [config.cxx("sgemm/scalar.cc")] + gtest_objects,
"sgemm-scalar-test")
fp16_values = config.cxx("fp16/values.cc")
fp16_ieee_to_fp32_bits_test = \
config.unittest([config.cxx("fp16/ieee-to-fp32-bits.cc"), fp16_values] + gtest_objects,
"fp16-ieee-to-fp32-bits-test")
fp16_ieee_to_fp32_value_test = \
config.unittest([config.cxx("fp16/ieee-to-fp32-value.cc"), fp16_values] + gtest_objects,
"fp16-ieee-to-fp32-value-test")
fp32_to_fp16_ieee_value_test = \
config.unittest([config.cxx("fp16/fp32-to-ieee-value.cc"), fp16_values] + gtest_objects,
"fp32-to-fp16-ieee-value-test")
convolution_output_smoke_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("convolution-output/smoke.cc")] + gtest_objects,
"convolution-output-smoketest")
convolution_output_alexnet_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("convolution-output/alexnet.cc")] + gtest_objects,
"convolution-output-alexnet-test")
convolution_output_vgg_a_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("convolution-output/vgg-a.cc")] + gtest_objects,
"convolution-output-vgg-a-test")
convolution_output_overfeat_fast_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("convolution-output/overfeat-fast.cc")] + gtest_objects,
"convolution-output-overfeat-fast-test")
config.phony("convolution-output-test",
[convolution_output_smoke_test, convolution_output_alexnet_test, convolution_output_vgg_a_test, convolution_output_overfeat_fast_test])
convolution_input_gradient_smoke_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("convolution-input-gradient/smoke.cc")] + gtest_objects,
"convolution-input-gradient-smoketest")
convolution_input_gradient_alexnet_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("convolution-input-gradient/alexnet.cc")] + gtest_objects,
"convolution-input-gradient-alexnet-test")
convolution_input_gradient_vgg_a_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("convolution-input-gradient/vgg-a.cc")] + gtest_objects,
"convolution-input-gradient-vgg-a-test")
convolution_input_gradient_overfeat_fast_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("convolution-input-gradient/overfeat-fast.cc")] + gtest_objects,
"convolution-input-gradient-overfeat-fast-test")
config.phony("convolution-input-gradient-test",
[convolution_input_gradient_smoke_test, convolution_input_gradient_alexnet_test, convolution_input_gradient_vgg_a_test, convolution_input_gradient_overfeat_fast_test])
convolution_kernel_gradient_smoke_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("convolution-kernel-gradient/smoke.cc")] + gtest_objects,
"convolution-kernel-gradient-smoketest")
convolution_kernel_gradient_alexnet_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("convolution-kernel-gradient/alexnet.cc")] + gtest_objects,
"convolution-kernel-gradient-alexnet-test")
convolution_kernel_gradient_vgg_a_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("convolution-kernel-gradient/vgg-a.cc")] + gtest_objects,
"convolution-kernel-gradient-vgg-a-test")
convolution_kernel_gradient_overfeat_fast_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("convolution-kernel-gradient/overfeat-fast.cc")] + gtest_objects,
"convolution-kernel-gradient-overfeat-fast-test")
config.phony("convolution-kernel-gradient-test",
[convolution_kernel_gradient_smoke_test, convolution_kernel_gradient_alexnet_test, convolution_kernel_gradient_vgg_a_test, convolution_kernel_gradient_overfeat_fast_test])
convolution_inference_smoke_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("convolution-inference/smoke.cc")] + gtest_objects,
"convolution-inference-smoketest")
convolution_inference_alexnet_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("convolution-inference/alexnet.cc")] + gtest_objects,
"convolution-inference-alexnet-test")
convolution_inference_vgg_a_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("convolution-inference/vgg-a.cc")] + gtest_objects,
"convolution-inference-vgg-a-test")
convolution_inference_overfeat_fast_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("convolution-inference/overfeat-fast.cc")] + gtest_objects,
"convolution-inference-overfeat-fast-test")
config.phony("convolution-inference-test",
[convolution_inference_smoke_test, convolution_inference_alexnet_test, convolution_inference_vgg_a_test, convolution_inference_overfeat_fast_test])
fully_connected_output_smoke_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("fully-connected-output/smoke.cc")] + gtest_objects,
"fully-connected-output-smoketest")
fully_connected_output_alexnet_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("fully-connected-output/alexnet.cc")] + gtest_objects,
"fully-connected-output-alexnet-test")
fully_connected_output_vgg_a_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("fully-connected-output/vgg-a.cc")] + gtest_objects,
"fully-connected-output-vgg-a-test")
fully_connected_output_overfeat_fast_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("fully-connected-output/overfeat-fast.cc")] + gtest_objects,
"fully-connected-output-overfeat-fast-test")
config.phony("fully-connected-output-test",
[fully_connected_output_smoke_test, fully_connected_output_alexnet_test, fully_connected_output_vgg_a_test, fully_connected_output_overfeat_fast_test])
fully_connected_inference_alexnet_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("fully-connected-inference/alexnet.cc")] + gtest_objects,
"fully-connected-inference-alexnet-test")
fully_connected_inference_vgg_a_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("fully-connected-inference/vgg-a.cc")] + gtest_objects,
"fully-connected-inference-vgg-a-test")
fully_connected_inference_overfeat_fast_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("fully-connected-inference/overfeat-fast.cc")] + gtest_objects,
"fully-connected-inference-overfeat-fast-test")
config.phony("fully-connected-inference-test",
[fully_connected_inference_alexnet_test, fully_connected_inference_vgg_a_test, fully_connected_inference_overfeat_fast_test])
max_pooling_output_smoke_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("max-pooling-output/smoke.cc")] + gtest_objects,
"max-pooling-output-smoketest")
max_pooling_output_vgg_a_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("max-pooling-output/vgg-a.cc")] + gtest_objects,
"max-pooling-output-vgg-a-test")
max_pooling_output_overfeat_fast_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("max-pooling-output/overfeat-fast.cc")] + gtest_objects,
"max-pooling-output-overfeat-fast")
config.phony("max-pooling-output-test",
[max_pooling_output_smoke_test, max_pooling_output_vgg_a_test, max_pooling_output_overfeat_fast_test])
relu_output_alexnet_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("relu-output/alexnet.cc")] + gtest_objects,
"relu-output-alexnet-test")
relu_output_vgg_a_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("relu-output/vgg-a.cc")] + gtest_objects,
"relu-output-vgg-a-test")
relu_output_overfeat_fast_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("relu-output/overfeat-fast.cc")] + gtest_objects,
"relu-output-overfeat-fast-test")
config.phony("relu-output-test",
[relu_output_alexnet_test, relu_output_vgg_a_test, relu_output_overfeat_fast_test])
relu_input_gradient_alexnet_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("relu-input-gradient/alexnet.cc")] + gtest_objects,
"relu-input-gradient-alexnet-test")
relu_input_gradient_vgg_a_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("relu-input-gradient/vgg-a.cc")] + gtest_objects,
"relu-input-gradient-vgg-a-test")
relu_input_gradient_overfeat_fast_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("relu-input-gradient/overfeat-fast.cc")] + gtest_objects,
"relu-input-gradient-overfeat-fast-test")
config.phony("relu-input-gradient-test",
[relu_input_gradient_alexnet_test, relu_input_gradient_vgg_a_test, relu_input_gradient_overfeat_fast_test])
softmax_output_smoke_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("softmax-output/smoke.cc")] + gtest_objects,
"softmax-output-smoketest")
softmax_output_imagenet_test = \
config.unittest(nnpack_objects + reference_layer_objects + [config.cxx("softmax-output/imagenet.cc")] + gtest_objects,
"softmax-output-imagenet-test")
config.phony("softmax-output-test",
[softmax_output_smoke_test, softmax_output_imagenet_test])
config.phony("test", [
"convolution-output-test", "convolution-input-gradient-test", "convolution-kernel-gradient-test", "convolution-inference-test",
"fully-connected-output-test", "fully-connected-inference-test",
"max-pooling-output-test",
"relu-output-test", "relu-input-gradient-test",
"softmax-output-test"])
config.phony("smoketest", [
convolution_output_smoke_test, convolution_input_gradient_smoke_test, convolution_kernel_gradient_smoke_test, convolution_inference_smoke_test,
fully_connected_output_smoke_test,
max_pooling_output_smoke_test,
softmax_output_smoke_test])
# Build benchmarks
config.source_dir = os.path.join(root_dir, "bench")
config.build_dir = os.path.join(root_dir, "build", "bench")
config.artifact_dir = os.path.join(root_dir, "bin")
config.include_dirs = [os.path.join(root_dir, "include"), os.path.join(pthreadpool_dir, "include"), os.path.join(root_dir, "bench")]
bench_support_objects = [config.cc("median.c")]
if config.host.startswith("x86_64-"):
bench_support_objects.append(config.peachpy("memread.py"))
if config.host == "x86_64-linux-gnu":
bench_support_objects.append(config.cc("perf_counter.c"))
extra_cflags = []
extra_lib_dirs = []
extra_ldlibs = []
extra_ldflags = []
if options.use_mkl:
extra_cflags.append("-DUSE_MKL")
extra_cflags.append("-I/opt/intel/mkl/include")
extra_lib_dirs.append("/opt/intel/mkl/lib/intel64")
extra_ldlibs = ["mkl_intel_lp64", "mkl_core", "mkl_sequential"]
extra_ldflags.append("-Wl,--no-as-needed")
transform_bench_binary = config.ccld_executable([config.cc("transform.c", extra_cflags=extra_cflags)] + nnpack_objects + bench_support_objects, "transform-bench",
lib_dirs=extra_lib_dirs, extra_ldlibs=extra_ldlibs, extra_ldflags=extra_ldflags)
config.phony("transform-bench", transform_bench_binary)
config.default(transform_bench_binary)
convolution_bench_binary = config.ccld_executable([config.cc("convolution.c")] + nnpack_objects + bench_support_objects,
"convolution-benchmark")
config.phony("convolution-bench", convolution_bench_binary)
fully_connected_bench_binary = config.ccld_executable([config.cc("fully-connected.c")] + nnpack_objects + bench_support_objects,
"fully-connected-benchmark")
config.phony("fully-connected-bench", fully_connected_bench_binary)
pooling_bench_binary = config.ccld_executable([config.cc("pooling.c")] + nnpack_objects + bench_support_objects,
"pooling-benchmark")
config.phony("pooling-bench", pooling_bench_binary)
relu_bench_binary = config.ccld_executable([config.cc("relu.c")] + nnpack_objects + bench_support_objects,
"relu-benchmark")
config.phony("relu-bench", relu_bench_binary)
config.default([convolution_bench_binary, fully_connected_bench_binary, pooling_bench_binary, relu_bench_binary])
vgg_bench_binary = config.ccld_executable([config.cc("vgg.c")] + nnpack_objects + bench_support_objects,
"vgg-benchmark")
config.phony("vgg-bench", vgg_bench_binary)
config.default([vgg_bench_binary])
if config.host.startswith("x86_64-") and not options.use_psimd and not options.use_scalar:
#ugemm_bench_binary = config.ccld_executable([config.cc("ugemm.c")] + arch_nnpack_objects + bench_support_objects, "ugemm-bench", libs=["m"])
#config.default(ugemm_bench_binary)
if options.use_mkl:
extra_cflags = ["-DUSE_MKL", "-I/opt/intel/mkl/include", "-pthread"]
extra_lib_dirs = ["/opt/intel/mkl/lib/intel64"]
extra_libs = ["mkl_intel_lp64", "mkl_core", "mkl_gnu_thread", "pthread"]
extra_ldflags = ["-Wl,--no-as-needed", "-pthread", "-fopenmp"]
mkl_gemm_bench_binary = config.ccld_executable([config.cc("gemm.c", "mkl-gemm", extra_cflags=extra_cflags)] + bench_support_objects, "mkl-gemm-bench", lib_dirs=extra_lib_dirs, libs=extra_libs, extra_ldflags=extra_ldflags)
config.default(mkl_gemm_bench_binary)
if options.use_openblas:
extra_cflags = ["-DUSE_OPENBLAS", "-I/opt/OpenBLAS/include", "-pthread"]
extra_lib_dirs = ["/opt/OpenBLAS/lib"]
extra_libs = ["openblas", "pthread"]
extra_ldflags = []
openblas_gemm_bench_binary = config.ccld_executable([config.cc("gemm.c", "openblas-gemm", extra_cflags=extra_cflags)] + bench_support_objects, "openblas-gemm-bench", lib_dirs=extra_lib_dirs, libs=extra_libs, extra_ldflags=extra_ldflags)
config.default(openblas_gemm_bench_binary)
if options.use_blis:
extra_cflags = ["-DUSE_BLIS", "-I/opt/BLIS/include", "-pthread", "-fopenmp"]
extra_libs = ["pthread"]
extra_ldflags = ["-fopenmp"]
blis_gemm_bench_binary = config.ccld_executable([config.cc("gemm.c", "blis-gemm", extra_cflags=extra_cflags), "/opt/BLIS/lib/libblis.a"] + bench_support_objects, "blis-gemm-bench", lib_dirs=extra_lib_dirs, libs=extra_libs, extra_ldflags=extra_ldflags)
config.default(blis_gemm_bench_binary)
if __name__ == "__main__":
sys.exit(main())