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-neuronpip whl, a Python package that adds on top oftensorflow.tensorflow_model_server_neuronbinary 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
bazelmkdir -p $HOME/bincurl -L https://github.com/bazelbuild/bazelisk/releases/download/v1.14.0/bazelisk-linux-amd64 --output $HOME/bin/bazelchmod +x $HOME/bin/bazelexport 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
venvand install dependenciespython3 -m venv env_tfnsource ./env_tfn/bin/activatepip install pip -Upip install numpy==1.20.0 wheel sixpip install keras_preprocessing --no-deps
- Clone
tensorflowsource code and setuptensorflow-neurondirectorygit clone https://github.com/tensorflow/tensorflow.git -b v2.8.3 tensorflowgit clone https://github.com/aws/aws-neuron-tensorflow ./tensorflow/tensorflow/neuron
- Build
tensorflow-neuronpip whlcd tensorflow./configurebazel 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-neuronpip whl on aninf1instance with pre-installedaws-neuronx-dkmsandaws-neuronx-runtime-lib- Copy
tensorflow_neuron-*.whlto theinf1instance's$HOMEdirectory, e. g.scp tensorflow_neuron-*.whl my_inf1:~/ - On the
inf1instance:mkdir ~/rundircd ~/rundirpython3 -m venv env_tfnsource ./env_tfn/bin/activatepip install pip -Upip install pytestpip install neuron-cc ~/tensorflow_neuron-*.whl --extra-index-url=https://pip.repos.neuron.amazonaws.compytest --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-servingwhich 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/neuronsed -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/BUILDto letSUPPORTED_TENSORFLOW_OPSinclude 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_serverinstall bazel-bin/tensorflow_serving/model_servers/tensorflow_model_server ./tensorflow_model_server_neuron- Verify by installing
aws-neuronx-runtime-liband runningtensorflow_model_server_neuronecho "deb [trusted=yes] https://apt.repos.neuron.amazonaws.com bionic main" > /etc/apt/sources.list.d/neuron.listapt-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
venvand install dependenciespython3 -m venv env_tfnsource ./env_tfn/bin/activatepip install pip -Upip install numpy==1.18.5 wheel sixpip install keras_preprocessing --no-deps
- Clone
tensorflowsource code and setuptensorflow-neurondirectorygit clone https://github.com/tensorflow/tensorflow.git -b v1.15.5 tensorflowgit clone https://github.com/aws/aws-neuron-tensorflow ./tensorflow/tensorflow/neuron
- Build
tensorflow-neuronpip whlcd tensorflowgit checkout refs/tags/v1.15.5USE_BAZEL_VERSION=0.26.1 ./configureUSE_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-neuronpip whlmkdir ../rundircd ../rundirpip install pytestpip install neuron-cc ../tensorflow/tensorflow_neuron-*.whl --extra-index-url=https://pip.repos.neuron.amazonaws.comenv NEURON_TF_COMPILE_ONLY=1 pytest --pyargs tensorflow_neuron, all tests should pass.- If tests are running on
inf1instances with pre-installedaws-neuronx-dkmsandaws-neuronx-runtime-lib, then you may simply runpytest --pyargs tensorflow_neuronand expect all tests passing. - Known issue: if you have
h5py>=3installed, 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-servingwhich 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/neurongit apply ./tensorflow_serving/neuron/runtime/serving_neuron_op.diff- All this patch does is to register
NeuronOpinto 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/BUILDto letSUPPORTED_TENSORFLOW_OPSinclude Bazel target"//tensorflow_serving/neuron/runtime:all_ops".
- All this patch does is to register
bazel build //tensorflow_serving/model_servers:tensorflow_model_serverinstall bazel-bin/tensorflow_serving/model_servers/tensorflow_model_server ./tensorflow_model_server_neuron- Verify by installing
aws-neuronx-runtime-liband runningtensorflow_model_server_neuronecho "deb [trusted=yes] https://apt.repos.neuron.amazonaws.com bionic main" > /etc/apt/sources.list.d/neuron.listapt-get update && apt-get install -y aws-neuronx-runtime-lib./tensorflow_model_server_neuron --help