Skip to main content

transformer-builder

License Python Version Project Status


Make your own transformers with ease.

Transformers have become a popular choice for a wide range of Natural Language Processing (NLP) and deep learning tasks. The transformer-builder package allows you to create custom transformer models with ease, providing flexibility and modularity for your deep learning projects.


Features

  • Build custom transformer models with a user-friendly and flexible interface.
  • Configurable encoder and decoder blocks with support for custom self-attention mechanisms.
  • Encapsulated self-attention blocks that adapt to your specific use case.
  • Open-source and customizable to fit your project's requirements.

Installation

You can install transformer-builder using pip:

pip install transformer-builder

Usage

Here's an example of how to use Transformer Builder to create a custom model:

import torch
from torch import nn

from transformer_builder.attention import SelfAttention, MultiHeadAttention
from transformer_builder.layers import ResidualConnection

vocab_size = 16_000
embedding_dim = 512
num_heads = 8
d_head = embedding_dim // num_heads

vocab_size = 16_000
embedding_dim = 512
num_heads = 4
num_blocks = 3
d_head = embedding_dim // num_heads

blocks = [MultiHeadAttention(
    layer_before=nn.Linear(embedding_dim, embedding_dim),
    self_attention_heads=[
        SelfAttention(
            q_architecture=nn.Linear(embedding_dim, d_head),  # Default: nn.Identity
            k_architecture=nn.Linear(embedding_dim, d_head),
            v_architecture=nn.Linear(embedding_dim, d_head),
        ),
        SelfAttention(
            # This will calculate scaled dot product attention of original inputs
            # And pass the result to the linear layer
            layer_after=nn.Linear(embedding_dim, d_head),
        ),
        SelfAttention(
            layer_after=nn.Linear(embedding_dim, d_head),
        ),
        SelfAttention(
            # Now some exotic attention architecture
            layer_before=SelfAttention(),
            # The default value for self_attention_heads is single default head
            layer_after=MultiHeadAttention(
                layer_after=nn.Linear(embedding_dim, d_head),
            )
        )
    ]
)
    for _ in range(num_blocks)]

gpt = nn.Sequential(
    # nn.Embedding(vocab_size, embedding_dim), for simplicity, we will use random embeddings
    # ResidualConnection will add original input to the output of the module and apply normalization
    *[ResidualConnection(
        module=multi_head_attention,
        normalization=nn.LayerNorm(embedding_dim)
    ) for multi_head_attention in blocks],
)

gpt(torch.randn(8, embedding_dim))

Customization

With transformer-builder, you can customize each aspect of your blocks individually, allowing for fine-grained control over your model's architecture. The example above demonstrates how to configure the self-attention layer, layer normalization, and linear layers. You can go crazy and create encoder inside decoder inside self-attention!


Contributing

If you would like to contribute to this project, please follow our contribution guidelines.


Support and Feedback

If you have questions, encounter issues, or have feedback, please open an issue on our GitHub repository.


Acknowledgments

This project was inspired by the need for a flexible and customizable API for creating decoder blocks in deep learning models.


Author

MrKekovich


License

This project is licensed under the BSD-3-Clause License. See the LICENSE file for details.

Metadata

Release files for transformer-builder 0.1.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 transformer-builder 0.1.0
File Size Uploaded Provenance Actions
transformer_builder-0.1.0.tar.gz 8.0 kB Without provenance Details for transformer_builder-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for transformer-builder 0.1.0
File Interpreter ABI Platform Actions
transformer_builder-0.1.0-py3-none-any.whl Python 3 none any Details for transformer_builder-0.1.0-py3-none-any.whl

Total release size: 20.1 kB

Release files / transformer_builder-0.1.0.tar.gz

Download URL transformer_builder-0.1.0.tar.gz
Size 8.0 kB
Tags Source
SHA-256 checksum
How to use checksums
9f815face01898f2a03ec8cc829f612c6d13b1a12e768adfaa429da778fd154d
BLAKE2b-256 checksum
How to use checksums
0d53541ad62506616a6ffdf6122d5428c756bb7fd77c7d88ba1d14b92b984ae9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.12.0 Windows/11

Release files / transformer_builder-0.1.0-py3-none-any.whl

Download URL transformer_builder-0.1.0-py3-none-any.whl
Size 12.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0c8a1d9ecd41ced92bb1b6704ee659a5a93511bf535a643ca0cbe3debcab8117
BLAKE2b-256 checksum
How to use checksums
c6da45b437a0a074cfeea9c0660c452df415c80f9e3a7db667d033ad02e0aeb9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.12.0 Windows/11

Security for transformer-builder 0.1.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.

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