serve icon indicating copy to clipboard operation
serve copied to clipboard

Waveglow example does not work on multiple GPUs.

Open harshbafna opened this issue 6 years ago • 10 comments

Machine : p3.8xlarge

AMI : DLAMI 27.0

When we start torchserve with wavaglow model as init model, by default 4 workers are registered, one on each GPU

torchserve --start --model-store model_store --models waveglow_synthesizer.mar

On running multiple inference on waveglow model, 75% of requests fail with following exceptions for 3 out of 4 requests.:

2020-03-18 06:20:13,671 [INFO ] W-9000-waveglow_synthesizer_1.0-stdout org.pytorch.serve.wlm.WorkerLifeCycle - Invoking custom service failed.
2020-03-18 06:20:13,672 [INFO ] W-9000-waveglow_synthesizer_1.0 org.pytorch.serve.wlm.WorkerThread - Backend response time: 16
2020-03-18 06:20:13,672 [INFO ] W-9000-waveglow_synthesizer_1.0-stdout org.pytorch.serve.wlm.WorkerLifeCycle - Traceback (most recent call last):
2020-03-18 06:20:13,672 [INFO ] W-9000-waveglow_synthesizer_1.0-stdout org.pytorch.serve.wlm.WorkerLifeCycle -   File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/ts/service.py", line 100, in predict
2020-03-18 06:20:13,672 [INFO ] W-9000-waveglow_synthesizer_1.0-stdout org.pytorch.serve.wlm.WorkerLifeCycle -     ret = self._entry_point(input_batch, self.context)
2020-03-18 06:20:13,672 [INFO ] W-9000-waveglow_synthesizer_1.0-stdout org.pytorch.serve.wlm.WorkerLifeCycle -   File "/tmp/models/4b62062c7ac0b98f2fdecac41061d09d2039472a/waveglow_handler.py", line 98, in handle
2020-03-18 06:20:13,672 [INFO ] W-9000-waveglow_synthesizer_1.0-stdout org.pytorch.serve.wlm.WorkerLifeCycle -     data = _service.inference(data)
2020-03-18 06:20:13,672 [INFO ] W-9000-waveglow_synthesizer_1.0-stdout org.pytorch.serve.wlm.WorkerLifeCycle -   File "/tmp/models/4b62062c7ac0b98f2fdecac41061d09d2039472a/waveglow_handler.py", line 76, in inference
2020-03-18 06:20:13,672 [INFO ] W-9000-waveglow_synthesizer_1.0-stdout org.pytorch.serve.wlm.WorkerLifeCycle -     _, mel, _, _ = self.tacotron2_model.infer(data)
2020-03-18 06:20:13,672 [INFO ] W-9000-waveglow_synthesizer_1.0-stdout org.pytorch.serve.wlm.WorkerLifeCycle -   File "/home/ubuntu/.cache/torch/hub/nvidia_DeepLearningExamples_torchhub/PyTorch/SpeechSynthesis/Tacotron2/tacotron2/model.py", line 651, in infer
2020-03-18 06:20:13,672 [INFO ] W-9000-waveglow_synthesizer_1.0-stdout org.pytorch.serve.wlm.WorkerLifeCycle -     encoder_outputs)
2020-03-18 06:20:13,672 [INFO ] W-9000-waveglow_synthesizer_1.0-stdout org.pytorch.serve.wlm.WorkerLifeCycle -   File "/home/ubuntu/.cache/torch/hub/nvidia_DeepLearningExamples_torchhub/PyTorch/SpeechSynthesis/Tacotron2/tacotron2/model.py", line 546, in inference
2020-03-18 06:20:13,673 [INFO ] W-9000-waveglow_synthesizer_1.0-stdout org.pytorch.serve.wlm.WorkerLifeCycle -     not_finished = not_finished*dec
2020-03-18 06:20:13,673 [INFO ] W-9000-waveglow_synthesizer_1.0-stdout org.pytorch.serve.wlm.WorkerLifeCycle - RuntimeError: expected device cuda:0 but got device cuda:1

