Encoder, decoder, and encoder-decoder Transformer architectures in PyTorch
Project description
Transformer Variants
Implementations of encoder, decoder, and encoder-decoder Transformer architectures in PyTorch, following "Attention Is All You Need" (Vaswani et al., 2017).
Installation
pip install transformers-from-scratch
PyTorch is required but not installed automatically (to allow users to choose their CUDA build). Install it first from pytorch.org.
Development Setup
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
Modules
The transformer_variants package provides three stand-alone models and the building blocks they are composed of.
| Class | Description |
|---|---|
EncoderTransformer |
BERT-style bidirectional encoder |
DecoderOnlyTransformer |
GPT-style autoregressive decoder |
EncoderDecoderTransformer |
Sequence-to-sequence encoder-decoder |
DecoderTransformer |
Decoder component — not for stand-alone use |
Usage
import torch
from transformer_variants import EncoderTransformer, DecoderOnlyTransformer, EncoderDecoderTransformer
VOCAB_SIZE, MAX_SEQ_LEN = 32000, 512
D_MODEL, D_FF, N_HEADS, N_BLOCKS = 512, 2048, 8, 6
Encoder
model = EncoderTransformer(VOCAB_SIZE, MAX_SEQ_LEN, D_MODEL, D_FF, N_HEADS, N_BLOCKS)
src = torch.randint(0, VOCAB_SIZE, (B, T_src)) # [B, T_src]
src_mask = (src == pad_id).unsqueeze(1) # [B, 1, T_src] True = padding
out = model(src, src_mask) # [B, T_src, d_model]
Decoder-only
model = DecoderOnlyTransformer(VOCAB_SIZE, MAX_SEQ_LEN, D_MODEL, D_FF, N_HEADS, N_BLOCKS)
tgt = torch.randint(0, VOCAB_SIZE, (B, T)) # [B, T]
pad_mask = (tgt == pad_id).unsqueeze(1) # [B, 1, T] True = padding (optional)
logits = model(tgt, pad_mask) # [B, T, vocab_size]
A causal mask is generated internally — no need to pass one.
Encoder-decoder
model = EncoderDecoderTransformer(VOCAB_SIZE, MAX_SEQ_LEN, D_MODEL, D_FF, N_HEADS, N_BLOCKS, N_BLOCKS)
src = torch.randint(0, VOCAB_SIZE, (B, T_src)) # [B, T_src]
tgt = torch.randint(0, VOCAB_SIZE, (B, T_tgt)) # [B, T_tgt]
src_mask = (src == pad_id).unsqueeze(1) # [B, 1, T_src]
T_tgt = tgt.size(1)
causal = torch.ones(T_tgt, T_tgt, dtype=torch.bool).triu(1).unsqueeze(0) # [1, T_tgt, T_tgt]
tgt_mask = causal | (tgt == pad_id).unsqueeze(1) # [B, T_tgt, T_tgt]
logits = model(src, tgt, src_mask, tgt_mask) # [B, T_tgt, vocab_size]
Running tests
python -m unittest test -v
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 transformers_from_scratch-0.1.1.tar.gz.
File metadata
- Download URL: transformers_from_scratch-0.1.1.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2b375aca122279a410317d6232c0085112315cce020599448264ba04c2f5b2ff
|
|
| MD5 |
8c4f44fdfa35fc54f6fc1cf103426d95
|
|
| BLAKE2b-256 |
eb5a09b7ade131f7002c7456217f83c30a5adceda69060530bc9a4eff893c15e
|
File details
Details for the file transformers_from_scratch-0.1.1-py3-none-any.whl.
File metadata
- Download URL: transformers_from_scratch-0.1.1-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aa6e1f01d65c76532ec74e4db5cf1858dc85474ef746e352982d7aa28a328ce8
|
|
| MD5 |
d6f686b37fdfe71178fb2a43697b1358
|
|
| BLAKE2b-256 |
f662a7a36659a607d73c0ed673c24de061a45a7228f11b36dceda4791463e8d9
|