Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions tensorflow/tools/sdk_package/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Description:
# TensorFlow is a computational framework, primarily for use in machine
# learning applications.
#
# Public targets:
#
# ":sdk_package" - Package the tensorflow dynamic library and necessry
# headers for developing. The script should be executed manually
# after 'bazel build'.

package(default_visibility = ["//visibility:public"])

load("//tensorflow:tensorflow.bzl", "transitive_hdrs", "tf_binary_additional_srcs")
load("//tensorflow/core/platform:default/build_config_root.bzl",
"tf_additional_plugin_deps")

transitive_hdrs(
name = "sdk_headers",
deps = [
# Need to check definition of //tensorflow:libtensorflow_cc.so
# for updates.
"//tensorflow/c:c_api",
"//tensorflow/cc:cc_ops",
"//tensorflow/cc:client_session",
"//tensorflow/cc:scope",
"//tensorflow/cc/saved_model:loader",
"//tensorflow/cc/saved_model:signature_constants",
"//tensorflow/cc/saved_model:tag_constants",
"//tensorflow/contrib/session_bundle:bundle_shim",
] + tf_additional_plugin_deps(),
tags = ["manual"],
)

sh_binary(
name = "build_sdk_package",
srcs = ["build_sdk_package.sh"],
data = [
":sdk_headers",
"@com_google_protobuf//:protoc",
"//tensorflow:libtensorflow_cc.so",
] + tf_binary_additional_srcs(),
tags = ["manual"],
)
41 changes: 41 additions & 0 deletions tensorflow/tools/sdk_package/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Bazel rules and bash scripts to package the DeepRec C/C++ APIs and
runtime library into '\<DeepRec Root Path\>/tensorflow_sdk.tar.gz' archive.

## SDK Build

First of all, edit and run the configurating script **'./configure'** under
DeeRec root directory (supposed '\<DeepRec Root Path\>').

Then simply run the following commands under '\<DeepRec Root Path\>' to build
the DeepRec SDK package:

```sh
./build sdk
```
_This command will put the SDK package named 'tensorflow\_sdk.tar.gz' into
the directory below:_
> <DeepRec Root Path>/built/sdk/[gpu|cpu]

## SDK usage:

To make use of DeepRec runtime SDK for C++ codes writting with original APIs
defined in TensorFlow, just decompress the SDK package into another work
directory (supposed '\<workdir path\>') with the command at first:

```sh
tar xzvf -C <workdir path> tensorflow_sdk.tar.gz
```

Then a directory named 'sdk' will be placed into the \<workdir path\>, which
contains necessary header files in the 'include' sub-directory, keeping the
original hierarchy in TensorFlow, and the 'libtensorflow_cc.so' dynamic
runtime library in the 'lib' sub-directoy to support TensorFlow running.

Just append **'-I\<workdir path\>/sdk/include'** to compiling arguments and
**'-L\<workdir path\>/sdk/lib'** -ltensorflow_cc to linking arguments, in the
cases of building a project, that contains codes using original TensorFlow
C++ APIs, together with DeepRec SDK.

Finally, to successfully run the binary building with DeepRec SDK, do not
forget to append '\<workdir path\>/sdk/lib' to **'LD_LIBRARY_PATH'** environment
variable.
136 changes: 136 additions & 0 deletions tensorflow/tools/sdk_package/build_sdk_package.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#!/usr/bin/env bash
# Copyright 2024 The DeepRec 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.
# ==============================================================================

# This script is used for packaging TensorFlow SDK files into a tarball.
# The processing flow took 'tensorflow/tools/pip_package/build_pip_package.sh'
# as the reference.

set -e

PLATFORM="$(uname -s | tr 'A-Z' 'a-z')"
function is_windows() {
# On windows, the shell script is actually running in msys
if [[ "${PLATFORM}" =~ msys_nt* ]]; then
true
else
false
fi
}