The inference succeeds only for worker running on cuda:0.

Need to fix the custom handler in the waveglow example.

harshbafna avatar Mar 18 '20 05:03 harshbafna

On further investigation we observed that executing the waveglow example available on pytorch blog also fails if we change the device to cuda:1

import torch
import numpy as np
from scipy.io.wavfile import write

waveglow = torch.hub.load('nvidia/DeepLearningExamples:torchhub', 'nvidia_waveglow')

waveglow = waveglow.remove_weightnorm(waveglow)
waveglow = waveglow.to('cuda:1')
waveglow.eval()

tacotron2 = torch.hub.load('nvidia/DeepLearningExamples:torchhub', 'nvidia_tacotron2')
tacotron2 = tacotron2.to('cuda:1')
tacotron2.eval()

text = "hello world, I missed you"

sequence = np.array(tacotron2.text_to_sequence(text, ['english_cleaners']))[None, :]
sequence = torch.from_numpy(sequence).to(device='cuda:1', dtype=torch.int64)

# run the models
_, mel, _, _ = tacotron2.infer(sequence)
audio = waveglow.infer(mel)
audio_numpy = audio[0].data.cpu().numpy()
rate = 22050

write("audio.wav", rate, audio_numpy)

harshbafna avatar Mar 18 '20 07:03 harshbafna

On exploring tacotron2 model's source code we observed that the tensors are loaded into default cuda device

not_finished = torch.ones([memory.size(0)], dtype=torch.int32).cuda()

and fails when operated with the data loaded in other cuda device like cuda:1

not_finished = not_finished*dec

with exception :

RuntimeError: expected device cuda:0 but got device cuda:1

harshbafna avatar Mar 18 '20 10:03 harshbafna

Created following topic on PyTorch forum :

https://discuss.pytorch.org/t/error-while-running-the-pre-trained-waveglow-model-on-specific-gpu-device/75804

Created following bug on Nvidia DeepLearningExamples repo :

https://github.com/NVIDIA/DeepLearningExamples/issues/450

harshbafna avatar Apr 08 '20 11:04 harshbafna

Hi, @harshbafna I ran into this issue as well, my Crowd detection models are also failing I'm using this stmt in my custom handler script:

self.device = torch.device("cuda:" + str(properties.get("gpu_id")) if torch.cuda.is_available() else "cpu")

Error:

