Skip to content

Commit

Permalink
Add an automatically-generated "tensorflow.python.platform.build_info…
Browse files Browse the repository at this point in the history
…" script.

The motivation for this script is to provide better tools for
diagnosing load-time errors (such as the ones that plague the Windows
build due to DLL issues). Note that the script is intended to be
self-contained, so that it is possible to import it without loading
the entire TensorFlow runtime.

This generated script currently contains a single symbol,
`is_cuda_build`, which records whether the build has GPU support or not.

PiperOrigin-RevId: 168471034
  • Loading branch information
mrry authored and tensorflower-gardener committed Sep 13, 2017
1 parent c3b8634 commit 59bdf59
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 2 deletions.
1 change: 1 addition & 0 deletions tensorflow/BUILD
Expand Up @@ -413,6 +413,7 @@ filegroup(
"//tensorflow/tools/api/golden:all_files",
"//tensorflow/tools/api/lib:all_files",
"//tensorflow/tools/api/tests:all_files",
"//tensorflow/tools/build_info:all_files",
"//tensorflow/tools/common:all_files",
"//tensorflow/tools/compatibility:all_files",
"//tensorflow/tools/dist_test/server:all_files",
Expand Down
11 changes: 11 additions & 0 deletions tensorflow/contrib/cmake/tf_python.cmake
Expand Up @@ -624,6 +624,16 @@ add_python_module("tensorflow/contrib/reduce_slice_ops/python")
add_python_module("tensorflow/contrib/reduce_slice_ops/python/kernel_tests")
add_python_module("tensorflow/contrib/reduce_slice_ops/python/ops")

# Generate the tensorflow.python.platform.build_info module.
set(BUILD_INFO_PY "${CMAKE_CURRENT_BINARY_DIR}/tf_python/tensorflow/python/platform/build_info.py")
if(tensorflow_ENABLE_GPU)
set(BUILD_CONFIG_STRING "cuda")
else(tensorflow_ENABLE_GPU)
set(BUILD_CONFIG_STRING "cpu")
endif(tensorflow_ENABLE_GPU)
add_custom_command(TARGET tf_python_copy_scripts_to_destination PRE_BUILD
COMMAND ${PYTHON_EXECUTABLE} ${tensorflow_source_dir}/tensorflow/tools/build_info/gen_build_info.py --build_config ${BUILD_CONFIG_STRING} --raw_generate ${BUILD_INFO_PY})


########################################################
# tf_python_op_gen_main library
Expand Down Expand Up @@ -1173,3 +1183,4 @@ else()
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tf_python)
endif(${tensorflow_ENABLE_GPU})
endif(${tensorflow_TF_NIGHTLY})

1 change: 1 addition & 0 deletions tensorflow/contrib/cmake/tf_tests.cmake
Expand Up @@ -145,6 +145,7 @@ if (tensorflow_BUILD_PYTHON_TESTS)
"${tensorflow_source_dir}/tensorflow/contrib/estimator/python/estimator/*_test.py"
"${tensorflow_source_dir}/tensorflow/python/kernel_tests/*.py"
"${tensorflow_source_dir}/tensorflow/python/meta_graph_transform/*_test.py"
"${tensorflow_source_dir}/tensorflow/python/platform/build_info_test.py"
"${tensorflow_source_dir}/tensorflow/python/profiler/*_test.py"
"${tensorflow_source_dir}/tensorflow/python/profiler/internal/*_test.py"
"${tensorflow_source_dir}/tensorflow/python/saved_model/*_test.py"
Expand Down
25 changes: 23 additions & 2 deletions tensorflow/python/BUILD
Expand Up @@ -19,6 +19,7 @@ load("//tensorflow:tensorflow.bzl", "tf_gen_op_wrapper_py")
load("//tensorflow:tensorflow.bzl", "py_test")
load("//tensorflow:tensorflow.bzl", "tf_py_test")
load("//tensorflow:tensorflow.bzl", "py_tests")
load("//tensorflow:tensorflow.bzl", "tf_py_build_info_genrule")
load("//tensorflow:tensorflow.bzl", "tf_py_wrap_cc")
load("//tensorflow:tensorflow.bzl", "cuda_py_test")
load("//tensorflow:tensorflow.bzl", "cuda_py_tests")
Expand Down Expand Up @@ -100,15 +101,19 @@ py_library(
]),
)

