-
Notifications
You must be signed in to change notification settings - Fork 492
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
Remove threadsafe #2907
Merged
Merged
Remove threadsafe #2907
Changes from 7 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
19738b2
remove threadsafe
grimoire acab4e7
optimize performance
grimoire 47a03e7
22.4
grimoire 7f8dec6
22.5
grimoire a1caab8
delete jsonl
grimoire 2de5db2
Merge branch 'main' into remove-threadsafe
grimoire 1007f86
add docs
grimoire 053a34e
fix link
grimoire 7254c0d
rst
grimoire b9998d9
Merge branch 'main' into remove-threadsafe
grimoire d62076b
remove sleep req step
grimoire 6ded3f6
remove scheduler sleep
grimoire 9b3ad0e
Merge branch 'main' into remove-threadsafe
grimoire 23cbcc5
fix ut
grimoire 54f8d7b
solve conflict
grimoire 751a98e
Merge branch 'remove-threadsafe' of github.com:grimoire/lmdeploy into…
grimoire 4a8b970
recovery async engine
grimoire File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# PyTorchEngine Multithread | ||
|
||
We have removed `thread_safe` mode from PytorchEngine since [PR2907](https://github.com/InternLM/lmdeploy/pull/2907). We encourage users to achieve high concurrency by using **service API** or **coroutines** whenever possible, for example: | ||
|
||
```python | ||
import asyncio | ||
from lmdeploy import pipeline, PytorchEngineConfig | ||
|
||
event_loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(event_loop) | ||
|
||
model_path = 'Llama-3.2-1B-Instruct' | ||
pipe = pipeline(model_path, backend_config=PytorchEngineConfig()) | ||
|
||
async def _gather_output(): | ||
tasks = [ | ||
pipe.async_batch_infer('Hakuna Matata'), | ||
pipe.async_batch_infer('giraffes are heartless creatures'), | ||
] | ||
return await asyncio.gather(*tasks) | ||
|
||
output = asyncio.run(_gather_output()) | ||
print(output[0].text) | ||
print(output[1].text) | ||
``` | ||
|
||
If you do need multithreading, it would be easy to warp it like below: | ||
|
||
```python | ||
import threading | ||
from queue import Queue | ||
import asyncio | ||
from lmdeploy import pipeline, PytorchEngineConfig | ||
|
||
model_path = 'Llama-3.2-1B-Instruct' | ||
|
||
|
||
async def _batch_infer(inque: Queue, outque: Queue, pipe): | ||
while True: | ||
if inque.empty(): | ||
await asyncio.sleep(0) | ||
continue | ||
|
||
input = inque.get_nowait() | ||
output = await pipe.async_batch_infer(input) | ||
outque.put(output) | ||
|
||
|
||
def server(inques, outques): | ||
event_loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(event_loop) | ||
pipe = pipeline(model_path, backend_config=PytorchEngineConfig()) | ||
for inque, outque in zip(inques, outques): | ||
event_loop.create_task(_batch_infer(inque, outque, pipe)) | ||
event_loop.run_forever() | ||
|
||
def client(inque, outque, message): | ||
inque.put(message) | ||
print(outque.get().text) | ||
|
||
|
||
inques = [Queue(), Queue()] | ||
outques = [Queue(), Queue()] | ||
|
||
t_server = threading.Thread(target=server, args=(inques, outques)) | ||
t_client0 = threading.Thread(target=client, args=(inques[0], outques[0], 'Hakuna Matata')) | ||
t_client1 = threading.Thread(target=client, args=(inques[1], outques[1], 'giraffes are heartless creatures')) | ||
|
||
t_server.start() | ||
t_client0.start() | ||
t_client1.start() | ||
|
||
t_client0.join() | ||
t_client1.join() | ||
``` | ||
|
||
> \[!WARNING\] | ||
> This is NOT recommended, as multithreading introduces additional overhead, leading to unstable inference performance. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# PyTorchEngine 多线程推理 | ||
grimoire marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
自 [PR2907](https://github.com/InternLM/lmdeploy/pull/2907) 起,我们废除了 PytorchEngine 的 thread_safe 模式以保证引擎能够更高效的运行。我们鼓励用户尽可能使用**服务接口**或**协程**来实现高并发,比如: | ||
|
||
```python | ||
import asyncio | ||
from lmdeploy import pipeline, PytorchEngineConfig | ||
|
||
event_loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(event_loop) | ||
|
||
model_path = 'Llama-3.2-1B-Instruct' | ||
pipe = pipeline(model_path, backend_config=PytorchEngineConfig()) | ||
|
||
async def _gather_output(): | ||
tasks = [ | ||
pipe.async_batch_infer('Hakuna Matata'), | ||
pipe.async_batch_infer('giraffes are heartless creatures'), | ||
] | ||
return await asyncio.gather(*tasks) | ||
|
||
output = asyncio.run(_gather_output()) | ||
print(output[0].text) | ||
print(output[1].text) | ||
``` | ||
|
||
如果你确实有多线程推理的需求,那么可以进行简单的封装,来实现类似的效果。 | ||
|
||
```python | ||
import threading | ||
from queue import Queue | ||
import asyncio | ||
from lmdeploy import pipeline, PytorchEngineConfig | ||
|
||
model_path = 'Llama-3.2-1B-Instruct' | ||
|
||
|
||
async def _batch_infer(inque: Queue, outque: Queue, pipe): | ||
while True: | ||
if inque.empty(): | ||
await asyncio.sleep(0) | ||
continue | ||
|
||
input = inque.get_nowait() | ||
output = await pipe.async_batch_infer(input) | ||
outque.put(output) | ||
|
||
|
||
def server(inques, outques): | ||
event_loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(event_loop) | ||
pipe = pipeline(model_path, backend_config=PytorchEngineConfig()) | ||
for inque, outque in zip(inques, outques): | ||
event_loop.create_task(_batch_infer(inque, outque, pipe)) | ||
event_loop.run_forever() | ||
|
||
def client(inque, outque, message): | ||
inque.put(message) | ||
print(outque.get().text) | ||
|
||
|
||
inques = [Queue(), Queue()] | ||
outques = [Queue(), Queue()] | ||
|
||
t_server = threading.Thread(target=server, args=(inques, outques)) | ||
t_client0 = threading.Thread(target=client, args=(inques[0], outques[0], 'Hakuna Matata')) | ||
t_client1 = threading.Thread(target=client, args=(inques[1], outques[1], 'giraffes are heartless creatures')) | ||
|
||
t_server.start() | ||
t_client0.start() | ||
t_client1.start() | ||
|
||
t_client0.join() | ||
t_client1.join() | ||
``` | ||
|
||
> \[!WARNING\] | ||
> 我们不鼓励这样实现,多线程会带来额外的开销,使得推理性能不稳定 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lzhangzz After PR #2968, can users use
pipeline
api in multithreading?