Transformers vs rust-bert: NLP Models Compared
The Python Transformers ecosystem made pretrained NLP models widely accessible. rust-bert provides Rust implementations and bindings for transformer-based models, enabling tasks like classification, embeddings, and generation in Rust applications and notebooks.
What Transformers does
Transformers provides access to pretrained transformer models:
- Pretrained models (BERT, GPT, T5, etc.)
- Tokenization and preprocessing
- Model fine-tuning for custom tasks
- Pipeline APIs for common NLP tasks
- Integration with PyTorch and TensorFlow
- Hub sharing for model distribution
It became the default library for NLP in Python, democratizing access to state-of-the-art transformer models.
What rust-bert brings to Rust
rust-bert provides Rust implementations of transformer models:
- BERT models for classification and embeddings
- Tokenization with Rust implementations
- Inference APIs for pretrained models
- Type-safe tensor operations
- Performance via native compilation
- Zero-copy operations where possible
The key difference: rust-bert allows you to use transformer models from Rust, combining state-of-the-art NLP capabilities with Rust's type safety and performance.
Comparison
| Feature | Transformers (Python) | rust-bert (Rust) |
|---|---|---|
| Load model | AutoModel.from_pretrained('bert') | BertModel::load('bert-base-uncased') |
| Tokenization | AutoTokenizer.from_pretrained('bert') | Rust tokenizer implementation |
| Inference | model(input_ids) | model.forward(&input_ids) |
| Type safety | Runtime | Compile-time |
| Performance | Python + backend | Native compilation |
| Integration | PyTorch, TensorFlow, etc. | tch-rs, ndarray, etc. |
Example: Transformers
from transformers import AutoModel, AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
model = AutoModel.from_pretrained('bert-base-uncased')
inputs = tokenizer("Hello world", return_tensors="pt")
outputs = model(**inputs)
Example: rust-bert
use rust_bert::BertModel;
let model = BertModel::load("bert-base-uncased")?;
let inputs = tokenizer.encode("Hello world");
let outputs = model.forward(&inputs)?;
When to use each
- Transformers in Python when you need the full Hugging Face ecosystem, access to thousands of models, or integration with PyTorch and TensorFlow.
- rust-bert in Rust when you want to use transformer models from Rust, combine them with Rust's type system, or deploy NLP models in Rust applications.
Try it in Project Mars
Both runtimes share one .mars document: Python cells run in a persistent interpreter, Rust cells on the replay runtime with // :dep Cargo dependencies — and state stays inside each language's own session.
NLP in a notebook — a pretrained pipeline call in Python, and the token-counting step rust-bert wraps, in Rust:
# Transformers side: a pretrained pipeline (downloads weights once).
from transformers import pipeline
classifier = pipeline("sentiment-analysis")
classifier("Project Mars makes notebooks reproducible.")
// rust-bert handles tokenization + the transformer itself;
// the input side is just text → token ids. Word counts first:
let text = "mars notebooks are reproducible";
let mut counts = std::collections::HashMap::new();
for word in text.split_whitespace() {
*counts.entry(word).or_insert(0usize) += 1;
}
println!("token histogram: {counts:?}");
How Project Mars supports both
Project Mars supports both Transformers and rust-bert in their respective language runtimes:
- Python cells can use Transformers with standard library imports
- Rust cells can use rust-bert via Cargo dependencies
- Both can coexist in the same
.marsnotebook - Models and tokenizers persist across cells within each language runtime
Start with the getting started guide to explore Transformers and rust-bert in Project Mars notebooks.