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

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


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.1.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.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for transformers_from_scratch-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2b375aca122279a410317d6232c0085112315cce020599448264ba04c2f5b2ff
MD5 8c4f44fdfa35fc54f6fc1cf103426d95
BLAKE2b-256 eb5a09b7ade131f7002c7456217f83c30a5adceda69060530bc9a4eff893c15e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for transformers_from_scratch-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 aa6e1f01d65c76532ec74e4db5cf1858dc85474ef746e352982d7aa28a328ce8
MD5 d6f686b37fdfe71178fb2a43697b1358
BLAKE2b-256 f662a7a36659a607d73c0ed673c24de061a45a7228f11b36dceda4791463e8d9

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