Skip to main content

A PyTorch utility library that streamlines the deep learning training pipeline.

Project description

torchaid

torchaid is a PyTorch utility library that provides structured abstractions and reusable components to streamline the deep learning training pipeline.

Features

  • Structured training abstractions — base classes for metrics and settings built on Pydantic v2
  • Training framework — a full training loop with mixed-precision support, automatic checkpointing, metric logging (CSV), and early stopping
  • Transformer modules — standard and relative-position-aware Transformer encoder layers with multi-head self-attention
  • Task templates — ready-to-use implementation for multi-label classification
  • Utilities — dataset splitting, random seed management, attention mask generation, and JSON-to-Pydantic loading
  • Learning rate schedulers — cosine decay with linear warm-up, and triangular2 cyclic scheduling

Requirements

  • Python 3.10+
  • PyTorch 2.0+

Installation

pip install torchaid

Or install from source:

git clone https://github.com/harunori-kawano/torchaid.git
cd torchaid
pip install -e .

Quick Start

1. Implement your model

Batches are plain dict[str, Any]. forward returns a (outputs, error) tuple — set error to a non-None value to signal a recoverable per-batch error; the framework will skip backpropagation and log it to stderr.

from torchaid import TaskModule, Mode
from typing import Any, Optional
from torch import nn

class MyModel(TaskModule):
    def __init__(self, vocab_size: int, num_classes: int):
        super().__init__()
        self.embed = nn.Embedding(vocab_size, 128)
        self.classifier = nn.Linear(128, num_classes)
        self.criterion = nn.CrossEntropyLoss()

    def forward(self, mode: Mode, batch: dict[str, Any]) -> tuple[dict[str, Any], Optional[Any]]:
        x = self.embed(batch["input_ids"]).mean(dim=1)
        logits = self.classifier(x)
        loss = self.criterion(logits, batch["labels"])
        if mode == Mode.TRAIN:
            return {"loss": loss}, None
        return {"loss": loss, "logits": logits}, None

2. Define metrics and settings

from torchaid import BaseMetrics, BaseSettings, BaseMetricCalculator
from typing import Optional, Any

class MyMetrics(BaseMetrics):
    train_loss: Optional[float] = None
    val_loss: Optional[float] = None

class MySettings(BaseSettings):
    batch_size: int = 32
    max_epoch_num: int = 10

class MyCalculator(BaseMetricCalculator[MyMetrics]):
    def __init__(self):
        super().__init__(MyMetrics())
        self._losses: list[float] = []

    def train_step(self, outputs: dict[str, Any], batch: dict[str, Any]) -> dict[str, Any]:
        loss = outputs["loss"].item()
        self._losses.append(loss)
        return {"loss": loss}

    def val_step(self, outputs: dict[str, Any], batch: dict[str, Any]) -> dict[str, Any]:
        return self.train_step(outputs, batch)

    def test_step(self, outputs: dict[str, Any], batch: dict[str, Any]) -> dict[str, Any]:
        return self.train_step(outputs, batch)

    def check(self) -> bool:
        import statistics
        self.metrics.train_loss = statistics.mean(self._losses)
        return True

    def test(self): pass

    def reset(self):
        self._losses.clear()

3. Train

import torch
from torchaid.core.trainer import TrainFramework

settings = MySettings(batch_size=32, max_epoch_num=10, device="cuda")
model = MyModel(vocab_size=1000, num_classes=5)
optimizer = torch.optim.AdamW(model.parameters(), lr=1e-4)

framework = TrainFramework(
    model=model,
    ls=settings,
    metric_calculator=MyCalculator(),
    optimizer=optimizer,
)

framework.train(train_dataset, val_dataset, save_dir="./outputs")

Module Overview

Module Description
torchaid.core Base classes (BaseMetrics, BaseSettings, TaskModule, BaseMetricCalculator, Mode) and TrainFramework
torchaid.templates.multilabel_classification Complete template for multi-label classification
torchaid.extras.modules.transformer Transformer, TransformerWithRelativePosition, and sub-modules
torchaid.extras.modules.positional_encoders PositionalEmbedding, RelativePositionEmbedding
torchaid.extras.utils split_dataset, set_random_seed, make_attention_mask, json_to_instance
torchaid.extras.scheduler get_cosine_scheduler, get_cycle_scheduler

Template: Multi-Label Classification

from torchaid.templates import multilabel_classification as mlc
from torchaid.core.trainer import TrainFramework
from torch import nn
import torch

backbone = nn.Sequential(nn.Linear(128, 64), nn.ReLU(), nn.Linear(64, 10))
model = mlc.MultiLabelClassification(backbone)
optimizer = torch.optim.Adam(model.parameters())

framework = TrainFramework(
    model=model,
    ls=settings,
    metric_calculator=mlc.MetricsCalculator(),
    optimizer=optimizer,
)

Extras

Cosine Decay Scheduler

from torchaid.extras.scheduler import get_cosine_scheduler

scheduler = get_cosine_scheduler(
    optimizer, warmup_steps=500, max_steps=10000
)

Dataset Split

from torchaid.extras.utils import split_dataset

train, val, test = split_dataset(dataset, ratios=[8, 1, 1], seed=42)

Relative Position Transformer

from torchaid.extras.modules.transformer import TransformerWithRelativePosition

layer = TransformerWithRelativePosition(
    hidden_size=256,
    intermediate_size=1024,
    num_attention_heads=8,
    dropout_probability=0.1,
    max_length=512,
    with_cls=True,
)

License

MIT License. See LICENSE for details.

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

torchaid-0.1.7.tar.gz (26.1 kB view details)

Uploaded Source

Built Distribution

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

torchaid-0.1.7-py3-none-any.whl (29.8 kB view details)

Uploaded Python 3

File details

Details for the file torchaid-0.1.7.tar.gz.

File metadata

  • Download URL: torchaid-0.1.7.tar.gz
  • Upload date:
  • Size: 26.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for torchaid-0.1.7.tar.gz
Algorithm Hash digest
SHA256 25014113f75d04eedfb302d7c8420540d3031f2dfba6672c2c8d95e499b9546b
MD5 12f299a50db3f9e2e4a8e7a5c463e892
BLAKE2b-256 d68e89125d0d3812535a87cb2653e845ed52c5ba3fe63223a0bb6b3d966e9f33

See more details on using hashes here.

File details

Details for the file torchaid-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: torchaid-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 29.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for torchaid-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 ddd1b6c1be71f24aedfe1cb75aeb8191f7d2af053e182a8aee545bdbfc5d0459
MD5 d83b6f31ddda304705a89e1240f88218
BLAKE2b-256 7984b17ceab3de167c6f3ee5f3e0f347b33ea1a49f5a4b93789477a31a2ac6b6

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