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

API server支持更好的并发 #1018

Closed
wants to merge 2 commits into from
Closed

API server支持更好的并发 #1018

wants to merge 2 commits into from

Conversation

is
Copy link
Contributor

@is is commented May 14, 2023

通过ThreadPoolExecutor异步执行model.chat
保证多路同时请求能并发响应.

@is
Copy link
Contributor Author

is commented May 15, 2023

参考 #808 使用pydantic定义请求/相应结构.

@hellocxj
Copy link

WEB API 使用pydantic定义输入和输出结构后,可以支持chat_stream模式吗?
另外,如果是单卡,在使用CHAT_STREAM模式的时候,发现同时多人询问,问题会串到不同人的问题中,如何解决。
还有多卡的话,在不同的端口号上启动多个进程响应询问服务,外面通过NGINX统一一个端口上进行转发,是不是也会遇到上面,问答会 串的问题?

@is
Copy link
Contributor Author

is commented May 27, 2023

这个PR并不能实现真正意义上的单卡并发,多请求排队可能都有问题,所以直接关了。

CHAT_STREAM的话,需要换一下thread pool的执行方式,单个session的流反馈用同步方式来执行。

多卡反而比较好搞,FASTAPI本身是支持多worker模式的,根据卡的数量开worker,注意一下worker初始化阶段通过CUDA_VISABLE_DEVICE或者类似的方式控制一下就可以了。

@is
Copy link
Contributor Author

is commented May 27, 2023

def model_stream_predict(input_text, max_length, top_p, temperature, history=[]):
    global model, tokenizer
    next_response_index = 0
    for response, history in model.stream_chat(tokenizer, input_text, history=history, max_length=max_length,
                                               top_p=top_p,
                                               temperature=temperature):
            if len(response) > next_response_index:
                yield response[next_response_index:]
                next_response_index = len(response)
    # print(history)
    torch_gc()

@app.post("/stream_chat")
async def create_stream_chat(params: Params) -> StreamingResponse:
    model_response = model_stream_predict(
        params.prompt,
        history=params.history,
        max_length=params.max_length,
        top_p=params.top_p,
        temperature=params.temperature
    )
    return StreamingResponse(
        content = model_response,
        status_code=status.HTTP_200_OK,
        media_type="text/html",
    )

@hellocxj
Copy link

“需要换一下thread pool的执行方式,单个session的流反馈用同步方式来执行”这个实例代码是不是可以参考这个“通过ThreadPoolExecutor异步执行model.chat
保证多路同时请求能并发响应.” 这个代码实现?

@hellocxj
Copy link

还有一个问题请教,FASTAPI本身是支**持多worker模式的,根据卡的数量开worker,注意一下worker初始化阶段通过CUDA_VISABLE_DEVICE或者类似的方式控制一下就可以了。**这个有示例吗?怎么设置比较合理?

2 similar comments
@hellocxj
Copy link

还有一个问题请教,FASTAPI本身是支**持多worker模式的,根据卡的数量开worker,注意一下worker初始化阶段通过CUDA_VISABLE_DEVICE或者类似的方式控制一下就可以了。**这个有示例吗?怎么设置比较合理?

@hellocxj
Copy link

还有一个问题请教,FASTAPI本身是支**持多worker模式的,根据卡的数量开worker,注意一下worker初始化阶段通过CUDA_VISABLE_DEVICE或者类似的方式控制一下就可以了。**这个有示例吗?怎么设置比较合理?

@is
Copy link
Contributor Author

is commented May 27, 2023 via email

@whitesay
Copy link

“需要换一下thread pool的执行方式,单个session的流反馈用同步方式来执行”这个实例代码是不是可以参考这个“通过ThreadPoolExecutor异步执行model.chat 保证多路同时请求能并发响应.” 这个代码实现?

请问一下这个问题有解决方案吗?我也遇到了类似的问题,在sse流式输出的时候同步执行时,问题会串到不同人的问题中,从而出现ASGI application的报错,请问一下能否实现stream下不相互干扰的多并发?谢谢!

@whitesay
Copy link

另外,如果是单卡,在使用CHAT_STREAM模式的时候,发现同时多人询问,问题会串到不同人的问题中,如何解决。

请问一下您解决这个问题了吗?感谢!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants