This repository contains the source code of the AWS Neuron TensorFlow integration project. It primarily serves the purpose of demonstrating how to integrate AWS Neuron into an existing, self-maintained fork of TensorFlow. For detailed usage and examples on integrating your TensorFlow based deep learning applications with AWS Neuron, please refer to the AWS Neuron SDK main documentation site.
Here are the steps for building AWS Neuron TensorFlow integration. It is available in the following two forms.
tensorflow-neuron
pip whl, a Python package that adds on top oftensorflow
.tensorflow_model_server_neuron
binary executable, a special build of TensorFlow Serving (tf-serving).
The AWS Neuron runtime is integrated into TensorFlow as a TensorFlow custom operator, namely NeuronOp, without any modification to the core TensorFlow code. As a result, AWS customers may bring in their own fork of TensorFlow, potentially with their own modifications, and expect it to work with AWS Neuron seemlessly together.
The open source distribution of tensorflow-neuron requires deb/rpm package
aws-neuronx-runtime-lib
at run-time. For more information, please refer to
Introducing Packaging and installation changes
and
AWS Neuron Runtime 2.x (libnrt.so).
We recommend Bazelisk which is "a user-friendly launcher for Bazel".
- Install Bazelisk (from [https://github.com/bazelbuild/bazelisk#installation]) and name it as
bazel
mkdir -p $HOME/bin
curl -L https://github.com/bazelbuild/bazelisk/releases/download/v1.14.0/bazelisk-linux-amd64 --output $HOME/bin/bazel
chmod +x $HOME/bin/bazel
export PATH=$PATH:$HOME/bin
- Verify by running
bazel version
- Install Python3 developer package
- On Debian-based OS (e. g., Ubuntu):
sudo apt install python3-dev python3-pip python3-venv
- On AmazonLinux2 or other CentOS-based OS:
sudo yum install python3-devel python3-pip
- On Debian-based OS (e. g., Ubuntu):
- Setup build
venv
and install dependenciespython3 -m venv env_tfn
source ./env_tfn/bin/activate
pip install pip -U
pip install numpy==1.20.0 wheel six
pip install keras_preprocessing --no-deps
- Clone
tensorflow
source code and setuptensorflow-neuron
directorygit clone https://github.com/tensorflow/tensorflow.git -b v2.8.3 tensorflow
git clone https://github.com/aws/aws-neuron-tensorflow ./tensorflow/tensorflow/neuron
- Build
tensorflow-neuron
pip whlcd tensorflow
./configure
bazel build --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0" //tensorflow/neuron:build_pip_package
./bazel-bin/tensorflow/neuron/build_pip_package ./
- pip whl can be found by
ls tensorflow_neuron-*.whl
- (Optional) Validate the
tensorflow-neuron
pip whl on aninf1
instance with pre-installedaws-neuronx-dkms
andaws-neuronx-runtime-lib
- Copy
tensorflow_neuron-*.whl
to theinf1
instance's$HOME
directory, e. g.scp tensorflow_neuron-*.whl my_inf1:~/
- On the
inf1
instance:mkdir ~/rundir
cd ~/rundir
python3 -m venv env_tfn
source ./env_tfn/bin/activate
pip install pip -U
pip install pytest
pip install neuron-cc ~/tensorflow_neuron-*.whl --extra-index-url=https://pip.repos.neuron.amazonaws.com
pytest --pyargs tensorflow_neuron -k 'custom or dot or batchnorm'
, all tests should pass.
- Copy
We recommend building and running tensorflow_model_server_neuron
on docker image
tensorflow/serving:2.8.3-devel
which includes the source code of
tf-serving 2.8.3 and its entire build dependency environment. To install docker, please refer to
https://docs.docker.com/engine/install/.
docker run -it --rm -v $(pwd):/host_workspace tensorflow/serving:2.8.3-devel bash
- This step should let you drop into
/tensorflow-serving
which has the same content as https://github.com/tensorflow/serving/tree/2.8.3.
- This step should let you drop into
git clone https://github.com/aws/aws-neuron-tensorflow ./tensorflow_serving/neuron
sed -i 's/SUPPORTED_TENSORFLOW_OPS = /SUPPORTED_TENSORFLOW_OPS = ["\/\/tensorflow_serving\/neuron\/runtime:all_ops"] + /g' ./tensorflow_serving/model_servers/BUILD
- If the sed command fails to execute, you may choose to manually edit
tensorflow_serving/model_servers/BUILD
to letSUPPORTED_TENSORFLOW_OPS
include Bazel target"//tensorflow_serving/neuron/runtime:all_ops"
.
- If the sed command fails to execute, you may choose to manually edit
bazel build //tensorflow_serving/model_servers:tensorflow_model_server
install bazel-bin/tensorflow_serving/model_servers/tensorflow_model_server ./tensorflow_model_server_neuron
- Verify by installing
aws-neuronx-runtime-lib
and runningtensorflow_model_server_neuron
echo "deb [trusted=yes] https://apt.repos.neuron.amazonaws.com bionic main" > /etc/apt/sources.list.d/neuron.list
apt-get update && apt-get install -y aws-neuronx-runtime-lib
./tensorflow_model_server_neuron --help
- Install Python3 developer package
- On Debian-based OS (e. g., Ubuntu):
sudo apt install python3-dev python3-pip python3-venv
- On AmazonLinux2 or other CentOS-based OS:
sudo yum install python3-devel python3-pip
- On Debian-based OS (e. g., Ubuntu):
- Setup build
venv
and install dependenciespython3 -m venv env_tfn
source ./env_tfn/bin/activate
pip install pip -U
pip install numpy==1.18.5 wheel six
pip install keras_preprocessing --no-deps
- Clone
tensorflow
source code and setuptensorflow-neuron
directorygit clone https://github.com/tensorflow/tensorflow.git -b v1.15.5 tensorflow
git clone https://github.com/aws/aws-neuron-tensorflow ./tensorflow/tensorflow/neuron
- Build
tensorflow-neuron
pip whlcd tensorflow
git checkout refs/tags/v1.15.5
USE_BAZEL_VERSION=0.26.1 ./configure
USE_BAZEL_VERSION=0.26.1 bazel build --incompatible_remap_main_repo --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0" //tensorflow/neuron:build_pip_package
./bazel-bin/tensorflow/neuron/build_pip_package ./
- pip whl can be found by
ls tensorflow_neuron-*.whl
- (Optional) Validate the
tensorflow-neuron
pip whlmkdir ../rundir
cd ../rundir
pip install pytest
pip install neuron-cc ../tensorflow/tensorflow_neuron-*.whl --extra-index-url=https://pip.repos.neuron.amazonaws.com
env NEURON_TF_COMPILE_ONLY=1 pytest --pyargs tensorflow_neuron
, all tests should pass.- If tests are running on
inf1
instances with pre-installedaws-neuronx-dkms
andaws-neuronx-runtime-lib
, then you may simply runpytest --pyargs tensorflow_neuron
and expect all tests passing. - Known issue: if you have
h5py>=3
installed, some Keras related tests may fail due to tensorflow/tensorflow#44467
- If tests are running on
We recommend building and running tensorflow_model_server_neuron
on docker image
tensorflow/serving:1.15.0-devel
which includes the source code of
tf-serving 1.15.0 and its entire build dependency environment. To install docker, please refer to
https://docs.docker.com/engine/install/.
docker run -it --rm -v $(pwd):/host_workspace tensorflow/serving:1.15.0-devel bash
- This step should let you drop into
/tensorflow-serving
which has the same content as https://github.com/tensorflow/serving/tree/1.15.0.
- This step should let you drop into
git clone https://github.com/aws/aws-neuron-tensorflow ./tensorflow_serving/neuron
git apply ./tensorflow_serving/neuron/runtime/serving_neuron_op.diff
- All this patch does is to register
NeuronOp
into tf-serving by adding the following line of code into Bazel BUILD filetensorflow_serving/model_servers/BUILD
.
SUPPORTED_TENSORFLOW_OPS.append("//tensorflow_serving/neuron/runtime:all_ops")
- If the patch fails to apply due to file content conflicts, you may choose to manually edit
tensorflow_serving/model_servers/BUILD
to letSUPPORTED_TENSORFLOW_OPS
include Bazel target"//tensorflow_serving/neuron/runtime:all_ops"
.
- All this patch does is to register
bazel build //tensorflow_serving/model_servers:tensorflow_model_server
install bazel-bin/tensorflow_serving/model_servers/tensorflow_model_server ./tensorflow_model_server_neuron
- Verify by installing
aws-neuronx-runtime-lib
and runningtensorflow_model_server_neuron
echo "deb [trusted=yes] https://apt.repos.neuron.amazonaws.com bionic main" > /etc/apt/sources.list.d/neuron.list
apt-get update && apt-get install -y aws-neuronx-runtime-lib
./tensorflow_model_server_neuron --help