tf_py_build_info_genrule()

py_library(
name = "platform",
srcs = glob(
["platform/*.py"],
[
"platform/*.py",
],
exclude = [
"**/*test.py",
"**/benchmark.py", # In platform_benchmark.
],
),
) + ["platform/build_info.py"],
srcs_version = "PY2AND3",
deps = [
":lib",
Expand Down Expand Up @@ -806,6 +811,22 @@ py_test(
],
)

py_test(
name = "build_info_test",
size = "small",
srcs = [
"platform/build_info.py",
"platform/build_info_test.py",
],
main = "platform/build_info_test.py",
srcs_version = "PY2AND3",
tags = ["notap"],
deps = [
":client_testlib",
":platform",
],
)

py_test(
name = "proto_test",
size = "small",
Expand Down
32 changes: 32 additions & 0 deletions tensorflow/python/platform/build_info_test.py
@@ -0,0 +1,32 @@
# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Test for the generated build_info script."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from tensorflow.python.platform import build_info
from tensorflow.python.platform import test


class BuildInfoTest(test.TestCase):

def testBuildInfo(self):
self.assertEqual(build_info.is_cuda_build, test.is_built_with_cuda())


if __name__ == '__main__':
test.main()
10 changes: 10 additions & 0 deletions tensorflow/tensorflow.bzl
Expand Up @@ -1294,6 +1294,16 @@ def tf_version_info_genrule():
tools=[clean_dep("//tensorflow/tools/git:gen_git_source.py")],)


def tf_py_build_info_genrule():
native.genrule(
name="py_build_info_gen",
outs=["platform/build_info.py"],
cmd=
"$(location //tensorflow/tools/build_info:gen_build_info.py) --raw_generate \"$@\" --build_config " + if_cuda("cuda", "cpu"),
local=1,
tools=[clean_dep("//tensorflow/tools/build_info:gen_build_info.py")],)


def cc_library_with_android_deps(deps,
android_deps=[],
common_deps=[],
Expand Down
26 changes: 26 additions & 0 deletions tensorflow/tools/build_info/BUILD
@@ -0,0 +1,26 @@
# Description:
# Contains script to generate tensorflow/python/platform/build_info.py
package(default_visibility = ["//tensorflow:internal"])

licenses(["notice"]) # Apache 2.0

exports_files(
glob(["gen/*"]) + [
"gen_build_info.py",
],
)

# -----------------------------------------------------------------------------
# Google-internal targets. These must be at the end for syncrepo.

filegroup(
name = "all_files",
srcs = glob(
["**/*"],
exclude = [
"**/METADATA",
"**/OWNERS",
],
),
visibility = ["//tensorflow:__subpackages__"],
)
77 changes: 77 additions & 0 deletions tensorflow/tools/build_info/gen_build_info.py
@@ -0,0 +1,77 @@
#!/usr/bin/env python
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Generates a Python module containing information about the build."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse


def write_build_info(filename, build_config):
"""Writes a Python that describes the build.
Args:
filename: filename to write to.
build_config: A string containinggit_version: the result of a git describe.
"""
module_docstring = "\"\"\"Generates a Python module containing information "
module_docstring += "about the build.\"\"\""
if build_config == "cuda":
build_config_bool = "True"
else:
build_config_bool = "False"

contents = """
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
%s
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
is_cuda_build = %s
""" % (module_docstring, build_config_bool)
open(filename, "w").write(contents)


parser = argparse.ArgumentParser(
description="""Build info injection into the PIP package.""")

parser.add_argument(
"--build_config",
type=str,
help="Either 'cuda' for GPU builds or 'cpu' for CPU builds.")

parser.add_argument("--raw_generate", type=str, help="Generate build_info.py")

args = parser.parse_args()

if args.raw_generate is not None and args.build_config is not None:
write_build_info(args.raw_generate, args.build_config)
else:
raise RuntimeError("--raw_generate and --build_config must be used")

0 comments on commit 59bdf59

Please sign in to comment.