Rust native ready-to-use NLP pipelines and transformer-based models (BERT, DistilBERT, GPT2,...)
Rust native ready-to-use NLP pipelines and transformer-based models (BERT, DistilBERT, GPT2,...)
Rust-native state-of-the-art Natural Language Processing models and pipelines. Port of Hugging Face's Transformers library, using tch-rs or onnxruntime bindings and pre-processing from rust-tokenizers. Supports multi-threaded tokenization and GPU inference. This repository exposes the model base architecture, task-specific heads (see below) and ready-to-use pipelines. Benchmarks are available at the end of this document.
Get started with tasks including question answering, named entity recognition, translation, summarization, text generation, conversational agents and more in just a few lines of code:
let qa_model = QuestionAnsweringModel::new(Default::default ()) ?;
let question = String::from("Where does Amy live ?");
let context = String::from("Amy lives in Amsterdam");
let answers = qa_model.predict( & [QaInput { question, context }], 1, 32);Output:
[Answer { score: 0.9976, start: 13, end: 21, answer: "Amsterdam" }]The tasks currently supported include:
| Sequence classification | Token classification | Question answering | Text Generation | Summarization | Translation | Masked LM | Sentence Embeddings | |
|---|---|---|---|---|---|---|---|---|
| DistilBERT | ✅ | ✅ | ✅ | ✅ | ✅ | |||
| MobileBERT | ✅ | ✅ | ✅ | ✅ | ||||
| DeBERTa | ✅ | ✅ | ✅ | ✅ | ||||
| DeBERTa (v2) | ✅ | ✅ | ✅ | ✅ | ||||
| FNet | ✅ | ✅ | ✅ | ✅ | ||||
| BERT | ✅ | ✅ | ✅ | ✅ | ✅ | |||
| RoBERTa | ✅ | ✅ | ✅ | ✅ | ✅ | |||
| GPT | ✅ | |||||||
| GPT2 | ✅ | |||||||
| GPT-Neo | ✅ | |||||||
| GPT-J | ✅ | |||||||
| BART | ✅ | ✅ | ✅ | |||||
| Marian | ✅ | |||||||
| MBart | ✅ | ✅ | ||||||
| M2M100 | ✅ | |||||||
| NLLB | ✅ | |||||||
| Electra | ✅ | ✅ | ||||||
| ALBERT | ✅ | ✅ | ✅ | ✅ | ✅ | |||
| T5 | ✅ | ✅ | ✅ | ✅ | ||||
| LongT5 | ✅ | ✅ | ||||||
| XLNet | ✅ | ✅ | ✅ | ✅ | ✅ | |||
| Reformer | ✅ | ✅ | ✅ | ✅ | ||||
| ProphetNet | ✅ | ✅ | ||||||
| Longformer | ✅ | ✅ | ✅ | ✅ | ||||
| Pegasus | ✅ |
This library relies on the tch crate for bindings to the C++ Libtorch API. The libtorch library is required can be downloaded either automatically or manually. The following provides a reference on how to set-up your environment to use these bindings, please refer to the tch for detailed information or support.
Furthermore, this library relies on a cache folder for downloading pre-trained
models. This cache location defaults to ~/.cache/.rustbert, but can be changed
by setting the RUSTBERT_CACHE environment variable. Note that the language
models used by this library are in the order of the 100s of MBs to GBs.
libtorch from https://pytorch.org/get-started/locally/. This
package requires v2.4: if this version is no longer available on the "get
started" page, the file should be accessible by modifying the target link,
for example
https://download.pytorch.org/libtorch/cu124/libtorch-cxx11-abi-shared-with-deps-2.4.0%2Bcu124.zip
for a Linux version with CUDA12. NOTE: When using rust-bert as
dependency from crates.io, please check the required
LIBTORCH on the published package
readme as it may differ from the
version documented here (applying to the current repository version).export LIBTORCH=/path/to/libtorch
export LD_LIBRARY_PATH=${LIBTORCH}/lib:$LD_LIBRARY_PATH$Env:LIBTORCH = "X:\path\to\libtorch"
$Env:Path += ";X:\path\to\libtorch\lib"brew install pytorch jq
export LIBTORCH=$(brew --cellar pytorch)/$(brew info --json pytorch | jq -r '.[0].installed[0].version')
export LD_LIBRARY_PATH=${LIBTORCH}/lib:$LD_LIBRARY_PATHAlternatively, you can let the build script automatically download the
libtorch library for you. The download-libtorch feature flag needs to be
enabled. The CPU version of libtorch will be downloaded by default. To download
a CUDA version, please set the environment variable TORCH_CUDA_VERSION to
cu124. Note that the libtorch library is large (order of several GBs for the
CUDA-enabled version) and the first build may therefore take several minutes to
complete.
Verify your installation (and linking with libtorch) by adding the rust-bert
dependency to your Cargo.toml or by cloning the rust-bert source and running
an example:
git clone [email protected]:guillaume-be/rust-bert.git
cd rust-bert
cargo run --example sentence_embeddingsONNX support can be enabled via the optional onnx feature. This crate then
leverages the ort crate with bindings to the
onnxruntime C++ library. We refer the user to this page project for further
installation instructions/support.
onnx feature. The rust-bert crate does not include
any optional dependencies for ort, the end user should select the set of
features that would be adequate for pulling the required onnxruntime C++
library.load-dynamic cargo feature for ort.ORT_DYLIB_PATH to point to the location of downloaded onnxruntime
library (onnxruntime.dll/libonnxruntime.so/libonnxruntime.dylib
depending on the operating system). These can be downloaded from the
release page of the
onnxruntime projectMost architectures (including encoders, decoders and encoder-decoders) are supported. the library aims at keeping compatibility with models exported using the Optimum library. A detailed guide on how to export a Transformer model to ONNX using Optimum is available at https://huggingface.co/docs/optimum/main/en/exporters/onnx/usage_guides/export_a_model The resources used to create ONNX models are similar to those based on Pytorch, replacing the pytorch by the ONNX model. Since ONNX models are less flexible than their Pytorch counterparts in the handling of optional arguments, exporting a decoder or encoder-decoder model to ONNX will usually result in multiple files. These files are expected (but not all are necessary) for use in this library as per the table below:
| Architecture | Encoder file | Decoder without past file | Decoder with past file |
|---|---|---|---|
| Encoder (e.g. BERT) | required | not used | not used |
No open issues yet, or sync has not completed.