# torch2transformer
Project description
torch2transformer
torch2transformer lets you wrap plain PyTorch models so they work seamlessly with the Hugging Face Transformers ecosystem.
Important: The original PyTorch model class must be available at run time and load time.
Features
- Wrap any PyTorch model for Hugging Face
Trainer - Save / load via
save_pretrained/from_pretrained - Supports Hugging Face
.generate()for autoregressive text generation - Minimal interface required:
forward(input_ids, labels=None)returning{"logits": ..., "loss": ...} - No custom training loops needed
Installation
pip install torch2transformer
or,
uv pip install torch2transformer
Example
from torch2transformer import TorchAdapter, wrap_model, load_model
# wrap Pytorch model as a Transformer model
model = wrap_model(
torch_model_cls=TinyCharModel,
torch_model_kwargs={"vocab_size": 100, "hidden_size": 32},
task_type="causal_lm"
)
# then can be used with Trainer()
# save model
model.save_pretrained("./tiny_ckpt")
# load model
model = load_model("./tiny_ckpt", torch_model_cls=TinyCharModel)
# example input
seed_text = "I love "
input_ids = torch.tensor([[char2id[c] for c in seed_text]])
# generate next characters
output_ids = model.generate(input_ids, max_new_tokens=20)
pred_text = "".join([id2char[i] for i in output_ids[0].tolist()])
print(pred_text)
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 Distribution
Built Distribution
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 torch2transformer-0.2.0.tar.gz.
File metadata
- Download URL: torch2transformer-0.2.0.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e581f6cd350659d74809956b0d117aea9375de822b42b7bee84eb0a916b15f0
|
|
| MD5 |
5e90afe884e40c1234ffc24922078b54
|
|
| BLAKE2b-256 |
7952982a520eace82dcbc8b495619de93dbc22b1dd5587e1c96b16b442a74799
|
File details
Details for the file torch2transformer-0.2.0-py3-none-any.whl.
File metadata
- Download URL: torch2transformer-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
66654d84dd676b55582b8b21234a455f204159d69c757ad3a2e95623c540cbbb
|
|
| MD5 |
954d020229ca4f5c551a557f37442a02
|
|
| BLAKE2b-256 |
2ec4e80a46b47323c557893461e3a5b561eefc448fdcd99a42b26d1718e7ac47
|