Skip to main content

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)

Metadata

Release files for torch2transformer 0.2.0

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

Source distribution (sdist)

Source distribution for torch2transformer 0.2.0
File Size Uploaded Provenance
torch2transformer-0.2.0.tar.gz 4.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for torch2transformer 0.2.0
File Interpreter ABI Platform
torch2transformer-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.2 kB

Release files / torch2transformer-0.2.0.tar.gz

Download URL torch2transformer-0.2.0.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
0e581f6cd350659d74809956b0d117aea9375de822b42b7bee84eb0a916b15f0
BLAKE2b-256 checksum
How to use checksums
7952982a520eace82dcbc8b495619de93dbc22b1dd5587e1c96b16b442a74799
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.12

Release files / torch2transformer-0.2.0-py3-none-any.whl

Download URL torch2transformer-0.2.0-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
66654d84dd676b55582b8b21234a455f204159d69c757ad3a2e95623c540cbbb
BLAKE2b-256 checksum
How to use checksums
2ec4e80a46b47323c557893461e3a5b561eefc448fdcd99a42b26d1718e7ac47
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.12

Security for torch2transformer 0.2.0

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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