[#SAMPLE- 7] Constrained generation with DFA #4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR subsumes everything done in #1 and #2 and some more experimentation.
It shows how to implement constrained generation with DFA.
Two modes are available:
:statelessIn each generation step, we start at the initial state, then we go through each of the already generated tokens and find the new state using the state transition tensor.
After we went through the complete generated sequence to the current point we find the current state.
With the current state we can determine which token ids are allowed, using the state transitions tensor.
(this mode implicitly assumes that the initial state is 0, and that we won't return to the initial state at a later step as we see values of 0 in the state transitions tensor as "empty/invalid". I think we could fix that by making sure that row 0 is actually empty and 0 is an invalid state).
:statefulFor this we need stateful logit processors, that's why this branch is based on [#SAMPLE-6] Add state to logits processing #3 .
With that we can just remember the last state and use that and the last token to determine the current state using the state transitions tensor.
With the current state we can determine which token ids are allowed, using the state transitions tensor.