Skip to main content

Composable neural network components for building models in PyTorch.

Project description

composennent

PyPI version Python 3.8+ License: MIT

Composable neural network components for building models in PyTorch.

Composennent provides modular, reusable building blocks for constructing transformer-based models. Train GPT, BERT, and other architectures with minimal code.

Features

  • 🧩 Modular Components: Encoder, Decoder, Attention blocks that compose together
  • 🚀 Built-in Training: Pre-training and fine-tuning with a single method call
  • 📝 Multiple Architectures: GPT, BERT, Seq2Seq support out of the box
  • 🔧 Tokenizer Support: WordPiece and SentencePiece tokenizers included
  • Mixed Precision: Automatic mixed precision (AMP) support
  • 🎯 Instruction Tuning: Fine-tune models on instruction datasets (Alpaca format)

Installation

pip install composennent

For tokenizer support:

pip install composennent[tokenizers]

For development:

pip install composennent[dev]

Quick Start

Pre-train a GPT Model

import torch
from composennent.models import GPT
from composennent.nlp.tokenizers import SentencePieceTokenizer

# Create model
model = GPT(
    vocab_size=32000,
    latent_dim=512,
    num_heads=8,
    num_layers=6,
    max_seq_len=512,
)

# Load tokenizer
tokenizer = SentencePieceTokenizer.from_pretrained("tokenizer.model")

# Pre-train
texts = ["Your training data here...", ...]
model.pretrain(
    texts=texts,
    tokenizer=tokenizer,
    epochs=3,
    batch_size=16,
    device="cuda",
)

# Save
model.save("my_model.pt")

Fine-tune on Instructions

# Load pre-trained model
model = GPT.load("my_model.pt", device="cuda")

# Instruction data (Alpaca format)
instruction_data = [
    {
        "instruction": "What is the capital of France?",
        "input": "",
        "output": "The capital of France is Paris."
    },
    # ... more examples
]

# Fine-tune
model.fine_tune(
    data=instruction_data,
    tokenizer=tokenizer,
    epochs=2,
    lr=5e-5,
    mask_prompt=True,  # Only compute loss on outputs
)

Generate Text

prompt = tokenizer.encode("What is")
generated = model.generate(
    input_ids=prompt,
    max_length=100,
    temperature=0.8,
)
print(tokenizer.decode(generated[0].tolist()))

Modules

Module Description
composennent.modules Core building blocks (Encoder, Decoder, Block)
composennent.modules.attention Attention mechanisms and masks
composennent.models GPT, BERT, and other transformer models
composennent.nlp.tokenizers WordPiece and SentencePiece tokenizers
composennent.trainer Training utilities and trainer classes
composennent.modules.experts Mixture of Experts components
composennent.vision Vision transformer components
composennent.utils Utility functions

Training API

For more control over training, use the trainer classes directly:

from composennent.trainer import CausalLMTrainer, train

# Option 1: Use the train() convenience function
train(model, texts, tokenizer, model_type="causal_lm", epochs=5)

# Option 2: Use trainer class directly
trainer = CausalLMTrainer(model, tokenizer, device="cuda")
trainer.train(texts, epochs=5, batch_size=16)
trainer.save_checkpoint("checkpoint.pt")

Available trainers:

  • CausalLMTrainer - GPT-style next-token prediction
  • MaskedLMTrainer - BERT-style masked language modeling
  • Seq2SeqTrainer - Encoder-decoder models
  • MultiTaskTrainer - Multi-task learning (MLM + NSP)
  • CustomTrainer - Custom loss functions

Requirements

  • Python >= 3.8
  • PyTorch >= 2.0.0

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Install dev dependencies (pip install -e ".[dev]")
  4. Run tests (pytest)
  5. Run formatters (black . && ruff check .)
  6. Commit your changes (git commit -m 'Add amazing feature')
  7. Push to the branch (git push origin feature/amazing-feature)
  8. Open a Pull Request

License

MIT License - see LICENSE for details.

Links

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

composennent-0.4.4.tar.gz (58.5 kB view details)

Uploaded Source

Built Distribution

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

composennent-0.4.4-py3-none-any.whl (79.8 kB view details)

Uploaded Python 3

File details

Details for the file composennent-0.4.4.tar.gz.

File metadata

  • Download URL: composennent-0.4.4.tar.gz
  • Upload date:
  • Size: 58.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for composennent-0.4.4.tar.gz
Algorithm Hash digest
SHA256 2179e2a87a73cf89a5add15f431806e11b8a9299d45b912766914ab76b005e89
MD5 a7e0164cc58489183cb6e5e6b19e612b
BLAKE2b-256 ae6c4ccf596f684d8b2916308cc11b40eaf0f190edeaeff7411919ea7543c485

See more details on using hashes here.

File details

Details for the file composennent-0.4.4-py3-none-any.whl.

File metadata

  • Download URL: composennent-0.4.4-py3-none-any.whl
  • Upload date:
  • Size: 79.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for composennent-0.4.4-py3-none-any.whl
Algorithm Hash digest
SHA256 aab2588735451332fdb7ecdca4e885808c202cc35647fee546f5864e4b78424a
MD5 b298a8baca1f91257f6afa844c398b0d
BLAKE2b-256 2ab958f0bd8eebf6bcd85d91edda58d7d8b93efacdb53bbee4dc2a550b74940f

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