Failed to start torchserve: Backend worker error
🐛 Describe the bug
About 30~40 seconds after torchserve --start ..., it prints some error messages and stops my model.
The client side has not been involved yet.
Error logs
Start model
torchserve --start --ncs \
--model-store /data/serving-models \
--ts-config config.properties \
--models test-model.mar &
Outputs:
2023-06-05T07:28:08,429 [INFO ] main org.pytorch.serve.servingsdk.impl.PluginsManager - Initializing plugins manager...
2023-06-05T07:28:08,481 [INFO ] main org.pytorch.serve.metrics.configuration.MetricConfiguration - Successfully loaded metrics configuration from /home/aiscuser/.local/lib/python3.8/site-packages/ts/configs/metrics.yaml
2023-06-05T07:28:08,775 [INFO ] main org.pytorch.serve.ModelServer -
Torchserve version: 0.8.0
TS Home: /home/aiscuser/.local/lib/python3.8/site-packages
Current directory: /scratch/amlt_code
Temp directory: /tmp
Metrics config path: /home/aiscuser/.local/lib/python3.8/site-packages/ts/configs/metrics.yaml
Number of GPUs: 0
Number of CPUs: 104
Max heap size: 16384 M
Python executable: /opt/conda/bin/python3.8
Config file: config.properties
Inference address: http://127.0.0.1:18443
Management address: http://127.0.0.1:18444
Metrics address: http://127.0.0.1:18445
Model Store: /data/serving-models
Initial Models: test-model.mar
Log dir: /scratch/amlt_code/logs
Metrics dir: /scratch/amlt_code/logs
Netty threads: 0
Netty client threads: 0
Default workers per model: 104
Blacklist Regex: N/A
Maximum Response Size: 6553500
Maximum Request Size: 6553500
Limit Maximum Image Pixels: true
Prefer direct buffer: false
Allowed Urls: [file://.*|http(s)?://.*]
Custom python dependency for model allowed: false
Enable metrics API: true
Metrics mode: log
Disable system metrics: false
Workflow Store: /data/serving-models
Model config: N/A
2023-06-05T07:28:08,783 [INFO ] main org.pytorch.serve.servingsdk.impl.PluginsManager - Loading snapshot serializer plugin...
2023-06-05T07:28:08,801 [INFO ] main org.pytorch.serve.ModelServer - Loading initial models: test-model.mar
2023-06-05T07:28:09,211 [DEBUG] main org.pytorch.serve.wlm.ModelVersionedRefs - Adding new version 0.1 for model test-model
2023-06-05T07:28:09,211 [DEBUG] main org.pytorch.serve.wlm.ModelVersionedRefs - Setting default version to 0.1 for model test-model
2023-06-05T07:28:09,211 [INFO ] main org.pytorch.serve.wlm.ModelManager - Model test-model loaded.
2023-06-05T07:28:09,212 [DEBUG] main org.pytorch.serve.wlm.ModelManager - updateModel: test-model, count: 1
2023-06-05T07:28:09,221 [INFO ] main org.pytorch.serve.ModelServer - Initialize Inference server with: EpollServerSocketChannel.
2023-06-05T07:28:09,221 [DEBUG] W-9000-test-model_0.1 org.pytorch.serve.wlm.WorkerLifeCycle - Worker cmdline: [/opt/conda/bin/python3.8, /home/aiscuser/.local/lib/python3.8/site-packages/ts/model_service_worker.py, --sock-type, unix, --sock-name, /tmp/.ts.sock.9000, --metrics-config, /home/aiscuser/.local/lib/python3.8/site-packages/ts/configs/metrics.yaml]
2023-06-05T07:28:09,280 [INFO ] main org.pytorch.serve.ModelServer - Inference API bind to: http://127.0.0.1:18443
2023-06-05T07:28:09,280 [INFO ] main org.pytorch.serve.ModelServer - Initialize Management server with: EpollServerSocketChannel.
2023-06-05T07:28:09,281 [INFO ] main org.pytorch.serve.ModelServer - Management API bind to: http://127.0.0.1:18444
2023-06-05T07:28:09,282 [INFO ] main org.pytorch.serve.ModelServer - Initialize Metrics server with: EpollServerSocketChannel.
2023-06-05T07:28:09,282 [INFO ] main org.pytorch.serve.ModelServer - Metrics API bind to: http://127.0.0.1:18445
Model server started.
2023-06-05T07:28:09,478 [WARN ] pool-3-thread-1 org.pytorch.serve.metrics.MetricCollector - worker pid is not available yet.
2023-06-05T07:28:43,544 [WARN ] W-9000-test-model_0.1-stderr MODEL_LOG - ERROR:root:Backend worker did not receive connection in: 30
2023-06-05T07:28:44,546 [INFO ] W-9000-test-model_0.1-stderr org.pytorch.serve.wlm.WorkerLifeCycle - Stopped Scanner - W-9000-test-model_0.1-stderr
2023-06-05T07:28:44,546 [INFO ] W-9000-test-model_0.1-stdout org.pytorch.serve.wlm.WorkerLifeCycle - Stopped Scanner - W-9000-test-model_0.1-stdout
2023-06-05T07:28:44,547 [WARN ] W-9000-test-model_0.1 org.pytorch.serve.wlm.WorkerLifeCycle - terminateIOStreams() threadName=W-9000-test-model_0.1-stderr
2023-06-05T07:28:44,547 [WARN ] W-9000-test-model_0.1 org.pytorch.serve.wlm.WorkerLifeCycle - terminateIOStreams() threadName=W-9000-test-model_0.1-stdout
2023-06-05T07:28:44,547 [ERROR] W-9000-test-model_0.1 org.pytorch.serve.wlm.WorkerThread - Backend worker error
org.pytorch.serve.wlm.WorkerInitializationException: Backend stream closed.
at org.pytorch.serve.wlm.WorkerLifeCycle.startWorker(WorkerLifeCycle.java:173) ~[model-server.jar:?]
at org.pytorch.serve.wlm.WorkerThread.connect(WorkerThread.java:339) ~[model-server.jar:?]
at org.pytorch.serve.wlm.WorkerThread.run(WorkerThread.java:183) [model-server.jar:?]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) [?:?]
at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) [?:?]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) [?:?]
at java.lang.Thread.run(Thread.java:833) [?:?]
2023-06-05T07:28:44,558 [DEBUG] W-9000-test-model_0.1 org.pytorch.serve.wlm.WorkerThread - W-9000-test-model_0.1 State change null -> WORKER_STOPPED
2023-06-05T07:28:44,558 [INFO ] W-9000-test-model_0.1 org.pytorch.serve.wlm.WorkerThread - Auto recovery start timestamp: 1685950124558
2023-06-05T07:28:44,558 [WARN ] W-9000-test-model_0.1 org.pytorch.serve.wlm.WorkerLifeCycle - terminateIOStreams() threadName=W-9000-test-model_0.1-stderr
2023-06-05T07:28:44,558 [WARN ] W-9000-test-model_0.1 org.pytorch.serve.wlm.WorkerLifeCycle - terminateIOStreams() threadName=W-9000-test-model_0.1-stdout
2023-06-05T07:28:44,559 [INFO ] W-9000-test-model_0.1 org.pytorch.serve.wlm.WorkerThread - Retry worker: 9000 in 1 seconds.
2023-06-05T07:28:45,560 [DEBUG] W-9000-test-model_0.1 org.pytorch.serve.wlm.WorkerLifeCycle - Worker cmdline: [/opt/conda/bin/python3.8, /home/aiscuser/.local/lib/python3.8/site-packages/ts/model_service_worker.py, --sock-type, unix, --sock-name, /tmp/.ts.sock.9000, --metrics-config, /home/aiscuser/.local/lib/python3.8/site-packages/ts/configs/metrics.yaml]
Ping to torchserve
curl http://127.0.0.1:18443/ping
Output:
2023-06-05T07:28:18,930 [INFO ] pool-2-thread-2 ACCESS_LOG - /127.0.0.1:45220 "GET /ping HTTP/1.1" 200 8
2023-06-05T07:28:18,931 [INFO ] pool-2-thread-2 TS_METRICS - Requests2XX.Count:1.0|#Level:Host|#hostname:node-0,timestamp:1685950098
{
"status": "Healthy"
}
Installation instructions
Python packages
Install all packages with pip, including torchserve.
Java
sudo apt update
sudo apt install -y openjdk-17-jre
java --version
openjdk 17.0.7 2023-04-18
OpenJDK Runtime Environment (build 17.0.7+7-Ubuntu-0ubuntu120.04)
OpenJDK 64-Bit Server VM (build 17.0.7+7-Ubuntu-0ubuntu120.04, mixed mode, sharing)
Model Packaing
My model hander is as follow:
import numpy as np
import torch
from torch import Tensor
from ts.torch_handler.base_handler import BaseHandler
class ServingHandler(BaseHandler):
"""Handler for model serving."""
def __init__(self):
print("Start to init server...")
super().__init__()
def preprocess(self, data):
batch_size = len(data)
print(f"---receive data: {batch_size}")
items = []
for state in data:
items.append(np.frombuffer(state.get("data"), dtype=np.int16))
arr = np.asarray(items, dtype=np.int16)
preprocessed_data = torch.tensor(arr).reshape((batch_size, -1))
if torch.cuda.is_available():
preprocessed_data = preprocessed_data.cuda()
return preprocessed_data
def sample(self, logits: Tensor):
probs = torch.nn.functional.softmax(logits, dim=1)
index = torch.multinomial(probs, 1, replacement=True)
return index
def inference(self, data, *args, **kwargs):
logits = self.model(data) # type: ignore
index = self.sample(logits=logits)
return index
Create test-model.mar
torch-model-archiver --model-name test-model \
--version 0.1 \
--serialized-file /data/serving-models/model.pt
--handler serving_handler.py \
--export-path /data/serving-models
--config-file config.yaml \
--force
config.properties
config.yaml
# TS frontend parameters
# See all supported parameters: https://github.com/pytorch/serve/blob/master/frontend/archive/src/main/java/org/pytorch/serve/archive/model/ModelConfig.java#L14
minWorkers: 1 # default: #CPU or #GPU
maxWorkers: 1 # default: #CPU or #GPU
batchSize: 8 # default: 1
maxBatchDelay: 30 # default: 100 msec
responseTimeout: 120 # default: 120 sec
config.properties
inference_address=http://127.0.0.1:18443
management_address=http://127.0.0.1:18444
metrics_address=http://127.0.0.1:18445
grpc_inference_port=17070
grpc_management_port=17071
Versions
Environment headers
------------------------------------------------------------------------------------------
Torchserve branch:
torchserve==0.8.0
torch-model-archiver==0.8.0
Python version: 3.8 (64-bit runtime)
Python executable: /opt/conda/bin/python
Versions of relevant python libraries:
numpy==1.22.4
nvgpu==0.10.0
psutil==5.9.1
pytest==6.2.5
pytest-cov==3.0.0
pytest-pythonpath==0.7.4
pytorch-lightning==2.0.0
pytorch-quantization==2.1.2
requests==2.27.1
requests-oauthlib==1.3.1
torch==2.0.1
torch-model-archiver==0.8.0
torch-tensorrt==1.2.0a0
torch-workflow-archiver==0.2.8
torchaudio==2.0.2
torchdata==0.6.1
torchmetrics==0.11.4
torchserve==0.8.0
torchtext==0.15.2
torchvision==0.15.2
wheel==0.37.1
torch==2.0.1
torchtext==0.15.2
torchvision==0.15.2
torchaudio==2.0.2
Java Version:
OS: Ubuntu 20.04.4 LTS
GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Clang version: N/A
CMake version: version 3.26.3
Repro instructions
torchserve --start --ncs \
--model-store /data/serving-models \
--ts-config config.properties \
--models test-model.mar &
Possible Solution
No response
Note: the code runs well on my local image, but fails on a k8s cluster with the same image, which is similar to https://github.com/pytorch/serve/issues/2300.
However, unlike random errors in https://github.com/pytorch/serve/issues/2300, the error is always the same from myside.
I am also having same issue. @lee-junjie did you found a fix?
I've same exact error
I am also having same issue. @lee-junjie did you found a fix?
Oh, as there was no response from this thread, I had a try with triton-server, which runs well on both my local and k8s environments.
Note: I failed to build triton-server from source code, so I finally built my image by inheriting from the official triton-server image.
@arnavmehta7
I was also seeing a BackendWorker related error. In my case, it was: WorkerInitializationException: Backend worker startup time out. (https://stackoverflow.com/questions/76566993/why-is-my-torchserve-docker-image-not-working-on-google-cloud-run)
I was deploying the container using GCP Cloud Run. I increased the memory for the Cloud Run instance, reduced the number of worker threads, and increased the startup timeout value:
default_startup_timeout=600
default_workers_per_model=2
These parameters can be set in config.properties:
https://pytorch.org/serve/configuration.html#other-properties
These changes fixed the issue so I think it was a memory related issue.