2020-05-28 15:18:26,308 [INFO ] W-9001-ssdcnet_shb_gpu_2_1.0-stdout org.pytorch.serve.wlm.WorkerLifeCycle -   File "/home/ubuntu/anaconda3/envs/torchserve_gpu/lib/python3.8/site-packages/ts/service.py", line 100, in predict
2020-05-28 15:18:26,308 [INFO ] W-9001-ssdcnet_shb_gpu_2_1.0-stdout org.pytorch.serve.wlm.WorkerLifeCycle -     ret = self._entry_point(input_batch, self.context)
2020-05-28 15:18:26,308 [INFO ] W-9001-ssdcnet_shb_gpu_2_1.0-stdout org.pytorch.serve.wlm.WorkerLifeCycle -   File "/tmp/models/8ddd68bf5d55746df30deaee810a151ddca4e510/crdcnt_handler_gpu.py", line 171, in handle
2020-05-28 15:18:26,308 [INFO ] W-9001-ssdcnet_shb_gpu_2_1.0-stdout org.pytorch.serve.wlm.WorkerLifeCycle -     data = _service.inference(data)
2020-05-28 15:18:26,308 [INFO ] W-9001-ssdcnet_shb_gpu_2_1.0 ACCESS_LOG - /127.0.0.1:40444 "POST /predictions/ssdcnet_shb_gpu_2 HTTP/1.1" 503 5446
2020-05-28 15:18:26,308 [INFO ] W-9001-ssdcnet_shb_gpu_2_1.0-stdout org.pytorch.serve.wlm.WorkerLifeCycle -   File "/tmp/models/8ddd68bf5d55746df30deaee810a151ddca4e510/crdcnt_handler_gpu.py", line 130, in inference
2020-05-28 15:18:26,308 [INFO ] W-9001-ssdcnet_shb_gpu_2_1.0 TS_METRICS - Requests5XX.Count:1|#Level:Host|#hostname:ip-172-31-15-199,timestamp:null
2020-05-28 15:18:26,308 [INFO ] W-9001-ssdcnet_shb_gpu_2_1.0-stdout org.pytorch.serve.wlm.WorkerLifeCycle -     merge_res = self.model.parse_merge(div_res)
2020-05-28 15:18:26,308 [INFO ] W-9001-ssdcnet_shb_gpu_2_1.0-stdout org.pytorch.serve.wlm.WorkerLifeCycle -   File "/home/ubuntu/mayub/Github/SSDCNet/Network/SSDCNet.py", line 284, in parse_merge
2020-05-28 15:18:26,308 [INFO ] W-9001-ssdcnet_shb_gpu_2_1.0-stdout org.pytorch.serve.wlm.WorkerLifeCycle -     res[tname] = (1-tw)*tclow + tw*tchigh
2020-05-28 15:18:26,308 [INFO ] W-9001-ssdcnet_shb_gpu_2_1.0-stdout org.pytorch.serve.wlm.WorkerLifeCycle - RuntimeError: expected device cuda:2 but got device cuda:0
2020-05-28 15:18:26,308 [DEBUG] W-9001-ssdcnet_shb_gpu_2_1.0 org.pytorch.serve.wlm.Job - Waiting time: 5081, Inference time: 5446

It runs fine on single GPU machine. Is there a way to automatically scale to number of GPU's.

Thanks !

mohammedayub44 avatar May 28 '20 15:05 mohammedayub44

@mohammedayub44 This issue has been fixed in latest Tacotron2 as per following issue [reported by @harshbafna ] on Nvidia forum https://github.com/NVIDIA/DeepLearningExamples/issues/450

codinnvrends avatar May 28 '20 17:05 codinnvrends

@dhaniram-kshirsagar what exactly was the fix. Do we need to change anything in the handler ?

Thanks !

mohammedayub44 avatar May 28 '20 23:05 mohammedayub44

@mohammedayub44 : The issue was with the tacotron2 model it self where the tensors were loaded into default cuda device only and when we tried to load the model in a different cuda device (other than default) it failed. I would recommend to check your model as well as handler and ensure that you are loading all the tensors on the same device as your model.

harshbafna avatar May 29 '20 05:05 harshbafna

@harshbafna I was actually trying to run on 4 GPU's (p3.8xlarge instance). I'm able to run on single GPU id without any problem, I run into this error when running on 4 GPU's, is that not supported out of box by Torchserve.

mohammedayub44 avatar Jun 01 '20 04:06 mohammedayub44

@mohammedayub44 : Running on multiple GPUs is supported in TorchServe. Every workers is started on different GPU devices in a round-robin fashion, which is taken care by following piece of code in the handler :

self.device = torch.device("cuda:" + str(properties.get("gpu_id")) if torch.cuda.is_available() else "cpu")

From the stack trace shared, the following line your model seems to be the culprit, where atleast one of the tensor is not loaded in the same device as model.

res[tname] = (1-tw)*tclow + tw*tchigh

In case you have further queries on this, please create a new ticket using this template

harshbafna avatar Jun 01 '20 05:06 harshbafna

Thanks so much. I'll try debugging why this is happening. Will raise separate ticket if I don't figure out.

mohammedayub44 avatar Jun 02 '20 14:06 mohammedayub44