Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tutorial and sample code for TensorFlow plugin development #352

Merged
merged 30 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
260049d
Add tutorial and sample code for TensorFlow plugin development
jzhoulon Feb 1, 2021
8c292c7
fix conv demo code
jzhoulon Feb 2, 2021
482cbd4
remove useless file
jzhoulon Feb 2, 2021
8aa3d42
remove useless code
jzhoulon Feb 2, 2021
7867d60
modify the format
jzhoulon Feb 2, 2021
cb64115
Fix README format
jzhoulon Feb 2, 2021
99106ff
fix README
jzhoulon Feb 3, 2021
ee29b3c
Fix README
jzhoulon Feb 3, 2021
a5cdabc
add plug-in run example
jzhoulon Feb 4, 2021
73b0c24
fix README format for plug-in
jzhoulon Feb 4, 2021
b3a4cec
Update rfcs/20200624-pluggable-device-for-tensorflow/sample/tensorflo…
jzhoulon May 26, 2021
626bb31
make package install two directory
jzhoulon May 26, 2021
3762265
update graph
jzhoulon May 28, 2021
be5e24b
update graph flow
jzhoulon May 28, 2021
0bd8a73
fix bug
jzhoulon May 28, 2021
d888a7d
add profiler sample code
jzhoulon Oct 21, 2021
40bf8d4
add profiler test case
jzhoulon Oct 21, 2021
8a776a7
update README
jzhoulon Oct 21, 2021
028800a
update README
jzhoulon Oct 21, 2021
a7e30cc
update README
jzhoulon Oct 21, 2021
a835c14
remove useless file
jzhoulon Oct 21, 2021
ea1d520
update image
jzhoulon Oct 21, 2021
fc90602
update tutorial
jzhoulon Oct 22, 2021
cf541dd
update image
jzhoulon Oct 22, 2021
1c78e90
updata profiler tutorial part
jzhoulon Oct 25, 2021
4433d5c
upload XSpace img
jzhoulon Oct 25, 2021
4317611
update tutorial
jzhoulon Oct 25, 2021
fe26181
update profiler
jzhoulon Oct 25, 2021
673cda8
add profiler example
jzhoulon Oct 27, 2021
3274846
Add license for C++ and python files
jzhoulon Oct 17, 2022
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions rfcs/20200624-pluggable-device-for-tensorflow/sample/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
build --define=use_fast_cpp_protos=true
build --define=allow_oversize_protos=true

build --spawn_strategy=standalone
# build --strategy=Genrule=standalone
build -c opt

# Default paths for TF_SYSTEM_LIBS
build --define=PREFIX=/usr
build --define=LIBDIR=$(PREFIX)/lib
build --define=INCLUDEDIR=$(PREFIX)/include

# host build is useless
build --distinct_host_configuration=false

try-import %workspace%/.tf_plugin_configure.bazelrc
96 changes: 96 additions & 0 deletions rfcs/20200624-pluggable-device-for-tensorflow/sample/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# TensorFlow Plugin demo
This sample is a simple demo shows how to implement, build, install and run a TensorFlow plugin.

## Supported OS
* Linux

## Prerequisites

* [Bazel](https://docs.bazel.build/versions/master/install-ubuntu.html) (version 3.1 and above)
* Git (version 1.8 and above)
* Python (version 3.6 and above)

## Build and Run

### Linux
1. Run the following command to install the latest `tensorflow`.
```
$ pip install tensorflow
```
2. In the plug-in `sample` code folder, configure the build options:
```
$ ./configure

Please specify the location of python. [Default is /home/test/miniconda2/envs/sycl3.6/bin/python]:


Found possible Python library paths:
/home/test/miniconda2/envs/sycl3.6/lib/python3.6/site-packages
Please input the desired Python library path to use. Default is [/home/test/miniconda2/envs/sycl3.6/lib/python3.6/site-packages]

Do you wish to build TensorFlow plug-in with MPI support? [y/N]:
No MPI support will be enabled for TensorFlow plug-in.

Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native -Wno-sign-compare]:
```

3. Built the plug-in with
```
$ bazel build -c opt //tensorflow_plugin/tools/pip_package:build_pip_package --verbose_failures
```
4. Then generate a python wheel and install it.
```
$ bazel-bin/tensorflow_plugin/tools/pip_package/build_pip_package .
$ pip install tensorflow_plugins-0.0.1-cp36-cp36m-linux_x86_64.whl
```
5. Now we can run the TensorFlow with plug-in device enabled.
```
$ python
>>> import tensorflow as tf
>>> tf.config.list_physical_devices()
[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU'), PhysicalDevice(name='/physical_device:MY_DEVICE:0', device_type='MY_DEVICE')]
```
* Relu case:
```
$ python relu.py
random_normal/RandomStandardNormal: (RandomStandardNormal): /job:localhost/replica:0/task:0/device:CPU:0
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
random_normal/mul: (Mul): /job:localhost/replica:0/task:0/device:CPU:0
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
random_normal: (AddV2): /job:localhost/replica:0/task:0/device:CPU:0
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
Relu: (Relu): /job:localhost/replica:0/task:0/device:MY_DEVICE:0
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
random_normal/shape: (Const): /job:localhost/replica:0/task:0/device:CPU:0
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
random_normal/mean: (Const): /job:localhost/replica:0/task:0/device:CPU:0
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
random_normal/stddev: (Const): /job:localhost/replica:0/task:0/device:CPU:0
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
[2.9109507 0. 0. 0. 0. 0. 0.
0. 0. 1.316411 ]

```
* Conv + Relu case:
```
$ python conv_relu.py
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
random_normal_3: (AddV2): /job:localhost/replica:0/task:0/device:CPU:0
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
Relu: (Relu): /job:localhost/replica:0/task:0/device:MY_DEVICE:0
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
Conv2D: (Conv2D): /job:localhost/replica:0/task:0/device:MY_DEVICE:0
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
Relu_1: (Relu): /job:localhost/replica:0/task:0/device:CPU:0
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
Conv2D_1: (Conv2D): /job:localhost/replica:0/task:0/device:CPU:0
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
```
* Profiler case:
```
$python test_profiler.py
```
<div>
<img src=profiler_result.png>
</div>

21 changes: 21 additions & 0 deletions rfcs/20200624-pluggable-device-for-tensorflow/sample/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
workspace(name = "org_tensorflow_plugin")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file")
load("//third_party:version_check.bzl", "check_bazel_version_at_least")

check_bazel_version_at_least("3.1.0")

load("//tensorflow_plugin:tf_configure.bzl", "tf_configure")

tf_configure(name = "local_config_tf")

load("//tensorflow_plugin:workspace.bzl", "clean_dep", "demo_plugin_workspace")

demo_plugin_workspace()

load(
"@bazel_toolchains//repositories:repositories.bzl",
bazel_toolchains_repositories = "repositories",
)

bazel_toolchains_repositories()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
bazel build -c opt //tensorflow_plugin/tools/pip_package:build_pip_package --verbose_failures
15 changes: 15 additions & 0 deletions rfcs/20200624-pluggable-device-for-tensorflow/sample/configure
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash

set -e
set -o pipefail

if [ -z "$PYTHON_BIN_PATH" ]; then
PYTHON_BIN_PATH=$(which python || which python3 || true)
fi

# Set all env variables
CONFIGURE_DIR=$(dirname "$0")
"$PYTHON_BIN_PATH" "${CONFIGURE_DIR}/configure.py" "$@"

echo "Configuration finished"

Loading