serving icon indicating copy to clipboard operation
serving copied to clipboard

Tensorflow (TF) Serving on Multi-GPU box

Open Immexxx opened this issue 9 years ago • 42 comments

The issue is for a single model (not multi-model)

  1. What is the best way to serve requests from TF Serving in a multi-GPU box. TF is able to see all the GPUs but uses only one (Default Inception v3 model - from ~ 03/2016 I believe). (Is there some configuration in TF Serving that I am not aware of.)

Is this dependent on how the model is loaded and exported?

For inference (and not training) - Is there an example of a saved model being loaded onto multiple GPUs (with a single CPU) where the CPU splits the GPU load among multiple GPUs instead of using only one GPU.

  1. If the model is trained on a single GPU machine, when it is being used for inference in TF Serving, can it be used on a multi GPU box where all GPUs are being used?

Immexxx avatar Feb 07 '17 21:02 Immexxx

TF Serving only executes the graph loaded. If you graph is using multiple GPUs TF serving will use them, if you graph is only using one GPU (most common case) then you will be able to run it in one GPU in TF Serving. Probably your best solution is to build a script which loads your graph once per GPU, then uses some code in the CPU to split the batch data into the GPU graphs and finally export the whole graph with support for multiGPU.

I am not aware of other solution as it could be to replicate the same graph in every GPU in TF serving, but I guess it could be possible by modifying the serving code. Although I don't think this will be supported anytime soon and you will have to do it by yourself.

jorgemf avatar Feb 08 '17 13:02 jorgemf

Using CUDA_VISIBLE_DEVICES environment variable you can pin Model Server processes to a specific GPU. Run each of them on a separate port and throw a load balancer in front.

If you are handling any significant load you probably want to run model server with --enable_batching

jlertle avatar Feb 25 '17 03:02 jlertle

@jlertle : Thanks for the comment. But even with that, if it is a 4 GPU box, you would have to run 4 servers - which does not seem like the most efficient way to do it. Also, you have to run this on 4 different ports, which in turn adds routing complexity.

Immexxx avatar Mar 24 '17 21:03 Immexxx

@Immexxx I think that there are not so much resources invested in serving and ecosystem repositories comparing to TF. It is comprensible cause this are a little bit in conflict with selling managed services.

bhack avatar Apr 23 '17 09:04 bhack

Guessing I could close this issue based on the above responses, but will be cool for the system to "intelligently" figure out load - for example, a graph requiring only one GPU - and scale it based on available resources - if multiple GPUs are available, replicate the graph - and serve it.

Could be a flag when launching TF Serving.

In summary, will leave this open for now - in case someone (including me) is looking for an interesting project. In general, TF's handling of GPUs - grabbing all the memory of all GPUs and 'locking' them up but using only a subset for compute does not seem like the most elegant way of doing this. A flag for "auto-replicate and serve" would be nice.

Immexxx avatar Jul 05 '17 18:07 Immexxx

Let's re-open this; With 8GPU machine, it would be really helpful if the sever was simply alternating requests across N available GPUs. For most of us without TPUs, the only way to speed things up is using GPUs. The difference can be 5x faster on GPUs.

vitalyli avatar Oct 17 '17 03:10 vitalyli

+1. This would be incredibly useful. Currently I have a model that I am trying to run on a p2.16xlarge instance but its only utilizing one of the GPUs. It would be incredibly useful if the batch could be split automatically between all the GPUs.

zacharynevin avatar Oct 22 '17 19:10 zacharynevin

In the end I simply used client-side load balancing and had a separate tensorflow serving server for each GPU

zacharynevin avatar Oct 22 '17 20:10 zacharynevin

@zacharynevin . me too. tensorflow serving is a wasting of multi gpus

gawinghe avatar Nov 03 '17 06:11 gawinghe

another +1 here for the exact use case as above. running this on a p2 instance with extra gpus would be an incredibly easy way to scale up. but right now it requires an instance per gpu

mattfeury avatar Jan 18 '18 23:01 mattfeury

A natural way to do this would be to use the Session option visible_device_list to create a session for each GPU and then serve the same model on each GPU.

