Skip to main content

Faster backend for the `Outlines` library.

Project description

Faster-Outlines

Supercharge your structured text generation with faster-outlines - a high-
performance Rust backend for the Outlines library.

Overview

faster_outlines is designed to significantly boost the performance of regex-guided text generation, particularly for LLM inference servers. It's an ideal solution for scenarios where regex patterns for guiding LLM generation are not known in advance.

Key features:

  • 🚀 Seamless one-line integration with existing Outlines projects
  • 🚀 All the features you already love about outlines
  • ⚡ Asynchronous FSM compilation for immediate start of LLM inference
  • 🏎️ Substantial performance improvements, especially for complex regex patterns ( like JSON )
  • 🔄 Continuous updates to improve speed!

Upcoming:

  • 🍴 vLLM fork using faster_outlines
  • 🤝 Official integration with vLLM's main repo (hopefully)
  • Redis as a caching backend, for large inference setups

Why faster_outlines?

  1. Optimized for LLM Inference Servers: Ideal for scenarios where regex patterns are dynamic and not known beforehand.

  2. Asynchronous Processing: Unlike the standard Outlines library, faster_outlines allows you to start LLM inference immediately, without waiting for the entire FSM to compile.

  3. Significant Performance Boost: Especially noticeable with complex regex patterns and large state spaces.

  4. Seamless Integration: Works with your existing Outlines code with minimal changes.

Installation

pip install faster_outlines

Quick Start

Integrating faster_outlines into your project is as simple as adding one line of code:

import outlines
from faster_outlines import patch

patch(outlines)

# Now use outlines as you normally would
# Your code here...

You can also pass save_to_sys_modules=True to the patch function, in which case all normal outlines imports will use the modified / patched module.

from faster_outlines import patch
import outlines
patch(outlines)

from outlines_core.fsm.fsm import RegexFSM # Import as usual.

Example

import outlines
from faster_outlines import patch

patch(outlines)

model = outlines.models.transformers("mistralai/Mistral-7B-Instruct-v0.2", device="cuda:0", model_kwargs={"load_in_8bit": True})

schema = '''{
    "title": "Character",
    "type": "object",
    "properties": {
        "name": {
            "title": "Name",
            "maxLength": 10,
            "type": "string"
        },
        "age": {
            "title": "Age",
            "type": "integer"
        },
        "armor": {"$ref": "#/definitions/Armor"},
        "weapon": {"$ref": "#/definitions/Weapon"},
        "strength": {
            "title": "Strength",
            "type": "integer"
        }
    },
    "required": ["name", "age", "armor", "weapon", "strength"],
    "definitions": {
        "Armor": {
            "title": "Armor",
            "description": "An enumeration.",
            "enum": ["leather", "chainmail", "plate"],
            "type": "string"
        },
        "Weapon": {
            "title": "Weapon",
            "description": "An enumeration.",
            "enum": ["sword", "axe", "mace", "spear", "bow", "crossbow"],
            "type": "string"
        }
    }
}'''

model = outlines.models.transformers("mistralai/Mistral-7B-Instruct-v0.2", device="cuda:0", model_kwargs={"load_in_8bit": True})
print("Model loaded.")
generator = outlines.generate.json(model, schema)
character = generator("Give me a character description")
print(character)

Performance Comparison

Performance Graph

Latest as of 7.13.2024 (0.0.46)

However, if you would like to manually control the number of threads used, you can do so via environment variable:

export FASTER_OUTLINES_NUM_THREADS=<num-threads>

Please note that setting the number of threads to a number higher than the number of cores / logical threads on your machine WILL DETERIORATE PERFORMANCE, not improve it.

If you would like to test performance at different thread counts on your machine, you can use the script at tests/test_fsm_comp_time.py, by first running the script using the automatic thread count ( or what ever you are currently using ), and then the number of threads you are thinking of using.

Compatibility

faster_outlines is designed to be fully compatible with the Outlines API, however, currently only full support for version 0.0.46 ( latest as of 7/13/24 ) can be garunteed.

Contributing & Support

We welcome contributions!

If you would like to support the further development and more speed improvements for faster_outlines, please consider supporting us on Github sponsors, or make a donation using the Buy-Me-A-Coffee link below!

Issues

If you have an issue with the lib, please, please open a github issue describing how to reproduce it, and we will be sure to work on fixing it.

Acknowledgments

  • This project builds upon the excellent work of the Outlines library.

Citations:

@article{willard2023efficient,
  title={Efficient Guided Generation for LLMs},
  author={Willard, Brandon T and Louf, R{\'e}mi},
  journal={arXiv preprint arXiv:2307.09702},
  year={2023}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

faster_outlines-9.18.2024-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl (298.4 kB view hashes)

Uploaded PyPy manylinux: glibc 2.28+ x86-64

faster_outlines-9.18.2024-pp310-pypy310_pp73-manylinux_2_28_aarch64.whl (283.4 kB view hashes)

Uploaded PyPy manylinux: glibc 2.28+ ARM64

faster_outlines-9.18.2024-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl (298.6 kB view hashes)

Uploaded PyPy manylinux: glibc 2.28+ x86-64

faster_outlines-9.18.2024-pp39-pypy39_pp73-manylinux_2_28_aarch64.whl (283.5 kB view hashes)

Uploaded PyPy manylinux: glibc 2.28+ ARM64

faster_outlines-9.18.2024-cp313-cp313-manylinux_2_28_x86_64.whl (297.3 kB view hashes)

Uploaded CPython 3.13 manylinux: glibc 2.28+ x86-64

faster_outlines-9.18.2024-cp313-cp313-manylinux_2_28_aarch64.whl (282.8 kB view hashes)

Uploaded CPython 3.13 manylinux: glibc 2.28+ ARM64

faster_outlines-9.18.2024-cp312-cp312-manylinux_2_28_x86_64.whl (297.3 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.28+ x86-64

faster_outlines-9.18.2024-cp312-cp312-manylinux_2_28_aarch64.whl (282.8 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.28+ ARM64

faster_outlines-9.18.2024-cp311-cp311-manylinux_2_28_x86_64.whl (297.8 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.28+ x86-64

faster_outlines-9.18.2024-cp311-cp311-manylinux_2_28_aarch64.whl (283.1 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.28+ ARM64

faster_outlines-9.18.2024-cp310-cp310-manylinux_2_28_x86_64.whl (298.2 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.28+ x86-64

faster_outlines-9.18.2024-cp310-cp310-manylinux_2_28_aarch64.whl (283.4 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.28+ ARM64

faster_outlines-9.18.2024-cp39-cp39-manylinux_2_28_x86_64.whl (298.6 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.28+ x86-64

faster_outlines-9.18.2024-cp39-cp39-manylinux_2_28_aarch64.whl (284.2 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.28+ ARM64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page