Outlines-core 0.1.0 发布:Rust 实现结构化生成

原标题:Releasing Outlines-core 0.1.0: structured generation in Rust and Python
68AI 研判分

dottxt 与 Hugging Face 联合发布 Outlines-core 0.1.0,将核心结构化生成算法移植至 Rust。该库旨在通过高性能解析器确保 LLM 输出严格符合 JSON 等预定义格式,解决 Python 版本在大规模部署中的性能瓶颈。开发者现可直接调用底层 Rust 接口或 Python 绑定以优化推理延迟。

关联知识实体

报道正文

本文目录10 个章节

返回文章列表

发布 Outlines-core 0.1.0:Rust 和 Python 中的结构化生成

发布于 2024 年 10 月 22 日 在 GitHub 上更新 点赞 45

bwillard bwillard dottxt David Holtz drbh Erik Kaunismäki erikkaum Kaustubh Chaudhari kc611 dottxt Remi Louf remi dottxt Umut Şahin umut-sahin dottxt Will Kurt willkurt dottxt

dottxt 和 Hugging Face 很高兴地宣布,我们一直在合作开发 outlines-core,这是 outlines 核心算法的 Rust 移植版本,用于实现结构化生成。除了通过 outlines 从 LLM 中获得可靠的输出外,这个 Rust 移植版本还为 outlines 用户提供了以下几项额外优势:

  • 速度:用户可以预期索引编译性能提升 2 倍。
  • 关注点分离:现在更容易将结构化生成功能集成到其他库中。outlines-core 非常轻量级。
  • 可移植性:核心算法采用 Rust 编写,使得为非 Python 语言提供绑定成为可能。

这些改进不仅提升了现有 outlines 用户的性能,还极大地扩展了用户在 LLM 工作流中集成结构化生成的方式。outlines-core 现已公开,并集成到 outlines 中,Python 绑定的 0.1.0 版本也已发布。你可以在 这里 找到代码仓库。

结构化生成简介 🧑‍🎓

工作原理

结构化生成意味着你的 LLM 保证遵循期望的格式。这可以是 JSON、Pydantic 模型、正则表达式或上下文无关文法。关键在于,结构化生成禁止生成“错误”的 token。

Let’s take an extremely simple example. The LLM should generate a boolean, “true” or “false”. And nothing more. For the sake of illustration, let’s say that LLMs generate characters instead of tokens. So the first character is ", we can just skip the forward pass. For the second, we don’t need to sample from all possible characters. The LLM should just choose between t or f.

After that, regardless of the path we take, there is only one valid next character. If the LLM chose t as the first character, then it has to follow with r, u and e. And similarly if it chose f it follows with a, l, s, e. And will choose the last " as the final character regardless of the path. There is of course more under the hood, for more in-depth coverage we recommend this dottxt blog and the associated paper on arxiv.

Why it’s important

It might not immediately be obvious how amazing structured generation can be. The first use-case many think of is “nice, now my LLM can return valid JSON, so I can treat it as an API and serialize/deserialize JSON reliably”. But that’s just scratching the surface. When you think about it, structure is everywhere, even in places where you least expect it like the GSM8K benchmark.

These are just a few examples of what structured generation enables:

And, perhaps more surprising, it reduces the sensitivity of evaluations to the specific prompt being used and the number of shots. Apart from the amazing tricks that structure gives you, it’s also more performant. The dottxt blog has many good articles with performance benchmarks.

Why rewrite in Rust? 🦀

Speed

Probably the first thing that comes to your mind when you hear “rewrite in Rust” is performance. And yes, that’s the case for outlines-core as well. Several key parts are yet to be moved over to Rust, and despite that, we already see an average 2x improvement in compilation speed.

Before the Rust port, Outlines used Numba to accelerate the building of the index. While Numba is fast (the runtime performance is comparable to Rust), the JIT-compilation of the Numba functions added a source of latency during the first run, which was a source of frustration for many users. Using Rust means we can compile the index building functions ahead of time, adding no latency during the first run. While this was not important in a production context (since the first run could anyways be done as part of deployment), it can make a huge difference during the experimentation phase!

Safety and Reliability

One of the main reasons for rewriting Outlines in Rust is the emphasis on safety and reliability that Rust brings to the table. Rust's strong static typing, combined with Rust's ownership model, eliminate entire classes of bugs, such as null pointer dereferences and data races in concurrent code. This leads to more robust and secure software.

In the context of Outlines, safety is crucial. Structured generation often involves complex data structures and manipulations, especially when dealing with high-performance inference engines. By leveraging Rust's safety guarantees, we reduce the risk of runtime errors and undefined behaviors that can arise from memory mismanagement.

Additionally, Rust's compile-time checks encourage developers to write cleaner and more maintainable code. This improves the current codebase and makes future development more efficient. New contributors can onboard more quickly, and the code is easier to audit and verify for correctness.

Separation of concerns

Outlines was designed to do more than providing the core algorithms for structured generation. Among other things, it includes integrations to other libraries like transformers which mean the library packs many dependencies. Separating the core algorithms from the Outlines library means that other libraries wishing to include structured generation can do so by importing a very lightweight library. So we can imagine in the near future libraries such as transformers and llama-cpp-python integrating structured generation directly. This allows the dottxt team to focus on the core algorithms.

Portability

Most of LLM training is written in Python, but inference is slightly different. It happens on many different devices, on specialized servers and is written in a range of programming languages. This is why portability also matters for structured generation. By having the core functionality of outlines written in rust, we can now create bindings to other languages.

For example, this port makes the integration into the text-generation-inference much smoother. TGI’s server logic is written in Rust, and we want to avoid having to call Python code as much as we possibly can. It also means libraries like mistral.rs or models implemented using candle can benefit from Outlines’s performance and capabilities.

In the future we plan to explore bindings to JS/TS, allowing outlines to be used in transformers-js. Or potentially Swift bindings, making outlines natively usable on Apple devices. But for now the focus is going to be on the Python bindings, and continuing to make outlines-core’s feature set complete by expanding support for the JSON Schema specification.

Contribute

Do you like working with structured generation, parsers, making LLMs output only valid JSON? Star the library, tweet about it, join in and contribute! Share your work on Twitter, and with dottxt’s and Hugging Face's community.