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

[fix] fix vl gradio, use pipeline api and remove interactive chat #3136

Merged
merged 2 commits into from
Feb 17, 2025

Conversation

irexyc
Copy link
Collaborator

@irexyc irexyc commented Feb 12, 2025

Motivation

Use pipeline api and remove interactive chat to support vl models like qwen2-vl

Currently with pytoch backend, it seems that the generator cannot be interrupted when sending a stop request. @grimoire

# reproduction code

import asyncio
from lmdeploy import pipeline, PytorchEngineConfig
pipe = pipeline('/mnt/141/vicuna-7b-v1.5/', backend_config=PytorchEngineConfig())

event = asyncio.Event()
messages = '写一篇高考作文'
session_id = 0

async def inference():
    step = 0
    async for out in pipe.generate(messages=messages, session_id=session_id):
        print(step, out)
        step += 1
        if (step == 5):
            event.set()
    print('inference done')


async def stop():
    await event.wait()
    await pipe.stop_session(session_id=session_id)


async def main():
    await asyncio.gather(inference(), stop())


if __name__ == '__main__':
    asyncio.run(main())

@grimoire
Copy link
Collaborator

The asyncio.Event should be created in a coroutine to prevent different even loop error.

Pytorch engine assumes user would break the generate loop by themself. If the automatic break is required, you can change _on_stop_session:

def _on_stop_session(self, reqs: Request, **kwargs):

to

    def _on_stop_session(self, reqs: Request, **kwargs):
        """on stop session callback."""
        for req in reqs:
            session_id = req.data['session_id']
            resp = req.data.get('response', True)
            resp_type = ResponseType.SESSION_NOT_EXIST
            if session_id in self.scheduler.sessions:
                self.scheduler.stop_session(session_id)
                session =  self.scheduler.sessions[session_id]
                for seq in session.sequences.values():
                    resp: Response = getattr(seq, 'resp', None)
                    if resp is not None:
                        resp.type = ResponseType.FINISH
                        self.req_manager.response(resp)
                resp_type = ResponseType.SUCCESS
            if resp:
                self._response(req.resp, resp_type)

@irexyc
Copy link
Collaborator Author

irexyc commented Feb 13, 2025

@grimoire
After applying this modification, there is still a probability that the program may hang

The following code is easier to reproduce

import asyncio
import numpy as np
from lmdeploy import pipeline, PytorchEngineConfig
import random

messages = '写一篇高考作文'
pipe = pipeline('/mnt/141/vicuna-7b-v1.5/', backend_config=PytorchEngineConfig())


async def inference(session_id):
    async for out in pipe.generate(messages=messages, session_id=session_id):
        pass
    print('inference done')


async def timer(event):
    dur = 1 + random.random() * 2
    await asyncio.sleep(dur)
    event.set()


async def stop(event, session_id):
    await event.wait()
    await pipe.stop_session(session_id=session_id)
    print('stop session')


async def main():
    session_id = 0
    event = asyncio.Event()
    while True:
        await asyncio.gather(inference(session_id), timer(event), stop(event, session_id))
        print(f'session={session_id} done\n')
        session_id += 1
        event.clear()


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

@grimoire
Copy link
Collaborator

def _response(self, resp: Response, resp_type: ResponseType, data: Any = None, err_msg: str = ''):

    def _response(self, resp: Response, resp_type: ResponseType, data: Any = None, err_msg: str = ''):
        """response."""
        if resp.type == ResponseType.FINISH:
            return
        resp.type = resp_type
        resp.data = data
        resp.err_msg = err_msg
        self.req_manager.response(resp)

@zhulinJulia24
Copy link
Collaborator

It works on gradio!

Copy link
Collaborator

@zhulinJulia24 zhulinJulia24 left a comment

Choose a reason for hiding this comment

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

lgtm

@lvhan028 lvhan028 merged commit 28f3027 into InternLM:main Feb 17, 2025
5 checks passed
tastelikefeet added a commit to tastelikefeet/lmdeploy that referenced this pull request Feb 25, 2025
…oad_state_dict

* commit 'f6f7a5d707e3ccbc69af10babf1c9afcaf72a402':
  fix deepseekv2 has no attribute use_mla error (InternLM#3188)
  fix blocked fp8 moe (InternLM#3181)
  [Feature] support deepseek-vl2 for pytorch engine (InternLM#3149)
  make turbomind support gpu embedding inputs (InternLM#3177)
  fix temperature=0 (InternLM#3176)
  Update qwen2.py (InternLM#3174)
  Fix tool call prompt for InternLM and Qwen (InternLM#3156)
  Use pad_token_id as image_token_id for vl models (InternLM#3158)
  fix default temperature value (InternLM#3166)
  fix min length penalty (InternLM#3150)
  update cuda runtime package dependencies (InternLM#3142)
  fix typing (InternLM#3153)
  support deepseekv2 for maca backend. (InternLM#2918)
  fix the issue that stop_token may be less than defined in model.py (InternLM#3148)
  [fix] fix vl gradio, use pipeline api and remove interactive chat (InternLM#3136)
  [feature] add dlinfer w8a8 support. (InternLM#2988)
  Use aiohttp inside proxy server && add --disable-cache-status argument (InternLM#3020)
  support eos_token list in turbomind (InternLM#3044)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants