-
Notifications
You must be signed in to change notification settings - Fork 485
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
Use a trie to speed up index construction #887
Conversation
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
I believe I've found a bug in For the token Edit: appears it's being addressed in #904 |
Seeing great results with this so far!
Pretty close to Full results
|
e189254
to
79c67ea
Compare
Fixes #795
Awaiting #1070
Problem
In
regex.py
Outlines compiles an index of legal tokens for each state of the FSM (state_scan_tokens
)On the
main
branch 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_states
times 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
foo
is illegal at a state, the tokenfoobar
is also illegal. Therefore we construct a trie, and if only iffoo
is legal, we checkfoobar
, and only iffoobar
is 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
->374ms
complex_schema
):3.09s
->1.30s
Analysis
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_schema
New 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:
Profile
by
tottime
by
cumtime