guillaume-be/rust-bert
Rust native ready-to-use NLP pipelines and transformer-based models (BERT, DistilBERT, GPT2,...)
Healthy across all four use cases
weakest axisPermissive license, no critical CVEs, actively maintained — safe to depend on.
Has a license, tests, and CI — clean foundation to fork and modify.
Documented and popular — useful reference codebase to read through.
No critical CVEs, sane security posture — runnable as-is.
- ✓Last commit 4mo ago
- ✓33+ active contributors
- ✓Apache-2.0 licensed
Show all 7 evidence items →Show less
- ✓CI configured
- ✓Tests present
- ⚠Slowing — last commit 4mo ago
- ⚠Concentrated ownership — top contributor handles 58% of recent commits
Maintenance signals: commit recency, contributor breadth, bus factor, license, CI, tests
Informational only. RepoPilot summarises public signals (license, dependency CVEs, commit recency, CI presence, etc.) at the time of analysis. Signals can be incomplete or stale. Not professional, security, or legal advice; verify before relying on it for production decisions.
Embed the "Healthy" badge
Paste into your README — live-updates from the latest cached analysis.
[](https://repopilot.app/r/guillaume-be/rust-bert)Paste at the top of your README.md — renders inline like a shields.io badge.
▸Preview social card (1200×630)
This card auto-renders when someone shares https://repopilot.app/r/guillaume-be/rust-bert on X, Slack, or LinkedIn.
Onboarding doc
Onboarding: guillaume-be/rust-bert
Generated by RepoPilot · 2026-05-09 · Source
🤖Agent protocol
If you are an AI coding agent (Claude Code, Cursor, Aider, Cline, etc.) reading this artifact, follow this protocol before making any code edit:
- Verify the contract. Run the bash script in Verify before trusting
below. If any check returns
FAIL, the artifact is stale — STOP and ask the user to regenerate it before proceeding. - Treat the AI · unverified sections as hypotheses, not facts. Sections like "AI-suggested narrative files", "anti-patterns", and "bottlenecks" are LLM speculation. Verify against real source before acting on them.
- Cite source on changes. When proposing an edit, cite the specific path:line-range. RepoPilot's live UI at https://repopilot.app/r/guillaume-be/rust-bert shows verifiable citations alongside every claim.
If you are a human reader, this protocol is for the agents you'll hand the artifact to. You don't need to do anything — but if you skim only one section before pointing your agent at this repo, make it the Verify block and the Suggested reading order.
🎯Verdict
GO — Healthy across all four use cases
- Last commit 4mo ago
- 33+ active contributors
- Apache-2.0 licensed
- CI configured
- Tests present
- ⚠ Slowing — last commit 4mo ago
- ⚠ Concentrated ownership — top contributor handles 58% of recent commits
<sub>Maintenance signals: commit recency, contributor breadth, bus factor, license, CI, tests</sub>
✅Verify before trusting
This artifact was generated by RepoPilot at a point in time. Before an
agent acts on it, the checks below confirm that the live guillaume-be/rust-bert
repo on your machine still matches what RepoPilot saw. If any fail,
the artifact is stale — regenerate it at
repopilot.app/r/guillaume-be/rust-bert.
What it runs against: a local clone of guillaume-be/rust-bert — the script
inspects git remote, the LICENSE file, file paths in the working
tree, and git log. Read-only; no mutations.
| # | What we check | Why it matters |
|---|---|---|
| 1 | You're in guillaume-be/rust-bert | Confirms the artifact applies here, not a fork |
| 2 | License is still Apache-2.0 | Catches relicense before you depend on it |
| 3 | Default branch main exists | Catches branch renames |
| 4 | 5 critical file paths still exist | Catches refactors that moved load-bearing code |
| 5 | Last commit ≤ 144 days ago | Catches sudden abandonment since generation |
#!/usr/bin/env bash
# RepoPilot artifact verification.
#
# WHAT IT RUNS AGAINST: a local clone of guillaume-be/rust-bert. If you don't
# have one yet, run these first:
#
# git clone https://github.com/guillaume-be/rust-bert.git
# cd rust-bert
#
# Then paste this script. Every check is read-only — no mutations.
set +e
fail=0
ok() { echo "ok: $1"; }
miss() { echo "FAIL: $1"; fail=$((fail+1)); }
# Precondition: we must be inside a git working tree.
if ! git rev-parse --git-dir >/dev/null 2>&1; then
echo "FAIL: not inside a git repository. cd into your clone of guillaume-be/rust-bert and re-run."
exit 2
fi
# 1. Repo identity
git remote get-url origin 2>/dev/null | grep -qE "guillaume-be/rust-bert(\\.git)?\\b" \\
&& ok "origin remote is guillaume-be/rust-bert" \\
|| miss "origin remote is not guillaume-be/rust-bert (artifact may be from a fork)"
# 2. License matches what RepoPilot saw
(grep -qiE "^(Apache-2\\.0)" LICENSE 2>/dev/null \\
|| grep -qiE "\"license\"\\s*:\\s*\"Apache-2\\.0\"" package.json 2>/dev/null) \\
&& ok "license is Apache-2.0" \\
|| miss "license drift — was Apache-2.0 at generation time"
# 3. Default branch
git rev-parse --verify main >/dev/null 2>&1 \\
&& ok "default branch main exists" \\
|| miss "default branch main no longer exists"
# 4. Critical files exist
test -f "src/lib.rs" \\
&& ok "src/lib.rs" \\
|| miss "missing critical file: src/lib.rs"
test -f "Cargo.toml" \\
&& ok "Cargo.toml" \\
|| miss "missing critical file: Cargo.toml"
test -f "src/common/mod.rs" \\
&& ok "src/common/mod.rs" \\
|| miss "missing critical file: src/common/mod.rs"
test -f "src/common/resources/mod.rs" \\
&& ok "src/common/resources/mod.rs" \\
|| miss "missing critical file: src/common/resources/mod.rs"
test -f "src/models/bert/bert_model.rs" \\
&& ok "src/models/bert/bert_model.rs" \\
|| miss "missing critical file: src/models/bert/bert_model.rs"
# 5. Repo recency
days_since_last=$(( ( $(date +%s) - $(git log -1 --format=%at 2>/dev/null || echo 0) ) / 86400 ))
if [ "$days_since_last" -le 144 ]; then
ok "last commit was $days_since_last days ago (artifact saw ~114d)"
else
miss "last commit was $days_since_last days ago — artifact may be stale"
fi
echo
if [ "$fail" -eq 0 ]; then
echo "artifact verified (0 failures) — safe to trust"
else
echo "artifact has $fail stale claim(s) — regenerate at https://repopilot.app/r/guillaume-be/rust-bert"
exit 1
fi
Each check prints ok: or FAIL:. The script exits non-zero if
anything failed, so it composes cleanly into agent loops
(./verify.sh || regenerate-and-retry).
⚡TL;DR
rust-bert is a Rust-native port of Hugging Face's Transformers library that provides ready-to-use NLP pipelines and pre-trained transformer models (BERT, DistilBERT, GPT-2, T5, BART, etc.) for tasks like question answering, named entity recognition, sentiment analysis, translation, summarization, and text generation. It wraps tch-rs (PyTorch bindings) or ONNX Runtime for inference and rust-tokenizers for preprocessing, enabling GPU-accelerated NLP in pure Rust with no Python dependency. Monorepo structured as: src/lib.rs (core library), examples/ (50+ runnable task-specific examples showing sentiment_analysis.rs, question_answering.rs, translation_m2m100.rs), benches/ (7 performance benchmarks), and a build.rs build script. Task-specific models are implemented as separate types (e.g., QuestionAnsweringModel, SummarizationModel) with a common pipeline abstraction. Feature flags control backend (tch vs onnx) and optional tokenizer support.
👥Who it's for
Rust systems engineers and ML practitioners who need production-grade NLP capabilities without depending on Python/PyTorch, particularly those building fast backend services, embedded ML systems, or deploying models in low-latency production environments where Rust's performance and safety guarantees matter.
🌱Maturity & risk
Actively maintained and production-ready: version 0.23.0, CI/CD pipeline via GitHub Actions (continuous-integration.yml), comprehensive example suite covering 30+ use cases, and benchmarks (squad_benchmark.rs, summarization_benchmark.rs, etc.) validating performance. Single maintainer (guillaume-be) but stable release cadence and broad model/task coverage indicate maturity.
Moderate single-maintainer dependency risk and tight coupling to PyTorch via tch-rs (0.17.0) which brings C++ libtorch binary bloat. The project has conditional feature flags (remote, download-libtorch, onnx) that can cause build fragility. ONNX support (ort 1.16.3) exists but appears secondary; breaking changes in tch-rs could impact the entire codebase, and the lack of stable Rust tokenizer ecosystem means dependency on rust-tokenizers 8.1.1.
Active areas of work
No visible commit timestamps in provided data, but version 0.23.0 and recent examples (generation_gptj.rs, natural_language_inference_deberta.rs suggesting DeBERTa support) indicate active development. ONNX examples (onnx-*.rs files) suggest ongoing work to reduce PyTorch dependency. CI workflow configured, suggesting regular testing.
🚀Get running
git clone https://github.com/guillaume-be/rust-bert.git
cd rust-bert
cargo build --release --features download-libtorch
cargo run --example sentiment_analysis
Daily commands:
For a specific example: cargo run --example question_answering --release. For benchmarks: cargo bench --bench squad_benchmark. For ONNX backend: cargo run --example onnx-question-answering --features onnx --release. The download-libtorch feature auto-downloads PyTorch; omit it if you have libtorch pre-installed.
🗺️Map of the codebase
src/lib.rs— Entry point and public API surface; exposes all pipelines and model interfaces that users depend on.Cargo.toml— Defines core dependencies (tch-rs, ort, rust-tokenizers) and feature flags that control model and backend support.src/common/mod.rs— Central hub for common utilities, activations, embeddings, and error handling shared across all model implementations.src/common/resources/mod.rs— Abstracts model weight loading from remote (HuggingFace) and local sources; critical for reproducible model initialization.src/models/bert/bert_model.rs— Reference implementation of the base BERT architecture; patterns here replicate across all other transformer variants (DistilBERT, ALBERT, etc.).build.rs— Build script handling tensor format conversions and pre-compilation setup for PyTorch → Rust-bert model porting.src/convert-tensor.rs— CLI utility for converting HuggingFace model weights to Rust-compatible format; used by maintainers during model onboarding.
🛠️How to make changes
Add a new transformer model variant (e.g., new BERT-like model)
- Create model directory: mkdir src/models/my_model (
src/models/my_model) - Define config deserialization struct, mimicking src/models/bert/bert_model.rs structure with custom layer differences (
src/models/my_model/mod.rs) - Implement embeddings using src/common/embeddings.rs as base template (
src/models/my_model/embeddings.rs) - Implement attention layer, typically copying src/models/bert/attention.rs and adapting for model-specific tweaks (
src/models/my_model/attention.rs) - Implement encoder stacking transformer blocks, reference src/models/bert/encoder.rs pattern (
src/models/my_model/encoder.rs) - Create main model struct forward() method in src/models/my_model/my_model_model.rs (
src/models/my_model/my_model_model.rs) - Export in src/models/mod.rs and src/lib.rs; add example in examples/my_model_task.rs (
src/lib.rs)
Add a new NLP task pipeline (e.g., semantic similarity)
- Create pipeline struct and impl following existing task patterns (e.g., SentimentClassification pattern from src/lib.rs region) (
src/pipelines/semantic_similarity.rs) - Define task-specific output struct with confidence scores and label logic (
src/pipelines/semantic_similarity.rs) - Implement pipeline::Pipeline trait with forward() taking tokenized input and returning typed output (
src/pipelines/semantic_similarity.rs) - Add constructor leveraging src/common/resources/mod.rs to load base model weights (
src/pipelines/semantic_similarity.rs) - Export from src/lib.rs and create examples/semantic_similarity.rs with end-to-end usage (
src/lib.rs)
Port a new HuggingFace model to Rust-bert
- Download model weights using 'huggingface-hub' CLI or Python, save as .safetensors or .bin (
build.rs) - Run src/convert-tensor.rs binary to validate and convert weight format for tch-rs compatibility (
src/convert-tensor.rs) - Implement model architecture following pattern in src/models/bert/ (embeddings → encoder → output head) (
src/models/my_model/mod.rs) - Load converted weights in model constructor using src/common/resources/remote.rs or local.rs (
src/models/my_model/my_model_model.rs) - Add integration test running inference against reference HuggingFace outputs to validate numerical parity (
examples/my_model_validate.rs)
Enable ONNX backend support for a task
- Study existing ONNX example (e.g., examples/onnx-question-answering.rs) showing ORT session creation (
examples/onnx-question-answering.rs) - Create conditional module in src/pipelines/my_task.rs guarded by #[cfg(feature = 'onnx')] (
src/pipelines/my_task.rs) - Implement alternative forward() using ort crate to execute .onnx model file instead of tch-rs tensors (
src/pipelines/my_task.rs) - Update Cargo.toml to expose 'onnx' feature flag that gates ONNX-specific dependencies (
Cargo.toml) - Add example demonstrating both tch-rs and ONNX paths for same task, showing performance tradeoff (
examples/onnx-my_task.rs)
🔧Why these technologies
- tch-rs (PyTorch bindings) — Provides GPU-accelerated tensor operations and seamless model weight loading from PyTorch; avoids reinventing CUDA/MPS kernels.
- rust-tokenizers — Rust-native tokenization matches Python HuggingFace behavior
🪤Traps & gotchas
- libtorch binary bloat: default
download-libtorchfeature pulls ~1-2GB binary; omit for existing installations or use ONNX path. 2. GPU support implicit: CUDA/cuDNN detection happens in tch-rs; CPU-only builds work but require explicit environment configuration. 3. Model caching: Models auto-download to ~/.cache/huggingface/hub (via cached-path); custom cache paths must be set via env var before pipeline instantiation. 4. Feature interaction:onnxand defaulttchbackends are not simultaneously available in same build; choose one via feature flags. 5. Tokenizer availability: Some examples usehf-tokenizersfeature (external tokenizer library) which may not be available; fall back to rust-tokenizers.
🏗️Architecture
💡Concepts to learn
- Transformer architecture (attention, self-attention, multi-head attention) — Core mechanism powering all models in rust-bert (BERT, DistilBERT, GPT-2, T5); understanding attention allows you to interpret model inputs/outputs and know when to use which model variant
- Subword tokenization (BPE, WordPiece) — rust-bert models require fixed-size token sequences; tokenization via rust-tokenizers converts raw text to token IDs that models expect, and vocabulary size affects embedding layer dimensions
- Transfer learning and fine-tuning — rust-bert provides pre-trained weights from Hugging Face Hub; understanding transfer learning explains why you can use these models off-the-shelf without training from scratch
- Tensor shape broadcasting and padding — NLP pipelines often pad variable-length sequences to fixed lengths; understanding tensor shapes is critical for debugging dimension mismatches in tch-rs tensor operations and attention masks
- Model quantization and ONNX IR — rust-bert's ONNX backend enables quantized inference for faster deployment; ONNX Intermediate Representation abstracts PyTorch/TensorFlow models for portable inference
- Batch processing and async inference — examples/async-sentiment.rs shows batching and async patterns; critical for production NLP services processing many requests concurrently without blocking on GPU inference
- Feature gating and conditional compilation — rust-bert uses Cargo features (download-libtorch, onnx, remote) to control backend selection and optional dependencies; understanding #[cfg(feature="...")] is essential for configuring builds for your deployment target
🔗Related repos
huggingface/transformers— The original Python reference implementation that rust-bert ports to Rust; essential for architecture understanding and model compatibility referenceLaurentMazare/tch-rs— Core PyTorch binding crate providing torch tensor operations and neural network primitives that rust-bert depends on for inferenceguillaume-be/rust-tokenizers— Companion crate providing tokenization (BPE, WordPiece, SentencePiece) that rust-bert uses for text preprocessing before inferencepykeio/ort— ONNX Runtime bindings that rust-bert optionally integrates as backend alternative to PyTorch, enabling smaller model deploymentsburn-rs/burn— Pure-Rust deep learning framework offering a pure-Rust alternative to tch-rs dependency, relevant for vision of eliminating C++ PyTorch dependency
🪄PR ideas
To work on one of these in Claude Code or Cursor, paste:
Implement the "<title>" PR idea from CLAUDE.md, working through the checklist as the task list.
Add integration tests for ONNX pipeline examples
The repo has 5 ONNX example files (onnx-*.rs) but no corresponding integration tests. These examples demonstrate critical inference paths that differ from the default tch-rs backend. Adding tests would catch regressions in ONNX model loading, tensor conversion, and output validation—especially important since ONNX is an optional feature with separate dependencies (ort, ndarray).
- [ ] Create tests/onnx_integration_test.rs with test cases for each ONNX example (masked-lm, question-answering, sequence-classification, token-classification, text-generation, translation)
- [ ] Add CI step in .github/workflows/continuous-integration.yml to run tests with --features onnx flag
- [ ] Ensure tests validate both model loading from HuggingFace hub and output tensor shapes/values
Add feature-gated tests for TLS configuration options
The Cargo.toml defines three mutually-exclusive TLS features (default-tls, rustls-tls, and remote resources), but there are no tests verifying that model downloads work correctly under each configuration. This is critical for users in restricted environments who need rustls-tls instead of the default.
- [ ] Create tests/tls_configuration_test.rs that attempts to download a small model using cached-path under different feature combinations
- [ ] Add matrix CI configuration in .github/workflows/continuous-integration.yml to test: [default-tls], [rustls-tls], [no features but remote=false]
- [ ] Document in CONTRIBUTING.md how to test TLS configurations locally
Add benchmark comparison CI for performance regressions
The repo has 7 benchmarks (benches/*.rs) but they're not integrated into CI—regressions could silently degrade performance. Adding automated benchmark tracking would help catch tensor operation slowdowns, model inference regressions, and memory issues early.
- [ ] Create .github/workflows/benchmark-comparison.yml that runs all benchmarks on PR and compares against main branch using criterion.rs output
- [ ] Add criterion as a dev-dependency in Cargo.toml if not already present (needed for benchmark output comparison)
- [ ] Document in CONTRIBUTING.md expected benchmark baselines and acceptable variance thresholds
🌿Good first issues
- Add missing model variants: ELECTRA and RoBERTa examples are absent from examples/ but mentioned in model support matrix; create examples/sequence_classification_electra.rs and examples/sequence_classification_roberta.rs following the DistilBERT pattern.
- Expand ONNX backend coverage: Only 6 ONNX examples exist (onnx-*.rs) versus 40+ torch examples; implement onnx-sentiment-analysis.rs and onnx-named-entity-recognition.rs for parity, useful for low-latency deployments.
- Add integration tests: benches/ has benchmarks but src/ lacks unit/integration tests for core types; add tests/pipeline_integration_tests.rs covering model loading, inference on fixed inputs, and output shape validation across 3-4 task types.
⭐Top contributors
Click to expand
Top contributors
- @guillaume-be — 58 commits
- @dependabot[bot] — 4 commits
- @njaard — 3 commits
- @lerouxrgd — 3 commits
- @blmarket — 2 commits
📝Recent commits
Click to expand
Recent commits
6db859e— Bump jinja2 from 3.1.4 to 3.1.6 in /utils (#478) (dependabot[bot])c2e325c— Dependencies update & Clippy fixes (#483) (guillaume-be)411c224— Changelog update (#470) (guillaume-be)5c638eb— Torch version update (#467) (guillaume-be)9707981— Update async-sentiment.rs (#337) (rick68)9af98f8— Update README.md (#460) (hkfi)db1ed8c— Fix formatting error insrc/pipelines/masked_language.rs(#456) (youzarsiph)62b40d0— added bert-large-uncased model (#439) (kavan-mevada)eabebc6— add serde for Keyword (#465) (oiwn)3df3816— Fix clippy warnings (#466) (guillaume-be)
🔒Security observations
The rust-bert codebase is a mature ML/NLP library with generally good security practices. Primary concerns are: (1) incomplete Cargo.toml declaration requiring verification, (2) critical dependency on 'tch' and 'ort' which require regular security auditing, (3) supply chain risk from remote model downloads lacking verification mechanisms, and (4) multiple TLS/feature configurations with unclear security testing. The codebase lacks explicit security documentation and an audit policy. No evidence of code injection risks (SQL/XSS) as this is a ML inference library. Recommend implementing cargo-audit, model verification mechanisms, and security documentation.
- Medium · Incomplete Dependency Declaration in Cargo.toml —
Cargo.toml (dependencies section). The Cargo.toml file appears to be truncated at the 'ort' dependency declaration. The line 'ort = { version = "1.16.3", optio' is incomplete, which may indicate parsing issues or incomplete configuration. This could lead to unexpected dependency resolution or missing security constraints. Fix: Complete the ort dependency declaration with proper feature flags and version constraints. Ensure all optional dependencies are fully specified and the file is properly formatted. - Medium · Use of Outdated or Potentially Vulnerable Dependencies —
Cargo.toml (dependencies: tch, rust_tokenizers, etc.). The codebase uses 'tch' version 0.17.0 which provides PyTorch C++ bindings. This dependency is critical for ML inference and should be regularly audited. Additionally, 'rust_tokenizers' 8.1.1 and other dependencies should be checked against known vulnerabilities. No evidence of dependency auditing via 'cargo-audit' configuration is visible. Fix: Regularly run 'cargo audit' to identify known vulnerabilities. Consider pinning to specific patch versions and implementing a dependency update strategy. Review security advisories for 'tch-rs' and 'ort' regularly. - Medium · Optional TLS Configuration —
Cargo.toml (features section). The crate offers both 'rustls-tls' and 'default-tls' as feature flags with 'default-tls' being the default. This allows users to accidentally use the default OpenSSL-based TLS which may have different security characteristics. The 'cached-path' dependency is used for remote model downloads, making TLS configuration critical. Fix: Document TLS security implications clearly. Consider recommending 'rustls-tls' as the default for supply chain security benefits. Provide security guidance for users downloading pre-trained models. - Low · Remote Model Download Capability —
Cargo.toml (cached-path dependency), src/common/resources/remote.rs. The crate includes remote download functionality via 'cached-path' dependency for loading pre-trained models from external sources (likely Hugging Face Model Hub). While necessary for functionality, this introduces supply chain risk if models are not verified. Fix: Implement cryptographic verification (checksums/signatures) for downloaded models. Document security implications of using remote models. Consider providing hash verification mechanisms for downloaded artifacts. - Low · Multiple Optional Feature Combinations —
Cargo.toml (features section), .github/workflows/continuous-integration.yml. The crate has multiple feature combinations (remote/local resources, ONNX support, different TLS backends) which could lead to untested security configurations. No evidence of feature combination testing in CI. Fix: Add CI tests for all meaningful feature combinations, particularly security-sensitive ones (TLS backends, remote vs local models). Document supported and tested configurations. - Low · Missing Security Policy Documentation —
Repository root. No SECURITY.md file is present in the repository structure to document security reporting procedures, dependency policies, or security practices. Fix: Create a SECURITY.md file documenting how to report security vulnerabilities responsibly, security update procedures, and dependency management practices.
LLM-derived; treat as a starting point, not a security audit.
👉Where to read next
- Open issues — current backlog
- Recent PRs — what's actively shipping
- Source on GitHub
Generated by RepoPilot. Verdict based on maintenance signals — see the live page for receipts. Re-run on a new commit to refresh.