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

Add the logit_bias option in API serving #6444

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions src/llamafactory/api/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ async def create_chat_completion_response(
max_new_tokens=request.max_tokens,
num_return_sequences=request.n,
stop=request.stop,
logit_bias=request.logit_bias,
)

prompt_length, response_length = 0, 0
Expand Down Expand Up @@ -214,6 +215,7 @@ async def create_stream_chat_completion_response(
top_p=request.top_p,
max_new_tokens=request.max_tokens,
stop=request.stop,
logit_bias=request.logit_bias,
):
if len(new_token) != 0:
yield _create_stream_chat_completion_chunk(
Expand Down
1 change: 1 addition & 0 deletions src/llamafactory/api/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class ChatCompletionRequest(BaseModel):
max_tokens: Optional[int] = None
stop: Optional[Union[str, List[str]]] = None
stream: bool = False
logit_bias: Optional[Union[Dict[int, float], Dict[str, float]]] = None


class ChatCompletionResponseChoice(BaseModel):
Expand Down
8 changes: 8 additions & 0 deletions src/llamafactory/chat/hf_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,16 @@ def _process_args(
max_length: Optional[int] = input_kwargs.pop("max_length", None)
max_new_tokens: Optional[int] = input_kwargs.pop("max_new_tokens", None)
stop: Optional[Union[str, List[str]]] = input_kwargs.pop("stop", None)
logit_bias: Optional[Union[Dict[int, float], Dict[str, float]]] = input_kwargs.pop("logit_bias", None)

if stop is not None:
logger.warning_rank0("Stop parameter is not supported by the huggingface engine yet.")

if logit_bias is not None:
logit_bias = {
int(token): bias
for token, bias in logit_bias.items()
}

generating_args = generating_args.copy()
generating_args.update(
Expand All @@ -135,6 +142,7 @@ def _process_args(
length_penalty=length_penalty if length_penalty is not None else generating_args["length_penalty"],
eos_token_id=[tokenizer.eos_token_id] + tokenizer.additional_special_tokens_ids,
pad_token_id=tokenizer.pad_token_id,
logit_bias=logit_bias,
)
)

Expand Down
8 changes: 8 additions & 0 deletions src/llamafactory/chat/vllm_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ async def _generate(
max_length: Optional[int] = input_kwargs.pop("max_length", None)
max_new_tokens: Optional[int] = input_kwargs.pop("max_new_tokens", None)
stop: Optional[Union[str, List[str]]] = input_kwargs.pop("stop", None)
logit_bias: Optional[Union[Dict[int, float], Dict[str, float]]] = input_kwargs.pop("logit_bias", None)

if length_penalty is not None:
logger.warning_rank0("Length penalty is not supported by the vllm engine yet.")
Expand All @@ -158,6 +159,12 @@ async def _generate(
if max_new_tokens:
max_tokens = max_new_tokens

if logit_bias:
logit_bias = {
int(token): bias
for token, bias in logit_bias.items()
}

sampling_params = SamplingParams(
n=num_return_sequences,
repetition_penalty=(
Expand All @@ -171,6 +178,7 @@ async def _generate(
stop_token_ids=[self.tokenizer.eos_token_id] + self.tokenizer.additional_special_tokens_ids,
max_tokens=max_tokens,
skip_special_tokens=self.generating_args["skip_special_tokens"],
logit_bias=logit_bias,
)

if images is not None: # add image features
Expand Down