Skip to content

Commit 70b32df

Browse files
authored
[BUILD] Add build SDK package. (#972)
Signed-off-by: candy.dc <[email protected]>
1 parent 2b15e8a commit 70b32df

File tree

3 files changed

+220
-0
lines changed

3 files changed

+220
-0
lines changed

tensorflow/tools/sdk_package/BUILD

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Description:
2+
# TensorFlow is a computational framework, primarily for use in machine
3+
# learning applications.
4+
#
5+
# Public targets:
6+
#
7+
# ":sdk_package" - Package the tensorflow dynamic library and necessry
8+
# headers for developing. The script should be executed manually
9+
# after 'bazel build'.
10+
11+
package(default_visibility = ["//visibility:public"])
12+
13+
load("//tensorflow:tensorflow.bzl", "transitive_hdrs", "tf_binary_additional_srcs")
14+
load("//tensorflow/core/platform:default/build_config_root.bzl",
15+
"tf_additional_plugin_deps")
16+
17+
transitive_hdrs(
18+
name = "sdk_headers",
19+
deps = [
20+
# Need to check definition of //tensorflow:libtensorflow_cc.so
21+
# for updates.
22+
"//tensorflow/c:c_api",
23+
"//tensorflow/cc:cc_ops",
24+
"//tensorflow/cc:client_session",
25+
"//tensorflow/cc:scope",
26+
"//tensorflow/cc/saved_model:loader",
27+
"//tensorflow/cc/saved_model:signature_constants",
28+
"//tensorflow/cc/saved_model:tag_constants",
29+
"//tensorflow/contrib/session_bundle:bundle_shim",
30+
] + tf_additional_plugin_deps(),
31+
tags = ["manual"],
32+
)
33+
34+
sh_binary(
35+
name = "build_sdk_package",
36+
srcs = ["build_sdk_package.sh"],
37+
data = [
38+
":sdk_headers",
39+
"@com_google_protobuf//:protoc",
40+
"//tensorflow:libtensorflow_cc.so",
41+
] + tf_binary_additional_srcs(),
42+
tags = ["manual"],
43+
)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Bazel rules and bash scripts to package the DeepRec C/C++ APIs and
2+
runtime library into '\<DeepRec Root Path\>/tensorflow_sdk.tar.gz' archive.
3+
4+
## SDK Build
5+
6+
First of all, edit and run the configurating script **'./configure'** under
7+
DeeRec root directory (supposed '\<DeepRec Root Path\>').
8+
9+
Then simply run the following commands under '\<DeepRec Root Path\>' to build
10+
the DeepRec SDK package:
11+
12+
```sh
13+
./build sdk
14+
```
15+
_This command will put the SDK package named 'tensorflow\_sdk.tar.gz' into
16+
the directory below:_
17+
> <DeepRec Root Path>/built/sdk/[gpu|cpu]
18+
19+
## SDK usage:
20+
21+
To make use of DeepRec runtime SDK for C++ codes writting with original APIs
22+
defined in TensorFlow, just decompress the SDK package into another work
23+
directory (supposed '\<workdir path\>') with the command at first:
24+
25+
```sh
26+
tar xzvf -C <workdir path> tensorflow_sdk.tar.gz
27+
```
28+
29+
Then a directory named 'sdk' will be placed into the \<workdir path\>, which
30+
contains necessary header files in the 'include' sub-directory, keeping the
31+
original hierarchy in TensorFlow, and the 'libtensorflow_cc.so' dynamic
32+
runtime library in the 'lib' sub-directoy to support TensorFlow running.
33+
34+
Just append **'-I\<workdir path\>/sdk/include'** to compiling arguments and
35+
**'-L\<workdir path\>/sdk/lib'** -ltensorflow_cc to linking arguments, in the
36+
cases of building a project, that contains codes using original TensorFlow
37+
C++ APIs, together with DeepRec SDK.
38+
39+
Finally, to successfully run the binary building with DeepRec SDK, do not
40+
forget to append '\<workdir path\>/sdk/lib' to **'LD_LIBRARY_PATH'** environment
41+
variable.
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2024 The DeepRec Authors. All Rights Reserved.
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+
17+
# This script is used for packaging TensorFlow SDK files into a tarball.
18+
# The processing flow took 'tensorflow/tools/pip_package/build_pip_package.sh'
19+
# as the reference.
20+
21+
set -e
22+
23+
PLATFORM="$(uname -s | tr 'A-Z' 'a-z')"
24+
function is_windows() {
25+
# On windows, the shell script is actually running in msys
26+
if [[ "${PLATFORM}" =~ msys_nt* ]]; then
27+
true
28+
else
29+
false
30+
fi
31+
}
32+
33+
function main() {
34+
if [ $# -lt 1 ] ; then
35+
echo "No destination dir provided"
36+
exit 1
37+
fi
38+
39+
DEST=$1
40+
TMPDIR=$(mktemp -d -t tmp.XXXXXXXXXX)
41+
mkdir -p "${TMPDIR}/sdk/bin"
42+
mkdir -p "${TMPDIR}/sdk/include"
43+
mkdir -p "${TMPDIR}/sdk/lib"
44+
45+
echo $(date) : "=== Using tmpdir: ${TMPDIR}"
46+
47+
if [ ! -d bazel-bin/tensorflow ]; then
48+
echo "Could not find bazel-bin. Did you run from the root of the build tree?"
49+
exit 1
50+
fi
51+
52+
if is_windows; then
53+
echo "Windows version TensorFlow SDK not supported..."
54+
elif [ ! -d bazel-bin/tensorflow/tools/sdk_package/build_sdk_package.runfiles/org_tensorflow ]; then
55+
# Really old (0.2.1-) runfiles, without workspace name.
56+
echo "TensorFlow SDK does not support such old verions..."
57+
else
58+
RUNFILES=bazel-bin/tensorflow/tools/sdk_package/build_sdk_package.runfiles/org_tensorflow
59+
if [ -d ${RUNFILES}/external ]; then
60+
# Old-style runfiles structure (--legacy_external_runfiles).
61+
cp -RL ${RUNFILES}/tensorflow "${TMPDIR}/sdk/include"
62+
# Check LLVM headers for XLA support.
63+
if [ -d ${RUNFILES}/external/llvm_archive ]; then
64+
# Old-style runfiles structure (--legacy_external_runfiles).
65+
mkdir -p ${TMPDIR}/sdk/include/external/llvm/include
66+
cp -RL ${RUNFILES}/external/llvm_archive/include/llvm \
67+
"${TMPDIR}/sdk/include/external/llvm/include"
68+
pushd ${TMPDIR}/sdk/include
69+
ln -s external/llvm/include/llvm llvm
70+
popd
71+
fi
72+
# Copy MKL libs over so they can be loaded at runtime
73+
so_lib_dir=$(ls $RUNFILES | grep solib) || true
74+
if [ -n "${so_lib_dir}" ]; then
75+
mkl_so_dir=$(ls ${RUNFILES}/${so_lib_dir} | grep mkl) || true
76+
if [ -n "${mkl_so_dir}" ]; then
77+
cp -L ${RUNFILES}/${so_lib_dir}/${mkl_so_dir}/*.so "${TMPDIR}/sdk/lib"
78+
fi
79+
fi
80+
else
81+
# New-style runfiles structure (--nolegacy_external_runfiles).
82+
cp -RL ${RUNFILES}/tensorflow "${TMPDIR}/sdk/include"
83+
# Check LLVM headers for XLA support.
84+
if [ -d bazel-bin/tensorflow/tools/sdk_package/build_sdk_package.runfiles/llvm_archive ]; then
85+
cp -RL \
86+
bazel-bin/tensorflow/tools/sdk_package/build_sdk_package.runfiles/llvm_archive/include/llvm \
87+
"${TMPDIR}/sdk/include"
88+
fi
89+
# Copy MKL libs over so they can be loaded at runtime
90+
so_lib_dir=$(ls $RUNFILES | grep solib) || true
91+
if [ -n "${so_lib_dir}" ]; then
92+
mkl_so_dir=$(ls ${RUNFILES}/${so_lib_dir} | grep mkl) || true
93+
if [ -n "${mkl_so_dir}" ]; then
94+
cp -L ${RUNFILES}/${so_lib_dir}/${mkl_so_dir}/*.so "${TMPDIR}/sdk/lib"
95+
fi
96+
fi
97+
fi
98+
fi
99+
100+
# move and strip the dynamic library file for packaging.
101+
# at default the .so file was not writable for the owner,
102+
# so using a 'chmod +w' to perform the strip command.
103+
chmod +w ${TMPDIR}/sdk/include/tensorflow/libtensorflow_cc.so
104+
chmod +w ${TMPDIR}/sdk/include/tensorflow/libtensorflow_framework.so.1
105+
strip ${TMPDIR}/sdk/include/tensorflow/libtensorflow_cc.so
106+
strip ${TMPDIR}/sdk/include/tensorflow/libtensorflow_framework.so.1
107+
mv ${TMPDIR}/sdk/include/tensorflow/libtensorflow_*.so* ${TMPDIR}/sdk/lib
108+
109+
# third party packages doesn't ship with header files. Copy the headers
110+
# over so user defined ops can be compiled.
111+
mkdir -p ${TMPDIR}/sdk/include/google
112+
mkdir -p ${TMPDIR}/sdk/include/third_party
113+
pushd ${RUNFILES%org_tensorflow}/com_google_protobuf/src/google
114+
for header in $(find protobuf -name \*.h); do
115+
mkdir -p "${TMPDIR}/sdk/include/google/$(dirname ${header})"
116+
cp -L "$header" "${TMPDIR}/sdk/include/google/$(dirname ${header})/"
117+
done
118+
popd
119+
cp -RL $RUNFILES/third_party/eigen3 ${TMPDIR}/sdk/include/third_party
120+
cp -RL ${RUNFILES%org_tensorflow}/eigen_archive/* ${TMPDIR}/sdk/include/
121+
cp -RL ${RUNFILES%org_tensorflow}/nsync/public/* ${TMPDIR}/sdk/include
122+
cp -L ${RUNFILES%org_tensorflow}/com_google_protobuf/protoc ${TMPDIR}/sdk/bin
123+
124+
# package all files into the target file.
125+
pushd ${TMPDIR}
126+
rm -f MANIFEST
127+
echo $(date) : "=== Building sdk package"
128+
tar czvf tensorflow_sdk.tar.gz sdk/ 1> /dev/null
129+
popd
130+
mkdir -p ${DEST}
131+
mv ${TMPDIR}/tensorflow_sdk.tar.gz ${DEST}
132+
rm -rf ${TMPDIR}
133+
echo $(date) : "=== Output sdk package file is: ${DEST}/tensorflow_sdk.tar.gz"
134+
}
135+
136+
main "$@"

0 commit comments

Comments
 (0)