Python bindings for TrustformeRS - High-performance Rust Transformers library
Project description
TrustformeRS Python
High-performance transformer library for Python, written in Rust. Drop-in replacement for Hugging Face Transformers with significant performance improvements.
Features
- 🚀 10-100x faster than pure Python implementations
- 🔄 Drop-in replacement for Hugging Face Transformers
- 🦀 Written in Rust for memory safety and performance
- 🔧 Zero-copy tensor operations with NumPy
- 🤝 PyTorch interoperability (optional)
- 📦 No external dependencies for core functionality
Installation
pip install trustformers
From source
# Install maturin (build tool for Rust Python extensions)
pip install maturin
# Clone the repository
git clone https://github.com/cool-japan/trustformers
cd trustformers/trustformers-py
# Build and install
maturin develop --release
Quick Start
Basic Usage
from trustformers import AutoModel, AutoTokenizer, pipeline
# Load model and tokenizer
model = AutoModel.from_pretrained("bert-base-uncased")
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
# Create a pipeline
classifier = pipeline("text-classification", model=model, tokenizer=tokenizer)
# Run inference
results = classifier("This is a great library!")
print(results)
Direct Model Usage
import numpy as np
from trustformers import BertModel, Tensor
# Create model
model = BertModel.from_pretrained("bert-base-uncased")
# Create input tensors
input_ids = Tensor(np.array([[101, 2023, 2003, 1037, 2742, 102]]))
attention_mask = Tensor(np.ones((1, 6)))
# Forward pass
outputs = model(input_ids, attention_mask)
print(outputs["last_hidden_state"].shape)
NumPy Integration
import numpy as np
from trustformers import Tensor
# Create tensor from NumPy array
np_array = np.random.randn(2, 3, 4).astype(np.float32)
tensor = Tensor(np_array)
# Convert back to NumPy
np_array_back = tensor.numpy()
# Tensor operations
result = tensor.matmul(tensor.transpose())
PyTorch Interoperability
import torch
from trustformers import Tensor
# Convert from PyTorch
torch_tensor = torch.randn(2, 3, 4)
trust_tensor = Tensor.from_torch(torch_tensor)
# Convert to PyTorch
torch_tensor_back = trust_tensor.to_torch()
Supported Models
- BERT and variants (RoBERTa, ALBERT, DistilBERT, ELECTRA, DeBERTa)
- GPT-2 and variants (GPT-Neo, GPT-J)
- T5 (encoder-decoder)
- LLaMA and Mistral
- Vision Transformer (ViT)
- CLIP (multimodal)
API Compatibility
TrustformeRS provides a compatible API with Hugging Face Transformers:
# Hugging Face Transformers
from transformers import AutoModel, AutoTokenizer
# TrustformeRS (drop-in replacement)
from trustformers import AutoModel, AutoTokenizer
Most code written for Hugging Face Transformers will work with minimal changes.
Performance
Benchmarks on common tasks:
| Task | Model | HF Transformers | TrustformeRS | Speedup |
|---|---|---|---|---|
| Text Classification | BERT-base | 52 ms | 3.2 ms | 16.3x |
| Text Generation | GPT-2 | 124 ms | 8.7 ms | 14.3x |
| Question Answering | BERT-large | 89 ms | 5.4 ms | 16.5x |
Benchmarks run on Apple M1 Pro, batch size 1, sequence length 512
Advanced Features
Custom Models
from trustformers import PreTrainedModel, Tensor
import numpy as np
class CustomModel(PreTrainedModel):
def __init__(self, config):
super().__init__(config)
# Define your model architecture
def forward(self, input_ids, attention_mask=None):
# Implement forward pass
pass
Training (Coming Soon)
from trustformers import Trainer, TrainingArguments
training_args = TrainingArguments(
output_dir="./results",
num_train_epochs=3,
per_device_train_batch_size=16,
learning_rate=5e-5,
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=eval_dataset,
)
trainer.train()
Development
Building from source
# Install development dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Format code
black .
isort .
# Lint
ruff check .
Architecture
The library is organized into several components:
tensor.rs- Tensor operations and NumPy integrationmodels.rs- Model implementations (BERT, GPT-2, etc.)tokenizers.rs- Tokenizer implementationspipelines.rs- High-level pipeline APIauto.rs- Auto classes for model/tokenizer loadingtraining.rs- Training utilities (WIP)
License
Apache License 2.0
Contributing
Contributions are welcome! Please read our Contributing Guide for details.
Citation
If you use TrustformeRS in your research, please cite:
@software{trustformers2024,
title = {TrustformeRS: High-Performance Transformers in Rust},
year = {2024},
url = {https://github.com/cool-japan/trustformers}
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file trustformers-0.1.1-cp39-abi3-win_amd64.whl.
File metadata
- Download URL: trustformers-0.1.1-cp39-abi3-win_amd64.whl
- Upload date:
- Size: 1.9 MB
- Tags: CPython 3.9+, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7f82da6beb4f4fb388b6f5e55c91b0bf0f521db2223853a0b82b325160a8f154
|
|
| MD5 |
98a68a35b5423525286af5740548609d
|
|
| BLAKE2b-256 |
47e038e3a330563200c95b6b0868ce6208785d45040809d95e0a817f5accbb5a
|
Provenance
The following attestation bundles were made for trustformers-0.1.1-cp39-abi3-win_amd64.whl:
Publisher:
pypi-publish.yml on cool-japan/trustformers
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
trustformers-0.1.1-cp39-abi3-win_amd64.whl -
Subject digest:
7f82da6beb4f4fb388b6f5e55c91b0bf0f521db2223853a0b82b325160a8f154 - Sigstore transparency entry: 1392541394
- Sigstore integration time:
-
Permalink:
cool-japan/trustformers@de3583a7635448bc13719e643fe738c6a4c0d6e7 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/cool-japan
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@de3583a7635448bc13719e643fe738c6a4c0d6e7 -
Trigger Event:
push
-
Statement type:
File details
Details for the file trustformers-0.1.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: trustformers-0.1.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 1.5 MB
- Tags: CPython 3.9+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2efed384be205258b5c70735c3f21ac668b05a800f16092df14d8430f20062bb
|
|
| MD5 |
499693bbdba2d1c61379bea62d758a47
|
|
| BLAKE2b-256 |
363382cb4997f5817651e33dabd2f89e24f96b8c92bf67145d753f91e45eaf17
|
Provenance
The following attestation bundles were made for trustformers-0.1.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:
Publisher:
pypi-publish.yml on cool-japan/trustformers
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
trustformers-0.1.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl -
Subject digest:
2efed384be205258b5c70735c3f21ac668b05a800f16092df14d8430f20062bb - Sigstore transparency entry: 1392541397
- Sigstore integration time:
-
Permalink:
cool-japan/trustformers@de3583a7635448bc13719e643fe738c6a4c0d6e7 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/cool-japan
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@de3583a7635448bc13719e643fe738c6a4c0d6e7 -
Trigger Event:
push
-
Statement type:
File details
Details for the file trustformers-0.1.1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.
File metadata
- Download URL: trustformers-0.1.1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 964.9 kB
- Tags: CPython 3.9+, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
41a9cc90cfa540ae642714b0f9ab98aa530d2efaed6bd571027e6577bafa243c
|
|
| MD5 |
711ad2de3b18c2208dc64fd3339bc80d
|
|
| BLAKE2b-256 |
ee5973dc420508ed12b0dbc57ed1f23120d7881b7a95f41de64a030791bececd
|
Provenance
The following attestation bundles were made for trustformers-0.1.1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:
Publisher:
pypi-publish.yml on cool-japan/trustformers
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
trustformers-0.1.1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl -
Subject digest:
41a9cc90cfa540ae642714b0f9ab98aa530d2efaed6bd571027e6577bafa243c - Sigstore transparency entry: 1392541396
- Sigstore integration time:
-
Permalink:
cool-japan/trustformers@de3583a7635448bc13719e643fe738c6a4c0d6e7 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/cool-japan
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@de3583a7635448bc13719e643fe738c6a4c0d6e7 -
Trigger Event:
push
-
Statement type:
File details
Details for the file trustformers-0.1.1-cp39-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: trustformers-0.1.1-cp39-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 1.3 MB
- Tags: CPython 3.9+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
66e1d009a44f62a3551384e1d8ac3ef31939d3c495292df285728243fe9067c7
|
|
| MD5 |
75c47b11abc12cb6911cb8e5b95b6805
|
|
| BLAKE2b-256 |
a442385aeae45b401bcd6401380ae63a5c4436cedd7767e9a43fb0333db9f21e
|
Provenance
The following attestation bundles were made for trustformers-0.1.1-cp39-abi3-macosx_11_0_arm64.whl:
Publisher:
pypi-publish.yml on cool-japan/trustformers
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
trustformers-0.1.1-cp39-abi3-macosx_11_0_arm64.whl -
Subject digest:
66e1d009a44f62a3551384e1d8ac3ef31939d3c495292df285728243fe9067c7 - Sigstore transparency entry: 1392541399
- Sigstore integration time:
-
Permalink:
cool-japan/trustformers@de3583a7635448bc13719e643fe738c6a4c0d6e7 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/cool-japan
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@de3583a7635448bc13719e643fe738c6a4c0d6e7 -
Trigger Event:
push
-
Statement type:
File details
Details for the file trustformers-0.1.1-cp39-abi3-macosx_10_12_x86_64.whl.
File metadata
- Download URL: trustformers-0.1.1-cp39-abi3-macosx_10_12_x86_64.whl
- Upload date:
- Size: 1.6 MB
- Tags: CPython 3.9+, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6604144cc05592a957903ef245971356b643c19d4819cc1404511fb0894a1fca
|
|
| MD5 |
43e62d2ddfb10bd22c200f24a9800810
|
|
| BLAKE2b-256 |
ba3cc34490c8495020fc11ef72d1b9ed1860b2cafbe4f4eb914786e5c0409db9
|
Provenance
The following attestation bundles were made for trustformers-0.1.1-cp39-abi3-macosx_10_12_x86_64.whl:
Publisher:
pypi-publish.yml on cool-japan/trustformers
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
trustformers-0.1.1-cp39-abi3-macosx_10_12_x86_64.whl -
Subject digest:
6604144cc05592a957903ef245971356b643c19d4819cc1404511fb0894a1fca - Sigstore transparency entry: 1392541400
- Sigstore integration time:
-
Permalink:
cool-japan/trustformers@de3583a7635448bc13719e643fe738c6a4c0d6e7 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/cool-japan
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@de3583a7635448bc13719e643fe738c6a4c0d6e7 -
Trigger Event:
push
-
Statement type: