Skip to main content

composennent

PyPI version Python 3.8+ License: MIT

Composable neural network composennents 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()))

Documentation

Detailed documentation is available in the docs/ directory:

Models

  • GPT: Decoder-only architecture with self-modifying memory.
  • Encoder-Decoder: Flexible T5/BART-style architecture.

Modules

Training

Features

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.

Metadata

Release files for composennent 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for composennent 0.5.0
File Size Uploaded Provenance Actions
composennent-0.5.0.tar.gz 89.4 kB Without provenance Details for composennent-0.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for composennent 0.5.0
File Interpreter ABI Platform Actions
composennent-0.5.0-py3-none-any.whl Python 3 none any Details for composennent-0.5.0-py3-none-any.whl

Total release size: 210.1 kB

Release files / composennent-0.5.0.tar.gz

Download URL composennent-0.5.0.tar.gz
Size 89.4 kB
Tags Source
SHA-256 checksum
How to use checksums
802771307bbc952cffd402bb420658242cf6086fecf602c363b72faff6fb851f
BLAKE2b-256 checksum
How to use checksums
03f7be4db46fefe678370a680bcc054f296607e4c9317a781f2fa2181e8b7206
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / composennent-0.5.0-py3-none-any.whl

Download URL composennent-0.5.0-py3-none-any.whl
Size 120.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
66c79265e87b2787b720ce85f0686529c570289771bcd79e95d6c808996be4e0
BLAKE2b-256 checksum
How to use checksums
c2bcfc9d88e28f4719cd6326493512048efa1eef642d37e687b6fb599d7c2989
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Security for composennent 0.5.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page