serve icon indicating copy to clipboard operation
serve copied to clipboard

model run fails with CUDA version error - b/c server is CPU only vs trained on GPU...dynamically adjust map_location?

Open lessw2020 opened this issue 6 years ago • 6 comments

I trained a new model on GPU and exported it as JIT script.
When I went to run it on our server, it failed out with complaints about insufficient CUDA drivers...which is b/c this server is CPU only.

Is there a way to make this scenario better handled -i.e. can Serve check the devices available and then pass the appropriate device map_location argument to avoid this?

The hard coded fix is for me to move the model over to CPU, then export but we may have a scenario of some deployments of same model running on CPU and some on GPU and having to deal with that seems something better dynamically handled by Serve? (Otherwise I have to maintain two .mar versions).

Here's the log errors for reference: 2020-05-27 19:21:06,576 [DEBUG] main org.pytorch.serve.wlm.ModelVersionedRefs - Adding new version 1.527 for model nest101 2020-05-27 19:21:06,576 [DEBUG] main org.pytorch.serve.wlm.ModelVersionedRefs - Setting default version to 1.527 for model nest101 2020-05-27 19:21:06,576 [INFO ] main org.pytorch.serve.wlm.ModelManager - Model nest101 loaded. 2020-05-27 19:21:06,576 [DEBUG] main org.pytorch.serve.wlm.ModelManager - updateModel: nest101, count: 2 2020-05-27 19:21:06,582 [INFO ] main org.pytorch.serve.ModelServer - Initialize Inference server with: EpollServerSocketChannel. 2020-05-27 19:21:06,633 [INFO ] main org.pytorch.serve.ModelServer - Inference API bind to: http://127.0.0.1:2000 2020-05-27 19:21:06,633 [INFO ] main org.pytorch.serve.ModelServer - Initialize Management server with: EpollServerSocketChannel. 2020-05-27 19:21:06,641 [INFO ] main org.pytorch.serve.ModelServer - Management API bind to: http://127.0.0.1:3000 2020-05-27 19:21:06,683 [INFO ] W-9002-nest101_1.527-stdout org.pytorch.serve.wlm.WorkerLifeCycle - Listening on port: /tmp/.ts.sock.9002 2020-05-27 19:21:06,684 [INFO ] W-9002-nest101_1.527-stdout org.pytorch.serve.wlm.WorkerLifeCycle - [PID]3474 2020-05-27 19:21:06,685 [INFO ] W-9002-nest101_1.527-stdout org.pytorch.serve.wlm.WorkerLifeCycle - Torch worker started. 2020-05-27 19:21:06,685 [DEBUG] W-9002-nest101_1.527 org.pytorch.serve.wlm.WorkerThread - W-9002-nest101_1.527 State change null -> WORKER_STARTED 2020-05-27 19:21:06,688 [INFO ] W-9002-nest101_1.527 org.pytorch.serve.wlm.WorkerThread - Connecting to: /tmp/.ts.sock.9002 2020-05-27 19:21:06,688 [INFO ] W-9002-nest101_1.527-stdout org.pytorch.serve.wlm.WorkerLifeCycle - Python runtime: 3.8.2 2020-05-27 19:21:06,696 [INFO ] W-9003-nest101_1.527-stdout org.pytorch.serve.wlm.WorkerLifeCycle - Listening on port: /tmp/.ts.sock.9003 2020-05-27 19:21:06,696 [INFO ] W-9003-nest101_1.527-stdout org.pytorch.serve.wlm.WorkerLifeCycle - [PID]3477 2020-05-27 19:21:06,696 [INFO ] W-9003-nest101_1.527-stdout org.pytorch.serve.wlm.WorkerLifeCycle - Torch worker started. 2020-05-27 19:21:06,696 [DEBUG] W-9003-nest101_1.527 org.pytorch.serve.wlm.WorkerThread - W-9003-nest101_1.527 State change null -> WORKER_STARTED 2020-05-27 19:21:06,700 [INFO ] W-9003-nest101_1.527 org.pytorch.serve.wlm.WorkerThread - Connecting to: /tmp/.ts.sock.9003 2020-05-27 19:21:06,696 [INFO ] W-9003-nest101_1.527-stdout org.pytorch.serve.wlm.WorkerLifeCycle - Python runtime: 3.8.2 2020-05-27 19:21:06,705 [INFO ] W-9002-nest101_1.527-stdout org.pytorch.serve.wlm.WorkerLifeCycle - Connection accepted: /tmp/.ts.sock.9002. Model server started. 2020-05-27 19:21:06,736 [INFO ] W-9003-nest101_1.527-stdout org.pytorch.serve.wlm.WorkerLifeCycle - Connection accepted: /tmp/.ts.sock.9003. 2020-05-27 19:21:06,817 [INFO ] pool-2-thread-1 TS_METRICS - CPUUtilization.Percent:0.0|#Level:Host|#hostname:openrdt-dev,timestamp:1590607266 2020-05-27 19:21:06,818 [INFO ] pool-2-thread-1 TS_METRICS - DiskAvailable.Gigabytes:74.79508972167969|#Level:Host|#hostname:openrdt-dev,timestamp:1590607266 2020-05-27 19:21:06,818 [INFO ] pool-2-thread-1 TS_METRICS - DiskUsage.Gigabytes:22.067127227783203|#Level:Host|#hostname:openrdt-dev,timestamp:1590607266 2020-05-27 19:21:06,819 [INFO ] pool-2-thread-1 TS_METRICS - DiskUtilization.Percent:22.8|#Level:Host|#hostname:openrdt-dev,timestamp:1590607266 2020-05-27 19:21:06,820 [INFO ] pool-2-thread-1 TS_METRICS - MemoryAvailable.Megabytes:6876.796875|#Level:Host|#hostname:openrdt-dev,timestamp:1590607266 2020-05-27 19:21:06,821 [INFO ] pool-2-thread-1 TS_METRICS - MemoryUsed.Megabytes:792.828125|#Level:Host|#hostname:openrdt-dev,timestamp:1590607266 2020-05-27 19:21:06,821 [INFO ] pool-2-thread-1 TS_METRICS - MemoryUtilization.Percent:13.6|#Level:Host|#hostname:openrdt-dev,timestamp:1590607266 2020-05-27 19:21:07,590 [WARN ] W-9002-nest101_1.527-stderr org.pytorch.serve.wlm.WorkerLifeCycle - THCudaCheck FAIL file=/opt/conda/conda-bld/pytorch_1587428207430/work/aten/src/THC/THCGeneral.cpp line=47 error=35 : CUDA driver version is insufficient for CUDA runtime version 2020-05-27 19:21:07,613 [INFO ] W-9002-nest101_1.527-stdout org.pytorch.serve.wlm.WorkerLifeCycle - Backend worker process die. 2020-05-27 19:21:07,613 [INFO ] W-9002-nest101_1.527-stdout org.pytorch.serve.wlm.WorkerLifeCycle - Traceback (most recent call last): 2020-05-27 19:21:07,614 [INFO ] W-9002-nest101_1.527-stdout org.pytorch.serve.wlm.WorkerLifeCycle - File "/home/lessw/anaconda3/envs/torchserve/lib/python3.8/site-packages/ts/torch_handler/base_handler.py", line 42, in initialize 2020-05-27 19:21:07,617 [INFO ] W-9002-nest101_1.527-stdout org.pytorch.serve.wlm.WorkerLifeCycle - self.model = torch.jit.load(model_pt_path) 2020-05-27 19:21:07,618 [INFO ] W-9002-nest101_1.527-stdout org.pytorch.serve.wlm.WorkerLifeCycle - File "/home/lessw/anaconda3/envs/torchserve/lib/python3.8/site-packages/torch/jit/init.py", line 228, in load 2020-05-27 19:21:07,618 [INFO ] W-9002-nest101_1.527-stdout org.pytorch.serve.wlm.WorkerLifeCycle - cpp_module = torch._C.import_ir_module(cu, f, map_location, _extra_files) 2020-05-27 19:21:07,619 [INFO ] W-9002-nest101_1.527-stdout org.pytorch.serve.wlm.WorkerLifeCycle - RuntimeError: cuda runtime error (35) : CUDA driver version is insufficient for CUDA runtime version at /opt/conda/conda-bld/pytorch_1587428207430/work/aten/src/THC/THCGeneral.cpp:47 2020-05-27 19:21:07,619 [INFO ] W-9002-nest101_1.527-stdout org.pytorch.serve.wlm.WorkerLifeCycle - 2020-05-27 19:21:07,620 [INFO ] W-9002-nest101_1.527-stdout org.pytorch.serve.wlm.WorkerLifeCycle - During handling of the above exception, another exception occurred: 2020-05-27 19:21:07,620 [INFO ] W-9002-nest101_1.527-stdout org.pytorch.serve.wlm.WorkerLifeCycle - 2020-05-27 19:21:07,620 [INFO ] W-9002-nest101_1.527-stdout org.pytorch.serve.wlm.WorkerLifeCycle - Traceback (most recent call last): 2020-05-27 19:21:07,620 [INFO ] W-9002-nest101_1.527-stdout org.pytorch.serve.wlm.WorkerLifeCycle - File "/home/lessw/anaconda3/envs/torchserve/lib/python3.8/site-packages/ts/torch_handler/image_classifier.py", line 84, in handle 2020-05-27 19:21:07,621 [INFO ] W-9002-nest101_1.527-stdout org.pytorch.serve.wlm.WorkerLifeCycle - _service.initialize(context) 2020-05-27 19:21:07,621 [INFO ] W-9002-nest101_1.527-stdout org.pytorch.serve.wlm.WorkerLifeCycle - File "/home/lessw/anaconda3/envs/torchserve/lib/python3.8/site-packages/ts/torch_handler/base_handler.py", line 45, in initialize 2020-05-27 19:21:07,622 [INFO ] W-9002-nest101_1.527-stdout org.pytorch.serve.wlm.WorkerLifeCycle - model_file = self.manifest['model']['modelFile'] 2020-05-27 19:21:07,624 [INFO ] W-9002-nest101_1.527-stdout org.pytorch.serve.wlm.WorkerLifeCycle - KeyError: 'modelFile' 2020-05-27 19:21:07,624 [INFO ] W-9002-nest101_1.527-stdout org.pytorch.serve.wlm.WorkerLifeCycle - 2020-05-27 19:21:07,624 [INFO ] W-9002-nest101_1.527-stdout org.pytorch.serve.wlm.WorkerLifeCycle - During handling of the above exception, another exception occurred: 2020-05-27

