Skip to main content

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 integration
  • models.rs - Model implementations (BERT, GPT-2, etc.)
  • tokenizers.rs - Tokenizer implementations
  • pipelines.rs - High-level pipeline API
  • auto.rs - Auto classes for model/tokenizer loading
  • training.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}
}

Metadata

Release files for trustformers 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for trustformers 0.1.3
File Actions
trustformers-0.1.3-cp39-abi3-win_amd64.whl CPython 3.9 abi3 Windows x86-64 Details for trustformers-0.1.3-cp39-abi3-win_amd64.whl
trustformers-0.1.3-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.9 abi3 Linux glibc 2.17+ x86-64 Details for trustformers-0.1.3-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
trustformers-0.1.3-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.9 abi3 Linux glibc 2.17+ ARM64 Details for trustformers-0.1.3-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
trustformers-0.1.3-cp39-abi3-macosx_11_0_arm64.whl CPython 3.9 abi3 macOS 11.0+ ARM64 Details for trustformers-0.1.3-cp39-abi3-macosx_11_0_arm64.whl
trustformers-0.1.3-cp39-abi3-macosx_10_12_x86_64.whl CPython 3.9 abi3 macOS 10.12+ x86-64 Details for trustformers-0.1.3-cp39-abi3-macosx_10_12_x86_64.whl

Total release size: 7.6 MB

Release files / trustformers-0.1.3-cp39-abi3-win_amd64.whl

Download URL trustformers-0.1.3-cp39-abi3-win_amd64.whl
Size 2.0 MB
Tags CPython 3.9 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
cc5b5d4fcba36be410d7ac3d59d9122bf7ec50e9f419e4cbfcc0023b5e2e91a2
BLAKE2b-256 checksum
How to use checksums
2e309a93e0a5729375518efdf3f023c1a3931e48a07ffcfc885e3058297e257d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 24, 2026.

Transparency log

Release files / trustformers-0.1.3-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL trustformers-0.1.3-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 1.6 MB
Tags CPython 3.9 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
57260cf8af09868e1c4e542b4cb3fc7fee336998ceda82f15834979655e6a0be
BLAKE2b-256 checksum
How to use checksums
82568ff0a1ebab4cc5854b93e91871e87eba576a6d07c39bea824ae1788328cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 24, 2026.

Transparency log

Release files / trustformers-0.1.3-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL trustformers-0.1.3-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 1.0 MB
Tags CPython 3.9 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
71e26b67ca6b3a48ae0c94471f2697ec3d5468fa24479d6a09febd9192e473dc
BLAKE2b-256 checksum
How to use checksums
e681b3f37d83b308cfdef400d200640f6f87f8859d04d8794fbfcad1853460a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 24, 2026.

Transparency log

Release files / trustformers-0.1.3-cp39-abi3-macosx_11_0_arm64.whl

Download URL trustformers-0.1.3-cp39-abi3-macosx_11_0_arm64.whl
Size 1.4 MB
Tags CPython 3.9 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
92105ef17ed005822002a688fcb55b5d5bc0394b092b9b882b14a3003ab06c8c
BLAKE2b-256 checksum
How to use checksums
5c775074e34b89e10953cc3b7d8f5ef6b54ef2b1e9b72a9acb59f43f7d436506
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 24, 2026.

Transparency log

Release files / trustformers-0.1.3-cp39-abi3-macosx_10_12_x86_64.whl

Download URL trustformers-0.1.3-cp39-abi3-macosx_10_12_x86_64.whl
Size 1.7 MB
Tags CPython 3.9 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
238ed276d0a67c52d79f4d2e1a0508f204f714055361ee43014eb7bc4b3dcaa7
BLAKE2b-256 checksum
How to use checksums
ad19ae06251e773de62e44052fce7794a87734300e7403056bd89cb689af1c33
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 24, 2026.

Transparency log

Security for trustformers 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 5 files in this release have been uploaded with provenance metadata.

Select from 5 files

Attestation for trustformers-0.1.3-cp39-abi3-win_amd64.whl

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 24, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page