Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
R

rust-bert

> 数据库
Open source

Rust native ready-to-use NLP pipelines and transformer-based models (BERT, DistilBERT, GPT2,...)

3.1K stars0 likes2 views
WebsiteGitHub

About

Rust native ready-to-use NLP pipelines and transformer-based models (BERT, DistilBERT, GPT2,...)

rust-bert

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:

rust
    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:

  • Translation
  • Summarization
  • Multi-turn dialogue
  • Zero-shot classification
  • Sentiment Analysis
  • Named Entity Recognition
  • Part of Speech tagging
  • Question-Answering
  • Language Generation
  • Masked Language Model
  • Sentence Embeddings
  • Keywords extraction
Expand to display the supported models/tasks matrix
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 ✅

Getting started

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.

Manual installation (recommended)

  1. Download 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).
  2. Extract the library to a location of your choice
  3. Set the following environment variables
Linux:
bash
export LIBTORCH=/path/to/libtorch
export LD_LIBRARY_PATH=${LIBTORCH}/lib:$LD_LIBRARY_PATH
Windows
powershell
$Env:LIBTORCH = "X:\path\to\libtorch"
$Env:Path += ";X:\path\to\libtorch\lib"

macOS + Homebrew

bash
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_PATH

Automatic installation

Alternatively, 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.

Verifying installation

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:

bash
git clone [email protected]:guillaume-be/rust-bert.git
cd rust-bert
cargo run --example sentence_embeddings

ONNX Support (Optional)

ONNX 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.

  1. Enable the optional 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.
  2. The current recommended installation is to use dynamic linking by pointing to an existing library location. Use the load-dynamic cargo feature for ort.
  3. set the 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 project

Most 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

Issues· 76 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rustbartbertdeep-learningelectra

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category数据库
PricingOpen source

> Related tools

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库