kbnf


Namekbnf JSON
Version 0.4.0 PyPI version JSON
download
home_pageNone
SummaryA fast constrained decoding engine based on context free grammar.
upload_time2024-11-08 03:12:18
maintainerNone
docs_urlNone
authorXintong Sun <xs28@rice.edu>
requires_python>=3.7
licenseMIT OR Apache-2.0
keywords deep-learning language-model guided-generation structured constrained-decoding
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # kbnf

[![crates.io](https://img.shields.io/crates/v/kbnf)](https://crates.io/crates/kbnf)
[![docs.rs](https://docs.rs/kbnf/badge.svg)](https://docs.rs/kbnf)
[![PyPI](https://img.shields.io/pypi/v/kbnf.svg)](https://pypi.python.org/pypi/kbnf)
![CI](https://github.com/Dan-wanna-M/kbnf/actions/workflows/CI.yml/badge.svg)

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.

If you are interested in the design and implementation behind this crate, you may want to check out [my blog](https://dan-wanna-m.github.io/blog/).

## Features

- Supports full context free grammar with worst case O(m\*n^3) time complexity, where `n` is the generated text length and `m` 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.

## Documentation

[Documentation and examples](https://docs.rs/kbnf/).

## 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](https://github.com/Dan-wanna-M/blog/issues/new).


            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "kbnf",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": null,
    "keywords": "deep-learning, language-model, guided-generation, structured, constrained-decoding",
    "author": "Xintong Sun <xs28@rice.edu>",
    "author_email": "Xintong Sun <xs28@rice.edu>",
    "download_url": "https://files.pythonhosted.org/packages/02/50/a0d1f7cba74065d799f2253223121d908a8b7da5912d42357331b0897681/kbnf-0.4.0.tar.gz",
    "platform": null,
    "description": "# kbnf\n\n[![crates.io](https://img.shields.io/crates/v/kbnf)](https://crates.io/crates/kbnf)\n[![docs.rs](https://docs.rs/kbnf/badge.svg)](https://docs.rs/kbnf)\n[![PyPI](https://img.shields.io/pypi/v/kbnf.svg)](https://pypi.python.org/pypi/kbnf)\n![CI](https://github.com/Dan-wanna-M/kbnf/actions/workflows/CI.yml/badge.svg)\n\nThis 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.\n\nIf you are interested in the design and implementation behind this crate, you may want to check out [my blog](https://dan-wanna-m.github.io/blog/).\n\n## Features\n\n- Supports full context free grammar with worst case O(m\\*n^3) time complexity, where `n` is the generated text length and `m` is the vocabulary size.\n- Asymptotically fastest for subclasses of context free grammar.\n  - Guarantees worst case O(m*n) time complexity for every LR(k) grammar(which includes almost all practical grammars)\n  - Achieves O(n) time complexity with caching eventually given that `n` has a fixed upper bound, or the grammar is regular.\n- Vocabulary-independent.\n  - BPE, BBPE, you-name-it, all types of vocabulary are supported.\n- Supports UTF-8 characters in grammar.\n- Embeddable regular expressions.\n\n## Documentation\n\n[Documentation and examples](https://docs.rs/kbnf/).\n\n## Add to your project\n\nSimply add it to your `Cargo.toml` or run `cargo add kbnf` in your command line.\n\n## Performance\n\nOne of the goals of this crate is for the constrained decoding engine to be \"fast.\" This can be interpreted both theoretically and practically.\n\nTheoretically, 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.\n\nPractically, 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](https://github.com/Dan-wanna-M/blog/issues/new).\n\n",
    "bugtrack_url": null,
    "license": "MIT OR Apache-2.0",
    "summary": "A fast constrained decoding engine based on context free grammar.",
    "version": "0.4.0",
    "project_urls": {
        "Source Code": "https://github.com/Dan-Wanna-M/kbnf"
    },
    "split_keywords": [
        "deep-learning",
        " language-model",
        " guided-generation",
        " structured",
        " constrained-decoding"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f867e330d762d4eab4ad3ba5ab43c990c88eda9e83d3ddb79fda5f6409513b89",
                "md5": "f5df2d4af0246e29c34156ba0854e986",
                "sha256": "953d14bed99b75394e6e26cf69ed43a9902791734f46d7fbd1cc8dae8284ece4"
            },
            "downloads": -1,
            "filename": "kbnf-0.4.0-cp37-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl",
            "has_sig": false,
            "md5_digest": "f5df2d4af0246e29c34156ba0854e986",
            "packagetype": "bdist_wheel",
            "python_version": "cp37",
            "requires_python": ">=3.7",
            "size": 2044013,
            "upload_time": "2024-11-08T03:11:59",
            "upload_time_iso_8601": "2024-11-08T03:11:59.509371Z",
            "url": "https://files.pythonhosted.org/packages/f8/67/e330d762d4eab4ad3ba5ab43c990c88eda9e83d3ddb79fda5f6409513b89/kbnf-0.4.0-cp37-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6a39d0dc383d40beee817989eabacb810a77e936123e6a01d489d6d2027fdf43",
                "md5": "5348a570e283a79985e7f8c8454a0266",
                "sha256": "1be1392bdc6c9f4346b9cccbfbd3b29c45b496963f1b8a872150ed7317457ac9"
            },
            "downloads": -1,
            "filename": "kbnf-0.4.0-cp37-abi3-macosx_11_0_arm64.whl",
            "has_sig": false,
            "md5_digest": "5348a570e283a79985e7f8c8454a0266",
            "packagetype": "bdist_wheel",
            "python_version": "cp37",
            "requires_python": ">=3.7",
            "size": 991941,
            "upload_time": "2024-11-08T03:12:01",
            "upload_time_iso_8601": "2024-11-08T03:12:01.748615Z",
            "url": "https://files.pythonhosted.org/packages/6a/39/d0dc383d40beee817989eabacb810a77e936123e6a01d489d6d2027fdf43/kbnf-0.4.0-cp37-abi3-macosx_11_0_arm64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4f46d301adeaf71b8e5058b56b65196241430c6dd37bd748dd0607da3c519663",
                "md5": "4c9a439175b5360540fe57b7c3194ba1",
                "sha256": "9147ddb9dea2c1b99ef694ed340405b25742e192d81ec6c5b3b149bda40a9f4b"
            },
            "downloads": -1,
            "filename": "kbnf-0.4.0-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl",
            "has_sig": false,
            "md5_digest": "4c9a439175b5360540fe57b7c3194ba1",
            "packagetype": "bdist_wheel",
            "python_version": "cp37",
            "requires_python": ">=3.7",
            "size": 1084712,
            "upload_time": "2024-11-08T03:12:02",
            "upload_time_iso_8601": "2024-11-08T03:12:02.957208Z",
            "url": "https://files.pythonhosted.org/packages/4f/46/d301adeaf71b8e5058b56b65196241430c6dd37bd748dd0607da3c519663/kbnf-0.4.0-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6c0f606c872485dd8460b50cb02490fe75851ad27de43c9fcf80e9f1bd798946",
                "md5": "ef7877c7fe484c4debf41faec8fbee3f",
                "sha256": "862bcd9088e79826156bb598da78c1b945ae610eb709e2142edea76237a1202b"
            },
            "downloads": -1,
            "filename": "kbnf-0.4.0-cp37-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl",
            "has_sig": false,
            "md5_digest": "ef7877c7fe484c4debf41faec8fbee3f",
            "packagetype": "bdist_wheel",
            "python_version": "cp37",
            "requires_python": ">=3.7",
            "size": 1118623,
            "upload_time": "2024-11-08T03:12:04",
            "upload_time_iso_8601": "2024-11-08T03:12:04.910651Z",
            "url": "https://files.pythonhosted.org/packages/6c/0f/606c872485dd8460b50cb02490fe75851ad27de43c9fcf80e9f1bd798946/kbnf-0.4.0-cp37-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "80455b52d137d5f28f954c39749afb8f3560d3306029db4db45e4555429c9daa",
                "md5": "ef970d530d66c88565309d3caf8cb165",
                "sha256": "d66704f25ba09a858084118cb4102930ee7792b6e2b10dcb0c5b62e3965bc8ad"
            },
            "downloads": -1,
            "filename": "kbnf-0.4.0-cp37-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl",
            "has_sig": false,
            "md5_digest": "ef970d530d66c88565309d3caf8cb165",
            "packagetype": "bdist_wheel",
            "python_version": "cp37",
            "requires_python": ">=3.7",
            "size": 1224156,
            "upload_time": "2024-11-08T03:12:06",
            "upload_time_iso_8601": "2024-11-08T03:12:06.061299Z",
            "url": "https://files.pythonhosted.org/packages/80/45/5b52d137d5f28f954c39749afb8f3560d3306029db4db45e4555429c9daa/kbnf-0.4.0-cp37-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5506ac7a89ac2128f8ec69fac3921e5623f99e22ebeae6a8e9f278e86a784f7b",
                "md5": "e3f2bc45905de37c3a4a17e6bd18c75c",
                "sha256": "e86ee5876cecf7376bcf2214cb18c1401fdf6630e6866273a8270f1d3b9182bf"
            },
            "downloads": -1,
            "filename": "kbnf-0.4.0-cp37-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl",
            "has_sig": false,
            "md5_digest": "e3f2bc45905de37c3a4a17e6bd18c75c",
            "packagetype": "bdist_wheel",
            "python_version": "cp37",
            "requires_python": ">=3.7",
            "size": 1329677,
            "upload_time": "2024-11-08T03:12:08",
            "upload_time_iso_8601": "2024-11-08T03:12:08.575886Z",
            "url": "https://files.pythonhosted.org/packages/55/06/ac7a89ac2128f8ec69fac3921e5623f99e22ebeae6a8e9f278e86a784f7b/kbnf-0.4.0-cp37-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "abcc78f66b1294213df38c4dcd8d5887eb72c12cf9d98a69db63f2f90fb82f9f",
                "md5": "eb31c126ee32d997727cef671bb8f289",
                "sha256": "0511038b05bc6f9a683e2316b671b4380ec8886fca5ea79ca3286fde1110729c"
            },
            "downloads": -1,
            "filename": "kbnf-0.4.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
            "has_sig": false,
            "md5_digest": "eb31c126ee32d997727cef671bb8f289",
            "packagetype": "bdist_wheel",
            "python_version": "cp37",
            "requires_python": ">=3.7",
            "size": 1184948,
            "upload_time": "2024-11-08T03:12:10",
            "upload_time_iso_8601": "2024-11-08T03:12:10.429124Z",
            "url": "https://files.pythonhosted.org/packages/ab/cc/78f66b1294213df38c4dcd8d5887eb72c12cf9d98a69db63f2f90fb82f9f/kbnf-0.4.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f11beb8e8d190046a6be5cf42680f24fa7b77fbae63d81924fe311d6584e31b5",
                "md5": "063c55cd450c53bae212d4bc08e0b038",
                "sha256": "6b4885558948ed8781d9cd73d793668ec745dbf8fbcb0d7c8942d97d8a199e65"
            },
            "downloads": -1,
            "filename": "kbnf-0.4.0-cp37-abi3-musllinux_1_2_aarch64.whl",
            "has_sig": false,
            "md5_digest": "063c55cd450c53bae212d4bc08e0b038",
            "packagetype": "bdist_wheel",
            "python_version": "cp37",
            "requires_python": ">=3.7",
            "size": 1261767,
            "upload_time": "2024-11-08T03:12:11",
            "upload_time_iso_8601": "2024-11-08T03:12:11.749462Z",
            "url": "https://files.pythonhosted.org/packages/f1/1b/eb8e8d190046a6be5cf42680f24fa7b77fbae63d81924fe311d6584e31b5/kbnf-0.4.0-cp37-abi3-musllinux_1_2_aarch64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4fa2fa783a4ebe488ef7451236e7dfe1e74f57cee248fa3b20ba9ebec15cf841",
                "md5": "7f025fb551c1bd65369d8cb83e90d1f0",
                "sha256": "17f68b327c9c2a9a1d356fcf93ce1ad818fb523bf212a814f4bc888f59e7d3ac"
            },
            "downloads": -1,
            "filename": "kbnf-0.4.0-cp37-abi3-musllinux_1_2_armv7l.whl",
            "has_sig": false,
            "md5_digest": "7f025fb551c1bd65369d8cb83e90d1f0",
            "packagetype": "bdist_wheel",
            "python_version": "cp37",
            "requires_python": ">=3.7",
            "size": 1355647,
            "upload_time": "2024-11-08T03:12:13",
            "upload_time_iso_8601": "2024-11-08T03:12:13.590204Z",
            "url": "https://files.pythonhosted.org/packages/4f/a2/fa783a4ebe488ef7451236e7dfe1e74f57cee248fa3b20ba9ebec15cf841/kbnf-0.4.0-cp37-abi3-musllinux_1_2_armv7l.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "48bfa81b70fc4a91c7e206db1a80f1a1c1efbfa92fe1b80b5678351530c6a2ba",
                "md5": "0e572aa2c36532249f02f63f9ea87801",
                "sha256": "40123c923d73764f3e592c7bfca6047edce89713e260c38714d86259bedd4202"
            },
            "downloads": -1,
            "filename": "kbnf-0.4.0-cp37-abi3-musllinux_1_2_x86_64.whl",
            "has_sig": false,
            "md5_digest": "0e572aa2c36532249f02f63f9ea87801",
            "packagetype": "bdist_wheel",
            "python_version": "cp37",
            "requires_python": ">=3.7",
            "size": 1281524,
            "upload_time": "2024-11-08T03:12:14",
            "upload_time_iso_8601": "2024-11-08T03:12:14.766742Z",
            "url": "https://files.pythonhosted.org/packages/48/bf/a81b70fc4a91c7e206db1a80f1a1c1efbfa92fe1b80b5678351530c6a2ba/kbnf-0.4.0-cp37-abi3-musllinux_1_2_x86_64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a5627d30e54da6d9d189b03df5f7e0e0e86323bf75e429e3c1d0b3ef45085018",
                "md5": "9ed55355f59badd9bbcd0f98e528b139",
                "sha256": "fd9756f95ac154ed24d682704640cb401c16ee59fbe0d82d871b2debb01be8e8"
            },
            "downloads": -1,
            "filename": "kbnf-0.4.0-cp37-abi3-win32.whl",
            "has_sig": false,
            "md5_digest": "9ed55355f59badd9bbcd0f98e528b139",
            "packagetype": "bdist_wheel",
            "python_version": "cp37",
            "requires_python": ">=3.7",
            "size": 856304,
            "upload_time": "2024-11-08T03:12:16",
            "upload_time_iso_8601": "2024-11-08T03:12:16.616088Z",
            "url": "https://files.pythonhosted.org/packages/a5/62/7d30e54da6d9d189b03df5f7e0e0e86323bf75e429e3c1d0b3ef45085018/kbnf-0.4.0-cp37-abi3-win32.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "641ebde027fb4ecd66ec5dd3709a2211d48a064ec76f7827420db1ebf6ccb219",
                "md5": "7cb33d50d5cbc8c844d3a1ee55411b95",
                "sha256": "103877625ee4d2e3b2a6030fd2e39172570427eae68e8846feee27663e22129c"
            },
            "downloads": -1,
            "filename": "kbnf-0.4.0-cp37-abi3-win_amd64.whl",
            "has_sig": false,
            "md5_digest": "7cb33d50d5cbc8c844d3a1ee55411b95",
            "packagetype": "bdist_wheel",
            "python_version": "cp37",
            "requires_python": ">=3.7",
            "size": 898038,
            "upload_time": "2024-11-08T03:12:17",
            "upload_time_iso_8601": "2024-11-08T03:12:17.758001Z",
            "url": "https://files.pythonhosted.org/packages/64/1e/bde027fb4ecd66ec5dd3709a2211d48a064ec76f7827420db1ebf6ccb219/kbnf-0.4.0-cp37-abi3-win_amd64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0250a0d1f7cba74065d799f2253223121d908a8b7da5912d42357331b0897681",
                "md5": "449d0c0b58356179a883a22fcc7f0f11",
                "sha256": "361559a6fa9bcf3c266a8150c8032889693397fbd8735c67841d6c840ec27b39"
            },
            "downloads": -1,
            "filename": "kbnf-0.4.0.tar.gz",
            "has_sig": false,
            "md5_digest": "449d0c0b58356179a883a22fcc7f0f11",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 1023505,
            "upload_time": "2024-11-08T03:12:18",
            "upload_time_iso_8601": "2024-11-08T03:12:18.917018Z",
            "url": "https://files.pythonhosted.org/packages/02/50/a0d1f7cba74065d799f2253223121d908a8b7da5912d42357331b0897681/kbnf-0.4.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-08 03:12:18",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Dan-Wanna-M",
    "github_project": "kbnf",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "kbnf"
}
        
Elapsed time: 0.40754s