Skip to main content

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

model.py 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 model 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


Download files

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

Source Distribution

transformers_from_scratch-0.1.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

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

transformers_from_scratch-0.1.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file transformers_from_scratch-0.1.0.tar.gz.

File metadata

File hashes

Hashes for transformers_from_scratch-0.1.0.tar.gz
Algorithm Hash digest
SHA256 19b29cbff9d977225cffd7a425cc57cc4241cadafa7333ffa5c39b5ba0ff8ef1
MD5 d7adc9ff6645f1083bd253224e3cd91e
BLAKE2b-256 aec4a3ad3a7ce825ed90fca3eb049d4de3f68cc60b12fefca7ae58441888ecd0

See more details on using hashes here.

File details

Details for the file transformers_from_scratch-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for transformers_from_scratch-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 45c1c57409b378ddd0646a10bbd40d62e6f9ab2845476d0fdc48769847e5c43a
MD5 868319684a5d1cadec5a23037263fa68
BLAKE2b-256 49dfedd31da9d7d8b3c5619b07e782ca4b2237102869f6d7fe0ada028d7f867b

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