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] lmdeploy bc #74

Merged
merged 1 commit into from
Nov 29, 2023
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
14 changes: 8 additions & 6 deletions lagent/llms/lmdeploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import random

import lmdeploy.turbomind.chat as tm_chat
from lmdeploy import turbomind as tm
from lmdeploy.serve.turbomind.chatbot import Chatbot, Session, get_logger
from lmdeploy.tokenizer import Tokenizer

from .base_llm import BaseModel

Expand Down Expand Up @@ -98,31 +100,32 @@ class TurboMind(BaseModel):

def __init__(self,
path: str,
max_seq_len: int = 2048,
max_seq_len: int = 8192,
tokenizer_only: bool = False,
meta_template=None,
tp=1,
**kwargs):
super().__init__(path, max_seq_len, tokenizer_only, meta_template)
tokenizer_model_path = osp.join(path, 'triton_models', 'tokenizer')
self.tokenizer = tm_chat.Tokenizer(tokenizer_model_path)
self.tm_model = tm_chat.tm.TurboMind(
self.tokenizer = Tokenizer(tokenizer_model_path)
self.tm_model = tm.TurboMind(
path, eos_id=self.tokenizer.eos_token_id, tp=tp)
self.generator = self.tm_model.create_instance()

model_name = self.tm_model.model_name
self.model = tm_chat.MODELS.get(model_name)(
capability='completion', **kwargs)
self._session_id = 0

def generate(self, prompt, **kwargs):
seed = random.getrandbits(64)
input_ids = self.tokenizer.encode(prompt)
gen_param = tm_chat.get_gen_param(
'completion', self.model.sampling_param, step=0, nth_round=1)

response_size = 0
self._session_id = (self._session_id + 1) % 100000
for outputs in self.generator.stream_infer(
session_id=1,
session_id=self._session_id,
input_ids=[input_ids],
stream_output=False,
**dataclasses.asdict(gen_param),
Expand All @@ -133,5 +136,4 @@ def generate(self, prompt, **kwargs):
response = self.tokenizer.decode(
res.tolist(), offset=response_size)
response = tm_chat.valid_str(response)

return response