Skip to main content

ArchIt: A framework for base-and-head language models, and toolkit for converting in-place modifications of PyTorch objects into class code.

Project description

ArchIt: Automatic PyTorch architectures

ArchIt helps you rewrite PyTorch code for base models augmented at runtime, and lets you put heads on top of models without having to write dedicated task classes again and again.

archit.declaration: Convert a PyTorch instance into PyTorch architecture classes.

Recursively rewrite a class hierarchy (i.e. generate Python code of PyTorch architectures) so that in-place modifications are now defined explicitly.

As an example of this: I'm involved in two projects where I replace the embedding matrix of a RobertaForMaskedLM by a new class. If I want to load a checkpoint of that model, I need to write a new class definition for the RobertaEmbeddings that uses my replacement of the Embedding, a new RobertaModel using the new embeddings, and a new RobertaForMaskedLM using that new model. ArchIt writes that code for you.

archit.instantiation: Add heads to a base model, without needing to write YourModelForThatTask classes

Why in the heavens do we need separate classes for RobertaForTokenClassification and DebertaForTokenclassification? The base model encodes tokens into embeddings, and the head, which only cares about the resulting embeddings, converts them to logits. Separation of concerns. There is no need to rewrite "model-with-head" classes over and over again for each model augmentation.

Installation

pip install "archit @ git+https://github.com/bauwenst/ArchIt"

Usage

Minimal working example to show what ArchIt does:

from transformers import RobertaForMaskedLM
import torch

class CoolNewEmbeddingMatrix(torch.nn.Module):
    def forward(self, input_ids):
        pass

model_with_head = RobertaForMaskedLM.from_pretrained("roberta-base")
model_with_head.roberta.embeddings.word_embeddings = CoolNewEmbeddingMatrix()
# ^--- This works, but there is no class definition declaring word_embeddings as a CoolNewEmbeddingMatrix.


from archit import printDifference
printDifference(model_with_head, RobertaForMaskedLM)  # Outputs Python code for 3 new classes.

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

archit-2025.4.1.tar.gz (37.2 kB view details)

Uploaded Source

Built Distribution

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

archit-2025.4.1-py3-none-any.whl (40.2 kB view details)

Uploaded Python 3

File details

Details for the file archit-2025.4.1.tar.gz.

File metadata

  • Download URL: archit-2025.4.1.tar.gz
  • Upload date:
  • Size: 37.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.5 cpython/3.13.12 HTTPX/0.28.1

File hashes

Hashes for archit-2025.4.1.tar.gz
Algorithm Hash digest
SHA256 d8b2fded0d3f0678ab593689602fc2737978166d2fa8ac3ce92b01ff63533b76
MD5 b5b13ae7921d92d6e8199008bcc72a71
BLAKE2b-256 86a1939282fa56e30a9e502bcbcdd078c0fe8fa8fcfa598b4168b6c40578b740

See more details on using hashes here.

File details

Details for the file archit-2025.4.1-py3-none-any.whl.

File metadata

  • Download URL: archit-2025.4.1-py3-none-any.whl
  • Upload date:
  • Size: 40.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.5 cpython/3.13.12 HTTPX/0.28.1

File hashes

Hashes for archit-2025.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c1410e7573c4c07d3070a8f58448edda6410ebbe763b855d1c90cb7280844224
MD5 a096632f2de6311a266e3b650bfd1402
BLAKE2b-256 256b7bd92272fddd28d451a407ebd2a56ed8f0f5dc43c350bc1f74f55cc19a41

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