Repository navigation
Conversation
Member
|
Could you add a high-level description of what the PR does so the PR is self-contained? Is there an issue we could link to this PR? Also there is no need to add "WIP" to the title, this is what "Draft PR" means :) |
regex.py
Contributor
Author
|
I believe I've found a bug in For the token Edit: appears it's being addressed in #904 |
Contributor
Author
|
Seeing great results with this so far!
Pretty close to Full results
|
lapp0
force-pushed
the
trie
branch
3 times, most recently
from
July 30, 2024 05:58
e189254 to
79c67ea
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #795
Awaiting #1070
Problem
In
regex.pyOutlines compiles an index of legal tokens for each state of the FSM (state_scan_tokens)On the
mainbranch we use a naive approach:state,token) belonging to (FSM,vocabulary), simulate FSM traversal character-by-character, saving (state,token) pairs which can successfully walk the FSM.Calling walking the FSM
num_tokens * num_statestimes is inefficient and the current bottleneck in index construction.Solution
We can improve this by using a Trie.
We know for a fact that if the token
foois illegal at a state, the tokenfoobaris also illegal. Therefore we construct a trie, and if only iffoois legal, we checkfoobar, and only iffoobaris legal, we checkfoobarbaz.This prevents unnecessary work from being performed and reduces runtime on average.
Result
Some overhead is introduced, resulting in a slight degradation in performance for simple patterns, but a great performance increase for complex patterns.
ssn):352ms->374mscomplex_schema):3.09s->1.30sAnalysis
This analysis reviews the profiling details of multiple runs using
complex_schema.This PR is primarily an optimization of
state_scan_tokens. Runtime of this function reduces from an average of 6.52s to 1.77 forcomplex_schemaNew result profiling breakdown (performance varies by pattern):
RegexGuide(): 4.55screate_states_mapping(): 4.15sPattern.to_fsm().reduce(): 0.961screate_fsm_index_tokenizer(): 3.34sreduced_vocabulary(): 0.55screate_fsm_index_end_to_end(): 2.29sstate_scan_tokens(...): 1.77s (359 calls)Benchmarks
(Benchmarks are relative to #1070)
Benchmarks that have improved:
Benchmarks that have stayed the same:
Details
Profile
Details
by
tottimeby
cumtime