A fast constrained decoding engine based on context free grammar.
Project description
kbnf
This crate provides a constrained decoding engine which ensures that a language model's output adheres strictly to the format defined by KBNF (Koishi's BNF), an enhanced variant of EBNF. KBNF includes features that enhance usability, notably embeddable regular expressions and more flexible exceptions.
If you are interested in the design and implementation behind this crate, you may want to check out my blog.
Features
- Supports full context free grammar with worst case O(m*n^3) time complexity, where
n
is the generated text length andm
is the vocabulary size. - Asymptotically fastest for subclasses of context free grammar.
- Guarantees worst case O(m*n) time complexity for every LR(k) grammar(which includes almost all practical grammars)
- Achieves O(n) time complexity with caching eventually given that
n
has a fixed upper bound, or the grammar is regular.
- Vocabulary-independent.
- BPE, BBPE, you-name-it, all types of vocabulary are supported.
- Supports UTF-8 characters in grammar.
- Embeddable regular expressions.
- More flexible exceptions that can exclude unions of strings.
Documentation
Add to your project
Simply add it to your Cargo.toml
or run cargo add kbnf
in your command line.
Performance
One of the goals of this crate is for the constrained decoding engine to be "fast." This can be interpreted both theoretically and practically.
Theoretically, this crate is designed to provide the asymptotically fastest algorithms for each subclass of context free grammar. By implementing an Earley recognizer with Leo optimization, this crate has successfully achieve linear time complexity for every LR(k) grammar and quadratic time complexity for every unambiguous grammar. For general context free grammar, things are more ambiguous(pun intended): while subcubic algorithms exist(although with a large constant), all other general-purpose parsing algorithms(like Earley, GLR, GLL...) are indeed cubic, like ours.
Practically, this crate tries to make the engine be as efficient as possible for grammars used in practice. While many improvements, such as Earley sets compaction and lazy caching, have been made, this is inherently an ongoing process. If you find the engine is a bottleneck in your application, feel free to open an issue.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for kbnf-0.1.5-cp37-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 06d97a71466113b4a6e31e9059bd7682520e2134393e9a4c72807bf6e7df6616 |
|
MD5 | 8d33e4e3cc99121a5ec19c2795c13b47 |
|
BLAKE2b-256 | 293545c30a7419d5e8f453110e2aca542bdadc91db51d78466c9e85a47642fda |
Hashes for kbnf-0.1.5-cp37-abi3-musllinux_1_2_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9c804645309c97187466547d273ef71f9a830afc6b81bae3ad801ea7a3d1819d |
|
MD5 | 8a73fd24377ee6e1750538d96da8043c |
|
BLAKE2b-256 | fcb36102a6342f7071971e1da6078b2fac5d4411eddf8043984a228b314aa03f |
Hashes for kbnf-0.1.5-cp37-abi3-musllinux_1_2_armv7l.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 123b68712e64c7bfe7a630f5a03aba6a92cbb1022bd66e6d1f0c2c0d210a2288 |
|
MD5 | bf6b349b8642bdf7b156b7086e59aea8 |
|
BLAKE2b-256 | 2830bb3f34190176ce6845af11c641e4ed2ab7289dca3275e98ffd476840bed1 |
Hashes for kbnf-0.1.5-cp37-abi3-musllinux_1_2_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a3e5f0eed76b19aaaf7573ce7953886bb5bd957dab5b70f3cc270a9225888b8d |
|
MD5 | 31034644d783fc376bc3685726aadcc8 |
|
BLAKE2b-256 | ebc73ce2376e4c60239b29678e20ce657058a69918447099242a72770bf6fcbc |
Hashes for kbnf-0.1.5-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 453d1e1ab8bda37a2a23900915db8bdb7843697ccb2afc128833a355d2925fa5 |
|
MD5 | 6a72c8eab665b51fbb1163959c685bb7 |
|
BLAKE2b-256 | fcf546beeb20677af9a00c740770d281094bdc6644761db4c247ccccffc605d7 |
Hashes for kbnf-0.1.5-cp37-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1bc5572d788e24ff68ba28503da3bfa45423e27815bcec2bcb1f279858379d43 |
|
MD5 | 3af3cf3c71907602788b9f576a422ce8 |
|
BLAKE2b-256 | 4ff3b0e5a7e062984a67b613cedbe847418c2107f297819092df4d609c9e41e5 |
Hashes for kbnf-0.1.5-cp37-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 862ee7617113d4af6ce8aa5d7000ec5086e3bc3e161deb6461d3416d3c6beb78 |
|
MD5 | 4de02670cf17a6256f51cb8b1fd27ba2 |
|
BLAKE2b-256 | aaa07ab8617e2b6eda0ce492225199f65fe916f675df6be36be16b67f9c66047 |
Hashes for kbnf-0.1.5-cp37-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4c79cddfe7278591b121a8ae6944d90f5b7ec63b1fee294a035dce9562d1fe53 |
|
MD5 | aab4b66cb93b8815473614d85b9145d0 |
|
BLAKE2b-256 | 9300cea2c004e47f0ff1b2bb9664ba4550d5a1ef1faa3e9316a9c9fe568b2fc9 |
Hashes for kbnf-0.1.5-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1dfe2cbaa7ce795d42cbad55fe4c32dc8b8be25e81bcca99da478a17d87116b6 |
|
MD5 | 3e09bec9c028271bf905bb4927491b46 |
|
BLAKE2b-256 | 497671c102bcf34be210e4dbefc6a835b986fb018cfc40e8dc02ea61a2ce76e6 |
Hashes for kbnf-0.1.5-cp37-abi3-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8dd1727c5a1665337f5c931e1b40bc6ec484c1de916e88593ab1217b79eea9bd |
|
MD5 | c2c55ca6e46f887f93a6deb417181eea |
|
BLAKE2b-256 | 6b72354363e86b4c5108c439f2ee7d04b059acf86a86e8407c0e6a542e459172 |
Hashes for kbnf-0.1.5-cp37-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2720ac02509a9ed4d66cda111f0237672a6c7231d4cae2b15e6046998b197287 |
|
MD5 | be541754a491ff563dac9151982c3a14 |
|
BLAKE2b-256 | 5b0e34b8f0f2d9eea7ebcaafd1ddb698d57ec48420b3cd088cff7045742b23fb |