function main() {
if [ $# -lt 1 ] ; then
echo "No destination dir provided"
exit 1
fi

DEST=$1
TMPDIR=$(mktemp -d -t tmp.XXXXXXXXXX)
mkdir -p "${TMPDIR}/sdk/bin"
mkdir -p "${TMPDIR}/sdk/include"
mkdir -p "${TMPDIR}/sdk/lib"

echo $(date) : "=== Using tmpdir: ${TMPDIR}"

if [ ! -d bazel-bin/tensorflow ]; then
echo "Could not find bazel-bin. Did you run from the root of the build tree?"
exit 1
fi

if is_windows; then
echo "Windows version TensorFlow SDK not supported..."
elif [ ! -d bazel-bin/tensorflow/tools/sdk_package/build_sdk_package.runfiles/org_tensorflow ]; then
# Really old (0.2.1-) runfiles, without workspace name.
echo "TensorFlow SDK does not support such old verions..."
else
RUNFILES=bazel-bin/tensorflow/tools/sdk_package/build_sdk_package.runfiles/org_tensorflow
if [ -d ${RUNFILES}/external ]; then
# Old-style runfiles structure (--legacy_external_runfiles).
cp -RL ${RUNFILES}/tensorflow "${TMPDIR}/sdk/include"
# Check LLVM headers for XLA support.
if [ -d ${RUNFILES}/external/llvm_archive ]; then
# Old-style runfiles structure (--legacy_external_runfiles).
mkdir -p ${TMPDIR}/sdk/include/external/llvm/include
cp -RL ${RUNFILES}/external/llvm_archive/include/llvm \
"${TMPDIR}/sdk/include/external/llvm/include"
pushd ${TMPDIR}/sdk/include
ln -s external/llvm/include/llvm llvm
popd
fi
# Copy MKL libs over so they can be loaded at runtime
so_lib_dir=$(ls $RUNFILES | grep solib) || true
if [ -n "${so_lib_dir}" ]; then
mkl_so_dir=$(ls ${RUNFILES}/${so_lib_dir} | grep mkl) || true
if [ -n "${mkl_so_dir}" ]; then
cp -L ${RUNFILES}/${so_lib_dir}/${mkl_so_dir}/*.so "${TMPDIR}/sdk/lib"
fi
fi
else
# New-style runfiles structure (--nolegacy_external_runfiles).
cp -RL ${RUNFILES}/tensorflow "${TMPDIR}/sdk/include"
# Check LLVM headers for XLA support.
if [ -d bazel-bin/tensorflow/tools/sdk_package/build_sdk_package.runfiles/llvm_archive ]; then
cp -RL \
bazel-bin/tensorflow/tools/sdk_package/build_sdk_package.runfiles/llvm_archive/include/llvm \
"${TMPDIR}/sdk/include"
fi
# Copy MKL libs over so they can be loaded at runtime
so_lib_dir=$(ls $RUNFILES | grep solib) || true
if [ -n "${so_lib_dir}" ]; then
mkl_so_dir=$(ls ${RUNFILES}/${so_lib_dir} | grep mkl) || true
if [ -n "${mkl_so_dir}" ]; then
cp -L ${RUNFILES}/${so_lib_dir}/${mkl_so_dir}/*.so "${TMPDIR}/sdk/lib"
fi
fi
fi
fi

# move and strip the dynamic library file for packaging.
# at default the .so file was not writable for the owner,
# so using a 'chmod +w' to perform the strip command.
chmod +w ${TMPDIR}/sdk/include/tensorflow/libtensorflow_cc.so
chmod +w ${TMPDIR}/sdk/include/tensorflow/libtensorflow_framework.so.1
strip ${TMPDIR}/sdk/include/tensorflow/libtensorflow_cc.so
strip ${TMPDIR}/sdk/include/tensorflow/libtensorflow_framework.so.1
mv ${TMPDIR}/sdk/include/tensorflow/libtensorflow_*.so* ${TMPDIR}/sdk/lib

# third party packages doesn't ship with header files. Copy the headers
# over so user defined ops can be compiled.
mkdir -p ${TMPDIR}/sdk/include/google
mkdir -p ${TMPDIR}/sdk/include/third_party
pushd ${RUNFILES%org_tensorflow}/com_google_protobuf/src/google
for header in $(find protobuf -name \*.h); do
mkdir -p "${TMPDIR}/sdk/include/google/$(dirname ${header})"
cp -L "$header" "${TMPDIR}/sdk/include/google/$(dirname ${header})/"
done
popd
cp -RL $RUNFILES/third_party/eigen3 ${TMPDIR}/sdk/include/third_party
cp -RL ${RUNFILES%org_tensorflow}/eigen_archive/* ${TMPDIR}/sdk/include/
cp -RL ${RUNFILES%org_tensorflow}/nsync/public/* ${TMPDIR}/sdk/include
cp -L ${RUNFILES%org_tensorflow}/com_google_protobuf/protoc ${TMPDIR}/sdk/bin

# package all files into the target file.
pushd ${TMPDIR}
rm -f MANIFEST
echo $(date) : "=== Building sdk package"
tar czvf tensorflow_sdk.tar.gz sdk/ 1> /dev/null
popd
mkdir -p ${DEST}
mv ${TMPDIR}/tensorflow_sdk.tar.gz ${DEST}
rm -rf ${TMPDIR}
echo $(date) : "=== Output sdk package file is: ${DEST}/tensorflow_sdk.tar.gz"
}

main "$@"