Skip to content
This repository was archived by the owner on Jul 10, 2025. It is now read-only.

Commit 22d927e

Browse files
author
Thea Lamkin
authored
Merge pull request #352 from jzhoulon/plugin_tutorial
Add tutorial and sample code for TensorFlow plugin development
2 parents b7b535d + 3274846 commit 22d927e

150 files changed

Lines changed: 21194 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
79.1 KB
Loading
55.5 KB
Loading
24.6 KB
Loading
19.4 KB
Loading
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
build --define=use_fast_cpp_protos=true
2+
build --define=allow_oversize_protos=true
3+
4+
build --spawn_strategy=standalone
5+
# build --strategy=Genrule=standalone
6+
build -c opt
7+
8+
# Default paths for TF_SYSTEM_LIBS
9+
build --define=PREFIX=/usr
10+
build --define=LIBDIR=$(PREFIX)/lib
11+
build --define=INCLUDEDIR=$(PREFIX)/include
12+
13+
# host build is useless
14+
build --distinct_host_configuration=false
15+
16+
try-import %workspace%/.tf_plugin_configure.bazelrc
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# TensorFlow Plugin demo
2+
This sample is a simple demo shows how to implement, build, install and run a TensorFlow plugin.
3+
4+
## Supported OS
5+
* Linux
6+
7+
## Prerequisites
8+
9+
* [Bazel](https://docs.bazel.build/versions/master/install-ubuntu.html) (version 3.1 and above)
10+
* Git (version 1.8 and above)
11+
* Python (version 3.6 and above)
12+
13+
## Build and Run
14+
15+
### Linux
16+
1. Run the following command to install the latest `tensorflow`.
17+
```
18+
$ pip install tensorflow
19+
```
20+
2. In the plug-in `sample` code folder, configure the build options:
21+
```
22+
$ ./configure
23+
24+
Please specify the location of python. [Default is /home/test/miniconda2/envs/sycl3.6/bin/python]:
25+
26+
27+
Found possible Python library paths:
28+
/home/test/miniconda2/envs/sycl3.6/lib/python3.6/site-packages
29+
Please input the desired Python library path to use. Default is [/home/test/miniconda2/envs/sycl3.6/lib/python3.6/site-packages]
30+
31+
Do you wish to build TensorFlow plug-in with MPI support? [y/N]:
32+
No MPI support will be enabled for TensorFlow plug-in.
33+
34+
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native -Wno-sign-compare]:
35+
```
36+
37+
3. Built the plug-in with
38+
```
39+
$ bazel build -c opt //tensorflow_plugin/tools/pip_package:build_pip_package --verbose_failures
40+
```
41+
4. Then generate a python wheel and install it.
42+
```
43+
$ bazel-bin/tensorflow_plugin/tools/pip_package/build_pip_package .
44+
$ pip install tensorflow_plugins-0.0.1-cp36-cp36m-linux_x86_64.whl
45+
```
46+
5. Now we can run the TensorFlow with plug-in device enabled.
47+
```
48+
$ python
49+
>>> import tensorflow as tf
50+
>>> tf.config.list_physical_devices()
51+
[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU'), PhysicalDevice(name='/physical_device:MY_DEVICE:0', device_type='MY_DEVICE')]
52+
```
53+
* Relu case:
54+
```
55+
$ python relu.py
56+
random_normal/RandomStandardNormal: (RandomStandardNormal): /job:localhost/replica:0/task:0/device:CPU:0
57+
2021-10-21 12:48:20.714819: I tensorflow/core/common_runtime/placer.cc:114] random_normal/RandomStandardNormal: (RandomStandardNormal): /job:localhost/replica:0/task:0/device:CPU:0
58+
random_normal/mul: (Mul): /job:localhost/replica:0/task:0/device:CPU:0
59+
2021-10-21 12:48:20.714864: I tensorflow/core/common_runtime/placer.cc:114] random_normal/mul: (Mul): /job:localhost/replica:0/task:0/device:CPU:0
60+
random_normal: (AddV2): /job:localhost/replica:0/task:0/device:CPU:0
61+
2021-10-21 12:48:20.714903: I tensorflow/core/common_runtime/placer.cc:114] random_normal: (AddV2): /job:localhost/replica:0/task:0/device:CPU:0
62+
Relu: (Relu): /job:localhost/replica:0/task:0/device:MY_DEVICE:0
63+
2021-10-21 12:48:20.714937: I tensorflow/core/common_runtime/placer.cc:114] Relu: (Relu): /job:localhost/replica:0/task:0/device:MY_DEVICE:0
64+
random_normal/shape: (Const): /job:localhost/replica:0/task:0/device:CPU:0
65+
2021-10-21 12:48:20.714968: I tensorflow/core/common_runtime/placer.cc:114] random_normal/shape: (Const): /job:localhost/replica:0/task:0/device:CPU:0
66+
random_normal/mean: (Const): /job:localhost/replica:0/task:0/device:CPU:0
67+
2021-10-21 12:48:20.714997: I tensorflow/core/common_runtime/placer.cc:114] random_normal/mean: (Const): /job:localhost/replica:0/task:0/device:CPU:0
68+
random_normal/stddev: (Const): /job:localhost/replica:0/task:0/device:CPU:0
69+
2021-10-21 12:48:20.715022: I tensorflow/core/common_runtime/placer.cc:114] random_normal/stddev: (Const): /job:localhost/replica:0/task:0/device:CPU:0
70+
[2.9109507 0. 0. 0. 0. 0. 0.
71+
0. 0. 1.316411 ]
72+
73+
```
74+
* Conv + Relu case:
75+
```
76+
$ python conv_relu.py
77+
2021-10-21 12:53:36.389514: I tensorflow/core/common_runtime/placer.cc:114] random_normal_3/mul: (Mul): /job:localhost/replica:0/task:0/device:CPU:0
78+
random_normal_3: (AddV2): /job:localhost/replica:0/task:0/device:CPU:0
79+
2021-10-21 12:53:36.389537: I tensorflow/core/common_runtime/placer.cc:114] random_normal_3: (AddV2): /job:localhost/replica:0/task:0/device:CPU:0
80+
Relu: (Relu): /job:localhost/replica:0/task:0/device:MY_DEVICE:0
81+
2021-10-21 12:53:36.389565: I tensorflow/core/common_runtime/placer.cc:114] Relu: (Relu): /job:localhost/replica:0/task:0/device:MY_DEVICE:0
82+
Conv2D: (Conv2D): /job:localhost/replica:0/task:0/device:MY_DEVICE:0
83+
2021-10-21 12:53:36.389592: I tensorflow/core/common_runtime/placer.cc:114] Conv2D: (Conv2D): /job:localhost/replica:0/task:0/device:MY_DEVICE:0
84+
Relu_1: (Relu): /job:localhost/replica:0/task:0/device:CPU:0
85+
2021-10-21 12:53:36.389617: I tensorflow/core/common_runtime/placer.cc:114] Relu_1: (Relu): /job:localhost/replica:0/task:0/device:CPU:0
86+
Conv2D_1: (Conv2D): /job:localhost/replica:0/task:0/device:CPU:0
87+
2021-10-21 12:53:36.389641: I tensorflow/core/common_runtime/placer.cc:114] Conv2D_1: (Conv2D): /job:localhost/replica:0/task:0/device:CPU:0
88+
```
89+
* Profiler case:
90+
```
91+
$python test_profiler.py
92+
```
93+
<div>
94+
<img src=profiler_result.png>
95+
</div>
96+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
workspace(name = "org_tensorflow_plugin")
2+
3+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file")
4+
load("//third_party:version_check.bzl", "check_bazel_version_at_least")
5+
6+
check_bazel_version_at_least("3.1.0")
7+
8+
load("//tensorflow_plugin:tf_configure.bzl", "tf_configure")
9+
10+
tf_configure(name = "local_config_tf")
11+
12+
load("//tensorflow_plugin:workspace.bzl", "clean_dep", "demo_plugin_workspace")
13+
14+
demo_plugin_workspace()
15+
16+
load(
17+
"@bazel_toolchains//repositories:repositories.bzl",
18+
bazel_toolchains_repositories = "repositories",
19+
)
20+
21+
bazel_toolchains_repositories()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
bazel build -c opt //tensorflow_plugin/tools/pip_package:build_pip_package --verbose_failures
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
set -o pipefail
5+
6+
if [ -z "$PYTHON_BIN_PATH" ]; then
7+
PYTHON_BIN_PATH=$(which python || which python3 || true)
8+
fi
9+
10+
# Set all env variables
11+
CONFIGURE_DIR=$(dirname "$0")
12+
"$PYTHON_BIN_PATH" "${CONFIGURE_DIR}/configure.py" "$@"
13+
14+
echo "Configuration finished"
15+

0 commit comments

Comments
 (0)