lessw2020 avatar May 27 '20 19:05 lessw2020

Note that I was using the default image classifier handler above and in reviewing the code see that while the device is checked in base_handler.py:

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

    # Read model serialize/pt file
    serialized_file = self.manifest['model']['serializedFile']
    model_pt_path = os.path.join(model_dir, serialized_file)
    if not os.path.isfile(model_pt_path):
        raise RuntimeError("Missing the model.pt file")

   `

It's not using the map_location param: try: logger.debug('Loading torchscript model') self.model = torch.jit.load(model_pt_path)

If it helps, I checked the info on the torch.load api: Load all tensors to the original device torch.jit.load(buffer)

Load all tensors onto CPU, using a device buffer.seek(0) torch.jit.load(buffer, map_location=torch.device('cpu'))

Load all tensors onto CPU, using a string buffer.seek(0) torch.jit.load(buffer, map_location='cpu')

file reference: https://github.com/pytorch/serve/blob/master/ts/torch_handler/base_handler.py

lessw2020 avatar May 28 '20 01:05 lessw2020

@lessw2020 Please see Save on GPU, load on CPU recipe for proper way of handling this.

chauhang avatar May 30 '20 17:05 chauhang

Hi @chauhang - thanks for the link. I see the various setups in the link, but my question remains of whether you can update the base_handler class to dynamically handle the three main load cases? Right now I believe the code properly handles: 1 - Train GPU / Load GPU 2 - Train CPU / Load CPU but does not handle: 3 - Train GPU / Load CPU 4 - (Less common) Train CPU / Load GPU 3 and 4 both require using the map_location param. If you don't want to support #3, then I think at least catching the exception and reporting it in the log would help as currently it just spews out a bunch of incorrect CUDA version errors.

But it seems better would be to catch and retry for scenario 3 or otherwise dynamically compare and do the right thing (or worst case pass in a param at load to tell it to load on CPU)? Otherwise the fix is we have to save out one model for CPU deployment, another for GPU deployment if you have mixed servers, or else subclass from the basehandler and update the load code to support train GPU / load CPU? Please let me know what you'd recommend. Thanks

lessw2020 avatar May 30 '20 23:05 lessw2020

@lessw2020 Tried all the above combination with scripted as well as eager mode models however not facing any such issues. Here is the sample script I am using on cpu and gpu machines. NOTE - once scripted module is saved, I am moving the same to gpu/cpu machine for cross-testing.

codinnvrends avatar Jun 08 '20 19:06 codinnvrends

Hi @dhaniram-kshirsagar - thanks for the update. My scenario that it failed on was training on GPU, model on GPU and save/script model. Then load on CPU only (no gpu anywhere) server. If that's now working, that's great to hear though I'm not clear what would have changed. Ultimately just want to make it easy for people to load and run their models without having to track how it was originally saved. Thanks for testing out the various combos!

lessw2020 avatar Jun 08 '20 19:06 lessw2020

@lessw2020 You are correct GPU to CPU [only] is giving issue for both scripted and eager mode models. Looks like I forgot to copy model saved on gpu to cpu box while testing! Created fix PR #444 . Thanks a lot again!

codinnvrends avatar Jun 09 '20 12:06 codinnvrends