Skip to main content

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 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}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

trustformers-0.1.2-cp39-abi3-win_amd64.whl (2.0 MB view details)

Uploaded CPython 3.9+Windows x86-64

trustformers-0.1.2-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ x86-64

trustformers-0.1.2-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.0 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

trustformers-0.1.2-cp39-abi3-macosx_11_0_arm64.whl (1.4 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

trustformers-0.1.2-cp39-abi3-macosx_10_12_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

Details for the file trustformers-0.1.2-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: trustformers-0.1.2-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 2.0 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

Hashes for trustformers-0.1.2-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 b1446d311c9dcc58ba877954af1430fe5dd4e71d1cf0ad1f58fd5038c91bf3e2
MD5 185fa816f9fa1c4ee2e19310ccef95af
BLAKE2b-256 2f5c443b3591505d92112251466ef6520b60f69fdf4d9c6dd38d063c63d21ff7

See more details on using hashes here.

Provenance

The following attestation bundles were made for trustformers-0.1.2-cp39-abi3-win_amd64.whl:

Publisher: pypi-publish.yml on cool-japan/trustformers

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file trustformers-0.1.2-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for trustformers-0.1.2-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 07f660127971cbbeccb1aff0a1b6a78eb16a4eefa89070192b97dda86b7c3f50
MD5 6f5c7f25e13603a1d1f1eea2452299ca
BLAKE2b-256 7a79b7520c38c710ef5be30db843e94b56d327448c31ae8b841cf5e3c6667a03

See more details on using hashes here.

Provenance

The following attestation bundles were made for trustformers-0.1.2-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: pypi-publish.yml on cool-japan/trustformers

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file trustformers-0.1.2-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for trustformers-0.1.2-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 55e889e777b6a4aeffc463bcdb21c8c5daaead4ceddcc83ac2db2163e938e004
MD5 6fd8ffa1a771efab481a6ccbf251763f
BLAKE2b-256 4be1ceba87bc4168ada5206b70e83861d69ac52501f65abcbe82da338ace4c52

See more details on using hashes here.

Provenance

The following attestation bundles were made for trustformers-0.1.2-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: pypi-publish.yml on cool-japan/trustformers

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file trustformers-0.1.2-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for trustformers-0.1.2-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 89c150d80984078114312772feaa1fe4c90f73678ae94ff5abec2ab6d9e576a0
MD5 d86b12c0dad723f93402472b12e77ea0
BLAKE2b-256 1900c2ffbd2c305ff7c6c879fc9dfb7a95f0c698ff96192ee623a84349dd96b7

See more details on using hashes here.

Provenance

The following attestation bundles were made for trustformers-0.1.2-cp39-abi3-macosx_11_0_arm64.whl:

Publisher: pypi-publish.yml on cool-japan/trustformers

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file trustformers-0.1.2-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for trustformers-0.1.2-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 556aec32ac08556893676f86ea9d39b3e71257109cd3b599c4051c796438e633
MD5 9d9eea763905ccadeb38704881d962a1
BLAKE2b-256 7e84be5246daf76e0e266af096da2561b3796e441344c6dcb57c54332321224b

See more details on using hashes here.

Provenance

The following attestation bundles were made for trustformers-0.1.2-cp39-abi3-macosx_10_12_x86_64.whl:

Publisher: pypi-publish.yml on cool-japan/trustformers

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page