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

Generate a choice between different strings #188

Merged
merged 3 commits into from
Jul 24, 2023

Conversation

rlouf
Copy link
Member

@rlouf rlouf commented Jul 13, 2023

Closes #152. I use the Regex generation method using a (choice_1|choice_2|...|choice_n) regex. This method is equivalent to rejection sampling: draw a token and discard it if it is not on a path that generates either of the choices.

An alternative implementation would be to tokenize the choices, do a forward pass through the model with each and sample from the resulting categorical distribution. However, some strings can be built from different combinations of tokens and we would only be considering one for each stopping sequence.

Unsurprisingly, the check to verify that a sequence can be generated with the vocabulary added in #175 fails. We fix this by checking whether we can build terminal paths with the vocabulary. Closes #184.

@rlouf rlouf added text Linked to text generation enhancement labels Jul 13, 2023
@rlouf rlouf force-pushed the add-choice-generation-method branch from 9228675 to c1d7a61 Compare July 15, 2023 16:42
for i, vocab_string in enumerate(vocabulary):
if vocab_string == final_state_string:
final_state_string_idx = i

for end_idx, state_seq in find_partial_matches(fsm, vocab_string):
if partial_match_filter(vocab_string, end_idx, state_seq):
terminal_possible_paths[state_seq[0]].add(state_seq[-1])
Copy link
Contributor

Choose a reason for hiding this comment

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

Whether or not the last state is a final/accept state of the FSM will depend on partial_match_filter, no? We might need extra handling for this, or we could track these things in that filter function, after adding the symbol name as an argument, of course.

Copy link
Member Author

@rlouf rlouf Jul 16, 2023

Choose a reason for hiding this comment

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

The check would fail if the combination of the vocabulary and partial_match_filter didn't allow to reach a final state, no? Isn't that the desired behaviour?

@rlouf rlouf added the structured generation Linked to structured generation label Jul 17, 2023
@rlouf rlouf force-pushed the add-choice-generation-method branch 3 times, most recently from a36c5c2 to b29a601 Compare July 17, 2023 15:14
@rlouf rlouf added this to the 0.1 milestone Jul 19, 2023
If the vocabulary does not allow to transition from the FSM's initial
state to one of its terminal paths it will be impossible to generate
a sequence that matches the input regex. We therefore raise an exception
when no such transition exists.
@rlouf rlouf force-pushed the add-choice-generation-method branch from b29a601 to a3f7b9e Compare July 24, 2023 11:14
@rlouf rlouf merged commit a9cd45b into outlines-dev:main Jul 24, 2023
4 checks passed
@rlouf rlouf deleted the add-choice-generation-method branch July 24, 2023 11:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement structured generation Linked to structured generation text Linked to text generation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Relax check for whether we can build a sequence that matches a regex Add a Choice generation method
2 participants