Unfortunately due to a significant TensorFlow issue (described in https://github.com/tensorflow/tensorflow/issues/8136 and many related bugs), it seems that you cannot have multiple Sessions in the same process with different visible_device_list settings. There doesn't seem to be any traction to getting it fixed as all the related bugs have been closed without any code changes.

deadeyegoodwin avatar Apr 13 '18 22:04 deadeyegoodwin

I placed a comment on one of the many closed tickets referred to by @deadeyegoodwin. The comments on closing those tickets speak as if the functionality must be this way. Yet this works absolutely fine in Tensorflow 1.3!!! We have an app where we call list_local_devices() to get the list of available GPUS. We then create a Session per-GPU, each running the same graph, and use visible_device_list to make each Session see the GPU assigned to it as GPU 0. We have this running in live production code with zero problems. I just started trying to upgrade to Tensorflow 1.8 and ran smack into this problem. So I feel like more explanation is needed beyond saying that this doesn't work. It used to work. It got broken at some point. Please return it to working, or explain why something changed between 1.3 and 1.5 which necessitated this change. I have an idea for a workaround which I am going to try, I will post the workaround if it succeeds.

rundembear avatar May 04 '18 09:05 rundembear

The problem is NOT with list_local_devices!! I ran my code once, got the result we use, which is just the string description. Then I commented out the call to list_local_devices and I still get the same crash with the same error:

F tensorflow/core/common_runtime/gpu/gpu_id_manager.cc:45] Check failed: cuda_gpu_id.value() == result.first->second (1 vs. 0)Mapping the same TfGpuId to a different CUDA GPU id. TfGpuId: 0 Existing mapped CUDA GPU id: 0 CUDA GPU id being tried to map to: 1

I am going to try some earlier versions of Tensorflow to see if I can identify where this went bad.

rundembear avatar May 04 '18 10:05 rundembear

This issue still appears on the latest versions.

I'm using an AWS g3.8xlarge instance which has 2 GPUs.

TF serving is able to detect both GPUs and initialise them but while running the model it only uses 1 GPU to the maximum.

We are on version 1.7, even though the client sends upto 32 requests in parallel, the model server only uses the first GPU (check screenshot from nvidia-smi) 06_09_21

Seeing that this ticket is open since quite some time, Is the external load balancer solution(that @zacharynevin suggested)the only solution at the moment?

@rickragv @cyberwillis

t27 avatar May 29 '18 12:05 t27

Mapping the same TfGpuId to a different CUDA GPU id. TfGpuId: 0 Existing mapped CUDA GPU id: 0 CUDA GPU id being tried to map to: 1

when I use multi GPU in tf1.6, I met the error

qiaohaijun avatar Jun 06 '18 11:06 qiaohaijun

@jlertle @zacharynevin , hi, I would like to ask what commands you were using to run a separate tensorflow serving server for each GPU.

I have a machine with two 1080 Tis. My TF-Serving is able to correctly identify both of the GPUs when the CUDA_VISIBLE_DEVICES flag is not set. And I was trying to run one TF-Serving server per GPU, so I opened two terminals to run the following two commands:

CUDA_VISIBLE_DEVICES=0 bazel-bin/tensorflow_serving/model_servers/tensorflow_model_server --port=9000 --model_name=inception --model_base_path=/path/to/inception_model

CUDA_VISIBLE_DEVICES=1 bazel-bin/tensorflow_serving/model_servers/tensorflow_model_server --port=9001 --model_name=mnist --model_base_path=/path/to/mnist_model

The first command was running fine, but the second one will reach error says

terminate called after throwing an instance of 'std::system_error' what(): Resource temporarily unavailable [1] 4021 abort (core dumped) CUDA_VISIBLE_DEVICES=1 --port=9001 --model_name=mnist

And idea or suggestions? Thanks in advance!

sugartom avatar Jul 28 '18 18:07 sugartom

Hi @sugartom, can you post the output of nvidia-smi?

zacharynevin avatar Jul 28 '18 19:07 zacharynevin

@zacharynevin , thanks for your reply, my output of nvidia-smi (without TF-Serving running) is:

+-----------------------------------------------------------------------------+ | NVIDIA-SMI 390.59 Driver Version: 390.59 | |-------------------------------+----------------------+----------------------+ | GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC | | Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. | |===============================+======================+======================| | 0 GeForce GTX 108... Off | 00000000:01:00.0 On | N/A | | 23% 31C P8 15W / 250W | 334MiB / 11176MiB | 28% Default | +-------------------------------+----------------------+----------------------+ | 1 GeForce GTX 108... Off | 00000000:02:00.0 Off | N/A | | 23% 34C P8 15W / 250W | 2MiB / 11178MiB | 0% Default | +-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+ | Processes: GPU Memory | | GPU PID Type Process name Usage | |=============================================================================| | 0 1089 G /usr/lib/xorg/Xorg 225MiB | | 0 1892 G compiz 103MiB | | 0 5429 G /usr/lib/firefox/firefox 2MiB | +-----------------------------------------------------------------------------+

Please let me know if you need any more information. Thanks!

sugartom avatar Jul 28 '18 20:07 sugartom

@sugartom the commands and output of nvidia-smi look good. I'm not sure what the issue could be at this time

jlertle avatar Jul 28 '18 20:07 jlertle

@jlertle , thanks for your reply. May I ask what's your TF version? I am using v1.2...

sugartom avatar Jul 28 '18 20:07 sugartom