Skip to main content

torchmocks

Test pytorch code with minimal computational overhead.

Problem

The computational overhead of neural networks discourages thorough testing during development and within CI/CD pipelines.

Solution

Torchmocks replaces common building blocks (such as torch.nn.Conv2d) with replicas that only keep track of tensor shapes and device location. This is often the only information that we need to check to ensure proper function of pytorch code.

Install

pip install torchmocks

Example

import torch
import torchmocks
from torchvision.models import resnet152

def test_mock_resnet():
    net = resnet152()
    torchmocks.mock(net)
    image_batch = torch.zeros(4, 3, 255, 255)
    output = net(image_batch)
    assert output.shape == (4, 1000)

Pytorch Lightning Users

You can exercise most of your training code with torchmocks and the run_fast_dev option for Trainer. See full example here.

def test_training():
    dataset = MockDataset()
    train_loader = torch.utils.data.DataLoader(dataset, batch_size=3)
    val_loader = torch.utils.data.DataLoader(dataset, batch_size=3)
    model = ExamplePytorchLightningModule()
    mock(model, debug=True)
    trainer = pytorch_lightning.Trainer(fast_dev_run=2)
    trainer.fit(model, train_loader, val_loader)

Status

This is a work in progress and only a handful of torch modules have been mocked. Modules that have not been mocked will run their normal computation during the forward pass. I'm also exploring other ways to do shape inference in order to mock operations that don't appear in the module tree. Let me know if you have any ideas.

Metadata

Release files for torchmocks 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 torchmocks 0.1.0
File Size Uploaded
torchmocks-0.1.0.tar.gz 7.5 kB Details

Built distribution (wheel)

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

Total release size: 15.9 kB

Release files / torchmocks-0.1.0.tar.gz

Download URL torchmocks-0.1.0.tar.gz
Size 7.5 kB
Tags Source
SHA-256 checksum
How to use checksums
68a2938d2fc98828bb5a97535d44dfbdcb04973a30c539e98f270cc3c1d861db
BLAKE2b-256 checksum
How to use checksums
9247b9ecb04c70b292b461ad03b74bc93e0d4331e49bfabeb74032833eb33407
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

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

Download URL torchmocks-0.1.0-py3-none-any.whl
Size 8.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a08ee08fdaad394f426780c28d64a871b5e79733daab150ca1e411b5978e0128
BLAKE2b-256 checksum
How to use checksums
09fd899a8c29075e2d8dcd450349eb975299c52a4e42b9430a07b9db394f85b1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

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