Skip to content

Commit 9228bce

Browse files
committed
[Feat] Add tf-serving config shell script
1 parent e84368d commit 9228bce

File tree

7 files changed

+147
-36
lines changed

7 files changed

+147
-36
lines changed

README.md

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -278,41 +278,56 @@ de = tfra.dynamic_embedding.get_variable("VariableOnGpu",
278278
sess_config.gpu_options.allow_growth = True
279279
```
280280

281-
## Inference with TensorFlow Serving
281+
## Inference
282+
283+
### With TensorFlow Serving
282284

283285
#### Compatibility Matrix
284-
| TFRA | TensorFlow | Serving | Compiler | CUDA | CUDNN | Compute Capability |
285-
|:------|:-----------|:---- |:---------| :------------ | :---- | :------------ |
286-
| 0.6.0 | 2.8.3 | 2.5.2 | GCC 7.3.1 | 11.2| 8.1 | 6.0, 6.1, 7.0, 7.5, 8.0, 8.6 |
287-
| 0.5.1 | 2.8.3 | 2.5.2 | GCC 7.3.1 | 11.2| 8.1 | 6.0, 6.1, 7.0, 7.5, 8.0, 8.6 |
288-
| 0.5.0 | 2.8.3 | 2.5.2 | GCC 7.3.1 | 11.2| 8.1 | 6.0, 6.1, 7.0, 7.5, 8.0, 8.6 |
289-
| 0.4.0 | 2.5.1 | 2.5.2 | GCC 7.3.1 | 11.2| 8.1 | 6.0, 6.1, 7.0, 7.5, 8.0, 8.6 |
290-
| 0.3.1 | 2.5.1 | 2.5.2 | GCC 7.3.1 | 11.2| 8.1 | 6.0, 6.1, 7.0, 7.5, 8.0, 8.6 |
291-
| 0.2.0 | 2.4.1 | 2.4.0 | GCC 7.3.1 | 11.0 | 8.0 | 6.0, 6.1, 7.0, 7.5, 8.0 |
292-
| 0.2.0 | 1.15.2 | 1.15.0 | GCC 7.3.1 | 10.0 | 7.6 | 6.0, 6.1, 7.0, 7.5 |
293-
| 0.1.0 | 2.4.1 | 2.4.0 | GCC 7.3.1 | - | - | - |
294-
295-
**NOTICE**:Reference documents: https://www.tensorflow.org/tfx/serving/custom_op
296-
297-
#### CPU or GPU Serving TensorFlow models with custom ops
298-
When compiling, set the environment variable:
286+
| TFRA | TensorFlow | Serving branch | Compiler | CUDA | CUDNN | Compute Capability |
287+
|:------|:-----------|:---------------|:---------| :------------ | :---- | :------------ |
288+
| 0.6.0 | 2.8.3 | r2.8 | GCC 7.3.1 | 11.2| 8.1 | 6.0, 6.1, 7.0, 7.5, 8.0, 8.6 |
289+
| 0.5.1 | 2.8.3 | r2.8 | GCC 7.3.1 | 11.2| 8.1 | 6.0, 6.1, 7.0, 7.5, 8.0, 8.6 |
290+
| 0.5.0 | 2.8.3 | r2.8 | GCC 7.3.1 | 11.2| 8.1 | 6.0, 6.1, 7.0, 7.5, 8.0, 8.6 |
291+
| 0.4.0 | 2.5.1 | r2.5 | GCC 7.3.1 | 11.2| 8.1 | 6.0, 6.1, 7.0, 7.5, 8.0, 8.6 |
292+
| 0.3.1 | 2.5.1 | r2.5 | GCC 7.3.1 | 11.2| 8.1 | 6.0, 6.1, 7.0, 7.5, 8.0, 8.6 |
293+
| 0.2.0 | 2.4.1 | r2.4 | GCC 7.3.1 | 11.0 | 8.0 | 6.0, 6.1, 7.0, 7.5, 8.0 |
294+
| 0.2.0 | 1.15.2 | r1.15 | GCC 7.3.1 | 10.0 | 7.6 | 6.0, 6.1, 7.0, 7.5 |
295+
| 0.1.0 | 2.4.1 | r2.4 | GCC 7.3.1 | - | - | - |
296+
297+
Serving TFRA-enable models by custom ops in TensorFlow Serving.
298+
299299
```sh
300-
export FOR_TF_SERVING = "1"
301-
```
302-
Tensorflow Serving modification(**model_servers/BUILD**):
303-
```
304-
SUPPORTED_TENSORFLOW_OPS = if_v2([]) + if_not_v2([
305-
"@org_tensorflow//tensorflow/contrib:contrib_kernels",
306-
"@org_tensorflow//tensorflow/contrib:contrib_ops_op_lib",
307-
]) + [
308-
"@org_tensorflow_text//tensorflow_text:ops_lib",
309-
"//tensorflow_recommenders_addons/dynamic_embedding/core:_cuckoo_hashtable_ops.so",
310-
"//tensorflow_recommenders_addons/dynamic_embedding/core:_math_ops.so",
311-
]
300+
## If enable GPU OPs
301+
export SERVING_WITH_GPU=1
302+
303+
## Specifiy the branch of TFRA
304+
export TFRA_BRANCH="master" # The `master` and `r0.6` are available.
305+
306+
## Create workspace, modify the directory as you prefer to.
307+
export TFRA_SERVING_WORKSPACE=~/tfra_serving_workspace/
308+
mkdir -p $TFRA_SERVING_WORKSPACE && cd $TFRA_SERVING_WORKSPACE
309+
310+
## Clone the release branches of serving and TFRA according to `Compatibility Matrix`.
311+
git clone -b r2.8 https://github.com/tensorflow/serving.git
312+
git clone -b $TFRA_BRANCH https://github.com/tensorflow/recommenders-addons.git
313+
314+
## Run config shell script
315+
cd $TFRA_SERVING_WORKSPACE/recommenders-addons/tools
316+
bash config_tfserving.sh $TFRA_BRANCH $TFRA_SERVING_WORKSPACE/serving $SERVING_WITH_GPU
317+
318+
## Build serving with TFRA OPs.
319+
cd $TFRA_SERVING_WORKSPACE/serving
320+
./tools/run_in_docker.sh bazel build tensorflow_serving/model_servers:tensorflow_model_server
321+
312322
```
323+
324+
For more detail, please refer to the shell script `./tools/config_tfserving.sh`.
325+
313326
**NOTICE**
314327
- Distributed inference is only supported when using Redis as Key-Value storage.
328+
- Reference documents: https://www.tensorflow.org/tfx/serving/custom_op
315329

330+
### With Triton(W.I.P)
316331

317332
## Community
318333

tensorflow_recommenders_addons/dynamic_embedding/core/ops/redis_table_ops.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ Status ScalarAndTwoElementVectorInputsAndScalarOutputs(InferenceContext *c) {
4444

4545
} // namespace
4646

47-
Status ValidateTableResourceHandle(InferenceContext *c, ShapeHandle keys,
48-
const string &key_dtype_attr,
49-
const string &value_dtype_attr,
50-
bool is_lookup,
51-
ShapeAndType *output_shape_and_type) {
47+
Status ValidateTableResourceHandleRedis(InferenceContext *c, ShapeHandle keys,
48+
const string &key_dtype_attr,
49+
const string &value_dtype_attr,
50+
bool is_lookup,
51+
ShapeAndType *output_shape_and_type) {
5252
auto *handle_data = c->input_handle_shapes_and_types(0);
5353
if (handle_data == nullptr || handle_data->size() != 2) {
5454
output_shape_and_type->shape = c->UnknownShape();
@@ -124,7 +124,7 @@ REGISTER_OP(PREFIX_OP_NAME(RedisTableFind))
124124
TF_RETURN_IF_ERROR(c->WithRank(c->input(0), 0, &handle));
125125

126126
ShapeAndType value_shape_and_type;
127-
TF_RETURN_IF_ERROR(ValidateTableResourceHandle(
127+
TF_RETURN_IF_ERROR(ValidateTableResourceHandleRedis(
128128
c,
129129
/*keys=*/c->input(1),
130130
/*key_dtype_attr=*/"Tin",
@@ -149,7 +149,7 @@ REGISTER_OP(PREFIX_OP_NAME(RedisTableFindWithExists))
149149

150150
ShapeHandle keys = c->UnknownShapeOfRank(1);
151151
ShapeAndType value_shape_and_type;
152-
TF_RETURN_IF_ERROR(ValidateTableResourceHandle(
152+
TF_RETURN_IF_ERROR(ValidateTableResourceHandleRedis(
153153
c,
154154
/*keys=*/c->input(1),
155155
/*key_dtype_attr=*/"Tin",
@@ -224,7 +224,7 @@ REGISTER_OP(PREFIX_OP_NAME(RedisTableExport))
224224
TF_RETURN_IF_ERROR(c->WithRank(c->input(0), 0, &handle));
225225
ShapeHandle keys = c->UnknownShapeOfRank(1);
226226
ShapeAndType value_shape_and_type;
227-
TF_RETURN_IF_ERROR(ValidateTableResourceHandle(
227+
TF_RETURN_IF_ERROR(ValidateTableResourceHandleRedis(
228228
c,
229229
/*keys=*/keys,
230230
/*key_dtype_attr=*/"Tkeys",

tools/config_tfserving.sh

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env bash
2+
3+
# Config the serving for build serving with TFRA OPs.
4+
# Usage:
5+
# Param $1 is the branch name of TFRA
6+
# Param $2 is the serving root directory.
7+
# Param $3 is the flag indicating if enable CUDA for GPU.
8+
# ```shell
9+
# cd /recommenders_addons/tools
10+
# bash ./config_tfserving.sh "r0.6" /root/of/serving/ "1"
11+
# cd /root/of/serving/
12+
# ./tools/run_in_docker.sh bazel build tensorflow_serving/model_servers:tensorflow_model_server
13+
# ```
14+
15+
tfra_branch=$1
16+
tfserving_root=$2
17+
is_gpu=$3
18+
19+
# Pleas modify this mapping when update TFRA.
20+
declare -A serving_version
21+
serving_version["master"]="2.8.3"
22+
23+
# 1. copy directory to tfserving root
24+
cp -r ../tensorflow_recommenders_addons ${tfserving_root}/
25+
cp -r ../build_deps ${tfserving_root}/
26+
27+
# 2. Padding the WORKSPACE
28+
cat "../WORKSPACE"| tail -n +2 >> ${tfserving_root}/WORKSPACE
29+
30+
# 3. Padding the tools/run_in_docker.sh
31+
file="${tfserving_root}/tools/run_in_docker.sh"
32+
original_docker="tensorflow\/serving:nightly-devel"
33+
replacement_docker="tfra\/serving:${serving_version[$tfra_branch]}-devel"
34+
if [[ "$is_gpu" == "1" ]]; then
35+
replacement_docker="tfra\/serving:${serving_version[$tfra_branch]}-devel-gpu"
36+
fi
37+
38+
sed -i "s/$original_docker/$replacement_docker/g" $file
39+
40+
# 4. Padding .bazelrc
41+
file=${tfserving_root}/.bazelrc
42+
cat "serving_padding/.bazelrc_padding" >> $file
43+
44+
if [[ "$is_gpu" == "1" ]]; then
45+
cat "serving_padding/.bazelrc_gpu_padding" >> $file
46+
fi
47+
48+
# 5. Padding tensorflow_serving/model_servers/BUILD
49+
file=${tfserving_root}/tensorflow_serving/model_servers/BUILD
50+
target_string="org_tensorflow_text\/\/tensorflow_text:ops_lib"
51+
52+
sed -i "/$target_string/a \\
53+
\"\/\/tensorflow_recommenders_addons\/dynamic_embedding\/core:_cuckoo_hashtable_ops.so\", \\
54+
\"\/\/tensorflow_recommenders_addons\/dynamic_embedding\/core:_math_ops.so\", \\
55+
\"\/\/tensorflow_recommenders_addons\/dynamic_embedding\/core:_redis_table_ops.so\",
56+
" $file
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Dockerfile to build a serving with TFRA OPs.
2+
#
3+
# To push a new version, run:
4+
# $ docker build -f serving-devel.Dockerfile . --tag "tfra/serving:2.8.3-devel-gpu"
5+
# $ docker push tfra/serving:2.8.3-devel-gpu
6+
7+
FROM tensorflow/serving:2.6.3-devel-gpu
8+
9+
RUN pip install --upgrade pip && pip install tensorflow==2.8.3
10+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Dockerfile to build a serving with TFRA OPs.
2+
#
3+
# To push a new version, run:
4+
# $ docker build -f serving-devel.Dockerfile . --tag "tfra/serving:2.8.3-devel"
5+
# $ docker push tfra/serving:2.8.3-devel
6+
7+
FROM tensorflow/serving:2.6.3-devel
8+
9+
RUN pip install --upgrade pip && pip install tensorflow==2.8.3
10+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
build --action_env TF_NEED_CUDA="1"
2+
build --action_env CUDA_TOOLKIT_PATH="/usr/local/cuda"
3+
build --action_env CUDNN_INSTALL_PATH="/usr/lib/x86_64-linux-gnu"
4+
build --action_env TF_CUDA_VERSION="11.2"
5+
build --action_env TF_CUDNN_VERSION="8.1"
6+
test --config=cuda
7+
build --config=cuda
8+
build:cuda --define=using_cuda=true --define=using_cuda_nvcc=true
9+
build:cuda --crosstool_top=@local_config_cuda//crosstool:toolchain
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
build --action_env TF_HEADER_DIR="/usr/local/lib/python3.7/dist-packages/tensorflow/include"
2+
build --action_env TF_SHARED_LIBRARY_DIR="/usr/local/lib/python3.7/dist-packages/tensorflow"
3+
build --action_env TF_SHARED_LIBRARY_NAME="libtensorflow_framework.so.2"
4+
build --action_env TF_CXX11_ABI_FLAG="0"
5+
build --action_env TF_CXX_STANDARD="c++14"
6+
build --action_env TF_VERSION_INTEGER="2083"
7+
build --action_env FOR_TF_SERVING="1"
8+
build --spawn_strategy=standalone
9+
build --strategy=Genrule=standalone
10+
build -c opt
11+
build --copt=-mavx

0 commit comments

Comments
 (0)