Skip to content
This repository has been archived by the owner on Jan 3, 2023. It is now read-only.

Commit

Permalink
Add bazel support to build nGraph (#434)
Browse files Browse the repository at this point in the history
* Added bazel scripts for building libngraph_bridge.so and the INTERPRETER backend
* Added support for using the proper CXX ABI based on what is used to buld TensorFlow. Also upgraded the JSON library and fixed the interpreter build
* Fixed the example so that it works on CentOS. Also added the NDEBUG flag to ensure that the graph mods work.
* Fixed a makefile bug and updated the documentation
* Cleaned up and added a README for the bazel build setup
* Update README.md
* Update examples/tf_cpp_examples/README.md
* Added SDL flags and also updated based on PR comments
  • Loading branch information
avijit-nervana authored Feb 20, 2019
1 parent 2f078e4 commit 22d30d2
Show file tree
Hide file tree
Showing 15 changed files with 628 additions and 24 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ cpu_codegen/

*.pbtxt
*.dot
*.whl
*.bzl
.bazelrc

.*.swp
.nfs*
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ TensorFlow model scripts and running them the usual way:

Note: The version of the ngraph-tensorflow-bridge is not going to be exactly the same as when you build from source. This is due to delay in the source release and publishing the corresponding Python wheel.

### Option 2: Build nGraph bridge from source using TensorFlow source
### Option 2: Build nGraph bridge from source

To use the latest version, or to run unit tests, or if you are planning to contribute, install the nGraph
bridge using the TensorFlow source tree as follows:
Expand Down
88 changes: 88 additions & 0 deletions bazel/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# ==============================================================================
# Copyright 2019 Intel Corporation
#
# 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.
# ==============================================================================
load("//:cxx_abi_option.bzl", "CXX_ABI")

cc_binary(
name = 'libngraph_bridge.so',
srcs = [
"src/ngraph_api.cc",
"src/ngraph_api.h",
"src/ngraph_assign_clusters.cc",
"src/ngraph_assign_clusters.h",
"src/ngraph_builder.cc",
"src/ngraph_builder.h",
"src/ngraph_backend_manager.h",
"src/ngraph_backend_manager.cc",
"src/ngraph_capture_variables.cc",
"src/ngraph_capture_variables.h",
"src/ngraph_cluster_manager.cc",
"src/ngraph_cluster_manager.h",
"src/ngraph_conversions.h",
"src/ngraph_deassign_clusters.cc",
"src/ngraph_deassign_clusters.h",
"src/ngraph_encapsulate_clusters.cc",
"src/ngraph_encapsulate_clusters.h",
"src/ngraph_encapsulate_op.cc",
"src/ngraph_freshness_tracker.cc",
"src/ngraph_freshness_tracker.h",
"src/ngraph_mark_for_clustering.cc",
"src/ngraph_mark_for_clustering.h",
"src/ngraph_rewrite_for_tracking.cc",
"src/ngraph_rewrite_for_tracking.h",
"src/ngraph_rewrite_pass.cc",
"src/ngraph_tracked_variable.cc",
"src/ngraph_utils.cc",
"src/ngraph_utils.h",
"src/ngraph_version_utils.h",
"src/tf_deadness_analysis.cc",
"src/tf_deadness_analysis.h",
"src/tf_graphcycles.cc",
"src/tf_graphcycles.h",
"src/version.h",
"src/version.cc",
"logging/ngraph_log.h",
"logging/ngraph_log.cc",
"logging/tf_graph_writer.h",
"logging/tf_graph_writer.cc",
],
linkshared = 1,
deps = [
"@local_config_tf//:libtensorflow_framework",
"@local_config_tf//:tf_header_lib",
"@ngraph//:ngraph_headers",
"@ngraph//:ngraph_core",
],
copts = [
"-pthread",
"-std=c++11",
"-D_FORTIFY_SOURCE=2",
"-Wformat",
"-Wformat-security",
"-Wformat",
"-fstack-protector-strong",
"-D NDEBUG",
'-D SHARED_LIB_PREFIX=\\"lib\\"',
'-D SHARED_LIB_SUFFIX=\\".so\\"',
"-I logging",
"-I external/ngraph/src",
] + CXX_ABI,
linkopts = [
"-Wl,-z,noexecstack",
"-Wl,-z,relro",
"-Wl,-z,now",
],
visibility = ["//visibility:public"],
)
45 changes: 45 additions & 0 deletions bazel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Build nGraph TensorFlow bridge using bazel

This directory contains scripts necessary to build the nGraph TensorFlow bridge using `bazel`.

:warning: This is experimental and will change over time.

## Prerequisites

Please ensure that bazel and Python is installed on your system and you are able to build TensorFlow from source (though not needed for building the bridge). Please see the [build preperation] for details.

## Build C++ library

Go to the ngraph-tf directory and execute these commands to build the C++ library for nGraph-TensorFlow bridge:

./configure_bazel.sh
bazel build libngraph_bridge.so
bazel build @ngraph//:libinterpreter_backend.so

This will produce the following binary files:

```
bazel-bin/libngraph_bridge.so
bazel-bin/external/ngraph/libinterpreter_backend.so
```

### How to use the C++ library

The C++ library `libngraph_bridge.so` can be used with a TensorFlow C++ application as described in the examples/tf_cpp_examples ([TensorFlow C++ example]) directory. Basic steps are the following:

1. Get a copy of the TensorFlow C++ library by building one. Use [Option 2] to build all the necessary libraries as described in the [TensorFlow C++ example]

2. Replace the `libngraph_bridge.so` built by this bazel script in the `build/artifacts/lib<64>` directory.

3. Run `make` to relink and run the example as described in the [TensorFlow C++ example] document.

**Note** Currently only the INTERPRETER backend may be built using bazel. However other backends built using cmake system is fully binary compatible with the bridge built with bazel based build system as described here.

## Build the Python wheel

Coming up soon. For now please use the cmake based build system described in the [main README]

[build preperation]: ../README.md#prepare-the-build-environment
[Option 2]: ../README.md#option-2-build-ngraph-bridge-from-source
[TensorFlow C++ example]: ../examples/tf_cpp_examples/README.md
[main README]: ../README.md
44 changes: 44 additions & 0 deletions bazel/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# ==============================================================================
# Copyright 2019 Intel Corporation
#
# 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.
# ==============================================================================

workspace(name = "ngraph_bridge")
load("//tf_configure:tf_configure.bzl", "tf_configure")

tf_configure(
name = "local_config_tf",
)

new_http_archive(
name = "ngraph",
build_file = "bazel/ngraph.BUILD",
sha256 = "1efd0cae2bc8febe40863727fcadf7eecbf7724073c5ddd2c95c6db00dd70985",
strip_prefix = "ngraph-0.14.0-rc.1",
urls = [
"https://mirror.bazel.build/github.com/NervanaSystems/ngraph/archive/v0.14.0-rc.1.tar.gz",
"https://github.com/NervanaSystems/ngraph/archive/v0.14.0-rc.1.tar.gz",
],
)

new_http_archive(
name = "nlohmann_json_lib",
build_file = "bazel/nlohmann_json.BUILD",
sha256 = "e0b1fc6cc6ca05706cce99118a87aca5248bd9db3113e703023d23f044995c1d",
strip_prefix = "json-3.5.0",
urls = [
"https://mirror.bazel.build/github.com/nlohmann/json/archive/v3.5.0.tar.gz",
"https://github.com/nlohmann/json/archive/v3.5.0.tar.gz",
],
)
115 changes: 115 additions & 0 deletions bazel/ngraph.BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# ==============================================================================
# Copyright 2019 Intel Corporation
#
# 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.
# ==============================================================================
licenses(["notice"])
exports_files(["LICENSE"])

load("@ngraph_bridge//:cxx_abi_option.bzl", "CXX_ABI")

cc_library(
name = "ngraph_headers",
hdrs = glob(["src/ngraph/**/*.hpp"]),
visibility = ["//visibility:public"],
)

cc_library(
name = "ngraph_core",
srcs = glob([
"src/ngraph/*.cpp",
"src/ngraph/autodiff/*.cpp",
"src/ngraph/builder/*.cpp",
"src/ngraph/descriptor/*.cpp",
"src/ngraph/descriptor/layout/*.cpp",
"src/ngraph/op/*.cpp",
"src/ngraph/op/experimental/generate_mask.cpp",
"src/ngraph/op/experimental/quantized_avg_pool.cpp",
"src/ngraph/op/experimental/quantized_conv.cpp",
"src/ngraph/op/experimental/quantized_conv_bias.cpp",
"src/ngraph/op/experimental/quantized_conv_relu.cpp",
"src/ngraph/op/experimental/quantized_max_pool.cpp",
"src/ngraph/op/experimental/shape_of.cpp",
"src/ngraph/op/util/*.cpp",
"src/ngraph/pattern/*.cpp",
"src/ngraph/pattern/*.hpp",
"src/ngraph/pass/*.cpp",
"src/ngraph/pass/*.hpp",
"src/ngraph/runtime/*.cpp",
"src/ngraph/type/*.cpp",
],
exclude = [
"src/ngraph/ngraph.cpp",
]),
deps = [
":ngraph_headers",
"@nlohmann_json_lib",
],
copts = [
"-I external/ngraph/src",
"-I external/nlohmann_json_lib/include/",
"-D_FORTIFY_SOURCE=2",
"-Wformat",
"-Wformat-security",
"-Wformat",
"-fstack-protector-strong",
'-D SHARED_LIB_PREFIX=\\"lib\\"',
'-D SHARED_LIB_SUFFIX=\\".so\\"',
'-D NGRAPH_VERSION=\\"0.14.0-rc.1\\"',
"-D NGRAPH_DEX_ONLY",
'-D PROJECT_ROOT_DIR=\\"\\"',
] + CXX_ABI,
linkopts = [
"-Wl,-z,noexecstack",
"-Wl,-z,relro",
"-Wl,-z,now",
],
visibility = ["//visibility:public"],
alwayslink = 1,
)

cc_binary(
name = 'libinterpreter_backend.so',
srcs = glob([
"src/ngraph/except.hpp",
"src/ngraph/runtime/interpreter/*.cpp",
"src/ngraph/state/rng_state.cpp",
]),
deps = [
":ngraph_headers",
":ngraph_core",
],
copts = [
"-I external/ngraph/src",
"-I external/ngraph/src/ngraph",
"-I external/nlohmann_json_lib/include/",
"-D_FORTIFY_SOURCE=2",
"-Wformat",
"-Wformat-security",
"-Wformat",
"-fstack-protector-strong",
'-D SHARED_LIB_PREFIX=\\"lib\\"',
'-D SHARED_LIB_SUFFIX=\\".so\\"',
'-D NGRAPH_VERSION=\\"0.14.0-rc.1\\"',
"-D NGRAPH_DEX_ONLY",
'-D PROJECT_ROOT_DIR=\\"\\"',
] + CXX_ABI,
linkopts = [
"-Wl,-z,noexecstack",
"-Wl,-z,relro",
"-Wl,-z,now",
],
linkshared = 1,
visibility = ["//visibility:public"],
)

31 changes: 31 additions & 0 deletions bazel/nlohmann_json.BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# ==============================================================================
# Copyright 2019 Intel Corporation
#
# 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.
# ==============================================================================
licenses(["notice"])

exports_files(["LICENSE.MIT"])
load("@ngraph_bridge//:cxx_abi_option.bzl", "CXX_ABI")

cc_library(
name = "nlohmann_json_lib",
hdrs = glob([
"include/nlohmann/**/*.hpp",
]),
copts = [
"-I external/nlohmann_json_lib",
]+ CXX_ABI,
visibility = ["//visibility:public"],
alwayslink = 1,
)
Empty file added bazel/tf_configure/BUILD
Empty file.
18 changes: 18 additions & 0 deletions bazel/tf_configure/BUILD.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package(default_visibility = ["//visibility:public"])

cc_library(
name = "tf_header_lib",
hdrs = [":tf_header_include"],
includes = ["include"],
visibility = ["//visibility:public"],
)

cc_library(
name = "libtensorflow_framework",
srcs = [":libtensorflow_framework.so"],
#data = ["lib/libtensorflow_framework.so"],
visibility = ["//visibility:public"],
)

%{TF_HEADER_GENRULE}
%{TF_SHARED_LIBRARY_GENRULE}
Loading

0 comments on commit 22d30d2

Please sign in to comment.