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

[Frontend][Core] Update Outlines Integration from FSM to Guide #4109

Merged
merged 17 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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: 1 addition & 1 deletion requirements-common.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ pydantic >= 2.0 # Required for OpenAI server.
prometheus_client >= 0.18.0
tiktoken == 0.6.0 # Required for DBRX tokenizer
lm-format-enforcer == 0.9.3
outlines == 0.0.34 # Requires torch >= 2.1.0
outlines >= 0.0.37
typing_extensions
filelock >= 3.10.4 # filelock starts to support `mode` argument from 3.10.4
3 changes: 2 additions & 1 deletion tests/entrypoints/test_openai_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ def __init__(self, args):
env = os.environ.copy()
env["PYTHONUNBUFFERED"] = "1"
self.proc = subprocess.Popen(
["python3", "-m", "vllm.entrypoints.openai.api_server"] + args,
[sys.executable, "-m", "vllm.entrypoints.openai.api_server"] +
args,
env=env,
stdout=sys.stdout,
stderr=sys.stderr,
Expand Down
26 changes: 15 additions & 11 deletions vllm/model_executor/guided_decoding/outlines_logits_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,16 @@
from typing import Callable, DefaultDict, Dict, List, Optional, Union

import torch
from outlines.fsm.fsm import CFGFSM, FSM, RegexFSM
from outlines.fsm.guide import CFGGuide, Generate, Guide, RegexGuide, Write
from outlines.fsm.json_schema import build_regex_from_schema
from pydantic import BaseModel
from transformers import PreTrainedTokenizerBase


class BaseLogitsProcessor:

def __init__(self):
# Child class should use initialize in their init.
self.fsm: FSM
def __init__(self, guide: Guide):
self._guide: Guide = guide

def init_state(self):
"""Initialize the FSM states."""
Expand All @@ -47,10 +46,17 @@ def __call__(self, input_ids: List[int],
else:
last_token = input_ids[-1]
last_seq_id = hash(tuple(input_ids[:-1]))
self.fsm_state[seq_id] = self.fsm.next_state(
self.fsm_state[last_seq_id], last_token)
self.fsm_state[seq_id] = self._guide.get_next_state(
state=self.fsm_state[last_seq_id], token_id=last_token)

allowed_tokens = self.fsm.allowed_token_ids(self.fsm_state[seq_id])
instruction = self._guide.get_next_instruction(
state=self.fsm_state[seq_id])

if type(instruction) == Generate:
allowed_tokens = instruction.tokens
elif type(instruction) == Write:
# TODO: support fast forward tokens
allowed_tokens = [instruction.tokens[0]]
br3no marked this conversation as resolved.
Show resolved Hide resolved

mask = torch.full((scores.shape[-1], ),
-math.inf,
Expand All @@ -74,8 +80,7 @@ def __init__(self, regex_string: str, tokenizer: PreTrainedTokenizerBase):

"""
tokenizer = _adapt_tokenizer(tokenizer)
fsm = RegexFSM(regex_string, tokenizer)
self.fsm = fsm
super().__init__(RegexGuide(regex_string, tokenizer))


class JSONLogitsProcessor(RegexLogitsProcessor):
Expand Down Expand Up @@ -128,8 +133,7 @@ def __init__(self, cfg: str, tokenizer: PreTrainedTokenizerBase):

"""
tokenizer = _adapt_tokenizer(tokenizer)
fsm = CFGFSM(cfg, tokenizer)
self.fsm = fsm
super().__init__(CFGGuide(cfg, tokenizer))


@lru_cache
Expand Down
Loading