Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions sdks/python/apache_beam/ml/inference/vllm_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,28 +114,30 @@ def __init__(self, model_name: str, vllm_server_kwargs: dict[str, str]):
self._server_started = False
self._server_process = None
self._server_port: int = -1
self._server_process_lock = threading.RLock()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when multiple threads try to start the server at the same time? Should we use a cross process lock since multiple processes also share the server?

Copy link
Contributor Author

@tvalentyn tvalentyn Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When large_model=true, multiple processes share the server through a single server handle shared across processes, and only one process is responsible for managing the handle. The cross-process lock should not be necessary.

The problem is that right now we call check_connectivity from an async method:

and check_connectivity happens to also start the server if the process already terminated, so as a result we create many processes at once (see internal: b/422577332), and overwrite self._server_process.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Thanks


self.start_server()

def start_server(self, retries=3):
if not self._server_started:
server_cmd = [
sys.executable,
'-m',
'vllm.entrypoints.openai.api_server',
'--model',
self._model_name,
'--port',
'{{PORT}}',
]
for k, v in self._vllm_server_kwargs.items():
server_cmd.append(f'--{k}')
# Only add values for commands with value part.
if v is not None:
server_cmd.append(v)
self._server_process, self._server_port = start_process(server_cmd)

self.check_connectivity(retries)
with self._server_process_lock:
if not self._server_started:
server_cmd = [
sys.executable,
'-m',
'vllm.entrypoints.openai.api_server',
'--model',
self._model_name,
'--port',
'{{PORT}}',
]
for k, v in self._vllm_server_kwargs.items():
server_cmd.append(f'--{k}')
# Only add values for commands with value part.
if v is not None:
server_cmd.append(v)
self._server_process, self._server_port = start_process(server_cmd)

self.check_connectivity(retries)

def get_server_port(self) -> int:
if not self._server_started:
Expand Down
Loading