This repository has been archived by the owner on Jul 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
/
build_tf.py
executable file
·142 lines (125 loc) · 4.99 KB
/
build_tf.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
#!/usr/bin/env python3
# ==============================================================================
# Copyright (C) 2023 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
# ==============================================================================
from tools.build_utils import *
import os, shutil
import argparse
def main():
# Command line parser options
parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument(
'--tf_version',
type=str,
help="TensorFlow tag/branch/SHA\n",
action="store",
default="v2.9.3")
parser.add_argument(
'--output_dir',
type=str,
help="Location where TensorFlow build will happen\n",
action="store",
required=True)
parser.add_argument(
'--target_arch',
help=
"Architecture flag to use (e.g., haswell, core-avx2 etc. Default \'native\'\n",
default="native")
parser.add_argument(
'--use_intel_tensorflow',
help="Build using Intel TensorFlow.",
action="store_true")
parser.add_argument(
'--cxx11_abi_version',
help="Desired version of ABI to be used while building Tensorflow",
default='1')
parser.add_argument(
'--resource_usage_ratio',
help="Ratio of CPU / RAM resources to utilize during Tensorflow build",
default=0.5)
arguments = parser.parse_args()
# Check bazel version
bazel_kind, bazel_ver = get_bazel_version()
got_correct_bazel_version = bazel_kind == 'Bazelisk version'
if (not got_correct_bazel_version and int(bazel_ver[0]) < 2):
raise Exception("Need bazel version >= 2.0.0 \n" + "Got: " +
'.'.join(bazel_ver))
if not os.path.isdir(arguments.output_dir):
os.makedirs(arguments.output_dir)
if not os.path.isdir(arguments.output_dir):
raise AssertionError("Did not find output directory: " +
arguments.output_dir)
if not os.path.exists(arguments.output_dir):
raise AssertionError("path doesn't exist {0}".format(
arguments.output_dir))
os.chdir(arguments.output_dir)
if (platform.system() == 'Windows'):
venv_dir = '.\\venv3\\'
else:
venv_dir = './venv3/'
install_virtual_env(venv_dir)
load_venv(venv_dir)
setup_venv(venv_dir, arguments.tf_version)
if not os.path.exists(arguments.output_dir):
raise AssertionError("Directory doesn't exist {0}".format(
arguments.output_dir))
if not os.path.isdir(os.path.join(arguments.output_dir, "tensorflow")):
# Download TensorFlow
download_repo("tensorflow",
"https://github.com/tensorflow/tensorflow.git",
arguments.tf_version)
else:
pwd = os.getcwd()
if not os.path.exists(arguments.output_dir):
raise AssertionError("Path doesn't exist {}".format(
arguments.output_dir))
os.chdir(os.path.join(arguments.output_dir, "tensorflow"))
call(["git", "fetch"])
command_executor(["git", "checkout", arguments.tf_version])
call(["git", "pull"])
if not os.path.exists(pwd):
raise AssertionError("Path doesn't exist {0}".format(pwd))
os.chdir(pwd)
# Build TensorFlow
build_tensorflow(
arguments.tf_version,
"tensorflow",
'artifacts',
arguments.target_arch,
False,
arguments.use_intel_tensorflow,
arguments.cxx11_abi_version,
resource_usage_ratio=float(arguments.resource_usage_ratio))
# Build TensorFlow C++ Library
build_tensorflow_cc(
arguments.tf_version, "tensorflow", 'artifacts', arguments.target_arch,
False, arguments.use_intel_tensorflow, arguments.cxx11_abi_version)
pwd = os.getcwd()
if (platform.system() == 'Windows'):
artifacts_dir = os.path.join(pwd, 'tensorflow')
else:
artifacts_dir = os.path.join(pwd, 'artifacts/tensorflow')
os.chdir("tensorflow")
copy_tf_to_artifacts(arguments.tf_version, artifacts_dir, None,
arguments.use_intel_tensorflow)
print('\033[1;35mTensorFlow Build finished\033[0m')
print(
'When building openvino_tensorflow using this prebuilt tensorflow, use:'
)
print('\033[3;34mpython3 build_ovtf.py --use_tensorflow_from_location ' +
os.path.abspath(arguments.output_dir) + '\033[1;0m')
if __name__ == '__main__':
main()
# Usage
# Build TF once
# ./build_tf.py --tf_version v1.15.2 --output_dir /prebuilt/tf/dir
#
# Reuse TF in different openvino_tensorflow builds
# mkdir ovtf_1; cd ovtf_1
# git clone https://github.com/openvinotoolkit/openvino_tensorflow.git
# ./build_ovtf.py --use_tensorflow_from_location /prebuilt/tf/dir
# cd ..; mkdir ovtf_2; cd ovtf_2
# git clone https://github.com/openvinotoolkit/openvino_tensorflow.git
# ./build_ovtf.py --use_tensorflow_from_location /prebuilt/tf/dir