# 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
```python
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](https://github.com/nathanbreitsch/torchmocks/blob/main/tests/lightning_train.py).
```python
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.
Raw data
{
"_id": null,
"home_page": "",
"name": "torchmocks",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "deep learning,machine learning,neural networks,scientific computations,torchmocks",
"author": "Nathan Breitsch",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/92/47/b9ecb04c70b292b461ad03b74bc93e0d4331e49bfabeb74032833eb33407/torchmocks-0.1.0.tar.gz",
"platform": null,
"description": "# torchmocks\nTest pytorch code with minimal computational overhead.\n\n## Problem\nThe computational overhead of neural networks discourages thorough testing during development and within CI/CD pipelines.\n\n## Solution\nTorchmocks 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.\n\n## Install\n```\npip install torchmocks\n```\n\n## Example\n```python\nimport torch\nimport torchmocks\nfrom torchvision.models import resnet152\n\ndef test_mock_resnet():\n net = resnet152()\n torchmocks.mock(net)\n image_batch = torch.zeros(4, 3, 255, 255)\n output = net(image_batch)\n assert output.shape == (4, 1000)\n\n```\n\n## Pytorch Lightning Users\nYou can exercise most of your training code with torchmocks and the run_fast_dev option for Trainer.\nSee full example [here](https://github.com/nathanbreitsch/torchmocks/blob/main/tests/lightning_train.py).\n\n```python\ndef test_training():\n dataset = MockDataset()\n train_loader = torch.utils.data.DataLoader(dataset, batch_size=3)\n val_loader = torch.utils.data.DataLoader(dataset, batch_size=3)\n model = ExamplePytorchLightningModule()\n mock(model, debug=True)\n trainer = pytorch_lightning.Trainer(fast_dev_run=2)\n trainer.fit(model, train_loader, val_loader)\n```\n\n## Status\nThis 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.\nI'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.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Mocks pytorch modules so that test run faster",
"version": "0.1.0",
"project_urls": {
"Homepage": "https://github.com/nathanbreitsch/torchmocks"
},
"split_keywords": [
"deep learning",
"machine learning",
"neural networks",
"scientific computations",
"torchmocks"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "09fd899a8c29075e2d8dcd450349eb975299c52a4e42b9430a07b9db394f85b1",
"md5": "2d558f7e5cb228796d4ba3c77d839ba4",
"sha256": "a08ee08fdaad394f426780c28d64a871b5e79733daab150ca1e411b5978e0128"
},
"downloads": -1,
"filename": "torchmocks-0.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "2d558f7e5cb228796d4ba3c77d839ba4",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 8497,
"upload_time": "2023-06-08T01:47:44",
"upload_time_iso_8601": "2023-06-08T01:47:44.371745Z",
"url": "https://files.pythonhosted.org/packages/09/fd/899a8c29075e2d8dcd450349eb975299c52a4e42b9430a07b9db394f85b1/torchmocks-0.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "9247b9ecb04c70b292b461ad03b74bc93e0d4331e49bfabeb74032833eb33407",
"md5": "5fcdc5fafee84258d86588b59fac5f00",
"sha256": "68a2938d2fc98828bb5a97535d44dfbdcb04973a30c539e98f270cc3c1d861db"
},
"downloads": -1,
"filename": "torchmocks-0.1.0.tar.gz",
"has_sig": false,
"md5_digest": "5fcdc5fafee84258d86588b59fac5f00",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 7450,
"upload_time": "2023-06-08T01:47:45",
"upload_time_iso_8601": "2023-06-08T01:47:45.904645Z",
"url": "https://files.pythonhosted.org/packages/92/47/b9ecb04c70b292b461ad03b74bc93e0d4331e49bfabeb74032833eb33407/torchmocks-0.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-06-08 01:47:45",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "nathanbreitsch",
"github_project": "torchmocks",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "torchmocks"
}