Skip to content

Commit 0aaa5f9

Browse files
committed
Initial commit
0 parents  commit 0aaa5f9

File tree

10 files changed

+371
-0
lines changed

10 files changed

+371
-0
lines changed

.gitignore

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Prerequisites
2+
*.d
3+
4+
# Compiled Object files
5+
*.slo
6+
*.lo
7+
*.o
8+
*.obj
9+
10+
# Precompiled Headers
11+
*.gch
12+
*.pch
13+
14+
# Compiled Dynamic libraries
15+
*.so
16+
*.dylib
17+
*.dll
18+
19+
# Fortran module files
20+
*.mod
21+
*.smod
22+
23+
# Compiled Static libraries
24+
*.lai
25+
*.la
26+
*.a
27+
*.lib
28+
29+
# Executables
30+
*.exe
31+
*.out
32+
*.app
33+
34+
# Build folders
35+
example/build
36+
tensorflow_cc/build

Dockerfile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
FROM ubuntu
2+
3+
ARG shared=OFF
4+
5+
RUN apt-get update -y
6+
RUN apt-get -y install build-essential curl git cmake unzip autoconf autogen libtool \
7+
python python3-numpy python3-dev python3-pip python3-wheel
8+
9+
# install bazel for the shared library version
10+
RUN echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | tee /etc/apt/sources.list.d/bazel.list
11+
RUN curl https://bazel.build/bazel-release.pub.gpg | apt-key add -
12+
RUN apt-get -y update && apt-get -y install bazel
13+
14+
# clone the repository
15+
RUN git clone https://github.com/FloopCZ/tensorflow_cc.git
16+
17+
# install tensorflow
18+
RUN mkdir /tensorflow_cc/tensorflow_cc/build
19+
WORKDIR /tensorflow_cc/tensorflow_cc/build
20+
RUN cmake -DTENSORFLOW_SHARED=${shared} ..
21+
RUN make && make install
22+
23+
# build and run example
24+
RUN mkdir /tensorflow_cc/example/build
25+
WORKDIR /tensorflow_cc/example/build
26+
RUN cmake .. && make && ./example

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Filip Matzner
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# tensorflow_cc
2+
[![Build Status](http://plum.floop.cz:8080/buildStatus/icon?job=tensorflow_cc)](http://plum.floop.cz:8080/job/tensorflow_cc/)
3+
4+
This repository contains two CMake projects. The [tensorflow_cc](tensorflow_cc) project downloads, builds and installs the [TensorFlow C++](https://www.tensorflow.org/api_docs/cc/) library into the operatring system and the [example](example) project demonstrates its simple usage.
5+
6+
This repository allows to use the TensorFlow C++ library from the outside of the TensorFlow folder hierarchy and without the use of the [Bazel](https://bazel.build/) build system.
7+
8+
### Install Requirements (Ubuntu 16.04+)
9+
```
10+
sudo apt-get -y install build-essential curl git cmake unzip autoconf autogen libtool \
11+
python python3-numpy python3-dev python3-pip python3-wheel
12+
```
13+
14+
If you require GPU support, please also install [Bazel](https://bazel.build/), NVIDIA CUDA Toolkit, NVIDIA drivers, cuDNN, and `libcupti-dev` package. The tensorflow build script will automatically detect CUDA if it is installed in `/opt/cuda` directory.
15+
16+
### Clone the Repository
17+
```
18+
git clone https://github.com/FloopCZ/tensorflow_cc.git
19+
cd tensorflow_cc
20+
```
21+
22+
### Install the TensorFlow C++ Library
23+
```
24+
cd tensorflow_cc
25+
mkdir build && cd build
26+
cmake ..
27+
# alternatively, use the following for GPU support (requires Bazel)
28+
# cmake -DTENSORFLOW_SHARED=ON ..
29+
make && sudo make install
30+
```
31+
32+
### Build and Run the Example
33+
```
34+
cd ../../example
35+
mkdir build && cd build
36+
cmake .. && make
37+
./example
38+
```

example/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
2+
3+
find_package(TensorflowCC REQUIRED)
4+
add_executable(example example.cpp)
5+
target_link_libraries(
6+
example
7+
PRIVATE TensorflowCC
8+
)

example/example.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <tensorflow/core/platform/env.h>
2+
#include <tensorflow/core/public/session.h>
3+
4+
#include <iostream>
5+
6+
using namespace std;
7+
using namespace tensorflow;
8+
9+
int main()
10+
{
11+
Session* session;
12+
Status status = NewSession(SessionOptions(), &session);
13+
if (!status.ok()) {
14+
cout << status.ToString() << "\n";
15+
return 1;
16+
}
17+
cout << "Session successfully created.\n";
18+
}

tensorflow_cc/CMakeLists.txt

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
2+
3+
# Static library with no GPU support is built by default.
4+
# Use the following option to build a shared library with GPU support.
5+
# If enabled, bazel has to be installed.
6+
option(TENSORFLOW_SHARED "Build shared library (required for GPU support)." OFF)
7+
8+
# -------------
9+
# CMake Options
10+
# -------------
11+
12+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
13+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/lib")
14+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/lib")
15+
include(CMakePackageConfigHelpers)
16+
set(CMAKECFG_INSTALL_DIR lib/cmake/TensorflowCC)
17+
18+
# ---------------------------------
19+
# Build static / dynamic tensorflow
20+
# ---------------------------------
21+
22+
if (TENSORFLOW_SHARED)
23+
include(TensorflowShared)
24+
else()
25+
include(TensorflowStatic)
26+
endif()
27+
28+
# ---------------------
29+
# Add interface library
30+
# ---------------------
31+
32+
add_library(tensorflow_cc INTERFACE)
33+
set_target_properties(
34+
tensorflow_cc PROPERTIES EXPORT_NAME TensorflowCC
35+
)
36+
target_compile_options(
37+
tensorflow_cc INTERFACE
38+
"-std=c++1z"
39+
)
40+
41+
# -----------------------
42+
# Build shared tensorflow
43+
# -----------------------
44+
45+
if(TENSORFLOW_SHARED)
46+
add_dependencies(
47+
tensorflow_cc
48+
tensorflow_shared
49+
)
50+
target_include_directories(
51+
tensorflow_cc INTERFACE
52+
${CMAKE_INSTALL_PREFIX}/include/tensorflow
53+
${CMAKE_INSTALL_PREFIX}/include/tensorflow/bazel-genfiles
54+
${CMAKE_INSTALL_PREFIX}/include/tensorflow/tensorflow/contrib/makefile/downloads
55+
${CMAKE_INSTALL_PREFIX}/include/tensorflow/tensorflow/contrib/makefile/downloads/eigen
56+
${CMAKE_INSTALL_PREFIX}/include/tensorflow/tensorflow/contrib/makefile/downloads/gemmlowp
57+
${CMAKE_INSTALL_PREFIX}/include/tensorflow/tensorflow/contrib/makefile/gen/protobuf-host/include
58+
)
59+
target_link_libraries(
60+
tensorflow_cc INTERFACE
61+
"${CMAKE_INSTALL_PREFIX}/lib/tensorflow_cc/libtensorflow_cc.so"
62+
# static protobuf is used from the contrib/makefile
63+
"${CMAKE_INSTALL_PREFIX}/lib/tensorflow_cc/libprotobuf.a"
64+
dl pthread
65+
)
66+
67+
# -----------------------
68+
# Build static tensorflow
69+
# -----------------------
70+
71+
else()
72+
add_dependencies(
73+
tensorflow_cc
74+
tensorflow_static
75+
)
76+
target_include_directories(
77+
tensorflow_cc INTERFACE
78+
${CMAKE_INSTALL_PREFIX}/include/tensorflow
79+
${CMAKE_INSTALL_PREFIX}/include/tensorflow/tensorflow/contrib/makefile/gen/host_obj
80+
${CMAKE_INSTALL_PREFIX}/include/tensorflow/tensorflow/contrib/makefile/downloads
81+
${CMAKE_INSTALL_PREFIX}/include/tensorflow/tensorflow/contrib/makefile/downloads/eigen
82+
${CMAKE_INSTALL_PREFIX}/include/tensorflow/tensorflow/contrib/makefile/downloads/gemmlowp
83+
${CMAKE_INSTALL_PREFIX}/include/tensorflow/tensorflow/contrib/makefile/gen/protobuf-host/include
84+
${CMAKE_INSTALL_PREFIX}/include/tensorflow/tensorflow/contrib/makefile/gen/proto
85+
${CMAKE_INSTALL_PREFIX}/include/tensorflow/tensorflow/contrib/makefile/gen/proto_text
86+
)
87+
target_link_libraries(
88+
tensorflow_cc INTERFACE
89+
"-Wl,--allow-multiple-definition"
90+
"-Wl,--whole-archive ${CMAKE_INSTALL_PREFIX}/lib/tensorflow_cc/libtensorflow-core.a"
91+
"-Wl,--no-whole-archive"
92+
"${CMAKE_INSTALL_PREFIX}/lib/tensorflow_cc/libprotobuf.a"
93+
dl pthread
94+
)
95+
endif()
96+
97+
# -------
98+
# Install
99+
# -------
100+
101+
# install all header files
102+
install(
103+
DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/tensorflow/"
104+
DESTINATION include/tensorflow
105+
FILES_MATCHING PATTERN "*.h"
106+
)
107+
# install all files downloaded by contrib/makefile as dependencies
108+
install(
109+
DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/tensorflow/tensorflow/contrib/makefile/downloads/"
110+
DESTINATION include/tensorflow/tensorflow/contrib/makefile/downloads
111+
)
112+
# install all files from third_party folder (e.g., Eigen/Tensor)
113+
install(
114+
DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/tensorflow/third_party/"
115+
DESTINATION include/tensorflow/third_party
116+
)
117+
# install static libprotobuf from contrib/makefile
118+
install(
119+
FILES "${CMAKE_CURRENT_BINARY_DIR}/tensorflow/tensorflow/contrib/makefile/gen/protobuf-host/lib/libprotobuf.a"
120+
DESTINATION lib/tensorflow_cc
121+
)
122+
# shared library specific
123+
if(TENSORFLOW_SHARED)
124+
install(
125+
FILES "${CMAKE_CURRENT_BINARY_DIR}/tensorflow/bazel-bin/tensorflow/libtensorflow_cc.so"
126+
DESTINATION lib/tensorflow_cc
127+
)
128+
# static library specific
129+
else()
130+
install(
131+
FILES "${CMAKE_CURRENT_BINARY_DIR}/tensorflow/tensorflow/contrib/makefile/gen/lib/libtensorflow-core.a"
132+
DESTINATION lib/tensorflow_cc
133+
)
134+
endif()
135+
136+
# --------------------------
137+
# Install CMake targets file
138+
# --------------------------
139+
140+
install(
141+
TARGETS tensorflow_cc
142+
EXPORT TensorflowCCTargets
143+
)
144+
145+
install(
146+
EXPORT TensorflowCCTargets
147+
FILE TensorflowCCConfig.cmake
148+
DESTINATION "${CMAKECFG_INSTALL_DIR}"
149+
)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
include(ExternalProject)
2+
3+
ExternalProject_Add(
4+
tensorflow_shared
5+
GIT_REPOSITORY http://github.com/tensorflow/tensorflow.git
6+
GIT_TAG v1.2.0-rc0
7+
TMP_DIR "/tmp"
8+
STAMP_DIR "tensorflow-stamp"
9+
DOWNLOAD_DIR "tensorflow"
10+
SOURCE_DIR "tensorflow"
11+
BUILD_IN_SOURCE 1
12+
UPDATE_COMMAND ""
13+
CONFIGURE_COMMAND make -f tensorflow/contrib/makefile/Makefile clean
14+
COMMAND tensorflow/contrib/makefile/download_dependencies.sh
15+
COMMAND tensorflow/contrib/makefile/compile_linux_protobuf.sh
16+
COMMAND cp "${CMAKE_CURRENT_SOURCE_DIR}/cmake/build_tensorflow.sh" .
17+
COMMAND ./build_tensorflow.sh
18+
BUILD_COMMAND ""
19+
INSTALL_COMMAND ""
20+
)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
include(ExternalProject)
2+
3+
ExternalProject_Add(
4+
tensorflow_static
5+
GIT_REPOSITORY http://github.com/tensorflow/tensorflow.git
6+
GIT_TAG v1.2.0-rc0
7+
TMP_DIR "/tmp"
8+
STAMP_DIR "tensorflow-stamp"
9+
DOWNLOAD_DIR "tensorflow"
10+
SOURCE_DIR "tensorflow"
11+
BUILD_IN_SOURCE 1
12+
UPDATE_COMMAND ""
13+
CONFIGURE_COMMAND make -f tensorflow/contrib/makefile/Makefile clean
14+
COMMAND tensorflow/contrib/makefile/download_dependencies.sh
15+
COMMAND tensorflow/contrib/makefile/build_all_linux.sh
16+
BUILD_COMMAND ""
17+
INSTALL_COMMAND ""
18+
)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/sh
2+
set -e
3+
4+
# configure environmental variables
5+
export CC_OPT_FLAGS="-march=native"
6+
export TF_NEED_GCP=0
7+
export TF_NEED_HDFS=0
8+
export TF_NEED_OPENCL=0
9+
export TF_NEED_JEMALLOC=1
10+
export TF_NEED_VERBS=0
11+
export TF_NEED_MKL=0
12+
export TF_ENABLE_XLA=1
13+
export TF_CUDA_CLANG=0
14+
export PYTHON_BIN_PATH="$(which python3)"
15+
export PYTHON_LIB_PATH="$($PYTHON_BIN_PATH -c 'import site; print(site.getsitepackages()[0])')"
16+
17+
# configure cuda environmental variables
18+
if [ -e /opt/cuda ]; then
19+
echo "CUDA support enabled"
20+
cuda_config_opts="--config=cuda"
21+
export TF_NEED_CUDA=1
22+
export TF_CUDA_COMPUTE_CAPABILITIES="3.5,5.2"
23+
export CUDA_TOOLKIT_PATH=/opt/cuda
24+
export CUDNN_INSTALL_PATH=/opt/cuda
25+
export TF_CUDA_VERSION="$($CUDA_TOOLKIT_PATH/bin/nvcc --version | sed -n 's/^.*release \(.*\),.*/\1/p')"
26+
export TF_CUDNN_VERSION="$(sed -n 's/^#define CUDNN_MAJOR\s*\(.*\).*/\1/p' $CUDNN_INSTALL_PATH/include/cudnn.h)"
27+
export GCC_HOST_COMPILER_PATH=/usr/bin/gcc-5
28+
else
29+
echo "CUDA support disabled"
30+
cuda_config_opts=""
31+
export TF_NEED_CUDA=0
32+
fi
33+
34+
# configure and build
35+
./configure
36+
bazel build -c opt $cuda_config_opts --copt=${CC_OPT_FLAGS} tensorflow:libtensorflow_cc.so
37+
bazel shutdown

0 commit comments

Comments
 (0)