Skip to main content

# 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


Download files

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

Source Distribution

torch2transformer-0.2.0.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

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

torch2transformer-0.2.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

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

Hashes for torch2transformer-0.2.0.tar.gz
Algorithm Hash digest
SHA256 0e581f6cd350659d74809956b0d117aea9375de822b42b7bee84eb0a916b15f0
MD5 5e90afe884e40c1234ffc24922078b54
BLAKE2b-256 7952982a520eace82dcbc8b495619de93dbc22b1dd5587e1c96b16b442a74799

See more details on using hashes here.

File details

Details for the file torch2transformer-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for torch2transformer-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 66654d84dd676b55582b8b21234a455f204159d69c757ad3a2e95623c540cbbb
MD5 954d020229ca4f5c551a557f37442a02
BLAKE2b-256 2ec4e80a46b47323c557893461e3a5b561eefc448fdcd99a42b26d1718e7ac47

See more details on using hashes here.

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