google-research
144 строки · 4.5 Кб
1# coding=utf-8
2# Copyright 2024 The Google Research Authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16# Copyright 2022 The TensorFlow Authors. All Rights Reserved.
17#
18# Licensed under the Apache License, Version 2.0 (the "License");
19# you may not use this file except in compliance with the License.
20# You may obtain a copy of the License at
21#
22# http://www.apache.org/licenses/LICENSE-2.0
23#
24# Unless required by applicable law or agreed to in writing, software
25# distributed under the License is distributed on an "AS IS" BASIS,
26# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27# See the License for the specific language governing permissions and
28# limitations under the License.
29# ==============================================================================
30# Usage: configure.py [--quiet] [--no-deps]
31#
32# Options:
33# --quiet Give less output.
34# --no-deps Don't install Python dependencies
35"""Configures Gigamol to be built from source."""
36
37import argparse38import logging39import os40import subprocess41import sys42from typing import List43
44_BAZELRC = ".bazelrc"45_BAZEL_QUERY = ".bazel-query.sh"46
47
48# Writes variables to bazelrc file
49def write_to_bazelrc(line):50with open(_BAZELRC, "a") as f:51f.write(line + "\n")52
53
54def write_action_env(var_name, var):55write_to_bazelrc('build --action_env %s="%s"' % (var_name, str(var)))56with open(_BAZEL_QUERY, "a") as f:57f.write('{}="{}" '.format(var_name, var))58
59
60def generate_shared_lib_name(tf_lflags):61"""Converts the linkflag namespec to the full shared library name.62
63Args:
64tf_lflags: List of linkflag namespec. The first entry specifies the
65directory containing the TensorFlow framework library. The second entry
66specifies the name of the Tensorflow shared lib (For Linux,
67'-l:libtensorflow_framework.so.%s' % version).
68
69Returns:
70Name of the Tensorflow shared lib.
71"""
72# Assume Linux for now73return tf_lflags[1][3:]74
75
76def create_build_configuration():77"""Main function to create build configuration."""78print()79print("Configuring Gigamol to be built from source...")80
81pip_install_options = ["--upgrade"]82parser = argparse.ArgumentParser()83parser.add_argument("--quiet", action="store_true", help="Give less output.")84parser.add_argument(85"--no-deps",86action="store_true",87help="Do not check and install Python dependencies.",88)89args = parser.parse_args()90if args.quiet:91pip_install_options.append("--quiet")92
93python_path = sys.executable94with open("requirements.txt") as f:95required_packages = f.read().splitlines()96
97print()98if args.no_deps:99print("> Using pre-installed Tensorflow.")100else:101print("> Installing", required_packages)102install_cmd = [python_path, "-m", "pip", "install"]103install_cmd.extend(pip_install_options)104install_cmd.extend(required_packages)105subprocess.check_call(install_cmd)106
107if os.path.isfile(_BAZELRC):108os.remove(_BAZELRC)109if os.path.isfile(_BAZEL_QUERY):110os.remove(_BAZEL_QUERY)111
112logging.disable(logging.WARNING)113
114import tensorflow.compat.v2 as tf # pylint: disable=g-import-not-at-top115
116# pylint: disable=invalid-name117_TF_CFLAGS = tf.sysconfig.get_compile_flags()118_TF_LFLAGS = tf.sysconfig.get_link_flags()119_TF_CXX11_ABI_FLAG = tf.sysconfig.CXX11_ABI_FLAG120
121_TF_SHARED_LIBRARY_NAME = generate_shared_lib_name(_TF_LFLAGS)122_TF_HEADER_DIR = _TF_CFLAGS[0][2:]123_TF_SHARED_LIBRARY_DIR = _TF_LFLAGS[0][2:]124# pylint: enable=invalid-name125
126write_action_env("TF_HEADER_DIR", _TF_HEADER_DIR)127write_action_env("TF_SHARED_LIBRARY_DIR", _TF_SHARED_LIBRARY_DIR)128write_action_env("TF_SHARED_LIBRARY_NAME", _TF_SHARED_LIBRARY_NAME)129write_action_env("TF_CXX11_ABI_FLAG", _TF_CXX11_ABI_FLAG)130
131write_to_bazelrc("build --spawn_strategy=standalone")132write_to_bazelrc("build --strategy=Genrule=standalone")133write_to_bazelrc("build -c opt")134
135print()136print("Build configurations successfully written to", _BAZELRC)137print()138
139with open(_BAZEL_QUERY, "a") as f:140f.write('bazel query "$@"')141
142
143if __name__ == "__main__":144create_build_configuration()145