Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Torchrun launching multiple api_server #2402

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 27 additions & 0 deletions docs/en/llm/api_server.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,33 @@ curl http://{server_ip}:{server_port}/v1/chat/interactive \
lmdeploy serve gradio api_server_url --server-name ${gradio_ui_ip} --server-port ${gradio_ui_port}
```

## Launch multiple api servers

Following is a possible way to launch multiple api servers through torchrun. Just create a python script with the following codes.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hi @AllentDan In what scenarios would this feature be used?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Some researchers tend to use torchrun.

Launch the script through `torchrun --nproc_per_node 2 script.py InternLM/internlm2-chat-1_8b`.

```python
from typing import List
import fire

import os

def main(model_path: str,
port: int = 23333):
local_rank = int(os.environ.get('LOCAL_RANK', -1))
if isinstance(port, List):
assert len(port) == int(os.environ.get('WORLD_SIZE', -1))
port = port[local_rank]
else:
port += local_rank*10
command = f'CUDA_VISIBLE_DEVICES={local_rank} lmdeploy serve api_server {model_path} --server-port {port}'
os.system(command)


if __name__ == '__main__':
fire.Fire(main)
```

## FAQ

1. When user got `"finish_reason":"length"`, it means the session is too long to be continued. The session length can be
Expand Down
26 changes: 26 additions & 0 deletions docs/zh_cn/llm/api_server.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,32 @@ curl http://{server_ip}:{server_port}/v1/chat/interactive \
}'
```

## 同时启动多个 api_server

下面是一个可以用 torchrun 启动的脚本。用下面的代码跑 torchrun: `torchrun --nproc_per_node 2 script.py InternLM/internlm2-chat-1_8b`.

```python
from typing import List
import fire

import os

def main(model_path: str,
port: int = 23333):
local_rank = int(os.environ.get('LOCAL_RANK', -1))
if isinstance(port, List):
assert len(port) == int(os.environ.get('WORLD_SIZE', -1))
port = port[local_rank]
else:
port += local_rank*10
command = f'CUDA_VISIBLE_DEVICES={local_rank} lmdeploy serve api_server {model_path} --server-port {port}'
os.system(command)


if __name__ == '__main__':
fire.Fire(main)
```

## 接入 WebUI

LMDeploy 提供 gradio 和 [OpenAOE](https://github.com/InternLM/OpenAOE) 两种方式,为 api_server 接入 WebUI。
Expand Down
16 changes: 15 additions & 1 deletion lmdeploy/serve/openai/api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,20 @@ async def stream_results() -> AsyncGenerator[bytes, None]:
return JSONResponse(ret)


def handle_torchrun():
"""To disable mmengine logging logic when using torchrun."""

def dummy_get_device_id():
return 0

if int(os.environ.get('LOCAL_RANK', -1)) > 0:
from lmdeploy.vl.model.utils import _set_func

# the replacement can't be recovered
_set_func('mmengine.logging.logger._get_device_id',
dummy_get_device_id)


def serve(model_path: str,
model_name: Optional[str] = None,
backend: Literal['turbomind', 'pytorch'] = 'turbomind',
Expand Down Expand Up @@ -986,8 +1000,8 @@ def serve(model_path: str,
ssl_certfile = os.environ['SSL_CERTFILE']
http_or_https = 'https'

handle_torchrun()
_, pipeline_class = get_task(model_path)

VariableInterface.async_engine = pipeline_class(
model_path=model_path,
model_name=model_name,
Expand Down
Loading